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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.107   ! albertel    4: # $Id: bridgetask.pm,v 1.106 2006/02/09 23:02:51 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.29      albertel  158:     $result.='<form name="getprevious" method="POST" action="';
                    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);
                    239:     $result.='<form method="POST" action="/adm/slotrequest">'.
                    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">
                    263: .fail, .pass, .neutral {
                    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: 
                    276: .fail h4, .pass h4 {
                    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: 
                    286: .fail {
                    287: 	color: red;
                    288: }
                    289: 
                    290: .pass {
                    291:     color : green;
                    292: }
                    293: 
                    294: .neutral {
                    295: 	color : blue;
                    296: }
                    297: 
                    298: .question {
                    299: 	border : thin solid black;
                    300: 	padding : 4px;
                    301: 	margin-bottom : 1em;
                    302: }
                    303: 
                    304: .grade {
                    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/';
                    378:     my $file_list="<ul>\n";
                    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.70      albertel  384: 	    &Apache::loncommon::icon($file).'" border=0"> '.$file.
                    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;
                    394: <div class="handininfo">
                    395:   <input type="submit" name="next" value="$lt{'done'}" />
                    396:   <input type="submit" name="stop" value="$lt{'stop'}" />
                    397:   $file_list
                    398: </div>
                    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) {
                    423: 	    $body_tag_start.='<form name="gradesubmission" method="POST" action="';
                    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'})) {
                    429: 		$body_tag_start.='<form method="POST" action="/adm/slotrequest">'.
                    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>';
                    435: 	    }
1.13      albertel  436: 	}
1.8       albertel  437:     }
1.74      albertel  438:     if ($target eq 'web' && $env{'request.state'} eq 'construct') {
                    439: 	$form_tag_start.=&Apache::structuretags::problem_web_to_edit_header($env{'form.rndseed'});
                    440:     }
1.21      albertel  441:     if ($target eq 'web' || ($target eq 'grade' && !$env{'form.webgrade'}) || $target eq 'answer' ||
1.1       albertel  442: 	$target eq 'tex') {
1.29      albertel  443: 	my ($version,$previous)=&get_version();
1.14      albertel  444: 	($status,$accessmsg,my $slot_name,$slot) = 
1.81      albertel  445: 	    &Apache::lonhomework::check_slot_access('0','Task');
1.9       albertel  446: 	push(@Apache::inputtags::status,$status);
1.14      albertel  447: 	$Apache::inputtags::slot_name=$slot_name;
1.1       albertel  448: 	my $expression='$external::datestatus="'.$status.'";';
1.89      albertel  449: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$version.0.solved"}.'";';
1.1       albertel  450: 	&Apache::run::run($expression,$safeeval);
                    451: 	&Apache::lonxml::debug("Got $status");
1.25      albertel  452: 	$body_tag_start.=&add_previous_version_button($status);
1.54      albertel  453: 	if (!&show_task($status,$previous)) {
1.87      albertel  454: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.1       albertel  455: 	    if ( $target eq "web" ) {
1.4       albertel  456: 		$result.= $head_tag_start.'</head>'.$body_tag_start;
1.74      albertel  457: 		if ($env{'request.state'} eq 'construct') {
                    458: 		    $result.=$form_tag_start;
                    459: 		}
1.4       albertel  460: 		my $msg;
1.1       albertel  461: 		if ($status eq 'UNAVAILABLE') {
                    462: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3       albertel  463: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    464: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.38      albertel  465: 		    $msg.=&add_request_another_attempt_button("Sign up for time to work.");
1.4       albertel  466: 		} elsif ($status eq 'NEEDS_CHECKIN') {
                    467: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
                    468: 			'</h1>'.&proctor_validation_screen($slot);
1.22      albertel  469: 		} elsif ($status eq 'WAITING_FOR_GRADE') {
                    470: 		    $msg.='<h1>'.&mt('Your submission is in the grading queue.').'</h1>';
1.64      albertel  471: 		} elsif ($env{'form.donescreen'}) {
                    472: 		    my $title=&Apache::lonnet::gettitle();
1.67      albertel  473: 		    my @files=split(',',$Apache::lonhomework::history{'resource.'.$version.'.0.bridgetask.portfiles'});
                    474: 		    my $files='<ul><li>'.join('</li><li>',@files).'</li></ul>';
1.64      albertel  475: 		    $result.=<<DONESCREEN;
                    476: <h2>$title</h2>
                    477: <p> Files submitted: $files </p>
1.67      albertel  478: <p> You are now done with this Bridge Task </p>
1.64      albertel  479: <hr />
                    480: <p> <a href="/adm/logout">Logout</a> </p>
                    481: <p> <a href="/adm/roles">Change to a different course</a> </p>
                    482: DONESCREEN
1.1       albertel  483: 		} elsif ($status ne 'NOT_YET_VIEWED') {
                    484: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
                    485: 		}
                    486: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
                    487: 		    $msg.='The problem '.$accessmsg;
                    488: 		}
                    489: 		$result.=$msg.'<br />';
                    490: 	    } elsif ($target eq 'tex') {
                    491: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
                    492: 		if ($status eq 'UNAVAILABLE') {
                    493: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
                    494: 		} else {
                    495: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
                    496: 		}
1.22      albertel  497: 	    } elsif ($target eq 'grade' && !$env{'form.webgrade'}) {
1.4       albertel  498: 		if ($status eq 'NEEDS_CHECKIN') {
1.83      albertel  499: 		    if(&proctor_check_auth($slot_name,$slot,'Task')
                    500: 		       && defined($Apache::inputtags::slot_name)) {
1.77      albertel  501: 			my $result=&add_to_queue('gradingqueue',
1.82      albertel  502: 						 [$Apache::inputtags::slot_name]);
1.77      albertel  503: 			&Apache::lonxml::debug("add_to_queue said $result");
                    504: 		    }
1.4       albertel  505: 		}
1.1       albertel  506: 	    }
                    507: 	} elsif ($target eq 'web') {
                    508: 	    my $name= &Apache::structuretags::get_resource_name($parstack,$safeeval);
                    509: 	    $result.="$head_tag_start<title>$name</title></head>
1.53      albertel  510:               $body_tag_start \n".&style();
1.36      albertel  511: 	    
1.57      albertel  512: 	    $result.=&preserve_grade_info();
                    513: 	    $result.=&internal_location();
1.36      albertel  514: 	    $result.=$form_tag_start.
                    515: 		'<input type="hidden" name="submitted" value="yes" />';
1.54      albertel  516: 	    &Apache::lonxml::startredirection();
1.1       albertel  517: 	}
1.21      albertel  518:     } elsif ( ($target eq 'grade' && $env{'form.webgrade'}) ||
                    519: 	      $target eq 'webgrade') {
1.32      albertel  520: 	my $webgrade='yes';
1.21      albertel  521: 	if ($target eq 'webgrade') {
1.32      albertel  522: 	    $result.=$head_tag_start.$body_tag_start;
1.49      albertel  523: 	    #$result.='<br />Review'.&show_queue('reviewqueue');
                    524: 	    #$result.='<br />Grade'.&show_queue('gradingqueue');
1.30      albertel  525: 	}
1.33      albertel  526: 	# FIXME Blast! still need to reorg this, need to reshow the
                    527:         #       queue being reviewed once done with the grade pass...
                    528:         #       Hrrm, vaildation pass should perhaps say 'not_locked'
                    529:         #       perhaps do a search if there is a key that is mine and if
                    530:         #       there isn't reshow the queue....
1.105     albertel  531: 	my ($todo,$status_code,$msg)=&get_key_todo($target);
1.33      albertel  532: 
                    533: 	if ($todo) {
                    534: 	    &setup_env_for_other_user($todo,$safeeval);
                    535: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
                    536: 	    $result.="\n".'<table><tr><td>Found '.
                    537: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
                    538: 	    $form_tag_start.=
                    539: 		'<input type="hidden" name="gradingkey" value="'.
                    540: 		&Apache::lonnet::escape($todo).'" />';
                    541: 	    $Apache::bridgetask::queue_key=$todo;
                    542: 	    &Apache::structuretags::initialize_storage();
                    543: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
                    544: 	    if ($target eq 'webgrade') {
1.49      albertel  545: 		#$result.='<br />After -'.&show_queue($env{'form.queue'});
1.33      albertel  546: 		$result.="\n".'<table width="100%" style="width:100%" border="1">';
                    547: 		if ($status_code eq 'selected') {
                    548: 		    $form_tag_start.=
                    549: 			'<input type="hidden" name="queuemode" value="selected" />';
                    550: 		}
                    551: 	    }
1.15      albertel  552: 	} else {
1.33      albertel  553: 	    if ($target eq 'webgrade') {
                    554: 		$result.="\n";
1.81      albertel  555: 		my $back='<p><a href="/adm/flip?postdata=return:">'.
                    556: 		    &mt('Return to resource').'</a></p>';
1.33      albertel  557: 		if      ($status_code eq 'stop') {
1.81      albertel  558: 		    $result.='<b>'.&mt("Stopped grading.").'</b>'.$back;
1.33      albertel  559: 		} elsif ($status_code eq 'lock_failed') {
1.105     albertel  560: 		    $result.='<b>'.&mt("Failed to lock the requested record.")
1.81      albertel  561: 			.'</b>'.$back;
1.33      albertel  562: 		} elsif ($status_code eq 'unlock') {
1.81      albertel  563: 		    $result.='<b>'.&mt("Unlocked the requested record.")
                    564: 			.'</b>'.$back;
1.33      albertel  565: 		    $result.=&show_queue($env{'form.queue'},1);
                    566: 		} elsif ($status_code eq 'show_list') {
                    567: 		    $result.=&show_queue($env{'form.queue'},1);
1.49      albertel  568: 		} elsif ($status_code eq 'select_user') {
                    569: 		    $result.=&select_user();
1.95      albertel  570: 		} elsif ($status_code eq 'unable') {
                    571: 		    $result.='<b>'.&mt("Unable to aqcuire a user to grade.").'</b>'.$back;
1.105     albertel  572: 		} elsif ($status_code eq 'not_allowed') {
                    573: 		    $result.='<b>'.&mt('Not allowed to grade the requested user.').' '.$msg.'</b>'.$back;
1.33      albertel  574: 		} else {
1.81      albertel  575: 		    $result.='<b>'.&mt("No user to be graded.").'</b>'.$back;
1.32      albertel  576: 		}
1.21      albertel  577: 	    }
1.33      albertel  578: 	    $webgrade='no';
1.87      albertel  579: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.32      albertel  580: 	}
                    581: 	if ($target eq 'webgrade' && defined($env{'form.queue'})) {
1.61      albertel  582: 	    if ($webgrade eq 'yes') {
                    583: 		$result.=&submission_time_stamp();
                    584: 	    }
1.32      albertel  585: 	    $result.=$form_tag_start;
                    586: 	    $result.='<input type="hidden" name="webgrade" value="'.
                    587: 		$webgrade.'" />';
                    588: 	    $result.='<input type="hidden" name="queue" value="'.
                    589: 		$env{'form.queue'}.'" />';
1.52      albertel  590: 	    if ($env{'form.regrade'}) {
                    591: 		$result.='<input type="hidden" name="regrade" value="'.
                    592: 		    $env{'form.regrade'}.'" />';
                    593: 	    }
1.62      albertel  594: 	    if ($env{'form.chosensections'}) {
                    595: 		my @chosen_sections=
                    596: 		    &Apache::loncommon::get_env_multiple('form.chosensections');
                    597: 		foreach my $sec (@chosen_sections) {
                    598: 		    $result.='<input type="hidden" name="chosensections" 
                    599:                                value="'.$sec.'" />';
                    600: 		}
                    601: 	    }
1.70      albertel  602: 	    if ($webgrade eq 'yes') { $result.=&webgrade_standard_info(); }
1.15      albertel  603: 	}
1.74      albertel  604:     } elsif ($target eq 'edit') {
                    605: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
                    606: 	    &Apache::structuretags::problem_edit_header();
                    607: 	$Apache::lonxml::warnings_error_header=
                    608: 	    &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 />";
                    609: 	my $temp=&Apache::edit::insertlist($target,$token);
                    610: 	$result.=$temp;
1.1       albertel  611:     } else {
                    612: 	# page_start returned a starting result, delete it if we don't need it
                    613: 	$result = '';
                    614:     }
                    615:     return $result;
                    616: }
                    617: 
