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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.48    ! albertel    4: # $Id: bridgetask.pm,v 1.47 2005/09/14 18:25:40 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.46      albertel  465: 							  'portfolioonly',
                    466: 							  '
                    467: <h2>'.&mt('Submit Portfolio Files for Grading').'</h2>
                    468: <p>'.&mt('Indicate the files from your portfolio to be evaluated in grading this task.').'</p>').
1.9       albertel  469: 							  "</table>";
                    470: 		    $result.=&Apache::inputtags::gradestatus('0');
                    471: 		}
1.13      albertel  472: 	    } 
                    473: 	    if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  474: 		$result.=&Apache::lonxml::xmlend().'</html>';
                    475: 	    }
                    476: 	}
1.29      albertel  477: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel  478: 	    my $award='SUBMITTED';
1.28      albertel  479: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel  480: 						    'portfiles',\$award);
1.14      albertel  481: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel  482: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                    483: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    484: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                    485: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                    486: 
                    487: 		$Apache::lonhomework::results{"resource.0.award"}=
                    488: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                    489: 		    $award;
1.10      albertel  490: 	    }
1.4       albertel  491: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    492: 	    &Apache::structuretags::finalize_storage();
1.14      albertel  493: 	    if ($award eq 'SUBMITTED') {
1.31      albertel  494: 		&add_to_queue('gradingqueue',$Apache::inputtags::slot_name);
1.14      albertel  495: 	    }
1.1       albertel  496: 	}
1.32      albertel  497: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes') {
1.20      albertel  498: 	    my $optional_required=
                    499: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                    500: 					   $safeeval);
                    501: 	    my $optional_passed=0;
                    502: 	    my $mandatory_failed=0;
                    503: 	    my $ungraded=0;
                    504: 	    my $review=0;   
1.21      albertel  505: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.20      albertel  506: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    507: 		my $status=
1.25      albertel  508: 		    $Apache::lonhomework::results{"resource.$version.$dim_id.status"};
1.20      albertel  509: 		my $mandatory=
                    510: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
                    511: 		if ($status eq 'pass') {
                    512: 		    if (!$mandatory) { $optional_passed++; }
                    513: 		} elsif ($status eq 'fail') {
                    514: 		    if ($mandatory) { $mandatory_failed++; }
                    515: 		} elsif ($status eq 'ungraded') {
                    516: 		    $ungraded++;
                    517: 		} elsif ($status eq 'review') {
                    518: 		    $review++;
                    519: 		}	
                    520: 	    }
                    521: 	    if ($optional_passed < $optional_required) {
                    522: 		$mandatory_failed++;
                    523: 	    }
1.21      albertel  524: 	    &Apache::lonxml::debug("all dim ".join(':',@Apache::bridgetask::dimensionlist)."results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
                    525: 	    $Apache::lonhomework::results{'resource.grader'}=
1.20      albertel  526: 		$env{'user.name'}.'@'.$env{'user.domain'};
                    527: 	    if ($review) {
1.25      albertel  528: 		$Apache::lonhomework::results{"resource.$version.status"}='review';
1.33      albertel  529: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    530: 		    &check_queue_unlock($env{'form.queue'});
                    531: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                    532: 		} else {
                    533: 		    &move_between_queues('gradingqueue','reviewqueue');
                    534: 		}
1.20      albertel  535: 	    } elsif ($ungraded) {
1.25      albertel  536: 		$Apache::lonhomework::results{"resource.$version.status"}='ungraded';
1.33      albertel  537: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    538: 		    &Apache::lonxml::debug("moving back.");
                    539: 		    &move_between_queues('reviewqueue','gradingqueue');
                    540: 		} else {
                    541: 		    &check_queue_unlock($env{'form.queue'});
                    542: 		}
