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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.61    ! albertel    4: # $Id: bridgetask.pm,v 1.60 2005/10/01 03:55:06 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.15      albertel  493: 	}
1.1       albertel  494:     } else {
                    495: 	# page_start returned a starting result, delete it if we don't need it
                    496: 	$result = '';
                    497:     }
                    498:     return $result;
                    499: }
                    500: 
1.32      albertel  501: sub get_key_todo {
                    502:     my ($target)=@_;
                    503:     my $todo;
1.33      albertel  504: 
                    505:     if (defined($env{'form.reviewasubmission'})) {
1.54      albertel  506: 	&Apache::lonxml::debug("review a submission....");
1.33      albertel  507: 	$env{'form.queue'}='reviewqueue';
                    508: 	return (undef,'show_list');
                    509:     }
                    510: 
                    511:     if (defined($env{'form.reviewagrading'})) {
                    512: 	&Apache::lonxml::debug("review a grading....");
                    513: 	$env{'form.queue'}='gradingqueue';
                    514: 	return (undef,'show_list');
                    515:     }
                    516: 
1.49      albertel  517:     if (defined($env{'form.regradeasubmission'})) {
                    518: 	&Apache::lonxml::debug("regrade a grading....");
                    519: 	$env{'form.queue'}='none';
                    520: 	return (undef,'select_user');
                    521:     }
                    522: 
1.32      albertel  523:     my $queue=$env{'form.queue'};
1.33      albertel  524: 
1.32      albertel  525:     if (!defined($queue)) {
                    526: 	$env{'form.queue'}=$queue='gradingqueue';
                    527:     }
1.33      albertel  528: 
1.32      albertel  529:     my $gradingkey=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.33      albertel  530: 
1.49      albertel  531:     if ($env{'form.queue'} eq 'none') {
                    532: 	if (defined($env{'form.gradingkey'})) {
                    533: 	    if ($target eq 'webgrade') {
                    534: 		if ($env{'form.stop'}) {
                    535: 		    return (undef,'stop');
                    536: 		} elsif ($env{'form.next'}) {
1.59      albertel  537: 		    return (undef,'select_user');
1.49      albertel  538: 		}
                    539: 	    }
                    540: 	    return ($gradingkey,'selected');
                    541: 	} else {
1.59      albertel  542: 	    return (undef,'select_user');
1.49      albertel  543: 	}
                    544:     }
1.32      albertel  545:     if (defined($env{'form.queue'}) && defined($env{'form.gradingkey'})
1.33      albertel  546: 	&& !defined($env{'form.gradingaction'}) 
                    547: 	&& $env{'form.queuemode'} eq 'selected') {
                    548: 	
                    549: 	my $who=&queue_key_locked($queue,$gradingkey);
                    550: 	my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    551: 	if ($who eq $me) {
                    552: 	    &Apache::lonxml::debug("Found a key was given to me");
                    553: 	    return ($gradingkey,'selected');
                    554: 	} else {
                    555: 	    return (undef,'show_list');
                    556: 	}
                    557: 
                    558:     }
                    559: 
                    560:     if ($target eq 'webgrade' && $env{'form.queuemode'} eq 'selected') {
                    561: 	if ($env{'form.gradingaction'} eq 'resume') {
                    562: 	    delete($env{'form.gradingaction'});
                    563: 	    &Apache::lonxml::debug("Resuming a key");
1.32      albertel  564: 	    return ($gradingkey);
1.33      albertel  565: 	} elsif ($env{'form.gradingaction'} eq 'unlock') {
                    566: 	    &Apache::lonxml::debug("Unlocking a key ".
                    567: 				     &check_queue_unlock($queue,$gradingkey,1));
                    568: 	    return (undef,'unlock');
                    569: 	} elsif ($env{'form.gradingaction'} eq 'select') {
                    570: 	    &Apache::lonxml::debug("Locking a key");
                    571: 	    if (&lock_key($queue,$gradingkey)) {
                    572: 		&Apache::lonxml::debug("Success $queue");
                    573: 		return ($gradingkey);
                    574: 	    }
                    575: 	    &Apache::lonxml::debug("Failed $queue");
                    576: 	    return (undef,'lock_failed');
1.32      albertel  577: 	}
                    578:     }
1.33      albertel  579: 
                    580:     if ($env{'form.queuemode'} ne 'selected') {
                    581: 	# don't get something new from the queue if they hit the stop button
                    582:     	if (!($env{'form.stop'} && $target eq 'webgrade') 
                    583: 	    && !$env{'form.gradingaction'}) {
                    584: 	    &Apache::lonxml::debug("Getting anew $queue");
                    585: 	    return (&get_from_queue($queue));
                    586: 	} else {
                    587: 	    return (undef,'stop');
                    588: 	}
1.32      albertel  589:     }
1.33      albertel  590:     return (undef,undef)
1.32      albertel  591: }
                    592: 
