Annotation of loncom/homework/bridgetask.pm, revision 1.116

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.116   ! albertel    4: # $Id: bridgetask.pm,v 1.115 2006/03/02 21:20:30 albertel Exp $
1.1       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: ###
                     29: 
                     30: 
                     31: package Apache::bridgetask; 
                     32: 
                     33: use strict;
                     34: use Apache::lonnet;
                     35: use Apache::File();
                     36: use Apache::lonmenu;
                     37: use Apache::lonlocal;
                     38: use Apache::lonxml;
1.37      albertel   39: use Apache::slotrequest();
1.1       albertel   40: use Time::HiRes qw( gettimeofday tv_interval );
1.9       albertel   41: 
1.1       albertel   42: BEGIN {
                     43:     &Apache::lonxml::register('Apache::bridgetask',('Task','IntroParagraph','Dimension','Instance','InstanceText','Criteria','ClosingParagraph'));
                     44: }
                     45: 
1.9       albertel   46: sub initialize_bridgetask {
                     47:     # id of current Dimension, 0 means that no dimension is current 
                     48:     # (inside <Task> only)
                     49:     $Apache::bridgetask::dimension='';
                     50:     # list of all Dimension ids seen
                     51:     @Apache::bridgetask::dimensionlist=();
1.20      albertel   52:     # mandatory attribute of all Dimensions seen
                     53:     %Apache::bridgetask::dimensionmandatory=();
1.9       albertel   54:     # list of all current Instance ids
                     55:     @Apache::bridgetask::instance=();
                     56:     # list of all Instance ids seen in this problem
                     57:     @Apache::bridgetask::instancelist=();
1.15      albertel   58:     # key of queud user data that we are currently grading
                     59:     $Apache::bridgetask::queue_key='';
1.9       albertel   60: }
                     61: 
1.4       albertel   62: sub proctor_check_auth {
1.81      albertel   63:     my ($slot_name,$slot,$type)=@_;
1.11      albertel   64:     my $user=$env{'form.proctorname'};
                     65:     my $domain=$env{'form.proctordomain'};
1.4       albertel   66:     
                     67:     my @allowed=split(",",$slot->{'proctor'});
                     68:     foreach my $possible (@allowed) {
                     69: 	my ($puser,$pdom)=(split('@',$possible));
                     70: 	if ($puser eq $user && $pdom eq $domain) {
1.72      albertel   71: 	    my $authenticated=0;
                     72: 	    if ( $slot->{'secret'} =~ /\S/ &&
                     73: 		 $env{'form.proctorpassword'} eq $slot->{'secret'} ) {
                     74: 		$authenticated=1;
                     75: 	    } else {
                     76: 		
                     77: 		my $authhost=&Apache::lonnet::authenticate($puser,$env{'form.proctorpassword'},$pdom);
                     78: 		if ($authhost ne 'no_host') {
                     79: 		    $authenticated=1;
                     80: 		}
                     81: 	    }
1.81      albertel   82: 	    if ($authenticated && $type eq 'Task') {
1.93      albertel   83: 		# increment version
1.23      albertel   84: 		my $version=
1.90      albertel   85: 		    $Apache::lonhomework::history{'resource.0.version'};
                     86: 		$version++;
1.93      albertel   87: 
                     88: 		#clean out all current results
                     89: 		foreach my $key (keys(%Apache::lonhomework::history)) {
                     90: 		    if ($key=~/^resource\.0\./) {
                     91: 			$Apache::lonhomework::results{$key}='';
                     92: 		    }
                     93: 		}
                     94: 		
                     95: 		#setup new version and who did it
1.90      albertel   96: 		$Apache::lonhomework::results{'resource.0.version'}=$version;
1.89      albertel   97: 		$Apache::lonhomework::results{"resource.$version.0.checkedin"}=
1.4       albertel   98: 		    $user.'@'.$domain;
1.89      albertel   99: 		$Apache::lonhomework::results{"resource.$version.0.checkedin.slot"}=
1.22      albertel  100: 		    $slot_name;
1.93      albertel  101: 
1.4       albertel  102: 		return 1;
1.81      albertel  103: 	    } elsif ($authenticated && $type eq 'problem') {
1.90      albertel  104: 		&Apache::lonxml::debug("authed $slot_name");
1.81      albertel  105: 		$Apache::lonhomework::results{"resource.0.checkedin"}=
                    106: 		    $user.'@'.$domain;
                    107: 		$Apache::lonhomework::results{"resource.0.checkedin.slot"}=
                    108: 		    $slot_name;
1.4       albertel  109: 	    }
                    110: 	}
                    111:     }
                    112:     return 0;
                    113: }
                    114: 
1.25      albertel  115: sub get_version {
1.29      albertel  116:     my ($version,$previous);
1.25      albertel  117:     if ($env{'form.previousversion'} && 
1.36      albertel  118: 	$env{'form.previousversion'} ne 'current' &&
1.89      albertel  119: 	defined($Apache::lonhomework::history{'resource.'.$env{'form.previousversion'}.'.0.status'})) {
1.29      albertel  120: 	$version=$env{'form.previousversion'};
                    121: 	$previous=1;
                    122:     } else {
1.89      albertel  123: 	$version=$Apache::lonhomework::history{'resource.0.version'};
1.29      albertel  124: 	$previous=0;
                    125:     }
                    126:     if (wantarray) {
                    127: 	return ($version,$previous);
1.25      albertel  128:     }
1.29      albertel  129:     return $version;
1.25      albertel  130: }
                    131: 
1.8       albertel  132: sub add_previous_version_button {
1.25      albertel  133:     my ($status)=@_;
1.8       albertel  134:     my $result;
1.89      albertel  135:     if ($Apache::lonhomework::history{'resource.0.version'} eq '') {
1.25      albertel  136: 	return '';
                    137:     }
1.89      albertel  138:     if ($Apache::lonhomework::history{'resource.0.version'} < 2 &&
1.29      albertel  139: 	$status ne 'NEEDS_CHECKIN') {
1.25      albertel  140: 	return '';
                    141:     }
1.29      albertel  142:     my $version=&get_version();
                    143:     if ($env{'form.previousversion'} ne '' &&
                    144: 	$env{'form.previousversion'} eq $version) {
                    145: 	$result.="<h3>".&mt("Showing previous version [_1]",$version).
                    146: 	    "</h3>\n";
                    147:     }
                    148:     my @to_show;
1.89      albertel  149:     foreach my $test_version (1..$Apache::lonhomework::history{'resource.0.version'}) {
                    150: 	if (defined($Apache::lonhomework::history{'resource.'.$test_version.'.0.status'})) {
1.29      albertel  151: 	    push(@to_show,$test_version);
                    152: 	}
                    153:     }
                    154:     my $list='<option>'.
                    155: 	join("</option>\n<option>",@to_show).
                    156: 	     "</option>\n";
1.36      albertel  157:     $list.='<option value="current">'.&mt('Current').'</option>';
1.115     albertel  158:     $result.='<form name="getprevious" method="post" action="';
1.29      albertel  159:     my $uri=$env{'request.uri'};
                    160:     if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    161:     $result.=$uri.'">'.
                    162: 	&mt(' Show a previously done version: [_1]','<select onchange="this.form.submit()" name="previousversion">
                    163: <option>'.&mt('Pick one').'</option>
                    164: '.$list.'
                    165: </select>')."</form>";
1.8       albertel  166:     return $result;
                    167: }
                    168: 
1.13      albertel  169: sub add_grading_button {
1.59      albertel  170:     my (undef,$cid)=&Apache::lonxml::whichuser();
                    171:     my $cnum=$env{'course.'.$cid.'.num'};
                    172:     my $cdom=$env{'course.'.$cid.'.domain'};
                    173:     my %sections;
                    174:     my $numsections=&Apache::loncommon::get_sections($cdom,$cnum,\%sections);
                    175:     my $size=5;
                    176:     if (scalar(keys(%sections)) < 3) {
                    177: 	$size=scalar(keys(%sections))+2;
                    178:     }
                    179:     my $sec_select = '<select multiple name="chosensections" size="'.$size.'">'."\n";
                    180:     $sec_select .= "<option value='all' selected='selected'>all</option>\n";
                    181:     foreach my $sec (sort {lc($a) cmp lc($b)} (keys(%sections))) {
                    182: 	$sec_select .= "<option name=\"$sec\">$sec</option>\n";
                    183:     }
                    184:     $sec_select .= "<option value='none'>none</option></select>\n";
                    185:     
1.29      albertel  186:     my $result=' <input type="submit" name="gradeasubmission" value="'.
1.13      albertel  187: 	&mt("Get a submission to grade").'" />';
                    188:     $result.='<input type="hidden" name="grade_target" value="webgrade" />';
1.40      albertel  189:     if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.34      albertel  190: 	my ($entries,$ready,$locks)=&get_queue_counts('gradingqueue');
1.106     albertel  191: 	$result.='<table><tr>';
                    192: 	$result.='<td rowspan="4">Specify a section: </td><td rowspan="4">'.$sec_select.'</td>';
                    193: 	$result.='<td>'.' <input type="submit" name="reviewagrading" value="'.
                    194: 	    &mt("Select an entry from the grading queue:").'" /> ';
1.34      albertel  195: 
1.107     albertel  196: 	$result.= &mt("[_1] entries, [_2] ready, [_3] being graded",$entries,$ready,$locks).' </td></tr>'."\n";
1.34      albertel  197: 
                    198: 	($entries,$ready,$locks)=&get_queue_counts('reviewqueue');
1.106     albertel  199: 	$result.='<tr><td>'.
                    200: 	    ' <input type="submit" name="reviewasubmission" value="'.
                    201: 	    &mt("Select an entry from the review queue:").'" /> ';
                    202: 	$result.=&mt("[_1] entries, [_2] ready, [_3] being graded",
                    203: 		     $entries,$ready,$locks).'</td></tr>'."\n";
                    204: 	$result.='<tr><td> <input type="submit" name="regradeasubmission" value="'.
                    205: 	    &mt("List of user's grade status").'" /> </td></tr></table>'."\n";
1.105     albertel  206: 	$result.='<p> <input type="submit" name="regradeaspecificsubmission" value="'.
1.106     albertel  207: 	    &mt("Regrade specific user:").'" />'."\n";
1.105     albertel  208: 	$result.='<input type="text" size="12" name="gradinguser" />';
                    209: 	$result.=&Apache::loncommon::select_dom_form($env{'user.domain'},
                    210: 						     'gradingdomain');
                    211: 	$result.=' '.
                    212: 	    &Apache::loncommon::selectstudent_link('gradesubmission',
                    213: 						   'gradinguser',
                    214: 						   'gradingdomain');
                    215: 	$result.=&Apache::loncommon::studentbrowser_javascript();
                    216:   }
1.13      albertel  217:     return $result;
                    218: }
                    219: 
1.22      albertel  220: sub add_request_another_attempt_button {
1.38      albertel  221:     my ($text)=@_;
                    222:     if (!$text) { $text="Request another attempt"; }
1.25      albertel  223:     my $result;
1.36      albertel  224:     my $symb=&Apache::lonnet::symbread();
1.37      albertel  225:     my ($slot_name,$slot)=&Apache::slotrequest::check_for_reservation($symb);
1.38      albertel  226:     my $action='get_reservation';
1.37      albertel  227:     if ($slot_name) {
1.38      albertel  228: 	$text="Change reservation.";
                    229: 	$action='change_reservation';
1.37      albertel  230: 	my $description=&Apache::slotrequest::get_description($slot_name,
                    231: 							      $slot);
                    232: 	$result.=(<<STUFF);
                    233: <p> Will be next available: $description </p>
                    234: STUFF
                    235:     }
1.38      albertel  236:     
                    237:     if ($env{'request.enc'}) { $symb=&Apache::lonenc::encrypted($symb); }
                    238:     $symb=&Apache::lonnet::escape($symb);
1.115     albertel  239:     $result.='<form method="post" action="/adm/slotrequest">'.
1.38      albertel  240: 	'<input type="hidden" name="symb" value="'.$symb.'" />'.
                    241: 	'<input type="hidden" name="command" value="'.$action.'" />'.
                    242: 	'<input type="submit" name="requestattempt" value="'.
                    243: 	&mt($text).'" />'.
                    244: 	'</form>';
1.25      albertel  245:     return $result;
1.22      albertel  246: }
                    247: 