1.20      albertel  543: 	    } elsif ($mandatory_failed) {
1.25      albertel  544: 		$Apache::lonhomework::results{"resource.$version.status"}='fail';
                    545: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                    546: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                    547: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.39      albertel  548: 		&remove_from_queue($env{'form.queue'}); 
                    549: 
                    550: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                    551: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    552: 		    $Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    553: 		&Apache::grades::version_portfiles(
                    554: 					\%Apache::lonhomework::results,
                    555: 					 ["$version.0.bridgetask"],$courseid,
                    556: 					 $symb,$udom,$uname,
                    557: 					 ["$version.0.bridgetask"]);
1.20      albertel  558: 	    } else {
1.25      albertel  559: 		$Apache::lonhomework::results{"resource.$version.status"}='pass';
                    560: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                    561: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                    562: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.32      albertel  563: 		&remove_from_queue($env{'form.queue'});
1.39      albertel  564: 
                    565: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                    566: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    567: 		    $Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    568: 		&Apache::grades::version_portfiles(
                    569: 					\%Apache::lonhomework::results,
                    570: 					 ["$version.0.bridgetask"],$courseid,
                    571: 					 $symb,$udom,$uname,
                    572: 					 ["$version.0.bridgetask"]);
1.20      albertel  573: 	    }
1.26      albertel  574: 	    $Apache::lonhomework::results{"resource.status"}=
                    575: 		$Apache::lonhomework::results{"resource.$version.status"};
1.28      albertel  576: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel  577: 		$Apache::lonhomework::results{"resource.0.award"}=
                    578: 		    $Apache::lonhomework::results{"resource.$version.award"};
                    579: 		$Apache::lonhomework::results{"resource.0.awarded"}=
                    580: 		    $Apache::lonhomework::results{"resource.$version.awarded"};
                    581: 		$Apache::lonhomework::results{"resource.0.solved"}=
                    582: 		    $Apache::lonhomework::results{"resource.$version.solved"};
1.25      albertel  583: 	    }
1.21      albertel  584: 	    &Apache::structuretags::finalize_storage();
1.20      albertel  585: 	}
1.15      albertel  586:     } elsif ($target eq 'webgrade') {
1.18      albertel  587: 	$result.="</table>\n<hr />";
1.20      albertel  588: 	#$result.='<input type="submit" name="next" value="'.
                    589: 	#    &mt('Save &amp; Next').'" /> ';
                    590: 	#$result.='<input type="submit" name="end" value="'.
                    591: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                    592: 	#$result.='<input type="submit" name="throwaway" value="'.
                    593: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                    594: 	#$result.='<input type="submit" name="save" value="'.
                    595: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.15      albertel  596: 	$result.='</form>'.&Apache::loncommon::endbodytag().'</html>';
1.1       albertel  597:     } elsif ($target eq 'meta') {
1.2       albertel  598: 	$result.='<parameter part="0" package="Task"></parameter>'."\n";
1.1       albertel  599: 	#$result.=&Apache::response::meta_part_order();
                    600: 	#$result.=&Apache::response::meta_response_order();
                    601:     }
1.4       albertel  602:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel  603:     return $result;
                    604: }
                    605: 
1.31      albertel  606: sub move_between_queues {
                    607:     my ($src_queue,$dest_queue)=@_;
                    608:     my $cur_data=&get_queue_data($src_queue);
                    609:     if (!$cur_data) { return 'not_exist'; }
                    610:     my $result=&add_to_queue($dest_queue,$cur_data->[0]);
                    611:     if ($result ne 'ok') {
                    612: 	return $result;
                    613:     }
                    614:     &check_queue_unlock($src_queue);
                    615:     return &remove_from_queue($src_queue);
1.21      albertel  616: }
                    617: 
                    618: sub check_queue_unlock {
1.32      albertel  619:     my ($queue,$key,$allow_not_me)=@_;
1.30      albertel  620:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.32      albertel  621:     if (!defined($key)) {
                    622: 	$key="$symb\0queue\0$uname\@$udom";
                    623:     }
1.30      albertel  624:     my $cnum=$env{'course.'.$cid.'.num'};
                    625:     my $cdom=$env{'course.'.$cid.'.domain'};
                    626:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    627:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                    628:     if  ($who eq $me) {
1.32      albertel  629: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                    630:     } elsif ($allow_not_me) {
1.33      albertel  631: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel  632: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel  633:     }
1.32      albertel  634:     return 'not_owner';
1.21      albertel  635: }
                    636: 
                    637: sub remove_from_queue {
1.30      albertel  638:     my ($queue)=@_;
1.27      albertel  639:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    640:     my $cnum=$env{'course.'.$cid.'.num'};
                    641:     my $cdom=$env{'course.'.$cid.'.domain'};
                    642:     my $key="$symb\0queue\0$uname\@$udom";
                    643:     my @keys=($key,"$key\0locked");
1.31      albertel  644:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel  645: }
                    646: 
