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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.63    ! albertel    4: # $Id: bridgetask.pm,v 1.62 2005/10/01 04:06:29 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.22      albertel   63:     my ($slot_name,$slot)=@_;
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.11      albertel   71: 	    my $authhost=&Apache::lonnet::authenticate($puser,$env{'form.proctorpassword'},$pdom);
1.4       albertel   72: 	    if ($authhost ne 'no_host') {
1.23      albertel   73: 		my $version=
                     74: 		    $Apache::lonhomework::results{'resource.version'}=
1.27      albertel   75: 		    ++$Apache::lonhomework::history{'resource.version'};
1.23      albertel   76: 		$Apache::lonhomework::results{"resource.$version.checkedin"}=
1.4       albertel   77: 		    $user.'@'.$domain;
1.23      albertel   78: 		$Apache::lonhomework::results{"resource.$version.checkedin.slot"}=
1.22      albertel   79: 		    $slot_name;
1.28      albertel   80: 		foreach my $key (keys(%Apache::lonhomework::history)) {
                     81: 		    if ($key=~/^resource\.0\./) {
                     82: 			$Apache::lonhomework::results{$key}='';
                     83: 		    }
                     84: 		}
1.4       albertel   85: 		return 1;
                     86: 	    }
                     87: 	}
                     88:     }
                     89:     return 0;
                     90: }
                     91: 
1.25      albertel   92: sub get_version {
1.29      albertel   93:     my ($version,$previous);
1.25      albertel   94:     if ($env{'form.previousversion'} && 
1.36      albertel   95: 	$env{'form.previousversion'} ne 'current' &&
1.25      albertel   96: 	defined($Apache::lonhomework::history{'resource.'.$env{'form.previousversion'}.'.status'})) {
1.29      albertel   97: 	$version=$env{'form.previousversion'};
                     98: 	$previous=1;
                     99:     } else {
                    100: 	$version=$Apache::lonhomework::history{'resource.version'};
                    101: 	$previous=0;
                    102:     }
                    103:     if (wantarray) {
                    104: 	return ($version,$previous);
1.25      albertel  105:     }
1.29      albertel  106:     return $version;
1.25      albertel  107: }
                    108: 
1.8       albertel  109: sub add_previous_version_button {
1.25      albertel  110:     my ($status)=@_;
1.8       albertel  111:     my $result;
1.29      albertel  112:     if ($Apache::lonhomework::history{'resource.version'} eq '') {
1.25      albertel  113: 	return '';
                    114:     }
1.29      albertel  115:     if ($Apache::lonhomework::history{'resource.version'} < 2 &&
                    116: 	$status ne 'NEEDS_CHECKIN') {
1.25      albertel  117: 	return '';
                    118:     }
1.29      albertel  119:     my $version=&get_version();
                    120:     if ($env{'form.previousversion'} ne '' &&
                    121: 	$env{'form.previousversion'} eq $version) {
                    122: 	$result.="<h3>".&mt("Showing previous version [_1]",$version).
                    123: 	    "</h3>\n";
                    124:     }
                    125:     my @to_show;
                    126:     foreach my $test_version (1..$Apache::lonhomework::history{'resource.version'}) {
                    127: 	if (defined($Apache::lonhomework::history{'resource.'.$test_version.'.status'})) {
                    128: 	    push(@to_show,$test_version);
                    129: 	}
                    130:     }
                    131:     my $list='<option>'.
                    132: 	join("</option>\n<option>",@to_show).
                    133: 	     "</option>\n";
1.36      albertel  134:     $list.='<option value="current">'.&mt('Current').'</option>';
1.29      albertel  135:     $result.='<form name="getprevious" method="POST" action="';
                    136:     my $uri=$env{'request.uri'};
                    137:     if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    138:     $result.=$uri.'">'.
                    139: 	&mt(' Show a previously done version: [_1]','<select onchange="this.form.submit()" name="previousversion">
                    140: <option>'.&mt('Pick one').'</option>
                    141: '.$list.'
                    142: </select>')."</form>";
1.8       albertel  143:     return $result;
                    144: }
                    145: 
1.13      albertel  146: sub add_grading_button {
1.59      albertel  147:     my (undef,$cid)=&Apache::lonxml::whichuser();
                    148:     my $cnum=$env{'course.'.$cid.'.num'};
                    149:     my $cdom=$env{'course.'.$cid.'.domain'};
                    150:     my %sections;
                    151:     my $numsections=&Apache::loncommon::get_sections($cdom,$cnum,\%sections);
                    152:     my $size=5;
                    153:     if (scalar(keys(%sections)) < 3) {
                    154: 	$size=scalar(keys(%sections))+2;
                    155:     }
                    156:     my $sec_select = '<select multiple name="chosensections" size="'.$size.'">'."\n";
                    157:     $sec_select .= "<option value='all' selected='selected'>all</option>\n";
                    158:     foreach my $sec (sort {lc($a) cmp lc($b)} (keys(%sections))) {
                    159: 	$sec_select .= "<option name=\"$sec\">$sec</option>\n";
                    160:     }
                    161:     $sec_select .= "<option value='none'>none</option></select>\n";
                    162:     
1.29      albertel  163:     my $result=' <input type="submit" name="gradeasubmission" value="'.
1.13      albertel  164: 	&mt("Get a submission to grade").'" />';
                    165:     $result.='<input type="hidden" name="grade_target" value="webgrade" />';
1.40      albertel  166:     if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.34      albertel  167: 	my ($entries,$ready,$locks)=&get_queue_counts('gradingqueue');
1.59      albertel  168: 	$result.='<p>Specify a section: '.$sec_select.'</p>';
1.34      albertel  169: 	$result.='<p>'.&mt("Grading Queue has [_1] entries. [_2] of them are ready to be graded and [_3] of them are currently being graded",$entries,$ready,$locks);
                    170: 
                    171: 	$result.=' <input type="submit" name="reviewagrading" value="'.
                    172: 	    &mt("Select an entry from the grading queue").'" /> </p>'."\n";
                    173: 
                    174: 	($entries,$ready,$locks)=&get_queue_counts('reviewqueue');
                    175: 	$result.='<p>'.&mt("Review Queue has [_1] entries. [_2] of them are ready to be graded and [_3] of them are currently being graded",$entries,$ready,$locks);
1.32      albertel  176: 	$result.=' <input type="submit" name="reviewasubmission" value="'.
1.34      albertel  177: 	    &mt("Select an entry from the review queue").'" /> </p>'."\n";
1.49      albertel  178: 	$result.=' <input type="submit" name="regradeasubmission" value="'.
                    179: 	    &mt("Select a user to regrade.").'" /> </p>'."\n";
1.32      albertel  180:     }
1.13      albertel  181:     return $result;
                    182: }
                    183: 
1.22      albertel  184: sub add_request_another_attempt_button {
1.38      albertel  185:     my ($text)=@_;
                    186:     if (!$text) { $text="Request another attempt"; }
1.25      albertel  187:     my $result;
1.36      albertel  188:     my $symb=&Apache::lonnet::symbread();
1.37      albertel  189:     my ($slot_name,$slot)=&Apache::slotrequest::check_for_reservation($symb);
1.38      albertel  190:     my $action='get_reservation';
1.37      albertel  191:     if ($slot_name) {
1.38      albertel  192: 	$text="Change reservation.";
                    193: 	$action='change_reservation';
1.37      albertel  194: 	my $description=&Apache::slotrequest::get_description($slot_name,
                    195: 							      $slot);
                    196: 	$result.=(<<STUFF);
                    197: <p> Will be next available: $description </p>
                    198: STUFF
                    199:     }
1.38      albertel  200:     
                    201:     if ($env{'request.enc'}) { $symb=&Apache::lonenc::encrypted($symb); }
                    202:     $symb=&Apache::lonnet::escape($symb);
                    203:     $result.='<form method="POST" action="/adm/slotrequest">'.
                    204: 	'<input type="hidden" name="symb" value="'.$symb.'" />'.
                    205: 	'<input type="hidden" name="command" value="'.$action.'" />'.
                    206: 	'<input type="submit" name="requestattempt" value="'.
                    207: 	&mt($text).'" />'.
                    208: 	'</form>';
1.25      albertel  209:     return $result;
1.22      albertel  210: }
                    211: 
1.30      albertel  212: sub preserve_grade_info {
                    213:     my $result;
                    214:     # if we are viewing someone else preserve that info
                    215:     if (defined $env{'form.grade_symb'}) {
                    216: 	foreach my $field ('symb','courseid','domain','username') {
                    217: 	    $result .= '<input type="hidden" name="grade_'.$field.
                    218: 		'" value="'.$env{"form.grade_$field"}.'" />'."\n";
                    219: 	}
                    220:     }
                    221:     return $result;
                    222: }
                    223: 
1.53      albertel  224: sub style {
                    225:     return (<<STYLE);
                    226: <style type="text/css">
                    227: .fail, .pass, .neutral {
                    228:     position: relative;
                    229:     margin : 5px;
                    230:     margin-bottom :10px;
                    231:     padding : 4px;
                    232:     padding-left : 75px;
                    233: 	border : thin solid;
                    234:     font-weight : bolder;
                    235:     font-size: smaller;
                    236:     font-family: Arial;
                    237:     background-color : rgb(255,240,225);
                    238: }
                    239: 
                    240: .fail h4, .pass h4 {
                    241: 	position:absolute;
                    242: 	left: -4px;
                    243: 	top: -8px;
                    244: 	padding:2px;
                    245: 	margin:0;
                    246: 	background-color : rgb(255,240,225);
                    247: 	border : thin solid;
                    248: }
                    249: 
                    250: .fail {
                    251: 	color: red;
                    252: }
                    253: 
                    254: .pass {
                    255:     color : green;
                    256: }
                    257: 
                    258: .neutral {
                    259: 	color : blue;
                    260: }
                    261: 
                    262: .question {
                    263: 	border : thin solid black;
                    264: 	padding : 4px;
                    265: 	margin-bottom : 1em;
                    266: }
                    267: 
                    268: .grade {
                    269: 	font-size: x-large;
                    270: 	font-family: Arial;
                    271: 	position:absolute;
                    272: 	left: 5px;
                    273: 	top: -5px;
                    274: 	width: 70px;
                    275: }
                    276: 
                    277: div#feedback h1 {
                    278:     color : inherit;
                    279: }
                    280: </style>
                    281: STYLE
                    282: 
                    283: }
                    284: 