1.30      albertel  248: sub preserve_grade_info {
                    249:     my $result;
                    250:     # if we are viewing someone else preserve that info
                    251:     if (defined $env{'form.grade_symb'}) {
                    252: 	foreach my $field ('symb','courseid','domain','username') {
                    253: 	    $result .= '<input type="hidden" name="grade_'.$field.
                    254: 		'" value="'.$env{"form.grade_$field"}.'" />'."\n";
                    255: 	}
                    256:     }
                    257:     return $result;
                    258: }
                    259: 
1.53      albertel  260: sub style {
                    261:     return (<<STYLE);
                    262: <style type="text/css">
1.116   ! albertel  263: .LC_fail, .LC_pass, .LC_neutral {
1.53      albertel  264:     position: relative;
                    265:     margin : 5px;
                    266:     margin-bottom :10px;
                    267:     padding : 4px;
                    268:     padding-left : 75px;
                    269: 	border : thin solid;
                    270:     font-weight : bolder;
                    271:     font-size: smaller;
                    272:     font-family: Arial;
                    273:     background-color : rgb(255,240,225);
                    274: }
                    275: 
1.116   ! albertel  276: .LC_fail h4, .LC_pass h4 {
1.53      albertel  277: 	position:absolute;
                    278: 	left: -4px;
                    279: 	top: -8px;
                    280: 	padding:2px;
                    281: 	margin:0;
                    282: 	background-color : rgb(255,240,225);
                    283: 	border : thin solid;
                    284: }
                    285: 
1.116   ! albertel  286: .LC_fail {
1.53      albertel  287: 	color: red;
                    288: }
                    289: 
1.116   ! albertel  290: .LC_pass {
1.53      albertel  291:     color : green;
                    292: }
                    293: 
1.116   ! albertel  294: .LC_neutral {
1.53      albertel  295: 	color : blue;
                    296: }
                    297: 
1.116   ! albertel  298: .LC_question {
1.53      albertel  299: 	border : thin solid black;
                    300: 	padding : 4px;
                    301: 	margin-bottom : 1em;
                    302: }
                    303: 
1.116   ! albertel  304: .LC_grade {
1.53      albertel  305: 	font-size: x-large;
                    306: 	font-family: Arial;
                    307: 	position:absolute;
                    308: 	left: 5px;
                    309: 	top: -5px;
                    310: 	width: 70px;
                    311: }
                    312: 
                    313: div#feedback h1 {
                    314:     color : inherit;
                    315: }
                    316: </style>
                    317: STYLE
                    318: 
                    319: }
                    320: 
1.54      albertel  321: sub show_task {
                    322:     my ($status,$previous)=@_;
                    323:     if (!$previous && (
                    324: 		       ( $status eq 'CLOSED' ) ||
                    325: 		       ( $status eq 'BANNED') ||
                    326: 		       ( $status eq 'UNAVAILABLE') ||
                    327: 		       ( $status eq 'NOT_IN_A_SLOT') ||
                    328: 		       ( $status eq 'NEEDS_CHECKIN') ||
                    329: 		       ( $status eq 'WAITING_FOR_GRADE') ||
                    330: 		       ( $status eq 'INVALID_ACCESS') )) {
                    331: 	return 0;
                    332:     }
1.64      albertel  333:     if ($env{'form.donescreen'}) { return 0; }
1.54      albertel  334:     return 1;
                    335: }
                    336: 
                    337: sub internal_location {
                    338:     my ($id)=@_;
                    339:     return '<!-- LONCAPA_INTERNAL_ADD_TASK_STATUS'.$id.' -->';
                    340: }
                    341: 
1.60      albertel  342: sub submission_time_stamp {
                    343:     my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                    344:     my $submissiontime;
1.89      albertel  345:     my $version=$Apache::lonhomework::history{'resource.0.version'};
1.60      albertel  346:     for (my $v=$Apache::lonhomework::history{'version'};$v>0;$v--) {
                    347: 	if (defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.bridgetask.portfiles'})) {
                    348: 	    $submissiontime=$Apache::lonhomework::history{$v.':timestamp'};
                    349: 	}
                    350:     }
                    351:     my $result;
                    352:     if ($submissiontime) {
1.89      albertel  353: 	my $slot_name=$Apache::lonhomework::history{'resource.'.$version.'.0.checkedin.slot'};
1.60      albertel  354: 	my %slot=&Apache::lonnet::get_slot($slot_name);
                    355: 	my $diff = $slot{'endtime'} - $submissiontime;
1.71      albertel  356: 	my ($color,$when)=('#FF6666','after');
                    357: 	if ($diff > 0) { ($color,$when)=('#336600','before'); }
1.60      albertel  358: 	my $info;
                    359: 	if ($diff%60) { $info=($diff%60).' seconds'; }
                    360: 	$diff=int($diff/60);
                    361: 	if ($diff%60) { $info=($diff%60).' minutes '.$info; }
                    362: 	$diff=int($diff/60);
                    363: 	if ($diff) {    $info=$diff.' hours '.$info; }
                    364: 	$result='<p><font color="'.$color.'">'.
                    365: 	    &mt('Student submitted [_1] [_2] the deadline. 
                    366:                  (Submission was at [_3], end of period was [_4].)',
                    367: 		$info,$when,scalar(localtime($submissiontime)),
                    368: 		scalar(localtime($slot{'endtime'}))).
                    369: 		'</font></p>';
                    370:     }
                    371:     return $result;
                    372: }
                    373: 