1.16      albertel  647: sub setup_env_for_other_user {
                    648:     my ($queue_key,$safeeval)=@_;
                    649:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel  650:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel  651:     $env{'form.grade_symb'}=$symb;
                    652:     $env{'form.grade_domain'}=$udom;
                    653:     $env{'form.grade_username'}=$uname;
                    654:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                    655:     &Apache::lonxml::initialize_rndseed($safeeval);
                    656: }
                    657: 
1.31      albertel  658: sub get_queue_data {
                    659:     my ($queue)=@_;
                    660:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    661:     my $cnum=$env{'course.'.$cid.'.num'};
                    662:     my $cdom=$env{'course.'.$cid.'.domain'};
                    663:     my $todo="$symb\0queue\0$uname\@$udom";
                    664:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                    665:     if ($key eq $todo && ref($value)) {
                    666: 	return $value;
                    667:     }
                    668:     return undef;
                    669: }
                    670: 
1.14      albertel  671: sub add_to_queue {
1.31      albertel  672:     my ($queue,$slot_name)=@_;
1.14      albertel  673:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    674:     my $cnum=$env{'course.'.$cid.'.num'};
                    675:     my $cdom=$env{'course.'.$cid.'.domain'};
                    676:     my %data;
1.31      albertel  677:     $data{"$symb\0queue\0$uname\@$udom"}=[$slot_name];
                    678:     return &Apache::lonnet::put($queue,\%data,$cdom,$cnum);
1.14      albertel  679: }
                    680: 
                    681: sub show_queue {
1.32      albertel  682:     my ($queue,$with_selects)=@_;
1.14      albertel  683:     my $result;
                    684:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    685:     my $cnum=$env{'course.'.$cid.'.num'};
                    686:     my $cdom=$env{'course.'.$cid.'.domain'};
1.16      albertel  687:     my $regexp="^$symb\0";
1.30      albertel  688:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel  689:     my ($tmp)=%queue;
                    690:     if ($tmp=~/^error: 2 /) {
1.33      albertel  691: 	return "\n<h3>Current Queue - $queue</h3><table border='1'><tr><td>Empty</td></tr></table>";
1.31      albertel  692:     }
1.33      albertel  693:     $result.="\n<h3>Current Queue - $queue </h3><table border='1'><tr>";
1.32      albertel  694:     if ($with_selects) { $result.="<th></th>"; }
                    695:     $result.="<th>resource</th><th>user</th><th>type</th><th>data</th></tr>";
1.14      albertel  696:     foreach my $key (sort(keys(%queue))) {
1.32      albertel  697: 	if ($key=~/locked$/ && !$with_selects) {
1.16      albertel  698: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                    699: 	    my $title=&Apache::lonnet::gettitle($symb);
1.27      albertel  700: 	    $result.="<tr><td>$title</td><td>$uname</td>";
1.16      albertel  701: 	    $result.='<td>lock</td><td>'.$queue{$key}.'</td></tr>';
1.32      albertel  702: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.16      albertel  703: 	    my ($symb,undef) = split("\0",$key);
                    704: 	    my $title=&Apache::lonnet::gettitle($symb);
1.27      albertel  705: 	    $result.="<tr><td>$title</td><td></td>";
1.16      albertel  706: 	    $result.='<td>last queue modification time</td><td>'.
                    707: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel  708: 	} elsif ($key!~/(timestamp|locked)$/) {
1.16      albertel  709: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                    710: 	    my $title=&Apache::lonnet::gettitle($symb);
1.32      albertel  711: 	    $result.="<tr>";
1.35      albertel  712: 	    my $slot=$queue{$key}->[0];
                    713: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
1.32      albertel  714: 	    if ($with_selects) {
                    715: 		my $ekey=&Apache::lonnet::escape($key);
                    716: 		my ($action,$description)=('select',&mt('Select'));
                    717: 		if (exists($queue{"$key\0locked"})) {
                    718: 		    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    719: 		    if ($me eq $queue{"$key\0locked"}) {
                    720: 			($action,$description)=('resume',&mt('Resume'));
                    721: 		    } else {
                    722: 			($action,$description)=('unlock',&mt('Unlock'));
                    723: 		    }
                    724: 		}
1.35      albertel  725: 		if (time > $slot_data{'endtime'}) {
                    726: 		    $result.=(<<FORM);
1.32      albertel  727: <td>
                    728: <form method="POST">
                    729:  <input type="hidden" name="gradingkey" value="$ekey" />
                    730:  <input type="hidden" name="queue" value="$queue" />
                    731:  <input type="hidden" name="gradingaction" value="$action" />
                    732:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel  733:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel  734:  <input type="submit" name="submit" value="$description" />
                    735: </form>
                    736: </td>
                    737: FORM
1.35      albertel  738: 
                    739:                 } else {
                    740: 		    $result.='<td>'.&mt("In Progress").'</td>'
                    741: 		}
1.32      albertel  742: 	    }
                    743: 	    $result.="<td>$title</td><td>$uname</td>";
1.27      albertel  744: 	    $result.='<td>queue entry</td><td>Slot: '.$slot.' End time: '.
1.20      albertel  745: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
                    746: 		"</td></tr>";
1.16      albertel  747: 	}
1.14      albertel  748:     }
1.15      albertel  749:     $result.="</table><hr />\n";
1.14      albertel  750:     return $result;
                    751: }
                    752: 
