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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.147     albertel    4: # $Id: bridgetask.pm,v 1.146 2006/05/09 19:01:19 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) {
1.138     albertel   69: 	my ($puser,$pdom)=(split(':',$possible));
1.4       albertel   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.138     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"}=
1.138     albertel  106: 		    $user.':'.$domain;
1.81      albertel  107: 		$Apache::lonhomework::results{"resource.0.checkedin.slot"}=
                    108: 		    $slot_name;
1.4       albertel  109: 	    }
                    110: 	}
                    111:     }
                    112:     return 0;
                    113: }
                    114: 
1.25      albertel  115: sub get_version {
1.29      albertel  116:     my ($version,$previous);
1.25      albertel  117:     if ($env{'form.previousversion'} && 
1.36      albertel  118: 	$env{'form.previousversion'} ne 'current' &&
1.89      albertel  119: 	defined($Apache::lonhomework::history{'resource.'.$env{'form.previousversion'}.'.0.status'})) {
1.29      albertel  120: 	$version=$env{'form.previousversion'};
                    121: 	$previous=1;
                    122:     } else {
1.89      albertel  123: 	$version=$Apache::lonhomework::history{'resource.0.version'};
1.29      albertel  124: 	$previous=0;
                    125:     }
                    126:     if (wantarray) {
                    127: 	return ($version,$previous);
1.25      albertel  128:     }
1.29      albertel  129:     return $version;
1.25      albertel  130: }
                    131: 
