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

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

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