1.70      albertel  374: sub webgrade_standard_info {
                    375:     my ($version)=&get_version();
                    376:     my (undef,undef,$udom,$uname) = &Apache::lonxml::whichuser();
                    377:     my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
1.116   ! albertel  378:     my $file_list="<ul class=\"LC_GRADE_handininfo\">\n";
1.70      albertel  379:     foreach my $partial_file (split(',',$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"})) {
                    380: 	my $file=$file_url.$partial_file;
                    381: 	$file=~s|/+|/|g;
                    382: 	&Apache::lonnet::allowuploaded('/adm/bridgetask',$file);
1.73      albertel  383: 	$file_list.='<li><nobr><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
1.109     albertel  384: 	    &Apache::loncommon::icon($file).'" border="0"> '.$file.
1.70      albertel  385: 	    '</a></nobr></li>'."\n";
                    386:     }
                    387:     $file_list.="</ul>\n";
                    388: 
                    389:     my %lt=&Apache::lonlocal::texthash('done' => 'Done',
                    390: 				       'stop' => 'Stop',
                    391: 				       );
                    392: 				       
                    393:     my $result=<<INFO;
1.116   ! albertel  394:   <div class="LC_GRADE_maincontrols">
1.111     albertel  395:     <input type="submit" name="next" value="$lt{'done'}" />
                    396:     <input type="submit" name="stop" value="$lt{'stop'}" />
                    397:   </div>
1.70      albertel  398:   $file_list
                    399: INFO
                    400:     return $result;
                    401: }
                    402: 
1.1       albertel  403: sub start_Task {
1.87      albertel  404:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel  405: 
1.4       albertel  406:     my ($status,$accessmsg,$slot);
1.16      albertel  407:     if ($target ne 'webgrade') {
1.70      albertel  408: 	&Apache::structuretags::init_problem_globals('Task');
1.16      albertel  409: 	&Apache::structuretags::initialize_storage();
                    410: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.74      albertel  411: 	if ($env{'request.state'} eq 'construct') {
                    412: 	    &Apache::structuretags::setup_rndseed($safeeval);
                    413: 	}
1.16      albertel  414:     } 
                    415: 
1.4       albertel  416:     $Apache::lonhomework::parsing_a_task=1;
1.1       albertel  417:     #should get back a <html> or the neccesary stuff to start XML/MathML
                    418:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    419: 	&Apache::structuretags::page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.16      albertel  420:     
1.74      albertel  421:     if ($target eq 'web' && $env{'request.state'} ne 'construct') {
1.13      albertel  422: 	if ($Apache::lonhomework::modifygrades) {
1.115     albertel  423: 	    $body_tag_start.='<form name="gradesubmission" method="post" action="';
1.13      albertel  424: 	    my $uri=$env{'request.uri'};
                    425: 	    if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    426: 	    $body_tag_start.=$uri.'">'.&add_grading_button()."</form>";
1.38      albertel  427: 	    my $symb=&Apache::lonnet::symbread();
1.40      albertel  428: 	    if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.115     albertel  429: 		$body_tag_start.='<form method="post" name="slotrequest" action="/adm/slotrequest">'.
1.40      albertel  430: 		    '<input type="hidden" name="symb" value="'.$symb.'" />'.
                    431: 		    '<input type="hidden" name="command" value="showslots" />'.
                    432: 		    '<input type="submit" name="requestattempt" value="'.
                    433: 		    &mt('Show Slot list').'" />'.
                    434: 		    '</form>';
1.108     albertel  435: 		my $target_id = 
                    436: 		    &Apache::lonstathelpers::make_target_id({symb => $symb,
                    437: 							     part => '0'});
1.115     albertel  438: 		$body_tag_start.='<form method="post" name="gradingstatus" action="/adm/statistics">'.
1.108     albertel  439: 		    '<input type="hidden" name="problemchoice" value="'.$target_id.'" />'.
                    440: 		    '<input type="hidden" name="reportSelected" value="grading_analysis" />'.
                    441: 		    '<input type="submit" name="grading" value="'.
                    442: 		    &mt('Show Grading Status').'" />'.
                    443: 		    '</form>';
1.40      albertel  444: 	    }
1.13      albertel  445: 	}
1.8       albertel  446:     }
1.74      albertel  447:     if ($target eq 'web' && $env{'request.state'} eq 'construct') {
                    448: 	$form_tag_start.=&Apache::structuretags::problem_web_to_edit_header($env{'form.rndseed'});
                    449:     }
1.21      albertel  450:     if ($target eq 'web' || ($target eq 'grade' && !$env{'form.webgrade'}) || $target eq 'answer' ||
1.1       albertel  451: 	$target eq 'tex') {
1.29      albertel  452: 	my ($version,$previous)=&get_version();
1.14      albertel  453: 	($status,$accessmsg,my $slot_name,$slot) = 
1.81      albertel  454: 	    &Apache::lonhomework::check_slot_access('0','Task');
1.9       albertel  455: 	push(@Apache::inputtags::status,$status);
1.14      albertel  456: 	$Apache::inputtags::slot_name=$slot_name;
1.1       albertel  457: 	my $expression='$external::datestatus="'.$status.'";';
1.89      albertel  458: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$version.0.solved"}.'";';
1.1       albertel  459: 	&Apache::run::run($expression,$safeeval);
                    460: 	&Apache::lonxml::debug("Got $status");
1.25      albertel  461: 	$body_tag_start.=&add_previous_version_button($status);
1.54      albertel  462: 	if (!&show_task($status,$previous)) {
1.87      albertel  463: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.1       albertel  464: 	    if ( $target eq "web" ) {
1.4       albertel  465: 		$result.= $head_tag_start.'</head>'.$body_tag_start;
1.74      albertel  466: 		if ($env{'request.state'} eq 'construct') {
                    467: 		    $result.=$form_tag_start;
                    468: 		}
1.4       albertel  469: 		my $msg;
1.1       albertel  470: 		if ($status eq 'UNAVAILABLE') {
                    471: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3       albertel  472: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    473: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.38      albertel  474: 		    $msg.=&add_request_another_attempt_button("Sign up for time to work.");
1.4       albertel  475: 		} elsif ($status eq 'NEEDS_CHECKIN') {
                    476: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
                    477: 			'</h1>'.&proctor_validation_screen($slot);
1.22      albertel  478: 		} elsif ($status eq 'WAITING_FOR_GRADE') {
                    479: 		    $msg.='<h1>'.&mt('Your submission is in the grading queue.').'</h1>';
1.64      albertel  480: 		} elsif ($env{'form.donescreen'}) {
                    481: 		    my $title=&Apache::lonnet::gettitle();
1.67      albertel  482: 		    my @files=split(',',$Apache::lonhomework::history{'resource.'.$version.'.0.bridgetask.portfiles'});
1.114     albertel  483: 		    my (undef,undef,$domain,$user)=
                    484: 			&Apache::lonxml::whichuser();
                    485: 		    my $files = '<ul>';
                    486: 		    foreach my $file (@files) {
                    487: 			my $url="/uploaded/$domain/$user/portfolio$file";
                    488: 			if (! &Apache::lonnet::stat_file($url)) {
                    489: 			    $file = &mt('<font color="red"> Nonexistant file:</font> <tt>[_1]</tt>',$file);
                    490: 			} else {
                    491: 			    $file = '<tt>'.$file.'</tt>';
                    492: 			}
                    493: 			$files .= '<li>'.$file.'</li>';
                    494: 		    }
                    495: 		    $files.='</ul>';
                    496: 
1.64      albertel  497: 		    $result.=<<DONESCREEN;
                    498: <h2>$title</h2>
                    499: <p> Files submitted: $files </p>
1.67      albertel  500: <p> You are now done with this Bridge Task </p>
1.64      albertel  501: <hr />
                    502: <p> <a href="/adm/logout">Logout</a> </p>
                    503: <p> <a href="/adm/roles">Change to a different course</a> </p>
                    504: DONESCREEN
1.1       albertel  505: 		} elsif ($status ne 'NOT_YET_VIEWED') {
                    506: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
                    507: 		}
                    508: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
                    509: 		    $msg.='The problem '.$accessmsg;
                    510: 		}
                    511: 		$result.=$msg.'<br />';
                    512: 	    } elsif ($target eq 'tex') {
                    513: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
                    514: 		if ($status eq 'UNAVAILABLE') {
                    515: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
                    516: 		} else {
                    517: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
                    518: 		}
1.22      albertel  519: 	    } elsif ($target eq 'grade' && !$env{'form.webgrade'}) {
1.4       albertel  520: 		if ($status eq 'NEEDS_CHECKIN') {
1.83      albertel  521: 		    if(&proctor_check_auth($slot_name,$slot,'Task')
                    522: 		       && defined($Apache::inputtags::slot_name)) {
1.77      albertel  523: 			my $result=&add_to_queue('gradingqueue',
1.82      albertel  524: 						 [$Apache::inputtags::slot_name]);
1.77      albertel  525: 			&Apache::lonxml::debug("add_to_queue said $result");
                    526: 		    }
1.4       albertel  527: 		}
1.1       albertel  528: 	    }
                    529: 	} elsif ($target eq 'web') {
                    530: 	    my $name= &Apache::structuretags::get_resource_name($parstack,$safeeval);
                    531: 	    $result.="$head_tag_start<title>$name</title></head>
1.53      albertel  532:               $body_tag_start \n".&style();
1.36      albertel  533: 	    
1.57      albertel  534: 	    $result.=&preserve_grade_info();
                    535: 	    $result.=&internal_location();
1.36      albertel  536: 	    $result.=$form_tag_start.
                    537: 		'<input type="hidden" name="submitted" value="yes" />';
1.54      albertel  538: 	    &Apache::lonxml::startredirection();
1.1       albertel  539: 	}
1.21      albertel  540:     } elsif ( ($target eq 'grade' && $env{'form.webgrade'}) ||
                    541: 	      $target eq 'webgrade') {
1.32      albertel  542: 	my $webgrade='yes';
1.21      albertel  543: 	if ($target eq 'webgrade') {
1.110     albertel  544: 	    $result.=$head_tag_start.'</head>'.$body_tag_start;
1.49      albertel  545: 	    #$result.='<br />Review'.&show_queue('reviewqueue');
                    546: 	    #$result.='<br />Grade'.&show_queue('gradingqueue');
1.30      albertel  547: 	}
1.33      albertel  548: 	# FIXME Blast! still need to reorg this, need to reshow the
                    549:         #       queue being reviewed once done with the grade pass...
                    550:         #       Hrrm, vaildation pass should perhaps say 'not_locked'
                    551:         #       perhaps do a search if there is a key that is mine and if
                    552:         #       there isn't reshow the queue....
1.105     albertel  553: 	my ($todo,$status_code,$msg)=&get_key_todo($target);
1.33      albertel  554: 
                    555: 	if ($todo) {
                    556: 	    &setup_env_for_other_user($todo,$safeeval);
                    557: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
                    558: 	    $result.="\n".'<table><tr><td>Found '.
                    559: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
                    560: 	    $form_tag_start.=
                    561: 		'<input type="hidden" name="gradingkey" value="'.
                    562: 		&Apache::lonnet::escape($todo).'" />';
                    563: 	    $Apache::bridgetask::queue_key=$todo;
                    564: 	    &Apache::structuretags::initialize_storage();
                    565: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.110     albertel  566: 	    if ($target eq 'webgrade' && $status_code eq 'selected') {
                    567: 		$form_tag_start.=
                    568: 		    '<input type="hidden" name="queuemode" value="selected" />';
1.33      albertel  569: 	    }
1.15      albertel  570: 	} else {
1.33      albertel  571: 	    if ($target eq 'webgrade') {
                    572: 		$result.="\n";
1.81      albertel  573: 		my $back='<p><a href="/adm/flip?postdata=return:">'.
                    574: 		    &mt('Return to resource').'</a></p>';
1.33      albertel  575: 		if      ($status_code eq 'stop') {
1.81      albertel  576: 		    $result.='<b>'.&mt("Stopped grading.").'</b>'.$back;
1.33      albertel  577: 		} elsif ($status_code eq 'lock_failed') {
1.105     albertel  578: 		    $result.='<b>'.&mt("Failed to lock the requested record.")
1.81      albertel  579: 			.'</b>'.$back;
1.33      albertel  580: 		} elsif ($status_code eq 'unlock') {
1.81      albertel  581: 		    $result.='<b>'.&mt("Unlocked the requested record.")
                    582: 			.'</b>'.$back;
1.33      albertel  583: 		    $result.=&show_queue($env{'form.queue'},1);
                    584: 		} elsif ($status_code eq 'show_list') {
                    585: 		    $result.=&show_queue($env{'form.queue'},1);
1.49      albertel  586: 		} elsif ($status_code eq 'select_user') {
                    587: 		    $result.=&select_user();
1.95      albertel  588: 		} elsif ($status_code eq 'unable') {
                    589: 		    $result.='<b>'.&mt("Unable to aqcuire a user to grade.").'</b>'.$back;
1.105     albertel  590: 		} elsif ($status_code eq 'not_allowed') {
                    591: 		    $result.='<b>'.&mt('Not allowed to grade the requested user.').' '.$msg.'</b>'.$back;
1.33      albertel  592: 		} else {
1.81      albertel  593: 		    $result.='<b>'.&mt("No user to be graded.").'</b>'.$back;
1.32      albertel  594: 		}
1.21      albertel  595: 	    }
1.33      albertel  596: 	    $webgrade='no';
1.87      albertel  597: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.32      albertel  598: 	}
                    599: 	if ($target eq 'webgrade' && defined($env{'form.queue'})) {
1.61      albertel  600: 	    if ($webgrade eq 'yes') {
                    601: 		$result.=&submission_time_stamp();
                    602: 	    }
1.32      albertel  603: 	    $result.=$form_tag_start;
                    604: 	    $result.='<input type="hidden" name="webgrade" value="'.
                    605: 		$webgrade.'" />';
                    606: 	    $result.='<input type="hidden" name="queue" value="'.
                    607: 		$env{'form.queue'}.'" />';
1.52      albertel  608: 	    if ($env{'form.regrade'}) {
                    609: 		$result.='<input type="hidden" name="regrade" value="'.
                    610: 		    $env{'form.regrade'}.'" />';
                    611: 	    }
1.62      albertel  612: 	    if ($env{'form.chosensections'}) {
                    613: 		my @chosen_sections=
                    614: 		    &Apache::loncommon::get_env_multiple('form.chosensections');
                    615: 		foreach my $sec (@chosen_sections) {
                    616: 		    $result.='<input type="hidden" name="chosensections" 
                    617:                                value="'.$sec.'" />';
                    618: 		}
                    619: 	    }
1.70      albertel  620: 	    if ($webgrade eq 'yes') { $result.=&webgrade_standard_info(); }
1.15      albertel  621: 	}
1.110     albertel  622: 	if ($target eq 'webgrade') {
1.116   ! albertel  623: 	    $result.="\n".'<div id="LC_GRADE_criterialist">';
1.110     albertel  624: 	}
1.74      albertel  625:     } elsif ($target eq 'edit') {
                    626: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
                    627: 	    &Apache::structuretags::problem_edit_header();
                    628: 	$Apache::lonxml::warnings_error_header=
                    629: 	    &mt("Editor Errors - these errors might not effect the running of the problem, but they will likely cause problems with further use of the Edit mode. Please use the EditXML mode to fix these errors.")."<br />";
                    630: 	my $temp=&Apache::edit::insertlist($target,$token);
                    631: 	$result.=$temp;
1.1       albertel  632:     } else {
                    633: 	# page_start returned a starting result, delete it if we don't need it
                    634: 	$result = '';
                    635:     }
                    636:     return $result;
                    637: }
                    638: 
1.32      albertel  639: sub get_key_todo {
                    640:     my ($target)=@_;
                    641:     my $todo;
1.33      albertel  642: 
                    643:     if (defined($env{'form.reviewasubmission'})) {
1.54      albertel  644: 	&Apache::lonxml::debug("review a submission....");
1.33      albertel  645: 	$env{'form.queue'}='reviewqueue';
                    646: 	return (undef,'show_list');
                    647:     }
                    648: 
                    649:     if (defined($env{'form.reviewagrading'})) {
                    650: 	&Apache::lonxml::debug("review a grading....");
                    651: 	$env{'form.queue'}='gradingqueue';
                    652: 	return (undef,'show_list');
                    653:     }
                    654: 
1.49      albertel  655:     if (defined($env{'form.regradeasubmission'})) {
                    656: 	&Apache::lonxml::debug("regrade a grading....");
                    657: 	$env{'form.queue'}='none';
                    658: 	return (undef,'select_user');
                    659:     }
                    660: 
1.105     albertel  661: 
                    662:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    663: 
                    664:     #need to try both queues..
                    665:     if (defined($env{'form.regradeaspecificsubmission'}) &&
                    666: 	defined($env{'form.gradinguser'})               &&
                    667: 	defined($env{'form.gradingdomain'})               ) {
                    668: 	my ($symb,$cid)=&Apache::lonxml::whichuser();
                    669: 	my $cnum  = $env{'course.'.$cid.'.num'};
                    670: 	my $cdom  = $env{'course.'.$cid.'.domain'};
                    671: 	my $uname = $env{'form.gradinguser'};
                    672: 	my $udom  = $env{'form.gradingdomain'};
                    673: 
                    674: 	my $gradingkey=&encode_queue_key($symb,$udom,$uname);
                    675: 
                    676: 	my $queue;
                    677: 
                    678: 	if      (&in_queue('gradingqueue',$symb,$cdom,$cnum,$udom,$uname)) {
                    679: 	    $env{'form.queue'} = $queue = 'gradingqueue';
                    680: 	} elsif (&in_queue('reviewqueue' ,$symb,$cdom,$cnum,$udom,$uname)) {
                    681: 	    $env{'form.queue'} = $queue = 'reviewqueue';
                    682: 	}
                    683: 	
                    684: 	if (!$queue) {
                    685: 	    $env{'form.queue'} = $queue = 'none';
                    686: 	    #not queued so doing either a re or pre grade
                    687: 	    return ($gradingkey);
                    688: 	}
                    689: 
                    690: 	my $who=&queue_key_locked($queue,$gradingkey);
                    691: 	if ($who eq $me) {
                    692: 	    #already have the lock
                    693: 	    $env{'form.gradingkey'}=&Apache::lonnet::escape($gradingkey);
                    694: 	    return ($gradingkey);
                    695: 	}
                    696: 	
                    697: 	if (!defined($who)) {
                    698: 	    if (&lock_key($queue,$gradingkey)) {
                    699: 		return ($gradingkey);
                    700: 	    } else {
                    701: 		return (undef,'lock_failed');
                    702: 	    }
                    703: 	}
                    704: 
                    705: 	#otherwise (defined($who) && $who ne $me) some else has it...
                    706: 	return (undef,'not_allowed',
                    707: 		&mt('Another user ([_1]) currently has the record for [_2] locked.',
                    708: 		    $who,$env{'form.gradinguser'}.'@'.$env{'form.gradingdomain'}));
                    709:     }
                    710: 
                    711: 
1.32      albertel  712:     my $queue=$env{'form.queue'};
1.33      albertel  713: 
1.32      albertel  714:     if (!defined($queue)) {
                    715: 	$env{'form.queue'}=$queue='gradingqueue';
                    716:     }
1.33      albertel  717: 
1.32      albertel  718:     my $gradingkey=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.33      albertel  719: 
1.49      albertel  720:     if ($env{'form.queue'} eq 'none') {
                    721: 	if (defined($env{'form.gradingkey'})) {
                    722: 	    if ($target eq 'webgrade') {
                    723: 		if ($env{'form.stop'}) {
                    724: 		    return (undef,'stop');
                    725: 		} elsif ($env{'form.next'}) {
1.59      albertel  726: 		    return (undef,'select_user');
1.49      albertel  727: 		}
                    728: 	    }
                    729: 	    return ($gradingkey,'selected');
                    730: 	} else {
1.59      albertel  731: 	    return (undef,'select_user');
1.49      albertel  732: 	}
                    733:     }
1.32      albertel  734:     if (defined($env{'form.queue'}) && defined($env{'form.gradingkey'})
1.33      albertel  735: 	&& !defined($env{'form.gradingaction'}) 
                    736: 	&& $env{'form.queuemode'} eq 'selected') {
                    737: 	
                    738: 	my $who=&queue_key_locked($queue,$gradingkey);
                    739: 	if ($who eq $me) {
                    740: 	    &Apache::lonxml::debug("Found a key was given to me");
                    741: 	    return ($gradingkey,'selected');
                    742: 	} else {
                    743: 	    return (undef,'show_list');
                    744: 	}
                    745: 
                    746:     }
                    747: 
                    748:     if ($target eq 'webgrade' && $env{'form.queuemode'} eq 'selected') {
                    749: 	if ($env{'form.gradingaction'} eq 'resume') {
                    750: 	    delete($env{'form.gradingaction'});
                    751: 	    &Apache::lonxml::debug("Resuming a key");
1.32      albertel  752: 	    return ($gradingkey);
1.33      albertel  753: 	} elsif ($env{'form.gradingaction'} eq 'unlock') {
                    754: 	    &Apache::lonxml::debug("Unlocking a key ".
                    755: 				     &check_queue_unlock($queue,$gradingkey,1));
                    756: 	    return (undef,'unlock');
                    757: 	} elsif ($env{'form.gradingaction'} eq 'select') {
                    758: 	    &Apache::lonxml::debug("Locking a key");
                    759: 	    if (&lock_key($queue,$gradingkey)) {
                    760: 		&Apache::lonxml::debug("Success $queue");
                    761: 		return ($gradingkey);
                    762: 	    }
                    763: 	    &Apache::lonxml::debug("Failed $queue");
                    764: 	    return (undef,'lock_failed');
1.32      albertel  765: 	}
                    766:     }
1.33      albertel  767: 
                    768:     if ($env{'form.queuemode'} ne 'selected') {
                    769: 	# don't get something new from the queue if they hit the stop button
                    770:     	if (!($env{'form.stop'} && $target eq 'webgrade') 
                    771: 	    && !$env{'form.gradingaction'}) {
                    772: 	    &Apache::lonxml::debug("Getting anew $queue");
                    773: 	    return (&get_from_queue($queue));
                    774: 	} else {
                    775: 	    return (undef,'stop');
                    776: 	}
1.32      albertel  777:     }
1.33      albertel  778:     return (undef,undef)
1.32      albertel  779: }
1.94      albertel  780: 
                    781: sub minimize_storage {
                    782:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    783: 	if ($key =~ /regrader$/) { next; }
                    784: 	if ($Apache::lonhomework::results{$key} eq
                    785: 	    $Apache::lonhomework::history{$key}) {
                    786: 	    delete($Apache::lonhomework::results{$key});
                    787: 	}
                    788:     }
                    789: }
                    790: 