1.8       albertel  132: sub add_previous_version_button {
1.25      albertel  133:     my ($status)=@_;
1.8       albertel  134:     my $result;
1.89      albertel  135:     if ($Apache::lonhomework::history{'resource.0.version'} eq '') {
1.25      albertel  136: 	return '';
                    137:     }
1.89      albertel  138:     if ($Apache::lonhomework::history{'resource.0.version'} < 2 &&
1.29      albertel  139: 	$status ne 'NEEDS_CHECKIN') {
1.25      albertel  140: 	return '';
                    141:     }
1.29      albertel  142:     my $version=&get_version();
                    143:     if ($env{'form.previousversion'} ne '' &&
                    144: 	$env{'form.previousversion'} eq $version) {
                    145: 	$result.="<h3>".&mt("Showing previous version [_1]",$version).
                    146: 	    "</h3>\n";
                    147:     }
                    148:     my @to_show;
1.89      albertel  149:     foreach my $test_version (1..$Apache::lonhomework::history{'resource.0.version'}) {
                    150: 	if (defined($Apache::lonhomework::history{'resource.'.$test_version.'.0.status'})) {
1.29      albertel  151: 	    push(@to_show,$test_version);
                    152: 	}
                    153:     }
                    154:     my $list='<option>'.
                    155: 	join("</option>\n<option>",@to_show).
                    156: 	     "</option>\n";
1.36      albertel  157:     $list.='<option value="current">'.&mt('Current').'</option>';
1.115     albertel  158:     $result.='<form name="getprevious" method="post" action="';
1.29      albertel  159:     my $uri=$env{'request.uri'};
                    160:     if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    161:     $result.=$uri.'">'.
                    162: 	&mt(' Show a previously done version: [_1]','<select onchange="this.form.submit()" name="previousversion">
                    163: <option>'.&mt('Pick one').'</option>
                    164: '.$list.'
                    165: </select>')."</form>";
1.8       albertel  166:     return $result;
                    167: }
                    168: 
1.13      albertel  169: sub add_grading_button {
1.59      albertel  170:     my (undef,$cid)=&Apache::lonxml::whichuser();
                    171:     my $cnum=$env{'course.'.$cid.'.num'};
                    172:     my $cdom=$env{'course.'.$cid.'.domain'};
1.144     albertel  173:     my %sections = &Apache::loncommon::get_sections($cdom,$cnum);
                    174: 
1.59      albertel  175:     my $size=5;
                    176:     if (scalar(keys(%sections)) < 3) {
                    177: 	$size=scalar(keys(%sections))+2;
                    178:     }
1.122     albertel  179:     my $sec_select = '<select multiple="multiple" name="chosensections" size="'.$size.'">'."\n";
1.59      albertel  180:     $sec_select .= "<option value='all' selected='selected'>all</option>\n";
                    181:     foreach my $sec (sort {lc($a) cmp lc($b)} (keys(%sections))) {
1.122     albertel  182: 	$sec_select .= "<option value=\"$sec\">$sec</option>\n";
1.59      albertel  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();
1.123     albertel  216: 	$result.= '</p>';
1.144     albertel  217:     }
1.13      albertel  218:     return $result;
                    219: }
                    220: 
1.22      albertel  221: sub add_request_another_attempt_button {
1.38      albertel  222:     my ($text)=@_;
                    223:     if (!$text) { $text="Request another attempt"; }
1.25      albertel  224:     my $result;
1.36      albertel  225:     my $symb=&Apache::lonnet::symbread();
1.37      albertel  226:     my ($slot_name,$slot)=&Apache::slotrequest::check_for_reservation($symb);
1.38      albertel  227:     my $action='get_reservation';
1.37      albertel  228:     if ($slot_name) {
1.38      albertel  229: 	$text="Change reservation.";
                    230: 	$action='change_reservation';
1.37      albertel  231: 	my $description=&Apache::slotrequest::get_description($slot_name,
                    232: 							      $slot);
                    233: 	$result.=(<<STUFF);
                    234: <p> Will be next available: $description </p>
                    235: STUFF
                    236:     }
1.38      albertel  237:     
                    238:     if ($env{'request.enc'}) { $symb=&Apache::lonenc::encrypted($symb); }
                    239:     $symb=&Apache::lonnet::escape($symb);
1.115     albertel  240:     $result.='<form method="post" action="/adm/slotrequest">'.
1.38      albertel  241: 	'<input type="hidden" name="symb" value="'.$symb.'" />'.
                    242: 	'<input type="hidden" name="command" value="'.$action.'" />'.
                    243: 	'<input type="submit" name="requestattempt" value="'.
                    244: 	&mt($text).'" />'.
                    245: 	'</form>';
1.25      albertel  246:     return $result;
1.22      albertel  247: }
                    248: 
1.30      albertel  249: sub preserve_grade_info {
                    250:     my $result;
                    251:     # if we are viewing someone else preserve that info
                    252:     if (defined $env{'form.grade_symb'}) {
                    253: 	foreach my $field ('symb','courseid','domain','username') {
                    254: 	    $result .= '<input type="hidden" name="grade_'.$field.
                    255: 		'" value="'.$env{"form.grade_$field"}.'" />'."\n";
                    256: 	}
                    257:     }
                    258:     return $result;
                    259: }
                    260: 
1.53      albertel  261: sub style {
1.125     albertel  262:     my ($target) = @_;
                    263:     if ($target eq 'web'
                    264: 	|| $target eq 'webgrade') {
                    265: 	return (<<STYLE);
1.126     albertel  266: <link rel="stylesheet" type="text/css" href="/res/adm/includes/task.css" />
1.53      albertel  267: STYLE
1.125     albertel  268:     }
                    269:     return;
1.53      albertel  270: }
                    271: 
1.54      albertel  272: sub show_task {
                    273:     my ($status,$previous)=@_;
                    274:     if (!$previous && (
                    275: 		       ( $status eq 'CLOSED' ) ||
                    276: 		       ( $status eq 'BANNED') ||
                    277: 		       ( $status eq 'UNAVAILABLE') ||
                    278: 		       ( $status eq 'NOT_IN_A_SLOT') ||
                    279: 		       ( $status eq 'NEEDS_CHECKIN') ||
                    280: 		       ( $status eq 'WAITING_FOR_GRADE') ||
                    281: 		       ( $status eq 'INVALID_ACCESS') )) {
                    282: 	return 0;
                    283:     }
1.64      albertel  284:     if ($env{'form.donescreen'}) { return 0; }
1.54      albertel  285:     return 1;
                    286: }
                    287: 
                    288: sub internal_location {
                    289:     my ($id)=@_;
                    290:     return '<!-- LONCAPA_INTERNAL_ADD_TASK_STATUS'.$id.' -->';
                    291: }
                    292: 
1.60      albertel  293: sub submission_time_stamp {
                    294:     my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                    295:     my $submissiontime;
1.89      albertel  296:     my $version=$Apache::lonhomework::history{'resource.0.version'};
1.60      albertel  297:     for (my $v=$Apache::lonhomework::history{'version'};$v>0;$v--) {
                    298: 	if (defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.bridgetask.portfiles'})) {
                    299: 	    $submissiontime=$Apache::lonhomework::history{$v.':timestamp'};
                    300: 	}
                    301:     }
                    302:     my $result;
                    303:     if ($submissiontime) {
1.89      albertel  304: 	my $slot_name=$Apache::lonhomework::history{'resource.'.$version.'.0.checkedin.slot'};
1.60      albertel  305: 	my %slot=&Apache::lonnet::get_slot($slot_name);
                    306: 	my $diff = $slot{'endtime'} - $submissiontime;
1.71      albertel  307: 	my ($color,$when)=('#FF6666','after');
                    308: 	if ($diff > 0) { ($color,$when)=('#336600','before'); }
1.60      albertel  309: 	my $info;
                    310: 	if ($diff%60) { $info=($diff%60).' seconds'; }
                    311: 	$diff=int($diff/60);
                    312: 	if ($diff%60) { $info=($diff%60).' minutes '.$info; }
                    313: 	$diff=int($diff/60);
                    314: 	if ($diff) {    $info=$diff.' hours '.$info; }
                    315: 	$result='<p><font color="'.$color.'">'.
                    316: 	    &mt('Student submitted [_1] [_2] the deadline. 
                    317:                  (Submission was at [_3], end of period was [_4].)',
                    318: 		$info,$when,scalar(localtime($submissiontime)),
                    319: 		scalar(localtime($slot{'endtime'}))).
                    320: 		'</font></p>';
                    321:     }
                    322:     return $result;
                    323: }
                    324: 
1.119     albertel  325: sub file_list {
                    326:     my ($files,$uname,$udom) = @_;
                    327:     if (!defined($uname) || !defined($udom)) {
                    328: 	(undef,undef,$udom,$uname) = &Apache::lonxml::whichuser();
                    329:     }
1.70      albertel  330:     my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
1.119     albertel  331: 
1.120     albertel  332:     my $file_list="<ul class=\"LC_GRADING_handininfo\">\n";
1.119     albertel  333:     foreach my $partial_file (split(',',$files)) {
1.70      albertel  334: 	my $file=$file_url.$partial_file;
                    335: 	$file=~s|/+|/|g;
                    336: 	&Apache::lonnet::allowuploaded('/adm/bridgetask',$file);
1.73      albertel  337: 	$file_list.='<li><nobr><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
1.109     albertel  338: 	    &Apache::loncommon::icon($file).'" border="0"> '.$file.
1.70      albertel  339: 	    '</a></nobr></li>'."\n";
                    340:     }
                    341:     $file_list.="</ul>\n";
1.119     albertel  342:     return $file_list;
                    343: }
                    344: 
                    345: sub webgrade_standard_info {
                    346:     my ($version)=&get_version();
                    347: 
                    348:     my $file_list = &file_list($Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"});
1.70      albertel  349: 
1.118     albertel  350:     my %lt=&Apache::lonlocal::texthash('done' => 'Next Item',
                    351: 				       'stop' => 'Quit Grading',
1.143     albertel  352: 				       'fail' => 'Fail Rest',
1.70      albertel  353: 				       );
                    354: 				       
                    355:     my $result=<<INFO;
1.120     albertel  356:   <div class="LC_GRADING_maincontrols">
1.111     albertel  357:     <input type="submit" name="next" value="$lt{'done'}" />
                    358:     <input type="submit" name="stop" value="$lt{'stop'}" />
1.143     albertel  359:     <input type="button" name="fail" value="$lt{'fail'}" 
                    360:            onclick="javascript:onFailRest()" />
1.111     albertel  361:   </div>
1.70      albertel  362:   $file_list
                    363: INFO
                    364:     return $result;
                    365: }
                    366: 
1.1       albertel  367: sub start_Task {
1.87      albertel  368:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel  369: 
1.4       albertel  370:     my ($status,$accessmsg,$slot);
1.16      albertel  371:     if ($target ne 'webgrade') {
1.70      albertel  372: 	&Apache::structuretags::init_problem_globals('Task');
1.16      albertel  373: 	&Apache::structuretags::initialize_storage();
                    374: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.74      albertel  375: 	if ($env{'request.state'} eq 'construct') {
                    376: 	    &Apache::structuretags::setup_rndseed($safeeval);
                    377: 	}
1.16      albertel  378:     } 
                    379: 
1.4       albertel  380:     $Apache::lonhomework::parsing_a_task=1;
1.141     albertel  381: 
                    382:     my $name;
                    383:     if ($target eq 'web' || $target eq 'webgrade') {
                    384: 	$name = &Apache::structuretags::get_resource_name($parstack,$safeeval);
                    385:     }
                    386: 
1.145     albertel  387:     my ($result,$form_tag_start);
                    388:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'tex'
                    389: 	|| $target eq 'edit') {
                    390: 	($result,$form_tag_start) =
                    391: 	    &Apache::structuretags::page_start($target,$token,$tagstack,
                    392: 					       $parstack,$parser,$safeeval,
1.146     albertel  393: 					       $name,&style($target));
1.145     albertel  394: 	$result .= '<div class="LC_task">'."\n";
                    395:     }
1.123     albertel  396: 
1.74      albertel  397:     if ($target eq 'web' && $env{'request.state'} ne 'construct') {
1.147     albertel  398: 	if ($Apache::lonhomework::queuegrade
                    399: 	    || $Apache::lonhomework::modifygrades) {
1.141     albertel  400: 	    $result.='<form name="gradesubmission" method="post" action="';
1.13      albertel  401: 	    my $uri=$env{'request.uri'};
                    402: 	    if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
1.141     albertel  403: 	    $result.=$uri.'">'.&add_grading_button()."</form>";
1.38      albertel  404: 	    my $symb=&Apache::lonnet::symbread();
1.40      albertel  405: 	    if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.141     albertel  406: 		$result.='<form method="post" name="slotrequest" action="/adm/slotrequest">'.
1.40      albertel  407: 		    '<input type="hidden" name="symb" value="'.$symb.'" />'.
                    408: 		    '<input type="hidden" name="command" value="showslots" />'.
                    409: 		    '<input type="submit" name="requestattempt" value="'.
                    410: 		    &mt('Show Slot list').'" />'.
                    411: 		    '</form>';
1.108     albertel  412: 		my $target_id = 
                    413: 		    &Apache::lonstathelpers::make_target_id({symb => $symb,
                    414: 							     part => '0'});
1.141     albertel  415: 		$result.='<form method="post" name="gradingstatus" action="/adm/statistics">'.
1.108     albertel  416: 		    '<input type="hidden" name="problemchoice" value="'.$target_id.'" />'.
                    417: 		    '<input type="hidden" name="reportSelected" value="grading_analysis" />'.
                    418: 		    '<input type="submit" name="grading" value="'.
                    419: 		    &mt('Show Grading Status').'" />'.
                    420: 		    '</form>';
1.40      albertel  421: 	    }
1.13      albertel  422: 	}
1.8       albertel  423:     }
1.74      albertel  424:     if ($target eq 'web' && $env{'request.state'} eq 'construct') {
                    425: 	$form_tag_start.=&Apache::structuretags::problem_web_to_edit_header($env{'form.rndseed'});
                    426:     }
1.21      albertel  427:     if ($target eq 'web' || ($target eq 'grade' && !$env{'form.webgrade'}) || $target eq 'answer' ||
1.1       albertel  428: 	$target eq 'tex') {
1.29      albertel  429: 	my ($version,$previous)=&get_version();
1.14      albertel  430: 	($status,$accessmsg,my $slot_name,$slot) = 
1.81      albertel  431: 	    &Apache::lonhomework::check_slot_access('0','Task');
1.9       albertel  432: 	push(@Apache::inputtags::status,$status);
1.14      albertel  433: 	$Apache::inputtags::slot_name=$slot_name;
1.1       albertel  434: 	my $expression='$external::datestatus="'.$status.'";';
1.89      albertel  435: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$version.0.solved"}.'";';
1.1       albertel  436: 	&Apache::run::run($expression,$safeeval);
                    437: 	&Apache::lonxml::debug("Got $status");
1.141     albertel  438: 	$result.=&add_previous_version_button($status);
1.54      albertel  439: 	if (!&show_task($status,$previous)) {
1.87      albertel  440: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.1       albertel  441: 	    if ( $target eq "web" ) {
1.74      albertel  442: 		if ($env{'request.state'} eq 'construct') {
                    443: 		    $result.=$form_tag_start;
                    444: 		}
1.4       albertel  445: 		my $msg;
1.1       albertel  446: 		if ($status eq 'UNAVAILABLE') {
                    447: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3       albertel  448: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    449: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.38      albertel  450: 		    $msg.=&add_request_another_attempt_button("Sign up for time to work.");
1.4       albertel  451: 		} elsif ($status eq 'NEEDS_CHECKIN') {
                    452: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
                    453: 			'</h1>'.&proctor_validation_screen($slot);
1.22      albertel  454: 		} elsif ($status eq 'WAITING_FOR_GRADE') {
                    455: 		    $msg.='<h1>'.&mt('Your submission is in the grading queue.').'</h1>';
1.64      albertel  456: 		} elsif ($env{'form.donescreen'}) {
                    457: 		    my $title=&Apache::lonnet::gettitle();
1.67      albertel  458: 		    my @files=split(',',$Apache::lonhomework::history{'resource.'.$version.'.0.bridgetask.portfiles'});
1.114     albertel  459: 		    my (undef,undef,$domain,$user)=
                    460: 			&Apache::lonxml::whichuser();
                    461: 		    my $files = '<ul>';
                    462: 		    foreach my $file (@files) {
                    463: 			my $url="/uploaded/$domain/$user/portfolio$file";
1.130     albertel  464: 			if (! &Apache::lonnet::stat_file($url)) {
                    465: 			    $file = &mt('<font color="red"> Nonexistant file:</font> <tt>[_1]</tt>',$file);
                    466: 			} else {
                    467: 			    $file = '<tt>'.$file.'</tt>';
                    468: 			}
1.114     albertel  469: 			$files .= '<li>'.$file.'</li>';
                    470: 		    }
                    471: 		    $files.='</ul>';
                    472: 
1.64      albertel  473: 		    $result.=<<DONESCREEN;
                    474: <h2>$title</h2>
                    475: <p> Files submitted: $files </p>
1.67      albertel  476: <p> You are now done with this Bridge Task </p>
1.64      albertel  477: <hr />
                    478: <p> <a href="/adm/logout">Logout</a> </p>
                    479: <p> <a href="/adm/roles">Change to a different course</a> </p>
                    480: DONESCREEN
1.1       albertel  481: 		} elsif ($status ne 'NOT_YET_VIEWED') {
                    482: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
                    483: 		}
                    484: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
                    485: 		    $msg.='The problem '.$accessmsg;
                    486: 		}
                    487: 		$result.=$msg.'<br />';
                    488: 	    } elsif ($target eq 'tex') {
                    489: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
                    490: 		if ($status eq 'UNAVAILABLE') {
                    491: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
                    492: 		} else {
                    493: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
                    494: 		}
1.22      albertel  495: 	    } elsif ($target eq 'grade' && !$env{'form.webgrade'}) {
1.4       albertel  496: 		if ($status eq 'NEEDS_CHECKIN') {
1.83      albertel  497: 		    if(&proctor_check_auth($slot_name,$slot,'Task')
                    498: 		       && defined($Apache::inputtags::slot_name)) {
1.148   ! albertel  499: 			my $result=
        !           500: 			    &add_to_queue('gradingqueue',
        !           501: 					  {'type' => 'task',
        !           502: 					   'time' => time,
        !           503: 					   'slot' => 
        !           504: 					       $Apache::inputtags::slot_name});
1.77      albertel  505: 			&Apache::lonxml::debug("add_to_queue said $result");
                    506: 		    }
1.4       albertel  507: 		}
1.1       albertel  508: 	    }
                    509: 	} elsif ($target eq 'web') {
1.141     albertel  510: 
1.57      albertel  511: 	    $result.=&preserve_grade_info();
                    512: 	    $result.=&internal_location();
1.36      albertel  513: 	    $result.=$form_tag_start.
                    514: 		'<input type="hidden" name="submitted" value="yes" />';
1.54      albertel  515: 	    &Apache::lonxml::startredirection();
1.1       albertel  516: 	}
1.21      albertel  517:     } elsif ( ($target eq 'grade' && $env{'form.webgrade'}) ||
                    518: 	      $target eq 'webgrade') {
1.32      albertel  519: 	my $webgrade='yes';
1.21      albertel  520: 	if ($target eq 'webgrade') {
1.141     albertel  521: 	    $result.= "\n".'<div class="LC_GRADING_task">'."\n".
1.124     albertel  522: 		'<script type="text/javascript" 
1.126     albertel  523:                          src="/res/adm/includes/task_grading.js"></script>';
1.49      albertel  524: 	    #$result.='<br />Review'.&show_queue('reviewqueue');
                    525: 	    #$result.='<br />Grade'.&show_queue('gradingqueue');
1.30      albertel  526: 	}
1.33      albertel  527: 	# FIXME Blast! still need to reorg this, need to reshow the
                    528:         #       queue being reviewed once done with the grade pass...
                    529:         #       Hrrm, vaildation pass should perhaps say 'not_locked'
                    530:         #       perhaps do a search if there is a key that is mine and if
                    531:         #       there isn't reshow the queue....
1.105     albertel  532: 	my ($todo,$status_code,$msg)=&get_key_todo($target);
1.33      albertel  533: 
                    534: 	if ($todo) {
                    535: 	    &setup_env_for_other_user($todo,$safeeval);
                    536: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
                    537: 	    $result.="\n".'<table><tr><td>Found '.
                    538: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
                    539: 	    $form_tag_start.=
                    540: 		'<input type="hidden" name="gradingkey" value="'.
                    541: 		&Apache::lonnet::escape($todo).'" />';
                    542: 	    $Apache::bridgetask::queue_key=$todo;
                    543: 	    &Apache::structuretags::initialize_storage();
                    544: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.110     albertel  545: 	    if ($target eq 'webgrade' && $status_code eq 'selected') {
                    546: 		$form_tag_start.=
                    547: 		    '<input type="hidden" name="queuemode" value="selected" />';
1.33      albertel  548: 	    }
1.15      albertel  549: 	} else {
1.33      albertel  550: 	    if ($target eq 'webgrade') {
                    551: 		$result.="\n";
1.81      albertel  552: 		my $back='<p><a href="/adm/flip?postdata=return:">'.
                    553: 		    &mt('Return to resource').'</a></p>';
1.33      albertel  554: 		if      ($status_code eq 'stop') {
1.81      albertel  555: 		    $result.='<b>'.&mt("Stopped grading.").'</b>'.$back;
1.33      albertel  556: 		} elsif ($status_code eq 'lock_failed') {
1.105     albertel  557: 		    $result.='<b>'.&mt("Failed to lock the requested record.")
1.81      albertel  558: 			.'</b>'.$back;
1.33      albertel  559: 		} elsif ($status_code eq 'unlock') {
1.81      albertel  560: 		    $result.='<b>'.&mt("Unlocked the requested record.")
                    561: 			.'</b>'.$back;
1.33      albertel  562: 		    $result.=&show_queue($env{'form.queue'},1);
                    563: 		} elsif ($status_code eq 'show_list') {
                    564: 		    $result.=&show_queue($env{'form.queue'},1);
1.49      albertel  565: 		} elsif ($status_code eq 'select_user') {
                    566: 		    $result.=&select_user();
1.95      albertel  567: 		} elsif ($status_code eq 'unable') {
                    568: 		    $result.='<b>'.&mt("Unable to aqcuire a user to grade.").'</b>'.$back;
1.105     albertel  569: 		} elsif ($status_code eq 'not_allowed') {
                    570: 		    $result.='<b>'.&mt('Not allowed to grade the requested user.').' '.$msg.'</b>'.$back;
1.33      albertel  571: 		} else {
1.81      albertel  572: 		    $result.='<b>'.&mt("No user to be graded.").'</b>'.$back;
1.32      albertel  573: 		}
1.21      albertel  574: 	    }
1.33      albertel  575: 	    $webgrade='no';
1.87      albertel  576: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.32      albertel  577: 	}
                    578: 	if ($target eq 'webgrade' && defined($env{'form.queue'})) {
1.61      albertel  579: 	    if ($webgrade eq 'yes') {
                    580: 		$result.=&submission_time_stamp();
                    581: 	    }
1.32      albertel  582: 	    $result.=$form_tag_start;
                    583: 	    $result.='<input type="hidden" name="webgrade" value="'.
                    584: 		$webgrade.'" />';
                    585: 	    $result.='<input type="hidden" name="queue" value="'.
                    586: 		$env{'form.queue'}.'" />';
1.52      albertel  587: 	    if ($env{'form.regrade'}) {
                    588: 		$result.='<input type="hidden" name="regrade" value="'.
                    589: 		    $env{'form.regrade'}.'" />';
                    590: 	    }
1.62      albertel  591: 	    if ($env{'form.chosensections'}) {
                    592: 		my @chosen_sections=
                    593: 		    &Apache::loncommon::get_env_multiple('form.chosensections');
                    594: 		foreach my $sec (@chosen_sections) {
                    595: 		    $result.='<input type="hidden" name="chosensections" 
                    596:                                value="'.$sec.'" />';
                    597: 		}
                    598: 	    }
1.70      albertel  599: 	    if ($webgrade eq 'yes') { $result.=&webgrade_standard_info(); }
1.15      albertel  600: 	}
1.110     albertel  601: 	if ($target eq 'webgrade') {
1.120     albertel  602: 	    $result.="\n".'<div id="LC_GRADING_criterialist">';
1.110     albertel  603: 	}
1.74      albertel  604:     } elsif ($target eq 'edit') {
1.141     albertel  605: 	$result.=$form_tag_start.
1.74      albertel  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: 
1.138     albertel  641:     my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.105     albertel  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.',
1.138     albertel  687: 		    $who,$env{'form.gradinguser'}.':'.$env{'form.gradingdomain'}));
1.105     albertel  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>';
1.116     albertel  797: 		    my $action = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.64      albertel  798: 		    $result.=<<DONEBUTTON;
1.115     albertel  799: <form name="done" method="post" action="$action">
1.64      albertel  800:    <input type="hidden" name="donescreen" value="1" />
                    801:    <input type="submit" value="Done" />
                    802: </form>
                    803: DONEBUTTON