1.1       albertel  593: sub end_Task {
                    594:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    595:     my $result='';
                    596:     my $status=$Apache::inputtags::status['-1'];
1.29      albertel  597:     my ($version,$previous)=&get_version();
1.1       albertel  598:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.15      albertel  599: 	$target eq 'tex') {
1.1       albertel  600: 	if (
1.11      albertel  601: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.1       albertel  602: 	    ($target eq 'answer') || ($target eq 'tex')
                    603: 	   ) {
                    604: 	    if ($target eq 'web') {
1.54      albertel  605: 		if (&show_task($status,$previous)) {
                    606: 		    $result.=&Apache::lonxml::endredirection();
                    607: 		}
1.29      albertel  608: 		if ($status eq 'CAN_ANSWER' && !$previous) {
1.15      albertel  609: 		    $result.="\n".'<table border="1">'.
1.28      albertel  610: 			&Apache::inputtags::file_selector("$version.0",
                    611: 							  "bridgetask","*",
1.46      albertel  612: 							  'portfolioonly',
                    613: 							  '
                    614: <h2>'.&mt('Submit Portfolio Files for Grading').'</h2>
                    615: <p>'.&mt('Indicate the files from your portfolio to be evaluated in grading this task.').'</p>').
1.9       albertel  616: 							  "</table>";
                    617: 		    $result.=&Apache::inputtags::gradestatus('0');
                    618: 		}
1.56      albertel  619: 		if (&show_task($status,$previous) &&
                    620: 		    $Apache::lonhomework::history{"resource.$version.status"} =~ /^(pass|fail)$/) {
1.54      albertel  621: 		    my $bt_status=$Apache::lonhomework::history{"resource.$version.status"};
                    622: 		    my $title=&Apache::lonnet::gettitle();
                    623: 		    my %slot=&Apache::lonnet::get_slot($Apache::inputtags::slot_name);
1.55      albertel  624: 		    my $start_time=
                    625: 			&Apache::lonlocal::locallocaltime($slot{'starttime'});
1.54      albertel  626: 
                    627: 		    my $status;
                    628: 		    $status.="\n<div class='$bt_status'>\n";
                    629: 		    
                    630: 		    if ($bt_status eq 'pass')  {
                    631: 			$status.='<h2>You passed the '.$title.' given on '.
1.55      albertel  632: 			    $start_time.'.</h2>';
1.54      albertel  633: 		    }
                    634: 		    if ($bt_status eq 'fail')  {
                    635: 			$status.='<h2>You did not pass the '.$title.' given on '.
1.55      albertel  636: 			    $start_time.'.</h2>';
1.54      albertel  637: 			if (!$previous) {
                    638: 			    $status.=&add_request_another_attempt_button();
                    639: 			}
                    640: 		    }
                    641: 		    my $man_count=0;
                    642: 		    my $opt_count=0;
                    643: 		    my $opt_passed=0;
                    644: 		    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    645: 			if ($Apache::bridgetask::dimensionmandatory{$dim_id}
                    646: 			    eq 'N') {
                    647: 			    $opt_count++;
                    648: 			    if ($Apache::lonhomework::history{"resource.$version.$dim_id.status"} eq 'pass') {
                    649: 				$opt_passed++;
                    650: 			    }
                    651: 			} else {
                    652: 			    $man_count++;
                    653: 			}
                    654: 		    }
                    655: 		    my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
                    656: 							 $parstack,$safeeval);
                    657: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
                    658: 		    $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";
                    659: 
                    660: 		    my $internal_location=&internal_location();
                    661: 		    $result=~s/\Q$internal_location\E/$status/;
                    662: 		}
1.13      albertel  663: 	    } 
                    664: 	    if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  665: 		$result.=&Apache::lonxml::xmlend().'</html>';
                    666: 	    }
                    667: 	}
1.29      albertel  668: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel  669: 	    my $award='SUBMITTED';
1.28      albertel  670: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel  671: 						    'portfiles',\$award);
1.14      albertel  672: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel  673: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                    674: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    675: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                    676: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                    677: 
                    678: 		$Apache::lonhomework::results{"resource.0.award"}=
                    679: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                    680: 		    $award;
1.51      albertel  681: 		$Apache::lonhomework::results{"resource.0.submission"}=
                    682: 		    $Apache::lonhomework::results{"resource.$version.0.submission"}='';
1.10      albertel  683: 	    }
1.4       albertel  684: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    685: 	    &Apache::structuretags::finalize_storage();
1.14      albertel  686: 	    if ($award eq 'SUBMITTED') {
1.31      albertel  687: 		&add_to_queue('gradingqueue',$Apache::inputtags::slot_name);
1.14      albertel  688: 	    }
1.1       albertel  689: 	}
1.32      albertel  690: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes') {
1.20      albertel  691: 	    my $optional_required=
                    692: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                    693: 					   $safeeval);
                    694: 	    my $optional_passed=0;
                    695: 	    my $mandatory_failed=0;
                    696: 	    my $ungraded=0;
                    697: 	    my $review=0;   
1.21      albertel  698: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.20      albertel  699: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    700: 		my $status=
1.25      albertel  701: 		    $Apache::lonhomework::results{"resource.$version.$dim_id.status"};
1.20      albertel  702: 		my $mandatory=
                    703: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
                    704: 		if ($status eq 'pass') {
                    705: 		    if (!$mandatory) { $optional_passed++; }
                    706: 		} elsif ($status eq 'fail') {
                    707: 		    if ($mandatory) { $mandatory_failed++; }
                    708: 		} elsif ($status eq 'ungraded') {
                    709: 		    $ungraded++;
                    710: 		} elsif ($status eq 'review') {
                    711: 		    $review++;
1.49      albertel  712: 		} else {
                    713: 		    $ungraded++;
                    714: 		}
1.20      albertel  715: 	    }
                    716: 	    if ($optional_passed < $optional_required) {
                    717: 		$mandatory_failed++;
                    718: 	    }
1.21      albertel  719: 	    &Apache::lonxml::debug("all dim ".join(':',@Apache::bridgetask::dimensionlist)."results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
                    720: 	    $Apache::lonhomework::results{'resource.grader'}=
1.20      albertel  721: 		$env{'user.name'}.'@'.$env{'user.domain'};
                    722: 	    if ($review) {
1.25      albertel  723: 		$Apache::lonhomework::results{"resource.$version.status"}='review';
1.33      albertel  724: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    725: 		    &check_queue_unlock($env{'form.queue'});
                    726: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                    727: 		} else {
1.49      albertel  728: 		    &move_between_queues($env{'form.queue'},'reviewqueue');
1.33      albertel  729: 		}
1.20      albertel  730: 	    } elsif ($ungraded) {
1.25      albertel  731: 		$Apache::lonhomework::results{"resource.$version.status"}='ungraded';
1.49      albertel  732: 		if ($env{'form.queue'} eq 'reviewqueue' ||
                    733: 		    $env{'form.queue'} eq 'none' ) {
1.33      albertel  734: 		    &Apache::lonxml::debug("moving back.");
1.49      albertel  735: 		    &move_between_queues($env{'form.queue'},'gradingqueue');
1.33      albertel  736: 		} else {
                    737: 		    &check_queue_unlock($env{'form.queue'});
                    738: 		}
1.20      albertel  739: 	    } elsif ($mandatory_failed) {
1.25      albertel  740: 		$Apache::lonhomework::results{"resource.$version.status"}='fail';
                    741: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                    742: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                    743: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.39      albertel  744: 		&remove_from_queue($env{'form.queue'}); 
                    745: 
                    746: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  747: 		
                    748: 		if ($env{'form.regrade'} ne 'yes') {
                    749: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    750: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    751: 		    &Apache::grades::version_portfiles(
                    752: 						       \%Apache::lonhomework::results,
                    753: 						       ["$version.0.bridgetask"],$courseid,
                    754: 						       $symb,$udom,$uname,
                    755: 						       ["$version.0.bridgetask"]);
                    756: 		}
1.20      albertel  757: 	    } else {
1.25      albertel  758: 		$Apache::lonhomework::results{"resource.$version.status"}='pass';
                    759: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                    760: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                    761: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.32      albertel  762: 		&remove_from_queue($env{'form.queue'});
1.39      albertel  763: 
                    764: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  765: 		if ($env{'form.regrade'} ne 'yes') {
                    766: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    767: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    768: 		    &Apache::grades::version_portfiles(
                    769: 						       \%Apache::lonhomework::results,
                    770: 						       ["$version.0.bridgetask"],$courseid,
                    771: 						       $symb,$udom,$uname,
                    772: 						       ["$version.0.bridgetask"]);
                    773: 		}
1.20      albertel  774: 	    }
1.26      albertel  775: 	    $Apache::lonhomework::results{"resource.status"}=
                    776: 		$Apache::lonhomework::results{"resource.$version.status"};
