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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.119   ! albertel    4: # $Id: bridgetask.pm,v 1.118 2006/03/06 19:46:59 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.81      albertel   63:     my ($slot_name,$slot,$type)=@_;
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.72      albertel   71: 	    my $authenticated=0;
                     72: 	    if ( $slot->{'secret'} =~ /\S/ &&
                     73: 		 $env{'form.proctorpassword'} eq $slot->{'secret'} ) {
                     74: 		$authenticated=1;
                     75: 	    } else {
                     76: 		
                     77: 		my $authhost=&Apache::lonnet::authenticate($puser,$env{'form.proctorpassword'},$pdom);
                     78: 		if ($authhost ne 'no_host') {
                     79: 		    $authenticated=1;
                     80: 		}
                     81: 	    }
1.81      albertel   82: 	    if ($authenticated && $type eq 'Task') {
1.93      albertel   83: 		# increment version
1.23      albertel   84: 		my $version=
1.90      albertel   85: 		    $Apache::lonhomework::history{'resource.0.version'};
                     86: 		$version++;
1.93      albertel   87: 
                     88: 		#clean out all current results
                     89: 		foreach my $key (keys(%Apache::lonhomework::history)) {
                     90: 		    if ($key=~/^resource\.0\./) {
                     91: 			$Apache::lonhomework::results{$key}='';
                     92: 		    }
                     93: 		}
                     94: 		
                     95: 		#setup new version and who did it
1.90      albertel   96: 		$Apache::lonhomework::results{'resource.0.version'}=$version;
1.89      albertel   97: 		$Apache::lonhomework::results{"resource.$version.0.checkedin"}=
1.4       albertel   98: 		    $user.'@'.$domain;
1.89      albertel   99: 		$Apache::lonhomework::results{"resource.$version.0.checkedin.slot"}=
1.22      albertel  100: 		    $slot_name;
1.93      albertel  101: 
1.4       albertel  102: 		return 1;
1.81      albertel  103: 	    } elsif ($authenticated && $type eq 'problem') {
1.90      albertel  104: 		&Apache::lonxml::debug("authed $slot_name");
1.81      albertel  105: 		$Apache::lonhomework::results{"resource.0.checkedin"}=
                    106: 		    $user.'@'.$domain;
                    107: 		$Apache::lonhomework::results{"resource.0.checkedin.slot"}=
                    108: 		    $slot_name;
1.4       albertel  109: 	    }
                    110: 	}
                    111:     }
                    112:     return 0;
                    113: }
                    114: 
1.25      albertel  115: sub get_version {
1.29      albertel  116:     my ($version,$previous);
1.25      albertel  117:     if ($env{'form.previousversion'} && 
1.36      albertel  118: 	$env{'form.previousversion'} ne 'current' &&
1.89      albertel  119: 	defined($Apache::lonhomework::history{'resource.'.$env{'form.previousversion'}.'.0.status'})) {
1.29      albertel  120: 	$version=$env{'form.previousversion'};
                    121: 	$previous=1;
                    122:     } else {
1.89      albertel  123: 	$version=$Apache::lonhomework::history{'resource.0.version'};
1.29      albertel  124: 	$previous=0;
                    125:     }
                    126:     if (wantarray) {
                    127: 	return ($version,$previous);
1.25      albertel  128:     }
1.29      albertel  129:     return $version;
1.25      albertel  130: }
                    131: 
1.8       albertel  132: sub add_previous_version_button {
1.25      albertel  133:     my ($status)=@_;
1.8       albertel  134:     my $result;
1.89      albertel  135:     if ($Apache::lonhomework::history{'resource.0.version'} eq '') {
1.25      albertel  136: 	return '';
                    137:     }
1.89      albertel  138:     if ($Apache::lonhomework::history{'resource.0.version'} < 2 &&
1.29      albertel  139: 	$status ne 'NEEDS_CHECKIN') {
1.25      albertel  140: 	return '';
                    141:     }
1.29      albertel  142:     my $version=&get_version();
                    143:     if ($env{'form.previousversion'} ne '' &&
                    144: 	$env{'form.previousversion'} eq $version) {
                    145: 	$result.="<h3>".&mt("Showing previous version [_1]",$version).
                    146: 	    "</h3>\n";
                    147:     }
                    148:     my @to_show;
1.89      albertel  149:     foreach my $test_version (1..$Apache::lonhomework::history{'resource.0.version'}) {
                    150: 	if (defined($Apache::lonhomework::history{'resource.'.$test_version.'.0.status'})) {
1.29      albertel  151: 	    push(@to_show,$test_version);
                    152: 	}
                    153:     }
                    154:     my $list='<option>'.
                    155: 	join("</option>\n<option>",@to_show).
                    156: 	     "</option>\n";
1.36      albertel  157:     $list.='<option value="current">'.&mt('Current').'</option>';
1.115     albertel  158:     $result.='<form name="getprevious" method="post" action="';
1.29      albertel  159:     my $uri=$env{'request.uri'};
                    160:     if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    161:     $result.=$uri.'">'.
                    162: 	&mt(' Show a previously done version: [_1]','<select onchange="this.form.submit()" name="previousversion">
                    163: <option>'.&mt('Pick one').'</option>
                    164: '.$list.'
                    165: </select>')."</form>";
1.8       albertel  166:     return $result;
                    167: }
                    168: 
1.13      albertel  169: sub add_grading_button {
1.59      albertel  170:     my (undef,$cid)=&Apache::lonxml::whichuser();
                    171:     my $cnum=$env{'course.'.$cid.'.num'};
                    172:     my $cdom=$env{'course.'.$cid.'.domain'};
                    173:     my %sections;
                    174:     my $numsections=&Apache::loncommon::get_sections($cdom,$cnum,\%sections);
                    175:     my $size=5;
                    176:     if (scalar(keys(%sections)) < 3) {
                    177: 	$size=scalar(keys(%sections))+2;
                    178:     }
                    179:     my $sec_select = '<select multiple name="chosensections" size="'.$size.'">'."\n";
                    180:     $sec_select .= "<option value='all' selected='selected'>all</option>\n";
                    181:     foreach my $sec (sort {lc($a) cmp lc($b)} (keys(%sections))) {
                    182: 	$sec_select .= "<option name=\"$sec\">$sec</option>\n";
                    183:     }
                    184:     $sec_select .= "<option value='none'>none</option></select>\n";
                    185:     
1.29      albertel  186:     my $result=' <input type="submit" name="gradeasubmission" value="'.
1.13      albertel  187: 	&mt("Get a submission to grade").'" />';
                    188:     $result.='<input type="hidden" name="grade_target" value="webgrade" />';
1.40      albertel  189:     if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.34      albertel  190: 	my ($entries,$ready,$locks)=&get_queue_counts('gradingqueue');
1.106     albertel  191: 	$result.='<table><tr>';
                    192: 	$result.='<td rowspan="4">Specify a section: </td><td rowspan="4">'.$sec_select.'</td>';
                    193: 	$result.='<td>'.' <input type="submit" name="reviewagrading" value="'.
                    194: 	    &mt("Select an entry from the grading queue:").'" /> ';
1.34      albertel  195: 
1.107     albertel  196: 	$result.= &mt("[_1] entries, [_2] ready, [_3] being graded",$entries,$ready,$locks).' </td></tr>'."\n";
1.34      albertel  197: 
                    198: 	($entries,$ready,$locks)=&get_queue_counts('reviewqueue');
1.106     albertel  199: 	$result.='<tr><td>'.
                    200: 	    ' <input type="submit" name="reviewasubmission" value="'.
                    201: 	    &mt("Select an entry from the review queue:").'" /> ';
                    202: 	$result.=&mt("[_1] entries, [_2] ready, [_3] being graded",
                    203: 		     $entries,$ready,$locks).'</td></tr>'."\n";
                    204: 	$result.='<tr><td> <input type="submit" name="regradeasubmission" value="'.
                    205: 	    &mt("List of user's grade status").'" /> </td></tr></table>'."\n";
1.105     albertel  206: 	$result.='<p> <input type="submit" name="regradeaspecificsubmission" value="'.
1.106     albertel  207: 	    &mt("Regrade specific user:").'" />'."\n";
1.105     albertel  208: 	$result.='<input type="text" size="12" name="gradinguser" />';
                    209: 	$result.=&Apache::loncommon::select_dom_form($env{'user.domain'},
                    210: 						     'gradingdomain');
                    211: 	$result.=' '.
                    212: 	    &Apache::loncommon::selectstudent_link('gradesubmission',
                    213: 						   'gradinguser',
                    214: 						   'gradingdomain');
                    215: 	$result.=&Apache::loncommon::studentbrowser_javascript();
                    216:   }
1.13      albertel  217:     return $result;
                    218: }
                    219: 
1.22      albertel  220: sub add_request_another_attempt_button {
1.38      albertel  221:     my ($text)=@_;
                    222:     if (!$text) { $text="Request another attempt"; }
1.25      albertel  223:     my $result;
1.36      albertel  224:     my $symb=&Apache::lonnet::symbread();
1.37      albertel  225:     my ($slot_name,$slot)=&Apache::slotrequest::check_for_reservation($symb);
1.38      albertel  226:     my $action='get_reservation';
1.37      albertel  227:     if ($slot_name) {
1.38      albertel  228: 	$text="Change reservation.";
                    229: 	$action='change_reservation';
1.37      albertel  230: 	my $description=&Apache::slotrequest::get_description($slot_name,
                    231: 							      $slot);
                    232: 	$result.=(<<STUFF);
                    233: <p> Will be next available: $description </p>
                    234: STUFF
                    235:     }
1.38      albertel  236:     
                    237:     if ($env{'request.enc'}) { $symb=&Apache::lonenc::encrypted($symb); }
                    238:     $symb=&Apache::lonnet::escape($symb);
1.115     albertel  239:     $result.='<form method="post" action="/adm/slotrequest">'.
1.38      albertel  240: 	'<input type="hidden" name="symb" value="'.$symb.'" />'.
                    241: 	'<input type="hidden" name="command" value="'.$action.'" />'.
                    242: 	'<input type="submit" name="requestattempt" value="'.
                    243: 	&mt($text).'" />'.
                    244: 	'</form>';
1.25      albertel  245:     return $result;
1.22      albertel  246: }
                    247: 
1.30      albertel  248: sub preserve_grade_info {
                    249:     my $result;
                    250:     # if we are viewing someone else preserve that info
                    251:     if (defined $env{'form.grade_symb'}) {
                    252: 	foreach my $field ('symb','courseid','domain','username') {
                    253: 	    $result .= '<input type="hidden" name="grade_'.$field.
                    254: 		'" value="'.$env{"form.grade_$field"}.'" />'."\n";
                    255: 	}
                    256:     }
                    257:     return $result;
                    258: }
                    259: 
1.53      albertel  260: sub style {
                    261:     return (<<STYLE);
                    262: <style type="text/css">
1.116     albertel  263: .LC_fail, .LC_pass, .LC_neutral {
1.53      albertel  264:     position: relative;
                    265:     margin : 5px;
                    266:     margin-bottom :10px;
                    267:     padding : 4px;
                    268:     padding-left : 75px;
                    269: 	border : thin solid;
                    270:     font-weight : bolder;
                    271:     font-size: smaller;
                    272:     font-family: Arial;
                    273:     background-color : rgb(255,240,225);
                    274: }
                    275: 
1.116     albertel  276: .LC_fail h4, .LC_pass h4 {
1.53      albertel  277: 	position:absolute;
                    278: 	left: -4px;
                    279: 	top: -8px;
                    280: 	padding:2px;
                    281: 	margin:0;
                    282: 	background-color : rgb(255,240,225);
                    283: 	border : thin solid;
                    284: }
                    285: 
1.116     albertel  286: .LC_fail {
1.53      albertel  287: 	color: red;
                    288: }
                    289: 
1.116     albertel  290: .LC_pass {
1.53      albertel  291:     color : green;
                    292: }
                    293: 
1.116     albertel  294: .LC_neutral {
1.53      albertel  295: 	color : blue;
                    296: }
                    297: 
1.116     albertel  298: .LC_question {
1.53      albertel  299: 	border : thin solid black;
                    300: 	padding : 4px;
                    301: 	margin-bottom : 1em;
                    302: }
                    303: 
1.116     albertel  304: .LC_grade {
1.53      albertel  305: 	font-size: x-large;
                    306: 	font-family: Arial;
                    307: 	position:absolute;
                    308: 	left: 5px;
                    309: 	top: -5px;
                    310: 	width: 70px;
                    311: }
                    312: 
                    313: div#feedback h1 {
                    314:     color : inherit;
                    315: }
                    316: </style>
                    317: STYLE
                    318: 
                    319: }
                    320: 