1.77      albertel  804:                 }
1.56      albertel  805: 		if (&show_task($status,$previous) &&
1.89      albertel  806: 		    $Apache::lonhomework::history{"resource.$version.0.status"} =~ /^(pass|fail)$/) {
                    807: 		    my $bt_status=$Apache::lonhomework::history{"resource.$version.0.status"};
1.54      albertel  808: 		    my $title=&Apache::lonnet::gettitle();
1.80      albertel  809: 
                    810: 		    my $slot_name=
1.89      albertel  811: 			$Apache::lonhomework::history{"resource.$version.0.checkedin.slot"};
1.80      albertel  812: 		    my %slot=&Apache::lonnet::get_slot($slot_name);
1.55      albertel  813: 		    my $start_time=
                    814: 			&Apache::lonlocal::locallocaltime($slot{'starttime'});
1.54      albertel  815: 
1.116     albertel  816: 		    my $status = "\n<div class='LC_$bt_status LC_criteria'>\n";
1.54      albertel  817: 		    
                    818: 		    if ($bt_status eq 'pass')  {
                    819: 			$status.='<h2>You passed the '.$title.' given on '.
1.80      albertel  820: 			    $start_time.'</h2>';
1.54      albertel  821: 		    }
                    822: 		    if ($bt_status eq 'fail')  {
                    823: 			$status.='<h2>You did not pass the '.$title.' given on '.
1.80      albertel  824: 			    $start_time.'</h2>';
1.54      albertel  825: 			if (!$previous) {
                    826: 			    $status.=&add_request_another_attempt_button();
                    827: 			}
                    828: 		    }
                    829: 		    my $man_count=0;
                    830: 		    my $opt_count=0;
                    831: 		    my $opt_passed=0;
                    832: 		    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    833: 			if ($Apache::bridgetask::dimensionmandatory{$dim_id}
                    834: 			    eq 'N') {
                    835: 			    $opt_count++;
1.89      albertel  836: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim_id.status"} eq 'pass') {
1.54      albertel  837: 				$opt_passed++;
                    838: 			    }
                    839: 			} else {
                    840: 			    $man_count++;
                    841: 			}
                    842: 		    }
                    843: 		    my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
                    844: 							 $parstack,$safeeval);
                    845: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
1.99      albertel  846: 		    $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  847: 
                    848: 		    my $internal_location=&internal_location();
                    849: 		    $result=~s/\Q$internal_location\E/$status/;
                    850: 		}
1.13      albertel  851: 	    } 
1.117     albertel  852: 	    if ($target eq 'web') {
1.142     albertel  853: 		$result.="\n</div>\n".
                    854: 		    &Apache::loncommon::end_page({'discussion' => 1});
1.1       albertel  855: 	    }
                    856: 	}
1.29      albertel  857: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel  858: 	    my $award='SUBMITTED';
1.28      albertel  859: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel  860: 						    'portfiles',\$award);
1.14      albertel  861: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel  862: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                    863: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    864: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                    865: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                    866: 
                    867: 		$Apache::lonhomework::results{"resource.0.award"}=
                    868: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                    869: 		    $award;
1.51      albertel  870: 		$Apache::lonhomework::results{"resource.0.submission"}=
                    871: 		    $Apache::lonhomework::results{"resource.$version.0.submission"}='';
1.64      albertel  872: 	    } else {
                    873: 		delete($Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"});
1.77      albertel  874: 		$award = '';
1.10      albertel  875: 	    }
1.4       albertel  876: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    877: 	    &Apache::structuretags::finalize_storage();
1.148   ! albertel  878: 	    if ($award eq 'SUBMITTED') {
        !           879: 		my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
        !           880: 		if ($useslots =~ /^\s*no\s*$/i) {
        !           881: 		    &add_to_queue('gradingqueue',
        !           882: 				  {'type' => 'task',
        !           883: 				   'time' => time});
        !           884: 		} elsif (defined($Apache::inputtags::slot_name)) {
        !           885: 		    &add_to_queue('gradingqueue',
        !           886: 				  {'type' => 'task',
        !           887: 				   'time' => time,
        !           888: 				   'slot' => $Apache::inputtags::slot_name});
        !           889: 		}
1.14      albertel  890: 	    }
1.79      albertel  891: 	} elsif ($Apache::lonhomework::results{'INTERNAL_store'}) {
                    892: 	    &Apache::structuretags::finalize_storage();
1.1       albertel  893: 	}
1.32      albertel  894: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes') {
1.20      albertel  895: 	    my $optional_required=
                    896: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                    897: 					   $safeeval);
                    898: 	    my $optional_passed=0;
                    899: 	    my $mandatory_failed=0;
                    900: 	    my $ungraded=0;
                    901: 	    my $review=0;   
1.21      albertel  902: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.20      albertel  903: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    904: 		my $status=
1.89      albertel  905: 		    $Apache::lonhomework::results{"resource.$version.0.$dim_id.status"};
1.20      albertel  906: 		my $mandatory=
                    907: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
                    908: 		if ($status eq 'pass') {
                    909: 		    if (!$mandatory) { $optional_passed++; }
                    910: 		} elsif ($status eq 'fail') {
                    911: 		    if ($mandatory) { $mandatory_failed++; }
                    912: 		} elsif ($status eq 'ungraded') {
                    913: 		    $ungraded++;
                    914: 		} elsif ($status eq 'review') {
                    915: 		    $review++;
1.49      albertel  916: 		} else {
                    917: 		    $ungraded++;
                    918: 		}
1.20      albertel  919: 	    }
                    920: 	    if ($optional_passed < $optional_required) {
                    921: 		$mandatory_failed++;
                    922: 	    }