1.32      albertel  618: sub get_key_todo {
                    619:     my ($target)=@_;
                    620:     my $todo;
1.33      albertel  621: 
                    622:     if (defined($env{'form.reviewasubmission'})) {
1.54      albertel  623: 	&Apache::lonxml::debug("review a submission....");
1.33      albertel  624: 	$env{'form.queue'}='reviewqueue';
                    625: 	return (undef,'show_list');
                    626:     }
                    627: 
                    628:     if (defined($env{'form.reviewagrading'})) {
                    629: 	&Apache::lonxml::debug("review a grading....");
                    630: 	$env{'form.queue'}='gradingqueue';
                    631: 	return (undef,'show_list');
                    632:     }
                    633: 
1.49      albertel  634:     if (defined($env{'form.regradeasubmission'})) {
                    635: 	&Apache::lonxml::debug("regrade a grading....");
                    636: 	$env{'form.queue'}='none';
                    637: 	return (undef,'select_user');
                    638:     }
                    639: 
1.105     albertel  640: 
                    641:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    642: 
                    643:     #need to try both queues..
                    644:     if (defined($env{'form.regradeaspecificsubmission'}) &&
                    645: 	defined($env{'form.gradinguser'})               &&
                    646: 	defined($env{'form.gradingdomain'})               ) {
                    647: 	my ($symb,$cid)=&Apache::lonxml::whichuser();
                    648: 	my $cnum  = $env{'course.'.$cid.'.num'};
                    649: 	my $cdom  = $env{'course.'.$cid.'.domain'};
                    650: 	my $uname = $env{'form.gradinguser'};
                    651: 	my $udom  = $env{'form.gradingdomain'};
                    652: 
                    653: 	my $gradingkey=&encode_queue_key($symb,$udom,$uname);
                    654: 
                    655: 	my $queue;
                    656: 
                    657: 	if      (&in_queue('gradingqueue',$symb,$cdom,$cnum,$udom,$uname)) {
                    658: 	    $env{'form.queue'} = $queue = 'gradingqueue';
                    659: 	} elsif (&in_queue('reviewqueue' ,$symb,$cdom,$cnum,$udom,$uname)) {
                    660: 	    $env{'form.queue'} = $queue = 'reviewqueue';
                    661: 	}
                    662: 	
                    663: 	if (!$queue) {
                    664: 	    $env{'form.queue'} = $queue = 'none';
                    665: 	    #not queued so doing either a re or pre grade
                    666: 	    return ($gradingkey);
                    667: 	}
                    668: 
                    669: 	my $who=&queue_key_locked($queue,$gradingkey);
                    670: 	if ($who eq $me) {
                    671: 	    #already have the lock
                    672: 	    $env{'form.gradingkey'}=&Apache::lonnet::escape($gradingkey);
                    673: 	    return ($gradingkey);
                    674: 	}
                    675: 	
                    676: 	if (!defined($who)) {
                    677: 	    if (&lock_key($queue,$gradingkey)) {
                    678: 		return ($gradingkey);
                    679: 	    } else {
                    680: 		return (undef,'lock_failed');
                    681: 	    }
                    682: 	}
                    683: 
                    684: 	#otherwise (defined($who) && $who ne $me) some else has it...
                    685: 	return (undef,'not_allowed',
                    686: 		&mt('Another user ([_1]) currently has the record for [_2] locked.',
                    687: 		    $who,$env{'form.gradinguser'}.'@'.$env{'form.gradingdomain'}));
                    688:     }
                    689: 
                    690: 
1.32      albertel  691:     my $queue=$env{'form.queue'};
1.33      albertel  692: 
1.32      albertel  693:     if (!defined($queue)) {
                    694: 	$env{'form.queue'}=$queue='gradingqueue';
                    695:     }
1.33      albertel  696: 
1.32      albertel  697:     my $gradingkey=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.33      albertel  698: 
1.49      albertel  699:     if ($env{'form.queue'} eq 'none') {
                    700: 	if (defined($env{'form.gradingkey'})) {
                    701: 	    if ($target eq 'webgrade') {
                    702: 		if ($env{'form.stop'}) {
                    703: 		    return (undef,'stop');
                    704: 		} elsif ($env{'form.next'}) {
1.59      albertel  705: 		    return (undef,'select_user');
1.49      albertel  706: 		}
                    707: 	    }
                    708: 	    return ($gradingkey,'selected');
                    709: 	} else {
1.59      albertel  710: 	    return (undef,'select_user');
1.49      albertel  711: 	}
                    712:     }
1.32      albertel  713:     if (defined($env{'form.queue'}) && defined($env{'form.gradingkey'})
1.33      albertel  714: 	&& !defined($env{'form.gradingaction'}) 
                    715: 	&& $env{'form.queuemode'} eq 'selected') {
                    716: 	
                    717: 	my $who=&queue_key_locked($queue,$gradingkey);
                    718: 	if ($who eq $me) {
                    719: 	    &Apache::lonxml::debug("Found a key was given to me");
                    720: 	    return ($gradingkey,'selected');
                    721: 	} else {
                    722: 	    return (undef,'show_list');
                    723: 	}
                    724: 
                    725:     }
                    726: 
                    727:     if ($target eq 'webgrade' && $env{'form.queuemode'} eq 'selected') {
                    728: 	if ($env{'form.gradingaction'} eq 'resume') {
                    729: 	    delete($env{'form.gradingaction'});
                    730: 	    &Apache::lonxml::debug("Resuming a key");
1.32      albertel  731: 	    return ($gradingkey);
1.33      albertel  732: 	} elsif ($env{'form.gradingaction'} eq 'unlock') {
                    733: 	    &Apache::lonxml::debug("Unlocking a key ".
                    734: 				     &check_queue_unlock($queue,$gradingkey,1));
                    735: 	    return (undef,'unlock');
                    736: 	} elsif ($env{'form.gradingaction'} eq 'select') {
                    737: 	    &Apache::lonxml::debug("Locking a key");
                    738: 	    if (&lock_key($queue,$gradingkey)) {
                    739: 		&Apache::lonxml::debug("Success $queue");
                    740: 		return ($gradingkey);
                    741: 	    }
                    742: 	    &Apache::lonxml::debug("Failed $queue");
                    743: 	    return (undef,'lock_failed');
1.32      albertel  744: 	}
                    745:     }
1.33      albertel  746: 
                    747:     if ($env{'form.queuemode'} ne 'selected') {
                    748: 	# don't get something new from the queue if they hit the stop button
                    749:     	if (!($env{'form.stop'} && $target eq 'webgrade') 
                    750: 	    && !$env{'form.gradingaction'}) {
                    751: 	    &Apache::lonxml::debug("Getting anew $queue");
                    752: 	    return (&get_from_queue($queue));
                    753: 	} else {
                    754: 	    return (undef,'stop');
                    755: 	}
1.32      albertel  756:     }
1.33      albertel  757:     return (undef,undef)
1.32      albertel  758: }
1.94      albertel  759: 
                    760: sub minimize_storage {
                    761:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    762: 	if ($key =~ /regrader$/) { next; }
                    763: 	if ($Apache::lonhomework::results{$key} eq
                    764: 	    $Apache::lonhomework::history{$key}) {
                    765: 	    delete($Apache::lonhomework::results{$key});
                    766: 	}
                    767:     }
                    768: }
                    769: 
