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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.43    ! albertel    4: # $Id: bridgetask.pm,v 1.42 2005/08/15 19:41:43 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.29      albertel  147:     my $result=' <input type="submit" name="gradeasubmission" value="'.
1.13      albertel  148: 	&mt("Get a submission to grade").'" />';
                    149:     $result.='<input type="hidden" name="grade_target" value="webgrade" />';
1.40      albertel  150:     if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.34      albertel  151: 	my ($entries,$ready,$locks)=&get_queue_counts('gradingqueue');
                    152: 	$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);
                    153: 
                    154: 	$result.=' <input type="submit" name="reviewagrading" value="'.
                    155: 	    &mt("Select an entry from the grading queue").'" /> </p>'."\n";
                    156: 
                    157: 	($entries,$ready,$locks)=&get_queue_counts('reviewqueue');
                    158: 	$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  159: 	$result.=' <input type="submit" name="reviewasubmission" value="'.
1.34      albertel  160: 	    &mt("Select an entry from the review queue").'" /> </p>'."\n";
1.32      albertel  161:     }
1.13      albertel  162:     return $result;
                    163: }
                    164: 
1.22      albertel  165: sub add_request_another_attempt_button {
1.38      albertel  166:     my ($text)=@_;
                    167:     if (!$text) { $text="Request another attempt"; }
1.25      albertel  168:     my $result;
1.36      albertel  169:     my $symb=&Apache::lonnet::symbread();
1.37      albertel  170:     my ($slot_name,$slot)=&Apache::slotrequest::check_for_reservation($symb);
1.38      albertel  171:     my $action='get_reservation';
1.37      albertel  172:     if ($slot_name) {
1.38      albertel  173: 	$text="Change reservation.";
                    174: 	$action='change_reservation';
1.37      albertel  175: 	my $description=&Apache::slotrequest::get_description($slot_name,
                    176: 							      $slot);
                    177: 	$result.=(<<STUFF);
                    178: <p> Will be next available: $description </p>
                    179: STUFF
                    180:     }
1.38      albertel  181:     
                    182:     if ($env{'request.enc'}) { $symb=&Apache::lonenc::encrypted($symb); }
                    183:     $symb=&Apache::lonnet::escape($symb);
                    184:     $result.='<form method="POST" action="/adm/slotrequest">'.
                    185: 	'<input type="hidden" name="symb" value="'.$symb.'" />'.
                    186: 	'<input type="hidden" name="command" value="'.$action.'" />'.
                    187: 	'<input type="submit" name="requestattempt" value="'.
                    188: 	&mt($text).'" />'.
                    189: 	'</form>';
1.25      albertel  190:     return $result;
1.22      albertel  191: }
                    192: 
1.30      albertel  193: sub preserve_grade_info {
                    194:     my $result;
                    195:     # if we are viewing someone else preserve that info
                    196:     if (defined $env{'form.grade_symb'}) {
                    197: 	foreach my $field ('symb','courseid','domain','username') {
                    198: 	    $result .= '<input type="hidden" name="grade_'.$field.
                    199: 		'" value="'.$env{"form.grade_$field"}.'" />'."\n";
                    200: 	}
                    201:     }
                    202:     return $result;
                    203: }
                    204: 