1.21      albertel  923: 	    &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  924: 	    $Apache::lonhomework::results{'resource.0.regrader'}=
1.138     albertel  925: 		$env{'user.name'}.':'.$env{'user.domain'};
1.20      albertel  926: 	    if ($review) {
1.89      albertel  927: 		$Apache::lonhomework::results{"resource.$version.0.status"}='review';
1.33      albertel  928: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    929: 		    &check_queue_unlock($env{'form.queue'});
                    930: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                    931: 		} else {
1.49      albertel  932: 		    &move_between_queues($env{'form.queue'},'reviewqueue');
1.33      albertel  933: 		}
1.20      albertel  934: 	    } elsif ($ungraded) {
1.89      albertel  935: 		$Apache::lonhomework::results{"resource.$version.0.status"}='ungraded';
1.49      albertel  936: 		if ($env{'form.queue'} eq 'reviewqueue' ||
                    937: 		    $env{'form.queue'} eq 'none' ) {
1.33      albertel  938: 		    &Apache::lonxml::debug("moving back.");
1.49      albertel  939: 		    &move_between_queues($env{'form.queue'},'gradingqueue');
1.33      albertel  940: 		} else {
                    941: 		    &check_queue_unlock($env{'form.queue'});
                    942: 		}
1.20      albertel  943: 	    } elsif ($mandatory_failed) {
1.89      albertel  944: 		$Apache::lonhomework::results{"resource.$version.0.status"}='fail';
1.25      albertel  945: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                    946: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                    947: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.39      albertel  948: 		&remove_from_queue($env{'form.queue'}); 
                    949: 
                    950: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  951: 		
                    952: 		if ($env{'form.regrade'} ne 'yes') {
                    953: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    954: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    955: 		    &Apache::grades::version_portfiles(
                    956: 						       \%Apache::lonhomework::results,
                    957: 						       ["$version.0.bridgetask"],$courseid,
                    958: 						       $symb,$udom,$uname,
                    959: 						       ["$version.0.bridgetask"]);
                    960: 		}
1.20      albertel  961: 	    } else {
1.89      albertel  962: 		$Apache::lonhomework::results{"resource.$version.0.status"}='pass';
1.25      albertel  963: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                    964: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                    965: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.32      albertel  966: 		&remove_from_queue($env{'form.queue'});
1.39      albertel  967: 
                    968: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  969: 		if ($env{'form.regrade'} ne 'yes') {
                    970: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    971: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    972: 		    &Apache::grades::version_portfiles(
                    973: 						       \%Apache::lonhomework::results,
                    974: 						       ["$version.0.bridgetask"],$courseid,
                    975: 						       $symb,$udom,$uname,
                    976: 						       ["$version.0.bridgetask"]);
                    977: 		}
1.20      albertel  978: 	    }
1.89      albertel  979: 	    $Apache::lonhomework::results{"resource.0.status"}=
                    980: 		$Apache::lonhomework::results{"resource.$version.0.status"};
1.28      albertel  981: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel  982: 		$Apache::lonhomework::results{"resource.0.award"}=
1.50      albertel  983: 		    $Apache::lonhomework::results{"resource.$version.0.award"};
1.26      albertel  984: 		$Apache::lonhomework::results{"resource.0.awarded"}=
1.50      albertel  985: 		    $Apache::lonhomework::results{"resource.$version.0.awarded"};
1.26      albertel  986: 		$Apache::lonhomework::results{"resource.0.solved"}=
1.50      albertel  987: 		    $Apache::lonhomework::results{"resource.$version.0.solved"};
1.25      albertel  988: 	    }
1.94      albertel  989: 	    &minimize_storage();
1.21      albertel  990: 	    &Apache::structuretags::finalize_storage();
1.20      albertel  991: 	}
1.15      albertel  992:     } elsif ($target eq 'webgrade') {
1.131     albertel  993: 	$result.="</div>";
1.20      albertel  994: 	#$result.='<input type="submit" name="next" value="'.
                    995: 	#    &mt('Save &amp; Next').'" /> ';
                    996: 	#$result.='<input type="submit" name="end" value="'.
                    997: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                    998: 	#$result.='<input type="submit" name="throwaway" value="'.
                    999: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                   1000: 	#$result.='<input type="submit" name="save" value="'.
                   1001: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.124     albertel 1002: 	$result.='</form>'."\n</div>\n</div>\n".
1.140     albertel 1003: 	    &Apache::loncommon::end_page();
1.1       albertel 1004:     } elsif ($target eq 'meta') {
1.70      albertel 1005: 	$result.=&Apache::response::meta_package_write('Task');
1.77      albertel 1006:         $result.=&Apache::response::meta_stores_write('solved','string',
                   1007: 						      'Problem Status');
                   1008: 	$result.=&Apache::response::meta_stores_write('tries','int_zeropos',
                   1009: 						      'Number of Attempts');
                   1010: 	$result.=&Apache::response::meta_stores_write('awarded','float',
                   1011: 						      'Partial Credit Factor');
                   1012: 	$result.=&Apache::response::meta_stores_write('status','string',
                   1013: 						      'Bridge Task Status');
1.1       albertel 1014:     }
1.4       albertel 1015:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel 1016:     return $result;
                   1017: }
                   1018: 
1.31      albertel 1019: sub move_between_queues {
                   1020:     my ($src_queue,$dest_queue)=@_;
1.49      albertel 1021:     my $cur_data;
                   1022:     if ($src_queue ne 'none') {
                   1023: 	$cur_data=&get_queue_data($src_queue);
                   1024: 	if (!$cur_data) { return 'not_exist'; }
                   1025:     } else {
                   1026: 	$cur_data = ['none'];
                   1027:     }
1.148   ! albertel 1028:     my $result=&add_to_queue($dest_queue,$cur_data);
1.31      albertel 1029:     if ($result ne 'ok') {
                   1030: 	return $result;
                   1031:     }
                   1032:     &check_queue_unlock($src_queue);
                   1033:     return &remove_from_queue($src_queue);
1.21      albertel 1034: }
                   1035: 
                   1036: sub check_queue_unlock {
1.32      albertel 1037:     my ($queue,$key,$allow_not_me)=@_;
1.49      albertel 1038:     if ($queue eq 'none') { return 'ok'; }
1.30      albertel 1039:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.32      albertel 1040:     if (!defined($key)) {
1.138     albertel 1041: 	$key="$symb\0queue\0$uname:$udom";
1.32      albertel 1042:     }
1.30      albertel 1043:     my $cnum=$env{'course.'.$cid.'.num'};
                   1044:     my $cdom=$env{'course.'.$cid.'.domain'};
1.138     albertel 1045:     my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.30      albertel 1046:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                   1047:     if  ($who eq $me) {
1.32      albertel 1048: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                   1049:     } elsif ($allow_not_me) {
1.33      albertel 1050: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel 1051: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel 1052:     }
1.32      albertel 1053:     return 'not_owner';
1.21      albertel 1054: }
                   1055: 
1.88      albertel 1056: sub in_queue {
                   1057:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
                   1058:     if ($queue eq 'none') { return 0; }
                   1059:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1060: 	|| !defined($udom) || !defined($uname)) {
                   1061: 	($symb,my $cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1062: 	$cnum=$env{'course.'.$cid.'.num'};
                   1063: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1064:     }
                   1065: 
                   1066:     my $key=&encode_queue_key($symb,$udom,$uname);
                   1067:     my %results = &Apache::lonnet::get($queue,[$key],$cdom,$cnum);
                   1068: 
                   1069:     if (defined($results{$key})) {
                   1070: 	return 1;
                   1071:     }
                   1072:     return 0;
                   1073: }
                   1074: 
1.21      albertel 1075: sub remove_from_queue {
1.86      albertel 1076:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
1.49      albertel 1077:     if ($queue eq 'none') { return 'ok'; }
1.86      albertel 1078:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1079: 	|| !defined($udom) || !defined($uname)) {
                   1080: 	($symb,my $cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1081: 	$cnum=$env{'course.'.$cid.'.num'};
                   1082: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1083:     }
1.88      albertel 1084:     if (!&in_queue($queue,$symb,$cdom,$cnum,$udom,$uname)) {
                   1085: 	return 'ok';
                   1086:     }
1.86      albertel 1087:     my $key=&encode_queue_key($symb,$udom,$uname);
1.27      albertel 1088:     my @keys=($key,"$key\0locked");
1.31      albertel 1089:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel 1090: }
                   1091: 
1.16      albertel 1092: sub setup_env_for_other_user {
                   1093:     my ($queue_key,$safeeval)=@_;
                   1094:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel 1095:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel 1096:     $env{'form.grade_symb'}=$symb;
                   1097:     $env{'form.grade_domain'}=$udom;
                   1098:     $env{'form.grade_username'}=$uname;
                   1099:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                   1100:     &Apache::lonxml::initialize_rndseed($safeeval);
                   1101: }
                   1102: 
1.31      albertel 1103: sub get_queue_data {
                   1104:     my ($queue)=@_;
                   1105:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1106:     my $cnum=$env{'course.'.$cid.'.num'};
                   1107:     my $cdom=$env{'course.'.$cid.'.domain'};
1.138     albertel 1108:     my $todo="$symb\0queue\0$uname:$udom";
1.31      albertel 1109:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                   1110:     if ($key eq $todo && ref($value)) {
                   1111: 	return $value;
                   1112:     }
                   1113:     return undef;
                   1114: }
                   1115: 
1.84      albertel 1116: 
1.49      albertel 1117: sub check_queue_for_key {
1.84      albertel 1118:     my ($cdom,$cnum,$queue,$todo)=@_;
                   1119: 
1.49      albertel 1120:     my %results=
                   1121: 	&Apache::lonnet::get($queue,[$todo,"$todo\0locked"],$cdom,$cnum);
                   1122:     
                   1123:     if (exists($results{$todo}) && ref($results{$todo})) {
                   1124: 	if (defined($results{"$todo\0locked"})) {
                   1125: 	    return 'locked';
                   1126: 	}
1.148   ! albertel 1127: 	if (my $slot=&slotted_access($results{$todo})) {
1.86      albertel 1128: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1129: 	    if ($slot_data{'endtime'} > time) { 
                   1130: 		return 'in_progress';
                   1131: 	    }
1.148   ! albertel 1132: 	} else {
        !          1133: 	    my ($symb) = &decode_queue_key($todo);
        !          1134: 	    my $due_date = &Apache::lonhomework::due_date('0',$symb);
        !          1135: 	    if ($due_date > time) {
        !          1136: 		return 'in_progress';
        !          1137: 	    }
1.58      albertel 1138: 	}
1.49      albertel 1139: 	return 'enqueued';
                   1140:     }
                   1141:     return undef;
                   1142: }
                   1143: 
