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

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

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