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

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

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