1.1       albertel  205: sub start_Task {
                    206:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    207: 
1.4       albertel  208:     my ($status,$accessmsg,$slot);
1.16      albertel  209:     if ($target ne 'webgrade') {
                    210: 	&Apache::structuretags::initialize_storage();
                    211: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
                    212:     } 
                    213: 
1.4       albertel  214:     $Apache::lonhomework::parsing_a_task=1;
1.1       albertel  215:     #should get back a <html> or the neccesary stuff to start XML/MathML
                    216:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    217: 	&Apache::structuretags::page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.16      albertel  218:     
1.8       albertel  219:     if ($target eq 'web') {
1.13      albertel  220: 	if ($Apache::lonhomework::modifygrades) {
                    221: 	    $body_tag_start.='<form name="gradesubmission" method="POST" action="';
                    222: 	    my $uri=$env{'request.uri'};
                    223: 	    if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    224: 	    $body_tag_start.=$uri.'">'.&add_grading_button()."</form>";
1.38      albertel  225: 	    my $symb=&Apache::lonnet::symbread();
1.40      albertel  226: 	    if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
                    227: 		$body_tag_start.='<form method="POST" action="/adm/slotrequest">'.
                    228: 		    '<input type="hidden" name="symb" value="'.$symb.'" />'.
                    229: 		    '<input type="hidden" name="command" value="showslots" />'.
                    230: 		    '<input type="submit" name="requestattempt" value="'.
                    231: 		    &mt('Show Slot list').'" />'.
                    232: 		    '</form>';
                    233: 	    }
1.13      albertel  234: 	}
1.8       albertel  235:     }
1.21      albertel  236:     if ($target eq 'web' || ($target eq 'grade' && !$env{'form.webgrade'}) || $target eq 'answer' ||
1.1       albertel  237: 	$target eq 'tex') {
1.29      albertel  238: 	my ($version,$previous)=&get_version();
1.14      albertel  239: 	($status,$accessmsg,my $slot_name,$slot) = 
1.4       albertel  240: 	    &Apache::lonhomework::check_task_access('0');
1.9       albertel  241: 	push(@Apache::inputtags::status,$status);
1.14      albertel  242: 	$Apache::inputtags::slot_name=$slot_name;
1.1       albertel  243: 	my $expression='$external::datestatus="'.$status.'";';
1.23      albertel  244: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$version.solved"}.'";';
1.1       albertel  245: 	&Apache::run::run($expression,$safeeval);
                    246: 	&Apache::lonxml::debug("Got $status");
1.25      albertel  247: 	$body_tag_start.=&add_previous_version_button($status);
1.29      albertel  248: 	if (!$previous && (
                    249: 			   ( $status eq 'CLOSED' ) ||
                    250: 			   ( $status eq 'BANNED') ||
                    251: 			   ( $status eq 'UNAVAILABLE') ||
                    252: 			   ( $status eq 'NOT_IN_A_SLOT') ||
                    253: 			   ( $status eq 'NEEDS_CHECKIN') ||
                    254: 			   ( $status eq 'WAITING_FOR_GRADE') ||
                    255: 			   ( $status eq 'INVALID_ACCESS') )) {
1.1       albertel  256: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
                    257: 	    if ( $target eq "web" ) {
1.4       albertel  258: 		$result.= $head_tag_start.'</head>'.$body_tag_start;
                    259: 		my $msg;
1.1       albertel  260: 		if ($status eq 'UNAVAILABLE') {
                    261: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3       albertel  262: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    263: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.38      albertel  264: 		    $msg.=&add_request_another_attempt_button("Sign up for time to work.");
1.4       albertel  265: 		} elsif ($status eq 'NEEDS_CHECKIN') {
                    266: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
                    267: 			'</h1>'.&proctor_validation_screen($slot);
1.22      albertel  268: 		} elsif ($status eq 'WAITING_FOR_GRADE') {
                    269: 		    $msg.='<h1>'.&mt('Your submission is in the grading queue.').'</h1>';
1.1       albertel  270: 		} elsif ($status ne 'NOT_YET_VIEWED') {
                    271: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
                    272: 		}
                    273: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
                    274: 		    $msg.='The problem '.$accessmsg;
                    275: 		}
                    276: 		$result.=$msg.'<br />';
                    277: 	    } elsif ($target eq 'tex') {
                    278: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
                    279: 		if ($status eq 'UNAVAILABLE') {
                    280: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
                    281: 		} else {
                    282: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
                    283: 		}
1.22      albertel  284: 	    } elsif ($target eq 'grade' && !$env{'form.webgrade'}) {
1.4       albertel  285: 		if ($status eq 'NEEDS_CHECKIN') {
1.41      albertel  286: 		    &proctor_check_auth($slot_name,$slot);
1.4       albertel  287: 		}
1.1       albertel  288: 	    }
                    289: 	} elsif ($target eq 'web') {
                    290: 	    my $name= &Apache::structuretags::get_resource_name($parstack,$safeeval);
                    291: 	    $result.="$head_tag_start<title>$name</title></head>
1.36      albertel  292:               $body_tag_start \n";
                    293: 	    
1.29      albertel  294: 	    my ($version,$previous)=&get_version();
1.25      albertel  295: 	    if ($Apache::lonhomework::history{"resource.$version.status"} eq 'fail') {
1.22      albertel  296: 		$result.='<h1><font color="red">'.&mt('Did not pass').'</font></h1>';
1.29      albertel  297: 		if (!$previous) {
                    298: 		    $result.=&add_request_another_attempt_button();
                    299: 		}
1.22      albertel  300: 	    }
1.25      albertel  301: 	    if ($Apache::lonhomework::history{"resource.$version.status"} eq 'pass') {
1.22      albertel  302: 		$result.='<h1><font color="green">'.&mt('Passed').'</font></h1>';
                    303: 	    }
1.36      albertel  304: 	    $result.=$form_tag_start.
                    305: 		'<input type="hidden" name="submitted" value="yes" />';
                    306: 	    $result.=&preserve_grade_info();
                    307: 
1.1       albertel  308: 	}
1.21      albertel  309:     } elsif ( ($target eq 'grade' && $env{'form.webgrade'}) ||
                    310: 	      $target eq 'webgrade') {
1.32      albertel  311: 	my $webgrade='yes';
1.21      albertel  312: 	if ($target eq 'webgrade') {
1.32      albertel  313: 	    $result.=$head_tag_start.$body_tag_start;
1.31      albertel  314: 	    $result.='<br />Review'.&show_queue('reviewqueue');
1.33      albertel  315: 	    $result.='<br />Grade'.&show_queue('gradingqueue');
1.30      albertel  316: 	}
1.33      albertel  317: 	# FIXME Blast! still need to reorg this, need to reshow the
                    318:         #       queue being reviewed once done with the grade pass...
                    319:         #       Hrrm, vaildation pass should perhaps say 'not_locked'
                    320:         #       perhaps do a search if there is a key that is mine and if
                    321:         #       there isn't reshow the queue....
                    322: 	my ($todo,$status_code)=&get_key_todo($target);
                    323: 
                    324: 	&Apache::lonnet::logthis("got todo -$todo- stat -$status_code-");
                    325: 	if ($todo) {
                    326: 	    &setup_env_for_other_user($todo,$safeeval);
                    327: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
                    328: 	    $result.="\n".'<table><tr><td>Found '.
                    329: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
                    330: 	    $form_tag_start.=
                    331: 		'<input type="hidden" name="gradingkey" value="'.
                    332: 		&Apache::lonnet::escape($todo).'" />';
                    333: 	    $Apache::bridgetask::queue_key=$todo;
                    334: 	    &Apache::structuretags::initialize_storage();
                    335: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
                    336: 	    if ($target eq 'webgrade') {
                    337: 		$result.='<br />After -'.&show_queue($env{'form.queue'});
                    338: 		$result.="\n".'<table width="100%" style="width:100%" border="1">';
                    339: 		if ($status_code eq 'selected') {
                    340: 		    $form_tag_start.=
                    341: 			'<input type="hidden" name="queuemode" value="selected" />';
                    342: 		}
                    343: 	    }
1.15      albertel  344: 	} else {
1.33      albertel  345: 	    if ($target eq 'webgrade') {
                    346: 		$result.="\n";
                    347: 		if      ($status_code eq 'stop') {
                    348: 		    $result.='<b>'.&mt("Stopped grading.").'</b>';
                    349: 		} elsif ($status_code eq 'lock_failed') {
                    350: 		    $result.='<b>'.&mt("Failed to lock the request record.").'</b>';
                    351: 		} elsif ($status_code eq 'unlock') {
                    352: 		    $result.='<b>'.&mt("Unlocked the requested record.").'</b>';
                    353: 		    $result.=&show_queue($env{'form.queue'},1);
                    354: 		} elsif ($status_code eq 'show_list') {
                    355: 		    $result.=&show_queue($env{'form.queue'},1);
                    356: 		} else {
                    357: 		    $result.='<b>'.&mt("No user to be graded.").'</b>';
1.32      albertel  358: 		}
1.21      albertel  359: 	    }
1.33      albertel  360: 	    $webgrade='no';
                    361: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
1.32      albertel  362: 	}
                    363: 	if ($target eq 'webgrade' && defined($env{'form.queue'})) {
                    364: 	    $result.=$form_tag_start;
                    365: 	    $result.='<input type="hidden" name="webgrade" value="'.
                    366: 		$webgrade.'" />';
                    367: 	    $result.='<input type="hidden" name="queue" value="'.
                    368: 		$env{'form.queue'}.'" />';
1.15      albertel  369: 	}
1.1       albertel  370:     } else {
                    371: 	# page_start returned a starting result, delete it if we don't need it
                    372: 	$result = '';
                    373:     }
                    374:     return $result;
                    375: }
                    376: 