1.28      albertel  777: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel  778: 		$Apache::lonhomework::results{"resource.0.award"}=
1.50      albertel  779: 		    $Apache::lonhomework::results{"resource.$version.0.award"};
1.26      albertel  780: 		$Apache::lonhomework::results{"resource.0.awarded"}=
1.50      albertel  781: 		    $Apache::lonhomework::results{"resource.$version.0.awarded"};
1.26      albertel  782: 		$Apache::lonhomework::results{"resource.0.solved"}=
1.50      albertel  783: 		    $Apache::lonhomework::results{"resource.$version.0.solved"};
1.25      albertel  784: 	    }
1.21      albertel  785: 	    &Apache::structuretags::finalize_storage();
1.20      albertel  786: 	}
1.15      albertel  787:     } elsif ($target eq 'webgrade') {
1.18      albertel  788: 	$result.="</table>\n<hr />";
1.20      albertel  789: 	#$result.='<input type="submit" name="next" value="'.
                    790: 	#    &mt('Save &amp; Next').'" /> ';
                    791: 	#$result.='<input type="submit" name="end" value="'.
                    792: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                    793: 	#$result.='<input type="submit" name="throwaway" value="'.
                    794: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                    795: 	#$result.='<input type="submit" name="save" value="'.
                    796: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.15      albertel  797: 	$result.='</form>'.&Apache::loncommon::endbodytag().'</html>';
1.1       albertel  798:     } elsif ($target eq 'meta') {
1.2       albertel  799: 	$result.='<parameter part="0" package="Task"></parameter>'."\n";
1.1       albertel  800: 	#$result.=&Apache::response::meta_part_order();
                    801: 	#$result.=&Apache::response::meta_response_order();
                    802:     }
1.4       albertel  803:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel  804:     return $result;
                    805: }
                    806: 
1.31      albertel  807: sub move_between_queues {
                    808:     my ($src_queue,$dest_queue)=@_;
1.49      albertel  809:     my $cur_data;
                    810:     if ($src_queue ne 'none') {
                    811: 	$cur_data=&get_queue_data($src_queue);
                    812: 	if (!$cur_data) { return 'not_exist'; }
                    813:     } else {
                    814: 	$cur_data = ['none'];
                    815:     }
1.31      albertel  816:     my $result=&add_to_queue($dest_queue,$cur_data->[0]);
                    817:     if ($result ne 'ok') {
                    818: 	return $result;
                    819:     }
                    820:     &check_queue_unlock($src_queue);
                    821:     return &remove_from_queue($src_queue);
1.21      albertel  822: }
                    823: 
                    824: sub check_queue_unlock {
1.32      albertel  825:     my ($queue,$key,$allow_not_me)=@_;
1.49      albertel  826:     if ($queue eq 'none') { return 'ok'; }
1.30      albertel  827:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.32      albertel  828:     if (!defined($key)) {
                    829: 	$key="$symb\0queue\0$uname\@$udom";
                    830:     }
1.30      albertel  831:     my $cnum=$env{'course.'.$cid.'.num'};
                    832:     my $cdom=$env{'course.'.$cid.'.domain'};
                    833:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    834:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                    835:     if  ($who eq $me) {
1.32      albertel  836: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                    837:     } elsif ($allow_not_me) {
1.33      albertel  838: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel  839: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel  840:     }
1.32      albertel  841:     return 'not_owner';
1.21      albertel  842: }
                    843: 
                    844: sub remove_from_queue {
1.30      albertel  845:     my ($queue)=@_;
1.49      albertel  846:     if ($queue eq 'none') { return 'ok'; }
1.27      albertel  847:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    848:     my $cnum=$env{'course.'.$cid.'.num'};
                    849:     my $cdom=$env{'course.'.$cid.'.domain'};
                    850:     my $key="$symb\0queue\0$uname\@$udom";
                    851:     my @keys=($key,"$key\0locked");
1.31      albertel  852:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel  853: }
                    854: 
1.16      albertel  855: sub setup_env_for_other_user {
                    856:     my ($queue_key,$safeeval)=@_;
                    857:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel  858:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel  859:     $env{'form.grade_symb'}=$symb;
                    860:     $env{'form.grade_domain'}=$udom;
                    861:     $env{'form.grade_username'}=$uname;
                    862:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                    863:     &Apache::lonxml::initialize_rndseed($safeeval);
                    864: }
                    865: 
1.31      albertel  866: sub get_queue_data {
                    867:     my ($queue)=@_;
                    868:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    869:     my $cnum=$env{'course.'.$cid.'.num'};
                    870:     my $cdom=$env{'course.'.$cid.'.domain'};
                    871:     my $todo="$symb\0queue\0$uname\@$udom";
                    872:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                    873:     if ($key eq $todo && ref($value)) {
                    874: 	return $value;
                    875:     }
                    876:     return undef;
                    877: }
                    878: 
1.49      albertel  879: sub check_queue_for_key {
                    880:     my ($cid,$queue,$todo)=@_;
                    881:     my $cnum=$env{'course.'.$cid.'.num'};
                    882:     my $cdom=$env{'course.'.$cid.'.domain'};
                    883:     my %results=
                    884: 	&Apache::lonnet::get($queue,[$todo,"$todo\0locked"],$cdom,$cnum);
                    885:     
                    886:     if (exists($results{$todo}) && ref($results{$todo})) {
                    887: 	if (defined($results{"$todo\0locked"})) {
                    888: 	    return 'locked';
                    889: 	}
1.58      albertel  890: 	my $slot=$results{$todo}->[0];
                    891: 	my %slot_data=&Apache::lonnet::get_slot($slot);
                    892: 	if ($slot_data{'endtime'} > time) { 
                    893: 	    return 'in_progress';
                    894: 	}
1.49      albertel  895: 	return 'enqueued';
                    896:     }
                    897:     return undef;
                    898: }
                    899: 