1.34      albertel  753: sub get_queue_counts {
                    754:     my ($queue)=@_;
                    755:     my $result;
                    756:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    757:     my $cnum=$env{'course.'.$cid.'.num'};
                    758:     my $cdom=$env{'course.'.$cid.'.domain'};
                    759:     my $regexp="^$symb\0";
                    760:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                    761:     my ($tmp)=%queue;
                    762:     if ($tmp=~/^error: 2 /) {
                    763: 	return (0,0,0);
                    764:     }
                    765:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
                    766:     foreach my $key (sort(keys(%queue))) {
                    767: 	if ($key=~/locked$/) {
                    768: 	    $locks++;
                    769: 	} elsif ($key=~/timestamp$/) {
                    770: 	    #ignore
                    771: 	} elsif ($key!~/(timestamp|locked)$/) {
                    772: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                    773: 	    $entries++;
                    774: 	    my $slot=$queue{$key}->[0];
                    775: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                    776: 	    if (time > $slot_data{'endtime'}) {
                    777: 		$ready_to_grade++;
                    778: 	    }
                    779: 	}
                    780:     }
                    781:     return ($entries,$ready_to_grade,$locks);
                    782: }
                    783: 
1.14      albertel  784: sub decode_queue_key {
                    785:     my ($key)=@_;
                    786:     my ($symb,undef,$user) = split("\0",$key);
                    787:     my ($uname,$udom) = split('@',$user);
                    788:     return ($symb,$uname,$udom);
                    789: }
                    790: 
                    791: sub queue_key_locked {
1.30      albertel  792:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel  793:     if (!defined($cdom) || !defined($cnum)) {
                    794: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                    795: 	$cnum=$env{'course.'.$cid.'.num'};
                    796: 	$cdom=$env{'course.'.$cid.'.domain'};
                    797:     }
1.14      albertel  798:     my ($key_locked,$value)=
1.30      albertel  799: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel  800:     if ($key_locked eq "$key\0locked") {
                    801: 	return $value;
                    802:     }
                    803:     return undef;
                    804: }
                    805: 
                    806: sub pick_from_queue_data {
1.30      albertel  807:     my ($queue,$check_section,$queuedata,$cdom,$cnum)=@_;
                    808:     foreach my $key (keys(%$queuedata)) {
1.14      albertel  809: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                    810: 	if ($check_section) {
                    811: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel  812: 	    if ($section eq $check_section) {
1.33      albertel  813: 		&Apache::lonxml::debug("my sec");
1.15      albertel  814: 		next;
                    815: 	    }
1.14      albertel  816: 	}
1.30      albertel  817: 	my $slot=$queuedata->{$key}[0];
1.14      albertel  818: 	my %slot_data=&Apache::lonnet::get_slot($slot);
1.15      albertel  819: 	if ($slot_data{'endtime'} > time) { 
1.33      albertel  820: 	    &Apache::lonxml::debug("not time");
1.15      albertel  821: 	    next;
                    822: 	}
1.30      albertel  823: 	if (&queue_key_locked($queue,$key,$cdom,$cnum)) {
1.33      albertel  824: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel  825: 	    next;
                    826: 	}
1.14      albertel  827: 	return $key;
                    828:     }
                    829:     return undef;
                    830: }
                    831: 