1.1       albertel  770: sub end_Task {
                    771:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    772:     my $result='';
                    773:     my $status=$Apache::inputtags::status['-1'];
1.29      albertel  774:     my ($version,$previous)=&get_version();
1.1       albertel  775:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.15      albertel  776: 	$target eq 'tex') {
1.69      albertel  777: 	if ($target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.1       albertel  778: 	    if ($target eq 'web') {
1.54      albertel  779: 		if (&show_task($status,$previous)) {
                    780: 		    $result.=&Apache::lonxml::endredirection();
                    781: 		}
1.64      albertel  782: 		if ($status eq 'CAN_ANSWER' && !$previous && 
                    783: 		    !$env{'form.donescreen'}) {
1.15      albertel  784: 		    $result.="\n".'<table border="1">'.
1.28      albertel  785: 			&Apache::inputtags::file_selector("$version.0",
                    786: 							  "bridgetask","*",
1.46      albertel  787: 							  'portfolioonly',
                    788: 							  '
                    789: <h2>'.&mt('Submit Portfolio Files for Grading').'</h2>
                    790: <p>'.&mt('Indicate the files from your portfolio to be evaluated in grading this task.').'</p>').
1.9       albertel  791: 							  "</table>";
1.77      albertel  792: 		}
1.78      albertel  793: 		if (!$previous && $status ne 'SHOW_ANSWER' &&
                    794: 		    &show_task($status,$previous)) {
1.9       albertel  795: 		    $result.=&Apache::inputtags::gradestatus('0');
1.64      albertel  796: 		    $result.='</form>';
                    797: 		    $result.=<<DONEBUTTON;
                    798: <form method="POST">
                    799:    <input type="hidden" name="donescreen" value="1" />
                    800:    <input type="submit" value="Done" />
                    801: </form>
                    802: DONEBUTTON
1.77      albertel  803:                 }
1.56      albertel  804: 		if (&show_task($status,$previous) &&
1.89      albertel  805: 		    $Apache::lonhomework::history{"resource.$version.0.status"} =~ /^(pass|fail)$/) {
                    806: 		    my $bt_status=$Apache::lonhomework::history{"resource.$version.0.status"};
1.54      albertel  807: 		    my $title=&Apache::lonnet::gettitle();
1.80      albertel  808: 
                    809: 		    my $slot_name=
1.89      albertel  810: 			$Apache::lonhomework::history{"resource.$version.0.checkedin.slot"};
1.80      albertel  811: 		    my %slot=&Apache::lonnet::get_slot($slot_name);
1.55      albertel  812: 		    my $start_time=
                    813: 			&Apache::lonlocal::locallocaltime($slot{'starttime'});
1.54      albertel  814: 
1.80      albertel  815: 		    my $status = "\n<div class='$bt_status'>\n";
1.54      albertel  816: 		    
                    817: 		    if ($bt_status eq 'pass')  {
                    818: 			$status.='<h2>You passed the '.$title.' given on '.
1.80      albertel  819: 			    $start_time.'</h2>';
1.54      albertel  820: 		    }
                    821: 		    if ($bt_status eq 'fail')  {
                    822: 			$status.='<h2>You did not pass the '.$title.' given on '.
1.80      albertel  823: 			    $start_time.'</h2>';
1.54      albertel  824: 			if (!$previous) {
                    825: 			    $status.=&add_request_another_attempt_button();
                    826: 			}
                    827: 		    }
                    828: 		    my $man_count=0;
                    829: 		    my $opt_count=0;
                    830: 		    my $opt_passed=0;
                    831: 		    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    832: 			if ($Apache::bridgetask::dimensionmandatory{$dim_id}
                    833: 			    eq 'N') {
                    834: 			    $opt_count++;
1.89      albertel  835: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim_id.status"} eq 'pass') {
1.54      albertel  836: 				$opt_passed++;
                    837: 			    }
                    838: 			} else {
                    839: 			    $man_count++;
                    840: 			}
                    841: 		    }
                    842: 		    my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
                    843: 							 $parstack,$safeeval);
                    844: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
1.99      albertel  845: 		    $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  846: 
                    847: 		    my $internal_location=&internal_location();
                    848: 		    $result=~s/\Q$internal_location\E/$status/;
                    849: 		}
1.13      albertel  850: 	    } 
                    851: 	    if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  852: 		$result.=&Apache::lonxml::xmlend().'</html>';
                    853: 	    }
                    854: 	}
1.29      albertel  855: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel  856: 	    my $award='SUBMITTED';
1.28      albertel  857: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel  858: 						    'portfiles',\$award);
1.14      albertel  859: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel  860: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                    861: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    862: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                    863: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                    864: 
                    865: 		$Apache::lonhomework::results{"resource.0.award"}=
                    866: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                    867: 		    $award;
1.51      albertel  868: 		$Apache::lonhomework::results{"resource.0.submission"}=
                    869: 		    $Apache::lonhomework::results{"resource.$version.0.submission"}='';
1.64      albertel  870: 	    } else {
                    871: 		delete($Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"});
1.77      albertel  872: 		$award = '';
1.10      albertel  873: 	    }
1.4       albertel  874: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    875: 	    &Apache::structuretags::finalize_storage();
1.83      albertel  876: 	    if ($award eq 'SUBMITTED'
                    877: 		&& defined($Apache::inputtags::slot_name)) {
1.82      albertel  878: 		&add_to_queue('gradingqueue',[$Apache::inputtags::slot_name]);
1.14      albertel  879: 	    }
1.79      albertel  880: 	} elsif ($Apache::lonhomework::results{'INTERNAL_store'}) {
                    881: 	    &Apache::structuretags::finalize_storage();
1.1       albertel  882: 	}
1.32      albertel  883: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes') {
1.20      albertel  884: 	    my $optional_required=
                    885: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                    886: 					   $safeeval);
                    887: 	    my $optional_passed=0;
                    888: 	    my $mandatory_failed=0;
                    889: 	    my $ungraded=0;
                    890: 	    my $review=0;   
1.21      albertel  891: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.20      albertel  892: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    893: 		my $status=
1.89      albertel  894: 		    $Apache::lonhomework::results{"resource.$version.0.$dim_id.status"};
1.20      albertel  895: 		my $mandatory=
                    896: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
                    897: 		if ($status eq 'pass') {
                    898: 		    if (!$mandatory) { $optional_passed++; }
                    899: 		} elsif ($status eq 'fail') {
                    900: 		    if ($mandatory) { $mandatory_failed++; }
                    901: 		} elsif ($status eq 'ungraded') {
                    902: 		    $ungraded++;
                    903: 		} elsif ($status eq 'review') {
                    904: 		    $review++;
1.49      albertel  905: 		} else {
                    906: 		    $ungraded++;
                    907: 		}
1.20      albertel  908: 	    }
                    909: 	    if ($optional_passed < $optional_required) {
                    910: 		$mandatory_failed++;
                    911: 	    }
1.21      albertel  912: 	    &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  913: 	    $Apache::lonhomework::results{'resource.0.regrader'}=
1.20      albertel  914: 		$env{'user.name'}.'@'.$env{'user.domain'};
                    915: 	    if ($review) {
1.89      albertel  916: 		$Apache::lonhomework::results{"resource.$version.0.status"}='review';
1.33      albertel  917: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    918: 		    &check_queue_unlock($env{'form.queue'});
                    919: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                    920: 		} else {
1.49      albertel  921: 		    &move_between_queues($env{'form.queue'},'reviewqueue');
1.33      albertel  922: 		}
1.20      albertel  923: 	    } elsif ($ungraded) {
1.89      albertel  924: 		$Apache::lonhomework::results{"resource.$version.0.status"}='ungraded';
1.49      albertel  925: 		if ($env{'form.queue'} eq 'reviewqueue' ||
                    926: 		    $env{'form.queue'} eq 'none' ) {
1.33      albertel  927: 		    &Apache::lonxml::debug("moving back.");
1.49      albertel  928: 		    &move_between_queues($env{'form.queue'},'gradingqueue');
1.33      albertel  929: 		} else {
                    930: 		    &check_queue_unlock($env{'form.queue'});
                    931: 		}
1.20      albertel  932: 	    } elsif ($mandatory_failed) {
1.89      albertel  933: 		$Apache::lonhomework::results{"resource.$version.0.status"}='fail';
1.25      albertel  934: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                    935: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                    936: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.39      albertel  937: 		&remove_from_queue($env{'form.queue'}); 
                    938: 
                    939: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  940: 		
                    941: 		if ($env{'form.regrade'} ne 'yes') {
                    942: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    943: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    944: 		    &Apache::grades::version_portfiles(
                    945: 						       \%Apache::lonhomework::results,
                    946: 						       ["$version.0.bridgetask"],$courseid,
                    947: 						       $symb,$udom,$uname,
                    948: 						       ["$version.0.bridgetask"]);
                    949: 		}
1.20      albertel  950: 	    } else {
1.89      albertel  951: 		$Apache::lonhomework::results{"resource.$version.0.status"}='pass';
1.25      albertel  952: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                    953: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                    954: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.32      albertel  955: 		&remove_from_queue($env{'form.queue'});
1.39      albertel  956: 
                    957: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  958: 		if ($env{'form.regrade'} ne 'yes') {
                    959: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    960: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    961: 		    &Apache::grades::version_portfiles(
                    962: 						       \%Apache::lonhomework::results,
                    963: 						       ["$version.0.bridgetask"],$courseid,
                    964: 						       $symb,$udom,$uname,
                    965: 						       ["$version.0.bridgetask"]);
                    966: 		}
1.20      albertel  967: 	    }
1.89      albertel  968: 	    $Apache::lonhomework::results{"resource.0.status"}=
                    969: 		$Apache::lonhomework::results{"resource.$version.0.status"};