1.14      albertel  900: sub add_to_queue {
1.31      albertel  901:     my ($queue,$slot_name)=@_;
1.49      albertel  902:     if ($queue eq 'none') { return 'ok'; }
1.14      albertel  903:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    904:     my $cnum=$env{'course.'.$cid.'.num'};
                    905:     my $cdom=$env{'course.'.$cid.'.domain'};
                    906:     my %data;
1.31      albertel  907:     $data{"$symb\0queue\0$uname\@$udom"}=[$slot_name];
                    908:     return &Apache::lonnet::put($queue,\%data,$cdom,$cnum);
1.14      albertel  909: }
                    910: 
                    911: sub show_queue {
1.32      albertel  912:     my ($queue,$with_selects)=@_;
1.14      albertel  913:     my $result;
                    914:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    915:     my $cnum=$env{'course.'.$cid.'.num'};
                    916:     my $cdom=$env{'course.'.$cid.'.domain'};
1.59      albertel  917: 
                    918:     my @chosen_sections=
                    919: 	&Apache::loncommon::get_env_multiple('form.chosensections');
                    920:     &Apache::grades::init_perm();
                    921:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
                    922:     &Apache::grades::reset_perm();
                    923: 
1.16      albertel  924:     my $regexp="^$symb\0";
1.30      albertel  925:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel  926:     my ($tmp)=%queue;
                    927:     if ($tmp=~/^error: 2 /) {
1.33      albertel  928: 	return "\n<h3>Current Queue - $queue</h3><table border='1'><tr><td>Empty</td></tr></table>";
1.31      albertel  929:     }
1.33      albertel  930:     $result.="\n<h3>Current Queue - $queue </h3><table border='1'><tr>";
1.32      albertel  931:     if ($with_selects) { $result.="<th></th>"; }
                    932:     $result.="<th>resource</th><th>user</th><th>type</th><th>data</th></tr>";
1.14      albertel  933:     foreach my $key (sort(keys(%queue))) {
1.59      albertel  934: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                    935: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
1.32      albertel  936: 	if ($key=~/locked$/ && !$with_selects) {
1.16      albertel  937: 	    my $title=&Apache::lonnet::gettitle($symb);
1.27      albertel  938: 	    $result.="<tr><td>$title</td><td>$uname</td>";
1.16      albertel  939: 	    $result.='<td>lock</td><td>'.$queue{$key}.'</td></tr>';
1.32      albertel  940: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.16      albertel  941: 	    my ($symb,undef) = split("\0",$key);
                    942: 	    my $title=&Apache::lonnet::gettitle($symb);
1.27      albertel  943: 	    $result.="<tr><td>$title</td><td></td>";
1.16      albertel  944: 	    $result.='<td>last queue modification time</td><td>'.
                    945: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel  946: 	} elsif ($key!~/(timestamp|locked)$/) {
1.16      albertel  947: 	    my $title=&Apache::lonnet::gettitle($symb);
1.32      albertel  948: 	    $result.="<tr>";
1.35      albertel  949: 	    my $slot=$queue{$key}->[0];
                    950: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
1.32      albertel  951: 	    if ($with_selects) {
                    952: 		my $ekey=&Apache::lonnet::escape($key);
                    953: 		my ($action,$description)=('select',&mt('Select'));
                    954: 		if (exists($queue{"$key\0locked"})) {
                    955: 		    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    956: 		    if ($me eq $queue{"$key\0locked"}) {
                    957: 			($action,$description)=('resume',&mt('Resume'));
                    958: 		    } else {
                    959: 			($action,$description)=('unlock',&mt('Unlock'));
                    960: 		    }
                    961: 		}
1.35      albertel  962: 		if (time > $slot_data{'endtime'}) {
                    963: 		    $result.=(<<FORM);
1.32      albertel  964: <td>
                    965: <form method="POST">
                    966:  <input type="hidden" name="gradingkey" value="$ekey" />
                    967:  <input type="hidden" name="queue" value="$queue" />
                    968:  <input type="hidden" name="gradingaction" value="$action" />
                    969:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel  970:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel  971:  <input type="submit" name="submit" value="$description" />
                    972: </form>
                    973: </td>
                    974: FORM
1.35      albertel  975: 
                    976:                 } else {
                    977: 		    $result.='<td>'.&mt("In Progress").'</td>'
                    978: 		}
1.32      albertel  979: 	    }
                    980: 	    $result.="<td>$title</td><td>$uname</td>";
1.27      albertel  981: 	    $result.='<td>queue entry</td><td>Slot: '.$slot.' End time: '.
1.20      albertel  982: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
                    983: 		"</td></tr>";
1.16      albertel  984: 	}
1.14      albertel  985:     }
1.15      albertel  986:     $result.="</table><hr />\n";
1.14      albertel  987:     return $result;
                    988: }
                    989: 
1.34      albertel  990: sub get_queue_counts {
                    991:     my ($queue)=@_;
                    992:     my $result;
                    993:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    994:     my $cnum=$env{'course.'.$cid.'.num'};
                    995:     my $cdom=$env{'course.'.$cid.'.domain'};
                    996:     my $regexp="^$symb\0";
                    997:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                    998:     my ($tmp)=%queue;
                    999:     if ($tmp=~/^error: 2 /) {
                   1000: 	return (0,0,0);
                   1001:     }
                   1002:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
                   1003:     foreach my $key (sort(keys(%queue))) {
                   1004: 	if ($key=~/locked$/) {
                   1005: 	    $locks++;
                   1006: 	} elsif ($key=~/timestamp$/) {
                   1007: 	    #ignore
                   1008: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1009: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1010: 	    $entries++;
                   1011: 	    my $slot=$queue{$key}->[0];
                   1012: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1013: 	    if (time > $slot_data{'endtime'}) {
                   1014: 		$ready_to_grade++;
                   1015: 	    }
                   1016: 	}
                   1017:     }
                   1018:     return ($entries,$ready_to_grade,$locks);
                   1019: }
                   1020: 
1.49      albertel 1021: sub encode_queue_key {
                   1022:     my ($symb,$udom,$uname)=@_;
                   1023:     return "$symb\0queue\0$uname\@$udom";
                   1024: }
                   1025: 
