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

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

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