1.28      albertel  970: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel  971: 		$Apache::lonhomework::results{"resource.0.award"}=
1.50      albertel  972: 		    $Apache::lonhomework::results{"resource.$version.0.award"};
1.26      albertel  973: 		$Apache::lonhomework::results{"resource.0.awarded"}=
1.50      albertel  974: 		    $Apache::lonhomework::results{"resource.$version.0.awarded"};
1.26      albertel  975: 		$Apache::lonhomework::results{"resource.0.solved"}=
1.50      albertel  976: 		    $Apache::lonhomework::results{"resource.$version.0.solved"};
1.25      albertel  977: 	    }
1.94      albertel  978: 	    &minimize_storage();
1.21      albertel  979: 	    &Apache::structuretags::finalize_storage();
1.20      albertel  980: 	}
1.15      albertel  981:     } elsif ($target eq 'webgrade') {
1.18      albertel  982: 	$result.="</table>\n<hr />";
1.20      albertel  983: 	#$result.='<input type="submit" name="next" value="'.
                    984: 	#    &mt('Save &amp; Next').'" /> ';
                    985: 	#$result.='<input type="submit" name="end" value="'.
                    986: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                    987: 	#$result.='<input type="submit" name="throwaway" value="'.
                    988: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                    989: 	#$result.='<input type="submit" name="save" value="'.
                    990: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.15      albertel  991: 	$result.='</form>'.&Apache::loncommon::endbodytag().'</html>';
1.1       albertel  992:     } elsif ($target eq 'meta') {
1.70      albertel  993: 	$result.=&Apache::response::meta_package_write('Task');
1.77      albertel  994:         $result.=&Apache::response::meta_stores_write('solved','string',
                    995: 						      'Problem Status');
                    996: 	$result.=&Apache::response::meta_stores_write('tries','int_zeropos',
                    997: 						      'Number of Attempts');
                    998: 	$result.=&Apache::response::meta_stores_write('awarded','float',
                    999: 						      'Partial Credit Factor');
                   1000: 	$result.=&Apache::response::meta_stores_write('status','string',
                   1001: 						      'Bridge Task Status');
1.1       albertel 1002:     }
1.4       albertel 1003:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel 1004:     return $result;
                   1005: }
                   1006: 
1.31      albertel 1007: sub move_between_queues {
                   1008:     my ($src_queue,$dest_queue)=@_;
1.49      albertel 1009:     my $cur_data;
                   1010:     if ($src_queue ne 'none') {
                   1011: 	$cur_data=&get_queue_data($src_queue);
                   1012: 	if (!$cur_data) { return 'not_exist'; }
                   1013:     } else {
                   1014: 	$cur_data = ['none'];
                   1015:     }
1.82      albertel 1016:     my $result=&add_to_queue($dest_queue,[$cur_data->[0]]);
1.31      albertel 1017:     if ($result ne 'ok') {
                   1018: 	return $result;
                   1019:     }
                   1020:     &check_queue_unlock($src_queue);
                   1021:     return &remove_from_queue($src_queue);
1.21      albertel 1022: }
                   1023: 
                   1024: sub check_queue_unlock {
1.32      albertel 1025:     my ($queue,$key,$allow_not_me)=@_;
1.49      albertel 1026:     if ($queue eq 'none') { return 'ok'; }
1.30      albertel 1027:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.32      albertel 1028:     if (!defined($key)) {
                   1029: 	$key="$symb\0queue\0$uname\@$udom";
                   1030:     }
1.30      albertel 1031:     my $cnum=$env{'course.'.$cid.'.num'};
                   1032:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1033:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1034:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                   1035:     if  ($who eq $me) {
1.32      albertel 1036: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                   1037:     } elsif ($allow_not_me) {
1.33      albertel 1038: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel 1039: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel 1040:     }
1.32      albertel 1041:     return 'not_owner';
1.21      albertel 1042: }
                   1043: 
1.88      albertel 1044: sub in_queue {
                   1045:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
                   1046:     if ($queue eq 'none') { return 0; }
                   1047:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1048: 	|| !defined($udom) || !defined($uname)) {
                   1049: 	($symb,my $cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1050: 	$cnum=$env{'course.'.$cid.'.num'};
                   1051: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1052:     }
                   1053: 
                   1054:     my $key=&encode_queue_key($symb,$udom,$uname);
                   1055:     my %results = &Apache::lonnet::get($queue,[$key],$cdom,$cnum);
                   1056: 
                   1057:     if (defined($results{$key})) {
                   1058: 	return 1;
                   1059:     }
                   1060:     return 0;
                   1061: }
                   1062: 
1.21      albertel 1063: sub remove_from_queue {
1.86      albertel 1064:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
1.49      albertel 1065:     if ($queue eq 'none') { return 'ok'; }
1.86      albertel 1066:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1067: 	|| !defined($udom) || !defined($uname)) {
                   1068: 	($symb,my $cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1069: 	$cnum=$env{'course.'.$cid.'.num'};
                   1070: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1071:     }
1.88      albertel 1072:     if (!&in_queue($queue,$symb,$cdom,$cnum,$udom,$uname)) {
                   1073: 	return 'ok';
                   1074:     }
1.86      albertel 1075:     my $key=&encode_queue_key($symb,$udom,$uname);
1.27      albertel 1076:     my @keys=($key,"$key\0locked");
1.31      albertel 1077:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel 1078: }
                   1079: 
1.16      albertel 1080: sub setup_env_for_other_user {
                   1081:     my ($queue_key,$safeeval)=@_;
                   1082:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel 1083:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel 1084:     $env{'form.grade_symb'}=$symb;
                   1085:     $env{'form.grade_domain'}=$udom;
                   1086:     $env{'form.grade_username'}=$uname;
                   1087:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                   1088:     &Apache::lonxml::initialize_rndseed($safeeval);
                   1089: }
                   1090: 
1.31      albertel 1091: sub get_queue_data {
                   1092:     my ($queue)=@_;
                   1093:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1094:     my $cnum=$env{'course.'.$cid.'.num'};
                   1095:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1096:     my $todo="$symb\0queue\0$uname\@$udom";
                   1097:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                   1098:     if ($key eq $todo && ref($value)) {
                   1099: 	return $value;
                   1100:     }
                   1101:     return undef;
                   1102: }
                   1103: 
1.84      albertel 1104: 
1.49      albertel 1105: sub check_queue_for_key {
1.84      albertel 1106:     my ($cdom,$cnum,$queue,$todo)=@_;
                   1107: 
1.49      albertel 1108:     my %results=
                   1109: 	&Apache::lonnet::get($queue,[$todo,"$todo\0locked"],$cdom,$cnum);
                   1110:     
                   1111:     if (exists($results{$todo}) && ref($results{$todo})) {
                   1112: 	if (defined($results{"$todo\0locked"})) {
                   1113: 	    return 'locked';
                   1114: 	}
1.86      albertel 1115: 	my $slot;
                   1116: 	if (ref($results{$todo}) eq 'ARRAY')  {
                   1117: 	    $slot = $results{$todo}[0];
                   1118: 	} elsif (ref($results{$todo}) eq 'HASH')  {
                   1119: 	    $slot = $results{$todo}{'slot'};
                   1120: 	}
                   1121: 	if (defined($slot)) {
                   1122: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1123: 	    if ($slot_data{'endtime'} > time) { 
                   1124: 		return 'in_progress';
                   1125: 	    }
1.58      albertel 1126: 	}
1.49      albertel 1127: 	return 'enqueued';
                   1128:     }
                   1129:     return undef;
                   1130: }
                   1131: 
