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

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

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