1.14      albertel 1026: sub decode_queue_key {
                   1027:     my ($key)=@_;
                   1028:     my ($symb,undef,$user) = split("\0",$key);
                   1029:     my ($uname,$udom) = split('@',$user);
                   1030:     return ($symb,$uname,$udom);
                   1031: }
                   1032: 
                   1033: sub queue_key_locked {
1.30      albertel 1034:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel 1035:     if (!defined($cdom) || !defined($cnum)) {
                   1036: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1037: 	$cnum=$env{'course.'.$cid.'.num'};
                   1038: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1039:     }
1.14      albertel 1040:     my ($key_locked,$value)=
1.30      albertel 1041: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel 1042:     if ($key_locked eq "$key\0locked") {
                   1043: 	return $value;
                   1044:     }
                   1045:     return undef;
                   1046: }
                   1047: 
                   1048: sub pick_from_queue_data {
1.30      albertel 1049:     my ($queue,$check_section,$queuedata,$cdom,$cnum)=@_;
                   1050:     foreach my $key (keys(%$queuedata)) {
1.14      albertel 1051: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                   1052: 	if ($check_section) {
                   1053: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel 1054: 	    if ($section eq $check_section) {
1.33      albertel 1055: 		&Apache::lonxml::debug("my sec");
1.15      albertel 1056: 		next;
                   1057: 	    }
1.14      albertel 1058: 	}
1.30      albertel 1059: 	my $slot=$queuedata->{$key}[0];
1.14      albertel 1060: 	my %slot_data=&Apache::lonnet::get_slot($slot);
1.15      albertel 1061: 	if ($slot_data{'endtime'} > time) { 
1.33      albertel 1062: 	    &Apache::lonxml::debug("not time");
1.15      albertel 1063: 	    next;
                   1064: 	}
1.30      albertel 1065: 	if (&queue_key_locked($queue,$key,$cdom,$cnum)) {
1.33      albertel 1066: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel 1067: 	    next;
                   1068: 	}
1.14      albertel 1069: 	return $key;
                   1070:     }
                   1071:     return undef;
                   1072: }
                   1073: 
1.15      albertel 1074: sub find_mid_grade {
1.30      albertel 1075:     my ($queue,$symb,$cdom,$cnum)=@_;
1.16      albertel 1076:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.15      albertel 1077:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1078:     if ($todo) {
1.30      albertel 1079: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel 1080: 	if ($who eq $me) { return $todo; }
                   1081:     }
                   1082:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel 1083:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel 1084:     foreach my $key (keys(%locks)) {
                   1085: 	my $who=$locks{$key};
                   1086: 	if ($who eq $me) {
                   1087: 	    $todo=$key;
                   1088: 	    $todo=~s/\0locked$//;
                   1089: 	    return $todo;
                   1090: 	}
                   1091:     }
                   1092:     return undef;
                   1093: }
                   1094: 
1.32      albertel 1095: sub lock_key {
                   1096:     my ($queue,$todo)=@_;
                   1097:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1098:     my (undef,$cid)=&Apache::lonxml::whichuser();
                   1099:     my $cnum=$env{'course.'.$cid.'.num'};
                   1100:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1101:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                   1102: 					$cdom,$cnum);
1.33      albertel 1103:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel 1104:     if ($success eq 'ok') {
                   1105: 	return 1;
                   1106:     }
                   1107:     return 0;
                   1108: }
                   1109: 
1.14      albertel 1110: sub get_from_queue {
1.30      albertel 1111:     my ($queue)=@_;
1.14      albertel 1112:     my $result;
                   1113:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1114:     my $cnum=$env{'course.'.$cid.'.num'};
                   1115:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel 1116:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel 1117:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel 1118:     if ($todo) { return $todo; }
1.14      albertel 1119:     while (1) {
                   1120: 	my $starttime=time;
1.30      albertel 1121: 	&Apache::lonnet::put($queue,{"$symb\0timestamp"=>$starttime},
1.14      albertel 1122: 			     $cdom,$cnum);
1.33      albertel 1123: 	&Apache::lonxml::debug("$starttime");
1.14      albertel 1124: 	my $regexp="^$symb\0queue\0";
1.30      albertel 1125: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel 1126: 	#make a pass looking for a user _not_ in my section
1.14      albertel 1127: 	if ($env{'request.course.sec'}) {
1.33      albertel 1128: 	    &Apache::lonxml::debug("sce");
1.30      albertel 1129: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                   1130: 					\%queue,$cdom,$cnum);
1.33      albertel 1131: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel 1132: 	}
1.33      albertel 1133: 	# no one _not_ in our section so look for any user that is
                   1134: 	# ready for grading
1.14      albertel 1135: 	if (!$todo) {
1.33      albertel 1136: 	    &Apache::lonxml::debug("no sce");
1.31      albertel 1137: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                   1138: 					\%queue,$cdom,$cnum);
1.33      albertel 1139: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel 1140: 	}
                   1141: 	# no user to grade 
                   1142: 	if (!$todo) { last; }
1.33      albertel 1143: 	&Apache::lonxml::debug("got $todo");
1.14      albertel 1144: 	# otherwise found someone so lets try to lock them
1.32      albertel 1145: 	# unless someone else already picked them
                   1146: 	if (!&lock_key($queue,$todo)) { next; }
1.14      albertel 1147: 	my (undef,$endtime)=
1.30      albertel 1148: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel 1149: 				 $cdom,$cnum);
1.33      albertel 1150: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel 1151: 	# someone else already modified the queue, 
                   1152: 	# perhaps our picked user wass already fully graded between
                   1153: 	# when we picked him and when we locked his record? so lets
                   1154: 	# double check.
                   1155: 	if ($endtime != $starttime) {
                   1156: 	    my ($key,$value)=
1.30      albertel 1157: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel 1158: 				     $cdom,$cnum);
1.33      albertel 1159: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel 1160: 	    if ($key eq $todo && ref($value)) {
                   1161: 	    } else {
1.30      albertel 1162: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel 1163: 				     $cdom,$cnum);
1.33      albertel 1164: 		&Apache::lonxml::debug("del");
1.14      albertel 1165: 		next;
                   1166: 	    }
                   1167: 	}
1.33      albertel 1168: 	&Apache::lonxml::debug("last $todo");
1.14      albertel 1169: 	last;
                   1170:     }
                   1171:     return $todo;
                   1172: }
                   1173: 