1.54      albertel  285: sub show_task {
                    286:     my ($status,$previous)=@_;
                    287:     if (!$previous && (
                    288: 		       ( $status eq 'CLOSED' ) ||
                    289: 		       ( $status eq 'BANNED') ||
                    290: 		       ( $status eq 'UNAVAILABLE') ||
                    291: 		       ( $status eq 'NOT_IN_A_SLOT') ||
                    292: 		       ( $status eq 'NEEDS_CHECKIN') ||
                    293: 		       ( $status eq 'WAITING_FOR_GRADE') ||
                    294: 		       ( $status eq 'INVALID_ACCESS') )) {
                    295: 	return 0;
                    296:     }
                    297:     return 1;
                    298: }
                    299: 
                    300: sub internal_location {
                    301:     my ($id)=@_;
                    302:     return '<!-- LONCAPA_INTERNAL_ADD_TASK_STATUS'.$id.' -->';
                    303: }
                    304: 
1.60      albertel  305: sub submission_time_stamp {
                    306:     my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                    307:     my $submissiontime;
                    308:     my $version=$Apache::lonhomework::history{'resource.version'};
                    309:     for (my $v=$Apache::lonhomework::history{'version'};$v>0;$v--) {
                    310: 	if (defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.bridgetask.portfiles'})) {
                    311: 	    $submissiontime=$Apache::lonhomework::history{$v.':timestamp'};
                    312: 	}
                    313:     }
                    314:     my $result;
                    315:     if ($submissiontime) {
                    316: 	my $slot_name=$Apache::lonhomework::history{'resource.'.$version.'.checkedin.slot'};
                    317: 	my %slot=&Apache::lonnet::get_slot($slot_name);
                    318: 	my $diff = $slot{'endtime'} - $submissiontime;
                    319: 	my ($color,$when)=('red','after');
                    320: 	if ($diff > 0) { ($color,$when)=('green','before'); }
                    321: 	my $info;
                    322: 	if ($diff%60) { $info=($diff%60).' seconds'; }
                    323: 	$diff=int($diff/60);
                    324: 	if ($diff%60) { $info=($diff%60).' minutes '.$info; }
                    325: 	$diff=int($diff/60);
                    326: 	if ($diff) {    $info=$diff.' hours '.$info; }
                    327: 	$result='<p><font color="'.$color.'">'.
                    328: 	    &mt('Student submitted [_1] [_2] the deadline. 
                    329:                  (Submission was at [_3], end of period was [_4].)',
                    330: 		$info,$when,scalar(localtime($submissiontime)),
                    331: 		scalar(localtime($slot{'endtime'}))).
                    332: 		'</font></p>';
                    333:     }
                    334:     return $result;
                    335: }
                    336: 
1.1       albertel  337: sub start_Task {
                    338:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    339: 
1.4       albertel  340:     my ($status,$accessmsg,$slot);
1.16      albertel  341:     if ($target ne 'webgrade') {
                    342: 	&Apache::structuretags::initialize_storage();
                    343: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
                    344:     } 
                    345: 
1.4       albertel  346:     $Apache::lonhomework::parsing_a_task=1;
1.1       albertel  347:     #should get back a <html> or the neccesary stuff to start XML/MathML
                    348:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    349: 	&Apache::structuretags::page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.16      albertel  350:     
1.8       albertel  351:     if ($target eq 'web') {
1.13      albertel  352: 	if ($Apache::lonhomework::modifygrades) {
                    353: 	    $body_tag_start.='<form name="gradesubmission" method="POST" action="';
                    354: 	    my $uri=$env{'request.uri'};
                    355: 	    if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    356: 	    $body_tag_start.=$uri.'">'.&add_grading_button()."</form>";
1.38      albertel  357: 	    my $symb=&Apache::lonnet::symbread();
1.40      albertel  358: 	    if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
                    359: 		$body_tag_start.='<form method="POST" action="/adm/slotrequest">'.
                    360: 		    '<input type="hidden" name="symb" value="'.$symb.'" />'.
                    361: 		    '<input type="hidden" name="command" value="showslots" />'.
                    362: 		    '<input type="submit" name="requestattempt" value="'.
                    363: 		    &mt('Show Slot list').'" />'.
                    364: 		    '</form>';
                    365: 	    }
1.13      albertel  366: 	}
1.8       albertel  367:     }
1.21      albertel  368:     if ($target eq 'web' || ($target eq 'grade' && !$env{'form.webgrade'}) || $target eq 'answer' ||
1.1       albertel  369: 	$target eq 'tex') {
1.29      albertel  370: 	my ($version,$previous)=&get_version();
1.14      albertel  371: 	($status,$accessmsg,my $slot_name,$slot) = 
1.4       albertel  372: 	    &Apache::lonhomework::check_task_access('0');
1.9       albertel  373: 	push(@Apache::inputtags::status,$status);
1.14      albertel  374: 	$Apache::inputtags::slot_name=$slot_name;
1.1       albertel  375: 	my $expression='$external::datestatus="'.$status.'";';
1.23      albertel  376: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$version.solved"}.'";';
1.1       albertel  377: 	&Apache::run::run($expression,$safeeval);
                    378: 	&Apache::lonxml::debug("Got $status");
1.25      albertel  379: 	$body_tag_start.=&add_previous_version_button($status);
1.54      albertel  380: 	if (!&show_task($status,$previous)) {
1.1       albertel  381: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
                    382: 	    if ( $target eq "web" ) {
1.4       albertel  383: 		$result.= $head_tag_start.'</head>'.$body_tag_start;
                    384: 		my $msg;
1.1       albertel  385: 		if ($status eq 'UNAVAILABLE') {
                    386: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3       albertel  387: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    388: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.38      albertel  389: 		    $msg.=&add_request_another_attempt_button("Sign up for time to work.");
1.4       albertel  390: 		} elsif ($status eq 'NEEDS_CHECKIN') {
                    391: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
                    392: 			'</h1>'.&proctor_validation_screen($slot);
1.22      albertel  393: 		} elsif ($status eq 'WAITING_FOR_GRADE') {
                    394: 		    $msg.='<h1>'.&mt('Your submission is in the grading queue.').'</h1>';
1.1       albertel  395: 		} elsif ($status ne 'NOT_YET_VIEWED') {
                    396: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
                    397: 		}
                    398: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
                    399: 		    $msg.='The problem '.$accessmsg;
                    400: 		}
                    401: 		$result.=$msg.'<br />';
                    402: 	    } elsif ($target eq 'tex') {
                    403: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
                    404: 		if ($status eq 'UNAVAILABLE') {
                    405: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
                    406: 		} else {
                    407: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
                    408: 		}
1.22      albertel  409: 	    } elsif ($target eq 'grade' && !$env{'form.webgrade'}) {
1.4       albertel  410: 		if ($status eq 'NEEDS_CHECKIN') {
1.41      albertel  411: 		    &proctor_check_auth($slot_name,$slot);
1.4       albertel  412: 		}
1.1       albertel  413: 	    }
                    414: 	} elsif ($target eq 'web') {
                    415: 	    my $name= &Apache::structuretags::get_resource_name($parstack,$safeeval);
                    416: 	    $result.="$head_tag_start<title>$name</title></head>
1.53      albertel  417:               $body_tag_start \n".&style();
1.36      albertel  418: 	    
1.57      albertel  419: 	    $result.=&preserve_grade_info();
                    420: 	    $result.=&internal_location();
1.36      albertel  421: 	    $result.=$form_tag_start.
                    422: 		'<input type="hidden" name="submitted" value="yes" />';
1.54      albertel  423: 	    &Apache::lonxml::startredirection();
1.1       albertel  424: 	}
1.21      albertel  425:     } elsif ( ($target eq 'grade' && $env{'form.webgrade'}) ||
                    426: 	      $target eq 'webgrade') {
1.32      albertel  427: 	my $webgrade='yes';
1.21      albertel  428: 	if ($target eq 'webgrade') {
1.32      albertel  429: 	    $result.=$head_tag_start.$body_tag_start;
1.49      albertel  430: 	    #$result.='<br />Review'.&show_queue('reviewqueue');
                    431: 	    #$result.='<br />Grade'.&show_queue('gradingqueue');
1.30      albertel  432: 	}
1.33      albertel  433: 	# FIXME Blast! still need to reorg this, need to reshow the
                    434:         #       queue being reviewed once done with the grade pass...
                    435:         #       Hrrm, vaildation pass should perhaps say 'not_locked'
                    436:         #       perhaps do a search if there is a key that is mine and if
                    437:         #       there isn't reshow the queue....
                    438: 	my ($todo,$status_code)=&get_key_todo($target);
                    439: 
                    440: 	if ($todo) {
                    441: 	    &setup_env_for_other_user($todo,$safeeval);
                    442: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
                    443: 	    $result.="\n".'<table><tr><td>Found '.
                    444: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
                    445: 	    $form_tag_start.=
                    446: 		'<input type="hidden" name="gradingkey" value="'.
                    447: 		&Apache::lonnet::escape($todo).'" />';
                    448: 	    $Apache::bridgetask::queue_key=$todo;
                    449: 	    &Apache::structuretags::initialize_storage();
                    450: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
                    451: 	    if ($target eq 'webgrade') {
1.49      albertel  452: 		#$result.='<br />After -'.&show_queue($env{'form.queue'});
1.33      albertel  453: 		$result.="\n".'<table width="100%" style="width:100%" border="1">';
                    454: 		if ($status_code eq 'selected') {
                    455: 		    $form_tag_start.=
                    456: 			'<input type="hidden" name="queuemode" value="selected" />';
                    457: 		}
                    458: 	    }
1.15      albertel  459: 	} else {
1.33      albertel  460: 	    if ($target eq 'webgrade') {
                    461: 		$result.="\n";
                    462: 		if      ($status_code eq 'stop') {
                    463: 		    $result.='<b>'.&mt("Stopped grading.").'</b>';
                    464: 		} elsif ($status_code eq 'lock_failed') {
                    465: 		    $result.='<b>'.&mt("Failed to lock the request record.").'</b>';
                    466: 		} elsif ($status_code eq 'unlock') {
                    467: 		    $result.='<b>'.&mt("Unlocked the requested record.").'</b>';
                    468: 		    $result.=&show_queue($env{'form.queue'},1);
                    469: 		} elsif ($status_code eq 'show_list') {
                    470: 		    $result.=&show_queue($env{'form.queue'},1);
1.49      albertel  471: 		} elsif ($status_code eq 'select_user') {
                    472: 		    $result.=&select_user();
1.33      albertel  473: 		} else {
                    474: 		    $result.='<b>'.&mt("No user to be graded.").'</b>';
1.32      albertel  475: 		}
1.21      albertel  476: 	    }
1.33      albertel  477: 	    $webgrade='no';
                    478: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
1.32      albertel  479: 	}
                    480: 	if ($target eq 'webgrade' && defined($env{'form.queue'})) {
1.61      albertel  481: 	    if ($webgrade eq 'yes') {
                    482: 		$result.=&submission_time_stamp();
                    483: 	    }
1.32      albertel  484: 	    $result.=$form_tag_start;
                    485: 	    $result.='<input type="hidden" name="webgrade" value="'.
                    486: 		$webgrade.'" />';
                    487: 	    $result.='<input type="hidden" name="queue" value="'.
                    488: 		$env{'form.queue'}.'" />';
1.52      albertel  489: 	    if ($env{'form.regrade'}) {
                    490: 		$result.='<input type="hidden" name="regrade" value="'.
                    491: 		    $env{'form.regrade'}.'" />';
                    492: 	    }
1.62      albertel  493: 	    if ($env{'form.chosensections'}) {
                    494: 		my @chosen_sections=
                    495: 		    &Apache::loncommon::get_env_multiple('form.chosensections');
                    496: 		foreach my $sec (@chosen_sections) {
                    497: 		    $result.='<input type="hidden" name="chosensections" 
                    498:                                value="'.$sec.'" />';
                    499: 		}
                    500: 	    }
1.15      albertel  501: 	}
1.1       albertel  502:     } else {
                    503: 	# page_start returned a starting result, delete it if we don't need it
                    504: 	$result = '';
                    505:     }
                    506:     return $result;
                    507: }
                    508: 