1.32      albertel  377: sub get_key_todo {
                    378:     my ($target)=@_;
                    379:     my $todo;
1.33      albertel  380: 
                    381:     if (defined($env{'form.reviewasubmission'})) {
                    382: 	&Apache::lonnet::logthis("review a submission....");
                    383: 	$env{'form.queue'}='reviewqueue';
                    384: 	return (undef,'show_list');
                    385:     }
                    386: 
                    387:     if (defined($env{'form.reviewagrading'})) {
                    388: 	&Apache::lonxml::debug("review a grading....");
                    389: 	$env{'form.queue'}='gradingqueue';
                    390: 	return (undef,'show_list');
                    391:     }
                    392: 
1.32      albertel  393:     my $queue=$env{'form.queue'};
1.33      albertel  394: 
1.32      albertel  395:     if (!defined($queue)) {
                    396: 	$env{'form.queue'}=$queue='gradingqueue';
                    397:     }
1.33      albertel  398: 
1.32      albertel  399:     my $gradingkey=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.33      albertel  400: 
1.32      albertel  401:     if (defined($env{'form.queue'}) && defined($env{'form.gradingkey'})
1.33      albertel  402: 	&& !defined($env{'form.gradingaction'}) 
                    403: 	&& $env{'form.queuemode'} eq 'selected') {
                    404: 	
                    405: 	my $who=&queue_key_locked($queue,$gradingkey);
                    406: 	my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    407: 	if ($who eq $me) {
                    408: 	    &Apache::lonxml::debug("Found a key was given to me");
                    409: 	    return ($gradingkey,'selected');
                    410: 	} else {
                    411: 	    return (undef,'show_list');
                    412: 	}
                    413: 
                    414:     }
                    415: 
                    416:     if ($target eq 'webgrade' && $env{'form.queuemode'} eq 'selected') {
                    417: 	if ($env{'form.gradingaction'} eq 'resume') {
                    418: 	    delete($env{'form.gradingaction'});
                    419: 	    &Apache::lonxml::debug("Resuming a key");
1.32      albertel  420: 	    return ($gradingkey);
1.33      albertel  421: 	} elsif ($env{'form.gradingaction'} eq 'unlock') {
                    422: 	    &Apache::lonxml::debug("Unlocking a key ".
                    423: 				     &check_queue_unlock($queue,$gradingkey,1));
                    424: 	    return (undef,'unlock');
                    425: 	} elsif ($env{'form.gradingaction'} eq 'select') {
                    426: 	    &Apache::lonxml::debug("Locking a key");
                    427: 	    if (&lock_key($queue,$gradingkey)) {
                    428: 		&Apache::lonxml::debug("Success $queue");
                    429: 		return ($gradingkey);
                    430: 	    }
                    431: 	    &Apache::lonxml::debug("Failed $queue");
                    432: 	    return (undef,'lock_failed');
1.32      albertel  433: 	}
                    434:     }
1.33      albertel  435: 
                    436:     if ($env{'form.queuemode'} ne 'selected') {
                    437: 	# don't get something new from the queue if they hit the stop button
                    438:     	if (!($env{'form.stop'} && $target eq 'webgrade') 
                    439: 	    && !$env{'form.gradingaction'}) {
                    440: 	    &Apache::lonxml::debug("Getting anew $queue");
                    441: 	    return (&get_from_queue($queue));
                    442: 	} else {
                    443: 	    return (undef,'stop');
                    444: 	}
1.32      albertel  445:     }
1.33      albertel  446:     return (undef,undef)
1.32      albertel  447: }
                    448: 
1.1       albertel  449: sub end_Task {
                    450:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    451:     my $result='';
                    452:     my $status=$Apache::inputtags::status['-1'];
1.29      albertel  453:     my ($version,$previous)=&get_version();
1.1       albertel  454:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.15      albertel  455: 	$target eq 'tex') {
1.1       albertel  456: 	if (
1.11      albertel  457: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.1       albertel  458: 	    ($target eq 'answer') || ($target eq 'tex')
                    459: 	   ) {
                    460: 	    if ($target eq 'web') {
1.29      albertel  461: 		if ($status eq 'CAN_ANSWER' && !$previous) {
1.15      albertel  462: 		    $result.="\n".'<table border="1">'.
1.28      albertel  463: 			&Apache::inputtags::file_selector("$version.0",
                    464: 							  "bridgetask","*",
1.9       albertel  465: 							  'portfolioonly').
                    466: 							  "</table>";
                    467: 		    $result.=&Apache::inputtags::gradestatus('0');
                    468: 		}
1.13      albertel  469: 	    } 
                    470: 	    if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  471: 		$result.=&Apache::lonxml::xmlend().'</html>';
                    472: 	    }
                    473: 	}
1.29      albertel  474: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel  475: 	    my $award='SUBMITTED';
1.28      albertel  476: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel  477: 						    'portfiles',\$award);
1.14      albertel  478: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel  479: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                    480: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    481: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                    482: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                    483: 
                    484: 		$Apache::lonhomework::results{"resource.0.award"}=
                    485: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                    486: 		    $award;
1.10      albertel  487: 	    }
1.4       albertel  488: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    489: 	    &Apache::structuretags::finalize_storage();
1.14      albertel  490: 	    if ($award eq 'SUBMITTED') {
1.31      albertel  491: 		&add_to_queue('gradingqueue',$Apache::inputtags::slot_name);
1.14      albertel  492: 	    }
1.1       albertel  493: 	}
1.32      albertel  494: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes') {
1.20      albertel  495: 	    my $optional_required=
                    496: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                    497: 					   $safeeval);
                    498: 	    my $optional_passed=0;
                    499: 	    my $mandatory_failed=0;
                    500: 	    my $ungraded=0;
                    501: 	    my $review=0;   