1.49      albertel 1174: sub select_user {
                   1175:     my ($symb,$cid)=&Apache::lonxml::whichuser();
                   1176:     my $result.='<table border="1">';
                   1177: 
1.59      albertel 1178:     my @chosen_sections=
                   1179: 	&Apache::loncommon::get_env_multiple('form.chosensections');
1.49      albertel 1180:     &Apache::grades::init_perm();
1.59      albertel 1181:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
1.49      albertel 1182:     &Apache::grades::reset_perm();
                   1183: 
                   1184:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
                   1185: 	my ($uname,$udom) = split(/:/,$student);
1.59      albertel 1186: 	
1.49      albertel 1187: 	my %status = &get_student_status($symb,$cid,$udom,$uname);
                   1188: 	my $queue = 'none';
1.58      albertel 1189: 	my $cannot_grade;
                   1190: 	if ($status{'reviewqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1191: 	    $queue = 'reviewqueue';
1.58      albertel 1192: 	    if ($status{'reviewqueue'} eq 'in_progress') {
                   1193: 		$cannot_grade=1;
                   1194: 	    }
                   1195: 	} elsif ($status{'gradingqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1196: 	    $queue = 'gradingqueue';
1.58      albertel 1197: 	    if ($status{'gradingqueue'} eq 'in_progress') {
                   1198: 		$cannot_grade=1;
                   1199: 	    }
1.49      albertel 1200: 	}
                   1201: 	my $todo = 
                   1202: 	    &Apache::lonnet::escape(&encode_queue_key($symb,$udom,$uname));
1.58      albertel 1203: 	if ($cannot_grade) {
                   1204: 	    $result.='<tr><td>&nbsp;</td><td>'.$fullname->{$student}.
                   1205: 		'</td><td>';
                   1206: 	} else {
                   1207: 	    $result.=<<RESULT;
1.49      albertel 1208: <tr>
                   1209:   <td>
                   1210:     <form method="POST">
                   1211:       <input type="hidden" name="gradingkey" value="$todo" />
                   1212:       <input type="hidden" name="queue" value="$queue" />
                   1213:       <input type="hidden" name="webgrade" value="no" />
1.52      albertel 1214:       <input type="hidden" name="regrade" value="yes" />
1.49      albertel 1215:      <input type="submit" name="submit" value="Regrade" />
                   1216:     </form>
                   1217:   <td>$fullname->{$student}</td>
                   1218:   <td>
                   1219: RESULT
1.58      albertel 1220:         }
1.49      albertel 1221:         if ($status{'status'} eq 'pass') {
                   1222: 	    $result .= '<font color="green">'.&mt('Passed').'</font>';
                   1223: 	} elsif ($status{'status'} eq 'fail') {
                   1224: 	    $result .= '<font color="red">'.&mt('Failed').'</font>';
                   1225: 	} elsif ($status{'status'} eq 'review') {
                   1226: 	    $result .= '<font color="blue">'.&mt('Under Review').'</font>';
                   1227: 	} elsif ($status{'status'} eq 'ungraded') {
                   1228: 	    $result .= &mt('Ungraded');
                   1229: 	} elsif ($status{'status'} ne '') {
                   1230: 	    $result .= '<font color="orange">'.&mt('Unknown Status').'</font>';
                   1231: 	} else {
                   1232: 	    $result.="&nbsp;";
                   1233: 	}
                   1234: 	if ($status{'version'}) {
                   1235: 	    $result .= ' '.&mt('Version').' '.$status{'version'};
                   1236: 	}
                   1237: 	$result.= '</td><td>';
                   1238: 	if ($status{'reviewqueue'} eq 'enqueued') {
                   1239: 	    $result .= &mt('Awaiting Review');
                   1240: 	} elsif ($status{'reviewqueue'} eq 'locked') {
                   1241: 	    $result .= &mt('Under Review');
1.58      albertel 1242: 	} elsif ($status{'reviewqueue'} eq 'in_progress') {
                   1243: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1244: 	} elsif ($status{'gradingqueue'} eq 'enqueued') {
                   1245: 	    $result .= &mt('Awaiting Grading');
                   1246: 	} elsif ($status{'gradingqueue'} eq 'locked') {
                   1247: 	    $result .= &mt('Being Graded');
1.58      albertel 1248: 	} elsif ($status{'gradingqueue'} eq 'in_progress') {
                   1249: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1250: 	} else {
                   1251: 	    $result.="&nbsp;";
                   1252: 	}
                   1253: 	$result.= '</td></tr>';
                   1254:     }
                   1255:     $result.='</table>';
                   1256:     return $result;
                   1257: }
                   1258: 
                   1259: sub get_student_status {
                   1260:     my ($symb,$cid,$udom,$uname)=@_;
                   1261:     my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
                   1262: 					  $udom,$uname);
                   1263:     my %status;
                   1264:     $status{'status'}=$record{'resource.status'};
                   1265:     $status{'version'}=$record{'resource.version'};
                   1266:     $status{'grader'}=$record{'resource.grader'};
                   1267:     $status{'reviewqueue'}=&check_queue_for_key($cid,'reviewqueue',
                   1268: 				       &encode_queue_key($symb,$udom,$uname));
                   1269:     $status{'gradingqueue'}=&check_queue_for_key($cid,'gradingqueue',
                   1270: 				       &encode_queue_key($symb,$udom,$uname));
                   1271:     return %status;
                   1272: }
                   1273: 
1.1       albertel 1274: sub start_ClosingParagraph {
                   1275:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1276:     my $result;
                   1277:     if ($target eq 'web') {
1.13      albertel 1278:     } elsif ($target eq 'webgrade') {
                   1279: 	&Apache::lonxml::startredirection();
1.1       albertel 1280:     }
                   1281:     return $result;
                   1282: }
                   1283: 
                   1284: sub end_ClosingParagraph {
                   1285:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1286:     my $result;
                   1287:     if ($target eq 'web') {
1.13      albertel 1288:     } elsif ($target eq 'webgrade') {
                   1289: 	&Apache::lonxml::endredirection();
1.1       albertel 1290:     }
                   1291:     return $result;
                   1292: }
                   1293: 
1.19      albertel 1294: sub get_id {
                   1295:     my ($parstack,$safeeval)=@_;
                   1296:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                   1297:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                   1298:     return $id;
                   1299: }
                   1300: 
1.1       albertel 1301: my %dimension;
                   1302: sub start_Dimension {
                   1303:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1304:     undef(%dimension);
1.19      albertel 1305:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel 1306:     $Apache::bridgetask::dimension=$dim_id;
                   1307:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                   1308:     undef(@Apache::bridgetask::instance);
1.20      albertel 1309:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
                   1310: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.54      albertel 1311:     &Apache::lonxml::startredirection();
                   1312:     return &internal_location($dim_id);
1.1       albertel 1313: }
                   1314: 
1.13      albertel 1315: sub get_instance {
                   1316:     &Apache::response::pushrandomnumber();
                   1317:     my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
1.29      albertel 1318:     my $num=@order;
                   1319:     my $version=&get_version();
                   1320:     my $which=($version-1)%$num;
                   1321:     return $order[$which];
1.13      albertel 1322: }
                   1323: 