1.14      albertel 1144: sub add_to_queue {
1.82      albertel 1145:     my ($queue,$user_data)=@_;
1.49      albertel 1146:     if ($queue eq 'none') { return 'ok'; }
1.14      albertel 1147:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.82      albertel 1148:     if (!$cid || $env{'request.state'} eq 'construct') {
                   1149: 	return 'no_queue';
                   1150:     }
1.14      albertel 1151:     my $cnum=$env{'course.'.$cid.'.num'};
                   1152:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1153:     my %data;
1.138     albertel 1154:     $data{"$symb\0queue\0$uname:$udom"}=$user_data;
1.83      albertel 1155:     return &Apache::lonnet::cput($queue,\%data,$cdom,$cnum);
1.14      albertel 1156: }
                   1157: 
                   1158: sub show_queue {
1.32      albertel 1159:     my ($queue,$with_selects)=@_;
1.14      albertel 1160:     my $result;
                   1161:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1162:     my $cnum=$env{'course.'.$cid.'.num'};
                   1163:     my $cdom=$env{'course.'.$cid.'.domain'};
1.59      albertel 1164: 
                   1165:     my @chosen_sections=
                   1166: 	&Apache::loncommon::get_env_multiple('form.chosensections');
                   1167:     &Apache::grades::init_perm();
                   1168:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
                   1169:     &Apache::grades::reset_perm();
1.63      albertel 1170:     if (!(grep(/^all$/,@chosen_sections))) {
                   1171: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1172: 	    '</tt>.</p> '."\n";
                   1173:     }
1.59      albertel 1174: 
1.16      albertel 1175:     my $regexp="^$symb\0";
1.30      albertel 1176:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel 1177:     my ($tmp)=%queue;
                   1178:     if ($tmp=~/^error: 2 /) {
1.33      albertel 1179: 	return "\n<h3>Current Queue - $queue</h3><table border='1'><tr><td>Empty</td></tr></table>";
1.31      albertel 1180:     }
1.103     albertel 1181:     my $title=&Apache::lonnet::gettitle($symb);
                   1182:     $result.="\n<h3>Current Queue - $title $queue </h3><table border='1'><tr>";
                   1183:     if ($with_selects) { $result.="<th>Status</th><th></th>"; }
                   1184:     $result.="<th>user</th><th>data</th></tr>";
1.14      albertel 1185:     foreach my $key (sort(keys(%queue))) {
1.59      albertel 1186: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1187: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
1.32      albertel 1188: 	if ($key=~/locked$/ && !$with_selects) {
1.103     albertel 1189: 	    $result.="<tr><td>$uname</td>";
                   1190: 	    $result.='<td>'.$queue{$key}.'</td></tr>';
1.32      albertel 1191: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.103     albertel 1192: 	    $result.="<tr><td></td>";
                   1193: 	    $result.='<td>'.
1.16      albertel 1194: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel 1195: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1196: 	    $result.="<tr>";
1.148   ! albertel 1197: 	    my ($end_time,$slot_text);
        !          1198: 	    if (my $slot=&slotted_access($queue{$key})) {
        !          1199: 		my %slot_data=&Apache::lonnet::get_slot($slot);
        !          1200: 		$end_time = $slot_data{'endtime'};
        !          1201: 		$slot_text = &mt('Slot: [_1]',$slot);
        !          1202: 	    } else {
        !          1203: 		$end_time = &Apache::lonhomework::due_date('0',$symb);
        !          1204: 		$slot_text = '';
        !          1205: 	    }
1.32      albertel 1206: 	    if ($with_selects) {
                   1207: 		my $ekey=&Apache::lonnet::escape($key);
1.103     albertel 1208: 		my ($action,$description,$status)=('select',&mt('Select'));
1.32      albertel 1209: 		if (exists($queue{"$key\0locked"})) {
1.138     albertel 1210: 		    my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.103     albertel 1211: 		    $status=&mt('Locked by <tt>[_1]</tt>',$queue{"$key\0locked"});
1.32      albertel 1212: 		    if ($me eq $queue{"$key\0locked"}) {
                   1213: 			($action,$description)=('resume',&mt('Resume'));
                   1214: 		    } else {
                   1215: 			($action,$description)=('unlock',&mt('Unlock'));
                   1216: 		    }
                   1217: 		}
1.62      albertel 1218: 		my $seclist;
                   1219: 		foreach my $sec (@chosen_sections) {
                   1220: 		    $seclist.='<input type="hidden" name="chosensections" 
                   1221:                                value="'.$sec.'" />';
                   1222: 		}
1.148   ! albertel 1223: 		if ($end_time ne '' && time > $end_time) {
1.35      albertel 1224: 		    $result.=(<<FORM);
1.103     albertel 1225: <td>$status</td>
1.32      albertel 1226: <td>
1.115     albertel 1227: <form style="display: inline" method="post">
1.32      albertel 1228:  <input type="hidden" name="gradingkey" value="$ekey" />
                   1229:  <input type="hidden" name="queue" value="$queue" />
                   1230:  <input type="hidden" name="gradingaction" value="$action" />
                   1231:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel 1232:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel 1233:  <input type="submit" name="submit" value="$description" />
1.62      albertel 1234:  $seclist
1.32      albertel 1235: </form>
                   1236: </td>
                   1237: FORM
1.35      albertel 1238: 
                   1239:                 } else {
1.148   ! albertel 1240: 		    $result.='<td>'.&mt("In Progress").'</td><td>&nbsp;</td>'
1.35      albertel 1241: 		}
1.32      albertel 1242: 	    }
1.104     albertel 1243: 	    $result.= "<td>".$fullname->{$uname.':'.$udom}.
1.138     albertel 1244: 		" <tt>($uname:$udom)</tt> </td>";
1.148   ! albertel 1245: 	    $result.='<td>'.$slot_text.' End time: '.
        !          1246: 		&Apache::lonlocal::locallocaltime($end_time).
1.20      albertel 1247: 		"</td></tr>";
1.16      albertel 1248: 	}
1.14      albertel 1249:     }
1.15      albertel 1250:     $result.="</table><hr />\n";
1.14      albertel 1251:     return $result;
                   1252: }
                   1253: 
1.34      albertel 1254: sub get_queue_counts {
                   1255:     my ($queue)=@_;
                   1256:     my $result;
                   1257:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1258:     my $cnum=$env{'course.'.$cid.'.num'};
                   1259:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1260:     my $regexp="^$symb\0";
                   1261:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1262:     my ($tmp)=%queue;
                   1263:     if ($tmp=~/^error: 2 /) {
                   1264: 	return (0,0,0);
                   1265:     }
                   1266:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
1.96      albertel 1267:     my %slot_cache;
1.34      albertel 1268:     foreach my $key (sort(keys(%queue))) {
                   1269: 	if ($key=~/locked$/) {
                   1270: 	    $locks++;
                   1271: 	} elsif ($key=~/timestamp$/) {
                   1272: 	    #ignore
                   1273: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1274: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1275: 	    $entries++;
1.148   ! albertel 1276: 	    if (my $slot=&slotted_access($queue{$key})) {
        !          1277: 		if (!exists($slot_cache{$slot})) {
        !          1278: 		    my %slot_data=&Apache::lonnet::get_slot($slot);
        !          1279: 		    $slot_cache{$slot} = \%slot_data;
        !          1280: 		}
        !          1281: 		if (time > $slot_cache{$slot}{'endtime'}) {
        !          1282: 		    $ready_to_grade++;
        !          1283: 		}
        !          1284: 	    } else {
        !          1285: 		my $due_date = &Apache::lonhomework::due_date('0',$symb);
        !          1286: 		if ($due_date ne '' && time > $due_date) {
        !          1287: 		    $ready_to_grade++;
        !          1288: 		}
1.34      albertel 1289: 	    }
                   1290: 	}
                   1291:     }
                   1292:     return ($entries,$ready_to_grade,$locks);
                   1293: }
                   1294: 
1.49      albertel 1295: sub encode_queue_key {
                   1296:     my ($symb,$udom,$uname)=@_;
1.138     albertel 1297:     return "$symb\0queue\0$uname:$udom";
1.49      albertel 1298: }
                   1299: 
1.14      albertel 1300: sub decode_queue_key {
                   1301:     my ($key)=@_;
                   1302:     my ($symb,undef,$user) = split("\0",$key);
1.138     albertel 1303:     my ($uname,$udom) = split(':',$user);
1.14      albertel 1304:     return ($symb,$uname,$udom);
                   1305: }
                   1306: 
                   1307: sub queue_key_locked {
1.30      albertel 1308:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel 1309:     if (!defined($cdom) || !defined($cnum)) {
                   1310: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1311: 	$cnum=$env{'course.'.$cid.'.num'};
                   1312: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1313:     }
1.14      albertel 1314:     my ($key_locked,$value)=
1.30      albertel 1315: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel 1316:     if ($key_locked eq "$key\0locked") {
                   1317: 	return $value;
                   1318:     }
                   1319:     return undef;
                   1320: }
                   1321: 
1.148   ! albertel 1322: sub slotted_access {
        !          1323:     my ($queue_entry) = @_;
        !          1324:     if (ref($queue_entry) eq 'ARRAY') {
        !          1325: 	if (defined($queue_entry->[0])) {
        !          1326: 	    return $queue_entry->[0];
        !          1327: 	}
        !          1328: 	return undef;
        !          1329:     } elsif (ref($queue_entry) eq 'HASH') {
        !          1330: 	if (defined($queue_entry->{'slot'})) {
        !          1331: 	    return $queue_entry->{'slot'};
        !          1332: 	}
        !          1333: 	return undef;
        !          1334:     }
        !          1335:     return undef;
        !          1336: }
        !          1337: 
1.14      albertel 1338: sub pick_from_queue_data {
1.30      albertel 1339:     my ($queue,$check_section,$queuedata,$cdom,$cnum)=@_;
1.98      albertel 1340:     my @possible; # will hold queue entries that are valid to be selected
1.30      albertel 1341:     foreach my $key (keys(%$queuedata)) {
1.68      albertel 1342: 	if ($key =~ /\0locked$/) { next; }
                   1343: 	if ($key =~ /\0timestamp$/) { next; }
1.14      albertel 1344: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                   1345: 	if ($check_section) {
                   1346: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel 1347: 	    if ($section eq $check_section) {
1.33      albertel 1348: 		&Apache::lonxml::debug("my sec");
1.15      albertel 1349: 		next;
                   1350: 	    }
1.14      albertel 1351: 	}
1.148   ! albertel 1352: 	my $end_time;
        !          1353: 	if (my $slot=&slotted_access($queuedata->{$key})) {
        !          1354: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
        !          1355: 	    if ($slot_data{'endtime'} < time) { 
        !          1356: 		$end_time = $slot_data{'endtime'};
        !          1357: 	    }
        !          1358: 	} else {
        !          1359: 	    my $due_date = &Apache::lonhomework::due_date('0',$symb);
        !          1360: 	    if ($due_date > time) {
        !          1361: 		$end_time = $due_date;
        !          1362: 	    }
        !          1363: 	}
        !          1364: 	if ($end_time ne '') {
1.33      albertel 1365: 	    &Apache::lonxml::debug("not time");
1.15      albertel 1366: 	    next;
                   1367: 	}
1.148   ! albertel 1368: 	
1.98      albertel 1369: 	if (exists($queuedata->{"$key\0locked"})) {
1.33      albertel 1370: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel 1371: 	    next;
                   1372: 	}
1.148   ! albertel 1373: 	push(@possible,[$key,$end_time]);
1.98      albertel 1374:     }
                   1375:     if (@possible) {
                   1376:         # sort entries in order by slot end time
                   1377: 	@possible = sort { $a->[1] <=> $b->[1] } @possible;
1.137     albertel 1378: 	# pick one of the entries in the top 10% in small queues and one
                   1379: 	# of the first ten entries in large queues
1.139     albertel 1380: 	#my $ten_percent = int($#possible * 0.1);
                   1381: 	#if ($ten_percent < 1 ) { $ten_percent = 1;  }
                   1382: 	#if ($ten_percent > 10) { $ten_percent = 10; }
                   1383: 	#my $max=($#possible < $ten_percent) ? $#possible : $ten_percent;
1.137     albertel 1384: 	
1.139     albertel 1385: 	#return $possible[int(rand($max))][0];
                   1386: 	return $possible[0][0];
1.14      albertel 1387:     }
                   1388:     return undef;
                   1389: }
                   1390: 