1.54      albertel  321: sub show_task {
                    322:     my ($status,$previous)=@_;
                    323:     if (!$previous && (
                    324: 		       ( $status eq 'CLOSED' ) ||
                    325: 		       ( $status eq 'BANNED') ||
                    326: 		       ( $status eq 'UNAVAILABLE') ||
                    327: 		       ( $status eq 'NOT_IN_A_SLOT') ||
                    328: 		       ( $status eq 'NEEDS_CHECKIN') ||
                    329: 		       ( $status eq 'WAITING_FOR_GRADE') ||
                    330: 		       ( $status eq 'INVALID_ACCESS') )) {
                    331: 	return 0;
                    332:     }
1.64      albertel  333:     if ($env{'form.donescreen'}) { return 0; }
1.54      albertel  334:     return 1;
                    335: }
                    336: 
                    337: sub internal_location {
                    338:     my ($id)=@_;
                    339:     return '<!-- LONCAPA_INTERNAL_ADD_TASK_STATUS'.$id.' -->';
                    340: }
                    341: 
1.60      albertel  342: sub submission_time_stamp {
                    343:     my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
                    344:     my $submissiontime;
1.89      albertel  345:     my $version=$Apache::lonhomework::history{'resource.0.version'};
1.60      albertel  346:     for (my $v=$Apache::lonhomework::history{'version'};$v>0;$v--) {
                    347: 	if (defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.bridgetask.portfiles'})) {
                    348: 	    $submissiontime=$Apache::lonhomework::history{$v.':timestamp'};
                    349: 	}
                    350:     }
                    351:     my $result;
                    352:     if ($submissiontime) {
1.89      albertel  353: 	my $slot_name=$Apache::lonhomework::history{'resource.'.$version.'.0.checkedin.slot'};
1.60      albertel  354: 	my %slot=&Apache::lonnet::get_slot($slot_name);
                    355: 	my $diff = $slot{'endtime'} - $submissiontime;
1.71      albertel  356: 	my ($color,$when)=('#FF6666','after');
                    357: 	if ($diff > 0) { ($color,$when)=('#336600','before'); }
1.60      albertel  358: 	my $info;
                    359: 	if ($diff%60) { $info=($diff%60).' seconds'; }
                    360: 	$diff=int($diff/60);
                    361: 	if ($diff%60) { $info=($diff%60).' minutes '.$info; }
                    362: 	$diff=int($diff/60);
                    363: 	if ($diff) {    $info=$diff.' hours '.$info; }
                    364: 	$result='<p><font color="'.$color.'">'.
                    365: 	    &mt('Student submitted [_1] [_2] the deadline. 
                    366:                  (Submission was at [_3], end of period was [_4].)',
                    367: 		$info,$when,scalar(localtime($submissiontime)),
                    368: 		scalar(localtime($slot{'endtime'}))).
                    369: 		'</font></p>';
                    370:     }
                    371:     return $result;
                    372: }
                    373: 
1.119   ! albertel  374: sub file_list {
        !           375:     my ($files,$uname,$udom) = @_;
        !           376:     if (!defined($uname) || !defined($udom)) {
        !           377: 	(undef,undef,$udom,$uname) = &Apache::lonxml::whichuser();
        !           378:     }
1.70      albertel  379:     my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
1.119   ! albertel  380: 
1.116     albertel  381:     my $file_list="<ul class=\"LC_GRADE_handininfo\">\n";
1.119   ! albertel  382:     foreach my $partial_file (split(',',$files)) {
1.70      albertel  383: 	my $file=$file_url.$partial_file;
                    384: 	$file=~s|/+|/|g;
                    385: 	&Apache::lonnet::allowuploaded('/adm/bridgetask',$file);
1.73      albertel  386: 	$file_list.='<li><nobr><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
1.109     albertel  387: 	    &Apache::loncommon::icon($file).'" border="0"> '.$file.
1.70      albertel  388: 	    '</a></nobr></li>'."\n";
                    389:     }
                    390:     $file_list.="</ul>\n";
1.119   ! albertel  391:     return $file_list;
        !           392: }
        !           393: 
        !           394: sub webgrade_standard_info {
        !           395:     my ($version)=&get_version();
        !           396: 
        !           397:     my $file_list = &file_list($Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"});
1.70      albertel  398: 
1.118     albertel  399:     my %lt=&Apache::lonlocal::texthash('done' => 'Next Item',
                    400: 				       'stop' => 'Quit Grading',
1.70      albertel  401: 				       );
                    402: 				       
                    403:     my $result=<<INFO;
1.116     albertel  404:   <div class="LC_GRADE_maincontrols">
1.111     albertel  405:     <input type="submit" name="next" value="$lt{'done'}" />
                    406:     <input type="submit" name="stop" value="$lt{'stop'}" />
                    407:   </div>
1.70      albertel  408:   $file_list
                    409: INFO
                    410:     return $result;
                    411: }
                    412: 
1.1       albertel  413: sub start_Task {
1.87      albertel  414:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel  415: 
1.4       albertel  416:     my ($status,$accessmsg,$slot);
1.16      albertel  417:     if ($target ne 'webgrade') {
1.70      albertel  418: 	&Apache::structuretags::init_problem_globals('Task');
1.16      albertel  419: 	&Apache::structuretags::initialize_storage();
                    420: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.74      albertel  421: 	if ($env{'request.state'} eq 'construct') {
                    422: 	    &Apache::structuretags::setup_rndseed($safeeval);
                    423: 	}
1.16      albertel  424:     } 
                    425: 
1.4       albertel  426:     $Apache::lonhomework::parsing_a_task=1;
1.1       albertel  427:     #should get back a <html> or the neccesary stuff to start XML/MathML
                    428:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    429: 	&Apache::structuretags::page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.16      albertel  430:     
1.117     albertel  431:     $body_tag_start = '<div class="LC_task">'."\n";
1.74      albertel  432:     if ($target eq 'web' && $env{'request.state'} ne 'construct') {
1.13      albertel  433: 	if ($Apache::lonhomework::modifygrades) {
1.115     albertel  434: 	    $body_tag_start.='<form name="gradesubmission" method="post" action="';
1.13      albertel  435: 	    my $uri=$env{'request.uri'};
                    436: 	    if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    437: 	    $body_tag_start.=$uri.'">'.&add_grading_button()."</form>";
1.38      albertel  438: 	    my $symb=&Apache::lonnet::symbread();
1.40      albertel  439: 	    if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.115     albertel  440: 		$body_tag_start.='<form method="post" name="slotrequest" action="/adm/slotrequest">'.
1.40      albertel  441: 		    '<input type="hidden" name="symb" value="'.$symb.'" />'.
                    442: 		    '<input type="hidden" name="command" value="showslots" />'.
                    443: 		    '<input type="submit" name="requestattempt" value="'.
                    444: 		    &mt('Show Slot list').'" />'.
                    445: 		    '</form>';
1.108     albertel  446: 		my $target_id = 
                    447: 		    &Apache::lonstathelpers::make_target_id({symb => $symb,
                    448: 							     part => '0'});
1.115     albertel  449: 		$body_tag_start.='<form method="post" name="gradingstatus" action="/adm/statistics">'.
1.108     albertel  450: 		    '<input type="hidden" name="problemchoice" value="'.$target_id.'" />'.
                    451: 		    '<input type="hidden" name="reportSelected" value="grading_analysis" />'.
                    452: 		    '<input type="submit" name="grading" value="'.
                    453: 		    &mt('Show Grading Status').'" />'.
                    454: 		    '</form>';
1.40      albertel  455: 	    }
1.13      albertel  456: 	}
1.8       albertel  457:     }
1.74      albertel  458:     if ($target eq 'web' && $env{'request.state'} eq 'construct') {
                    459: 	$form_tag_start.=&Apache::structuretags::problem_web_to_edit_header($env{'form.rndseed'});
                    460:     }
1.21      albertel  461:     if ($target eq 'web' || ($target eq 'grade' && !$env{'form.webgrade'}) || $target eq 'answer' ||
1.1       albertel  462: 	$target eq 'tex') {
1.29      albertel  463: 	my ($version,$previous)=&get_version();
1.14      albertel  464: 	($status,$accessmsg,my $slot_name,$slot) = 
1.81      albertel  465: 	    &Apache::lonhomework::check_slot_access('0','Task');
1.9       albertel  466: 	push(@Apache::inputtags::status,$status);
1.14      albertel  467: 	$Apache::inputtags::slot_name=$slot_name;
1.1       albertel  468: 	my $expression='$external::datestatus="'.$status.'";';
1.89      albertel  469: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$version.0.solved"}.'";';
1.1       albertel  470: 	&Apache::run::run($expression,$safeeval);
                    471: 	&Apache::lonxml::debug("Got $status");
1.25      albertel  472: 	$body_tag_start.=&add_previous_version_button($status);
1.54      albertel  473: 	if (!&show_task($status,$previous)) {
1.87      albertel  474: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.1       albertel  475: 	    if ( $target eq "web" ) {
1.4       albertel  476: 		$result.= $head_tag_start.'</head>'.$body_tag_start;
1.74      albertel  477: 		if ($env{'request.state'} eq 'construct') {
                    478: 		    $result.=$form_tag_start;
                    479: 		}
1.4       albertel  480: 		my $msg;
1.1       albertel  481: 		if ($status eq 'UNAVAILABLE') {
                    482: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3       albertel  483: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    484: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.38      albertel  485: 		    $msg.=&add_request_another_attempt_button("Sign up for time to work.");
1.4       albertel  486: 		} elsif ($status eq 'NEEDS_CHECKIN') {
                    487: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
                    488: 			'</h1>'.&proctor_validation_screen($slot);
1.22      albertel  489: 		} elsif ($status eq 'WAITING_FOR_GRADE') {
                    490: 		    $msg.='<h1>'.&mt('Your submission is in the grading queue.').'</h1>';
1.64      albertel  491: 		} elsif ($env{'form.donescreen'}) {
                    492: 		    my $title=&Apache::lonnet::gettitle();
1.67      albertel  493: 		    my @files=split(',',$Apache::lonhomework::history{'resource.'.$version.'.0.bridgetask.portfiles'});
1.114     albertel  494: 		    my (undef,undef,$domain,$user)=
                    495: 			&Apache::lonxml::whichuser();
                    496: 		    my $files = '<ul>';
                    497: 		    foreach my $file (@files) {
                    498: 			my $url="/uploaded/$domain/$user/portfolio$file";
                    499: 			if (! &Apache::lonnet::stat_file($url)) {
                    500: 			    $file = &mt('<font color="red"> Nonexistant file:</font> <tt>[_1]</tt>',$file);
                    501: 			} else {
                    502: 			    $file = '<tt>'.$file.'</tt>';
                    503: 			}
                    504: 			$files .= '<li>'.$file.'</li>';
                    505: 		    }
                    506: 		    $files.='</ul>';
                    507: 
1.64      albertel  508: 		    $result.=<<DONESCREEN;
                    509: <h2>$title</h2>
                    510: <p> Files submitted: $files </p>
1.67      albertel  511: <p> You are now done with this Bridge Task </p>
1.64      albertel  512: <hr />
                    513: <p> <a href="/adm/logout">Logout</a> </p>
                    514: <p> <a href="/adm/roles">Change to a different course</a> </p>
                    515: DONESCREEN
1.1       albertel  516: 		} elsif ($status ne 'NOT_YET_VIEWED') {
                    517: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
                    518: 		}
                    519: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
                    520: 		    $msg.='The problem '.$accessmsg;
                    521: 		}
                    522: 		$result.=$msg.'<br />';
                    523: 	    } elsif ($target eq 'tex') {
                    524: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
                    525: 		if ($status eq 'UNAVAILABLE') {
                    526: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
                    527: 		} else {
                    528: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
                    529: 		}
1.22      albertel  530: 	    } elsif ($target eq 'grade' && !$env{'form.webgrade'}) {
1.4       albertel  531: 		if ($status eq 'NEEDS_CHECKIN') {
1.83      albertel  532: 		    if(&proctor_check_auth($slot_name,$slot,'Task')
                    533: 		       && defined($Apache::inputtags::slot_name)) {
1.77      albertel  534: 			my $result=&add_to_queue('gradingqueue',
1.82      albertel  535: 						 [$Apache::inputtags::slot_name]);
1.77      albertel  536: 			&Apache::lonxml::debug("add_to_queue said $result");
                    537: 		    }
1.4       albertel  538: 		}
1.1       albertel  539: 	    }
                    540: 	} elsif ($target eq 'web') {
                    541: 	    my $name= &Apache::structuretags::get_resource_name($parstack,$safeeval);
                    542: 	    $result.="$head_tag_start<title>$name</title></head>
1.53      albertel  543:               $body_tag_start \n".&style();
1.36      albertel  544: 	    
1.57      albertel  545: 	    $result.=&preserve_grade_info();
                    546: 	    $result.=&internal_location();
1.36      albertel  547: 	    $result.=$form_tag_start.
                    548: 		'<input type="hidden" name="submitted" value="yes" />';
1.54      albertel  549: 	    &Apache::lonxml::startredirection();
1.1       albertel  550: 	}
1.21      albertel  551:     } elsif ( ($target eq 'grade' && $env{'form.webgrade'}) ||
                    552: 	      $target eq 'webgrade') {
1.32      albertel  553: 	my $webgrade='yes';
1.21      albertel  554: 	if ($target eq 'webgrade') {
1.110     albertel  555: 	    $result.=$head_tag_start.'</head>'.$body_tag_start;
1.49      albertel  556: 	    #$result.='<br />Review'.&show_queue('reviewqueue');
                    557: 	    #$result.='<br />Grade'.&show_queue('gradingqueue');
1.30      albertel  558: 	}
1.33      albertel  559: 	# FIXME Blast! still need to reorg this, need to reshow the
                    560:         #       queue being reviewed once done with the grade pass...
                    561:         #       Hrrm, vaildation pass should perhaps say 'not_locked'
                    562:         #       perhaps do a search if there is a key that is mine and if
                    563:         #       there isn't reshow the queue....
1.105     albertel  564: 	my ($todo,$status_code,$msg)=&get_key_todo($target);
1.33      albertel  565: 
                    566: 	if ($todo) {
                    567: 	    &setup_env_for_other_user($todo,$safeeval);
                    568: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
                    569: 	    $result.="\n".'<table><tr><td>Found '.
                    570: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
                    571: 	    $form_tag_start.=
                    572: 		'<input type="hidden" name="gradingkey" value="'.
                    573: 		&Apache::lonnet::escape($todo).'" />';
                    574: 	    $Apache::bridgetask::queue_key=$todo;
                    575: 	    &Apache::structuretags::initialize_storage();
                    576: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.110     albertel  577: 	    if ($target eq 'webgrade' && $status_code eq 'selected') {
                    578: 		$form_tag_start.=
                    579: 		    '<input type="hidden" name="queuemode" value="selected" />';
1.33      albertel  580: 	    }
1.15      albertel  581: 	} else {
1.33      albertel  582: 	    if ($target eq 'webgrade') {
                    583: 		$result.="\n";
1.81      albertel  584: 		my $back='<p><a href="/adm/flip?postdata=return:">'.
                    585: 		    &mt('Return to resource').'</a></p>';
1.33      albertel  586: 		if      ($status_code eq 'stop') {
1.81      albertel  587: 		    $result.='<b>'.&mt("Stopped grading.").'</b>'.$back;
1.33      albertel  588: 		} elsif ($status_code eq 'lock_failed') {
1.105     albertel  589: 		    $result.='<b>'.&mt("Failed to lock the requested record.")
1.81      albertel  590: 			.'</b>'.$back;
1.33      albertel  591: 		} elsif ($status_code eq 'unlock') {
1.81      albertel  592: 		    $result.='<b>'.&mt("Unlocked the requested record.")
                    593: 			.'</b>'.$back;
1.33      albertel  594: 		    $result.=&show_queue($env{'form.queue'},1);
                    595: 		} elsif ($status_code eq 'show_list') {
                    596: 		    $result.=&show_queue($env{'form.queue'},1);
1.49      albertel  597: 		} elsif ($status_code eq 'select_user') {
                    598: 		    $result.=&select_user();
1.95      albertel  599: 		} elsif ($status_code eq 'unable') {
                    600: 		    $result.='<b>'.&mt("Unable to aqcuire a user to grade.").'</b>'.$back;
1.105     albertel  601: 		} elsif ($status_code eq 'not_allowed') {
                    602: 		    $result.='<b>'.&mt('Not allowed to grade the requested user.').' '.$msg.'</b>'.$back;
1.33      albertel  603: 		} else {
1.81      albertel  604: 		    $result.='<b>'.&mt("No user to be graded.").'</b>'.$back;
1.32      albertel  605: 		}
1.21      albertel  606: 	    }
1.33      albertel  607: 	    $webgrade='no';
1.87      albertel  608: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.32      albertel  609: 	}
                    610: 	if ($target eq 'webgrade' && defined($env{'form.queue'})) {
1.61      albertel  611: 	    if ($webgrade eq 'yes') {
                    612: 		$result.=&submission_time_stamp();
                    613: 	    }
1.32      albertel  614: 	    $result.=$form_tag_start;
                    615: 	    $result.='<input type="hidden" name="webgrade" value="'.
                    616: 		$webgrade.'" />';
                    617: 	    $result.='<input type="hidden" name="queue" value="'.
                    618: 		$env{'form.queue'}.'" />';
1.52      albertel  619: 	    if ($env{'form.regrade'}) {
                    620: 		$result.='<input type="hidden" name="regrade" value="'.
                    621: 		    $env{'form.regrade'}.'" />';
                    622: 	    }
1.62      albertel  623: 	    if ($env{'form.chosensections'}) {
                    624: 		my @chosen_sections=
                    625: 		    &Apache::loncommon::get_env_multiple('form.chosensections');
                    626: 		foreach my $sec (@chosen_sections) {
                    627: 		    $result.='<input type="hidden" name="chosensections" 
                    628:                                value="'.$sec.'" />';
                    629: 		}
                    630: 	    }
1.70      albertel  631: 	    if ($webgrade eq 'yes') { $result.=&webgrade_standard_info(); }
1.15      albertel  632: 	}
1.110     albertel  633: 	if ($target eq 'webgrade') {
1.116     albertel  634: 	    $result.="\n".'<div id="LC_GRADE_criterialist">';
1.110     albertel  635: 	}
1.74      albertel  636:     } elsif ($target eq 'edit') {
                    637: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
                    638: 	    &Apache::structuretags::problem_edit_header();
                    639: 	$Apache::lonxml::warnings_error_header=
                    640: 	    &mt("Editor Errors - these errors might not effect the running of the problem, but they will likely cause problems with further use of the Edit mode. Please use the EditXML mode to fix these errors.")."<br />";
                    641: 	my $temp=&Apache::edit::insertlist($target,$token);
                    642: 	$result.=$temp;
1.1       albertel  643:     } else {
                    644: 	# page_start returned a starting result, delete it if we don't need it
                    645: 	$result = '';
                    646:     }
                    647:     return $result;
                    648: }
                    649: 