1.18      albertel 1324: {
                   1325:     my $last_link;
                   1326:     sub end_Dimension {
                   1327: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.54      albertel 1328: 	my $result=&Apache::lonxml::endredirection();
1.25      albertel 1329: 	my $dim=&get_id($parstack,$safeeval);
1.19      albertel 1330: 	my $instance=&get_instance();
1.25      albertel 1331: 	my $version=&get_version();
1.18      albertel 1332: 	if ($target eq 'web') {
1.47      albertel 1333: 	    @Apache::scripttag::parser_env = @_;
                   1334: 	    $result.=&Apache::scripttag::xmlparse($dimension{'intro'});
                   1335: 	    @Apache::scripttag::parser_env = @_;
                   1336: 	    $result.=&Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.26      albertel 1337: 	    if ($Apache::lonhomework::history{"resource.$version.status"} eq 'pass' ||
                   1338: 		$Apache::lonhomework::history{"resource.$version.status"} eq 'fail') {
1.54      albertel 1339: 
                   1340: 		my $dim_status=$Apache::lonhomework::history{"resource.$version.$dim.status"};
                   1341: 		my $mandatory='Mandatory';
                   1342: 		if ($Apache::bridgetask::dimensionmandatory{$dim} eq 'N') {
                   1343: 		    $mandatory='Optional';
                   1344: 		}
                   1345: 		my $dim_info="<div class='$dim_status'>\n";
                   1346: 		if ($dim_status eq 'pass') {
                   1347: 		    $dim_info.='<h3>Question : you passed this '.$mandatory.' question</h3>';
                   1348: 		}
                   1349: 		if ($dim_status eq 'fail') {
                   1350: 		    $dim_info.='<h3>Question : you did not pass this '.$mandatory.' question</h3>';
                   1351: 		}
                   1352: 		my $man_count=0;
                   1353: 		my $opt_count=0;
                   1354: 		my $opt_passed=0;
                   1355: 		foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1356: 		    if ($dimension{$instance.'.criteria.'.$id.'.mandatory'} 
                   1357: 			eq 'N') {
                   1358: 			$opt_count++;
                   1359: 			if ($Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"} eq 'pass') {
                   1360: 			    $opt_passed++;
                   1361: 			}
                   1362: 		    } else {
                   1363: 			$man_count++;
                   1364: 		    }
                   1365: 		}
                   1366: 		my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
                   1367: 						       $parstack,$safeeval);
                   1368: 		if ($opt_req !~ /\S/) { $opt_req='0'; }
                   1369: 		$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>";
                   1370: 
                   1371: 		my $internal_location=&internal_location($dim);
                   1372: 		$result=~s/\Q$internal_location\E/$dim_info/;
                   1373: 
1.22      albertel 1374: 		foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.25      albertel 1375: 		    my $status=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"};
                   1376: 		    my $comment=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"};
1.53      albertel 1377: 		    my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1378: 		    if ($mandatory) {
                   1379: 			$mandatory='Mandatory';
                   1380: 		    } else {
                   1381: 			$mandatory='Optional';
                   1382: 		    }
1.22      albertel 1383: 		    if ($status eq 'fail') {
                   1384: 		    } elsif ($status eq 'pass') {
                   1385: 		    } else {
1.49      albertel 1386: 			&Apache::lonxml::error("Student viewing a graded bridgetask was shown a status of $status");
1.22      albertel 1387: 		    }
1.53      albertel 1388: 		    my $status_display=$status;
                   1389: 		    $status_display=~s/^([a-z])/uc($1)/e;
1.54      albertel 1390: 		    @Apache::scripttag::parser_env = @_;
1.53      albertel 1391: 		    $result.='<div class="'.$status.'"><h4>'.$mandatory.
1.54      albertel 1392: 			' Criteria</h4><p>';
                   1393: 		    @Apache::scripttag::parser_env = @_;
                   1394: 		    $result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
                   1395: 		    $result.='</p><p class="grade">'.$status_display.'</p>';
1.25      albertel 1396: 		    if ($Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}) {
1.53      albertel 1397: 			$result.='<p class="comment">'.$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}.'</p>';
1.22      albertel 1398: 		    }
1.53      albertel 1399: 		    $result.='</div>';
1.22      albertel 1400: 		}
                   1401: 	    }
1.18      albertel 1402: 	} elsif ($target eq 'webgrade') {
1.47      albertel 1403: 	    # in case of any side effects that we need
                   1404: 	    @Apache::scripttag::parser_env = @_;
                   1405: 	    &Apache::scripttag::xmlparse($dimension{'intro'});
                   1406: 	    @Apache::scripttag::parser_env = @_;
                   1407: 	    &Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.18      albertel 1408: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1409: 		my $link='criteria_'.$instance.'_'.$id;
1.25      albertel 1410: 		my $status=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"};
1.19      albertel 1411: 		$result.='<tr><td width="100%" valign="top">'.
1.18      albertel 1412: 		    '<a name="'.$link.'" />'.
                   1413: 		    '<a name="next_'.$last_link.'" />'.
1.47      albertel 1414: 		    '<br /><textarea enabled="false" style="width:100%" rows="8" width="25" wrap="hard">';
                   1415: 		@Apache::scripttag::parser_env = @_;
                   1416: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
                   1417: 		$result.='</textarea>'.
1.18      albertel 1418: 		    #$dimension{$instance.'.criteria.'.$id}.
                   1419: 		    '</td>'.
1.19      albertel 1420: 		    '<td><nobr>Additional Comment for Student</nobr> <br />'.
1.25      albertel 1421: 		    '<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 1422: 		    '</td>'.
                   1423: 		    '<td>'.
1.47      albertel 1424: 		    '<nobr><label><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.&mt('Ungraded').'</label></nobr><br />'.
1.30      albertel 1425: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.&mt('Pass').'</label><br />'.
                   1426: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label><br />'.
                   1427: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.&mt('Review').'</label><br />'.
1.18      albertel 1428: 		    '</td>'.
                   1429: 		    '<td>'.
                   1430: 		    '<a href="#'.$last_link.'">Prev</a><br />'.
1.20      albertel 1431: 		    '<a href="#next_'.$link.'">Next</a><br /><br /><br />'.
                   1432: 		    '<input type="submit" name="next" value="'.
                   1433: 		    &mt('Done').'" /> '.
1.30      albertel 1434: 		    '<input type="submit" name="stop" value="'.
                   1435: 		    &mt('Stop').'" /> '.
1.18      albertel 1436: 		    '</td></tr>';
1.22      albertel 1437: 		$result.='<tr><td colspan="4">';
                   1438: 		my (undef,undef,$udom,$uname) = &Apache::lonxml::whichuser();
1.52      albertel 1439: 		my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
1.43      albertel 1440: 		foreach my $partial_file (split(',',$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"})) {
1.22      albertel 1441: 		    my $file=$file_url.$partial_file;
1.52      albertel 1442: 		    $file=~s|/+|/|g;
1.22      albertel 1443: 		    &Apache::lonnet::allowuploaded('/adm/bridgetask',$file);
1.48      albertel 1444: 		    $result.='<nobr><a href="'.$file.'" target="lonGRDs"><img src="'.
1.22      albertel 1445: 			&Apache::loncommon::icon($file).'" border=0"> '.$file.
1.48      albertel 1446: 			'</a></nobr> ';
1.22      albertel 1447: 		}
                   1448: 		$result.='</td></tr>';
1.18      albertel 1449: 		$last_link=$link;
                   1450: 	    }
1.22      albertel 1451: 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
1.19      albertel 1452: 	    my $optional_passed=0;
1.20      albertel 1453: 	    my $mandatory_failed=0;
                   1454: 	    my $ungraded=0;
                   1455: 	    my $review=0;
1.19      albertel 1456: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.25      albertel 1457: 		my $status=$Apache::lonhomework::results{"resource.$version.$dim.$instance.$id.status"}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
                   1458: 		$Apache::lonhomework::results{"resource.$version.$dim.$instance.$id.comment"}=$env{'form.HWVAL_comment_criteria_'.$instance.'_'.$id};
1.20      albertel 1459: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1460: 		if ($status eq 'pass') {
                   1461: 		    if (!$mandatory) { $optional_passed++; }
                   1462: 		} elsif ($status eq 'fail') {
                   1463: 		    if ($mandatory) { $mandatory_failed++; }
1.21      albertel 1464: 		} elsif ($status eq 'review') {
                   1465: 		    $review++;
1.20      albertel 1466: 		} elsif ($status eq 'ungraded') {
                   1467: 		    $ungraded++;
1.21      albertel 1468: 		} else {
1.47      albertel 1469: 		    $ungraded++;
1.19      albertel 1470: 		}
                   1471: 	    }
