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

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

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