1.32      albertel  509: sub get_key_todo {
                    510:     my ($target)=@_;
                    511:     my $todo;
1.33      albertel  512: 
                    513:     if (defined($env{'form.reviewasubmission'})) {
1.54      albertel  514: 	&Apache::lonxml::debug("review a submission....");
1.33      albertel  515: 	$env{'form.queue'}='reviewqueue';
                    516: 	return (undef,'show_list');
                    517:     }
                    518: 
                    519:     if (defined($env{'form.reviewagrading'})) {
                    520: 	&Apache::lonxml::debug("review a grading....");
                    521: 	$env{'form.queue'}='gradingqueue';
                    522: 	return (undef,'show_list');
                    523:     }
                    524: 
1.49      albertel  525:     if (defined($env{'form.regradeasubmission'})) {
                    526: 	&Apache::lonxml::debug("regrade a grading....");
                    527: 	$env{'form.queue'}='none';
                    528: 	return (undef,'select_user');
                    529:     }
                    530: 
1.32      albertel  531:     my $queue=$env{'form.queue'};
1.33      albertel  532: 
1.32      albertel  533:     if (!defined($queue)) {
                    534: 	$env{'form.queue'}=$queue='gradingqueue';
                    535:     }
1.33      albertel  536: 
1.32      albertel  537:     my $gradingkey=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.33      albertel  538: 
1.49      albertel  539:     if ($env{'form.queue'} eq 'none') {
                    540: 	if (defined($env{'form.gradingkey'})) {
                    541: 	    if ($target eq 'webgrade') {
                    542: 		if ($env{'form.stop'}) {
                    543: 		    return (undef,'stop');
                    544: 		} elsif ($env{'form.next'}) {
1.59      albertel  545: 		    return (undef,'select_user');
1.49      albertel  546: 		}
                    547: 	    }
                    548: 	    return ($gradingkey,'selected');
                    549: 	} else {
1.59      albertel  550: 	    return (undef,'select_user');
1.49      albertel  551: 	}
                    552:     }
1.32      albertel  553:     if (defined($env{'form.queue'}) && defined($env{'form.gradingkey'})
1.33      albertel  554: 	&& !defined($env{'form.gradingaction'}) 
                    555: 	&& $env{'form.queuemode'} eq 'selected') {
                    556: 	
                    557: 	my $who=&queue_key_locked($queue,$gradingkey);
                    558: 	my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    559: 	if ($who eq $me) {
                    560: 	    &Apache::lonxml::debug("Found a key was given to me");
                    561: 	    return ($gradingkey,'selected');
                    562: 	} else {
                    563: 	    return (undef,'show_list');
                    564: 	}
                    565: 
                    566:     }
                    567: 
                    568:     if ($target eq 'webgrade' && $env{'form.queuemode'} eq 'selected') {
                    569: 	if ($env{'form.gradingaction'} eq 'resume') {
                    570: 	    delete($env{'form.gradingaction'});
                    571: 	    &Apache::lonxml::debug("Resuming a key");
1.32      albertel  572: 	    return ($gradingkey);
1.33      albertel  573: 	} elsif ($env{'form.gradingaction'} eq 'unlock') {
                    574: 	    &Apache::lonxml::debug("Unlocking a key ".
                    575: 				     &check_queue_unlock($queue,$gradingkey,1));
                    576: 	    return (undef,'unlock');
                    577: 	} elsif ($env{'form.gradingaction'} eq 'select') {
                    578: 	    &Apache::lonxml::debug("Locking a key");
                    579: 	    if (&lock_key($queue,$gradingkey)) {
                    580: 		&Apache::lonxml::debug("Success $queue");
                    581: 		return ($gradingkey);
                    582: 	    }
                    583: 	    &Apache::lonxml::debug("Failed $queue");
                    584: 	    return (undef,'lock_failed');
1.32      albertel  585: 	}
                    586:     }
1.33      albertel  587: 
                    588:     if ($env{'form.queuemode'} ne 'selected') {
                    589: 	# don't get something new from the queue if they hit the stop button
                    590:     	if (!($env{'form.stop'} && $target eq 'webgrade') 
                    591: 	    && !$env{'form.gradingaction'}) {
                    592: 	    &Apache::lonxml::debug("Getting anew $queue");
                    593: 	    return (&get_from_queue($queue));
                    594: 	} else {
                    595: 	    return (undef,'stop');
                    596: 	}
1.32      albertel  597:     }
1.33      albertel  598:     return (undef,undef)
1.32      albertel  599: }
                    600: 