1.32      albertel  650: sub get_key_todo {
                    651:     my ($target)=@_;
                    652:     my $todo;
1.33      albertel  653: 
                    654:     if (defined($env{'form.reviewasubmission'})) {
1.54      albertel  655: 	&Apache::lonxml::debug("review a submission....");
1.33      albertel  656: 	$env{'form.queue'}='reviewqueue';
                    657: 	return (undef,'show_list');
                    658:     }
                    659: 
                    660:     if (defined($env{'form.reviewagrading'})) {
                    661: 	&Apache::lonxml::debug("review a grading....");
                    662: 	$env{'form.queue'}='gradingqueue';
                    663: 	return (undef,'show_list');
                    664:     }
                    665: 
1.49      albertel  666:     if (defined($env{'form.regradeasubmission'})) {
                    667: 	&Apache::lonxml::debug("regrade a grading....");
                    668: 	$env{'form.queue'}='none';
                    669: 	return (undef,'select_user');
                    670:     }
                    671: 
1.105     albertel  672: 
                    673:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    674: 
                    675:     #need to try both queues..
                    676:     if (defined($env{'form.regradeaspecificsubmission'}) &&
                    677: 	defined($env{'form.gradinguser'})               &&
                    678: 	defined($env{'form.gradingdomain'})               ) {
                    679: 	my ($symb,$cid)=&Apache::lonxml::whichuser();
                    680: 	my $cnum  = $env{'course.'.$cid.'.num'};
                    681: 	my $cdom  = $env{'course.'.$cid.'.domain'};
                    682: 	my $uname = $env{'form.gradinguser'};
                    683: 	my $udom  = $env{'form.gradingdomain'};
                    684: 
                    685: 	my $gradingkey=&encode_queue_key($symb,$udom,$uname);
                    686: 
                    687: 	my $queue;
                    688: 
                    689: 	if      (&in_queue('gradingqueue',$symb,$cdom,$cnum,$udom,$uname)) {
                    690: 	    $env{'form.queue'} = $queue = 'gradingqueue';
                    691: 	} elsif (&in_queue('reviewqueue' ,$symb,$cdom,$cnum,$udom,$uname)) {
                    692: 	    $env{'form.queue'} = $queue = 'reviewqueue';
                    693: 	}
                    694: 	
                    695: 	if (!$queue) {
                    696: 	    $env{'form.queue'} = $queue = 'none';
                    697: 	    #not queued so doing either a re or pre grade
                    698: 	    return ($gradingkey);
                    699: 	}
                    700: 
                    701: 	my $who=&queue_key_locked($queue,$gradingkey);
                    702: 	if ($who eq $me) {
                    703: 	    #already have the lock
                    704: 	    $env{'form.gradingkey'}=&Apache::lonnet::escape($gradingkey);
                    705: 	    return ($gradingkey);
                    706: 	}
                    707: 	
                    708: 	if (!defined($who)) {
                    709: 	    if (&lock_key($queue,$gradingkey)) {
                    710: 		return ($gradingkey);
                    711: 	    } else {
                    712: 		return (undef,'lock_failed');
                    713: 	    }
                    714: 	}
                    715: 
                    716: 	#otherwise (defined($who) && $who ne $me) some else has it...
                    717: 	return (undef,'not_allowed',
                    718: 		&mt('Another user ([_1]) currently has the record for [_2] locked.',
                    719: 		    $who,$env{'form.gradinguser'}.'@'.$env{'form.gradingdomain'}));
                    720:     }
                    721: 
                    722: 
1.32      albertel  723:     my $queue=$env{'form.queue'};
1.33      albertel  724: 
1.32      albertel  725:     if (!defined($queue)) {
                    726: 	$env{'form.queue'}=$queue='gradingqueue';
                    727:     }
1.33      albertel  728: 
1.32      albertel  729:     my $gradingkey=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.33      albertel  730: 
1.49      albertel  731:     if ($env{'form.queue'} eq 'none') {
                    732: 	if (defined($env{'form.gradingkey'})) {
                    733: 	    if ($target eq 'webgrade') {
                    734: 		if ($env{'form.stop'}) {
                    735: 		    return (undef,'stop');
                    736: 		} elsif ($env{'form.next'}) {
1.59      albertel  737: 		    return (undef,'select_user');
1.49      albertel  738: 		}
                    739: 	    }
                    740: 	    return ($gradingkey,'selected');
                    741: 	} else {
1.59      albertel  742: 	    return (undef,'select_user');
1.49      albertel  743: 	}
                    744:     }
1.32      albertel  745:     if (defined($env{'form.queue'}) && defined($env{'form.gradingkey'})
1.33      albertel  746: 	&& !defined($env{'form.gradingaction'}) 
                    747: 	&& $env{'form.queuemode'} eq 'selected') {
                    748: 	
                    749: 	my $who=&queue_key_locked($queue,$gradingkey);
                    750: 	if ($who eq $me) {
                    751: 	    &Apache::lonxml::debug("Found a key was given to me");
                    752: 	    return ($gradingkey,'selected');
                    753: 	} else {
                    754: 	    return (undef,'show_list');
                    755: 	}
                    756: 
                    757:     }
                    758: 
                    759:     if ($target eq 'webgrade' && $env{'form.queuemode'} eq 'selected') {
                    760: 	if ($env{'form.gradingaction'} eq 'resume') {
                    761: 	    delete($env{'form.gradingaction'});
                    762: 	    &Apache::lonxml::debug("Resuming a key");
1.32      albertel  763: 	    return ($gradingkey);
1.33      albertel  764: 	} elsif ($env{'form.gradingaction'} eq 'unlock') {
                    765: 	    &Apache::lonxml::debug("Unlocking a key ".
                    766: 				     &check_queue_unlock($queue,$gradingkey,1));
                    767: 	    return (undef,'unlock');
                    768: 	} elsif ($env{'form.gradingaction'} eq 'select') {
                    769: 	    &Apache::lonxml::debug("Locking a key");
                    770: 	    if (&lock_key($queue,$gradingkey)) {
                    771: 		&Apache::lonxml::debug("Success $queue");
                    772: 		return ($gradingkey);
                    773: 	    }
                    774: 	    &Apache::lonxml::debug("Failed $queue");
                    775: 	    return (undef,'lock_failed');
1.32      albertel  776: 	}
                    777:     }
1.33      albertel  778: 
                    779:     if ($env{'form.queuemode'} ne 'selected') {
                    780: 	# don't get something new from the queue if they hit the stop button
                    781:     	if (!($env{'form.stop'} && $target eq 'webgrade') 
                    782: 	    && !$env{'form.gradingaction'}) {
                    783: 	    &Apache::lonxml::debug("Getting anew $queue");
                    784: 	    return (&get_from_queue($queue));
                    785: 	} else {
                    786: 	    return (undef,'stop');
                    787: 	}
1.32      albertel  788:     }
1.33      albertel  789:     return (undef,undef)
1.32      albertel  790: }
1.94      albertel  791: 
                    792: sub minimize_storage {
                    793:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    794: 	if ($key =~ /regrader$/) { next; }
                    795: 	if ($Apache::lonhomework::results{$key} eq
                    796: 	    $Apache::lonhomework::history{$key}) {
                    797: 	    delete($Apache::lonhomework::results{$key});
                    798: 	}
                    799:     }
                    800: }
                    801: 