1.21      albertel  502: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.20      albertel  503: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    504: 		my $status=
1.25      albertel  505: 		    $Apache::lonhomework::results{"resource.$version.$dim_id.status"};
1.20      albertel  506: 		my $mandatory=
                    507: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
                    508: 		if ($status eq 'pass') {
                    509: 		    if (!$mandatory) { $optional_passed++; }
                    510: 		} elsif ($status eq 'fail') {
                    511: 		    if ($mandatory) { $mandatory_failed++; }
                    512: 		} elsif ($status eq 'ungraded') {
                    513: 		    $ungraded++;
                    514: 		} elsif ($status eq 'review') {
                    515: 		    $review++;
                    516: 		}	
                    517: 	    }
                    518: 	    if ($optional_passed < $optional_required) {
                    519: 		$mandatory_failed++;
                    520: 	    }
1.21      albertel  521: 	    &Apache::lonxml::debug("all dim ".join(':',@Apache::bridgetask::dimensionlist)."results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
                    522: 	    $Apache::lonhomework::results{'resource.grader'}=
1.20      albertel  523: 		$env{'user.name'}.'@'.$env{'user.domain'};
                    524: 	    if ($review) {
1.25      albertel  525: 		$Apache::lonhomework::results{"resource.$version.status"}='review';
1.33      albertel  526: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    527: 		    &check_queue_unlock($env{'form.queue'});
                    528: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                    529: 		} else {
                    530: 		    &move_between_queues('gradingqueue','reviewqueue');
                    531: 		}
1.20      albertel  532: 	    } elsif ($ungraded) {
1.25      albertel  533: 		$Apache::lonhomework::results{"resource.$version.status"}='ungraded';
1.33      albertel  534: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    535: 		    &Apache::lonxml::debug("moving back.");
                    536: 		    &move_between_queues('reviewqueue','gradingqueue');
                    537: 		} else {
                    538: 		    &check_queue_unlock($env{'form.queue'});
                    539: 		}
1.20      albertel  540: 	    } elsif ($mandatory_failed) {
1.25      albertel  541: 		$Apache::lonhomework::results{"resource.$version.status"}='fail';
                    542: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                    543: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                    544: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.39      albertel  545: 		&remove_from_queue($env{'form.queue'}); 
                    546: 
                    547: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                    548: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    549: 		    $Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    550: 		&Apache::grades::version_portfiles(
                    551: 					\%Apache::lonhomework::results,
                    552: 					 ["$version.0.bridgetask"],$courseid,
                    553: 					 $symb,$udom,$uname,
                    554: 					 ["$version.0.bridgetask"]);
1.20      albertel  555: 	    } else {
1.25      albertel  556: 		$Apache::lonhomework::results{"resource.$version.status"}='pass';
                    557: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                    558: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                    559: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.32      albertel  560: 		&remove_from_queue($env{'form.queue'});
1.39      albertel  561: 
                    562: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                    563: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    564: 		    $Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    565: 		&Apache::grades::version_portfiles(
                    566: 					\%Apache::lonhomework::results,
                    567: 					 ["$version.0.bridgetask"],$courseid,
                    568: 					 $symb,$udom,$uname,
                    569: 					 ["$version.0.bridgetask"]);
1.20      albertel  570: 	    }
1.26      albertel  571: 	    $Apache::lonhomework::results{"resource.status"}=
                    572: 		$Apache::lonhomework::results{"resource.$version.status"};
1.28      albertel  573: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel  574: 		$Apache::lonhomework::results{"resource.0.award"}=
                    575: 		    $Apache::lonhomework::results{"resource.$version.award"};
                    576: 		$Apache::lonhomework::results{"resource.0.awarded"}=
                    577: 		    $Apache::lonhomework::results{"resource.$version.awarded"};
                    578: 		$Apache::lonhomework::results{"resource.0.solved"}=
                    579: 		    $Apache::lonhomework::results{"resource.$version.solved"};
1.25      albertel  580: 	    }
1.21      albertel  581: 	    &Apache::structuretags::finalize_storage();
1.20      albertel  582: 	}
1.15      albertel  583:     } elsif ($target eq 'webgrade') {
1.18      albertel  584: 	$result.="</table>\n<hr />";
1.20      albertel  585: 	#$result.='<input type="submit" name="next" value="'.
                    586: 	#    &mt('Save &amp; Next').'" /> ';
                    587: 	#$result.='<input type="submit" name="end" value="'.
                    588: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                    589: 	#$result.='<input type="submit" name="throwaway" value="'.
                    590: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                    591: 	#$result.='<input type="submit" name="save" value="'.
                    592: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.15      albertel  593: 	$result.='</form>'.&Apache::loncommon::endbodytag().'</html>';
1.1       albertel  594:     } elsif ($target eq 'meta') {
1.2       albertel  595: 	$result.='<parameter part="0" package="Task"></parameter>'."\n";
1.1       albertel  596: 	#$result.=&Apache::response::meta_part_order();
                    597: 	#$result.=&Apache::response::meta_response_order();
                    598:     }
1.4       albertel  599:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel  600:     return $result;
                    601: }
                    602: 
1.31      albertel  603: sub move_between_queues {
                    604:     my ($src_queue,$dest_queue)=@_;
                    605:     my $cur_data=&get_queue_data($src_queue);
                    606:     if (!$cur_data) { return 'not_exist'; }
                    607:     my $result=&add_to_queue($dest_queue,$cur_data->[0]);
                    608:     if ($result ne 'ok') {
                    609: 	return $result;
                    610:     }
                    611:     &check_queue_unlock($src_queue);
                    612:     return &remove_from_queue($src_queue);
1.21      albertel  613: }
                    614: 
                    615: sub check_queue_unlock {
1.32      albertel  616:     my ($queue,$key,$allow_not_me)=@_;
1.30      albertel  617:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.32      albertel  618:     if (!defined($key)) {
                    619: 	$key="$symb\0queue\0$uname\@$udom";
                    620:     }
1.30      albertel  621:     my $cnum=$env{'course.'.$cid.'.num'};
                    622:     my $cdom=$env{'course.'.$cid.'.domain'};
                    623:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    624:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                    625:     if  ($who eq $me) {
1.32      albertel  626: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                    627:     } elsif ($allow_not_me) {
1.33      albertel  628: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel  629: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel  630:     }
1.32      albertel  631:     return 'not_owner';
1.21      albertel  632: }
                    633: 
                    634: sub remove_from_queue {
1.30      albertel  635:     my ($queue)=@_;
1.27      albertel  636:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    637:     my $cnum=$env{'course.'.$cid.'.num'};
                    638:     my $cdom=$env{'course.'.$cid.'.domain'};
                    639:     my $key="$symb\0queue\0$uname\@$udom";
                    640:     my @keys=($key,"$key\0locked");
1.31      albertel  641:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel  642: }
                    643: 