1.1       albertel  601: sub end_Task {
                    602:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    603:     my $result='';
                    604:     my $status=$Apache::inputtags::status['-1'];
1.29      albertel  605:     my ($version,$previous)=&get_version();
1.1       albertel  606:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.15      albertel  607: 	$target eq 'tex') {
1.1       albertel  608: 	if (
1.11      albertel  609: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.1       albertel  610: 	    ($target eq 'answer') || ($target eq 'tex')
                    611: 	   ) {
                    612: 	    if ($target eq 'web') {
1.54      albertel  613: 		if (&show_task($status,$previous)) {
                    614: 		    $result.=&Apache::lonxml::endredirection();
                    615: 		}
1.29      albertel  616: 		if ($status eq 'CAN_ANSWER' && !$previous) {
1.15      albertel  617: 		    $result.="\n".'<table border="1">'.
1.28      albertel  618: 			&Apache::inputtags::file_selector("$version.0",
                    619: 							  "bridgetask","*",
1.46      albertel  620: 							  'portfolioonly',
                    621: 							  '
                    622: <h2>'.&mt('Submit Portfolio Files for Grading').'</h2>
                    623: <p>'.&mt('Indicate the files from your portfolio to be evaluated in grading this task.').'</p>').
1.9       albertel  624: 							  "</table>";
                    625: 		    $result.=&Apache::inputtags::gradestatus('0');
                    626: 		}
1.56      albertel  627: 		if (&show_task($status,$previous) &&
                    628: 		    $Apache::lonhomework::history{"resource.$version.status"} =~ /^(pass|fail)$/) {
1.54      albertel  629: 		    my $bt_status=$Apache::lonhomework::history{"resource.$version.status"};
                    630: 		    my $title=&Apache::lonnet::gettitle();
                    631: 		    my %slot=&Apache::lonnet::get_slot($Apache::inputtags::slot_name);
1.55      albertel  632: 		    my $start_time=
                    633: 			&Apache::lonlocal::locallocaltime($slot{'starttime'});
1.54      albertel  634: 
                    635: 		    my $status;
                    636: 		    $status.="\n<div class='$bt_status'>\n";
                    637: 		    
                    638: 		    if ($bt_status eq 'pass')  {
                    639: 			$status.='<h2>You passed the '.$title.' given on '.
1.55      albertel  640: 			    $start_time.'.</h2>';
1.54      albertel  641: 		    }
                    642: 		    if ($bt_status eq 'fail')  {
                    643: 			$status.='<h2>You did not pass the '.$title.' given on '.
1.55      albertel  644: 			    $start_time.'.</h2>';
1.54      albertel  645: 			if (!$previous) {
                    646: 			    $status.=&add_request_another_attempt_button();
                    647: 			}
                    648: 		    }
                    649: 		    my $man_count=0;
                    650: 		    my $opt_count=0;
                    651: 		    my $opt_passed=0;
                    652: 		    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    653: 			if ($Apache::bridgetask::dimensionmandatory{$dim_id}
                    654: 			    eq 'N') {
                    655: 			    $opt_count++;
                    656: 			    if ($Apache::lonhomework::history{"resource.$version.$dim_id.status"} eq 'pass') {
                    657: 				$opt_passed++;
                    658: 			    }
                    659: 			} else {
                    660: 			    $man_count++;
                    661: 			}
                    662: 		    }
                    663: 		    my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
                    664: 							 $parstack,$safeeval);
                    665: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
                    666: 		    $status.="\n<p>".&mt('You needed to pass all of the [_1]  mandatory components and [_2] of the [_3] optional components on the bridge task.',$man_count,$opt_req,$opt_count)."</p></div>\n";
                    667: 
                    668: 		    my $internal_location=&internal_location();
                    669: 		    $result=~s/\Q$internal_location\E/$status/;
                    670: 		}
1.13      albertel  671: 	    } 
                    672: 	    if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  673: 		$result.=&Apache::lonxml::xmlend().'</html>';
                    674: 	    }
                    675: 	}
1.29      albertel  676: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel  677: 	    my $award='SUBMITTED';
1.28      albertel  678: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel  679: 						    'portfiles',\$award);
1.14      albertel  680: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel  681: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                    682: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    683: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                    684: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                    685: 
                    686: 		$Apache::lonhomework::results{"resource.0.award"}=
                    687: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                    688: 		    $award;
1.51      albertel  689: 		$Apache::lonhomework::results{"resource.0.submission"}=
                    690: 		    $Apache::lonhomework::results{"resource.$version.0.submission"}='';
1.10      albertel  691: 	    }
1.4       albertel  692: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    693: 	    &Apache::structuretags::finalize_storage();
1.14      albertel  694: 	    if ($award eq 'SUBMITTED') {
1.31      albertel  695: 		&add_to_queue('gradingqueue',$Apache::inputtags::slot_name);
1.14      albertel  696: 	    }
1.1       albertel  697: 	}
1.32      albertel  698: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes') {
1.20      albertel  699: 	    my $optional_required=
                    700: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                    701: 					   $safeeval);
                    702: 	    my $optional_passed=0;
                    703: 	    my $mandatory_failed=0;
                    704: 	    my $ungraded=0;
                    705: 	    my $review=0;   
1.21      albertel  706: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.20      albertel  707: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    708: 		my $status=
1.25      albertel  709: 		    $Apache::lonhomework::results{"resource.$version.$dim_id.status"};
1.20      albertel  710: 		my $mandatory=
                    711: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
                    712: 		if ($status eq 'pass') {
                    713: 		    if (!$mandatory) { $optional_passed++; }
                    714: 		} elsif ($status eq 'fail') {
                    715: 		    if ($mandatory) { $mandatory_failed++; }
                    716: 		} elsif ($status eq 'ungraded') {
                    717: 		    $ungraded++;
                    718: 		} elsif ($status eq 'review') {
                    719: 		    $review++;
1.49      albertel  720: 		} else {
                    721: 		    $ungraded++;
                    722: 		}
1.20      albertel  723: 	    }
                    724: 	    if ($optional_passed < $optional_required) {
                    725: 		$mandatory_failed++;
                    726: 	    }
1.21      albertel  727: 	    &Apache::lonxml::debug("all dim ".join(':',@Apache::bridgetask::dimensionlist)."results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
                    728: 	    $Apache::lonhomework::results{'resource.grader'}=
1.20      albertel  729: 		$env{'user.name'}.'@'.$env{'user.domain'};
                    730: 	    if ($review) {
1.25      albertel  731: 		$Apache::lonhomework::results{"resource.$version.status"}='review';
1.33      albertel  732: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    733: 		    &check_queue_unlock($env{'form.queue'});
                    734: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                    735: 		} else {
1.49      albertel  736: 		    &move_between_queues($env{'form.queue'},'reviewqueue');
1.33      albertel  737: 		}
1.20      albertel  738: 	    } elsif ($ungraded) {
1.25      albertel  739: 		$Apache::lonhomework::results{"resource.$version.status"}='ungraded';
1.49      albertel  740: 		if ($env{'form.queue'} eq 'reviewqueue' ||
                    741: 		    $env{'form.queue'} eq 'none' ) {
1.33      albertel  742: 		    &Apache::lonxml::debug("moving back.");
1.49      albertel  743: 		    &move_between_queues($env{'form.queue'},'gradingqueue');
1.33      albertel  744: 		} else {
                    745: 		    &check_queue_unlock($env{'form.queue'});
                    746: 		}
1.20      albertel  747: 	    } elsif ($mandatory_failed) {
1.25      albertel  748: 		$Apache::lonhomework::results{"resource.$version.status"}='fail';
                    749: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                    750: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                    751: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.39      albertel  752: 		&remove_from_queue($env{'form.queue'}); 
                    753: 
                    754: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  755: 		
                    756: 		if ($env{'form.regrade'} ne 'yes') {
                    757: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    758: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    759: 		    &Apache::grades::version_portfiles(
                    760: 						       \%Apache::lonhomework::results,
                    761: 						       ["$version.0.bridgetask"],$courseid,
                    762: 						       $symb,$udom,$uname,
                    763: 						       ["$version.0.bridgetask"]);
                    764: 		}
1.20      albertel  765: 	    } else {
1.25      albertel  766: 		$Apache::lonhomework::results{"resource.$version.status"}='pass';
                    767: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                    768: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                    769: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.32      albertel  770: 		&remove_from_queue($env{'form.queue'});
1.39      albertel  771: 
                    772: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  773: 		if ($env{'form.regrade'} ne 'yes') {
                    774: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    775: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    776: 		    &Apache::grades::version_portfiles(
                    777: 						       \%Apache::lonhomework::results,
                    778: 						       ["$version.0.bridgetask"],$courseid,
                    779: 						       $symb,$udom,$uname,
                    780: 						       ["$version.0.bridgetask"]);
                    781: 		}
1.20      albertel  782: 	    }
1.26      albertel  783: 	    $Apache::lonhomework::results{"resource.status"}=
                    784: 		$Apache::lonhomework::results{"resource.$version.status"};
1.28      albertel  785: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel  786: 		$Apache::lonhomework::results{"resource.0.award"}=
1.50      albertel  787: 		    $Apache::lonhomework::results{"resource.$version.0.award"};
1.26      albertel  788: 		$Apache::lonhomework::results{"resource.0.awarded"}=
1.50      albertel  789: 		    $Apache::lonhomework::results{"resource.$version.0.awarded"};
1.26      albertel  790: 		$Apache::lonhomework::results{"resource.0.solved"}=
1.50      albertel  791: 		    $Apache::lonhomework::results{"resource.$version.0.solved"};
1.25      albertel  792: 	    }
1.21      albertel  793: 	    &Apache::structuretags::finalize_storage();
1.20      albertel  794: 	}
1.15      albertel  795:     } elsif ($target eq 'webgrade') {
1.18      albertel  796: 	$result.="</table>\n<hr />";
1.20      albertel  797: 	#$result.='<input type="submit" name="next" value="'.
                    798: 	#    &mt('Save &amp; Next').'" /> ';
                    799: 	#$result.='<input type="submit" name="end" value="'.
                    800: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                    801: 	#$result.='<input type="submit" name="throwaway" value="'.
                    802: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                    803: 	#$result.='<input type="submit" name="save" value="'.
                    804: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.15      albertel  805: 	$result.='</form>'.&Apache::loncommon::endbodytag().'</html>';
1.1       albertel  806:     } elsif ($target eq 'meta') {
1.2       albertel  807: 	$result.='<parameter part="0" package="Task"></parameter>'."\n";
1.1       albertel  808: 	#$result.=&Apache::response::meta_part_order();
                    809: 	#$result.=&Apache::response::meta_response_order();
                    810:     }
1.4       albertel  811:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel  812:     return $result;
                    813: }
                    814: 