1.1       albertel  802: sub end_Task {
                    803:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    804:     my $result='';
                    805:     my $status=$Apache::inputtags::status['-1'];
1.29      albertel  806:     my ($version,$previous)=&get_version();
1.1       albertel  807:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.15      albertel  808: 	$target eq 'tex') {
1.69      albertel  809: 	if ($target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.1       albertel  810: 	    if ($target eq 'web') {
1.54      albertel  811: 		if (&show_task($status,$previous)) {
                    812: 		    $result.=&Apache::lonxml::endredirection();
                    813: 		}
1.64      albertel  814: 		if ($status eq 'CAN_ANSWER' && !$previous && 
                    815: 		    !$env{'form.donescreen'}) {
1.15      albertel  816: 		    $result.="\n".'<table border="1">'.
1.28      albertel  817: 			&Apache::inputtags::file_selector("$version.0",
                    818: 							  "bridgetask","*",
1.46      albertel  819: 							  'portfolioonly',
                    820: 							  '
                    821: <h2>'.&mt('Submit Portfolio Files for Grading').'</h2>
                    822: <p>'.&mt('Indicate the files from your portfolio to be evaluated in grading this task.').'</p>').
1.9       albertel  823: 							  "</table>";
1.77      albertel  824: 		}
1.78      albertel  825: 		if (!$previous && $status ne 'SHOW_ANSWER' &&
                    826: 		    &show_task($status,$previous)) {
1.9       albertel  827: 		    $result.=&Apache::inputtags::gradestatus('0');
1.64      albertel  828: 		    $result.='</form>';
1.116     albertel  829: 		    my $action = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.64      albertel  830: 		    $result.=<<DONEBUTTON;
1.115     albertel  831: <form name="done" method="post" action="$action">
1.64      albertel  832:    <input type="hidden" name="donescreen" value="1" />
                    833:    <input type="submit" value="Done" />
                    834: </form>
                    835: DONEBUTTON
1.77      albertel  836:                 }
1.56      albertel  837: 		if (&show_task($status,$previous) &&
1.89      albertel  838: 		    $Apache::lonhomework::history{"resource.$version.0.status"} =~ /^(pass|fail)$/) {
                    839: 		    my $bt_status=$Apache::lonhomework::history{"resource.$version.0.status"};
1.54      albertel  840: 		    my $title=&Apache::lonnet::gettitle();
1.80      albertel  841: 
                    842: 		    my $slot_name=
1.89      albertel  843: 			$Apache::lonhomework::history{"resource.$version.0.checkedin.slot"};
1.80      albertel  844: 		    my %slot=&Apache::lonnet::get_slot($slot_name);
1.55      albertel  845: 		    my $start_time=
                    846: 			&Apache::lonlocal::locallocaltime($slot{'starttime'});
1.54      albertel  847: 
1.116     albertel  848: 		    my $status = "\n<div class='LC_$bt_status LC_criteria'>\n";
1.54      albertel  849: 		    
                    850: 		    if ($bt_status eq 'pass')  {
                    851: 			$status.='<h2>You passed the '.$title.' given on '.
1.80      albertel  852: 			    $start_time.'</h2>';
1.54      albertel  853: 		    }
                    854: 		    if ($bt_status eq 'fail')  {
                    855: 			$status.='<h2>You did not pass the '.$title.' given on '.
1.80      albertel  856: 			    $start_time.'</h2>';
1.54      albertel  857: 			if (!$previous) {
                    858: 			    $status.=&add_request_another_attempt_button();
                    859: 			}
                    860: 		    }
                    861: 		    my $man_count=0;
                    862: 		    my $opt_count=0;
                    863: 		    my $opt_passed=0;
                    864: 		    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    865: 			if ($Apache::bridgetask::dimensionmandatory{$dim_id}
                    866: 			    eq 'N') {
                    867: 			    $opt_count++;
1.89      albertel  868: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim_id.status"} eq 'pass') {
1.54      albertel  869: 				$opt_passed++;
                    870: 			    }
                    871: 			} else {
                    872: 			    $man_count++;
                    873: 			}
                    874: 		    }
                    875: 		    my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
                    876: 							 $parstack,$safeeval);
                    877: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
1.99      albertel  878: 		    $status.="\n<p>".&mt('You needed to pass all of the [_1]  mandatory components and [_2] of the [_3] optional components, of which you passed [_4].',$man_count,$opt_req,$opt_count,$opt_passed)."</p></div>\n";
1.54      albertel  879: 
                    880: 		    my $internal_location=&internal_location();
                    881: 		    $result=~s/\Q$internal_location\E/$status/;
                    882: 		}
1.13      albertel  883: 	    } 
1.117     albertel  884: 	    if ($target eq 'web') {
                    885: 		$result.="\n</div>\n".&Apache::lonxml::xmlend().'</html>';
1.1       albertel  886: 	    }
                    887: 	}
1.29      albertel  888: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel  889: 	    my $award='SUBMITTED';
1.28      albertel  890: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel  891: 						    'portfiles',\$award);
1.14      albertel  892: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel  893: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                    894: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    895: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                    896: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                    897: 
                    898: 		$Apache::lonhomework::results{"resource.0.award"}=
                    899: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                    900: 		    $award;
1.51      albertel  901: 		$Apache::lonhomework::results{"resource.0.submission"}=
                    902: 		    $Apache::lonhomework::results{"resource.$version.0.submission"}='';
1.64      albertel  903: 	    } else {
                    904: 		delete($Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"});
1.77      albertel  905: 		$award = '';
1.10      albertel  906: 	    }
1.4       albertel  907: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    908: 	    &Apache::structuretags::finalize_storage();
1.83      albertel  909: 	    if ($award eq 'SUBMITTED'
                    910: 		&& defined($Apache::inputtags::slot_name)) {
1.82      albertel  911: 		&add_to_queue('gradingqueue',[$Apache::inputtags::slot_name]);
1.14      albertel  912: 	    }
1.79      albertel  913: 	} elsif ($Apache::lonhomework::results{'INTERNAL_store'}) {
                    914: 	    &Apache::structuretags::finalize_storage();
1.1       albertel  915: 	}
1.32      albertel  916: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes') {
1.20      albertel  917: 	    my $optional_required=
                    918: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                    919: 					   $safeeval);
                    920: 	    my $optional_passed=0;
                    921: 	    my $mandatory_failed=0;
                    922: 	    my $ungraded=0;
                    923: 	    my $review=0;   
1.21      albertel  924: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.20      albertel  925: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
                    926: 		my $status=
1.89      albertel  927: 		    $Apache::lonhomework::results{"resource.$version.0.$dim_id.status"};
1.20      albertel  928: 		my $mandatory=
                    929: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
                    930: 		if ($status eq 'pass') {
                    931: 		    if (!$mandatory) { $optional_passed++; }
                    932: 		} elsif ($status eq 'fail') {
                    933: 		    if ($mandatory) { $mandatory_failed++; }
                    934: 		} elsif ($status eq 'ungraded') {
                    935: 		    $ungraded++;
                    936: 		} elsif ($status eq 'review') {
                    937: 		    $review++;
1.49      albertel  938: 		} else {
                    939: 		    $ungraded++;
                    940: 		}
1.20      albertel  941: 	    }
                    942: 	    if ($optional_passed < $optional_required) {
                    943: 		$mandatory_failed++;
                    944: 	    }
1.21      albertel  945: 	    &Apache::lonxml::debug("all dim ".join(':',@Apache::bridgetask::dimensionlist)."results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
1.89      albertel  946: 	    $Apache::lonhomework::results{'resource.0.regrader'}=
1.20      albertel  947: 		$env{'user.name'}.'@'.$env{'user.domain'};
                    948: 	    if ($review) {
1.89      albertel  949: 		$Apache::lonhomework::results{"resource.$version.0.status"}='review';
1.33      albertel  950: 		if ($env{'form.queue'} eq 'reviewqueue') {
                    951: 		    &check_queue_unlock($env{'form.queue'});
                    952: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                    953: 		} else {
1.49      albertel  954: 		    &move_between_queues($env{'form.queue'},'reviewqueue');
1.33      albertel  955: 		}
1.20      albertel  956: 	    } elsif ($ungraded) {
1.89      albertel  957: 		$Apache::lonhomework::results{"resource.$version.0.status"}='ungraded';
1.49      albertel  958: 		if ($env{'form.queue'} eq 'reviewqueue' ||
                    959: 		    $env{'form.queue'} eq 'none' ) {
1.33      albertel  960: 		    &Apache::lonxml::debug("moving back.");
1.49      albertel  961: 		    &move_between_queues($env{'form.queue'},'gradingqueue');
1.33      albertel  962: 		} else {
                    963: 		    &check_queue_unlock($env{'form.queue'});
                    964: 		}
1.20      albertel  965: 	    } elsif ($mandatory_failed) {
1.89      albertel  966: 		$Apache::lonhomework::results{"resource.$version.0.status"}='fail';
1.25      albertel  967: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                    968: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                    969: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.39      albertel  970: 		&remove_from_queue($env{'form.queue'}); 
                    971: 
                    972: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  973: 		
                    974: 		if ($env{'form.regrade'} ne 'yes') {
                    975: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    976: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    977: 		    &Apache::grades::version_portfiles(
                    978: 						       \%Apache::lonhomework::results,
                    979: 						       ["$version.0.bridgetask"],$courseid,
                    980: 						       $symb,$udom,$uname,
                    981: 						       ["$version.0.bridgetask"]);
                    982: 		}
1.20      albertel  983: 	    } else {
1.89      albertel  984: 		$Apache::lonhomework::results{"resource.$version.0.status"}='pass';
1.25      albertel  985: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                    986: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                    987: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.32      albertel  988: 		&remove_from_queue($env{'form.queue'});
1.39      albertel  989: 
                    990: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonxml::whichuser();
1.52      albertel  991: 		if ($env{'form.regrade'} ne 'yes') {
                    992: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                    993: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                    994: 		    &Apache::grades::version_portfiles(
                    995: 						       \%Apache::lonhomework::results,
                    996: 						       ["$version.0.bridgetask"],$courseid,
                    997: 						       $symb,$udom,$uname,
                    998: 						       ["$version.0.bridgetask"]);
                    999: 		}
1.20      albertel 1000: 	    }
1.89      albertel 1001: 	    $Apache::lonhomework::results{"resource.0.status"}=
                   1002: 		$Apache::lonhomework::results{"resource.$version.0.status"};
1.28      albertel 1003: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel 1004: 		$Apache::lonhomework::results{"resource.0.award"}=
1.50      albertel 1005: 		    $Apache::lonhomework::results{"resource.$version.0.award"};
1.26      albertel 1006: 		$Apache::lonhomework::results{"resource.0.awarded"}=
1.50      albertel 1007: 		    $Apache::lonhomework::results{"resource.$version.0.awarded"};
1.26      albertel 1008: 		$Apache::lonhomework::results{"resource.0.solved"}=
1.50      albertel 1009: 		    $Apache::lonhomework::results{"resource.$version.0.solved"};
1.25      albertel 1010: 	    }
1.94      albertel 1011: 	    &minimize_storage();
1.21      albertel 1012: 	    &Apache::structuretags::finalize_storage();
1.20      albertel 1013: 	}
1.15      albertel 1014:     } elsif ($target eq 'webgrade') {
1.111     albertel 1015: 	$result.="</div>\n<hr />";
1.20      albertel 1016: 	#$result.='<input type="submit" name="next" value="'.
                   1017: 	#    &mt('Save &amp; Next').'" /> ';
                   1018: 	#$result.='<input type="submit" name="end" value="'.
                   1019: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                   1020: 	#$result.='<input type="submit" name="throwaway" value="'.
                   1021: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                   1022: 	#$result.='<input type="submit" name="save" value="'.
                   1023: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.117     albertel 1024: 	$result.='</form>'."\n</div>\n".
                   1025: 	    &Apache::loncommon::endbodytag().'</html>';
1.1       albertel 1026:     } elsif ($target eq 'meta') {
1.70      albertel 1027: 	$result.=&Apache::response::meta_package_write('Task');
1.77      albertel 1028:         $result.=&Apache::response::meta_stores_write('solved','string',
                   1029: 						      'Problem Status');
                   1030: 	$result.=&Apache::response::meta_stores_write('tries','int_zeropos',
                   1031: 						      'Number of Attempts');
                   1032: 	$result.=&Apache::response::meta_stores_write('awarded','float',
                   1033: 						      'Partial Credit Factor');
                   1034: 	$result.=&Apache::response::meta_stores_write('status','string',
                   1035: 						      'Bridge Task Status');
1.1       albertel 1036:     }
1.4       albertel 1037:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel 1038:     return $result;
                   1039: }
                   1040: 