1.15      albertel  832: sub find_mid_grade {
1.30      albertel  833:     my ($queue,$symb,$cdom,$cnum)=@_;
1.16      albertel  834:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.15      albertel  835:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    836:     if ($todo) {
1.30      albertel  837: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel  838: 	if ($who eq $me) { return $todo; }
                    839:     }
                    840:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel  841:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel  842:     foreach my $key (keys(%locks)) {
                    843: 	my $who=$locks{$key};
                    844: 	if ($who eq $me) {
                    845: 	    $todo=$key;
                    846: 	    $todo=~s/\0locked$//;
                    847: 	    return $todo;
                    848: 	}
                    849:     }
                    850:     return undef;
                    851: }
                    852: 
1.32      albertel  853: sub lock_key {
                    854:     my ($queue,$todo)=@_;
                    855:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    856:     my (undef,$cid)=&Apache::lonxml::whichuser();
                    857:     my $cnum=$env{'course.'.$cid.'.num'};
                    858:     my $cdom=$env{'course.'.$cid.'.domain'};
                    859:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                    860: 					$cdom,$cnum);
1.33      albertel  861:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel  862:     if ($success eq 'ok') {
                    863: 	return 1;
                    864:     }
                    865:     return 0;
                    866: }
                    867: 
1.14      albertel  868: sub get_from_queue {
1.30      albertel  869:     my ($queue)=@_;
1.14      albertel  870:     my $result;
                    871:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    872:     my $cnum=$env{'course.'.$cid.'.num'};
                    873:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel  874:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel  875:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel  876:     if ($todo) { return $todo; }
1.14      albertel  877:     while (1) {
                    878: 	my $starttime=time;
1.30      albertel  879: 	&Apache::lonnet::put($queue,{"$symb\0timestamp"=>$starttime},
1.14      albertel  880: 			     $cdom,$cnum);
1.33      albertel  881: 	&Apache::lonxml::debug("$starttime");
1.14      albertel  882: 	my $regexp="^$symb\0queue\0";
1.30      albertel  883: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel  884: 	#make a pass looking for a user _not_ in my section
1.14      albertel  885: 	if ($env{'request.course.sec'}) {
1.33      albertel  886: 	    &Apache::lonxml::debug("sce");
1.30      albertel  887: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                    888: 					\%queue,$cdom,$cnum);
1.33      albertel  889: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel  890: 	}
1.33      albertel  891: 	# no one _not_ in our section so look for any user that is
                    892: 	# ready for grading
1.14      albertel  893: 	if (!$todo) {
1.33      albertel  894: 	    &Apache::lonxml::debug("no sce");
1.31      albertel  895: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                    896: 					\%queue,$cdom,$cnum);
1.33      albertel  897: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel  898: 	}
                    899: 	# no user to grade 
                    900: 	if (!$todo) { last; }
1.33      albertel  901: 	&Apache::lonxml::debug("got $todo");
1.14      albertel  902: 	# otherwise found someone so lets try to lock them
1.32      albertel  903: 	# unless someone else already picked them
                    904: 	if (!&lock_key($queue,$todo)) { next; }
1.14      albertel  905: 	my (undef,$endtime)=
1.30      albertel  906: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel  907: 				 $cdom,$cnum);
1.33      albertel  908: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel  909: 	# someone else already modified the queue, 
                    910: 	# perhaps our picked user wass already fully graded between
                    911: 	# when we picked him and when we locked his record? so lets
                    912: 	# double check.
                    913: 	if ($endtime != $starttime) {
                    914: 	    my ($key,$value)=
1.30      albertel  915: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel  916: 				     $cdom,$cnum);
1.33      albertel  917: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel  918: 	    if ($key eq $todo && ref($value)) {
                    919: 	    } else {
1.30      albertel  920: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel  921: 				     $cdom,$cnum);
1.33      albertel  922: 		&Apache::lonxml::debug("del");
1.14      albertel  923: 		next;
                    924: 	    }
                    925: 	}
1.33      albertel  926: 	&Apache::lonxml::debug("last $todo");
1.14      albertel  927: 	last;
                    928:     }
                    929:     return $todo;
                    930: }
                    931: 