1.31      albertel  815: sub move_between_queues {
                    816:     my ($src_queue,$dest_queue)=@_;
1.49      albertel  817:     my $cur_data;
                    818:     if ($src_queue ne 'none') {
                    819: 	$cur_data=&get_queue_data($src_queue);
                    820: 	if (!$cur_data) { return 'not_exist'; }
                    821:     } else {
                    822: 	$cur_data = ['none'];
                    823:     }
1.31      albertel  824:     my $result=&add_to_queue($dest_queue,$cur_data->[0]);
                    825:     if ($result ne 'ok') {
                    826: 	return $result;
                    827:     }
                    828:     &check_queue_unlock($src_queue);
                    829:     return &remove_from_queue($src_queue);
1.21      albertel  830: }
                    831: 
                    832: sub check_queue_unlock {
1.32      albertel  833:     my ($queue,$key,$allow_not_me)=@_;
1.49      albertel  834:     if ($queue eq 'none') { return 'ok'; }
1.30      albertel  835:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.32      albertel  836:     if (!defined($key)) {
                    837: 	$key="$symb\0queue\0$uname\@$udom";
                    838:     }
1.30      albertel  839:     my $cnum=$env{'course.'.$cid.'.num'};
                    840:     my $cdom=$env{'course.'.$cid.'.domain'};
                    841:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    842:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                    843:     if  ($who eq $me) {
1.32      albertel  844: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                    845:     } elsif ($allow_not_me) {
1.33      albertel  846: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel  847: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel  848:     }
1.32      albertel  849:     return 'not_owner';
1.21      albertel  850: }
                    851: 
                    852: sub remove_from_queue {
1.30      albertel  853:     my ($queue)=@_;
1.49      albertel  854:     if ($queue eq 'none') { return 'ok'; }
1.27      albertel  855:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    856:     my $cnum=$env{'course.'.$cid.'.num'};
                    857:     my $cdom=$env{'course.'.$cid.'.domain'};
                    858:     my $key="$symb\0queue\0$uname\@$udom";
                    859:     my @keys=($key,"$key\0locked");
1.31      albertel  860:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel  861: }
                    862: 
1.16      albertel  863: sub setup_env_for_other_user {
                    864:     my ($queue_key,$safeeval)=@_;
                    865:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel  866:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel  867:     $env{'form.grade_symb'}=$symb;
                    868:     $env{'form.grade_domain'}=$udom;
                    869:     $env{'form.grade_username'}=$uname;
                    870:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                    871:     &Apache::lonxml::initialize_rndseed($safeeval);
                    872: }
                    873: 
1.31      albertel  874: sub get_queue_data {
                    875:     my ($queue)=@_;
                    876:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    877:     my $cnum=$env{'course.'.$cid.'.num'};
                    878:     my $cdom=$env{'course.'.$cid.'.domain'};
                    879:     my $todo="$symb\0queue\0$uname\@$udom";
                    880:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                    881:     if ($key eq $todo && ref($value)) {
                    882: 	return $value;
                    883:     }
                    884:     return undef;
                    885: }
                    886: 
1.49      albertel  887: sub check_queue_for_key {
                    888:     my ($cid,$queue,$todo)=@_;
                    889:     my $cnum=$env{'course.'.$cid.'.num'};
                    890:     my $cdom=$env{'course.'.$cid.'.domain'};
                    891:     my %results=
                    892: 	&Apache::lonnet::get($queue,[$todo,"$todo\0locked"],$cdom,$cnum);
                    893:     
                    894:     if (exists($results{$todo}) && ref($results{$todo})) {
                    895: 	if (defined($results{"$todo\0locked"})) {
                    896: 	    return 'locked';
                    897: 	}
1.58      albertel  898: 	my $slot=$results{$todo}->[0];
                    899: 	my %slot_data=&Apache::lonnet::get_slot($slot);
                    900: 	if ($slot_data{'endtime'} > time) { 
                    901: 	    return 'in_progress';
                    902: 	}
1.49      albertel  903: 	return 'enqueued';
                    904:     }
                    905:     return undef;
                    906: }
                    907: 
1.14      albertel  908: sub add_to_queue {
1.31      albertel  909:     my ($queue,$slot_name)=@_;
1.49      albertel  910:     if ($queue eq 'none') { return 'ok'; }
1.14      albertel  911:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    912:     my $cnum=$env{'course.'.$cid.'.num'};
                    913:     my $cdom=$env{'course.'.$cid.'.domain'};
                    914:     my %data;
1.31      albertel  915:     $data{"$symb\0queue\0$uname\@$udom"}=[$slot_name];
                    916:     return &Apache::lonnet::put($queue,\%data,$cdom,$cnum);
1.14      albertel  917: }
                    918: 
                    919: sub show_queue {
1.32      albertel  920:     my ($queue,$with_selects)=@_;
1.14      albertel  921:     my $result;
                    922:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    923:     my $cnum=$env{'course.'.$cid.'.num'};
                    924:     my $cdom=$env{'course.'.$cid.'.domain'};
1.59      albertel  925: 
                    926:     my @chosen_sections=
                    927: 	&Apache::loncommon::get_env_multiple('form.chosensections');
                    928:     &Apache::grades::init_perm();
                    929:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
                    930:     &Apache::grades::reset_perm();
1.63    ! albertel  931:     if (!(grep(/^all$/,@chosen_sections))) {
        !           932: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
        !           933: 	    '</tt>.</p> '."\n";
        !           934:     }
1.59      albertel  935: 
1.16      albertel  936:     my $regexp="^$symb\0";
1.30      albertel  937:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel  938:     my ($tmp)=%queue;
                    939:     if ($tmp=~/^error: 2 /) {
1.33      albertel  940: 	return "\n<h3>Current Queue - $queue</h3><table border='1'><tr><td>Empty</td></tr></table>";
1.31      albertel  941:     }
1.33      albertel  942:     $result.="\n<h3>Current Queue - $queue </h3><table border='1'><tr>";
1.32      albertel  943:     if ($with_selects) { $result.="<th></th>"; }
                    944:     $result.="<th>resource</th><th>user</th><th>type</th><th>data</th></tr>";
1.14      albertel  945:     foreach my $key (sort(keys(%queue))) {
1.59      albertel  946: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                    947: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
1.32      albertel  948: 	if ($key=~/locked$/ && !$with_selects) {
1.16      albertel  949: 	    my $title=&Apache::lonnet::gettitle($symb);
1.27      albertel  950: 	    $result.="<tr><td>$title</td><td>$uname</td>";
1.16      albertel  951: 	    $result.='<td>lock</td><td>'.$queue{$key}.'</td></tr>';
1.32      albertel  952: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.16      albertel  953: 	    my ($symb,undef) = split("\0",$key);
                    954: 	    my $title=&Apache::lonnet::gettitle($symb);
1.27      albertel  955: 	    $result.="<tr><td>$title</td><td></td>";
1.16      albertel  956: 	    $result.='<td>last queue modification time</td><td>'.
                    957: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel  958: 	} elsif ($key!~/(timestamp|locked)$/) {
1.16      albertel  959: 	    my $title=&Apache::lonnet::gettitle($symb);
1.32      albertel  960: 	    $result.="<tr>";
1.35      albertel  961: 	    my $slot=$queue{$key}->[0];
                    962: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
1.32      albertel  963: 	    if ($with_selects) {
                    964: 		my $ekey=&Apache::lonnet::escape($key);
                    965: 		my ($action,$description)=('select',&mt('Select'));
                    966: 		if (exists($queue{"$key\0locked"})) {
                    967: 		    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    968: 		    if ($me eq $queue{"$key\0locked"}) {
                    969: 			($action,$description)=('resume',&mt('Resume'));
                    970: 		    } else {
                    971: 			($action,$description)=('unlock',&mt('Unlock'));
                    972: 		    }
                    973: 		}
1.62      albertel  974: 		my $seclist;
                    975: 		foreach my $sec (@chosen_sections) {
                    976: 		    $seclist.='<input type="hidden" name="chosensections" 
                    977:                                value="'.$sec.'" />';
                    978: 		}
1.35      albertel  979: 		if (time > $slot_data{'endtime'}) {
                    980: 		    $result.=(<<FORM);
1.32      albertel  981: <td>
                    982: <form method="POST">
                    983:  <input type="hidden" name="gradingkey" value="$ekey" />
                    984:  <input type="hidden" name="queue" value="$queue" />
                    985:  <input type="hidden" name="gradingaction" value="$action" />
                    986:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel  987:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel  988:  <input type="submit" name="submit" value="$description" />
1.62      albertel  989:  $seclist
1.32      albertel  990: </form>
                    991: </td>
                    992: FORM
1.35      albertel  993: 
                    994:                 } else {
                    995: 		    $result.='<td>'.&mt("In Progress").'</td>'
                    996: 		}
1.32      albertel  997: 	    }
                    998: 	    $result.="<td>$title</td><td>$uname</td>";
1.27      albertel  999: 	    $result.='<td>queue entry</td><td>Slot: '.$slot.' End time: '.
1.20      albertel 1000: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
                   1001: 		"</td></tr>";
1.16      albertel 1002: 	}
1.14      albertel 1003:     }
1.15      albertel 1004:     $result.="</table><hr />\n";
1.14      albertel 1005:     return $result;
                   1006: }
                   1007: 
1.34      albertel 1008: sub get_queue_counts {
                   1009:     my ($queue)=@_;
                   1010:     my $result;
                   1011:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1012:     my $cnum=$env{'course.'.$cid.'.num'};
                   1013:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1014:     my $regexp="^$symb\0";
                   1015:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1016:     my ($tmp)=%queue;
                   1017:     if ($tmp=~/^error: 2 /) {
                   1018: 	return (0,0,0);
                   1019:     }
                   1020:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
                   1021:     foreach my $key (sort(keys(%queue))) {
                   1022: 	if ($key=~/locked$/) {
                   1023: 	    $locks++;
                   1024: 	} elsif ($key=~/timestamp$/) {
                   1025: 	    #ignore
                   1026: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1027: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1028: 	    $entries++;
                   1029: 	    my $slot=$queue{$key}->[0];
                   1030: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1031: 	    if (time > $slot_data{'endtime'}) {
                   1032: 		$ready_to_grade++;
                   1033: 	    }
                   1034: 	}
                   1035:     }
                   1036:     return ($entries,$ready_to_grade,$locks);
                   1037: }
                   1038: 