1.1       albertel  791: sub end_Task {
                    792:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    793:     my $result='';
                    794:     my $status=$Apache::inputtags::status['-1'];
1.29      albertel  795:     my ($version,$previous)=&get_version();
1.1       albertel  796:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.15      albertel  797: 	$target eq 'tex') {
1.69      albertel  798: 	if ($target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.1       albertel  799: 	    if ($target eq 'web') {
1.54      albertel  800: 		if (&show_task($status,$previous)) {
                    801: 		    $result.=&Apache::lonxml::endredirection();
                    802: 		}
1.64      albertel  803: 		if ($status eq 'CAN_ANSWER' && !$previous && 
                    804: 		    !$env{'form.donescreen'}) {
1.15      albertel  805: 		    $result.="\n".'<table border="1">'.
1.28      albertel  806: 			&Apache::inputtags::file_selector("$version.0",
                    807: 							  "bridgetask","*",
1.46      albertel  808: 							  'portfolioonly',
                    809: 							  '
                    810: <h2>'.&mt('Submit Portfolio Files for Grading').'</h2>
                    811: <p>'.&mt('Indicate the files from your portfolio to be evaluated in grading this task.').'</p>').
1.9       albertel  812: 							  "</table>";
1.77      albertel  813: 		}
1.78      albertel  814: 		if (!$previous && $status ne 'SHOW_ANSWER' &&
                    815: 		    &show_task($status,$previous)) {
1.9       albertel  816: 		    $result.=&Apache::inputtags::gradestatus('0');
1.64      albertel  817: 		    $result.='</form>';
1.116   ! albertel  818: 		    my $action = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.64      albertel  819: 		    $result.=<<DONEBUTTON;
1.115     albertel  820: <form name="done" method="post" action="$action">
1.64      albertel  821:    <input type="hidden" name="donescreen" value="1" />
                    822:    <input type="submit" value="Done" />
                    823: </form>
                    824: DONEBUTTON
1.77      albertel  825:                 }
1.56      albertel  826: 		if (&show_task($status,$previous) &&
1.89      albertel  827: 		    $Apache::lonhomework::history{"resource.$version.0.status"} =~ /^(pass|fail)$/) {
                    828: 		    my $bt_status=$Apache::lonhomework::history{"resource.$version.0.status"};
1.54      albertel  829: 		    my $title=&Apache::lonnet::gettitle();
1.80      albertel  830: 
                    831: 		    my $slot_name=
1.89      albertel  832: 			$Apache::lonhomework::history{"resource.$version.0.checkedin.slot"};
1.80      albertel  833: 		    my %slot=&Apache::lonnet::get_slot($slot_name);
1.55      albertel  834: 		    my $start_time=
                    835: 			&Apache::lonlocal::locallocaltime($slot{'starttime'});
1.54      albertel  836: 
1.116   ! albertel  837: 		    my $status = "\n<div class='LC_$bt_status LC_criteria'>\n";
1.54      albertel  838: 		    
                    839: 		    if ($bt_status eq 'pass')  {
                    840: 			$status.='<h2>You passed the '.$title.' given on '.
1.80      albertel  841: 			    $start_time.'</h2>';
1.54      albertel  842: 		    }
                    843: 		    if ($bt_status eq 'fail')  {
                    844: 			$status.='<h2>You did not pass the '.$title.' given on '.
1.80      albertel  845: 			    $start_time.'</h2>';
1.54      albertel  846: 			if (!$previous) {
                    847: 			    $status.=&add_request_another_attempt_button();
                    848: 			}
                    849: 		    }
                    850: 		    my $man_count=0;
                    851: 		    my $opt_count=0;
                    852: 		    my $opt_passed=0;
                    853: 		    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    854: 			if ($Apache::bridgetask::dimensionmandatory{$dim_id}
                    855: 			    eq 'N') {
                    856: 			    $opt_count++;
1.89      albertel  857: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim_id.status"} eq 'pass') {
1.54      albertel  858: 				$opt_passed++;
                    859: 			    }
                    860: 			} else {
                    861: 			    $man_count++;
                    862: 			}
                    863: 		    }
                    864: 		    my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
                    865: 							 $parstack,$safeeval);
                    866: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
1.99      albertel  867: 		    $status.="\n<p>".&mt('You needed to pass all of the [_1]  mandatory components and [_2] of the [_3] optional components, of which you passed [_4].',$man_count,$opt_req,$opt_count,$opt_passed)."</p></div>\n";
1.54      albertel  868: 
                    869: 		    my $internal_location=&internal_location();
                    870: 		    $result=~s/\Q$internal_location\E/$status/;
                    871: 		}
1.13      albertel  872: 	    } 
                    873: 	    if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  874: 		$result.=&Apache::lonxml::xmlend().'</html>';
                    875: 	    }
                    876: 	}
1.29      albertel  877: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel  878: 	    my $award='SUBMITTED';
1.28      albertel  879: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel  880: 						    'portfiles',\$award);
1.14      albertel  881: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel  882: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                    883: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    884: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                    885: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                    886: 
                    887: 		$Apache::lonhomework::results{"resource.0.award"}=
                    888: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                    889: 		    $award;
1.51      albertel  890: 		$Apache::lonhomework::results{"resource.0.submission"}=
                    891: 		    $Apache::lonhomework::results{"resource.$version.0.submission"}='';
1.64      albertel  892: 	    } else {
                    893: 		delete($Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"});
1.77      albertel  894: 		$award = '';
1.10      albertel  895: 	    }
1.4       albertel  896: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    897: 	    &Apache::structuretags::finalize_storage();
1.83      albertel  898: 	    if ($award eq 'SUBMITTED'
                    899: 		&& defined($Apache::inputtags::slot_name)) {
1.82      albertel  900: 		&add_to_queue('gradingqueue',[$Apache::inputtags::slot_name]);
1.14      albertel  901: 	    }
1.79      albertel  902: 	} elsif ($Apache::lonhomework::results{'INTERNAL_store'}) {
                    903: 	    &Apache::structuretags::finalize_storage();
1.1       albertel  904: 	}
1.32      albertel  905: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes') {
1.20      albertel  906: 	    my $optional_required=
                    907: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                    908: 					   $safeeval);
                    909: 	    my $optional_passed=0;
                    910: 	    my $mandatory_failed=0;
                    911: 	    my $ungraded=0;
                    912: 	    my $review=0;   
1.21      albertel  913: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.20      albertel  914: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    915: 		my $status=
1.89      albertel  916: 		    $Apache::lonhomework::results{"resource.$version.0.$dim_id.status"};
1.20      albertel  917: 		my $mandatory=
                    918: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
                    919: 		if ($status eq 'pass') {
                    920: 		    if (!$mandatory) { $optional_passed++; }
                    921: 		} elsif ($status eq 'fail') {
                    922: 		    if ($mandatory) { $mandatory_failed++; }
                    923: 		} elsif ($status eq 'ungraded') {
                    924: 		    $ungraded++;
                    925: 		} elsif ($status eq 'review') {
                    926: 		    $review++;
1.49      albertel  927: 		} else {
                    928: 		    $ungraded++;
                    929: 		}
1.20      albertel  930: 	    }
                    931: 	    if ($optional_passed < $optional_required) {
                    932: 		$mandatory_failed++;
                    933: 	    }
1.21      albertel  934: 	    &Apache::lonxml::debug("all dim ".join(':',@Apache::bridgetask::dimensionlist)."results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
1.89      albertel  935: 	    $Apache::lonhomework::results{'resource.0.regrader'}=
1.20      albertel  936: 		$env{'user.name'}.'@'.$env{'user.domain'};
                    937: 	    if ($review) {
1.89      albertel  938: 		$Apache::lonhomework::results{"resource.$version.0.status"}='review';
1.33      albertel  939: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    940: 		    &check_queue_unlock($env{'form.queue'});
                    941: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                    942: 		} else {
1.49      albertel  943: 		    &move_between_queues($env{'form.queue'},'reviewqueue');
1.33      albertel  944: 		}
1.20      albertel  945: 	    } elsif ($ungraded) {
1.89      albertel  946: 		$Apache::lonhomework::results{"resource.$version.0.status"}='ungraded';
1.49      albertel  947: 		if ($env{'form.queue'} eq 'reviewqueue' ||
                    948: 		    $env{'form.queue'} eq 'none' ) {
1.33      albertel  949: 		    &Apache::lonxml::debug("moving back.");
1.49      albertel  950: 		    &move_between_queues($env{'form.queue'},'gradingqueue');
1.33      albertel  951: 		} else {
                    952: 		    &check_queue_unlock($env{'form.queue'});
                    953: 		}
1.20      albertel  954: 	    } elsif ($mandatory_failed) {
1.89      albertel  955: 		$Apache::lonhomework::results{"resource.$version.0.status"}='fail';
1.25      albertel  956: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                    957: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                    958: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.39      albertel  959: 		&remove_from_queue($env{'form.queue'}); 
                    960: 
                    961: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  962: 		
                    963: 		if ($env{'form.regrade'} ne 'yes') {
                    964: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    965: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    966: 		    &Apache::grades::version_portfiles(
                    967: 						       \%Apache::lonhomework::results,
                    968: 						       ["$version.0.bridgetask"],$courseid,
                    969: 						       $symb,$udom,$uname,
                    970: 						       ["$version.0.bridgetask"]);
                    971: 		}
1.20      albertel  972: 	    } else {
1.89      albertel  973: 		$Apache::lonhomework::results{"resource.$version.0.status"}='pass';
1.25      albertel  974: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                    975: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                    976: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.32      albertel  977: 		&remove_from_queue($env{'form.queue'});
1.39      albertel  978: 
                    979: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  980: 		if ($env{'form.regrade'} ne 'yes') {
                    981: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    982: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    983: 		    &Apache::grades::version_portfiles(
                    984: 						       \%Apache::lonhomework::results,
                    985: 						       ["$version.0.bridgetask"],$courseid,
                    986: 						       $symb,$udom,$uname,
                    987: 						       ["$version.0.bridgetask"]);
                    988: 		}
1.20      albertel  989: 	    }
1.89      albertel  990: 	    $Apache::lonhomework::results{"resource.0.status"}=
                    991: 		$Apache::lonhomework::results{"resource.$version.0.status"};
