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

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

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