1.49      albertel 1039: sub encode_queue_key {
                   1040:     my ($symb,$udom,$uname)=@_;
                   1041:     return "$symb\0queue\0$uname\@$udom";
                   1042: }
                   1043: 
1.14      albertel 1044: sub decode_queue_key {
                   1045:     my ($key)=@_;
                   1046:     my ($symb,undef,$user) = split("\0",$key);
                   1047:     my ($uname,$udom) = split('@',$user);
                   1048:     return ($symb,$uname,$udom);
                   1049: }
                   1050: 
                   1051: sub queue_key_locked {
1.30      albertel 1052:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel 1053:     if (!defined($cdom) || !defined($cnum)) {
                   1054: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1055: 	$cnum=$env{'course.'.$cid.'.num'};
                   1056: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1057:     }
1.14      albertel 1058:     my ($key_locked,$value)=
1.30      albertel 1059: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel 1060:     if ($key_locked eq "$key\0locked") {
                   1061: 	return $value;
                   1062:     }
                   1063:     return undef;
                   1064: }
                   1065: 
                   1066: sub pick_from_queue_data {
1.30      albertel 1067:     my ($queue,$check_section,$queuedata,$cdom,$cnum)=@_;
                   1068:     foreach my $key (keys(%$queuedata)) {
1.14      albertel 1069: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                   1070: 	if ($check_section) {
                   1071: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel 1072: 	    if ($section eq $check_section) {
1.33      albertel 1073: 		&Apache::lonxml::debug("my sec");
1.15      albertel 1074: 		next;
                   1075: 	    }
1.14      albertel 1076: 	}
1.30      albertel 1077: 	my $slot=$queuedata->{$key}[0];
1.14      albertel 1078: 	my %slot_data=&Apache::lonnet::get_slot($slot);
1.15      albertel 1079: 	if ($slot_data{'endtime'} > time) { 
1.33      albertel 1080: 	    &Apache::lonxml::debug("not time");
1.15      albertel 1081: 	    next;
                   1082: 	}
1.30      albertel 1083: 	if (&queue_key_locked($queue,$key,$cdom,$cnum)) {
1.33      albertel 1084: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel 1085: 	    next;
                   1086: 	}
1.14      albertel 1087: 	return $key;
                   1088:     }
                   1089:     return undef;
                   1090: }
                   1091: 
1.15      albertel 1092: sub find_mid_grade {
1.30      albertel 1093:     my ($queue,$symb,$cdom,$cnum)=@_;
1.16      albertel 1094:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.15      albertel 1095:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1096:     if ($todo) {
1.30      albertel 1097: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel 1098: 	if ($who eq $me) { return $todo; }
                   1099:     }
                   1100:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel 1101:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel 1102:     foreach my $key (keys(%locks)) {
                   1103: 	my $who=$locks{$key};
                   1104: 	if ($who eq $me) {
                   1105: 	    $todo=$key;
                   1106: 	    $todo=~s/\0locked$//;
                   1107: 	    return $todo;
                   1108: 	}
                   1109:     }
                   1110:     return undef;
                   1111: }
                   1112: 
1.32      albertel 1113: sub lock_key {
                   1114:     my ($queue,$todo)=@_;
                   1115:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1116:     my (undef,$cid)=&Apache::lonxml::whichuser();
                   1117:     my $cnum=$env{'course.'.$cid.'.num'};
                   1118:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1119:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                   1120: 					$cdom,$cnum);
1.33      albertel 1121:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel 1122:     if ($success eq 'ok') {
                   1123: 	return 1;
                   1124:     }
                   1125:     return 0;
                   1126: }
                   1127: 
1.14      albertel 1128: sub get_from_queue {
1.30      albertel 1129:     my ($queue)=@_;
1.14      albertel 1130:     my $result;
                   1131:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1132:     my $cnum=$env{'course.'.$cid.'.num'};
                   1133:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel 1134:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel 1135:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel 1136:     if ($todo) { return $todo; }
1.14      albertel 1137:     while (1) {
                   1138: 	my $starttime=time;
1.30      albertel 1139: 	&Apache::lonnet::put($queue,{"$symb\0timestamp"=>$starttime},
1.14      albertel 1140: 			     $cdom,$cnum);
1.33      albertel 1141: 	&Apache::lonxml::debug("$starttime");
1.14      albertel 1142: 	my $regexp="^$symb\0queue\0";
1.30      albertel 1143: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel 1144: 	#make a pass looking for a user _not_ in my section
1.14      albertel 1145: 	if ($env{'request.course.sec'}) {
1.33      albertel 1146: 	    &Apache::lonxml::debug("sce");
1.30      albertel 1147: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                   1148: 					\%queue,$cdom,$cnum);
1.33      albertel 1149: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel 1150: 	}
1.33      albertel 1151: 	# no one _not_ in our section so look for any user that is
                   1152: 	# ready for grading
1.14      albertel 1153: 	if (!$todo) {
1.33      albertel 1154: 	    &Apache::lonxml::debug("no sce");
1.31      albertel 1155: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                   1156: 					\%queue,$cdom,$cnum);
1.33      albertel 1157: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel 1158: 	}
                   1159: 	# no user to grade 
                   1160: 	if (!$todo) { last; }
1.33      albertel 1161: 	&Apache::lonxml::debug("got $todo");
1.14      albertel 1162: 	# otherwise found someone so lets try to lock them
1.32      albertel 1163: 	# unless someone else already picked them
                   1164: 	if (!&lock_key($queue,$todo)) { next; }
1.14      albertel 1165: 	my (undef,$endtime)=
1.30      albertel 1166: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel 1167: 				 $cdom,$cnum);
1.33      albertel 1168: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel 1169: 	# someone else already modified the queue, 
                   1170: 	# perhaps our picked user wass already fully graded between
                   1171: 	# when we picked him and when we locked his record? so lets
                   1172: 	# double check.
                   1173: 	if ($endtime != $starttime) {
                   1174: 	    my ($key,$value)=
1.30      albertel 1175: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel 1176: 				     $cdom,$cnum);
1.33      albertel 1177: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel 1178: 	    if ($key eq $todo && ref($value)) {
                   1179: 	    } else {
1.30      albertel 1180: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel 1181: 				     $cdom,$cnum);
1.33      albertel 1182: 		&Apache::lonxml::debug("del");
1.14      albertel 1183: 		next;
                   1184: 	    }
                   1185: 	}
1.33      albertel 1186: 	&Apache::lonxml::debug("last $todo");
1.14      albertel 1187: 	last;
                   1188:     }
                   1189:     return $todo;
                   1190: }
                   1191: 