1.28      albertel  992: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel  993: 		$Apache::lonhomework::results{"resource.0.award"}=
1.50      albertel  994: 		    $Apache::lonhomework::results{"resource.$version.0.award"};
1.26      albertel  995: 		$Apache::lonhomework::results{"resource.0.awarded"}=
1.50      albertel  996: 		    $Apache::lonhomework::results{"resource.$version.0.awarded"};
1.26      albertel  997: 		$Apache::lonhomework::results{"resource.0.solved"}=
1.50      albertel  998: 		    $Apache::lonhomework::results{"resource.$version.0.solved"};
1.25      albertel  999: 	    }
1.94      albertel 1000: 	    &minimize_storage();
1.21      albertel 1001: 	    &Apache::structuretags::finalize_storage();
1.20      albertel 1002: 	}
1.15      albertel 1003:     } elsif ($target eq 'webgrade') {
1.111     albertel 1004: 	$result.="</div>\n<hr />";
1.20      albertel 1005: 	#$result.='<input type="submit" name="next" value="'.
                   1006: 	#    &mt('Save &amp; Next').'" /> ';
                   1007: 	#$result.='<input type="submit" name="end" value="'.
                   1008: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                   1009: 	#$result.='<input type="submit" name="throwaway" value="'.
                   1010: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                   1011: 	#$result.='<input type="submit" name="save" value="'.
                   1012: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.15      albertel 1013: 	$result.='</form>'.&Apache::loncommon::endbodytag().'</html>';
1.1       albertel 1014:     } elsif ($target eq 'meta') {
1.70      albertel 1015: 	$result.=&Apache::response::meta_package_write('Task');
1.77      albertel 1016:         $result.=&Apache::response::meta_stores_write('solved','string',
                   1017: 						      'Problem Status');
                   1018: 	$result.=&Apache::response::meta_stores_write('tries','int_zeropos',
                   1019: 						      'Number of Attempts');
                   1020: 	$result.=&Apache::response::meta_stores_write('awarded','float',
                   1021: 						      'Partial Credit Factor');
                   1022: 	$result.=&Apache::response::meta_stores_write('status','string',
                   1023: 						      'Bridge Task Status');
1.1       albertel 1024:     }
1.4       albertel 1025:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel 1026:     return $result;
                   1027: }
                   1028: 
1.31      albertel 1029: sub move_between_queues {
                   1030:     my ($src_queue,$dest_queue)=@_;
1.49      albertel 1031:     my $cur_data;
                   1032:     if ($src_queue ne 'none') {
                   1033: 	$cur_data=&get_queue_data($src_queue);
                   1034: 	if (!$cur_data) { return 'not_exist'; }
                   1035:     } else {
                   1036: 	$cur_data = ['none'];
                   1037:     }
1.82      albertel 1038:     my $result=&add_to_queue($dest_queue,[$cur_data->[0]]);
1.31      albertel 1039:     if ($result ne 'ok') {
                   1040: 	return $result;
                   1041:     }
                   1042:     &check_queue_unlock($src_queue);
                   1043:     return &remove_from_queue($src_queue);
1.21      albertel 1044: }
                   1045: 
                   1046: sub check_queue_unlock {
1.32      albertel 1047:     my ($queue,$key,$allow_not_me)=@_;
1.49      albertel 1048:     if ($queue eq 'none') { return 'ok'; }
1.30      albertel 1049:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.32      albertel 1050:     if (!defined($key)) {
                   1051: 	$key="$symb\0queue\0$uname\@$udom";
                   1052:     }
1.30      albertel 1053:     my $cnum=$env{'course.'.$cid.'.num'};
                   1054:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1055:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1056:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                   1057:     if  ($who eq $me) {
1.32      albertel 1058: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                   1059:     } elsif ($allow_not_me) {
1.33      albertel 1060: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel 1061: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel 1062:     }
1.32      albertel 1063:     return 'not_owner';
1.21      albertel 1064: }
                   1065: 
1.88      albertel 1066: sub in_queue {
                   1067:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
                   1068:     if ($queue eq 'none') { return 0; }
                   1069:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1070: 	|| !defined($udom) || !defined($uname)) {
                   1071: 	($symb,my $cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1072: 	$cnum=$env{'course.'.$cid.'.num'};
                   1073: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1074:     }
                   1075: 
                   1076:     my $key=&encode_queue_key($symb,$udom,$uname);
                   1077:     my %results = &Apache::lonnet::get($queue,[$key],$cdom,$cnum);
                   1078: 
                   1079:     if (defined($results{$key})) {
                   1080: 	return 1;
                   1081:     }
                   1082:     return 0;
                   1083: }
                   1084: 
1.21      albertel 1085: sub remove_from_queue {
1.86      albertel 1086:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
1.49      albertel 1087:     if ($queue eq 'none') { return 'ok'; }
1.86      albertel 1088:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1089: 	|| !defined($udom) || !defined($uname)) {
                   1090: 	($symb,my $cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1091: 	$cnum=$env{'course.'.$cid.'.num'};
                   1092: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1093:     }
1.88      albertel 1094:     if (!&in_queue($queue,$symb,$cdom,$cnum,$udom,$uname)) {
                   1095: 	return 'ok';
                   1096:     }
1.86      albertel 1097:     my $key=&encode_queue_key($symb,$udom,$uname);
1.27      albertel 1098:     my @keys=($key,"$key\0locked");
1.31      albertel 1099:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel 1100: }
                   1101: 
1.16      albertel 1102: sub setup_env_for_other_user {
                   1103:     my ($queue_key,$safeeval)=@_;
                   1104:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel 1105:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel 1106:     $env{'form.grade_symb'}=$symb;
                   1107:     $env{'form.grade_domain'}=$udom;
                   1108:     $env{'form.grade_username'}=$uname;
                   1109:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                   1110:     &Apache::lonxml::initialize_rndseed($safeeval);
                   1111: }
                   1112: 
1.31      albertel 1113: sub get_queue_data {
                   1114:     my ($queue)=@_;
                   1115:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1116:     my $cnum=$env{'course.'.$cid.'.num'};
                   1117:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1118:     my $todo="$symb\0queue\0$uname\@$udom";
                   1119:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                   1120:     if ($key eq $todo && ref($value)) {
                   1121: 	return $value;
                   1122:     }
                   1123:     return undef;
                   1124: }
                   1125: 
1.84      albertel 1126: 
1.49      albertel 1127: sub check_queue_for_key {
1.84      albertel 1128:     my ($cdom,$cnum,$queue,$todo)=@_;
                   1129: 
1.49      albertel 1130:     my %results=
                   1131: 	&Apache::lonnet::get($queue,[$todo,"$todo\0locked"],$cdom,$cnum);
                   1132:     
                   1133:     if (exists($results{$todo}) && ref($results{$todo})) {
                   1134: 	if (defined($results{"$todo\0locked"})) {
                   1135: 	    return 'locked';
                   1136: 	}
1.86      albertel 1137: 	my $slot;
                   1138: 	if (ref($results{$todo}) eq 'ARRAY')  {
                   1139: 	    $slot = $results{$todo}[0];
                   1140: 	} elsif (ref($results{$todo}) eq 'HASH')  {
                   1141: 	    $slot = $results{$todo}{'slot'};
                   1142: 	}
                   1143: 	if (defined($slot)) {
                   1144: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1145: 	    if ($slot_data{'endtime'} > time) { 
                   1146: 		return 'in_progress';
                   1147: 	    }
1.58      albertel 1148: 	}
1.49      albertel 1149: 	return 'enqueued';
                   1150:     }
                   1151:     return undef;
                   1152: }
                   1153: 
1.14      albertel 1154: sub add_to_queue {
1.82      albertel 1155:     my ($queue,$user_data)=@_;
1.49      albertel 1156:     if ($queue eq 'none') { return 'ok'; }
1.14      albertel 1157:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.82      albertel 1158:     if (!$cid || $env{'request.state'} eq 'construct') {
                   1159: 	return 'no_queue';
                   1160:     }
1.14      albertel 1161:     my $cnum=$env{'course.'.$cid.'.num'};
                   1162:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1163:     my %data;
1.82      albertel 1164:     $data{"$symb\0queue\0$uname\@$udom"}=$user_data;
1.83      albertel 1165:     return &Apache::lonnet::cput($queue,\%data,$cdom,$cnum);
1.14      albertel 1166: }
                   1167: 
                   1168: sub show_queue {
1.32      albertel 1169:     my ($queue,$with_selects)=@_;
1.14      albertel 1170:     my $result;
                   1171:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1172:     my $cnum=$env{'course.'.$cid.'.num'};
                   1173:     my $cdom=$env{'course.'.$cid.'.domain'};
1.59      albertel 1174: 
                   1175:     my @chosen_sections=
                   1176: 	&Apache::loncommon::get_env_multiple('form.chosensections');
                   1177:     &Apache::grades::init_perm();
                   1178:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
                   1179:     &Apache::grades::reset_perm();
1.63      albertel 1180:     if (!(grep(/^all$/,@chosen_sections))) {
                   1181: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1182: 	    '</tt>.</p> '."\n";
                   1183:     }
1.59      albertel 1184: 
1.16      albertel 1185:     my $regexp="^$symb\0";
1.30      albertel 1186:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel 1187:     my ($tmp)=%queue;
                   1188:     if ($tmp=~/^error: 2 /) {
1.33      albertel 1189: 	return "\n<h3>Current Queue - $queue</h3><table border='1'><tr><td>Empty</td></tr></table>";
1.31      albertel 1190:     }
1.103     albertel 1191:     my $title=&Apache::lonnet::gettitle($symb);
                   1192:     $result.="\n<h3>Current Queue - $title $queue </h3><table border='1'><tr>";
                   1193:     if ($with_selects) { $result.="<th>Status</th><th></th>"; }
                   1194:     $result.="<th>user</th><th>data</th></tr>";
1.14      albertel 1195:     foreach my $key (sort(keys(%queue))) {
1.59      albertel 1196: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1197: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
1.32      albertel 1198: 	if ($key=~/locked$/ && !$with_selects) {
1.103     albertel 1199: 	    $result.="<tr><td>$uname</td>";
                   1200: 	    $result.='<td>'.$queue{$key}.'</td></tr>';
1.32      albertel 1201: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.103     albertel 1202: 	    $result.="<tr><td></td>";
                   1203: 	    $result.='<td>'.
1.16      albertel 1204: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel 1205: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1206: 	    $result.="<tr>";
1.35      albertel 1207: 	    my $slot=$queue{$key}->[0];
                   1208: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
1.32      albertel 1209: 	    if ($with_selects) {
                   1210: 		my $ekey=&Apache::lonnet::escape($key);
1.103     albertel 1211: 		my ($action,$description,$status)=('select',&mt('Select'));
1.32      albertel 1212: 		if (exists($queue{"$key\0locked"})) {
                   1213: 		    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
1.103     albertel 1214: 		    $status=&mt('Locked by <tt>[_1]</tt>',$queue{"$key\0locked"});
1.32      albertel 1215: 		    if ($me eq $queue{"$key\0locked"}) {
                   1216: 			($action,$description)=('resume',&mt('Resume'));
                   1217: 		    } else {
                   1218: 			($action,$description)=('unlock',&mt('Unlock'));
                   1219: 		    }
                   1220: 		}
1.62      albertel 1221: 		my $seclist;
                   1222: 		foreach my $sec (@chosen_sections) {
                   1223: 		    $seclist.='<input type="hidden" name="chosensections" 
                   1224:                                value="'.$sec.'" />';
                   1225: 		}
1.35      albertel 1226: 		if (time > $slot_data{'endtime'}) {
                   1227: 		    $result.=(<<FORM);
1.103     albertel 1228: <td>$status</td>
1.32      albertel 1229: <td>
1.115     albertel 1230: <form style="display: inline" method="post">
1.32      albertel 1231:  <input type="hidden" name="gradingkey" value="$ekey" />
                   1232:  <input type="hidden" name="queue" value="$queue" />
                   1233:  <input type="hidden" name="gradingaction" value="$action" />
                   1234:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel 1235:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel 1236:  <input type="submit" name="submit" value="$description" />
1.62      albertel 1237:  $seclist
1.32      albertel 1238: </form>
                   1239: </td>
                   1240: FORM
1.35      albertel 1241: 
                   1242:                 } else {
                   1243: 		    $result.='<td>'.&mt("In Progress").'</td>'
                   1244: 		}
1.32      albertel 1245: 	    }
1.104     albertel 1246: 	    $result.= "<td>".$fullname->{$uname.':'.$udom}.
                   1247: 		" <tt>($uname\@$udom)</tt> </td>";
1.103     albertel 1248: 	    $result.='<td>Slot: '.$slot.' End time: '.
1.20      albertel 1249: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
                   1250: 		"</td></tr>";
1.16      albertel 1251: 	}
1.14      albertel 1252:     }
1.15      albertel 1253:     $result.="</table><hr />\n";
1.14      albertel 1254:     return $result;
                   1255: }
                   1256: 