1.1       albertel  932: sub start_ClosingParagraph {
                    933:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    934:     my $result;
                    935:     if ($target eq 'web') {
1.13      albertel  936:     } elsif ($target eq 'webgrade') {
                    937: 	&Apache::lonxml::startredirection();
1.1       albertel  938:     }
                    939:     return $result;
                    940: }
                    941: 
                    942: sub end_ClosingParagraph {
                    943:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    944:     my $result;
                    945:     if ($target eq 'web') {
1.13      albertel  946:     } elsif ($target eq 'webgrade') {
                    947: 	&Apache::lonxml::endredirection();
1.1       albertel  948:     }
                    949:     return $result;
                    950: }
                    951: 
1.19      albertel  952: sub get_id {
                    953:     my ($parstack,$safeeval)=@_;
                    954:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                    955:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                    956:     return $id;
                    957: }
                    958: 
1.1       albertel  959: my %dimension;
                    960: sub start_Dimension {
                    961:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    962:     undef(%dimension);
1.19      albertel  963:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel  964:     $Apache::bridgetask::dimension=$dim_id;
                    965:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                    966:     undef(@Apache::bridgetask::instance);
1.20      albertel  967:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
                    968: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel  969:     return '';
                    970: }
                    971: 
1.13      albertel  972: sub get_instance {
                    973:     &Apache::response::pushrandomnumber();
                    974:     my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
1.29      albertel  975:     my $num=@order;
                    976:     my $version=&get_version();
                    977:     my $which=($version-1)%$num;
                    978:     return $order[$which];
1.13      albertel  979: }
                    980: 