1.14      albertel 1132: sub add_to_queue {
1.82      albertel 1133:     my ($queue,$user_data)=@_;
1.49      albertel 1134:     if ($queue eq 'none') { return 'ok'; }
1.14      albertel 1135:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.82      albertel 1136:     if (!$cid || $env{'request.state'} eq 'construct') {
                   1137: 	return 'no_queue';
                   1138:     }
1.14      albertel 1139:     my $cnum=$env{'course.'.$cid.'.num'};
                   1140:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1141:     my %data;
1.82      albertel 1142:     $data{"$symb\0queue\0$uname\@$udom"}=$user_data;
1.83      albertel 1143:     return &Apache::lonnet::cput($queue,\%data,$cdom,$cnum);
1.14      albertel 1144: }
                   1145: 
                   1146: sub show_queue {
1.32      albertel 1147:     my ($queue,$with_selects)=@_;
1.14      albertel 1148:     my $result;
                   1149:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1150:     my $cnum=$env{'course.'.$cid.'.num'};
                   1151:     my $cdom=$env{'course.'.$cid.'.domain'};
1.59      albertel 1152: 
                   1153:     my @chosen_sections=
                   1154: 	&Apache::loncommon::get_env_multiple('form.chosensections');
                   1155:     &Apache::grades::init_perm();
                   1156:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
                   1157:     &Apache::grades::reset_perm();
1.63      albertel 1158:     if (!(grep(/^all$/,@chosen_sections))) {
                   1159: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1160: 	    '</tt>.</p> '."\n";
                   1161:     }
1.59      albertel 1162: 
1.16      albertel 1163:     my $regexp="^$symb\0";
1.30      albertel 1164:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel 1165:     my ($tmp)=%queue;
                   1166:     if ($tmp=~/^error: 2 /) {
1.33      albertel 1167: 	return "\n<h3>Current Queue - $queue</h3><table border='1'><tr><td>Empty</td></tr></table>";
1.31      albertel 1168:     }
1.103     albertel 1169:     my $title=&Apache::lonnet::gettitle($symb);
                   1170:     $result.="\n<h3>Current Queue - $title $queue </h3><table border='1'><tr>";
                   1171:     if ($with_selects) { $result.="<th>Status</th><th></th>"; }
                   1172:     $result.="<th>user</th><th>data</th></tr>";
1.14      albertel 1173:     foreach my $key (sort(keys(%queue))) {
1.59      albertel 1174: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1175: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
1.32      albertel 1176: 	if ($key=~/locked$/ && !$with_selects) {
1.103     albertel 1177: 	    $result.="<tr><td>$uname</td>";
                   1178: 	    $result.='<td>'.$queue{$key}.'</td></tr>';
1.32      albertel 1179: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.103     albertel 1180: 	    $result.="<tr><td></td>";
                   1181: 	    $result.='<td>'.
1.16      albertel 1182: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel 1183: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1184: 	    $result.="<tr>";
1.35      albertel 1185: 	    my $slot=$queue{$key}->[0];
                   1186: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
1.32      albertel 1187: 	    if ($with_selects) {
                   1188: 		my $ekey=&Apache::lonnet::escape($key);
1.103     albertel 1189: 		my ($action,$description,$status)=('select',&mt('Select'));
1.32      albertel 1190: 		if (exists($queue{"$key\0locked"})) {
                   1191: 		    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
1.103     albertel 1192: 		    $status=&mt('Locked by <tt>[_1]</tt>',$queue{"$key\0locked"});
1.32      albertel 1193: 		    if ($me eq $queue{"$key\0locked"}) {
                   1194: 			($action,$description)=('resume',&mt('Resume'));
                   1195: 		    } else {
                   1196: 			($action,$description)=('unlock',&mt('Unlock'));
                   1197: 		    }
                   1198: 		}
1.62      albertel 1199: 		my $seclist;
                   1200: 		foreach my $sec (@chosen_sections) {
                   1201: 		    $seclist.='<input type="hidden" name="chosensections" 
                   1202:                                value="'.$sec.'" />';
                   1203: 		}
1.35      albertel 1204: 		if (time > $slot_data{'endtime'}) {
                   1205: 		    $result.=(<<FORM);
1.103     albertel 1206: <td>$status</td>
1.32      albertel 1207: <td>
1.103     albertel 1208: <form style="display: inline" method="POST">
1.32      albertel 1209:  <input type="hidden" name="gradingkey" value="$ekey" />
                   1210:  <input type="hidden" name="queue" value="$queue" />
                   1211:  <input type="hidden" name="gradingaction" value="$action" />
                   1212:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel 1213:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel 1214:  <input type="submit" name="submit" value="$description" />
1.62      albertel 1215:  $seclist
1.32      albertel 1216: </form>
                   1217: </td>
                   1218: FORM
1.35      albertel 1219: 
                   1220:                 } else {
                   1221: 		    $result.='<td>'.&mt("In Progress").'</td>'
                   1222: 		}
1.32      albertel 1223: 	    }
1.104     albertel 1224: 	    $result.= "<td>".$fullname->{$uname.':'.$udom}.
                   1225: 		" <tt>($uname\@$udom)</tt> </td>";
1.103     albertel 1226: 	    $result.='<td>Slot: '.$slot.' End time: '.
1.20      albertel 1227: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
                   1228: 		"</td></tr>";
1.16      albertel 1229: 	}
1.14      albertel 1230:     }
1.15      albertel 1231:     $result.="</table><hr />\n";
1.14      albertel 1232:     return $result;
                   1233: }
                   1234: 
1.34      albertel 1235: sub get_queue_counts {
                   1236:     my ($queue)=@_;
                   1237:     my $result;
                   1238:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1239:     my $cnum=$env{'course.'.$cid.'.num'};
                   1240:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1241:     my $regexp="^$symb\0";
                   1242:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1243:     my ($tmp)=%queue;
                   1244:     if ($tmp=~/^error: 2 /) {
                   1245: 	return (0,0,0);
                   1246:     }
                   1247:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
1.96      albertel 1248:     my %slot_cache;
1.34      albertel 1249:     foreach my $key (sort(keys(%queue))) {
                   1250: 	if ($key=~/locked$/) {
                   1251: 	    $locks++;
                   1252: 	} elsif ($key=~/timestamp$/) {
                   1253: 	    #ignore
                   1254: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1255: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1256: 	    $entries++;
                   1257: 	    my $slot=$queue{$key}->[0];
1.96      albertel 1258: 	    if (!exists($slot_cache{$slot})) {
                   1259: 		my %slot_data=&Apache::lonnet::get_slot($slot);
                   1260: 		$slot_cache{$slot} = \%slot_data;
                   1261: 	    }
                   1262: 	    if (time > $slot_cache{$slot}{'endtime'}) {
1.34      albertel 1263: 		$ready_to_grade++;
                   1264: 	    }
                   1265: 	}
                   1266:     }
                   1267:     return ($entries,$ready_to_grade,$locks);
                   1268: }
                   1269: 
1.49      albertel 1270: sub encode_queue_key {
                   1271:     my ($symb,$udom,$uname)=@_;
                   1272:     return "$symb\0queue\0$uname\@$udom";
                   1273: }
                   1274: 
1.14      albertel 1275: sub decode_queue_key {
                   1276:     my ($key)=@_;
                   1277:     my ($symb,undef,$user) = split("\0",$key);
                   1278:     my ($uname,$udom) = split('@',$user);
                   1279:     return ($symb,$uname,$udom);
                   1280: }
                   1281: 
                   1282: sub queue_key_locked {
1.30      albertel 1283:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel 1284:     if (!defined($cdom) || !defined($cnum)) {
                   1285: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1286: 	$cnum=$env{'course.'.$cid.'.num'};
                   1287: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1288:     }
1.14      albertel 1289:     my ($key_locked,$value)=
1.30      albertel 1290: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel 1291:     if ($key_locked eq "$key\0locked") {
                   1292: 	return $value;
                   1293:     }
                   1294:     return undef;
                   1295: }
                   1296: 
                   1297: sub pick_from_queue_data {
1.30      albertel 1298:     my ($queue,$check_section,$queuedata,$cdom,$cnum)=@_;
1.98      albertel 1299:     my @possible; # will hold queue entries that are valid to be selected
1.30      albertel 1300:     foreach my $key (keys(%$queuedata)) {
1.68      albertel 1301: 	if ($key =~ /\0locked$/) { next; }
                   1302: 	if ($key =~ /\0timestamp$/) { next; }
1.14      albertel 1303: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                   1304: 	if ($check_section) {
                   1305: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel 1306: 	    if ($section eq $check_section) {
1.33      albertel 1307: 		&Apache::lonxml::debug("my sec");
1.15      albertel 1308: 		next;
                   1309: 	    }
1.14      albertel 1310: 	}
1.30      albertel 1311: 	my $slot=$queuedata->{$key}[0];
1.14      albertel 1312: 	my %slot_data=&Apache::lonnet::get_slot($slot);
1.15      albertel 1313: 	if ($slot_data{'endtime'} > time) { 
1.33      albertel 1314: 	    &Apache::lonxml::debug("not time");
1.15      albertel 1315: 	    next;
                   1316: 	}
1.98      albertel 1317: 	if (exists($queuedata->{"$key\0locked"})) {
1.33      albertel 1318: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel 1319: 	    next;
                   1320: 	}
1.98      albertel 1321: 	push(@possible,[$key,$slot_data{'endtime'}]);
                   1322:     }
                   1323:     if (@possible) {
                   1324:         # sort entries in order by slot end time
                   1325: 	@possible = sort { $a->[1] <=> $b->[1] } @possible;
                   1326: 	# pick one of the first ten entries
                   1327: 	my $max=($#possible < 10) ? $#possible : 10;
                   1328: 	return $possible[int(rand($max))][0];
1.14      albertel 1329:     }
                   1330:     return undef;
                   1331: }
                   1332: 
1.15      albertel 1333: sub find_mid_grade {
1.30      albertel 1334:     my ($queue,$symb,$cdom,$cnum)=@_;
1.16      albertel 1335:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.15      albertel 1336:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1337:     if ($todo) {
1.30      albertel 1338: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel 1339: 	if ($who eq $me) { return $todo; }
                   1340:     }
                   1341:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel 1342:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel 1343:     foreach my $key (keys(%locks)) {
                   1344: 	my $who=$locks{$key};
                   1345: 	if ($who eq $me) {
                   1346: 	    $todo=$key;
                   1347: 	    $todo=~s/\0locked$//;
                   1348: 	    return $todo;
                   1349: 	}
                   1350:     }
                   1351:     return undef;
                   1352: }
                   1353: 
1.32      albertel 1354: sub lock_key {
                   1355:     my ($queue,$todo)=@_;
                   1356:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1357:     my (undef,$cid)=&Apache::lonxml::whichuser();
                   1358:     my $cnum=$env{'course.'.$cid.'.num'};
                   1359:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1360:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                   1361: 					$cdom,$cnum);
1.33      albertel 1362:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel 1363:     if ($success eq 'ok') {
                   1364: 	return 1;
                   1365:     }
                   1366:     return 0;
                   1367: }
                   1368: 