1.49      albertel 1192: sub select_user {
                   1193:     my ($symb,$cid)=&Apache::lonxml::whichuser();
                   1194: 
1.59      albertel 1195:     my @chosen_sections=
                   1196: 	&Apache::loncommon::get_env_multiple('form.chosensections');
1.49      albertel 1197:     &Apache::grades::init_perm();
1.59      albertel 1198:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
1.49      albertel 1199:     &Apache::grades::reset_perm();
1.63    ! albertel 1200:     
        !          1201:     my $result;
        !          1202:     if (!(grep(/^all$/,@chosen_sections))) {
        !          1203: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
        !          1204: 	    '</tt>.</p> '."\n";
        !          1205:     }
        !          1206:     $result.='<table border="1">';
1.49      albertel 1207: 
                   1208:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
                   1209: 	my ($uname,$udom) = split(/:/,$student);
1.59      albertel 1210: 	
1.49      albertel 1211: 	my %status = &get_student_status($symb,$cid,$udom,$uname);
                   1212: 	my $queue = 'none';
1.58      albertel 1213: 	my $cannot_grade;
                   1214: 	if ($status{'reviewqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1215: 	    $queue = 'reviewqueue';
1.58      albertel 1216: 	    if ($status{'reviewqueue'} eq 'in_progress') {
                   1217: 		$cannot_grade=1;
                   1218: 	    }
                   1219: 	} elsif ($status{'gradingqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1220: 	    $queue = 'gradingqueue';
1.58      albertel 1221: 	    if ($status{'gradingqueue'} eq 'in_progress') {
                   1222: 		$cannot_grade=1;
                   1223: 	    }
1.49      albertel 1224: 	}
                   1225: 	my $todo = 
                   1226: 	    &Apache::lonnet::escape(&encode_queue_key($symb,$udom,$uname));
1.58      albertel 1227: 	if ($cannot_grade) {
                   1228: 	    $result.='<tr><td>&nbsp;</td><td>'.$fullname->{$student}.
                   1229: 		'</td><td>';
                   1230: 	} else {
1.62      albertel 1231: 	    my $seclist;
                   1232: 	    foreach my $sec (@chosen_sections) {
                   1233: 		$seclist.='<input type="hidden" name="chosensections" 
                   1234:                                value="'.$sec.'" />';
                   1235: 	    }
1.58      albertel 1236: 	    $result.=<<RESULT;
1.49      albertel 1237: <tr>
                   1238:   <td>
                   1239:     <form method="POST">
                   1240:       <input type="hidden" name="gradingkey" value="$todo" />
                   1241:       <input type="hidden" name="queue" value="$queue" />
                   1242:       <input type="hidden" name="webgrade" value="no" />
1.52      albertel 1243:       <input type="hidden" name="regrade" value="yes" />
1.62      albertel 1244:       <input type="submit" name="submit" value="Regrade" />
                   1245:       $seclist
1.49      albertel 1246:     </form>
                   1247:   <td>$fullname->{$student}</td>
                   1248:   <td>
                   1249: RESULT
1.58      albertel 1250:         }
1.49      albertel 1251:         if ($status{'status'} eq 'pass') {
                   1252: 	    $result .= '<font color="green">'.&mt('Passed').'</font>';
                   1253: 	} elsif ($status{'status'} eq 'fail') {
                   1254: 	    $result .= '<font color="red">'.&mt('Failed').'</font>';
                   1255: 	} elsif ($status{'status'} eq 'review') {
                   1256: 	    $result .= '<font color="blue">'.&mt('Under Review').'</font>';
                   1257: 	} elsif ($status{'status'} eq 'ungraded') {
                   1258: 	    $result .= &mt('Ungraded');
                   1259: 	} elsif ($status{'status'} ne '') {
                   1260: 	    $result .= '<font color="orange">'.&mt('Unknown Status').'</font>';
                   1261: 	} else {
                   1262: 	    $result.="&nbsp;";
                   1263: 	}
                   1264: 	if ($status{'version'}) {
                   1265: 	    $result .= ' '.&mt('Version').' '.$status{'version'};
                   1266: 	}
                   1267: 	$result.= '</td><td>';
                   1268: 	if ($status{'reviewqueue'} eq 'enqueued') {
                   1269: 	    $result .= &mt('Awaiting Review');
                   1270: 	} elsif ($status{'reviewqueue'} eq 'locked') {
                   1271: 	    $result .= &mt('Under Review');
1.58      albertel 1272: 	} elsif ($status{'reviewqueue'} eq 'in_progress') {
                   1273: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1274: 	} elsif ($status{'gradingqueue'} eq 'enqueued') {
                   1275: 	    $result .= &mt('Awaiting Grading');
                   1276: 	} elsif ($status{'gradingqueue'} eq 'locked') {
                   1277: 	    $result .= &mt('Being Graded');
1.58      albertel 1278: 	} elsif ($status{'gradingqueue'} eq 'in_progress') {
                   1279: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1280: 	} else {
                   1281: 	    $result.="&nbsp;";
                   1282: 	}
                   1283: 	$result.= '</td></tr>';
                   1284:     }
                   1285:     $result.='</table>';
                   1286:     return $result;
                   1287: }
                   1288: 
                   1289: sub get_student_status {
                   1290:     my ($symb,$cid,$udom,$uname)=@_;
                   1291:     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
                   1292: 					  $udom,$uname);
                   1293:     my %status;
                   1294:     $status{'status'}=$record{'resource.status'};
                   1295:     $status{'version'}=$record{'resource.version'};
                   1296:     $status{'grader'}=$record{'resource.grader'};
                   1297:     $status{'reviewqueue'}=&check_queue_for_key($cid,'reviewqueue',
                   1298: 				       &encode_queue_key($symb,$udom,$uname));
                   1299:     $status{'gradingqueue'}=&check_queue_for_key($cid,'gradingqueue',
                   1300: 				       &encode_queue_key($symb,$udom,$uname));
                   1301:     return %status;
                   1302: }
                   1303: 
1.1       albertel 1304: sub start_ClosingParagraph {
                   1305:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1306:     my $result;
                   1307:     if ($target eq 'web') {
1.13      albertel 1308:     } elsif ($target eq 'webgrade') {
                   1309: 	&Apache::lonxml::startredirection();
1.1       albertel 1310:     }
                   1311:     return $result;
                   1312: }
                   1313: 
                   1314: sub end_ClosingParagraph {
                   1315:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1316:     my $result;
                   1317:     if ($target eq 'web') {
1.13      albertel 1318:     } elsif ($target eq 'webgrade') {
                   1319: 	&Apache::lonxml::endredirection();
1.1       albertel 1320:     }
                   1321:     return $result;
                   1322: }
                   1323: 
1.19      albertel 1324: sub get_id {
                   1325:     my ($parstack,$safeeval)=@_;
                   1326:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                   1327:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                   1328:     return $id;
                   1329: }
                   1330: 
1.1       albertel 1331: my %dimension;
                   1332: sub start_Dimension {
                   1333:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1334:     undef(%dimension);
1.19      albertel 1335:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel 1336:     $Apache::bridgetask::dimension=$dim_id;
                   1337:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                   1338:     undef(@Apache::bridgetask::instance);
1.20      albertel 1339:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
                   1340: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.54      albertel 1341:     &Apache::lonxml::startredirection();
                   1342:     return &internal_location($dim_id);
1.1       albertel 1343: }
                   1344: 
1.13      albertel 1345: sub get_instance {
                   1346:     &Apache::response::pushrandomnumber();
                   1347:     my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
1.29      albertel 1348:     my $num=@order;
                   1349:     my $version=&get_version();
                   1350:     my $which=($version-1)%$num;
                   1351:     return $order[$which];
1.13      albertel 1352: }
                   1353: 
1.18      albertel 1354: {
                   1355:     my $last_link;
                   1356:     sub end_Dimension {
                   1357: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.54      albertel 1358: 	my $result=&Apache::lonxml::endredirection();
1.25      albertel 1359: 	my $dim=&get_id($parstack,$safeeval);
1.19      albertel 1360: 	my $instance=&get_instance();
1.25      albertel 1361: 	my $version=&get_version();
1.18      albertel 1362: 	if ($target eq 'web') {
1.47      albertel 1363: 	    @Apache::scripttag::parser_env = @_;
                   1364: 	    $result.=&Apache::scripttag::xmlparse($dimension{'intro'});
                   1365: 	    @Apache::scripttag::parser_env = @_;
                   1366: 	    $result.=&Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.26      albertel 1367: 	    if ($Apache::lonhomework::history{"resource.$version.status"} eq 'pass' ||
                   1368: 		$Apache::lonhomework::history{"resource.$version.status"} eq 'fail') {
1.54      albertel 1369: 
                   1370: 		my $dim_status=$Apache::lonhomework::history{"resource.$version.$dim.status"};
                   1371: 		my $mandatory='Mandatory';
                   1372: 		if ($Apache::bridgetask::dimensionmandatory{$dim} eq 'N') {
                   1373: 		    $mandatory='Optional';
                   1374: 		}
                   1375: 		my $dim_info="<div class='$dim_status'>\n";
                   1376: 		if ($dim_status eq 'pass') {
                   1377: 		    $dim_info.='<h3>Question : you passed this '.$mandatory.' question</h3>';
                   1378: 		}
                   1379: 		if ($dim_status eq 'fail') {
                   1380: 		    $dim_info.='<h3>Question : you did not pass this '.$mandatory.' question</h3>';
                   1381: 		}
                   1382: 		my $man_count=0;
                   1383: 		my $opt_count=0;
                   1384: 		my $opt_passed=0;
                   1385: 		foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1386: 		    if ($dimension{$instance.'.criteria.'.$id.'.mandatory'} 
                   1387: 			eq 'N') {
                   1388: 			$opt_count++;
                   1389: 			if ($Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"} eq 'pass') {
                   1390: 			    $opt_passed++;
                   1391: 			}
                   1392: 		    } else {
                   1393: 			$man_count++;
                   1394: 		    }
                   1395: 		}
                   1396: 		my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
                   1397: 						       $parstack,$safeeval);
                   1398: 		if ($opt_req !~ /\S/) { $opt_req='0'; }
                   1399: 		$dim_info.="\n<p>".&mt('You passed all of the mandatory components and [_1] of the [_2] optional components, of which you were required to pass [_3].',$opt_passed,$opt_count,$opt_req)."</p>\n</div>";
                   1400: 
                   1401: 		my $internal_location=&internal_location($dim);
                   1402: 		$result=~s/\Q$internal_location\E/$dim_info/;
                   1403: 
1.22      albertel 1404: 		foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.25      albertel 1405: 		    my $status=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"};
                   1406: 		    my $comment=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"};
1.53      albertel 1407: 		    my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1408: 		    if ($mandatory) {
                   1409: 			$mandatory='Mandatory';
                   1410: 		    } else {
                   1411: 			$mandatory='Optional';
                   1412: 		    }
1.22      albertel 1413: 		    if ($status eq 'fail') {
                   1414: 		    } elsif ($status eq 'pass') {
                   1415: 		    } else {
1.49      albertel 1416: 			&Apache::lonxml::error("Student viewing a graded bridgetask was shown a status of $status");
1.22      albertel 1417: 		    }
1.53      albertel 1418: 		    my $status_display=$status;
                   1419: 		    $status_display=~s/^([a-z])/uc($1)/e;
1.54      albertel 1420: 		    @Apache::scripttag::parser_env = @_;
1.53      albertel 1421: 		    $result.='<div class="'.$status.'"><h4>'.$mandatory.
1.54      albertel 1422: 			' Criteria</h4><p>';
                   1423: 		    @Apache::scripttag::parser_env = @_;
                   1424: 		    $result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
                   1425: 		    $result.='</p><p class="grade">'.$status_display.'</p>';
1.25      albertel 1426: 		    if ($Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}) {
1.53      albertel 1427: 			$result.='<p class="comment">'.$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}.'</p>';
1.22      albertel 1428: 		    }
1.53      albertel 1429: 		    $result.='</div>';
1.22      albertel 1430: 		}
                   1431: 	    }