1.31      albertel 1041: sub move_between_queues {
                   1042:     my ($src_queue,$dest_queue)=@_;
1.49      albertel 1043:     my $cur_data;
                   1044:     if ($src_queue ne 'none') {
                   1045: 	$cur_data=&get_queue_data($src_queue);
                   1046: 	if (!$cur_data) { return 'not_exist'; }
                   1047:     } else {
                   1048: 	$cur_data = ['none'];
                   1049:     }
1.82      albertel 1050:     my $result=&add_to_queue($dest_queue,[$cur_data->[0]]);
1.31      albertel 1051:     if ($result ne 'ok') {
                   1052: 	return $result;
                   1053:     }
                   1054:     &check_queue_unlock($src_queue);
                   1055:     return &remove_from_queue($src_queue);
1.21      albertel 1056: }
                   1057: 
                   1058: sub check_queue_unlock {
1.32      albertel 1059:     my ($queue,$key,$allow_not_me)=@_;
1.49      albertel 1060:     if ($queue eq 'none') { return 'ok'; }
1.30      albertel 1061:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.32      albertel 1062:     if (!defined($key)) {
                   1063: 	$key="$symb\0queue\0$uname\@$udom";
                   1064:     }
1.30      albertel 1065:     my $cnum=$env{'course.'.$cid.'.num'};
                   1066:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1067:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1068:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                   1069:     if  ($who eq $me) {
1.32      albertel 1070: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                   1071:     } elsif ($allow_not_me) {
1.33      albertel 1072: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel 1073: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel 1074:     }
1.32      albertel 1075:     return 'not_owner';
1.21      albertel 1076: }
                   1077: 
1.88      albertel 1078: sub in_queue {
                   1079:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
                   1080:     if ($queue eq 'none') { return 0; }
                   1081:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1082: 	|| !defined($udom) || !defined($uname)) {
                   1083: 	($symb,my $cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1084: 	$cnum=$env{'course.'.$cid.'.num'};
                   1085: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1086:     }
                   1087: 
                   1088:     my $key=&encode_queue_key($symb,$udom,$uname);
                   1089:     my %results = &Apache::lonnet::get($queue,[$key],$cdom,$cnum);
                   1090: 
                   1091:     if (defined($results{$key})) {
                   1092: 	return 1;
                   1093:     }
                   1094:     return 0;
                   1095: }
                   1096: 
1.21      albertel 1097: sub remove_from_queue {
1.86      albertel 1098:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
1.49      albertel 1099:     if ($queue eq 'none') { return 'ok'; }
1.86      albertel 1100:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1101: 	|| !defined($udom) || !defined($uname)) {
                   1102: 	($symb,my $cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1103: 	$cnum=$env{'course.'.$cid.'.num'};
                   1104: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1105:     }
1.88      albertel 1106:     if (!&in_queue($queue,$symb,$cdom,$cnum,$udom,$uname)) {
                   1107: 	return 'ok';
                   1108:     }
1.86      albertel 1109:     my $key=&encode_queue_key($symb,$udom,$uname);
1.27      albertel 1110:     my @keys=($key,"$key\0locked");
1.31      albertel 1111:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel 1112: }
                   1113: 
1.16      albertel 1114: sub setup_env_for_other_user {
                   1115:     my ($queue_key,$safeeval)=@_;
                   1116:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel 1117:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel 1118:     $env{'form.grade_symb'}=$symb;
                   1119:     $env{'form.grade_domain'}=$udom;
                   1120:     $env{'form.grade_username'}=$uname;
                   1121:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                   1122:     &Apache::lonxml::initialize_rndseed($safeeval);
                   1123: }
                   1124: 
1.31      albertel 1125: sub get_queue_data {
                   1126:     my ($queue)=@_;
                   1127:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1128:     my $cnum=$env{'course.'.$cid.'.num'};
                   1129:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1130:     my $todo="$symb\0queue\0$uname\@$udom";
                   1131:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                   1132:     if ($key eq $todo && ref($value)) {
                   1133: 	return $value;
                   1134:     }
                   1135:     return undef;
                   1136: }
                   1137: 
1.84      albertel 1138: 
1.49      albertel 1139: sub check_queue_for_key {
1.84      albertel 1140:     my ($cdom,$cnum,$queue,$todo)=@_;
                   1141: 
1.49      albertel 1142:     my %results=
                   1143: 	&Apache::lonnet::get($queue,[$todo,"$todo\0locked"],$cdom,$cnum);
                   1144:     
                   1145:     if (exists($results{$todo}) && ref($results{$todo})) {
                   1146: 	if (defined($results{"$todo\0locked"})) {
                   1147: 	    return 'locked';
                   1148: 	}
1.86      albertel 1149: 	my $slot;
                   1150: 	if (ref($results{$todo}) eq 'ARRAY')  {
                   1151: 	    $slot = $results{$todo}[0];
                   1152: 	} elsif (ref($results{$todo}) eq 'HASH')  {
                   1153: 	    $slot = $results{$todo}{'slot'};
                   1154: 	}
                   1155: 	if (defined($slot)) {
                   1156: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1157: 	    if ($slot_data{'endtime'} > time) { 
                   1158: 		return 'in_progress';
                   1159: 	    }
1.58      albertel 1160: 	}
1.49      albertel 1161: 	return 'enqueued';
                   1162:     }
                   1163:     return undef;
                   1164: }
                   1165: 
1.14      albertel 1166: sub add_to_queue {
1.82      albertel 1167:     my ($queue,$user_data)=@_;
1.49      albertel 1168:     if ($queue eq 'none') { return 'ok'; }
1.14      albertel 1169:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
1.82      albertel 1170:     if (!$cid || $env{'request.state'} eq 'construct') {
                   1171: 	return 'no_queue';
                   1172:     }
1.14      albertel 1173:     my $cnum=$env{'course.'.$cid.'.num'};
                   1174:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1175:     my %data;
1.82      albertel 1176:     $data{"$symb\0queue\0$uname\@$udom"}=$user_data;
1.83      albertel 1177:     return &Apache::lonnet::cput($queue,\%data,$cdom,$cnum);
1.14      albertel 1178: }
                   1179: 
                   1180: sub show_queue {
1.32      albertel 1181:     my ($queue,$with_selects)=@_;
1.14      albertel 1182:     my $result;
                   1183:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1184:     my $cnum=$env{'course.'.$cid.'.num'};
                   1185:     my $cdom=$env{'course.'.$cid.'.domain'};
1.59      albertel 1186: 
                   1187:     my @chosen_sections=
                   1188: 	&Apache::loncommon::get_env_multiple('form.chosensections');
                   1189:     &Apache::grades::init_perm();
                   1190:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
                   1191:     &Apache::grades::reset_perm();
1.63      albertel 1192:     if (!(grep(/^all$/,@chosen_sections))) {
                   1193: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1194: 	    '</tt>.</p> '."\n";
                   1195:     }
1.59      albertel 1196: 
1.16      albertel 1197:     my $regexp="^$symb\0";
1.30      albertel 1198:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel 1199:     my ($tmp)=%queue;
                   1200:     if ($tmp=~/^error: 2 /) {
1.33      albertel 1201: 	return "\n<h3>Current Queue - $queue</h3><table border='1'><tr><td>Empty</td></tr></table>";
1.31      albertel 1202:     }
1.103     albertel 1203:     my $title=&Apache::lonnet::gettitle($symb);
                   1204:     $result.="\n<h3>Current Queue - $title $queue </h3><table border='1'><tr>";
                   1205:     if ($with_selects) { $result.="<th>Status</th><th></th>"; }
                   1206:     $result.="<th>user</th><th>data</th></tr>";
1.14      albertel 1207:     foreach my $key (sort(keys(%queue))) {
1.59      albertel 1208: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1209: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
1.32      albertel 1210: 	if ($key=~/locked$/ && !$with_selects) {
1.103     albertel 1211: 	    $result.="<tr><td>$uname</td>";
                   1212: 	    $result.='<td>'.$queue{$key}.'</td></tr>';
1.32      albertel 1213: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.103     albertel 1214: 	    $result.="<tr><td></td>";
                   1215: 	    $result.='<td>'.
1.16      albertel 1216: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel 1217: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1218: 	    $result.="<tr>";
1.35      albertel 1219: 	    my $slot=$queue{$key}->[0];
                   1220: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
1.32      albertel 1221: 	    if ($with_selects) {
                   1222: 		my $ekey=&Apache::lonnet::escape($key);
1.103     albertel 1223: 		my ($action,$description,$status)=('select',&mt('Select'));
1.32      albertel 1224: 		if (exists($queue{"$key\0locked"})) {
                   1225: 		    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
1.103     albertel 1226: 		    $status=&mt('Locked by <tt>[_1]</tt>',$queue{"$key\0locked"});
1.32      albertel 1227: 		    if ($me eq $queue{"$key\0locked"}) {
                   1228: 			($action,$description)=('resume',&mt('Resume'));
                   1229: 		    } else {
                   1230: 			($action,$description)=('unlock',&mt('Unlock'));
                   1231: 		    }
                   1232: 		}
1.62      albertel 1233: 		my $seclist;
                   1234: 		foreach my $sec (@chosen_sections) {
                   1235: 		    $seclist.='<input type="hidden" name="chosensections" 
                   1236:                                value="'.$sec.'" />';
                   1237: 		}
1.35      albertel 1238: 		if (time > $slot_data{'endtime'}) {
                   1239: 		    $result.=(<<FORM);
1.103     albertel 1240: <td>$status</td>
1.32      albertel 1241: <td>
1.115     albertel 1242: <form style="display: inline" method="post">
1.32      albertel 1243:  <input type="hidden" name="gradingkey" value="$ekey" />
                   1244:  <input type="hidden" name="queue" value="$queue" />
                   1245:  <input type="hidden" name="gradingaction" value="$action" />
                   1246:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel 1247:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel 1248:  <input type="submit" name="submit" value="$description" />
1.62      albertel 1249:  $seclist
1.32      albertel 1250: </form>
                   1251: </td>
                   1252: FORM
1.35      albertel 1253: 
                   1254:                 } else {
                   1255: 		    $result.='<td>'.&mt("In Progress").'</td>'
                   1256: 		}
1.32      albertel 1257: 	    }
1.104     albertel 1258: 	    $result.= "<td>".$fullname->{$uname.':'.$udom}.
                   1259: 		" <tt>($uname\@$udom)</tt> </td>";
1.103     albertel 1260: 	    $result.='<td>Slot: '.$slot.' End time: '.
1.20      albertel 1261: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
                   1262: 		"</td></tr>";
1.16      albertel 1263: 	}
1.14      albertel 1264:     }
1.15      albertel 1265:     $result.="</table><hr />\n";
1.14      albertel 1266:     return $result;
                   1267: }
                   1268: 