1.15      albertel 1391: sub find_mid_grade {
1.30      albertel 1392:     my ($queue,$symb,$cdom,$cnum)=@_;
1.16      albertel 1393:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.138     albertel 1394:     my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.15      albertel 1395:     if ($todo) {
1.30      albertel 1396: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel 1397: 	if ($who eq $me) { return $todo; }
                   1398:     }
                   1399:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel 1400:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel 1401:     foreach my $key (keys(%locks)) {
                   1402: 	my $who=$locks{$key};
                   1403: 	if ($who eq $me) {
                   1404: 	    $todo=$key;
                   1405: 	    $todo=~s/\0locked$//;
                   1406: 	    return $todo;
                   1407: 	}
                   1408:     }
                   1409:     return undef;
                   1410: }
                   1411: 
1.32      albertel 1412: sub lock_key {
                   1413:     my ($queue,$todo)=@_;
1.138     albertel 1414:     my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.32      albertel 1415:     my (undef,$cid)=&Apache::lonxml::whichuser();
                   1416:     my $cnum=$env{'course.'.$cid.'.num'};
                   1417:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1418:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                   1419: 					$cdom,$cnum);
1.33      albertel 1420:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel 1421:     if ($success eq 'ok') {
                   1422: 	return 1;
                   1423:     }
                   1424:     return 0;
                   1425: }
                   1426: 
1.86      albertel 1427: sub get_queue_symb_status {
1.85      albertel 1428:     my ($queue,$symb,$cdom,$cnum) = @_;
                   1429:     if (!defined($cdom) || !defined($cnum)) {
                   1430: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1431: 	$cnum=$env{'course.'.$cid.'.num'};
                   1432: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1433:     }
                   1434:     my $regexp="^$symb\0";
                   1435:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1436:     my ($tmp)=%queue;
                   1437:     if ($tmp=~/^error: 2 /) { return; }
                   1438:     my @users;
                   1439:     foreach my $key (sort(keys(%queue))) {
                   1440: 	next if ($key=~/locked$/);
                   1441: 	next if ($key=~/timestamp$/);
                   1442: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1443: 	push(@users,"$uname:$udom");
                   1444:     }
                   1445:     return @users;
                   1446: }
                   1447: 
1.14      albertel 1448: sub get_from_queue {
1.30      albertel 1449:     my ($queue)=@_;
1.14      albertel 1450:     my $result;
                   1451:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1452:     my $cnum=$env{'course.'.$cid.'.num'};
                   1453:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel 1454:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel 1455:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel 1456:     if ($todo) { return $todo; }
1.95      albertel 1457:     my $attempts=0;
1.14      albertel 1458:     while (1) {
1.95      albertel 1459: 	if ($attempts > 2) {
                   1460: 	    # tried twice to get a queue entry, giving up
                   1461: 	    return (undef,'unable');
                   1462: 	}
1.14      albertel 1463: 	my $starttime=time;
1.83      albertel 1464: 	&Apache::lonnet::cput($queue,{"$symb\0timestamp"=>$starttime},
                   1465: 			      $cdom,$cnum);
1.33      albertel 1466: 	&Apache::lonxml::debug("$starttime");
1.14      albertel 1467: 	my $regexp="^$symb\0queue\0";
1.97      albertel 1468: 	my $range= ($attempts < 1 ) ? '0-100' : '0-400';
                   1469: 
1.98      albertel 1470: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel 1471: 	#make a pass looking for a user _not_ in my section
1.14      albertel 1472: 	if ($env{'request.course.sec'}) {
1.33      albertel 1473: 	    &Apache::lonxml::debug("sce");
1.30      albertel 1474: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                   1475: 					\%queue,$cdom,$cnum);
1.33      albertel 1476: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel 1477: 	}
1.33      albertel 1478: 	# no one _not_ in our section so look for any user that is
                   1479: 	# ready for grading
1.14      albertel 1480: 	if (!$todo) {
1.33      albertel 1481: 	    &Apache::lonxml::debug("no sce");
1.98      albertel 1482: 	    $todo=&pick_from_queue_data($queue,undef,\%queue,$cdom,$cnum);
1.33      albertel 1483: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel 1484: 	}
                   1485: 	# no user to grade 
                   1486: 	if (!$todo) { last; }
1.33      albertel 1487: 	&Apache::lonxml::debug("got $todo");
1.14      albertel 1488: 	# otherwise found someone so lets try to lock them
1.32      albertel 1489: 	# unless someone else already picked them
1.95      albertel 1490: 	if (!&lock_key($queue,$todo)) {
                   1491: 	    $attempts++;
                   1492: 	    next;
                   1493: 	}
1.14      albertel 1494: 	my (undef,$endtime)=
1.30      albertel 1495: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel 1496: 				 $cdom,$cnum);
1.33      albertel 1497: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel 1498: 	# someone else already modified the queue, 
                   1499: 	# perhaps our picked user wass already fully graded between
                   1500: 	# when we picked him and when we locked his record? so lets
                   1501: 	# double check.
                   1502: 	if ($endtime != $starttime) {
                   1503: 	    my ($key,$value)=
1.30      albertel 1504: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel 1505: 				     $cdom,$cnum);
1.33      albertel 1506: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel 1507: 	    if ($key eq $todo && ref($value)) {
                   1508: 	    } else {
1.30      albertel 1509: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel 1510: 				     $cdom,$cnum);
1.33      albertel 1511: 		&Apache::lonxml::debug("del");
1.95      albertel 1512: 		$attempts++;
1.14      albertel 1513: 		next;
                   1514: 	    }
                   1515: 	}
1.33      albertel 1516: 	&Apache::lonxml::debug("last $todo");
1.14      albertel 1517: 	last;
                   1518:     }
                   1519:     return $todo;
                   1520: }
                   1521: 
1.49      albertel 1522: sub select_user {
                   1523:     my ($symb,$cid)=&Apache::lonxml::whichuser();
                   1524: 
1.59      albertel 1525:     my @chosen_sections=
                   1526: 	&Apache::loncommon::get_env_multiple('form.chosensections');
1.49      albertel 1527:     &Apache::grades::init_perm();
1.59      albertel 1528:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
1.49      albertel 1529:     &Apache::grades::reset_perm();
1.63      albertel 1530:     
                   1531:     my $result;
                   1532:     if (!(grep(/^all$/,@chosen_sections))) {
                   1533: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1534: 	    '</tt>.</p> '."\n";
                   1535:     }
                   1536:     $result.='<table border="1">';
1.49      albertel 1537: 
                   1538:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
                   1539: 	my ($uname,$udom) = split(/:/,$student);
1.59      albertel 1540: 	
1.84      albertel 1541: 	my $cnum=$env{'course.'.$cid.'.num'};
                   1542: 	my $cdom=$env{'course.'.$cid.'.domain'};
1.88      albertel 1543: 	my %status = &get_student_status($symb,$cdom,$cnum,$udom,$uname,
                   1544: 					 'Task');
1.49      albertel 1545: 	my $queue = 'none';
1.58      albertel 1546: 	my $cannot_grade;
                   1547: 	if ($status{'reviewqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1548: 	    $queue = 'reviewqueue';
1.58      albertel 1549: 	    if ($status{'reviewqueue'} eq 'in_progress') {
                   1550: 		$cannot_grade=1;
                   1551: 	    }
                   1552: 	} elsif ($status{'gradingqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1553: 	    $queue = 'gradingqueue';
1.58      albertel 1554: 	    if ($status{'gradingqueue'} eq 'in_progress') {
                   1555: 		$cannot_grade=1;
                   1556: 	    }
1.49      albertel 1557: 	}
                   1558: 	my $todo = 
                   1559: 	    &Apache::lonnet::escape(&encode_queue_key($symb,$udom,$uname));
1.58      albertel 1560: 	if ($cannot_grade) {
                   1561: 	    $result.='<tr><td>&nbsp;</td><td>'.$fullname->{$student}.
                   1562: 		'</td><td>';
                   1563: 	} else {
1.62      albertel 1564: 	    my $seclist;
                   1565: 	    foreach my $sec (@chosen_sections) {
                   1566: 		$seclist.='<input type="hidden" name="chosensections" 
                   1567:                                value="'.$sec.'" />';
                   1568: 	    }
1.102     albertel 1569: 	    my $studentdis = $student;
                   1570: 	    $studentdis =~ tr/:/@/;
1.58      albertel 1571: 	    $result.=<<RESULT;
1.49      albertel 1572: <tr>
                   1573:   <td>
1.115     albertel 1574:     <form style="display: inline" method="post">
1.49      albertel 1575:       <input type="hidden" name="gradingkey" value="$todo" />
                   1576:       <input type="hidden" name="queue" value="$queue" />
                   1577:       <input type="hidden" name="webgrade" value="no" />
1.52      albertel 1578:       <input type="hidden" name="regrade" value="yes" />
1.62      albertel 1579:       <input type="submit" name="submit" value="Regrade" />
                   1580:       $seclist
1.49      albertel 1581:     </form>
1.102     albertel 1582:   <td>$fullname->{$student} <tt>($studentdis)</tt></td>
1.49      albertel 1583:   <td>
                   1584: RESULT
1.58      albertel 1585:         }
1.49      albertel 1586:         if ($status{'status'} eq 'pass') {
                   1587: 	    $result .= '<font color="green">'.&mt('Passed').'</font>';
                   1588: 	} elsif ($status{'status'} eq 'fail') {
                   1589: 	    $result .= '<font color="red">'.&mt('Failed').'</font>';
                   1590: 	} elsif ($status{'status'} eq 'review') {
                   1591: 	    $result .= '<font color="blue">'.&mt('Under Review').'</font>';
                   1592: 	} elsif ($status{'status'} eq 'ungraded') {
                   1593: 	    $result .= &mt('Ungraded');
                   1594: 	} elsif ($status{'status'} ne '') {
                   1595: 	    $result .= '<font color="orange">'.&mt('Unknown Status').'</font>';
                   1596: 	} else {
                   1597: 	    $result.="&nbsp;";
                   1598: 	}
                   1599: 	if ($status{'version'}) {
                   1600: 	    $result .= ' '.&mt('Version').' '.$status{'version'};
                   1601: 	}
1.101     albertel 1602: 	if ($status{'grader'}) {
                   1603: 	    $result .= ' '.&mt('(Graded by [_1])',$status{'grader'}).' ';
                   1604: 	}
1.49      albertel 1605: 	$result.= '</td><td>';
                   1606: 	if ($status{'reviewqueue'} eq 'enqueued') {
                   1607: 	    $result .= &mt('Awaiting Review');
                   1608: 	} elsif ($status{'reviewqueue'} eq 'locked') {
                   1609: 	    $result .= &mt('Under Review');
1.58      albertel 1610: 	} elsif ($status{'reviewqueue'} eq 'in_progress') {
                   1611: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1612: 	} elsif ($status{'gradingqueue'} eq 'enqueued') {
                   1613: 	    $result .= &mt('Awaiting Grading');
                   1614: 	} elsif ($status{'gradingqueue'} eq 'locked') {
                   1615: 	    $result .= &mt('Being Graded');
1.58      albertel 1616: 	} elsif ($status{'gradingqueue'} eq 'in_progress') {
                   1617: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1618: 	} else {
                   1619: 	    $result.="&nbsp;";
                   1620: 	}
                   1621: 	$result.= '</td></tr>';
                   1622:     }
                   1623:     $result.='</table>';
                   1624:     return $result;
                   1625: }
                   1626: 
                   1627: sub get_student_status {
1.86      albertel 1628:     my ($symb,$cdom,$cnum,$udom,$uname,$type)=@_;
                   1629: 
                   1630:     my %status;
                   1631: 
                   1632:     if ($type eq 'Task') {
                   1633: 	my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
1.49      albertel 1634: 					  $udom,$uname);
1.89      albertel 1635: 	$status{'status'}=$record{'resource.0.status'};
                   1636: 	$status{'version'}=$record{'resource.0.version'};
                   1637: 	$status{'grader'}=$record{'resource.0.regrader'};
1.86      albertel 1638:     }
                   1639:     $status{'reviewqueue'}=
                   1640: 	&check_queue_for_key($cdom,$cnum,'reviewqueue',
                   1641: 			     &encode_queue_key($symb,$udom,$uname));
                   1642:     $status{'gradingqueue'}=
                   1643: 	&check_queue_for_key($cdom,$cnum,'gradingqueue',
                   1644: 			     &encode_queue_key($symb,$udom,$uname));
1.49      albertel 1645:     return %status;
                   1646: }
                   1647: 