1.16      albertel  644: sub setup_env_for_other_user {
                    645:     my ($queue_key,$safeeval)=@_;
                    646:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel  647:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel  648:     $env{'form.grade_symb'}=$symb;
                    649:     $env{'form.grade_domain'}=$udom;
                    650:     $env{'form.grade_username'}=$uname;
                    651:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                    652:     &Apache::lonxml::initialize_rndseed($safeeval);
                    653: }
                    654: 
1.31      albertel  655: sub get_queue_data {
                    656:     my ($queue)=@_;
                    657:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    658:     my $cnum=$env{'course.'.$cid.'.num'};
                    659:     my $cdom=$env{'course.'.$cid.'.domain'};
                    660:     my $todo="$symb\0queue\0$uname\@$udom";
                    661:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                    662:     if ($key eq $todo && ref($value)) {
                    663: 	return $value;
                    664:     }
                    665:     return undef;
                    666: }
                    667: 
1.14      albertel  668: sub add_to_queue {
1.31      albertel  669:     my ($queue,$slot_name)=@_;
1.14      albertel  670:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    671:     my $cnum=$env{'course.'.$cid.'.num'};
                    672:     my $cdom=$env{'course.'.$cid.'.domain'};
                    673:     my %data;
1.31      albertel  674:     $data{"$symb\0queue\0$uname\@$udom"}=[$slot_name];
                    675:     return &Apache::lonnet::put($queue,\%data,$cdom,$cnum);
1.14      albertel  676: }
                    677: 
                    678: sub show_queue {
1.32      albertel  679:     my ($queue,$with_selects)=@_;
1.14      albertel  680:     my $result;
                    681:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    682:     my $cnum=$env{'course.'.$cid.'.num'};
                    683:     my $cdom=$env{'course.'.$cid.'.domain'};
1.16      albertel  684:     my $regexp="^$symb\0";
1.30      albertel  685:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel  686:     my ($tmp)=%queue;
                    687:     if ($tmp=~/^error: 2 /) {
1.33      albertel  688: 	return "\n<h3>Current Queue - $queue</h3><table border='1'><tr><td>Empty</td></tr></table>";
1.31      albertel  689:     }
1.33      albertel  690:     $result.="\n<h3>Current Queue - $queue </h3><table border='1'><tr>";
1.32      albertel  691:     if ($with_selects) { $result.="<th></th>"; }
                    692:     $result.="<th>resource</th><th>user</th><th>type</th><th>data</th></tr>";
1.14      albertel  693:     foreach my $key (sort(keys(%queue))) {
1.32      albertel  694: 	if ($key=~/locked$/ && !$with_selects) {
1.16      albertel  695: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                    696: 	    my $title=&Apache::lonnet::gettitle($symb);
1.27      albertel  697: 	    $result.="<tr><td>$title</td><td>$uname</td>";
1.16      albertel  698: 	    $result.='<td>lock</td><td>'.$queue{$key}.'</td></tr>';
1.32      albertel  699: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.16      albertel  700: 	    my ($symb,undef) = split("\0",$key);
                    701: 	    my $title=&Apache::lonnet::gettitle($symb);
1.27      albertel  702: 	    $result.="<tr><td>$title</td><td></td>";
1.16      albertel  703: 	    $result.='<td>last queue modification time</td><td>'.
                    704: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel  705: 	} elsif ($key!~/(timestamp|locked)$/) {
1.16      albertel  706: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                    707: 	    my $title=&Apache::lonnet::gettitle($symb);
1.32      albertel  708: 	    $result.="<tr>";
1.35      albertel  709: 	    my $slot=$queue{$key}->[0];
                    710: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
1.32      albertel  711: 	    if ($with_selects) {
                    712: 		my $ekey=&Apache::lonnet::escape($key);
                    713: 		my ($action,$description)=('select',&mt('Select'));
                    714: 		if (exists($queue{"$key\0locked"})) {
                    715: 		    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    716: 		    if ($me eq $queue{"$key\0locked"}) {
                    717: 			($action,$description)=('resume',&mt('Resume'));
                    718: 		    } else {
                    719: 			($action,$description)=('unlock',&mt('Unlock'));
                    720: 		    }
                    721: 		}
1.35      albertel  722: 		if (time > $slot_data{'endtime'}) {
                    723: 		    $result.=(<<FORM);
1.32      albertel  724: <td>
                    725: <form method="POST">
                    726:  <input type="hidden" name="gradingkey" value="$ekey" />
                    727:  <input type="hidden" name="queue" value="$queue" />
                    728:  <input type="hidden" name="gradingaction" value="$action" />
                    729:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel  730:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel  731:  <input type="submit" name="submit" value="$description" />
                    732: </form>
                    733: </td>
                    734: FORM
1.35      albertel  735: 
                    736:                 } else {
                    737: 		    $result.='<td>'.&mt("In Progress").'</td>'
                    738: 		}
1.32      albertel  739: 	    }
                    740: 	    $result.="<td>$title</td><td>$uname</td>";
1.27      albertel  741: 	    $result.='<td>queue entry</td><td>Slot: '.$slot.' End time: '.
1.20      albertel  742: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
                    743: 		"</td></tr>";
1.16      albertel  744: 	}
1.14      albertel  745:     }
1.15      albertel  746:     $result.="</table><hr />\n";
1.14      albertel  747:     return $result;
                    748: }
                    749: 
1.34      albertel  750: sub get_queue_counts {
                    751:     my ($queue)=@_;
                    752:     my $result;
                    753:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    754:     my $cnum=$env{'course.'.$cid.'.num'};
                    755:     my $cdom=$env{'course.'.$cid.'.domain'};
                    756:     my $regexp="^$symb\0";
                    757:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                    758:     my ($tmp)=%queue;
                    759:     if ($tmp=~/^error: 2 /) {
                    760: 	return (0,0,0);
                    761:     }
                    762:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
                    763:     foreach my $key (sort(keys(%queue))) {
                    764: 	if ($key=~/locked$/) {
                    765: 	    $locks++;
                    766: 	} elsif ($key=~/timestamp$/) {
                    767: 	    #ignore
                    768: 	} elsif ($key!~/(timestamp|locked)$/) {
                    769: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                    770: 	    $entries++;
                    771: 	    my $slot=$queue{$key}->[0];
                    772: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                    773: 	    if (time > $slot_data{'endtime'}) {
                    774: 		$ready_to_grade++;
                    775: 	    }
                    776: 	}
                    777:     }
                    778:     return ($entries,$ready_to_grade,$locks);
                    779: }
                    780: 