1.34      albertel 1257: sub get_queue_counts {
                   1258:     my ($queue)=@_;
                   1259:     my $result;
                   1260:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1261:     my $cnum=$env{'course.'.$cid.'.num'};
                   1262:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1263:     my $regexp="^$symb\0";
                   1264:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1265:     my ($tmp)=%queue;
                   1266:     if ($tmp=~/^error: 2 /) {
                   1267: 	return (0,0,0);
                   1268:     }
                   1269:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
1.96      albertel 1270:     my %slot_cache;
1.34      albertel 1271:     foreach my $key (sort(keys(%queue))) {
                   1272: 	if ($key=~/locked$/) {
                   1273: 	    $locks++;
                   1274: 	} elsif ($key=~/timestamp$/) {
                   1275: 	    #ignore
                   1276: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1277: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1278: 	    $entries++;
                   1279: 	    my $slot=$queue{$key}->[0];
1.96      albertel 1280: 	    if (!exists($slot_cache{$slot})) {
                   1281: 		my %slot_data=&Apache::lonnet::get_slot($slot);
                   1282: 		$slot_cache{$slot} = \%slot_data;
                   1283: 	    }
                   1284: 	    if (time > $slot_cache{$slot}{'endtime'}) {
1.34      albertel 1285: 		$ready_to_grade++;
                   1286: 	    }
                   1287: 	}
                   1288:     }
                   1289:     return ($entries,$ready_to_grade,$locks);
                   1290: }
                   1291: 
1.49      albertel 1292: sub encode_queue_key {
                   1293:     my ($symb,$udom,$uname)=@_;
                   1294:     return "$symb\0queue\0$uname\@$udom";
                   1295: }
                   1296: 
1.14      albertel 1297: sub decode_queue_key {
                   1298:     my ($key)=@_;
                   1299:     my ($symb,undef,$user) = split("\0",$key);
                   1300:     my ($uname,$udom) = split('@',$user);
                   1301:     return ($symb,$uname,$udom);
                   1302: }
                   1303: 
                   1304: sub queue_key_locked {
1.30      albertel 1305:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel 1306:     if (!defined($cdom) || !defined($cnum)) {
                   1307: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1308: 	$cnum=$env{'course.'.$cid.'.num'};
                   1309: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1310:     }
1.14      albertel 1311:     my ($key_locked,$value)=
1.30      albertel 1312: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel 1313:     if ($key_locked eq "$key\0locked") {
                   1314: 	return $value;
                   1315:     }
                   1316:     return undef;
                   1317: }
                   1318: 
                   1319: sub pick_from_queue_data {
1.30      albertel 1320:     my ($queue,$check_section,$queuedata,$cdom,$cnum)=@_;
1.98      albertel 1321:     my @possible; # will hold queue entries that are valid to be selected
1.30      albertel 1322:     foreach my $key (keys(%$queuedata)) {
1.68      albertel 1323: 	if ($key =~ /\0locked$/) { next; }
                   1324: 	if ($key =~ /\0timestamp$/) { next; }
1.14      albertel 1325: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                   1326: 	if ($check_section) {
                   1327: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel 1328: 	    if ($section eq $check_section) {
1.33      albertel 1329: 		&Apache::lonxml::debug("my sec");
1.15      albertel 1330: 		next;
                   1331: 	    }
1.14      albertel 1332: 	}
1.30      albertel 1333: 	my $slot=$queuedata->{$key}[0];
1.14      albertel 1334: 	my %slot_data=&Apache::lonnet::get_slot($slot);
1.15      albertel 1335: 	if ($slot_data{'endtime'} > time) { 
1.33      albertel 1336: 	    &Apache::lonxml::debug("not time");
1.15      albertel 1337: 	    next;
                   1338: 	}
1.98      albertel 1339: 	if (exists($queuedata->{"$key\0locked"})) {
1.33      albertel 1340: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel 1341: 	    next;
                   1342: 	}
1.98      albertel 1343: 	push(@possible,[$key,$slot_data{'endtime'}]);
                   1344:     }
                   1345:     if (@possible) {
                   1346:         # sort entries in order by slot end time
                   1347: 	@possible = sort { $a->[1] <=> $b->[1] } @possible;
                   1348: 	# pick one of the first ten entries
                   1349: 	my $max=($#possible < 10) ? $#possible : 10;
                   1350: 	return $possible[int(rand($max))][0];
1.14      albertel 1351:     }
                   1352:     return undef;
                   1353: }
                   1354: 
1.15      albertel 1355: sub find_mid_grade {
1.30      albertel 1356:     my ($queue,$symb,$cdom,$cnum)=@_;
1.16      albertel 1357:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.15      albertel 1358:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1359:     if ($todo) {
1.30      albertel 1360: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel 1361: 	if ($who eq $me) { return $todo; }
                   1362:     }
                   1363:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel 1364:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel 1365:     foreach my $key (keys(%locks)) {
                   1366: 	my $who=$locks{$key};
                   1367: 	if ($who eq $me) {
                   1368: 	    $todo=$key;
                   1369: 	    $todo=~s/\0locked$//;
                   1370: 	    return $todo;
                   1371: 	}
                   1372:     }
                   1373:     return undef;
                   1374: }
                   1375: 
1.32      albertel 1376: sub lock_key {
                   1377:     my ($queue,$todo)=@_;
                   1378:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1379:     my (undef,$cid)=&Apache::lonxml::whichuser();
                   1380:     my $cnum=$env{'course.'.$cid.'.num'};
                   1381:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1382:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                   1383: 					$cdom,$cnum);
1.33      albertel 1384:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel 1385:     if ($success eq 'ok') {
                   1386: 	return 1;
                   1387:     }
                   1388:     return 0;
                   1389: }
                   1390: 
1.86      albertel 1391: sub get_queue_symb_status {
1.85      albertel 1392:     my ($queue,$symb,$cdom,$cnum) = @_;
                   1393:     if (!defined($cdom) || !defined($cnum)) {
                   1394: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1395: 	$cnum=$env{'course.'.$cid.'.num'};
                   1396: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1397:     }
                   1398:     my $regexp="^$symb\0";
                   1399:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1400:     my ($tmp)=%queue;
                   1401:     if ($tmp=~/^error: 2 /) { return; }
                   1402:     my @users;
                   1403:     foreach my $key (sort(keys(%queue))) {
                   1404: 	next if ($key=~/locked$/);
                   1405: 	next if ($key=~/timestamp$/);
                   1406: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1407: 	push(@users,"$uname:$udom");
                   1408:     }
                   1409:     return @users;
                   1410: }
                   1411: 
1.14      albertel 1412: sub get_from_queue {
1.30      albertel 1413:     my ($queue)=@_;
1.14      albertel 1414:     my $result;
                   1415:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1416:     my $cnum=$env{'course.'.$cid.'.num'};
                   1417:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel 1418:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel 1419:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel 1420:     if ($todo) { return $todo; }
1.95      albertel 1421:     my $attempts=0;
1.14      albertel 1422:     while (1) {
1.95      albertel 1423: 	if ($attempts > 2) {
                   1424: 	    # tried twice to get a queue entry, giving up
                   1425: 	    return (undef,'unable');
                   1426: 	}
1.14      albertel 1427: 	my $starttime=time;
1.83      albertel 1428: 	&Apache::lonnet::cput($queue,{"$symb\0timestamp"=>$starttime},
                   1429: 			      $cdom,$cnum);
1.33      albertel 1430: 	&Apache::lonxml::debug("$starttime");
1.14      albertel 1431: 	my $regexp="^$symb\0queue\0";
1.97      albertel 1432: 	my $range= ($attempts < 1 ) ? '0-100' : '0-400';
                   1433: 
1.98      albertel 1434: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel 1435: 	#make a pass looking for a user _not_ in my section
1.14      albertel 1436: 	if ($env{'request.course.sec'}) {
1.33      albertel 1437: 	    &Apache::lonxml::debug("sce");
1.30      albertel 1438: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                   1439: 					\%queue,$cdom,$cnum);
1.33      albertel 1440: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel 1441: 	}
1.33      albertel 1442: 	# no one _not_ in our section so look for any user that is
                   1443: 	# ready for grading
1.14      albertel 1444: 	if (!$todo) {
1.33      albertel 1445: 	    &Apache::lonxml::debug("no sce");
1.98      albertel 1446: 	    $todo=&pick_from_queue_data($queue,undef,\%queue,$cdom,$cnum);
1.33      albertel 1447: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel 1448: 	}
                   1449: 	# no user to grade 
                   1450: 	if (!$todo) { last; }
1.33      albertel 1451: 	&Apache::lonxml::debug("got $todo");
1.14      albertel 1452: 	# otherwise found someone so lets try to lock them
1.32      albertel 1453: 	# unless someone else already picked them
1.95      albertel 1454: 	if (!&lock_key($queue,$todo)) {
                   1455: 	    $attempts++;
                   1456: 	    next;
                   1457: 	}
1.14      albertel 1458: 	my (undef,$endtime)=
1.30      albertel 1459: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel 1460: 				 $cdom,$cnum);
1.33      albertel 1461: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel 1462: 	# someone else already modified the queue, 
                   1463: 	# perhaps our picked user wass already fully graded between
                   1464: 	# when we picked him and when we locked his record? so lets
                   1465: 	# double check.
                   1466: 	if ($endtime != $starttime) {
                   1467: 	    my ($key,$value)=
1.30      albertel 1468: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel 1469: 				     $cdom,$cnum);
1.33      albertel 1470: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel 1471: 	    if ($key eq $todo && ref($value)) {
                   1472: 	    } else {
1.30      albertel 1473: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel 1474: 				     $cdom,$cnum);
1.33      albertel 1475: 		&Apache::lonxml::debug("del");
1.95      albertel 1476: 		$attempts++;
1.14      albertel 1477: 		next;
                   1478: 	    }
                   1479: 	}
1.33      albertel 1480: 	&Apache::lonxml::debug("last $todo");
1.14      albertel 1481: 	last;
                   1482:     }
                   1483:     return $todo;
                   1484: }
                   1485: 