1.34      albertel 1269: sub get_queue_counts {
                   1270:     my ($queue)=@_;
                   1271:     my $result;
                   1272:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1273:     my $cnum=$env{'course.'.$cid.'.num'};
                   1274:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1275:     my $regexp="^$symb\0";
                   1276:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1277:     my ($tmp)=%queue;
                   1278:     if ($tmp=~/^error: 2 /) {
                   1279: 	return (0,0,0);
                   1280:     }
                   1281:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
1.96      albertel 1282:     my %slot_cache;
1.34      albertel 1283:     foreach my $key (sort(keys(%queue))) {
                   1284: 	if ($key=~/locked$/) {
                   1285: 	    $locks++;
                   1286: 	} elsif ($key=~/timestamp$/) {
                   1287: 	    #ignore
                   1288: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1289: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1290: 	    $entries++;
                   1291: 	    my $slot=$queue{$key}->[0];
1.96      albertel 1292: 	    if (!exists($slot_cache{$slot})) {
                   1293: 		my %slot_data=&Apache::lonnet::get_slot($slot);
                   1294: 		$slot_cache{$slot} = \%slot_data;
                   1295: 	    }
                   1296: 	    if (time > $slot_cache{$slot}{'endtime'}) {
1.34      albertel 1297: 		$ready_to_grade++;
                   1298: 	    }
                   1299: 	}
                   1300:     }
                   1301:     return ($entries,$ready_to_grade,$locks);
                   1302: }
                   1303: 
1.49      albertel 1304: sub encode_queue_key {
                   1305:     my ($symb,$udom,$uname)=@_;
                   1306:     return "$symb\0queue\0$uname\@$udom";
                   1307: }
                   1308: 
1.14      albertel 1309: sub decode_queue_key {
                   1310:     my ($key)=@_;
                   1311:     my ($symb,undef,$user) = split("\0",$key);
                   1312:     my ($uname,$udom) = split('@',$user);
                   1313:     return ($symb,$uname,$udom);
                   1314: }
                   1315: 
                   1316: sub queue_key_locked {
1.30      albertel 1317:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel 1318:     if (!defined($cdom) || !defined($cnum)) {
                   1319: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1320: 	$cnum=$env{'course.'.$cid.'.num'};
                   1321: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1322:     }
1.14      albertel 1323:     my ($key_locked,$value)=
1.30      albertel 1324: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel 1325:     if ($key_locked eq "$key\0locked") {
                   1326: 	return $value;
                   1327:     }
                   1328:     return undef;
                   1329: }
                   1330: 
                   1331: sub pick_from_queue_data {
1.30      albertel 1332:     my ($queue,$check_section,$queuedata,$cdom,$cnum)=@_;
1.98      albertel 1333:     my @possible; # will hold queue entries that are valid to be selected
1.30      albertel 1334:     foreach my $key (keys(%$queuedata)) {
1.68      albertel 1335: 	if ($key =~ /\0locked$/) { next; }
                   1336: 	if ($key =~ /\0timestamp$/) { next; }
1.14      albertel 1337: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                   1338: 	if ($check_section) {
                   1339: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel 1340: 	    if ($section eq $check_section) {
1.33      albertel 1341: 		&Apache::lonxml::debug("my sec");
1.15      albertel 1342: 		next;
                   1343: 	    }
1.14      albertel 1344: 	}
1.30      albertel 1345: 	my $slot=$queuedata->{$key}[0];
1.14      albertel 1346: 	my %slot_data=&Apache::lonnet::get_slot($slot);
1.15      albertel 1347: 	if ($slot_data{'endtime'} > time) { 
1.33      albertel 1348: 	    &Apache::lonxml::debug("not time");
1.15      albertel 1349: 	    next;
                   1350: 	}
1.98      albertel 1351: 	if (exists($queuedata->{"$key\0locked"})) {
1.33      albertel 1352: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel 1353: 	    next;
                   1354: 	}
1.98      albertel 1355: 	push(@possible,[$key,$slot_data{'endtime'}]);
                   1356:     }
                   1357:     if (@possible) {
                   1358:         # sort entries in order by slot end time
                   1359: 	@possible = sort { $a->[1] <=> $b->[1] } @possible;
                   1360: 	# pick one of the first ten entries
                   1361: 	my $max=($#possible < 10) ? $#possible : 10;
                   1362: 	return $possible[int(rand($max))][0];
1.14      albertel 1363:     }
                   1364:     return undef;
                   1365: }
                   1366: 
1.15      albertel 1367: sub find_mid_grade {
1.30      albertel 1368:     my ($queue,$symb,$cdom,$cnum)=@_;
1.16      albertel 1369:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.15      albertel 1370:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1371:     if ($todo) {
1.30      albertel 1372: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel 1373: 	if ($who eq $me) { return $todo; }
                   1374:     }
                   1375:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel 1376:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel 1377:     foreach my $key (keys(%locks)) {
                   1378: 	my $who=$locks{$key};
                   1379: 	if ($who eq $me) {
                   1380: 	    $todo=$key;
                   1381: 	    $todo=~s/\0locked$//;
                   1382: 	    return $todo;
                   1383: 	}
                   1384:     }
                   1385:     return undef;
                   1386: }
                   1387: 
1.32      albertel 1388: sub lock_key {
                   1389:     my ($queue,$todo)=@_;
                   1390:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                   1391:     my (undef,$cid)=&Apache::lonxml::whichuser();
                   1392:     my $cnum=$env{'course.'.$cid.'.num'};
                   1393:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1394:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                   1395: 					$cdom,$cnum);
1.33      albertel 1396:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel 1397:     if ($success eq 'ok') {
                   1398: 	return 1;
                   1399:     }
                   1400:     return 0;
                   1401: }
                   1402: 
1.86      albertel 1403: sub get_queue_symb_status {
1.85      albertel 1404:     my ($queue,$symb,$cdom,$cnum) = @_;
                   1405:     if (!defined($cdom) || !defined($cnum)) {
                   1406: 	my (undef,$cid)=&Apache::lonxml::whichuser();
                   1407: 	$cnum=$env{'course.'.$cid.'.num'};
                   1408: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1409:     }
                   1410:     my $regexp="^$symb\0";
                   1411:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1412:     my ($tmp)=%queue;
                   1413:     if ($tmp=~/^error: 2 /) { return; }
                   1414:     my @users;
                   1415:     foreach my $key (sort(keys(%queue))) {
                   1416: 	next if ($key=~/locked$/);
                   1417: 	next if ($key=~/timestamp$/);
                   1418: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1419: 	push(@users,"$uname:$udom");
                   1420:     }
                   1421:     return @users;
                   1422: }
                   1423: 
1.14      albertel 1424: sub get_from_queue {
1.30      albertel 1425:     my ($queue)=@_;
1.14      albertel 1426:     my $result;
                   1427:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                   1428:     my $cnum=$env{'course.'.$cid.'.num'};
                   1429:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel 1430:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel 1431:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel 1432:     if ($todo) { return $todo; }
1.95      albertel 1433:     my $attempts=0;
1.14      albertel 1434:     while (1) {
1.95      albertel 1435: 	if ($attempts > 2) {
                   1436: 	    # tried twice to get a queue entry, giving up
                   1437: 	    return (undef,'unable');
                   1438: 	}
1.14      albertel 1439: 	my $starttime=time;
1.83      albertel 1440: 	&Apache::lonnet::cput($queue,{"$symb\0timestamp"=>$starttime},
                   1441: 			      $cdom,$cnum);
1.33      albertel 1442: 	&Apache::lonxml::debug("$starttime");
1.14      albertel 1443: 	my $regexp="^$symb\0queue\0";
1.97      albertel 1444: 	my $range= ($attempts < 1 ) ? '0-100' : '0-400';
                   1445: 
1.98      albertel 1446: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel 1447: 	#make a pass looking for a user _not_ in my section
1.14      albertel 1448: 	if ($env{'request.course.sec'}) {
1.33      albertel 1449: 	    &Apache::lonxml::debug("sce");
1.30      albertel 1450: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
                   1451: 					\%queue,$cdom,$cnum);
1.33      albertel 1452: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel 1453: 	}
1.33      albertel 1454: 	# no one _not_ in our section so look for any user that is
                   1455: 	# ready for grading
1.14      albertel 1456: 	if (!$todo) {
1.33      albertel 1457: 	    &Apache::lonxml::debug("no sce");
1.98      albertel 1458: 	    $todo=&pick_from_queue_data($queue,undef,\%queue,$cdom,$cnum);
1.33      albertel 1459: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel 1460: 	}
                   1461: 	# no user to grade 
                   1462: 	if (!$todo) { last; }
1.33      albertel 1463: 	&Apache::lonxml::debug("got $todo");
1.14      albertel 1464: 	# otherwise found someone so lets try to lock them
1.32      albertel 1465: 	# unless someone else already picked them
1.95      albertel 1466: 	if (!&lock_key($queue,$todo)) {
                   1467: 	    $attempts++;
                   1468: 	    next;
                   1469: 	}
1.14      albertel 1470: 	my (undef,$endtime)=
1.30      albertel 1471: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel 1472: 				 $cdom,$cnum);
1.33      albertel 1473: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel 1474: 	# someone else already modified the queue, 
                   1475: 	# perhaps our picked user wass already fully graded between
                   1476: 	# when we picked him and when we locked his record? so lets
                   1477: 	# double check.
                   1478: 	if ($endtime != $starttime) {
                   1479: 	    my ($key,$value)=
1.30      albertel 1480: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel 1481: 				     $cdom,$cnum);
1.33      albertel 1482: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel 1483: 	    if ($key eq $todo && ref($value)) {
                   1484: 	    } else {
1.30      albertel 1485: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel 1486: 				     $cdom,$cnum);
1.33      albertel 1487: 		&Apache::lonxml::debug("del");
1.95      albertel 1488: 		$attempts++;
1.14      albertel 1489: 		next;
                   1490: 	    }
                   1491: 	}
1.33      albertel 1492: 	&Apache::lonxml::debug("last $todo");
1.14      albertel 1493: 	last;
                   1494:     }
                   1495:     return $todo;
                   1496: }
                   1497: 