1.1       albertel 1648: sub start_ClosingParagraph {
                   1649:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1650:     my $result;
                   1651:     if ($target eq 'web') {
1.13      albertel 1652:     } elsif ($target eq 'webgrade') {
                   1653: 	&Apache::lonxml::startredirection();
1.1       albertel 1654:     }
                   1655:     return $result;
                   1656: }
                   1657: 
                   1658: sub end_ClosingParagraph {
                   1659:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1660:     my $result;
                   1661:     if ($target eq 'web') {
1.13      albertel 1662:     } elsif ($target eq 'webgrade') {
                   1663: 	&Apache::lonxml::endredirection();
1.1       albertel 1664:     }
                   1665:     return $result;
                   1666: }
                   1667: 
1.19      albertel 1668: sub get_id {
                   1669:     my ($parstack,$safeeval)=@_;
                   1670:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                   1671:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                   1672:     return $id;
                   1673: }
                   1674: 
1.1       albertel 1675: my %dimension;
                   1676: sub start_Dimension {
                   1677:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1678:     undef(%dimension);
1.19      albertel 1679:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel 1680:     $Apache::bridgetask::dimension=$dim_id;
                   1681:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                   1682:     undef(@Apache::bridgetask::instance);
1.20      albertel 1683:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
                   1684: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.54      albertel 1685:     &Apache::lonxml::startredirection();
                   1686:     return &internal_location($dim_id);
1.1       albertel 1687: }
                   1688: 
1.13      albertel 1689: sub get_instance {
1.75      albertel 1690:     my ($dim)=@_;
                   1691:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                   1692:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                   1693: 	$rand_alg eq '64bit2' || $rand_alg eq '64bit3' ||
                   1694: 	$rand_alg eq '64bit4' ) {
                   1695: 	&Apache::response::pushrandomnumber();
                   1696: 	my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
                   1697: 	my $num=@order;
                   1698: 	my $version=&get_version();
                   1699: 	my $which=($version-1)%$num;
                   1700: 	return $order[$which];
                   1701:     } else {
                   1702: 	my ($version,$previous) = &get_version();
                   1703: 	my $instance = 
                   1704: 	    $Apache::lonhomework::history{"resource.$version.0.$dim.instance"};
                   1705: 	if (defined($instance)) { return $instance; }
                   1706: 
                   1707: 	&Apache::response::pushrandomnumber();
                   1708: 	my @instances = @{$dimension{'instances'}};
                   1709: 	# remove disabled instances
                   1710: 	for (my $i=0; $i < $#instances; $i++) {
                   1711: 	    if ($dimension{$instances[$i].'.disabled'}) {
                   1712: 		splice(@instances,$i,1);
                   1713: 		$i--;
                   1714: 	    }
                   1715: 	}
                   1716: 	@instances = &Math::Random::random_permutation(@instances);
                   1717: 	$instance  = $instances[($version-1)%scalar(@instances)];
                   1718: 	$Apache::lonhomework::results{"resource.$version.0.$dim.instance"} = 
                   1719: 	    $instance;
1.79      albertel 1720: 	$Apache::lonhomework::results{'INTERNAL_store'} = 1; 
1.75      albertel 1721: 	&Apache::response::poprandomnumber();
                   1722: 	return $instance;
                   1723:     }
1.13      albertel 1724: }
                   1725: 
1.18      albertel 1726: {
                   1727:     my $last_link;
1.122     albertel 1728:     sub link {
                   1729: 	my ($instance,$id) = @_;
                   1730: 	return 'LC_GRADING_criteria_'.$instance.'_'.$id;
                   1731:     }
1.18      albertel 1732:     sub end_Dimension {
                   1733: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.54      albertel 1734: 	my $result=&Apache::lonxml::endredirection();
1.25      albertel 1735: 	my $dim=&get_id($parstack,$safeeval);
1.75      albertel 1736: 	my $instance=&get_instance($dim);
1.25      albertel 1737: 	my $version=&get_version();
1.18      albertel 1738: 	if ($target eq 'web') {
1.47      albertel 1739: 	    @Apache::scripttag::parser_env = @_;
                   1740: 	    $result.=&Apache::scripttag::xmlparse($dimension{'intro'});
1.76      albertel 1741: 	    my @instances = $instance;
                   1742: 	    if (&Apache::response::showallfoils()) {
                   1743: 		@instances = @{$dimension{'instances'}};
                   1744: 	    }
                   1745: 	    foreach my $instance (@instances) {
                   1746: 		@Apache::scripttag::parser_env = @_;
                   1747: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.89      albertel 1748: 		if ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass' ||
                   1749: 		    $Apache::lonhomework::history{"resource.$version.0.status"} eq 'fail') {
1.76      albertel 1750: 
1.89      albertel 1751: 		    my $dim_status=$Apache::lonhomework::history{"resource.$version.0.$dim.status"};
1.76      albertel 1752: 		    my $mandatory='Mandatory';
                   1753: 		    if ($Apache::bridgetask::dimensionmandatory{$dim} eq 'N') {
                   1754: 			$mandatory='Optional';
                   1755: 		    }
1.116     albertel 1756: 		    my $dim_info="<div class='LC_$dim_status LC_question_grade'>\n";
1.76      albertel 1757: 		    if ($dim_status eq 'pass') {
                   1758: 			$dim_info.='<h3>Question : you passed this '.$mandatory.' question</h3>';
1.54      albertel 1759: 		    }
1.76      albertel 1760: 		    if ($dim_status eq 'fail') {
                   1761: 			$dim_info.='<h3>Question : you did not pass this '.$mandatory.' question</h3>';
1.53      albertel 1762: 		    }
1.76      albertel 1763: 		    my $man_count=0;
                   1764: 		    my $man_passed=0;
                   1765: 		    my $opt_count=0;
                   1766: 		    my $opt_passed=0;
                   1767: 		    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1768: 			if ($dimension{$instance.'.criteria.'.$id.'.mandatory'} 
                   1769: 			    eq 'N') {
                   1770: 			    $opt_count++;
1.89      albertel 1771: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"} eq 'pass') {
1.76      albertel 1772: 				$opt_passed++;
                   1773: 			    }
                   1774: 			} else {
                   1775: 			    $man_count++;
1.89      albertel 1776: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"} eq 'pass') {
1.76      albertel 1777: 				$man_passed++;
                   1778: 			    }
                   1779: 			}
1.22      albertel 1780: 		    }
1.76      albertel 1781: 		    if ($man_passed eq $man_count) { $man_passed='all'; }
                   1782: 		    my $opt_req=$dimension{$instance.'.optionalrequired'};
                   1783: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
                   1784: 		    $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>";
                   1785: 
                   1786: 		    my $internal_location=&internal_location($dim);
                   1787: 		    $result=~s/\Q$internal_location\E/$dim_info/;
                   1788: 
                   1789: 		    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.89      albertel 1790: 			my $status=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"};
                   1791: 			my $comment=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"};
1.76      albertel 1792: 			my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1793: 			if ($mandatory) {
                   1794: 			    $mandatory='Mandatory';
                   1795: 			} else {
                   1796: 			    $mandatory='Optional';
                   1797: 			}
                   1798: 			if ($status eq 'fail') {
                   1799: 			} elsif ($status eq 'pass') {
                   1800: 			} else {
                   1801: 			    &Apache::lonxml::error("Student viewing a graded bridgetask was shown a status of $status");
                   1802: 			}
                   1803: 			my $status_display=$status;
                   1804: 			$status_display=~s/^([a-z])/uc($1)/e;
                   1805: 			@Apache::scripttag::parser_env = @_;
1.116     albertel 1806: 			$result.=
                   1807: 			    '<div class="LC_'.$status.' LC_criteria"><h4>'
                   1808: 			    .$mandatory.' Criteria</h4><p>';
1.76      albertel 1809: 			@Apache::scripttag::parser_env = @_;
                   1810: 			$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
1.116     albertel 1811: 			$result.='</p><p class="LC_grade">'.$status_display.'</p>';
1.89      albertel 1812: 			if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}) {
1.116     albertel 1813: 			    $result.='<p class="LC_comment">'.&mt('Comment: [_1]',$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}).'</p>';
1.76      albertel 1814: 			}
                   1815: 			$result.='</div>';
1.22      albertel 1816: 		    }
                   1817: 		}
                   1818: 	    }