1.14      albertel  781: sub decode_queue_key {
                    782:     my ($key)=@_;
                    783:     my ($symb,undef,$user) = split("\0",$key);
                    784:     my ($uname,$udom) = split('@',$user);
                    785:     return ($symb,$uname,$udom);
                    786: }
                    787: 
                    788: sub queue_key_locked {
1.30      albertel  789:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel  790:     if (!defined($cdom) || !defined($cnum)) {
                    791: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                    792: 	$cnum=$env{'course.'.$cid.'.num'};
                    793: 	$cdom=$env{'course.'.$cid.'.domain'};
                    794:     }
1.14      albertel  795:     my ($key_locked,$value)=
1.30      albertel  796: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel  797:     if ($key_locked eq "$key\0locked") {
                    798: 	return $value;
                    799:     }
                    800:     return undef;
                    801: }
                    802: 
                    803: sub pick_from_queue_data {
1.30      albertel  804:     my ($queue,$check_section,$queuedata,$cdom,$cnum)=@_;
                    805:     foreach my $key (keys(%$queuedata)) {
1.14      albertel  806: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                    807: 	if ($check_section) {
                    808: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel  809: 	    if ($section eq $check_section) {
1.33      albertel  810: 		&Apache::lonxml::debug("my sec");
1.15      albertel  811: 		next;
                    812: 	    }
1.14      albertel  813: 	}
1.30      albertel  814: 	my $slot=$queuedata->{$key}[0];
1.14      albertel  815: 	my %slot_data=&Apache::lonnet::get_slot($slot);
1.15      albertel  816: 	if ($slot_data{'endtime'} > time) { 
1.33      albertel  817: 	    &Apache::lonxml::debug("not time");
1.15      albertel  818: 	    next;
                    819: 	}
1.30      albertel  820: 	if (&queue_key_locked($queue,$key,$cdom,$cnum)) {
1.33      albertel  821: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel  822: 	    next;
                    823: 	}
1.14      albertel  824: 	return $key;
                    825:     }
                    826:     return undef;
                    827: }
                    828: 
1.15      albertel  829: sub find_mid_grade {
1.30      albertel  830:     my ($queue,$symb,$cdom,$cnum)=@_;
1.16      albertel  831:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.15      albertel  832:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    833:     if ($todo) {
1.30      albertel  834: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel  835: 	if ($who eq $me) { return $todo; }
                    836:     }
                    837:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel  838:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel  839:     foreach my $key (keys(%locks)) {
                    840: 	my $who=$locks{$key};
                    841: 	if ($who eq $me) {
                    842: 	    $todo=$key;
                    843: 	    $todo=~s/\0locked$//;
                    844: 	    return $todo;
                    845: 	}
                    846:     }
                    847:     return undef;
                    848: }
                    849: 
1.32      albertel  850: sub lock_key {
                    851:     my ($queue,$todo)=@_;
                    852:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    853:     my (undef,$cid)=&Apache::lonxml::whichuser();
                    854:     my $cnum=$env{'course.'.$cid.'.num'};
                    855:     my $cdom=$env{'course.'.$cid.'.domain'};
                    856:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                    857: 					$cdom,$cnum);
1.33      albertel  858:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel  859:     if ($success eq 'ok') {
                    860: 	return 1;
                    861:     }
                    862:     return 0;
                    863: }
                    864: 
1.14      albertel  865: sub get_from_queue {
1.30      albertel  866:     my ($queue)=@_;
1.14      albertel  867:     my $result;
                    868:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    869:     my $cnum=$env{'course.'.$cid.'.num'};
                    870:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel  871:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel  872:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel  873:     if ($todo) { return $todo; }
1.14      albertel  874:     while (1) {
                    875: 	my $starttime=time;
1.30      albertel  876: 	&Apache::lonnet::put($queue,{"$symb\0timestamp"=>$starttime},
1.14      albertel  877: 			     $cdom,$cnum);
1.33      albertel  878: 	&Apache::lonxml::debug("$starttime");
1.14      albertel  879: 	my $regexp="^$symb\0queue\0";
1.30      albertel  880: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel  881: 	#make a pass looking for a user _not_ in my section
1.14      albertel  882: 	if ($env{'request.course.sec'}) {
1.33      albertel  883: 	    &Apache::lonxml::debug("sce");
1.30      albertel  884: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                    885: 					\%queue,$cdom,$cnum);
1.33      albertel  886: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel  887: 	}
1.33      albertel  888: 	# no one _not_ in our section so look for any user that is
                    889: 	# ready for grading
1.14      albertel  890: 	if (!$todo) {
1.33      albertel  891: 	    &Apache::lonxml::debug("no sce");
1.31      albertel  892: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                    893: 					\%queue,$cdom,$cnum);
1.33      albertel  894: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel  895: 	}
                    896: 	# no user to grade 
                    897: 	if (!$todo) { last; }
1.33      albertel  898: 	&Apache::lonxml::debug("got $todo");
1.14      albertel  899: 	# otherwise found someone so lets try to lock them
1.32      albertel  900: 	# unless someone else already picked them
                    901: 	if (!&lock_key($queue,$todo)) { next; }
1.14      albertel  902: 	my (undef,$endtime)=
1.30      albertel  903: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel  904: 				 $cdom,$cnum);
1.33      albertel  905: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel  906: 	# someone else already modified the queue, 
                    907: 	# perhaps our picked user wass already fully graded between
                    908: 	# when we picked him and when we locked his record? so lets
                    909: 	# double check.
                    910: 	if ($endtime != $starttime) {
                    911: 	    my ($key,$value)=
1.30      albertel  912: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel  913: 				     $cdom,$cnum);
1.33      albertel  914: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel  915: 	    if ($key eq $todo && ref($value)) {
                    916: 	    } else {
1.30      albertel  917: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel  918: 				     $cdom,$cnum);
1.33      albertel  919: 		&Apache::lonxml::debug("del");
1.14      albertel  920: 		next;
                    921: 	    }
                    922: 	}