1.20      albertel 1472: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
                   1473: 		$mandatory_failed++;
                   1474: 	    }
1.21      albertel 1475: 	    &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 1476: 	    if ($review) {
1.25      albertel 1477: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1478: 		    'review';
1.20      albertel 1479: 	    } elsif ($ungraded) {
1.25      albertel 1480: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1481: 		    'ungraded';
1.20      albertel 1482: 	    } elsif ($mandatory_failed) {
1.25      albertel 1483: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1484: 		    'fail';
1.20      albertel 1485: 	    } else {
1.25      albertel 1486: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1487: 		    'pass';
1.20      albertel 1488: 	    }
1.13      albertel 1489: 	}
1.18      albertel 1490: 	return $result;
1.1       albertel 1491:     }
                   1492: }
                   1493: 
                   1494: sub start_IntroParagraph {
                   1495:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1496:     my $result;
1.13      albertel 1497:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1498: 	if ($tagstack->[-2] eq 'Dimension') {
                   1499: 	    $dimension{'intro'}=&Apache::lonxml::get_all_text('/introparagraph',$parser);
                   1500: 	
                   1501: 	} elsif ($target eq 'webgrade') {
                   1502: 	    &Apache::lonxml::get_all_text('/introparagraph',$parser);
1.1       albertel 1503: 	}
1.47      albertel 1504: 	
1.1       albertel 1505:     }
                   1506:     return $result;
                   1507: }
                   1508: 
                   1509: sub end_IntroParagraph {
                   1510: }
                   1511: 
                   1512: sub start_Instance {
                   1513:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel 1514:     my $id=&get_id($parstack,$safeeval);
                   1515:     push(@{$dimension{'instances'}},$id);
                   1516:     push(@Apache::bridgetask::instance,$id);
                   1517:     push(@Apache::bridgetask::instancelist,$id);
1.20      albertel 1518:     $dimension{$id.'.optionalrequired'}=
1.19      albertel 1519: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.1       albertel 1520:     return '';
                   1521: }
                   1522: 
                   1523: sub end_Instance {
                   1524: }
                   1525: 
                   1526: sub start_InstanceText {
                   1527:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.47      albertel 1528:     my $instance_id=$Apache::bridgetask::instance[-1];
                   1529:     my $text=&Apache::lonxml::get_all_text('/instancetext',$parser);
1.13      albertel 1530:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1531: 	$dimension{$instance_id.'.text'}=$text;
1.1       albertel 1532:     }
                   1533:     return '';
                   1534: }
                   1535: 
                   1536: sub end_InstanceText {
                   1537:     return '';
                   1538: }
                   1539: 
                   1540: sub start_Criteria {
                   1541:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel 1542:     my $instance_id=$Apache::bridgetask::instance[-1];
1.47      albertel 1543:     my $criteria=&Apache::lonxml::get_all_text('/criteria',$parser);
1.21      albertel 1544:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.19      albertel 1545: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel 1546: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel 1547: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                   1548: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel 1549: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                   1550:     }
                   1551:     return '';
                   1552: }
                   1553: 
1.47      albertel 1554: sub end_Criteria {
                   1555: }
                   1556: 
1.4       albertel 1557: sub proctor_validation_screen {
                   1558:     my ($slot) = @_;
                   1559:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5       albertel 1560:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.44      albertel 1561:     my $name=&Apache::loncommon::plainname($user,$domain);
                   1562:     
1.4       albertel 1563:     my $msg;
1.11      albertel 1564:     if ($env{'form.proctorpassword'}) {
1.4       albertel 1565: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                   1566: 	    .'</font></p>';
                   1567:     }
1.47      albertel 1568:     if (!$env{'form.proctordomain'}) { $env{'form.proctordomain'}=$domain; }
1.4       albertel 1569:     my $result= (<<ENDCHECKOUT);
                   1570: <h2>Proctor Validation</h2>
                   1571:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   1572:     $msg
1.11      albertel 1573: <form name="checkout" method="POST" action="$env{'request.uri'}">
1.4       albertel 1574: <input type="hidden" name="validate" value="yes" />
                   1575: <input type="hidden" name="submitted" value="yes" />
                   1576: <table>
1.44      albertel 1577:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$env{'form.proctorname'}" /></td></tr>
1.4       albertel 1578:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.46      albertel 1579:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$env{'form.proctordomain'}" /></td></tr>
1.4       albertel 1580: </table>
                   1581: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
1.44      albertel 1582: <table border="1">
                   1583:   <tr><td>
                   1584:     <table>
                   1585:       <tr><td colspan="2">Student who should be logged in is:</td></tr>
                   1586:       <tr><td>Name:</td><td>$name</td></tr>
1.45      albertel 1587:       <tr><td>Student ID:</td><td>$env{'environment.id'}</td></tr>
1.44      albertel 1588:       <tr><td>Usename</td><td>$user\@$domain</td></tr>
                   1589:       <tr><td colspan="2"><img src="$url" /></td></tr>
                   1590:     </table>
                   1591:   </tr></td>
                   1592: </table>
1.4       albertel 1593: </form>
                   1594: ENDCHECKOUT
                   1595:     return $result;
                   1596: }
                   1597: 
1.1       albertel 1598: 1;
                   1599: __END__

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