1.49      albertel 1486: sub select_user {
                   1487:     my ($symb,$cid)=&Apache::lonxml::whichuser();
                   1488: 
1.59      albertel 1489:     my @chosen_sections=
                   1490: 	&Apache::loncommon::get_env_multiple('form.chosensections');
1.49      albertel 1491:     &Apache::grades::init_perm();
1.59      albertel 1492:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
1.49      albertel 1493:     &Apache::grades::reset_perm();
1.63      albertel 1494:     
                   1495:     my $result;
                   1496:     if (!(grep(/^all$/,@chosen_sections))) {
                   1497: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1498: 	    '</tt>.</p> '."\n";
                   1499:     }
                   1500:     $result.='<table border="1">';
1.49      albertel 1501: 
                   1502:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
                   1503: 	my ($uname,$udom) = split(/:/,$student);
1.59      albertel 1504: 	
1.84      albertel 1505: 	my $cnum=$env{'course.'.$cid.'.num'};
                   1506: 	my $cdom=$env{'course.'.$cid.'.domain'};
1.88      albertel 1507: 	my %status = &get_student_status($symb,$cdom,$cnum,$udom,$uname,
                   1508: 					 'Task');
1.49      albertel 1509: 	my $queue = 'none';
1.58      albertel 1510: 	my $cannot_grade;
                   1511: 	if ($status{'reviewqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1512: 	    $queue = 'reviewqueue';
1.58      albertel 1513: 	    if ($status{'reviewqueue'} eq 'in_progress') {
                   1514: 		$cannot_grade=1;
                   1515: 	    }
                   1516: 	} elsif ($status{'gradingqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1517: 	    $queue = 'gradingqueue';
1.58      albertel 1518: 	    if ($status{'gradingqueue'} eq 'in_progress') {
                   1519: 		$cannot_grade=1;
                   1520: 	    }
1.49      albertel 1521: 	}
                   1522: 	my $todo = 
                   1523: 	    &Apache::lonnet::escape(&encode_queue_key($symb,$udom,$uname));
1.58      albertel 1524: 	if ($cannot_grade) {
                   1525: 	    $result.='<tr><td>&nbsp;</td><td>'.$fullname->{$student}.
                   1526: 		'</td><td>';
                   1527: 	} else {
1.62      albertel 1528: 	    my $seclist;
                   1529: 	    foreach my $sec (@chosen_sections) {
                   1530: 		$seclist.='<input type="hidden" name="chosensections" 
                   1531:                                value="'.$sec.'" />';
                   1532: 	    }
1.102     albertel 1533: 	    my $studentdis = $student;
                   1534: 	    $studentdis =~ tr/:/@/;
1.58      albertel 1535: 	    $result.=<<RESULT;
1.49      albertel 1536: <tr>
                   1537:   <td>
1.115     albertel 1538:     <form style="display: inline" method="post">
1.49      albertel 1539:       <input type="hidden" name="gradingkey" value="$todo" />
                   1540:       <input type="hidden" name="queue" value="$queue" />
                   1541:       <input type="hidden" name="webgrade" value="no" />
1.52      albertel 1542:       <input type="hidden" name="regrade" value="yes" />
1.62      albertel 1543:       <input type="submit" name="submit" value="Regrade" />
                   1544:       $seclist
1.49      albertel 1545:     </form>
1.102     albertel 1546:   <td>$fullname->{$student} <tt>($studentdis)</tt></td>
1.49      albertel 1547:   <td>
                   1548: RESULT
1.58      albertel 1549:         }
1.49      albertel 1550:         if ($status{'status'} eq 'pass') {
                   1551: 	    $result .= '<font color="green">'.&mt('Passed').'</font>';
                   1552: 	} elsif ($status{'status'} eq 'fail') {
                   1553: 	    $result .= '<font color="red">'.&mt('Failed').'</font>';
                   1554: 	} elsif ($status{'status'} eq 'review') {
                   1555: 	    $result .= '<font color="blue">'.&mt('Under Review').'</font>';
                   1556: 	} elsif ($status{'status'} eq 'ungraded') {
                   1557: 	    $result .= &mt('Ungraded');
                   1558: 	} elsif ($status{'status'} ne '') {
                   1559: 	    $result .= '<font color="orange">'.&mt('Unknown Status').'</font>';
                   1560: 	} else {
                   1561: 	    $result.="&nbsp;";
                   1562: 	}
                   1563: 	if ($status{'version'}) {
                   1564: 	    $result .= ' '.&mt('Version').' '.$status{'version'};
                   1565: 	}
1.101     albertel 1566: 	if ($status{'grader'}) {
                   1567: 	    $result .= ' '.&mt('(Graded by [_1])',$status{'grader'}).' ';
                   1568: 	}
1.49      albertel 1569: 	$result.= '</td><td>';
                   1570: 	if ($status{'reviewqueue'} eq 'enqueued') {
                   1571: 	    $result .= &mt('Awaiting Review');
                   1572: 	} elsif ($status{'reviewqueue'} eq 'locked') {
                   1573: 	    $result .= &mt('Under Review');
1.58      albertel 1574: 	} elsif ($status{'reviewqueue'} eq 'in_progress') {
                   1575: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1576: 	} elsif ($status{'gradingqueue'} eq 'enqueued') {
                   1577: 	    $result .= &mt('Awaiting Grading');
                   1578: 	} elsif ($status{'gradingqueue'} eq 'locked') {
                   1579: 	    $result .= &mt('Being Graded');
1.58      albertel 1580: 	} elsif ($status{'gradingqueue'} eq 'in_progress') {
                   1581: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1582: 	} else {
                   1583: 	    $result.="&nbsp;";
                   1584: 	}
                   1585: 	$result.= '</td></tr>';
                   1586:     }
                   1587:     $result.='</table>';
                   1588:     return $result;
                   1589: }
                   1590: 
                   1591: sub get_student_status {
1.86      albertel 1592:     my ($symb,$cdom,$cnum,$udom,$uname,$type)=@_;
                   1593: 
                   1594:     my %status;
                   1595: 
                   1596:     if ($type eq 'Task') {
                   1597: 	my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
1.49      albertel 1598: 					  $udom,$uname);
1.89      albertel 1599: 	$status{'status'}=$record{'resource.0.status'};
                   1600: 	$status{'version'}=$record{'resource.0.version'};
                   1601: 	$status{'grader'}=$record{'resource.0.regrader'};
1.86      albertel 1602:     }
                   1603:     $status{'reviewqueue'}=
                   1604: 	&check_queue_for_key($cdom,$cnum,'reviewqueue',
                   1605: 			     &encode_queue_key($symb,$udom,$uname));
                   1606:     $status{'gradingqueue'}=
                   1607: 	&check_queue_for_key($cdom,$cnum,'gradingqueue',
                   1608: 			     &encode_queue_key($symb,$udom,$uname));
1.49      albertel 1609:     return %status;
                   1610: }
                   1611: 
1.1       albertel 1612: sub start_ClosingParagraph {
                   1613:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1614:     my $result;
                   1615:     if ($target eq 'web') {
1.13      albertel 1616:     } elsif ($target eq 'webgrade') {
                   1617: 	&Apache::lonxml::startredirection();
1.1       albertel 1618:     }
                   1619:     return $result;
                   1620: }
                   1621: 
                   1622: sub end_ClosingParagraph {
                   1623:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1624:     my $result;
                   1625:     if ($target eq 'web') {
1.13      albertel 1626:     } elsif ($target eq 'webgrade') {
                   1627: 	&Apache::lonxml::endredirection();
1.1       albertel 1628:     }
                   1629:     return $result;
                   1630: }
                   1631: 
1.19      albertel 1632: sub get_id {
                   1633:     my ($parstack,$safeeval)=@_;
                   1634:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                   1635:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                   1636:     return $id;
                   1637: }
                   1638: 
1.1       albertel 1639: my %dimension;
                   1640: sub start_Dimension {
                   1641:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1642:     undef(%dimension);
1.19      albertel 1643:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel 1644:     $Apache::bridgetask::dimension=$dim_id;
                   1645:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                   1646:     undef(@Apache::bridgetask::instance);
1.20      albertel 1647:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
                   1648: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.54      albertel 1649:     &Apache::lonxml::startredirection();
                   1650:     return &internal_location($dim_id);
1.1       albertel 1651: }
                   1652: 
1.13      albertel 1653: sub get_instance {
1.75      albertel 1654:     my ($dim)=@_;
                   1655:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                   1656:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                   1657: 	$rand_alg eq '64bit2' || $rand_alg eq '64bit3' ||
                   1658: 	$rand_alg eq '64bit4' ) {
                   1659: 	&Apache::response::pushrandomnumber();
                   1660: 	my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
                   1661: 	my $num=@order;
                   1662: 	my $version=&get_version();
                   1663: 	my $which=($version-1)%$num;
                   1664: 	return $order[$which];
                   1665:     } else {
                   1666: 	my ($version,$previous) = &get_version();
                   1667: 	my $instance = 
                   1668: 	    $Apache::lonhomework::history{"resource.$version.0.$dim.instance"};
                   1669: 	if (defined($instance)) { return $instance; }
                   1670: 
                   1671: 	&Apache::response::pushrandomnumber();
                   1672: 	my @instances = @{$dimension{'instances'}};
                   1673: 	# remove disabled instances
                   1674: 	for (my $i=0; $i < $#instances; $i++) {
                   1675: 	    if ($dimension{$instances[$i].'.disabled'}) {
                   1676: 		splice(@instances,$i,1);
                   1677: 		$i--;
                   1678: 	    }
                   1679: 	}
                   1680: 	@instances = &Math::Random::random_permutation(@instances);
                   1681: 	$instance  = $instances[($version-1)%scalar(@instances)];
                   1682: 	$Apache::lonhomework::results{"resource.$version.0.$dim.instance"} = 
                   1683: 	    $instance;
1.79      albertel 1684: 	$Apache::lonhomework::results{'INTERNAL_store'} = 1; 
1.75      albertel 1685: 	&Apache::response::poprandomnumber();
                   1686: 	return $instance;
                   1687:     }
1.13      albertel 1688: }
                   1689: 
1.18      albertel 1690: {
                   1691:     my $last_link;
                   1692:     sub end_Dimension {
                   1693: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.54      albertel 1694: 	my $result=&Apache::lonxml::endredirection();
1.25      albertel 1695: 	my $dim=&get_id($parstack,$safeeval);
1.75      albertel 1696: 	my $instance=&get_instance($dim);
1.25      albertel 1697: 	my $version=&get_version();
1.18      albertel 1698: 	if ($target eq 'web') {
1.47      albertel 1699: 	    @Apache::scripttag::parser_env = @_;
                   1700: 	    $result.=&Apache::scripttag::xmlparse($dimension{'intro'});
1.76      albertel 1701: 	    my @instances = $instance;
                   1702: 	    if (&Apache::response::showallfoils()) {
                   1703: 		@instances = @{$dimension{'instances'}};
                   1704: 	    }
                   1705: 	    foreach my $instance (@instances) {
                   1706: 		@Apache::scripttag::parser_env = @_;
                   1707: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.89      albertel 1708: 		if ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass' ||
                   1709: 		    $Apache::lonhomework::history{"resource.$version.0.status"} eq 'fail') {
1.76      albertel 1710: 
1.89      albertel 1711: 		    my $dim_status=$Apache::lonhomework::history{"resource.$version.0.$dim.status"};
1.76      albertel 1712: 		    my $mandatory='Mandatory';
                   1713: 		    if ($Apache::bridgetask::dimensionmandatory{$dim} eq 'N') {
                   1714: 			$mandatory='Optional';
                   1715: 		    }
1.116   ! albertel 1716: 		    my $dim_info="<div class='LC_$dim_status LC_question_grade'>\n";
1.76      albertel 1717: 		    if ($dim_status eq 'pass') {
                   1718: 			$dim_info.='<h3>Question : you passed this '.$mandatory.' question</h3>';
1.54      albertel 1719: 		    }
1.76      albertel 1720: 		    if ($dim_status eq 'fail') {
                   1721: 			$dim_info.='<h3>Question : you did not pass this '.$mandatory.' question</h3>';
1.53      albertel 1722: 		    }
1.76      albertel 1723: 		    my $man_count=0;
                   1724: 		    my $man_passed=0;
                   1725: 		    my $opt_count=0;
                   1726: 		    my $opt_passed=0;
                   1727: 		    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1728: 			if ($dimension{$instance.'.criteria.'.$id.'.mandatory'} 
                   1729: 			    eq 'N') {
                   1730: 			    $opt_count++;
1.89      albertel 1731: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"} eq 'pass') {
1.76      albertel 1732: 				$opt_passed++;
                   1733: 			    }
                   1734: 			} else {
                   1735: 			    $man_count++;
1.89      albertel 1736: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"} eq 'pass') {
1.76      albertel 1737: 				$man_passed++;
                   1738: 			    }
                   1739: 			}
1.22      albertel 1740: 		    }
1.76      albertel 1741: 		    if ($man_passed eq $man_count) { $man_passed='all'; }
                   1742: 		    my $opt_req=$dimension{$instance.'.optionalrequired'};
                   1743: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
                   1744: 		    $dim_info.="\n<p>".&mt('You passed [_1] of the [_2] mandatory components and [_3] of the [_4] optional components, of which you were required to pass [_5].',$man_passed,$man_count,$opt_passed,$opt_count,$opt_req)."</p>\n</div>";
                   1745: 
                   1746: 		    my $internal_location=&internal_location($dim);
                   1747: 		    $result=~s/\Q$internal_location\E/$dim_info/;
                   1748: 
                   1749: 		    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.89      albertel 1750: 			my $status=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"};
                   1751: 			my $comment=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"};
1.76      albertel 1752: 			my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1753: 			if ($mandatory) {
                   1754: 			    $mandatory='Mandatory';
                   1755: 			} else {
                   1756: 			    $mandatory='Optional';
                   1757: 			}
                   1758: 			if ($status eq 'fail') {
                   1759: 			} elsif ($status eq 'pass') {
                   1760: 			} else {
                   1761: 			    &Apache::lonxml::error("Student viewing a graded bridgetask was shown a status of $status");
                   1762: 			}
                   1763: 			my $status_display=$status;
                   1764: 			$status_display=~s/^([a-z])/uc($1)/e;
                   1765: 			@Apache::scripttag::parser_env = @_;
1.116   ! albertel 1766: 			$result.=
        !          1767: 			    '<div class="LC_'.$status.' LC_criteria"><h4>'
        !          1768: 			    .$mandatory.' Criteria</h4><p>';
1.76      albertel 1769: 			@Apache::scripttag::parser_env = @_;
                   1770: 			$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
1.116   ! albertel 1771: 			$result.='</p><p class="LC_grade">'.$status_display.'</p>';
1.89      albertel 1772: 			if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}) {
1.116   ! albertel 1773: 			    $result.='<p class="LC_comment">'.&mt('Comment: [_1]',$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}).'</p>';
1.76      albertel 1774: 			}
                   1775: 			$result.='</div>';