1.49      albertel 1498: sub select_user {
                   1499:     my ($symb,$cid)=&Apache::lonxml::whichuser();
                   1500: 
1.59      albertel 1501:     my @chosen_sections=
                   1502: 	&Apache::loncommon::get_env_multiple('form.chosensections');
1.49      albertel 1503:     &Apache::grades::init_perm();
1.59      albertel 1504:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist(\@chosen_sections,);
1.49      albertel 1505:     &Apache::grades::reset_perm();
1.63      albertel 1506:     
                   1507:     my $result;
                   1508:     if (!(grep(/^all$/,@chosen_sections))) {
                   1509: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1510: 	    '</tt>.</p> '."\n";
                   1511:     }
                   1512:     $result.='<table border="1">';
1.49      albertel 1513: 
                   1514:     foreach my $student (sort {lc($$fullname{$a}) cmp lc($$fullname{$b}) } keys %$fullname) {
                   1515: 	my ($uname,$udom) = split(/:/,$student);
1.59      albertel 1516: 	
1.84      albertel 1517: 	my $cnum=$env{'course.'.$cid.'.num'};
                   1518: 	my $cdom=$env{'course.'.$cid.'.domain'};
1.88      albertel 1519: 	my %status = &get_student_status($symb,$cdom,$cnum,$udom,$uname,
                   1520: 					 'Task');
1.49      albertel 1521: 	my $queue = 'none';
1.58      albertel 1522: 	my $cannot_grade;
                   1523: 	if ($status{'reviewqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1524: 	    $queue = 'reviewqueue';
1.58      albertel 1525: 	    if ($status{'reviewqueue'} eq 'in_progress') {
                   1526: 		$cannot_grade=1;
                   1527: 	    }
                   1528: 	} elsif ($status{'gradingqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1529: 	    $queue = 'gradingqueue';
1.58      albertel 1530: 	    if ($status{'gradingqueue'} eq 'in_progress') {
                   1531: 		$cannot_grade=1;
                   1532: 	    }
1.49      albertel 1533: 	}
                   1534: 	my $todo = 
                   1535: 	    &Apache::lonnet::escape(&encode_queue_key($symb,$udom,$uname));
1.58      albertel 1536: 	if ($cannot_grade) {
                   1537: 	    $result.='<tr><td>&nbsp;</td><td>'.$fullname->{$student}.
                   1538: 		'</td><td>';
                   1539: 	} else {
1.62      albertel 1540: 	    my $seclist;
                   1541: 	    foreach my $sec (@chosen_sections) {
                   1542: 		$seclist.='<input type="hidden" name="chosensections" 
                   1543:                                value="'.$sec.'" />';
                   1544: 	    }
1.102     albertel 1545: 	    my $studentdis = $student;
                   1546: 	    $studentdis =~ tr/:/@/;
1.58      albertel 1547: 	    $result.=<<RESULT;
1.49      albertel 1548: <tr>
                   1549:   <td>
1.115     albertel 1550:     <form style="display: inline" method="post">
1.49      albertel 1551:       <input type="hidden" name="gradingkey" value="$todo" />
                   1552:       <input type="hidden" name="queue" value="$queue" />
                   1553:       <input type="hidden" name="webgrade" value="no" />
1.52      albertel 1554:       <input type="hidden" name="regrade" value="yes" />
1.62      albertel 1555:       <input type="submit" name="submit" value="Regrade" />
                   1556:       $seclist
1.49      albertel 1557:     </form>
1.102     albertel 1558:   <td>$fullname->{$student} <tt>($studentdis)</tt></td>
1.49      albertel 1559:   <td>
                   1560: RESULT
1.58      albertel 1561:         }
1.49      albertel 1562:         if ($status{'status'} eq 'pass') {
                   1563: 	    $result .= '<font color="green">'.&mt('Passed').'</font>';
                   1564: 	} elsif ($status{'status'} eq 'fail') {
                   1565: 	    $result .= '<font color="red">'.&mt('Failed').'</font>';
                   1566: 	} elsif ($status{'status'} eq 'review') {
                   1567: 	    $result .= '<font color="blue">'.&mt('Under Review').'</font>';
                   1568: 	} elsif ($status{'status'} eq 'ungraded') {
                   1569: 	    $result .= &mt('Ungraded');
                   1570: 	} elsif ($status{'status'} ne '') {
                   1571: 	    $result .= '<font color="orange">'.&mt('Unknown Status').'</font>';
                   1572: 	} else {
                   1573: 	    $result.="&nbsp;";
                   1574: 	}
                   1575: 	if ($status{'version'}) {
                   1576: 	    $result .= ' '.&mt('Version').' '.$status{'version'};
                   1577: 	}
1.101     albertel 1578: 	if ($status{'grader'}) {
                   1579: 	    $result .= ' '.&mt('(Graded by [_1])',$status{'grader'}).' ';
                   1580: 	}
1.49      albertel 1581: 	$result.= '</td><td>';
                   1582: 	if ($status{'reviewqueue'} eq 'enqueued') {
                   1583: 	    $result .= &mt('Awaiting Review');
                   1584: 	} elsif ($status{'reviewqueue'} eq 'locked') {
                   1585: 	    $result .= &mt('Under Review');
1.58      albertel 1586: 	} elsif ($status{'reviewqueue'} eq 'in_progress') {
                   1587: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1588: 	} elsif ($status{'gradingqueue'} eq 'enqueued') {
                   1589: 	    $result .= &mt('Awaiting Grading');
                   1590: 	} elsif ($status{'gradingqueue'} eq 'locked') {
                   1591: 	    $result .= &mt('Being Graded');
1.58      albertel 1592: 	} elsif ($status{'gradingqueue'} eq 'in_progress') {
                   1593: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1594: 	} else {
                   1595: 	    $result.="&nbsp;";
                   1596: 	}
                   1597: 	$result.= '</td></tr>';
                   1598:     }
                   1599:     $result.='</table>';
                   1600:     return $result;
                   1601: }
                   1602: 
                   1603: sub get_student_status {
1.86      albertel 1604:     my ($symb,$cdom,$cnum,$udom,$uname,$type)=@_;
                   1605: 
                   1606:     my %status;
                   1607: 
                   1608:     if ($type eq 'Task') {
                   1609: 	my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
1.49      albertel 1610: 					  $udom,$uname);
1.89      albertel 1611: 	$status{'status'}=$record{'resource.0.status'};
                   1612: 	$status{'version'}=$record{'resource.0.version'};
                   1613: 	$status{'grader'}=$record{'resource.0.regrader'};
1.86      albertel 1614:     }
                   1615:     $status{'reviewqueue'}=
                   1616: 	&check_queue_for_key($cdom,$cnum,'reviewqueue',
                   1617: 			     &encode_queue_key($symb,$udom,$uname));
                   1618:     $status{'gradingqueue'}=
                   1619: 	&check_queue_for_key($cdom,$cnum,'gradingqueue',
                   1620: 			     &encode_queue_key($symb,$udom,$uname));
1.49      albertel 1621:     return %status;
                   1622: }
                   1623: 
1.1       albertel 1624: sub start_ClosingParagraph {
                   1625:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1626:     my $result;
                   1627:     if ($target eq 'web') {
1.13      albertel 1628:     } elsif ($target eq 'webgrade') {
                   1629: 	&Apache::lonxml::startredirection();
1.1       albertel 1630:     }
                   1631:     return $result;
                   1632: }
                   1633: 
                   1634: sub end_ClosingParagraph {
                   1635:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1636:     my $result;
                   1637:     if ($target eq 'web') {
1.13      albertel 1638:     } elsif ($target eq 'webgrade') {
                   1639: 	&Apache::lonxml::endredirection();
1.1       albertel 1640:     }
                   1641:     return $result;
                   1642: }
                   1643: 
1.19      albertel 1644: sub get_id {
                   1645:     my ($parstack,$safeeval)=@_;
                   1646:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                   1647:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                   1648:     return $id;
                   1649: }
                   1650: 
1.1       albertel 1651: my %dimension;
                   1652: sub start_Dimension {
                   1653:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1654:     undef(%dimension);
1.19      albertel 1655:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel 1656:     $Apache::bridgetask::dimension=$dim_id;
                   1657:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                   1658:     undef(@Apache::bridgetask::instance);
1.20      albertel 1659:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
                   1660: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.54      albertel 1661:     &Apache::lonxml::startredirection();
                   1662:     return &internal_location($dim_id);
1.1       albertel 1663: }
                   1664: 
1.13      albertel 1665: sub get_instance {
1.75      albertel 1666:     my ($dim)=@_;
                   1667:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                   1668:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                   1669: 	$rand_alg eq '64bit2' || $rand_alg eq '64bit3' ||
                   1670: 	$rand_alg eq '64bit4' ) {
                   1671: 	&Apache::response::pushrandomnumber();
                   1672: 	my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
                   1673: 	my $num=@order;
                   1674: 	my $version=&get_version();
                   1675: 	my $which=($version-1)%$num;
                   1676: 	return $order[$which];
                   1677:     } else {
                   1678: 	my ($version,$previous) = &get_version();
                   1679: 	my $instance = 
                   1680: 	    $Apache::lonhomework::history{"resource.$version.0.$dim.instance"};
                   1681: 	if (defined($instance)) { return $instance; }
                   1682: 
                   1683: 	&Apache::response::pushrandomnumber();
                   1684: 	my @instances = @{$dimension{'instances'}};
                   1685: 	# remove disabled instances
                   1686: 	for (my $i=0; $i < $#instances; $i++) {
                   1687: 	    if ($dimension{$instances[$i].'.disabled'}) {
                   1688: 		splice(@instances,$i,1);
                   1689: 		$i--;
                   1690: 	    }
                   1691: 	}
                   1692: 	@instances = &Math::Random::random_permutation(@instances);
                   1693: 	$instance  = $instances[($version-1)%scalar(@instances)];
                   1694: 	$Apache::lonhomework::results{"resource.$version.0.$dim.instance"} = 
                   1695: 	    $instance;
1.79      albertel 1696: 	$Apache::lonhomework::results{'INTERNAL_store'} = 1; 
1.75      albertel 1697: 	&Apache::response::poprandomnumber();
                   1698: 	return $instance;
                   1699:     }
1.13      albertel 1700: }
                   1701: 
1.18      albertel 1702: {
                   1703:     my $last_link;
                   1704:     sub end_Dimension {
                   1705: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.54      albertel 1706: 	my $result=&Apache::lonxml::endredirection();
1.25      albertel 1707: 	my $dim=&get_id($parstack,$safeeval);
1.75      albertel 1708: 	my $instance=&get_instance($dim);
1.25      albertel 1709: 	my $version=&get_version();
1.18      albertel 1710: 	if ($target eq 'web') {
1.47      albertel 1711: 	    @Apache::scripttag::parser_env = @_;
                   1712: 	    $result.=&Apache::scripttag::xmlparse($dimension{'intro'});
1.76      albertel 1713: 	    my @instances = $instance;
                   1714: 	    if (&Apache::response::showallfoils()) {
                   1715: 		@instances = @{$dimension{'instances'}};
                   1716: 	    }
                   1717: 	    foreach my $instance (@instances) {
                   1718: 		@Apache::scripttag::parser_env = @_;
                   1719: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.89      albertel 1720: 		if ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass' ||
                   1721: 		    $Apache::lonhomework::history{"resource.$version.0.status"} eq 'fail') {
1.76      albertel 1722: 
1.89      albertel 1723: 		    my $dim_status=$Apache::lonhomework::history{"resource.$version.0.$dim.status"};
1.76      albertel 1724: 		    my $mandatory='Mandatory';
                   1725: 		    if ($Apache::bridgetask::dimensionmandatory{$dim} eq 'N') {
                   1726: 			$mandatory='Optional';
                   1727: 		    }
1.116     albertel 1728: 		    my $dim_info="<div class='LC_$dim_status LC_question_grade'>\n";
1.76      albertel 1729: 		    if ($dim_status eq 'pass') {
                   1730: 			$dim_info.='<h3>Question : you passed this '.$mandatory.' question</h3>';
1.54      albertel 1731: 		    }
1.76      albertel 1732: 		    if ($dim_status eq 'fail') {
                   1733: 			$dim_info.='<h3>Question : you did not pass this '.$mandatory.' question</h3>';
1.53      albertel 1734: 		    }
1.76      albertel 1735: 		    my $man_count=0;
                   1736: 		    my $man_passed=0;
                   1737: 		    my $opt_count=0;
                   1738: 		    my $opt_passed=0;
                   1739: 		    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                   1740: 			if ($dimension{$instance.'.criteria.'.$id.'.mandatory'} 
                   1741: 			    eq 'N') {
                   1742: 			    $opt_count++;
1.89      albertel 1743: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"} eq 'pass') {
1.76      albertel 1744: 				$opt_passed++;
                   1745: 			    }
                   1746: 			} else {
                   1747: 			    $man_count++;
1.89      albertel 1748: 			    if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"} eq 'pass') {
1.76      albertel 1749: 				$man_passed++;
                   1750: 			    }
                   1751: 			}
1.22      albertel 1752: 		    }
1.76      albertel 1753: 		    if ($man_passed eq $man_count) { $man_passed='all'; }
                   1754: 		    my $opt_req=$dimension{$instance.'.optionalrequired'};
                   1755: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
                   1756: 		    $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>";
                   1757: 
                   1758: 		    my $internal_location=&internal_location($dim);
                   1759: 		    $result=~s/\Q$internal_location\E/$dim_info/;
                   1760: 
                   1761: 		    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.89      albertel 1762: 			my $status=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"};
                   1763: 			my $comment=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"};
1.76      albertel 1764: 			my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1765: 			if ($mandatory) {
                   1766: 			    $mandatory='Mandatory';
                   1767: 			} else {
                   1768: 			    $mandatory='Optional';
                   1769: 			}
                   1770: 			if ($status eq 'fail') {
                   1771: 			} elsif ($status eq 'pass') {
                   1772: 			} else {
                   1773: 			    &Apache::lonxml::error("Student viewing a graded bridgetask was shown a status of $status");
                   1774: 			}
                   1775: 			my $status_display=$status;
                   1776: 			$status_display=~s/^([a-z])/uc($1)/e;
                   1777: 			@Apache::scripttag::parser_env = @_;
1.116     albertel 1778: 			$result.=
                   1779: 			    '<div class="LC_'.$status.' LC_criteria"><h4>'
                   1780: 			    .$mandatory.' Criteria</h4><p>';
1.76      albertel 1781: 			@Apache::scripttag::parser_env = @_;
                   1782: 			$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
1.116     albertel 1783: 			$result.='</p><p class="LC_grade">'.$status_display.'</p>';
1.89      albertel 1784: 			if ($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}) {
1.116     albertel 1785: 			    $result.='<p class="LC_comment">'.&mt('Comment: [_1]',$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}).'</p>';
1.76      albertel 1786: 			}
                   1787: 			$result.='</div>';