1.18      albertel  981: {
                    982:     my $last_link;
                    983:     sub end_Dimension {
                    984: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    985: 	my $result;
1.25      albertel  986: 	my $dim=&get_id($parstack,$safeeval);
1.19      albertel  987: 	my $instance=&get_instance();
1.25      albertel  988: 	my $version=&get_version();
1.18      albertel  989: 	if ($target eq 'web') {
1.47      albertel  990: 	    @Apache::scripttag::parser_env = @_;
                    991: 	    $result.=&Apache::scripttag::xmlparse($dimension{'intro'});
                    992: 	    @Apache::scripttag::parser_env = @_;
                    993: 	    $result.=&Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.26      albertel  994: 	    if ($Apache::lonhomework::history{"resource.$version.status"} eq 'pass' ||
                    995: 		$Apache::lonhomework::history{"resource.$version.status"} eq 'fail') {
1.22      albertel  996: 		foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.25      albertel  997: 		    my $status=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"};
                    998: 		    my $comment=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"};
1.22      albertel  999: 		    $result.='<table border="1"><tr><td>';
                   1000: 		    if ($status eq 'fail') {
                   1001: 			$result.='<font color="red">'.&mt('Did not pass').'</font>';
                   1002: 		    } elsif ($status eq 'pass') {
                   1003: 			$result.='<font color="green">'.&mt('Passed').'</font>';
                   1004: 		    } else {
                   1005: 			&Apache::lonxml::error("Student viewing a graded bridgetask was show a status of $status");
                   1006: 		    }
                   1007: 		    $result.='</td></tr><tr><td>';
1.25      albertel 1008: 		    if ($Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}) {
                   1009: 			$result.=&mt('Comment from grader: ').$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.comment"}.'</td></tr><tr><td>';
1.22      albertel 1010: 		    }
                   1011: 		    $result.=$dimension{$instance.'.criteria.'.$id}.
                   1012: 			'</td></tr></table>';
                   1013: 		}
                   1014: 	    }
1.18      albertel 1015: 	} elsif ($target eq 'webgrade') {
1.47      albertel 1016: 	    # in case of any side effects that we need
                   1017: 	    @Apache::scripttag::parser_env = @_;
                   1018: 	    &Apache::scripttag::xmlparse($dimension{'intro'});
                   1019: 	    @Apache::scripttag::parser_env = @_;
                   1020: 	    &Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.18      albertel 1021: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1022: 		my $link='criteria_'.$instance.'_'.$id;
1.25      albertel 1023: 		my $status=$Apache::lonhomework::history{"resource.$version.$dim.$instance.$id.status"};
1.19      albertel 1024: 		$result.='<tr><td width="100%" valign="top">'.
1.18      albertel 1025: 		    '<a name="'.$link.'" />'.
                   1026: 		    '<a name="next_'.$last_link.'" />'.
1.47      albertel 1027: 		    '<br /><textarea enabled="false" style="width:100%" rows="8" width="25" wrap="hard">';
                   1028: 		@Apache::scripttag::parser_env = @_;
                   1029: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
                   1030: 		$result.='</textarea>'.
1.18      albertel 1031: 		    #$dimension{$instance.'.criteria.'.$id}.
                   1032: 		    '</td>'.
1.19      albertel 1033: 		    '<td><nobr>Additional Comment for Student</nobr> <br />'.
1.25      albertel 1034: 		    '<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 1035: 		    '</td>'.
                   1036: 		    '<td>'.
1.47      albertel 1037: 		    '<nobr><label><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.&mt('Ungraded').'</label></nobr><br />'.
1.30      albertel 1038: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.&mt('Pass').'</label><br />'.
                   1039: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label><br />'.
                   1040: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.&mt('Review').'</label><br />'.
1.18      albertel 1041: 		    '</td>'.
                   1042: 		    '<td>'.
                   1043: 		    '<a href="#'.$last_link.'">Prev</a><br />'.
1.20      albertel 1044: 		    '<a href="#next_'.$link.'">Next</a><br /><br /><br />'.
                   1045: 		    '<input type="submit" name="next" value="'.
                   1046: 		    &mt('Done').'" /> '.
1.30      albertel 1047: 		    '<input type="submit" name="stop" value="'.
                   1048: 		    &mt('Stop').'" /> '.
1.18      albertel 1049: 		    '</td></tr>';
1.22      albertel 1050: 		$result.='<tr><td colspan="4">';
                   1051: 		my (undef,undef,$udom,$uname) = &Apache::lonxml::whichuser();
                   1052: 		my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio';
1.43      albertel 1053: 		foreach my $partial_file (split(',',$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"})) {
1.22      albertel 1054: 		    my $file=$file_url.$partial_file;
                   1055: 		    &Apache::lonnet::allowuploaded('/adm/bridgetask',$file);
1.48    ! albertel 1056: 		    $result.='<nobr><a href="'.$file.'" target="lonGRDs"><img src="'.
1.22      albertel 1057: 			&Apache::loncommon::icon($file).'" border=0"> '.$file.
1.48    ! albertel 1058: 			'</a></nobr> ';
1.22      albertel 1059: 		}
                   1060: 		$result.='</td></tr>';
1.18      albertel 1061: 		$last_link=$link;
                   1062: 	    }
1.22      albertel 1063: 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
1.19      albertel 1064: 	    my $optional_passed=0;
1.20      albertel 1065: 	    my $mandatory_failed=0;
                   1066: 	    my $ungraded=0;
                   1067: 	    my $review=0;
1.19      albertel 1068: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.25      albertel 1069: 		my $status=$Apache::lonhomework::results{"resource.$version.$dim.$instance.$id.status"}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
                   1070: 		$Apache::lonhomework::results{"resource.$version.$dim.$instance.$id.comment"}=$env{'form.HWVAL_comment_criteria_'.$instance.'_'.$id};
1.20      albertel 1071: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1072: 		if ($status eq 'pass') {
                   1073: 		    if (!$mandatory) { $optional_passed++; }
                   1074: 		} elsif ($status eq 'fail') {
                   1075: 		    if ($mandatory) { $mandatory_failed++; }
1.21      albertel 1076: 		} elsif ($status eq 'review') {
                   1077: 		    $review++;
1.20      albertel 1078: 		} elsif ($status eq 'ungraded') {
                   1079: 		    $ungraded++;
1.21      albertel 1080: 		} else {
1.47      albertel 1081: 		    $ungraded++;
1.19      albertel 1082: 		}
                   1083: 	    }
1.20      albertel 1084: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
                   1085: 		$mandatory_failed++;
                   1086: 	    }