1.22      albertel 1776: 		    }
                   1777: 		}
                   1778: 	    }
1.18      albertel 1779: 	} elsif ($target eq 'webgrade') {
1.47      albertel 1780: 	    # in case of any side effects that we need
                   1781: 	    @Apache::scripttag::parser_env = @_;
                   1782: 	    &Apache::scripttag::xmlparse($dimension{'intro'});
                   1783: 	    @Apache::scripttag::parser_env = @_;
                   1784: 	    &Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.18      albertel 1785: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.116   ! albertel 1786: 		my $link='LC_GRADE_criteria_'.$instance.'_'.$id;
1.89      albertel 1787: 		my $status=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"};
1.116   ! albertel 1788: 		$result.='<div class="LC_GRADE_criteria" id="'.$link.'">'."\n".
1.111     albertel 1789: 		    '<a name="'.$link.'" />'."\n".
                   1790: 		    '<a name="next_'.$last_link.'" />'."\n".
1.116   ! albertel 1791: 		    '<div class="LC_GRADE_criteriatext">'."\n";
1.47      albertel 1792: 		@Apache::scripttag::parser_env = @_;
                   1793: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
1.111     albertel 1794: 		$result.='</div>'."\n".
1.18      albertel 1795: 		    #$dimension{$instance.'.criteria.'.$id}.
1.116   ! albertel 1796: 		    '<div class="LC_GRADE_grade">'."\n".
        !          1797: 		    '<label class="LC_GRADE_ungraded"><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.&mt('Ungraded').'</label>'."\n".
        !          1798: 		    '<label class="LC_GRADE_fail"><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label>'."\n".
        !          1799: 		    '<label class="LC_GRADE_pass"><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.&mt('Pass').'</label>'."\n".
        !          1800: 		    '<label class="LC_GRADE_review"><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.&mt('Review').'</label>'."\n".
1.111     albertel 1801: 		    '</div>'."\n".
1.116   ! albertel 1802: 		    '<label class="LC_GRADE_comment">'.&mt('Additional Comment for Student')."\n".
        !          1803: 		    '<textarea class="LC_GRADE_comment_area" name="HWVAL_comment_'.$link.'">'.&HTML::Entities::encode($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}).'</textarea>'."\n".
1.111     albertel 1804: 		    '</label>'."\n".
1.116   ! albertel 1805: 		    '<ul class="LC_GRADE_navbuttons">'."\n".
1.111     albertel 1806: 		    '<li><a href="#'.$last_link.'">Prev</a></li>'."\n".
                   1807: 		    '<li><a href="#next_'.$link.'">Next</a></li>'."\n".
                   1808: 		    '</ul>'."\n".
                   1809:                     '</div>'."\n";
1.113     albertel 1810: 		$result.=&grading_history($version,$dim,$instance,$id);
1.18      albertel 1811: 		$last_link=$link;
                   1812: 	    }
1.22      albertel 1813: 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
1.19      albertel 1814: 	    my $optional_passed=0;
1.20      albertel 1815: 	    my $mandatory_failed=0;
                   1816: 	    my $ungraded=0;
                   1817: 	    my $review=0;
1.19      albertel 1818: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.89      albertel 1819: 		my $status=$Apache::lonhomework::results{"resource.$version.0.$dim.$instance.$id.status"}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
                   1820: 		$Apache::lonhomework::results{"resource.$version.0.$dim.$instance.$id.comment"}=$env{'form.HWVAL_comment_criteria_'.$instance.'_'.$id};
1.20      albertel 1821: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1822: 		if ($status eq 'pass') {
                   1823: 		    if (!$mandatory) { $optional_passed++; }
                   1824: 		} elsif ($status eq 'fail') {
                   1825: 		    if ($mandatory) { $mandatory_failed++; }
1.21      albertel 1826: 		} elsif ($status eq 'review') {
                   1827: 		    $review++;
1.20      albertel 1828: 		} elsif ($status eq 'ungraded') {
                   1829: 		    $ungraded++;
1.21      albertel 1830: 		} else {
1.47      albertel 1831: 		    $ungraded++;
1.19      albertel 1832: 		}
                   1833: 	    }
1.20      albertel 1834: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
                   1835: 		$mandatory_failed++;
                   1836: 	    }
1.21      albertel 1837: 	    &Apache::lonxml::debug("all instance ".join(':',@{$dimension{$instance.'.criterias'}})." results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
1.20      albertel 1838: 	    if ($review) {
1.89      albertel 1839: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1840: 		    'review';
1.20      albertel 1841: 	    } elsif ($ungraded) {
1.89      albertel 1842: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1843: 		    'ungraded';
1.20      albertel 1844: 	    } elsif ($mandatory_failed) {
1.89      albertel 1845: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1846: 		    'fail';
1.20      albertel 1847: 	    } else {
1.91      albertel 1848: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1849: 		    'pass';
1.20      albertel 1850: 	    }
1.69      albertel 1851: 	} else {
                   1852: 	    # any other targets no output
                   1853: 	    undef($result);
1.13      albertel 1854: 	}
1.18      albertel 1855: 	return $result;
1.1       albertel 1856:     }
                   1857: }
                   1858: 
1.113     albertel 1859: sub grading_history {
                   1860:     my ($version,$dim,$instance,$criteria) = @_;
                   1861:     if (!&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
                   1862: 	return '';
                   1863:     }
                   1864:     my ($result,$grader);
                   1865:     my $scope="resource.$version.0.$dim.$instance.$criteria";
                   1866:     foreach my $t (1..$Apache::lonhomework::history{'version'}) {
                   1867: 	if (exists($Apache::lonhomework::history{$t.':resource.0.regrader'})) {
                   1868: 	    my ($gname,$gdom) = 
                   1869: 		split('@',$Apache::lonhomework::history{$t.':resource.0.regrader'});
                   1870: 	    my $fullname = &Apache::loncommon::plainname($gname,$gdom);
                   1871: 	    $grader = &Apache::loncommon::aboutmewrapper($fullname,
                   1872: 							 $gname,$gdom);
                   1873: 	}
                   1874: 	my $entry;
                   1875: 	if (exists($Apache::lonhomework::history{"$t:$scope.status"})) {
                   1876: 	    $entry.="<tt>".$Apache::lonhomework::history{"$t:$scope.status"}.'</tt>';
                   1877: 	}
                   1878: 	if (exists($Apache::lonhomework::history{"$t:$scope.comment"})) {
                   1879: 	    $entry.=' comment: "'.$Apache::lonhomework::history{"$t:$scope.comment"}.'"';
                   1880: 	}
                   1881: 	if ($entry) {
                   1882: 	    $result.= "<li>$grader : $entry </li>";
                   1883: 	}
                   1884:     }
                   1885:     if ($result) {
1.116   ! albertel 1886: 	return '<ul class="LC_pastgrading">'.$result.'</ul>';
1.113     albertel 1887:     }
                   1888:     return '';
                   1889: }
                   1890: 
1.1       albertel 1891: sub start_IntroParagraph {
1.87      albertel 1892:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel 1893:     my $result;
1.13      albertel 1894:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1895: 	if ($tagstack->[-2] eq 'Dimension') {
1.87      albertel 1896: 	    $dimension{'intro'}=&Apache::lonxml::get_all_text('/introparagraph',$parser,$style);
1.47      albertel 1897: 	
                   1898: 	} elsif ($target eq 'webgrade') {
1.87      albertel 1899: 	    &Apache::lonxml::get_all_text('/introparagraph',$parser,$style);
1.1       albertel 1900: 	}
1.47      albertel 1901: 	
1.1       albertel 1902:     }
                   1903:     return $result;
                   1904: }
                   1905: 
                   1906: sub end_IntroParagraph {
                   1907: }
                   1908: 
                   1909: sub start_Instance {
                   1910:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel 1911:     my $id=&get_id($parstack,$safeeval);
                   1912:     push(@{$dimension{'instances'}},$id);
                   1913:     push(@Apache::bridgetask::instance,$id);
                   1914:     push(@Apache::bridgetask::instancelist,$id);
1.20      albertel 1915:     $dimension{$id.'.optionalrequired'}=
1.19      albertel 1916: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.75      albertel 1917:     my $disabled = &Apache::lonxml::get_param('Disabled',$parstack,$safeeval);
                   1918:     if (lc($disabled) eq 'yes') {
                   1919: 	$dimension{$id.'.disabled'}='1';
                   1920:     }
1.1       albertel 1921:     return '';
                   1922: }
                   1923: 
                   1924: sub end_Instance {
                   1925: }
                   1926: 
                   1927: sub start_InstanceText {
1.87      albertel 1928:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.47      albertel 1929:     my $instance_id=$Apache::bridgetask::instance[-1];
1.87      albertel 1930:     my $text=&Apache::lonxml::get_all_text('/instancetext',$parser,$style);
1.13      albertel 1931:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1932: 	$dimension{$instance_id.'.text'}=$text;
1.1       albertel 1933:     }
                   1934:     return '';
                   1935: }
                   1936: 
                   1937: sub end_InstanceText {
                   1938:     return '';
                   1939: }
                   1940: 
                   1941: sub start_Criteria {
1.87      albertel 1942:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.9       albertel 1943:     my $instance_id=$Apache::bridgetask::instance[-1];
1.87      albertel 1944:     my $criteria=&Apache::lonxml::get_all_text('/criteria',$parser,$style);
1.21      albertel 1945:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.19      albertel 1946: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel 1947: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel 1948: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                   1949: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel 1950: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                   1951:     }
                   1952:     return '';
                   1953: }
                   1954: 
1.47      albertel 1955: sub end_Criteria {
                   1956: }
                   1957: 
1.4       albertel 1958: sub proctor_validation_screen {
                   1959:     my ($slot) = @_;
                   1960:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5       albertel 1961:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.44      albertel 1962:     my $name=&Apache::loncommon::plainname($user,$domain);
                   1963:     
1.4       albertel 1964:     my $msg;
1.11      albertel 1965:     if ($env{'form.proctorpassword'}) {
1.4       albertel 1966: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                   1967: 	    .'</font></p>';
                   1968:     }
1.47      albertel 1969:     if (!$env{'form.proctordomain'}) { $env{'form.proctordomain'}=$domain; }
1.4       albertel 1970:     my $result= (<<ENDCHECKOUT);
                   1971: <h2>Proctor Validation</h2>
                   1972:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   1973:     $msg
1.115     albertel 1974: <form name="checkout" method="post" action="$env{'request.uri'}">
1.4       albertel 1975: <input type="hidden" name="validate" value="yes" />
                   1976: <input type="hidden" name="submitted" value="yes" />
                   1977: <table>
1.44      albertel 1978:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$env{'form.proctorname'}" /></td></tr>
1.4       albertel 1979:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.46      albertel 1980:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$env{'form.proctordomain'}" /></td></tr>
1.4       albertel 1981: </table>
                   1982: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
1.44      albertel 1983: <table border="1">
                   1984:   <tr><td>
                   1985:     <table>
                   1986:       <tr><td colspan="2">Student who should be logged in is:</td></tr>
                   1987:       <tr><td>Name:</td><td>$name</td></tr>
1.45      albertel 1988:       <tr><td>Student ID:</td><td>$env{'environment.id'}</td></tr>
1.44      albertel 1989:       <tr><td>Usename</td><td>$user\@$domain</td></tr>
                   1990:       <tr><td colspan="2"><img src="$url" /></td></tr>
                   1991:     </table>
                   1992:   </tr></td>
                   1993: </table>
1.4       albertel 1994: </form>
                   1995: ENDCHECKOUT
                   1996:     return $result;
                   1997: }
                   1998: 
1.1       albertel 1999: 1;
                   2000: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>