1.18      albertel 1432: 	} elsif ($target eq 'webgrade') {
1.47      albertel 1433: 	    # in case of any side effects that we need
                   1434: 	    @Apache::scripttag::parser_env = @_;
                   1435: 	    &Apache::scripttag::xmlparse($dimension{'intro'});
                   1436: 	    @Apache::scripttag::parser_env = @_;
                   1437: 	    &Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.18      albertel 1438: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1439: 		my $link='criteria_'.$instance.'_'.$id;
1.25      albertel 1440: 		my $status=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"};
1.19      albertel 1441: 		$result.='<tr><td width="100%" valign="top">'.
1.18      albertel 1442: 		    '<a name="'.$link.'" />'.
                   1443: 		    '<a name="next_'.$last_link.'" />'.
1.47      albertel 1444: 		    '<br /><textarea enabled="false" style="width:100%" rows="8" width="25" wrap="hard">';
                   1445: 		@Apache::scripttag::parser_env = @_;
                   1446: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
                   1447: 		$result.='</textarea>'.
1.18      albertel 1448: 		    #$dimension{$instance.'.criteria.'.$id}.
                   1449: 		    '</td>'.
1.19      albertel 1450: 		    '<td><nobr>Additional Comment for Student</nobr> <br />'.
1.25      albertel 1451: 		    '<textarea style="width:100%" rows="8" width="25" wrap="hard" name="HWVAL_comment_'.$link.'">'.&HTML::Entities::encode($Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}).'</textarea>'.
1.18      albertel 1452: 		    '</td>'.
                   1453: 		    '<td>'.
1.47      albertel 1454: 		    '<nobr><label><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.&mt('Ungraded').'</label></nobr><br />'.
1.30      albertel 1455: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.&mt('Pass').'</label><br />'.
                   1456: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label><br />'.
                   1457: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.&mt('Review').'</label><br />'.
1.18      albertel 1458: 		    '</td>'.
                   1459: 		    '<td>'.
                   1460: 		    '<a href="#'.$last_link.'">Prev</a><br />'.
1.20      albertel 1461: 		    '<a href="#next_'.$link.'">Next</a><br /><br /><br />'.
                   1462: 		    '<input type="submit" name="next" value="'.
                   1463: 		    &mt('Done').'" /> '.
1.30      albertel 1464: 		    '<input type="submit" name="stop" value="'.
                   1465: 		    &mt('Stop').'" /> '.
1.18      albertel 1466: 		    '</td></tr>';
1.22      albertel 1467: 		$result.='<tr><td colspan="4">';
                   1468: 		my (undef,undef,$udom,$uname) = &Apache::lonxml::whichuser();
1.52      albertel 1469: 		my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
1.43      albertel 1470: 		foreach my $partial_file (split(',',$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"})) {
1.22      albertel 1471: 		    my $file=$file_url.$partial_file;
1.52      albertel 1472: 		    $file=~s|/+|/|g;
1.22      albertel 1473: 		    &Apache::lonnet::allowuploaded('/adm/bridgetask',$file);
1.48      albertel 1474: 		    $result.='<nobr><a href="'.$file.'" target="lonGRDs"><img src="'.
1.22      albertel 1475: 			&Apache::loncommon::icon($file).'" border=0"> '.$file.
1.48      albertel 1476: 			'</a></nobr> ';
1.22      albertel 1477: 		}
                   1478: 		$result.='</td></tr>';
1.18      albertel 1479: 		$last_link=$link;
                   1480: 	    }
1.22      albertel 1481: 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
1.19      albertel 1482: 	    my $optional_passed=0;
1.20      albertel 1483: 	    my $mandatory_failed=0;
                   1484: 	    my $ungraded=0;
                   1485: 	    my $review=0;
1.19      albertel 1486: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.25      albertel 1487: 		my $status=$Apache::lonhomework::results{"resource.$version.$dim.$instance.$id.status"}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
                   1488: 		$Apache::lonhomework::results{"resource.$version.$dim.$instance.$id.comment"}=$env{'form.HWVAL_comment_criteria_'.$instance.'_'.$id};
1.20      albertel 1489: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1490: 		if ($status eq 'pass') {
                   1491: 		    if (!$mandatory) { $optional_passed++; }
                   1492: 		} elsif ($status eq 'fail') {
                   1493: 		    if ($mandatory) { $mandatory_failed++; }
1.21      albertel 1494: 		} elsif ($status eq 'review') {
                   1495: 		    $review++;
1.20      albertel 1496: 		} elsif ($status eq 'ungraded') {
                   1497: 		    $ungraded++;
1.21      albertel 1498: 		} else {
1.47      albertel 1499: 		    $ungraded++;
1.19      albertel 1500: 		}
                   1501: 	    }
1.20      albertel 1502: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
                   1503: 		$mandatory_failed++;
                   1504: 	    }
1.21      albertel 1505: 	    &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 1506: 	    if ($review) {
1.25      albertel 1507: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1508: 		    'review';
1.20      albertel 1509: 	    } elsif ($ungraded) {
1.25      albertel 1510: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1511: 		    'ungraded';
1.20      albertel 1512: 	    } elsif ($mandatory_failed) {
1.25      albertel 1513: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1514: 		    'fail';
1.20      albertel 1515: 	    } else {
1.25      albertel 1516: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1517: 		    'pass';
1.20      albertel 1518: 	    }
1.13      albertel 1519: 	}
1.18      albertel 1520: 	return $result;
1.1       albertel 1521:     }
                   1522: }
                   1523: 
                   1524: sub start_IntroParagraph {
                   1525:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1526:     my $result;
1.13      albertel 1527:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1528: 	if ($tagstack->[-2] eq 'Dimension') {
                   1529: 	    $dimension{'intro'}=&Apache::lonxml::get_all_text('/introparagraph',$parser);
                   1530: 	
                   1531: 	} elsif ($target eq 'webgrade') {
                   1532: 	    &Apache::lonxml::get_all_text('/introparagraph',$parser);
1.1       albertel 1533: 	}
1.47      albertel 1534: 	
1.1       albertel 1535:     }
                   1536:     return $result;
                   1537: }
                   1538: 
                   1539: sub end_IntroParagraph {
                   1540: }
                   1541: 
                   1542: sub start_Instance {
                   1543:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel 1544:     my $id=&get_id($parstack,$safeeval);
                   1545:     push(@{$dimension{'instances'}},$id);
                   1546:     push(@Apache::bridgetask::instance,$id);
                   1547:     push(@Apache::bridgetask::instancelist,$id);
1.20      albertel 1548:     $dimension{$id.'.optionalrequired'}=
1.19      albertel 1549: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.1       albertel 1550:     return '';
                   1551: }
                   1552: 
                   1553: sub end_Instance {
                   1554: }
                   1555: 
                   1556: sub start_InstanceText {
                   1557:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.47      albertel 1558:     my $instance_id=$Apache::bridgetask::instance[-1];
                   1559:     my $text=&Apache::lonxml::get_all_text('/instancetext',$parser);
1.13      albertel 1560:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1561: 	$dimension{$instance_id.'.text'}=$text;
1.1       albertel 1562:     }
                   1563:     return '';
                   1564: }
                   1565: 
                   1566: sub end_InstanceText {
                   1567:     return '';
                   1568: }
                   1569: 
                   1570: sub start_Criteria {
                   1571:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel 1572:     my $instance_id=$Apache::bridgetask::instance[-1];
1.47      albertel 1573:     my $criteria=&Apache::lonxml::get_all_text('/criteria',$parser);
1.21      albertel 1574:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.19      albertel 1575: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel 1576: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel 1577: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                   1578: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel 1579: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                   1580:     }
                   1581:     return '';
                   1582: }
                   1583: 
1.47      albertel 1584: sub end_Criteria {
                   1585: }
                   1586: 
1.4       albertel 1587: sub proctor_validation_screen {
                   1588:     my ($slot) = @_;
                   1589:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5       albertel 1590:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.44      albertel 1591:     my $name=&Apache::loncommon::plainname($user,$domain);
                   1592:     
1.4       albertel 1593:     my $msg;
1.11      albertel 1594:     if ($env{'form.proctorpassword'}) {
1.4       albertel 1595: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                   1596: 	    .'</font></p>';
                   1597:     }
1.47      albertel 1598:     if (!$env{'form.proctordomain'}) { $env{'form.proctordomain'}=$domain; }
1.4       albertel 1599:     my $result= (<<ENDCHECKOUT);
                   1600: <h2>Proctor Validation</h2>
                   1601:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   1602:     $msg
1.11      albertel 1603: <form name="checkout" method="POST" action="$env{'request.uri'}">
1.4       albertel 1604: <input type="hidden" name="validate" value="yes" />
                   1605: <input type="hidden" name="submitted" value="yes" />
                   1606: <table>
1.44      albertel 1607:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$env{'form.proctorname'}" /></td></tr>
1.4       albertel 1608:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.46      albertel 1609:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$env{'form.proctordomain'}" /></td></tr>
1.4       albertel 1610: </table>
                   1611: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
1.44      albertel 1612: <table border="1">
                   1613:   <tr><td>
                   1614:     <table>
                   1615:       <tr><td colspan="2">Student who should be logged in is:</td></tr>
                   1616:       <tr><td>Name:</td><td>$name</td></tr>
1.45      albertel 1617:       <tr><td>Student ID:</td><td>$env{'environment.id'}</td></tr>
1.44      albertel 1618:       <tr><td>Usename</td><td>$user\@$domain</td></tr>
                   1619:       <tr><td colspan="2"><img src="$url" /></td></tr>
                   1620:     </table>
                   1621:   </tr></td>
                   1622: </table>
1.4       albertel 1623: </form>
                   1624: ENDCHECKOUT
                   1625:     return $result;
                   1626: }
                   1627: 
1.1       albertel 1628: 1;
                   1629: __END__

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