1.86      albertel 1369: sub get_queue_symb_status {
1.85      albertel 1370:     my ($queue,$symb,$cdom,$cnum) = @_;
                   1371:     if (!defined($cdom) || !defined($cnum)) {
                   1372: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1373: 	$cnum=$env{'course.'.$cid.'.num'};
                   1374: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1375:     }
                   1376:     my $regexp="^$symb\0";
                   1377:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1378:     my ($tmp)=%queue;
                   1379:     if ($tmp=~/^error: 2 /) { return; }
                   1380:     my @users;
                   1381:     foreach my $key (sort(keys(%queue))) {
                   1382: 	next if ($key=~/locked$/);
                   1383: 	next if ($key=~/timestamp$/);
                   1384: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1385: 	push(@users,"$uname:$udom");
                   1386:     }
                   1387:     return @users;
                   1388: }
                   1389: 
1.14      albertel 1390: sub get_from_queue {
1.30      albertel 1391:     my ($queue)=@_;
1.14      albertel 1392:     my $result;
                   1393:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1394:     my $cnum=$env{'course.'.$cid.'.num'};
                   1395:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel 1396:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel 1397:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel 1398:     if ($todo) { return $todo; }
1.95      albertel 1399:     my $attempts=0;
1.14      albertel 1400:     while (1) {
1.95      albertel 1401: 	if ($attempts > 2) {
                   1402: 	    # tried twice to get a queue entry, giving up
                   1403: 	    return (undef,'unable');
                   1404: 	}
1.14      albertel 1405: 	my $starttime=time;
1.83      albertel 1406: 	&Apache::lonnet::cput($queue,{"$symb\0timestamp"=>$starttime},
                   1407: 			      $cdom,$cnum);
1.33      albertel 1408: 	&Apache::lonxml::debug("$starttime");
1.14      albertel 1409: 	my $regexp="^$symb\0queue\0";
1.97      albertel 1410: 	my $range= ($attempts < 1 ) ? '0-100' : '0-400';
                   1411: 
1.98      albertel 1412: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel 1413: 	#make a pass looking for a user _not_ in my section
1.14      albertel 1414: 	if ($env{'request.course.sec'}) {
1.33      albertel 1415: 	    &Apache::lonxml::debug("sce");
1.30      albertel 1416: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                   1417: 					\%queue,$cdom,$cnum);
1.33      albertel 1418: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel 1419: 	}
1.33      albertel 1420: 	# no one _not_ in our section so look for any user that is
                   1421: 	# ready for grading
1.14      albertel 1422: 	if (!$todo) {
1.33      albertel 1423: 	    &Apache::lonxml::debug("no sce");
1.98      albertel 1424: 	    $todo=&pick_from_queue_data($queue,undef,\%queue,$cdom,$cnum);
1.33      albertel 1425: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel 1426: 	}
                   1427: 	# no user to grade 
                   1428: 	if (!$todo) { last; }
1.33      albertel 1429: 	&Apache::lonxml::debug("got $todo");
1.14      albertel 1430: 	# otherwise found someone so lets try to lock them
1.32      albertel 1431: 	# unless someone else already picked them
1.95      albertel 1432: 	if (!&lock_key($queue,$todo)) {
                   1433: 	    $attempts++;
                   1434: 	    next;
                   1435: 	}
1.14      albertel 1436: 	my (undef,$endtime)=
1.30      albertel 1437: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel 1438: 				 $cdom,$cnum);
1.33      albertel 1439: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel 1440: 	# someone else already modified the queue, 
                   1441: 	# perhaps our picked user wass already fully graded between
                   1442: 	# when we picked him and when we locked his record? so lets
                   1443: 	# double check.
                   1444: 	if ($endtime != $starttime) {
                   1445: 	    my ($key,$value)=
1.30      albertel 1446: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel 1447: 				     $cdom,$cnum);
1.33      albertel 1448: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel 1449: 	    if ($key eq $todo && ref($value)) {
                   1450: 	    } else {
1.30      albertel 1451: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel 1452: 				     $cdom,$cnum);
1.33      albertel 1453: 		&Apache::lonxml::debug("del");
1.95      albertel 1454: 		$attempts++;
1.14      albertel 1455: 		next;
                   1456: 	    }
                   1457: 	}
1.33      albertel 1458: 	&Apache::lonxml::debug("last $todo");
1.14      albertel 1459: 	last;
                   1460:     }
                   1461:     return $todo;
                   1462: }
                   1463: 
1.49      albertel 1464: sub select_user {
                   1465:     my ($symb,$cid)=&Apache::lonxml::whichuser();
                   1466: 
1.59      albertel 1467:     my @chosen_sections=
                   1468: 	&Apache::loncommon::get_env_multiple('form.chosensections');
1.49      albertel 1469:     &Apache::grades::init_perm();
1.59      albertel 1470:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
1.49      albertel 1471:     &Apache::grades::reset_perm();
1.63      albertel 1472:     
                   1473:     my $result;
                   1474:     if (!(grep(/^all$/,@chosen_sections))) {
                   1475: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1476: 	    '</tt>.</p> '."\n";
                   1477:     }
                   1478:     $result.='<table border="1">';
1.49      albertel 1479: 
                   1480:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
                   1481: 	my ($uname,$udom) = split(/:/,$student);
1.59      albertel 1482: 	
1.84      albertel 1483: 	my $cnum=$env{'course.'.$cid.'.num'};
                   1484: 	my $cdom=$env{'course.'.$cid.'.domain'};
1.88      albertel 1485: 	my %status = &get_student_status($symb,$cdom,$cnum,$udom,$uname,
                   1486: 					 'Task');
1.49      albertel 1487: 	my $queue = 'none';
1.58      albertel 1488: 	my $cannot_grade;
                   1489: 	if ($status{'reviewqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1490: 	    $queue = 'reviewqueue';
1.58      albertel 1491: 	    if ($status{'reviewqueue'} eq 'in_progress') {
                   1492: 		$cannot_grade=1;
                   1493: 	    }
                   1494: 	} elsif ($status{'gradingqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1495: 	    $queue = 'gradingqueue';
1.58      albertel 1496: 	    if ($status{'gradingqueue'} eq 'in_progress') {
                   1497: 		$cannot_grade=1;
                   1498: 	    }
1.49      albertel 1499: 	}
                   1500: 	my $todo = 
                   1501: 	    &Apache::lonnet::escape(&encode_queue_key($symb,$udom,$uname));
1.58      albertel 1502: 	if ($cannot_grade) {
                   1503: 	    $result.='<tr><td>&nbsp;</td><td>'.$fullname->{$student}.
                   1504: 		'</td><td>';
                   1505: 	} else {
1.62      albertel 1506: 	    my $seclist;
                   1507: 	    foreach my $sec (@chosen_sections) {
                   1508: 		$seclist.='<input type="hidden" name="chosensections" 
                   1509:                                value="'.$sec.'" />';
                   1510: 	    }
1.102     albertel 1511: 	    my $studentdis = $student;
                   1512: 	    $studentdis =~ tr/:/@/;
1.58      albertel 1513: 	    $result.=<<RESULT;
1.49      albertel 1514: <tr>
                   1515:   <td>
1.102     albertel 1516:     <form style="display: inline" method="POST">
1.49      albertel 1517:       <input type="hidden" name="gradingkey" value="$todo" />
                   1518:       <input type="hidden" name="queue" value="$queue" />
                   1519:       <input type="hidden" name="webgrade" value="no" />
1.52      albertel 1520:       <input type="hidden" name="regrade" value="yes" />
1.62      albertel 1521:       <input type="submit" name="submit" value="Regrade" />
                   1522:       $seclist
1.49      albertel 1523:     </form>
1.102     albertel 1524:   <td>$fullname->{$student} <tt>($studentdis)</tt></td>
1.49      albertel 1525:   <td>
                   1526: RESULT
1.58      albertel 1527:         }
1.49      albertel 1528:         if ($status{'status'} eq 'pass') {
                   1529: 	    $result .= '<font color="green">'.&mt('Passed').'</font>';
                   1530: 	} elsif ($status{'status'} eq 'fail') {
                   1531: 	    $result .= '<font color="red">'.&mt('Failed').'</font>';
                   1532: 	} elsif ($status{'status'} eq 'review') {
                   1533: 	    $result .= '<font color="blue">'.&mt('Under Review').'</font>';
                   1534: 	} elsif ($status{'status'} eq 'ungraded') {
                   1535: 	    $result .= &mt('Ungraded');
                   1536: 	} elsif ($status{'status'} ne '') {
                   1537: 	    $result .= '<font color="orange">'.&mt('Unknown Status').'</font>';
                   1538: 	} else {
                   1539: 	    $result.="&nbsp;";
                   1540: 	}
                   1541: 	if ($status{'version'}) {
                   1542: 	    $result .= ' '.&mt('Version').' '.$status{'version'};
                   1543: 	}
1.101     albertel 1544: 	if ($status{'grader'}) {
                   1545: 	    $result .= ' '.&mt('(Graded by [_1])',$status{'grader'}).' ';
                   1546: 	}
1.49      albertel 1547: 	$result.= '</td><td>';
                   1548: 	if ($status{'reviewqueue'} eq 'enqueued') {
                   1549: 	    $result .= &mt('Awaiting Review');
                   1550: 	} elsif ($status{'reviewqueue'} eq 'locked') {
                   1551: 	    $result .= &mt('Under Review');
1.58      albertel 1552: 	} elsif ($status{'reviewqueue'} eq 'in_progress') {
                   1553: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1554: 	} elsif ($status{'gradingqueue'} eq 'enqueued') {
                   1555: 	    $result .= &mt('Awaiting Grading');
                   1556: 	} elsif ($status{'gradingqueue'} eq 'locked') {
                   1557: 	    $result .= &mt('Being Graded');
1.58      albertel 1558: 	} elsif ($status{'gradingqueue'} eq 'in_progress') {
                   1559: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1560: 	} else {
                   1561: 	    $result.="&nbsp;";
                   1562: 	}
                   1563: 	$result.= '</td></tr>';
                   1564:     }
                   1565:     $result.='</table>';
                   1566:     return $result;
                   1567: }
                   1568: 
                   1569: sub get_student_status {
1.86      albertel 1570:     my ($symb,$cdom,$cnum,$udom,$uname,$type)=@_;
                   1571: 
                   1572:     my %status;
                   1573: 
                   1574:     if ($type eq 'Task') {
                   1575: 	my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
1.49      albertel 1576: 					  $udom,$uname);
1.89      albertel 1577: 	$status{'status'}=$record{'resource.0.status'};
                   1578: 	$status{'version'}=$record{'resource.0.version'};
                   1579: 	$status{'grader'}=$record{'resource.0.regrader'};
1.86      albertel 1580:     }
                   1581:     $status{'reviewqueue'}=
                   1582: 	&check_queue_for_key($cdom,$cnum,'reviewqueue',
                   1583: 			     &encode_queue_key($symb,$udom,$uname));
                   1584:     $status{'gradingqueue'}=
                   1585: 	&check_queue_for_key($cdom,$cnum,'gradingqueue',
                   1586: 			     &encode_queue_key($symb,$udom,$uname));
1.49      albertel 1587:     return %status;
                   1588: }
                   1589: 