1.33      albertel  923: 	&Apache::lonxml::debug("last $todo");
1.14      albertel  924: 	last;
                    925:     }
                    926:     return $todo;
                    927: }
                    928: 
1.1       albertel  929: sub start_ClosingParagraph {
                    930:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    931:     my $result;
                    932:     if ($target eq 'web') {
1.13      albertel  933:     } elsif ($target eq 'webgrade') {
                    934: 	&Apache::lonxml::startredirection();
1.1       albertel  935:     }
                    936:     return $result;
                    937: }
                    938: 
                    939: sub end_ClosingParagraph {
                    940:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    941:     my $result;
                    942:     if ($target eq 'web') {
1.13      albertel  943:     } elsif ($target eq 'webgrade') {
                    944: 	&Apache::lonxml::endredirection();
1.1       albertel  945:     }
                    946:     return $result;
                    947: }
                    948: 
1.19      albertel  949: sub get_id {
                    950:     my ($parstack,$safeeval)=@_;
                    951:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                    952:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                    953:     return $id;
                    954: }
                    955: 
1.1       albertel  956: my %dimension;
                    957: sub start_Dimension {
                    958:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    959:     undef(%dimension);
1.19      albertel  960:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel  961:     $Apache::bridgetask::dimension=$dim_id;
                    962:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                    963:     undef(@Apache::bridgetask::instance);
1.20      albertel  964:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
                    965: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel  966:     return '';
                    967: }
                    968: 
1.13      albertel  969: sub get_instance {
                    970:     &Apache::response::pushrandomnumber();
                    971:     my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
1.29      albertel  972:     my $num=@order;
                    973:     my $version=&get_version();
                    974:     my $which=($version-1)%$num;
                    975:     return $order[$which];
1.13      albertel  976: }
                    977: 
1.18      albertel  978: {
                    979:     my $last_link;
                    980:     sub end_Dimension {
                    981: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    982: 	my $result;
1.25      albertel  983: 	my $dim=&get_id($parstack,$safeeval);
1.19      albertel  984: 	my $instance=&get_instance();
1.25      albertel  985: 	my $version=&get_version();
1.18      albertel  986: 	if ($target eq 'web') {
                    987: 	    $result=$dimension{'intro'}.$dimension{$instance.'.text'};
1.26      albertel  988: 	    if ($Apache::lonhomework::history{"resource.$version.status"} eq 'pass' ||
                    989: 		$Apache::lonhomework::history{"resource.$version.status"} eq 'fail') {
1.22      albertel  990: 		foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.25      albertel  991: 		    my $status=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"};
                    992: 		    my $comment=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"};
1.22      albertel  993: 		    $result.='<table border="1"><tr><td>';
                    994: 		    if ($status eq 'fail') {
                    995: 			$result.='<font color="red">'.&mt('Did not pass').'</font>';
                    996: 		    } elsif ($status eq 'pass') {
                    997: 			$result.='<font color="green">'.&mt('Passed').'</font>';
                    998: 		    } else {
                    999: 			&Apache::lonxml::error("Student viewing a graded bridgetask was show a status of $status");
                   1000: 		    }
                   1001: 		    $result.='</td></tr><tr><td>';
1.25      albertel 1002: 		    if ($Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}) {
                   1003: 			$result.=&mt('Comment from grader: ').$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}.'</td></tr><tr><td>';
1.22      albertel 1004: 		    }
                   1005: 		    $result.=$dimension{$instance.'.criteria.'.$id}.
                   1006: 			'</td></tr></table>';
                   1007: 		}
                   1008: 	    }
1.18      albertel 1009: 	} elsif ($target eq 'webgrade') {
                   1010: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1011: 		my $link='criteria_'.$instance.'_'.$id;
1.25      albertel 1012: 		my $status=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"};
1.19      albertel 1013: 		$result.='<tr><td width="100%" valign="top">'.
1.18      albertel 1014: 		    '<a name="'.$link.'" />'.
                   1015: 		    '<a name="next_'.$last_link.'" />'.
                   1016: 		    '<br /><textarea enabled="false" style="width:100%" rows="8" width="25" wrap="hard">'.$dimension{$instance.'.criteria.'.$id}.'</textarea>'.
                   1017: 		    #$dimension{$instance.'.criteria.'.$id}.
                   1018: 		    '</td>'.
1.19      albertel 1019: 		    '<td><nobr>Additional Comment for Student</nobr> <br />'.
1.25      albertel 1020: 		    '<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 1021: 		    '</td>'.
                   1022: 		    '<td>'.
1.30      albertel 1023: 		    '<nobr><label><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').'/>'.&mt('Ungraded').'</label></nobr><br />'.
                   1024: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.&mt('Pass').'</label><br />'.
                   1025: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label><br />'.
                   1026: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.&mt('Review').'</label><br />'.
1.18      albertel 1027: 		    '</td>'.
                   1028: 		    '<td>'.
                   1029: 		    '<a href="#'.$last_link.'">Prev</a><br />'.
1.20      albertel 1030: 		    '<a href="#next_'.$link.'">Next</a><br /><br /><br />'.
                   1031: 		    '<input type="submit" name="next" value="'.
                   1032: 		    &mt('Done').'" /> '.
1.30      albertel 1033: 		    '<input type="submit" name="stop" value="'.
                   1034: 		    &mt('Stop').'" /> '.
1.18      albertel 1035: 		    '</td></tr>';
1.22      albertel 1036: 		$result.='<tr><td colspan="4">';
                   1037: 		my (undef,undef,$udom,$uname) = &Apache::lonxml::whichuser();
                   1038: 		my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio';
1.43    ! albertel 1039: 		foreach my $partial_file (split(',',$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"})) {
1.22      albertel 1040: 		    my $file=$file_url.$partial_file;
                   1041: 		    &Apache::lonnet::allowuploaded('/adm/bridgetask',$file);
                   1042: 		    $result.='<a href="'.$file.'" target="lonGRDs"><img src="'.
                   1043: 			&Apache::loncommon::icon($file).'" border=0"> '.$file.
                   1044: 			'</a>';
                   1045: 		}
                   1046: 		$result.='</td></tr>';
1.18      albertel 1047: 		$last_link=$link;
                   1048: 	    }