1.22      albertel 1788: 		    }
                   1789: 		}
                   1790: 	    }
1.18      albertel 1791: 	} elsif ($target eq 'webgrade') {
1.47      albertel 1792: 	    # in case of any side effects that we need
                   1793: 	    @Apache::scripttag::parser_env = @_;
                   1794: 	    &Apache::scripttag::xmlparse($dimension{'intro'});
                   1795: 	    @Apache::scripttag::parser_env = @_;
                   1796: 	    &Apache::scripttag::xmlparse($dimension{$instance.'.text'});
1.18      albertel 1797: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.116     albertel 1798: 		my $link='LC_GRADE_criteria_'.$instance.'_'.$id;
1.89      albertel 1799: 		my $status=$Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.status"};
1.116     albertel 1800: 		$result.='<div class="LC_GRADE_criteria" id="'.$link.'">'."\n".
1.111     albertel 1801: 		    '<a name="'.$link.'" />'."\n".
                   1802: 		    '<a name="next_'.$last_link.'" />'."\n".
1.116     albertel 1803: 		    '<div class="LC_GRADE_criteriatext">'."\n";
1.47      albertel 1804: 		@Apache::scripttag::parser_env = @_;
                   1805: 		$result.=&Apache::scripttag::xmlparse($dimension{$instance.'.criteria.'.$id});
1.111     albertel 1806: 		$result.='</div>'."\n".
1.18      albertel 1807: 		    #$dimension{$instance.'.criteria.'.$id}.
1.116     albertel 1808: 		    '<div class="LC_GRADE_grade">'."\n".
                   1809: 		    '<label class="LC_GRADE_ungraded"><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.&mt('Ungraded').'</label>'."\n".
                   1810: 		    '<label class="LC_GRADE_fail"><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label>'."\n".
                   1811: 		    '<label class="LC_GRADE_pass"><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.&mt('Pass').'</label>'."\n".
                   1812: 		    '<label class="LC_GRADE_review"><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.&mt('Review').'</label>'."\n".
1.111     albertel 1813: 		    '</div>'."\n".
1.116     albertel 1814: 		    '<label class="LC_GRADE_comment">'.&mt('Additional Comment for Student')."\n".
                   1815: 		    '<textarea class="LC_GRADE_comment_area" name="HWVAL_comment_'.$link.'">'.&HTML::Entities::encode($Apache::lonhomework::history{"resource.$version.0.$dim.$instance.$id.comment"}).'</textarea>'."\n".
1.111     albertel 1816: 		    '</label>'."\n".
1.116     albertel 1817: 		    '<ul class="LC_GRADE_navbuttons">'."\n".
1.111     albertel 1818: 		    '<li><a href="#'.$last_link.'">Prev</a></li>'."\n".
                   1819: 		    '<li><a href="#next_'.$link.'">Next</a></li>'."\n".
                   1820: 		    '</ul>'."\n".
                   1821:                     '</div>'."\n";
1.113     albertel 1822: 		$result.=&grading_history($version,$dim,$instance,$id);
1.18      albertel 1823: 		$last_link=$link;
                   1824: 	    }
1.22      albertel 1825: 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
1.19      albertel 1826: 	    my $optional_passed=0;
1.20      albertel 1827: 	    my $mandatory_failed=0;
                   1828: 	    my $ungraded=0;
                   1829: 	    my $review=0;
1.19      albertel 1830: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.89      albertel 1831: 		my $status=$Apache::lonhomework::results{"resource.$version.0.$dim.$instance.$id.status"}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
                   1832: 		$Apache::lonhomework::results{"resource.$version.0.$dim.$instance.$id.comment"}=$env{'form.HWVAL_comment_criteria_'.$instance.'_'.$id};
1.20      albertel 1833: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
                   1834: 		if ($status eq 'pass') {
                   1835: 		    if (!$mandatory) { $optional_passed++; }
                   1836: 		} elsif ($status eq 'fail') {
                   1837: 		    if ($mandatory) { $mandatory_failed++; }
1.21      albertel 1838: 		} elsif ($status eq 'review') {
                   1839: 		    $review++;
1.20      albertel 1840: 		} elsif ($status eq 'ungraded') {
                   1841: 		    $ungraded++;
1.21      albertel 1842: 		} else {
1.47      albertel 1843: 		    $ungraded++;
1.19      albertel 1844: 		}
                   1845: 	    }
1.20      albertel 1846: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
                   1847: 		$mandatory_failed++;
                   1848: 	    }
1.21      albertel 1849: 	    &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 1850: 	    if ($review) {
1.89      albertel 1851: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1852: 		    'review';
1.20      albertel 1853: 	    } elsif ($ungraded) {
1.89      albertel 1854: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1855: 		    'ungraded';
1.20      albertel 1856: 	    } elsif ($mandatory_failed) {
1.89      albertel 1857: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1858: 		    'fail';
1.20      albertel 1859: 	    } else {
1.91      albertel 1860: 		$Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
1.22      albertel 1861: 		    'pass';
1.20      albertel 1862: 	    }
1.69      albertel 1863: 	} else {
                   1864: 	    # any other targets no output
                   1865: 	    undef($result);
1.13      albertel 1866: 	}
1.18      albertel 1867: 	return $result;
1.1       albertel 1868:     }
                   1869: }
                   1870: 
1.113     albertel 1871: sub grading_history {
                   1872:     my ($version,$dim,$instance,$criteria) = @_;
                   1873:     if (!&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
                   1874: 	return '';
                   1875:     }
                   1876:     my ($result,$grader);
                   1877:     my $scope="resource.$version.0.$dim.$instance.$criteria";
                   1878:     foreach my $t (1..$Apache::lonhomework::history{'version'}) {
                   1879: 	if (exists($Apache::lonhomework::history{$t.':resource.0.regrader'})) {
                   1880: 	    my ($gname,$gdom) = 
                   1881: 		split('@',$Apache::lonhomework::history{$t.':resource.0.regrader'});
                   1882: 	    my $fullname = &Apache::loncommon::plainname($gname,$gdom);
                   1883: 	    $grader = &Apache::loncommon::aboutmewrapper($fullname,
                   1884: 							 $gname,$gdom);
                   1885: 	}
                   1886: 	my $entry;
                   1887: 	if (exists($Apache::lonhomework::history{"$t:$scope.status"})) {
                   1888: 	    $entry.="<tt>".$Apache::lonhomework::history{"$t:$scope.status"}.'</tt>';
                   1889: 	}
                   1890: 	if (exists($Apache::lonhomework::history{"$t:$scope.comment"})) {
                   1891: 	    $entry.=' comment: "'.$Apache::lonhomework::history{"$t:$scope.comment"}.'"';
                   1892: 	}
                   1893: 	if ($entry) {
                   1894: 	    $result.= "<li>$grader : $entry </li>";
                   1895: 	}
                   1896:     }
                   1897:     if ($result) {
1.117     albertel 1898: 	return '<ul class="LC_GRADE_pastgrading">'.$result.'</ul>';
1.113     albertel 1899:     }
                   1900:     return '';
                   1901: }
                   1902: 
1.1       albertel 1903: sub start_IntroParagraph {
1.87      albertel 1904:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel 1905:     my $result;
1.13      albertel 1906:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1907: 	if ($tagstack->[-2] eq 'Dimension') {
1.87      albertel 1908: 	    $dimension{'intro'}=&Apache::lonxml::get_all_text('/introparagraph',$parser,$style);
1.47      albertel 1909: 	
                   1910: 	} elsif ($target eq 'webgrade') {
1.87      albertel 1911: 	    &Apache::lonxml::get_all_text('/introparagraph',$parser,$style);
1.1       albertel 1912: 	}
1.47      albertel 1913: 	
1.1       albertel 1914:     }
                   1915:     return $result;
                   1916: }
                   1917: 
                   1918: sub end_IntroParagraph {
                   1919: }
                   1920: 
                   1921: sub start_Instance {
                   1922:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel 1923:     my $id=&get_id($parstack,$safeeval);
                   1924:     push(@{$dimension{'instances'}},$id);
                   1925:     push(@Apache::bridgetask::instance,$id);
                   1926:     push(@Apache::bridgetask::instancelist,$id);
1.20      albertel 1927:     $dimension{$id.'.optionalrequired'}=
1.19      albertel 1928: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.75      albertel 1929:     my $disabled = &Apache::lonxml::get_param('Disabled',$parstack,$safeeval);
                   1930:     if (lc($disabled) eq 'yes') {
                   1931: 	$dimension{$id.'.disabled'}='1';
                   1932:     }
1.1       albertel 1933:     return '';
                   1934: }
                   1935: 
                   1936: sub end_Instance {
                   1937: }
                   1938: 
                   1939: sub start_InstanceText {
1.87      albertel 1940:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.47      albertel 1941:     my $instance_id=$Apache::bridgetask::instance[-1];
1.87      albertel 1942:     my $text=&Apache::lonxml::get_all_text('/instancetext',$parser,$style);
1.13      albertel 1943:     if ($target eq 'web' || $target eq 'webgrade') {
1.47      albertel 1944: 	$dimension{$instance_id.'.text'}=$text;
1.1       albertel 1945:     }
                   1946:     return '';
                   1947: }
                   1948: 
                   1949: sub end_InstanceText {
                   1950:     return '';
                   1951: }
                   1952: 
                   1953: sub start_Criteria {
1.87      albertel 1954:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.9       albertel 1955:     my $instance_id=$Apache::bridgetask::instance[-1];
1.87      albertel 1956:     my $criteria=&Apache::lonxml::get_all_text('/criteria',$parser,$style);
1.21      albertel 1957:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.19      albertel 1958: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel 1959: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel 1960: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                   1961: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel 1962: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                   1963:     }
                   1964:     return '';
                   1965: }
                   1966: 
1.47      albertel 1967: sub end_Criteria {
                   1968: }
                   1969: 
1.4       albertel 1970: sub proctor_validation_screen {
                   1971:     my ($slot) = @_;
                   1972:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5       albertel 1973:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.44      albertel 1974:     my $name=&Apache::loncommon::plainname($user,$domain);
                   1975:     
1.4       albertel 1976:     my $msg;
1.11      albertel 1977:     if ($env{'form.proctorpassword'}) {
1.4       albertel 1978: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                   1979: 	    .'</font></p>';
                   1980:     }
1.47      albertel 1981:     if (!$env{'form.proctordomain'}) { $env{'form.proctordomain'}=$domain; }
1.4       albertel 1982:     my $result= (<<ENDCHECKOUT);
                   1983: <h2>Proctor Validation</h2>
                   1984:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   1985:     $msg
1.115     albertel 1986: <form name="checkout" method="post" action="$env{'request.uri'}">
1.4       albertel 1987: <input type="hidden" name="validate" value="yes" />
                   1988: <input type="hidden" name="submitted" value="yes" />
                   1989: <table>
1.44      albertel 1990:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$env{'form.proctorname'}" /></td></tr>
1.4       albertel 1991:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.46      albertel 1992:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$env{'form.proctordomain'}" /></td></tr>
1.4       albertel 1993: </table>
                   1994: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
1.44      albertel 1995: <table border="1">
                   1996:   <tr><td>
                   1997:     <table>
                   1998:       <tr><td colspan="2">Student who should be logged in is:</td></tr>
                   1999:       <tr><td>Name:</td><td>$name</td></tr>
1.45      albertel 2000:       <tr><td>Student ID:</td><td>$env{'environment.id'}</td></tr>
1.44      albertel 2001:       <tr><td>Usename</td><td>$user\@$domain</td></tr>
                   2002:       <tr><td colspan="2"><img src="$url" /></td></tr>
                   2003:     </table>
                   2004:   </tr></td>
                   2005: </table>
1.4       albertel 2006: </form>
                   2007: ENDCHECKOUT
                   2008:     return $result;
                   2009: }
                   2010: 
1.1       albertel 2011: 1;
                   2012: __END__

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