1.1       albertel 1590: sub start_ClosingParagraph {
                   1591:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1592:     my $result;
                   1593:     if ($target eq 'web') {
1.13      albertel 1594:     } elsif ($target eq 'webgrade') {
                   1595: 	&Apache::lonxml::startredirection();
1.1       albertel 1596:     }
                   1597:     return $result;
                   1598: }
                   1599: 
                   1600: sub end_ClosingParagraph {
                   1601:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1602:     my $result;
                   1603:     if ($target eq 'web') {
1.13      albertel 1604:     } elsif ($target eq 'webgrade') {
                   1605: 	&Apache::lonxml::endredirection();
1.1       albertel 1606:     }
                   1607:     return $result;
                   1608: }
                   1609: 
1.19      albertel 1610: sub get_id {
                   1611:     my ($parstack,$safeeval)=@_;
                   1612:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                   1613:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                   1614:     return $id;
                   1615: }
                   1616: 
1.1       albertel 1617: my %dimension;
                   1618: sub start_Dimension {
                   1619:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1620:     undef(%dimension);
1.19      albertel 1621:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel 1622:     $Apache::bridgetask::dimension=$dim_id;
                   1623:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                   1624:     undef(@Apache::bridgetask::instance);
1.20      albertel 1625:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
                   1626: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.54      albertel 1627:     &Apache::lonxml::startredirection();
                   1628:     return &internal_location($dim_id);
1.1       albertel 1629: }
                   1630: 
1.13      albertel 1631: sub get_instance {
1.75      albertel 1632:     my ($dim)=@_;
                   1633:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                   1634:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                   1635: 	$rand_alg eq '64bit2' || $rand_alg eq '64bit3' ||
                   1636: 	$rand_alg eq '64bit4' ) {
                   1637: 	&Apache::response::pushrandomnumber();
                   1638: 	my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
                   1639: 	my $num=@order;
                   1640: 	my $version=&get_version();
                   1641: 	my $which=($version-1)%$num;
                   1642: 	return $order[$which];
                   1643:     } else {
                   1644: 	my ($version,$previous) = &get_version();
                   1645: 	my $instance = 
                   1646: 	    $Apache::lonhomework::history{"resource.$version.0.$dim.instance"};
                   1647: 	if (defined($instance)) { return $instance; }
                   1648: 
                   1649: 	&Apache::response::pushrandomnumber();
                   1650: 	my @instances = @{$dimension{'instances'}};
                   1651: 	# remove disabled instances
                   1652: 	for (my $i=0; $i < $#instances; $i++) {
                   1653: 	    if ($dimension{$instances[$i].'.disabled'}) {
                   1654: 		splice(@instances,$i,1);
                   1655: 		$i--;
                   1656: 	    }
                   1657: 	}
                   1658: 	@instances = &Math::Random::random_permutation(@instances);
                   1659: 	$instance  = $instances[($version-1)%scalar(@instances)];
                   1660: 	$Apache::lonhomework::results{"resource.$version.0.$dim.instance"} = 
                   1661: 	    $instance;
1.79      albertel 1662: 	$Apache::lonhomework::results{'INTERNAL_store'} = 1; 
1.75      albertel 1663: 	&Apache::response::poprandomnumber();
                   1664: 	return $instance;
                   1665:     }
1.13      albertel 1666: }
                   1667: 
1.18      albertel 1668: {
                   1669:     my $last_link;
                   1670:     sub end_Dimension {
                   1671: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.54      albertel 1672: 	my $result=&Apache::lonxml::endredirection();
1.25      albertel 1673: 	my $dim=&get_id($parstack,$safeeval);
1.75      albertel 1674: 	my $instance=&get_instance($dim);
1.25      albertel 1675: 	my $version=&get_version();
1.18      albertel 1676: 	if ($target eq 'web') {
1.47      albertel 1677: 	    @Apache::scripttag::parser_env = @_;
                   1678: 	    $result.=&Apache::scripttag::xmlparse($dimension{'intro'});
1.76      albertel 1679: 	    my @instances = $instance;
                   1680: 	    if (&Apache::response::showallfoils()) {
                   1681: 		@instances = @{$dimension{'instances'}};
                   1682: 	    }
                   1683: 	    foreach my $instance (@instances) {
                   1684: 		@Apache::scripttag::parser_env = @_;
                   1685: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.89      albertel 1686: 		if ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass' ||
                   1687: 		    $Apache::lonhomework::history{"resource.$version.0.status"} eq 'fail') {
1.76      albertel 1688: 
1.89      albertel 1689: 		    my $dim_status=$Apache::lonhomework::history{"resource.$version.0.$dim.status"};
1.76      albertel 1690: 		    my $mandatory='Mandatory';
                   1691: 		    if ($Apache::bridgetask::dimensionmandatory{$dim} eq 'N') {
                   1692: 			$mandatory='Optional';
                   1693: 		    }
                   1694: 		    my $dim_info="<div class='$dim_status'>\n";
                   1695: 		    if ($dim_status eq 'pass') {
                   1696: 			$dim_info.='<h3>Question : you passed this '.$mandatory.' question</h3>';
1.54      albertel 1697: 		    }
1.76      albertel 1698: 		    if ($dim_status eq 'fail') {
                   1699: 			$dim_info.='<h3>Question : you did not pass this '.$mandatory.' question</h3>';
1.53      albertel 1700: 		    }
1.76      albertel 1701: 		    my $man_count=0;
                   1702: 		    my $man_passed=0;
                   1703: 		    my $opt_count=0;
                   1704: 		    my $opt_passed=0;
                   1705: 		    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1706: 			if ($dimension{$instance.'.criteria.'.$id.'.mandatory'} 
                   1707: 			    eq 'N') {
                   1708: 			    $opt_count++;
1.89      albertel 1709: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"} eq 'pass') {
1.76      albertel 1710: 				$opt_passed++;
                   1711: 			    }
                   1712: 			} else {
                   1713: 			    $man_count++;
1.89      albertel 1714: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"} eq 'pass') {
1.76      albertel 1715: 				$man_passed++;
                   1716: 			    }
                   1717: 			}
1.22      albertel 1718: 		    }
1.76      albertel 1719: 		    if ($man_passed eq $man_count) { $man_passed='all'; }
                   1720: 		    my $opt_req=$dimension{$instance.'.optionalrequired'};
                   1721: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
                   1722: 		    $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>";
                   1723: 
                   1724: 		    my $internal_location=&internal_location($dim);
                   1725: 		    $result=~s/\Q$internal_location\E/$dim_info/;
                   1726: 
                   1727: 		    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.89      albertel 1728: 			my $status=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"};
                   1729: 			my $comment=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"};
1.76      albertel 1730: 			my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1731: 			if ($mandatory) {
                   1732: 			    $mandatory='Mandatory';
                   1733: 			} else {
                   1734: 			    $mandatory='Optional';
                   1735: 			}
                   1736: 			if ($status eq 'fail') {
                   1737: 			} elsif ($status eq 'pass') {
                   1738: 			} else {
                   1739: 			    &Apache::lonxml::error("Student viewing a graded bridgetask was shown a status of $status");
                   1740: 			}
                   1741: 			my $status_display=$status;
                   1742: 			$status_display=~s/^([a-z])/uc($1)/e;
                   1743: 			@Apache::scripttag::parser_env = @_;
                   1744: 			$result.='<div class="'.$status.'"><h4>'.$mandatory.
                   1745: 			    ' Criteria</h4><p>';
                   1746: 			@Apache::scripttag::parser_env = @_;
                   1747: 			$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
                   1748: 			$result.='</p><p class="grade">'.$status_display.'</p>';
1.89      albertel 1749: 			if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}) {
1.100     albertel 1750: 			    $result.='<p class="comment">'.&mt('Comment: [_1]',$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}).'</p>';
1.76      albertel 1751: 			}
                   1752: 			$result.='</div>';