1.22      albertel 1049: 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
1.19      albertel 1050: 	    my $optional_passed=0;
1.20      albertel 1051: 	    my $mandatory_failed=0;
                   1052: 	    my $ungraded=0;
                   1053: 	    my $review=0;
1.19      albertel 1054: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.25      albertel 1055: 		my $status=$Apache::lonhomework::results{"resource.$version.$dim.$instance.$id.status"}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
                   1056: 		$Apache::lonhomework::results{"resource.$version.$dim.$instance.$id.comment"}=$env{'form.HWVAL_comment_criteria_'.$instance.'_'.$id};
1.20      albertel 1057: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1058: 		if ($status eq 'pass') {
                   1059: 		    if (!$mandatory) { $optional_passed++; }
                   1060: 		} elsif ($status eq 'fail') {
                   1061: 		    if ($mandatory) { $mandatory_failed++; }
1.21      albertel 1062: 		} elsif ($status eq 'review') {
                   1063: 		    $review++;
1.20      albertel 1064: 		} elsif ($status eq 'ungraded') {
                   1065: 		    $ungraded++;
1.21      albertel 1066: 		} else {
1.22      albertel 1067: 		    &Apache::lonxml::error("got weird status --$status--");
1.19      albertel 1068: 		}
                   1069: 	    }
1.20      albertel 1070: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
                   1071: 		$mandatory_failed++;
                   1072: 	    }
1.21      albertel 1073: 	    &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 1074: 	    if ($review) {
1.25      albertel 1075: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1076: 		    'review';
1.20      albertel 1077: 	    } elsif ($ungraded) {
1.25      albertel 1078: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1079: 		    'ungraded';
1.20      albertel 1080: 	    } elsif ($mandatory_failed) {
1.25      albertel 1081: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1082: 		    'fail';
1.20      albertel 1083: 	    } else {
1.25      albertel 1084: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1085: 		    'pass';
1.20      albertel 1086: 	    }
1.13      albertel 1087: 	}
1.18      albertel 1088: 	return $result;
1.1       albertel 1089:     }
                   1090: }
                   1091: 
                   1092: sub start_IntroParagraph {
                   1093:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1094:     my $result;
1.13      albertel 1095:     if ($target eq 'web' || $target eq 'webgrade') {
                   1096: 	if ($tagstack->[-2] eq 'Dimension' || $target eq 'webgrade') {
1.1       albertel 1097: 	    &Apache::lonxml::startredirection();
                   1098: 	}
                   1099:     }
                   1100:     return $result;
                   1101: }
                   1102: 
                   1103: sub end_IntroParagraph {
                   1104:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1105:     my $result;
1.13      albertel 1106:     if ($target eq 'web' || $target eq 'webgrade') {
                   1107: 	if ($tagstack->[-2] eq 'Dimension' || $target eq 'webgrade') {
1.1       albertel 1108: 	    $dimension{'intro'}=&Apache::lonxml::endredirection();
                   1109: 	}
                   1110:     }
                   1111:     return $result;
                   1112: }
                   1113: 
                   1114: sub start_Instance {
                   1115:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel 1116:     my $id=&get_id($parstack,$safeeval);
                   1117:     push(@{$dimension{'instances'}},$id);
                   1118:     push(@Apache::bridgetask::instance,$id);
                   1119:     push(@Apache::bridgetask::instancelist,$id);
1.20      albertel 1120:     $dimension{$id.'.optionalrequired'}=
1.19      albertel 1121: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.1       albertel 1122:     return '';
                   1123: }
                   1124: 
                   1125: sub end_Instance {
                   1126:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1127:     return '';
                   1128: }
                   1129: 
                   1130: sub start_InstanceText {
                   1131:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.13      albertel 1132:     if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel 1133: 	&Apache::lonxml::startredirection();
                   1134:     }
                   1135:     return '';
                   1136: }
                   1137: 
                   1138: sub end_InstanceText {
                   1139:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel 1140:     my $instance_id=$Apache::bridgetask::instance[-1];
1.13      albertel 1141:     if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel 1142: 	$dimension{$instance_id.'.text'}=&Apache::lonxml::endredirection();
                   1143:     }
                   1144:     return '';
                   1145: }
                   1146: 
                   1147: sub start_Criteria {
                   1148:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.21      albertel 1149:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.1       albertel 1150: 	&Apache::lonxml::startredirection();
                   1151:     }
                   1152:     return '';
                   1153: }
                   1154: 
                   1155: sub end_Criteria {
                   1156:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel 1157:     my $instance_id=$Apache::bridgetask::instance[-1];
1.21      albertel 1158:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.1       albertel 1159: 	my $criteria=&Apache::lonxml::endredirection();
1.19      albertel 1160: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel 1161: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel 1162: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                   1163: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel 1164: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                   1165:     }
                   1166:     return '';
                   1167: }
                   1168: 
1.4       albertel 1169: sub proctor_validation_screen {
                   1170:     my ($slot) = @_;
                   1171:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5       albertel 1172:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.11      albertel 1173:     $user=$env{'form.proctorname'};
                   1174:     if ($env{'form.proctordomain'}) { $domain=$env{'form.proctordomain'}; }
1.4       albertel 1175:     my $msg;
1.11      albertel 1176:     if ($env{'form.proctorpassword'}) {
1.4       albertel 1177: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                   1178: 	    .'</font></p>';
                   1179:     }
                   1180:     my $result= (<<ENDCHECKOUT);
                   1181: <h2>Proctor Validation</h2>
                   1182:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   1183:     $msg
1.11      albertel 1184: <form name="checkout" method="POST" action="$env{'request.uri'}">
1.4       albertel 1185: <input type="hidden" name="validate" value="yes" />
                   1186: <input type="hidden" name="submitted" value="yes" />
                   1187: <table>
                   1188:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$user" /></td></tr>
                   1189:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.6       albertel 1190:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$domain" /></td></tr>
1.4       albertel 1191: </table>
                   1192: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
                   1193: Student who should be logged in is:<br />
1.5       albertel 1194: <img src="$url" /><br />
1.4       albertel 1195: </form>
                   1196: ENDCHECKOUT
                   1197:     return $result;
                   1198: }
                   1199: 
1.1       albertel 1200: 1;
                   1201: __END__

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