1.18      albertel 1819: 	} elsif ($target eq 'webgrade') {
1.47      albertel 1820: 	    # in case of any side effects that we need
                   1821: 	    @Apache::scripttag::parser_env = @_;
                   1822: 	    &Apache::scripttag::xmlparse($dimension{'intro'});
                   1823: 	    @Apache::scripttag::parser_env = @_;
                   1824: 	    &Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.18      albertel 1825: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.122     albertel 1826: 		my $link=&link($instance,$id);
1.89      albertel 1827: 		my $status=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"};
1.120     albertel 1828: 		$result.='<div class="LC_GRADING_criteria" id="'.$link.'">'."\n".
1.136     albertel 1829: 		    '<div class="LC_GRADING_criteriatext" id="next_'.$last_link.'">'."\n";
1.47      albertel 1830: 		@Apache::scripttag::parser_env = @_;
                   1831: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
1.111     albertel 1832: 		$result.='</div>'."\n".
1.18      albertel 1833: 		    #$dimension{$instance.'.criteria.'.$id}.
1.120     albertel 1834: 		    '<div class="LC_GRADING_grade">'."\n".
                   1835: 		    '<label class="LC_GRADING_ungraded"><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.&mt('Ungraded').'</label>'."\n".
                   1836: 		    '<label class="LC_GRADING_fail"><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label>'."\n".
                   1837: 		    '<label class="LC_GRADING_pass"><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.&mt('Pass').'</label>'."\n".
                   1838: 		    '<label class="LC_GRADING_review"><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.&mt('Review').'</label>'."\n".
1.111     albertel 1839: 		    '</div>'."\n".
1.120     albertel 1840: 		    '<label class="LC_GRADING_comment">'.&mt('Additional Comment for Student')."\n".
                   1841: 		    '<textarea class="LC_GRADING_comment_area" name="HWVAL_comment_'.$link.'">'.&HTML::Entities::encode($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}).'</textarea>'."\n".
1.111     albertel 1842: 		    '</label>'."\n".
1.120     albertel 1843: 		    '<ul class="LC_GRADING_navbuttons">'."\n".
1.111     albertel 1844: 		    '<li><a href="#'.$last_link.'">Prev</a></li>'."\n".
                   1845: 		    '<li><a href="#next_'.$link.'">Next</a></li>'."\n".
                   1846: 		    '</ul>'."\n".
                   1847:                     '</div>'."\n";
1.113     albertel 1848: 		$result.=&grading_history($version,$dim,$instance,$id);
1.18      albertel 1849: 		$last_link=$link;
                   1850: 	    }
1.22      albertel 1851: 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
1.19      albertel 1852: 	    my $optional_passed=0;
1.20      albertel 1853: 	    my $mandatory_failed=0;
                   1854: 	    my $ungraded=0;
                   1855: 	    my $review=0;
1.19      albertel 1856: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.122     albertel 1857: 		my $link=&link($instance,$id);
                   1858: 		my $status=$Apache::lonhomework::results{"resource.$version.0.$dim.$instance.$id.status"}=$env{'form.HWVAL_'.$link};
                   1859: 		$Apache::lonhomework::results{"resource.$version.0.$dim.$instance.$id.comment"}=$env{'form.HWVAL_comment_'.$link};
1.20      albertel 1860: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1861: 		if ($status eq 'pass') {
                   1862: 		    if (!$mandatory) { $optional_passed++; }
                   1863: 		} elsif ($status eq 'fail') {
                   1864: 		    if ($mandatory) { $mandatory_failed++; }
1.21      albertel 1865: 		} elsif ($status eq 'review') {
                   1866: 		    $review++;
1.20      albertel 1867: 		} elsif ($status eq 'ungraded') {
                   1868: 		    $ungraded++;
1.21      albertel 1869: 		} else {
1.47      albertel 1870: 		    $ungraded++;
1.19      albertel 1871: 		}
                   1872: 	    }
1.20      albertel 1873: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
                   1874: 		$mandatory_failed++;
                   1875: 	    }
1.21      albertel 1876: 	    &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 1877: 	    if ($review) {
1.89      albertel 1878: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1879: 		    'review';
1.20      albertel 1880: 	    } elsif ($ungraded) {
1.89      albertel 1881: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1882: 		    'ungraded';
1.20      albertel 1883: 	    } elsif ($mandatory_failed) {
1.89      albertel 1884: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1885: 		    'fail';
1.20      albertel 1886: 	    } else {
1.91      albertel 1887: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1888: 		    'pass';
1.20      albertel 1889: 	    }
1.69      albertel 1890: 	} else {
                   1891: 	    # any other targets no output
                   1892: 	    undef($result);
1.13      albertel 1893: 	}
1.18      albertel 1894: 	return $result;
1.1       albertel 1895:     }
                   1896: }
                   1897: 
1.113     albertel 1898: sub grading_history {
                   1899:     my ($version,$dim,$instance,$criteria) = @_;
                   1900:     if (!&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
                   1901: 	return '';
                   1902:     }
                   1903:     my ($result,$grader);
                   1904:     my $scope="resource.$version.0.$dim.$instance.$criteria";
                   1905:     foreach my $t (1..$Apache::lonhomework::history{'version'}) {
                   1906: 	if (exists($Apache::lonhomework::history{$t.':resource.0.regrader'})) {
                   1907: 	    my ($gname,$gdom) = 
1.138     albertel 1908: 		split(':',$Apache::lonhomework::history{$t.':resource.0.regrader'});
1.113     albertel 1909: 	    my $fullname = &Apache::loncommon::plainname($gname,$gdom);
                   1910: 	    $grader = &Apache::loncommon::aboutmewrapper($fullname,
                   1911: 							 $gname,$gdom);
                   1912: 	}
                   1913: 	my $entry;
                   1914: 	if (exists($Apache::lonhomework::history{"$t:$scope.status"})) {
                   1915: 	    $entry.="<tt>".$Apache::lonhomework::history{"$t:$scope.status"}.'</tt>';
                   1916: 	}
                   1917: 	if (exists($Apache::lonhomework::history{"$t:$scope.comment"})) {
                   1918: 	    $entry.=' comment: "'.$Apache::lonhomework::history{"$t:$scope.comment"}.'"';
                   1919: 	}
                   1920: 	if ($entry) {
                   1921: 	    $result.= "<li>$grader : $entry </li>";
                   1922: 	}
                   1923:     }
                   1924:     if ($result) {
1.120     albertel 1925: 	return '<ul class="LC_GRADING_pastgrading">'.$result.'</ul>';
1.113     albertel 1926:     }
                   1927:     return '';
                   1928: }
                   1929: 
1.1       albertel 1930: sub start_IntroParagraph {
1.87      albertel 1931:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel 1932:     my $result;
1.13      albertel 1933:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1934: 	if ($tagstack->[-2] eq 'Dimension') {
1.87      albertel 1935: 	    $dimension{'intro'}=&Apache::lonxml::get_all_text('/introparagraph',$parser,$style);
1.47      albertel 1936: 	
1.128     albertel 1937: 	} elsif ($tagstack->[-2] eq 'Task' && $target eq 'webgrade') {
1.127     albertel 1938: 	    &Apache::lonxml::startredirection();
1.1       albertel 1939: 	}
1.47      albertel 1940: 	
1.1       albertel 1941:     }
                   1942:     return $result;
                   1943: }
                   1944: 
                   1945: sub end_IntroParagraph {
1.127     albertel 1946:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.128     albertel 1947:     if ($tagstack->[-2] eq 'Task' && $target eq 'webgrade') {
1.127     albertel 1948: 	my $result = &Apache::lonxml::endredirection();
                   1949:     }
1.1       albertel 1950: }
                   1951: 
                   1952: sub start_Instance {
                   1953:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel 1954:     my $id=&get_id($parstack,$safeeval);
                   1955:     push(@{$dimension{'instances'}},$id);
                   1956:     push(@Apache::bridgetask::instance,$id);
                   1957:     push(@Apache::bridgetask::instancelist,$id);
1.20      albertel 1958:     $dimension{$id.'.optionalrequired'}=
1.19      albertel 1959: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.75      albertel 1960:     my $disabled = &Apache::lonxml::get_param('Disabled',$parstack,$safeeval);
                   1961:     if (lc($disabled) eq 'yes') {
                   1962: 	$dimension{$id.'.disabled'}='1';
                   1963:     }
1.1       albertel 1964:     return '';
                   1965: }
                   1966: 
                   1967: sub end_Instance {
                   1968: }
                   1969: 
                   1970: sub start_InstanceText {
1.87      albertel 1971:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.47      albertel 1972:     my $instance_id=$Apache::bridgetask::instance[-1];
1.87      albertel 1973:     my $text=&Apache::lonxml::get_all_text('/instancetext',$parser,$style);
1.13      albertel 1974:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1975: 	$dimension{$instance_id.'.text'}=$text;
1.1       albertel 1976:     }
                   1977:     return '';
                   1978: }
                   1979: 
                   1980: sub end_InstanceText {
                   1981:     return '';
                   1982: }
                   1983: 
                   1984: sub start_Criteria {
1.87      albertel 1985:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.9       albertel 1986:     my $instance_id=$Apache::bridgetask::instance[-1];
1.87      albertel 1987:     my $criteria=&Apache::lonxml::get_all_text('/criteria',$parser,$style);
1.21      albertel 1988:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.19      albertel 1989: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel 1990: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel 1991: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                   1992: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel 1993: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                   1994:     }
                   1995:     return '';
                   1996: }
                   1997: 
1.47      albertel 1998: sub end_Criteria {
                   1999: }
                   2000: 
1.4       albertel 2001: sub proctor_validation_screen {
                   2002:     my ($slot) = @_;
                   2003:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5       albertel 2004:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.44      albertel 2005:     my $name=&Apache::loncommon::plainname($user,$domain);
                   2006:     
1.4       albertel 2007:     my $msg;
1.11      albertel 2008:     if ($env{'form.proctorpassword'}) {
1.4       albertel 2009: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                   2010: 	    .'</font></p>';
                   2011:     }
1.47      albertel 2012:     if (!$env{'form.proctordomain'}) { $env{'form.proctordomain'}=$domain; }
1.4       albertel 2013:     my $result= (<<ENDCHECKOUT);
                   2014: <h2>Proctor Validation</h2>
                   2015:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   2016:     $msg
1.115     albertel 2017: <form name="checkout" method="post" action="$env{'request.uri'}">
1.4       albertel 2018: <input type="hidden" name="validate" value="yes" />
                   2019: <input type="hidden" name="submitted" value="yes" />
                   2020: <table>
1.44      albertel 2021:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$env{'form.proctorname'}" /></td></tr>
1.4       albertel 2022:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.46      albertel 2023:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$env{'form.proctordomain'}" /></td></tr>
1.4       albertel 2024: </table>
                   2025: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
1.44      albertel 2026: <table border="1">
                   2027:   <tr><td>
                   2028:     <table>
                   2029:       <tr><td colspan="2">Student who should be logged in is:</td></tr>
                   2030:       <tr><td>Name:</td><td>$name</td></tr>
1.45      albertel 2031:       <tr><td>Student ID:</td><td>$env{'environment.id'}</td></tr>
1.138     albertel 2032:       <tr><td>Usename</td><td>$user:$domain</td></tr>
1.44      albertel 2033:       <tr><td colspan="2"><img src="$url" /></td></tr>
                   2034:     </table>
                   2035:   </tr></td>
                   2036: </table>
1.4       albertel 2037: </form>
                   2038: ENDCHECKOUT
                   2039:     return $result;
                   2040: }
                   2041: 
1.1       albertel 2042: 1;
                   2043: __END__

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