1.21      albertel 1087: 	    &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 1088: 	    if ($review) {
1.25      albertel 1089: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1090: 		    'review';
1.20      albertel 1091: 	    } elsif ($ungraded) {
1.25      albertel 1092: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1093: 		    'ungraded';
1.20      albertel 1094: 	    } elsif ($mandatory_failed) {
1.25      albertel 1095: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1096: 		    'fail';
1.20      albertel 1097: 	    } else {
1.25      albertel 1098: 		$Apache::lonhomework::results{"resource.$version.$dim.status"}=
1.22      albertel 1099: 		    'pass';
1.20      albertel 1100: 	    }
1.13      albertel 1101: 	}
1.18      albertel 1102: 	return $result;
1.1       albertel 1103:     }
                   1104: }
                   1105: 
                   1106: sub start_IntroParagraph {
                   1107:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1108:     my $result;
1.13      albertel 1109:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1110: 	if ($tagstack->[-2] eq 'Dimension') {
                   1111: 	    $dimension{'intro'}=&Apache::lonxml::get_all_text('/introparagraph',$parser);
                   1112: 	
                   1113: 	} elsif ($target eq 'webgrade') {
                   1114: 	    &Apache::lonxml::get_all_text('/introparagraph',$parser);
1.1       albertel 1115: 	}
1.47      albertel 1116: 	
1.1       albertel 1117:     }
                   1118:     return $result;
                   1119: }
                   1120: 
                   1121: sub end_IntroParagraph {
                   1122: }
                   1123: 
                   1124: sub start_Instance {
                   1125:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel 1126:     my $id=&get_id($parstack,$safeeval);
                   1127:     push(@{$dimension{'instances'}},$id);
                   1128:     push(@Apache::bridgetask::instance,$id);
                   1129:     push(@Apache::bridgetask::instancelist,$id);
1.20      albertel 1130:     $dimension{$id.'.optionalrequired'}=
1.19      albertel 1131: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.1       albertel 1132:     return '';
                   1133: }
                   1134: 
                   1135: sub end_Instance {
                   1136: }
                   1137: 
                   1138: sub start_InstanceText {
                   1139:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.47      albertel 1140:     my $instance_id=$Apache::bridgetask::instance[-1];
                   1141:     my $text=&Apache::lonxml::get_all_text('/instancetext',$parser);
1.13      albertel 1142:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1143: 	$dimension{$instance_id.'.text'}=$text;
1.1       albertel 1144:     }
                   1145:     return '';
                   1146: }
                   1147: 
                   1148: sub end_InstanceText {
                   1149:     return '';
                   1150: }
                   1151: 
                   1152: sub start_Criteria {
                   1153:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel 1154:     my $instance_id=$Apache::bridgetask::instance[-1];
1.47      albertel 1155:     my $criteria=&Apache::lonxml::get_all_text('/criteria',$parser);
1.21      albertel 1156:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.19      albertel 1157: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel 1158: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel 1159: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                   1160: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel 1161: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                   1162:     }
                   1163:     return '';
                   1164: }
                   1165: 
1.47      albertel 1166: sub end_Criteria {
                   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.44      albertel 1173:     my $name=&Apache::loncommon::plainname($user,$domain);
                   1174:     
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:     }
1.47      albertel 1180:     if (!$env{'form.proctordomain'}) { $env{'form.proctordomain'}=$domain; }
1.4       albertel 1181:     my $result= (<<ENDCHECKOUT);
                   1182: <h2>Proctor Validation</h2>
                   1183:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   1184:     $msg
1.11      albertel 1185: <form name="checkout" method="POST" action="$env{'request.uri'}">
1.4       albertel 1186: <input type="hidden" name="validate" value="yes" />
                   1187: <input type="hidden" name="submitted" value="yes" />
                   1188: <table>
1.44      albertel 1189:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$env{'form.proctorname'}" /></td></tr>
1.4       albertel 1190:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.46      albertel 1191:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$env{'form.proctordomain'}" /></td></tr>
1.4       albertel 1192: </table>
                   1193: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
1.44      albertel 1194: <table border="1">
                   1195:   <tr><td>
                   1196:     <table>
                   1197:       <tr><td colspan="2">Student who should be logged in is:</td></tr>
                   1198:       <tr><td>Name:</td><td>$name</td></tr>
1.45      albertel 1199:       <tr><td>Student ID:</td><td>$env{'environment.id'}</td></tr>
1.44      albertel 1200:       <tr><td>Usename</td><td>$user\@$domain</td></tr>
                   1201:       <tr><td colspan="2"><img src="$url" /></td></tr>
                   1202:     </table>
                   1203:   </tr></td>
                   1204: </table>
1.4       albertel 1205: </form>
                   1206: ENDCHECKOUT
                   1207:     return $result;
                   1208: }
                   1209: 
1.1       albertel 1210: 1;
                   1211: __END__

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