1.22      albertel 1753: 		    }
                   1754: 		}
                   1755: 	    }
1.18      albertel 1756: 	} elsif ($target eq 'webgrade') {
1.47      albertel 1757: 	    # in case of any side effects that we need
                   1758: 	    @Apache::scripttag::parser_env = @_;
                   1759: 	    &Apache::scripttag::xmlparse($dimension{'intro'});
                   1760: 	    @Apache::scripttag::parser_env = @_;
                   1761: 	    &Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.18      albertel 1762: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1763: 		my $link='criteria_'.$instance.'_'.$id;
1.89      albertel 1764: 		my $status=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"};
1.19      albertel 1765: 		$result.='<tr><td width="100%" valign="top">'.
1.18      albertel 1766: 		    '<a name="'.$link.'" />'.
                   1767: 		    '<a name="next_'.$last_link.'" />'.
1.47      albertel 1768: 		    '<br /><textarea enabled="false" style="width:100%" rows="8" width="25" wrap="hard">';
                   1769: 		@Apache::scripttag::parser_env = @_;
                   1770: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
                   1771: 		$result.='</textarea>'.
1.18      albertel 1772: 		    #$dimension{$instance.'.criteria.'.$id}.
                   1773: 		    '</td>'.
1.19      albertel 1774: 		    '<td><nobr>Additional Comment for Student</nobr> <br />'.
1.89      albertel 1775: 		    '<textarea style="width:100%" rows="8" width="25" wrap="hard" name="HWVAL_comment_'.$link.'">'.&HTML::Entities::encode($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}).'</textarea>'.
1.18      albertel 1776: 		    '</td>'.
                   1777: 		    '<td>'.
1.47      albertel 1778: 		    '<nobr><label><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.&mt('Ungraded').'</label></nobr><br />'.
1.30      albertel 1779: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.&mt('Pass').'</label><br />'.
                   1780: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label><br />'.
                   1781: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.&mt('Review').'</label><br />'.
1.18      albertel 1782: 		    '</td>'.
                   1783: 		    '<td>'.
                   1784: 		    '<a href="#'.$last_link.'">Prev</a><br />'.
1.20      albertel 1785: 		    '<a href="#next_'.$link.'">Next</a><br /><br /><br />'.
1.18      albertel 1786: 		    '</td></tr>';
                   1787: 		$last_link=$link;
                   1788: 	    }
1.22      albertel 1789: 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
1.19      albertel 1790: 	    my $optional_passed=0;
1.20      albertel 1791: 	    my $mandatory_failed=0;
                   1792: 	    my $ungraded=0;
                   1793: 	    my $review=0;
1.19      albertel 1794: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.89      albertel 1795: 		my $status=$Apache::lonhomework::results{"resource.$version.0.$dim.$instance.$id.status"}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
                   1796: 		$Apache::lonhomework::results{"resource.$version.0.$dim.$instance.$id.comment"}=$env{'form.HWVAL_comment_criteria_'.$instance.'_'.$id};
1.20      albertel 1797: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1798: 		if ($status eq 'pass') {
                   1799: 		    if (!$mandatory) { $optional_passed++; }
                   1800: 		} elsif ($status eq 'fail') {
                   1801: 		    if ($mandatory) { $mandatory_failed++; }
1.21      albertel 1802: 		} elsif ($status eq 'review') {
                   1803: 		    $review++;
1.20      albertel 1804: 		} elsif ($status eq 'ungraded') {
                   1805: 		    $ungraded++;
1.21      albertel 1806: 		} else {
1.47      albertel 1807: 		    $ungraded++;
1.19      albertel 1808: 		}
                   1809: 	    }
1.20      albertel 1810: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
                   1811: 		$mandatory_failed++;
                   1812: 	    }
1.21      albertel 1813: 	    &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 1814: 	    if ($review) {
1.89      albertel 1815: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1816: 		    'review';
1.20      albertel 1817: 	    } elsif ($ungraded) {
1.89      albertel 1818: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1819: 		    'ungraded';
1.20      albertel 1820: 	    } elsif ($mandatory_failed) {
1.89      albertel 1821: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1822: 		    'fail';
1.20      albertel 1823: 	    } else {
1.91      albertel 1824: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1825: 		    'pass';
1.20      albertel 1826: 	    }
1.69      albertel 1827: 	} else {
                   1828: 	    # any other targets no output
                   1829: 	    undef($result);
1.13      albertel 1830: 	}
1.18      albertel 1831: 	return $result;
1.1       albertel 1832:     }
                   1833: }
                   1834: 
                   1835: sub start_IntroParagraph {
1.87      albertel 1836:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel 1837:     my $result;
1.13      albertel 1838:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1839: 	if ($tagstack->[-2] eq 'Dimension') {
1.87      albertel 1840: 	    $dimension{'intro'}=&Apache::lonxml::get_all_text('/introparagraph',$parser,$style);
1.47      albertel 1841: 	
                   1842: 	} elsif ($target eq 'webgrade') {
1.87      albertel 1843: 	    &Apache::lonxml::get_all_text('/introparagraph',$parser,$style);
1.1       albertel 1844: 	}
1.47      albertel 1845: 	
1.1       albertel 1846:     }
                   1847:     return $result;
                   1848: }
                   1849: 
                   1850: sub end_IntroParagraph {
                   1851: }
                   1852: 
                   1853: sub start_Instance {
                   1854:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel 1855:     my $id=&get_id($parstack,$safeeval);
                   1856:     push(@{$dimension{'instances'}},$id);
                   1857:     push(@Apache::bridgetask::instance,$id);
                   1858:     push(@Apache::bridgetask::instancelist,$id);
1.20      albertel 1859:     $dimension{$id.'.optionalrequired'}=
1.19      albertel 1860: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.75      albertel 1861:     my $disabled = &Apache::lonxml::get_param('Disabled',$parstack,$safeeval);
                   1862:     if (lc($disabled) eq 'yes') {
                   1863: 	$dimension{$id.'.disabled'}='1';
                   1864:     }
1.1       albertel 1865:     return '';
                   1866: }
                   1867: 
                   1868: sub end_Instance {
                   1869: }
                   1870: 
                   1871: sub start_InstanceText {
1.87      albertel 1872:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.47      albertel 1873:     my $instance_id=$Apache::bridgetask::instance[-1];
1.87      albertel 1874:     my $text=&Apache::lonxml::get_all_text('/instancetext',$parser,$style);
1.13      albertel 1875:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1876: 	$dimension{$instance_id.'.text'}=$text;
1.1       albertel 1877:     }
                   1878:     return '';
                   1879: }
                   1880: 
                   1881: sub end_InstanceText {
                   1882:     return '';
                   1883: }
                   1884: 
                   1885: sub start_Criteria {
1.87      albertel 1886:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.9       albertel 1887:     my $instance_id=$Apache::bridgetask::instance[-1];
1.87      albertel 1888:     my $criteria=&Apache::lonxml::get_all_text('/criteria',$parser,$style);
1.21      albertel 1889:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.19      albertel 1890: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel 1891: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel 1892: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                   1893: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel 1894: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                   1895:     }
                   1896:     return '';
                   1897: }
                   1898: 
1.47      albertel 1899: sub end_Criteria {
                   1900: }
                   1901: 
1.4       albertel 1902: sub proctor_validation_screen {
                   1903:     my ($slot) = @_;
                   1904:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5       albertel 1905:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.44      albertel 1906:     my $name=&Apache::loncommon::plainname($user,$domain);
                   1907:     
1.4       albertel 1908:     my $msg;
1.11      albertel 1909:     if ($env{'form.proctorpassword'}) {
1.4       albertel 1910: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                   1911: 	    .'</font></p>';
                   1912:     }
1.47      albertel 1913:     if (!$env{'form.proctordomain'}) { $env{'form.proctordomain'}=$domain; }
1.4       albertel 1914:     my $result= (<<ENDCHECKOUT);
                   1915: <h2>Proctor Validation</h2>
                   1916:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   1917:     $msg
1.11      albertel 1918: <form name="checkout" method="POST" action="$env{'request.uri'}">
1.4       albertel 1919: <input type="hidden" name="validate" value="yes" />
                   1920: <input type="hidden" name="submitted" value="yes" />
                   1921: <table>
1.44      albertel 1922:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$env{'form.proctorname'}" /></td></tr>
1.4       albertel 1923:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.46      albertel 1924:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$env{'form.proctordomain'}" /></td></tr>
1.4       albertel 1925: </table>
                   1926: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
1.44      albertel 1927: <table border="1">
                   1928:   <tr><td>
                   1929:     <table>
                   1930:       <tr><td colspan="2">Student who should be logged in is:</td></tr>
                   1931:       <tr><td>Name:</td><td>$name</td></tr>
1.45      albertel 1932:       <tr><td>Student ID:</td><td>$env{'environment.id'}</td></tr>
1.44      albertel 1933:       <tr><td>Usename</td><td>$user\@$domain</td></tr>
                   1934:       <tr><td colspan="2"><img src="$url" /></td></tr>
                   1935:     </table>
                   1936:   </tr></td>
                   1937: </table>
1.4       albertel 1938: </form>
                   1939: ENDCHECKOUT
                   1940:     return $result;
                   1941: }
                   1942: 
1.1       albertel 1943: 1;
                   1944: __END__

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