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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.195   ! albertel    4: # $Id: bridgetask.pm,v 1.194 2006/11/07 19:43:37 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.158     www        41: use lib '/home/httpd/lib/perl/';
                     42: use LONCAPA;
                     43:  
1.9       albertel   44: 
1.1       albertel   45: BEGIN {
1.186     albertel   46:     &Apache::lonxml::register('Apache::bridgetask',('Task','IntroParagraph','Dimension','Question','QuestionText','Setup','Instance','InstanceText','Criteria','GraderNote','ClosingParagraph'));
1.1       albertel   47: }
                     48: 
1.169     albertel   49: my %dimension;
1.194     albertel   50: my $top = 'top';
                     51: 
1.9       albertel   52: sub initialize_bridgetask {
                     53:     # id of current Dimension, 0 means that no dimension is current 
                     54:     # (inside <Task> only)
1.178     albertel   55:     @Apache::bridgetask::dimension=();
1.9       albertel   56:     # list of all current Instance ids
1.168     albertel   57:     %Apache::bridgetask::instance=();
1.9       albertel   58:     # list of all Instance ids seen in this problem
                     59:     @Apache::bridgetask::instancelist=();
1.15      albertel   60:     # key of queud user data that we are currently grading
                     61:     $Apache::bridgetask::queue_key='';
1.169     albertel   62:     undef(%dimension);
1.9       albertel   63: }
                     64: 
1.4       albertel   65: sub proctor_check_auth {
1.81      albertel   66:     my ($slot_name,$slot,$type)=@_;
1.11      albertel   67:     my $user=$env{'form.proctorname'};
                     68:     my $domain=$env{'form.proctordomain'};
1.4       albertel   69:     
                     70:     my @allowed=split(",",$slot->{'proctor'});
                     71:     foreach my $possible (@allowed) {
1.138     albertel   72: 	my ($puser,$pdom)=(split(':',$possible));
1.4       albertel   73: 	if ($puser eq $user && $pdom eq $domain) {
1.72      albertel   74: 	    my $authenticated=0;
                     75: 	    if ( $slot->{'secret'} =~ /\S/ &&
                     76: 		 $env{'form.proctorpassword'} eq $slot->{'secret'} ) {
                     77: 		$authenticated=1;
                     78: 	    } else {
                     79: 		
                     80: 		my $authhost=&Apache::lonnet::authenticate($puser,$env{'form.proctorpassword'},$pdom);
                     81: 		if ($authhost ne 'no_host') {
                     82: 		    $authenticated=1;
                     83: 		}
                     84: 	    }
1.150     albertel   85: 	    if ($authenticated) {
1.176     albertel   86: 		&check_in($type,$user,$domain,$slot_name);
1.4       albertel   87: 		return 1;
                     88: 	    }
                     89: 	}
                     90:     }
                     91:     return 0;
                     92: }
                     93: 
1.174     albertel   94: sub check_in {
                     95:     my ($type,$user,$domain,$slot_name) = @_;
                     96:     my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
                     97:     if ( $useslots eq 'map_map') {
                     98: 	&check_in_sequence($user,$domain,$slot_name);
                     99:     } else {
                    100: 	&create_new_version($type,$user,$domain,$slot_name);
                    101:     }
                    102:     return 1;
                    103: }
                    104: 
                    105: sub check_in_sequence {
                    106:     my ($user,$domain,$slot_name) = @_;
                    107:     my $navmap = Apache::lonnavmaps::navmap->new();
1.185     albertel  108:     my ($symb) = &Apache::lonnet::whichuser();
1.174     albertel  109:     my ($map)  = &Apache::lonnet::decode_symb($symb);
1.175     albertel  110:     my @resources = 
                    111: 	$navmap->retrieveResources($map, sub { $_[0]->is_problem() },0,0);
1.174     albertel  112:     my %old_history = %Apache::lonhomework::history;
                    113:     my %old_results = %Apache::lonhomework::results;
                    114: 
                    115:     foreach my $res (@resources) {
                    116: 	&Apache::lonxml::debug("doing ".$res->src);
                    117: 	&Apache::structuretags::initialize_storage($res->symb);
                    118: 	my $type = ($res->is_task()) ? 'Task' : 'problem';
                    119: 	&create_new_version($type,$user,$domain,$slot_name);
                    120: 	&Apache::structuretags::finalize_storage($res->symb);
                    121:     }
                    122:     
                    123:     %Apache::lonhomework::history = %old_history;
                    124:     %Apache::lonhomework::results = %old_results;
                    125: }
                    126: 
1.150     albertel  127: sub create_new_version {
                    128:     my ($type,$user,$domain,$slot_name) = @_;
1.174     albertel  129:     
                    130:     my $id = '0';
1.150     albertel  131:     if ($type eq 'Task') {
                    132: 	# increment version
                    133: 	my $version=
                    134: 	    $Apache::lonhomework::history{'resource.0.version'};
                    135: 	$version++;
1.152     albertel  136: 	&Apache::lonxml::debug("Making version $version");
1.150     albertel  137: 	#clean out all current results
                    138: 	foreach my $key (keys(%Apache::lonhomework::history)) {
                    139: 	    if ($key=~/^resource\.0\./) {
                    140: 		$Apache::lonhomework::results{$key}='';
                    141: 	    }
                    142: 	}
                    143: 	
                    144: 	#setup new version and who did it
1.174     albertel  145:        	$Apache::lonhomework::results{'resource.0.version'}=$version;
                    146: 	$id = "$version.0";
1.178     albertel  147: 	if (!defined($user) || !defined($domain)) {
1.174     albertel  148: 	    $user = $env{'user.name'};
                    149: 	    $domain = $env{'user.domain'};
1.150     albertel  150: 	}
1.174     albertel  151: 	
1.150     albertel  152:     } elsif ($type eq 'problem') {
                    153: 	&Apache::lonxml::debug("authed $slot_name");
1.174     albertel  154:     }
1.181     albertel  155:     if (!defined($user) || !defined($domain)) {
                    156: 	$user = $env{'user.name'};
                    157: 	$domain = $env{'user.domain'};
                    158:     }
                    159: 
                    160:     $Apache::lonhomework::results{"resource.$id.checkedin"}=
                    161: 	$user.':'.$domain;
1.174     albertel  162: 
                    163:     if (defined($slot_name)) {
                    164: 	$Apache::lonhomework::results{"resource.$id.checkedin.slot"}=
                    165: 	    $slot_name;
1.150     albertel  166:     }
                    167: }
                    168: 
1.25      albertel  169: sub get_version {
1.29      albertel  170:     my ($version,$previous);
1.25      albertel  171:     if ($env{'form.previousversion'} && 
1.36      albertel  172: 	$env{'form.previousversion'} ne 'current' &&
1.89      albertel  173: 	defined($Apache::lonhomework::history{'resource.'.$env{'form.previousversion'}.'.0.status'})) {
1.29      albertel  174: 	$version=$env{'form.previousversion'};
                    175: 	$previous=1;
                    176:     } else {
1.150     albertel  177: 	if (defined($Apache::lonhomework::results{'resource.0.version'})) {
                    178: 	    $version=$Apache::lonhomework::results{'resource.0.version'};
                    179: 	} elsif (defined($Apache::lonhomework::history{'resource.0.version'})) {
                    180: 	    $version=$Apache::lonhomework::history{'resource.0.version'};
                    181: 	}
1.29      albertel  182: 	$previous=0;
                    183:     }
                    184:     if (wantarray) {
                    185: 	return ($version,$previous);
1.25      albertel  186:     }
1.29      albertel  187:     return $version;
1.25      albertel  188: }
                    189: 
1.8       albertel  190: sub add_previous_version_button {
1.25      albertel  191:     my ($status)=@_;
1.8       albertel  192:     my $result;
1.89      albertel  193:     if ($Apache::lonhomework::history{'resource.0.version'} eq '') {
1.25      albertel  194: 	return '';
                    195:     }
1.89      albertel  196:     if ($Apache::lonhomework::history{'resource.0.version'} < 2 &&
1.29      albertel  197: 	$status ne 'NEEDS_CHECKIN') {
1.25      albertel  198: 	return '';
                    199:     }
1.29      albertel  200:     my $version=&get_version();
                    201:     if ($env{'form.previousversion'} ne '' &&
                    202: 	$env{'form.previousversion'} eq $version) {
                    203: 	$result.="<h3>".&mt("Showing previous version [_1]",$version).
                    204: 	    "</h3>\n";
                    205:     }
                    206:     my @to_show;
1.89      albertel  207:     foreach my $test_version (1..$Apache::lonhomework::history{'resource.0.version'}) {
                    208: 	if (defined($Apache::lonhomework::history{'resource.'.$test_version.'.0.status'})) {
1.29      albertel  209: 	    push(@to_show,$test_version);
                    210: 	}
                    211:     }
                    212:     my $list='<option>'.
                    213: 	join("</option>\n<option>",@to_show).
                    214: 	     "</option>\n";
1.36      albertel  215:     $list.='<option value="current">'.&mt('Current').'</option>';
1.115     albertel  216:     $result.='<form name="getprevious" method="post" action="';
1.29      albertel  217:     my $uri=$env{'request.uri'};
                    218:     if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    219:     $result.=$uri.'">'.
                    220: 	&mt(' Show a previously done version: [_1]','<select onchange="this.form.submit()" name="previousversion">
                    221: <option>'.&mt('Pick one').'</option>
                    222: '.$list.'
                    223: </select>')."</form>";
1.8       albertel  224:     return $result;
                    225: }
                    226: 
1.13      albertel  227: sub add_grading_button {
1.185     albertel  228:     my (undef,$cid)=&Apache::lonnet::whichuser();
1.59      albertel  229:     my $cnum=$env{'course.'.$cid.'.num'};
                    230:     my $cdom=$env{'course.'.$cid.'.domain'};
1.144     albertel  231:     my %sections = &Apache::loncommon::get_sections($cdom,$cnum);
                    232: 
1.59      albertel  233:     my $size=5;
                    234:     if (scalar(keys(%sections)) < 3) {
                    235: 	$size=scalar(keys(%sections))+2;
                    236:     }
1.122     albertel  237:     my $sec_select = '<select multiple="multiple" name="chosensections" size="'.$size.'">'."\n";
1.59      albertel  238:     $sec_select .= "<option value='all' selected='selected'>all</option>\n";
                    239:     foreach my $sec (sort {lc($a) cmp lc($b)} (keys(%sections))) {
1.122     albertel  240: 	$sec_select .= "<option value=\"$sec\">$sec</option>\n";
1.59      albertel  241:     }
                    242:     $sec_select .= "<option value='none'>none</option></select>\n";
                    243:     
1.29      albertel  244:     my $result=' <input type="submit" name="gradeasubmission" value="'.
1.13      albertel  245: 	&mt("Get a submission to grade").'" />';
                    246:     $result.='<input type="hidden" name="grade_target" value="webgrade" />';
1.40      albertel  247:     if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.34      albertel  248: 	my ($entries,$ready,$locks)=&get_queue_counts('gradingqueue');
1.106     albertel  249: 	$result.='<table><tr>';
                    250: 	$result.='<td rowspan="4">Specify a section: </td><td rowspan="4">'.$sec_select.'</td>';
                    251: 	$result.='<td>'.' <input type="submit" name="reviewagrading" value="'.
                    252: 	    &mt("Select an entry from the grading queue:").'" /> ';
1.34      albertel  253: 
1.107     albertel  254: 	$result.= &mt("[_1] entries, [_2] ready, [_3] being graded",$entries,$ready,$locks).' </td></tr>'."\n";
1.34      albertel  255: 
                    256: 	($entries,$ready,$locks)=&get_queue_counts('reviewqueue');
1.106     albertel  257: 	$result.='<tr><td>'.
                    258: 	    ' <input type="submit" name="reviewasubmission" value="'.
                    259: 	    &mt("Select an entry from the review queue:").'" /> ';
                    260: 	$result.=&mt("[_1] entries, [_2] ready, [_3] being graded",
                    261: 		     $entries,$ready,$locks).'</td></tr>'."\n";
                    262: 	$result.='<tr><td> <input type="submit" name="regradeasubmission" value="'.
                    263: 	    &mt("List of user's grade status").'" /> </td></tr></table>'."\n";
1.105     albertel  264: 	$result.='<p> <input type="submit" name="regradeaspecificsubmission" value="'.
1.106     albertel  265: 	    &mt("Regrade specific user:").'" />'."\n";
1.105     albertel  266: 	$result.='<input type="text" size="12" name="gradinguser" />';
                    267: 	$result.=&Apache::loncommon::select_dom_form($env{'user.domain'},
                    268: 						     'gradingdomain');
                    269: 	$result.=' '.
                    270: 	    &Apache::loncommon::selectstudent_link('gradesubmission',
                    271: 						   'gradinguser',
                    272: 						   'gradingdomain');
                    273: 	$result.=&Apache::loncommon::studentbrowser_javascript();
1.123     albertel  274: 	$result.= '</p>';
1.144     albertel  275:     }
1.13      albertel  276:     return $result;
                    277: }
                    278: 
1.22      albertel  279: sub add_request_another_attempt_button {
1.38      albertel  280:     my ($text)=@_;
                    281:     if (!$text) { $text="Request another attempt"; }
1.25      albertel  282:     my $result;
1.36      albertel  283:     my $symb=&Apache::lonnet::symbread();
1.149     albertel  284:     # not a slot access based resource
                    285:     my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
                    286:     if ($useslots =~ /^\s*no\s*$/i) {
                    287: 	return '';
                    288:     }
                    289: 
1.37      albertel  290:     my ($slot_name,$slot)=&Apache::slotrequest::check_for_reservation($symb);
1.38      albertel  291:     my $action='get_reservation';
1.37      albertel  292:     if ($slot_name) {
1.38      albertel  293: 	$text="Change reservation.";
                    294: 	$action='change_reservation';
1.37      albertel  295: 	my $description=&Apache::slotrequest::get_description($slot_name,
                    296: 							      $slot);
                    297: 	$result.=(<<STUFF);
                    298: <p> Will be next available: $description </p>
                    299: STUFF
                    300:     }
1.38      albertel  301:     
                    302:     if ($env{'request.enc'}) { $symb=&Apache::lonenc::encrypted($symb); }
1.158     www       303:     $symb=&escape($symb);
1.115     albertel  304:     $result.='<form method="post" action="/adm/slotrequest">'.
1.38      albertel  305: 	'<input type="hidden" name="symb" value="'.$symb.'" />'.
                    306: 	'<input type="hidden" name="command" value="'.$action.'" />'.
                    307: 	'<input type="submit" name="requestattempt" value="'.
                    308: 	&mt($text).'" />'.
                    309: 	'</form>';
1.25      albertel  310:     return $result;
1.22      albertel  311: }
                    312: 
1.30      albertel  313: sub preserve_grade_info {
                    314:     my $result;
                    315:     # if we are viewing someone else preserve that info
                    316:     if (defined $env{'form.grade_symb'}) {
                    317: 	foreach my $field ('symb','courseid','domain','username') {
                    318: 	    $result .= '<input type="hidden" name="grade_'.$field.
                    319: 		'" value="'.$env{"form.grade_$field"}.'" />'."\n";
                    320: 	}
                    321:     }
                    322:     return $result;
                    323: }
                    324: 
1.53      albertel  325: sub style {
1.125     albertel  326:     my ($target) = @_;
                    327:     if ($target eq 'web'
                    328: 	|| $target eq 'webgrade') {
1.192     albertel  329: 	my $style = (<<STYLE);
1.126     albertel  330: <link rel="stylesheet" type="text/css" href="/res/adm/includes/task.css" />
1.53      albertel  331: STYLE
1.192     albertel  332:         if ($env{'browser.type'} eq 'explorer'
                    333: 	    && $env{'browser.os'} eq 'win' ) {
                    334: 	    if ($env{'browser.version'} < 7) {
                    335: 		$style .= (<<STYLE);
                    336: <link rel="stylesheet" type="text/css" href="/res/adm/includes/task_ie.css" />
                    337: STYLE
                    338:             } else {
                    339: 		$style .= (<<STYLE);
                    340: <link rel="stylesheet" type="text/css" href="/res/adm/includes/task_ie7.css" />
                    341: STYLE
                    342: 	    }
                    343: 	}
1.193     albertel  344: 	return $style;
1.125     albertel  345:     }
                    346:     return;
1.53      albertel  347: }
                    348: 
1.54      albertel  349: sub show_task {
                    350:     my ($status,$previous)=@_;
                    351:     if (!$previous && (
                    352: 		       ( $status eq 'CLOSED' ) ||
                    353: 		       ( $status eq 'BANNED') ||
                    354: 		       ( $status eq 'UNAVAILABLE') ||
                    355: 		       ( $status eq 'NOT_IN_A_SLOT') ||
                    356: 		       ( $status eq 'NEEDS_CHECKIN') ||
                    357: 		       ( $status eq 'WAITING_FOR_GRADE') ||
1.150     albertel  358: 		       ( $status eq 'INVALID_ACCESS') ||
                    359: 		       ( &get_version() eq ''))) {
1.54      albertel  360: 	return 0;
                    361:     }
1.64      albertel  362:     if ($env{'form.donescreen'}) { return 0; }
1.54      albertel  363:     return 1;
                    364: }
                    365: 
1.173     albertel  366: my @delay;
                    367: sub nest { 
                    368:     if (@delay) {
                    369: 	return $delay[-1];
                    370:     } else {
                    371: 	return;
                    372:     }
                    373: }
                    374: 
                    375: sub nested_parse {
                    376:     my ($str,$env,$args) = @_;
                    377:     my @old_env = @Apache::scripttag::parser_env;
                    378:     @Apache::scripttag::parser_env = @$env;
                    379:     if (exists($args->{'set_dim_id'})) {
                    380: 	&enable_dimension_parsing($args->{'set_dim_id'});
                    381:     }
                    382:     push(@delay,(($args->{'delayed_dim_results'})? 1 : 0));
                    383:     my $result = &Apache::scripttag::xmlparse($$str);
                    384:     pop(@delay);
                    385:     if (exists($args->{'set_dim_id'})) {
                    386: 	&disable_dimension_parsing();
                    387:     }
                    388:     @Apache::scripttag::parser_env = @old_env;
                    389:     if ($args->{'delayed_dim_results'}) {
                    390: 	my $dim = &get_dim_id();
1.180     albertel  391: 	&Apache::lonxml::debug(" tossing out $result ");
                    392: 	&Apache::lonxml::debug(" usining out $dim 's  ". $dimension{$dim}{'result'});
1.173     albertel  393: 	return $dimension{$dim}{'result'};
                    394:     }
                    395:     return $result;
                    396: }
                    397: 
1.54      albertel  398: sub internal_location {
                    399:     my ($id)=@_;
                    400:     return '<!-- LONCAPA_INTERNAL_ADD_TASK_STATUS'.$id.' -->';
                    401: }
                    402: 
1.60      albertel  403: sub submission_time_stamp {
1.185     albertel  404:     my ($symb,$courseid,$udom,$uname)=&Apache::lonnet::whichuser();
1.60      albertel  405:     my $submissiontime;
1.89      albertel  406:     my $version=$Apache::lonhomework::history{'resource.0.version'};
1.60      albertel  407:     for (my $v=$Apache::lonhomework::history{'version'};$v>0;$v--) {
1.183     albertel  408: 	if (defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.bridgetask.portfiles'})
                    409: 	    && defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.tries'})) {
1.60      albertel  410: 	    $submissiontime=$Apache::lonhomework::history{$v.':timestamp'};
1.182     albertel  411: 	    last;
1.60      albertel  412: 	}
                    413:     }
                    414:     my $result;
                    415:     if ($submissiontime) {
1.89      albertel  416: 	my $slot_name=$Apache::lonhomework::history{'resource.'.$version.'.0.checkedin.slot'};
1.60      albertel  417: 	my %slot=&Apache::lonnet::get_slot($slot_name);
                    418: 	my $diff = $slot{'endtime'} - $submissiontime;
1.71      albertel  419: 	my ($color,$when)=('#FF6666','after');
                    420: 	if ($diff > 0) { ($color,$when)=('#336600','before'); }
1.60      albertel  421: 	my $info;
1.182     albertel  422: 	$diff = abs($diff);
1.60      albertel  423: 	if ($diff%60) { $info=($diff%60).' seconds'; }
                    424: 	$diff=int($diff/60);
                    425: 	if ($diff%60) { $info=($diff%60).' minutes '.$info; }
                    426: 	$diff=int($diff/60);
                    427: 	if ($diff) {    $info=$diff.' hours '.$info; }
                    428: 	$result='<p><font color="'.$color.'">'.
1.182     albertel  429: 	    &mt('Student submitted [_1] [_2] the deadline. '.
                    430: 		'(Submission was at [_3], end of period was [_4].)',
                    431: 		$info,$when,
                    432: 		&Apache::lonlocal::locallocaltime($submissiontime),
                    433: 		&Apache::lonlocal::locallocaltime($slot{'endtime'})).
1.60      albertel  434: 		'</font></p>';
                    435:     }
                    436:     return $result;
                    437: }
                    438: 
1.119     albertel  439: sub file_list {
                    440:     my ($files,$uname,$udom) = @_;
                    441:     if (!defined($uname) || !defined($udom)) {
1.185     albertel  442: 	(undef,undef,$udom,$uname) = &Apache::lonnet::whichuser();
1.119     albertel  443:     }
1.70      albertel  444:     my $file_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
1.119     albertel  445: 
1.120     albertel  446:     my $file_list="<ul class=\"LC_GRADING_handininfo\">\n";
1.119     albertel  447:     foreach my $partial_file (split(',',$files)) {
1.70      albertel  448: 	my $file=$file_url.$partial_file;
                    449: 	$file=~s|/+|/|g;
                    450: 	&Apache::lonnet::allowuploaded('/adm/bridgetask',$file);
1.161     albertel  451: 	$file_list.='<li><span style="white-space: nowrap;"><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
                    452: 	    &Apache::loncommon::icon($file).'" alt="file icon" border="0" /> '.$file.
                    453: 	    '</a></span></li>'."\n";
1.70      albertel  454:     }
                    455:     $file_list.="</ul>\n";
1.119     albertel  456:     return $file_list;
                    457: }
                    458: 
1.163     albertel  459: sub grade_mode {
                    460:     if ($env{'form.regrade'} || $env{'form.regradeaspecificsubmission'}) {
                    461: 	return 'regrade';
                    462:     }
                    463:     return 'queue_grade';
                    464: }
                    465: 
1.119     albertel  466: sub webgrade_standard_info {
                    467:     my ($version)=&get_version();
                    468: 
                    469:     my $file_list = &file_list($Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"});
1.70      albertel  470: 
1.163     albertel  471:     my %lt=('done'   => 'Next Item',
                    472: 	    'stop'   => 'Quit Grading',
                    473: 	    'fail'   => 'Fail Rest',
                    474: 	    'cancel' => 'Cancel',
                    475: 	    );
1.170     albertel  476:     %lt=&Apache::lonlocal::texthash(%lt);
1.163     albertel  477: 
1.70      albertel  478:     my $result=<<INFO;
1.120     albertel  479:   <div class="LC_GRADING_maincontrols">
1.163     albertel  480: INFO
                    481: 
                    482:     if (&grade_mode() eq 'regrade') {
                    483: 	$result.=<<INFO;
                    484:     <input type="submit" name="cancel" value="$lt{'cancel'}" />
                    485: INFO
                    486:     }
                    487: 
                    488:     $result.=<<INFO;
1.111     albertel  489:     <input type="submit" name="next" value="$lt{'done'}" />
                    490:     <input type="submit" name="stop" value="$lt{'stop'}" />
1.143     albertel  491:     <input type="button" name="fail" value="$lt{'fail'}" 
                    492:            onclick="javascript:onFailRest()" />
1.111     albertel  493:   </div>
1.70      albertel  494:   $file_list
                    495: INFO
                    496:     return $result;
                    497: }
                    498: 
1.166     albertel  499: sub done_screen {
                    500:     my ($version) = @_;
                    501:     my $title=&Apache::lonnet::gettitle();
                    502:     my @files=split(',',$Apache::lonhomework::history{'resource.'.$version.'.0.bridgetask.portfiles'});
1.185     albertel  503:     my (undef,undef,$domain,$user)= &Apache::lonnet::whichuser();
1.166     albertel  504:     my $files = '<ul>';
1.167     albertel  505:     my $msg;
1.166     albertel  506:     foreach my $file (@files) {
                    507: 	my $url="/uploaded/$domain/$user/portfolio$file";
                    508: 	if (! &Apache::lonnet::stat_file($url)) {
                    509: 	    $file = &mt('<span class="LC_error"> Nonexistent file:</span> '.
                    510: 			'<span class="LC_filename">[_1]</span>',$file);
1.167     albertel  511: 	    $msg .= "<p>Submitted non-existant file $file</p>\n";
1.166     albertel  512: 	} else {
                    513: 	    $file = '<span class="LC_filename">'.$file.'</span>';
1.167     albertel  514: 	    $msg .= "<p>Submitted file $file</p>\n";
1.166     albertel  515: 	}
                    516: 	$files .= '<li>'.$file.'</li>';
                    517:     }
                    518:     $files.='</ul>';
1.167     albertel  519:     my $subject = "Submission message for $title";
                    520:     my ($message_status,$comment_status);
                    521:     my $setting = $env{'course.'.$env{'request.course.id'}.'.task_messages'};
                    522:     $setting =~ s/^\s*(\S*)\s*$/$1/;
                    523:     $setting = lc($setting);
                    524:     if ($setting eq 'only_student'
                    525: 	|| $setting eq 'student_and_user_notes_screen') {
                    526: 	$message_status =
                    527: 	    &Apache::lonmsg::user_normal_msg($user,$domain,$subject,$msg);
                    528: 	$message_status = '<p>'.&mt('Message sent to user: [_1]',
                    529: 				    $message_status).' </p>';
                    530:     }
                    531:     if ($setting eq 'student_and_user_notes_screen') {
                    532: 	$comment_status = 
                    533: 	    &Apache::lonmsg::store_instructor_comment($subject.'<br />'.
                    534: 						      $msg,$user,$domain);
                    535: 	$comment_status = '<p>'.&mt('Message sent to instructor: [_1]',
                    536: 				    $comment_status).' </p>';
                    537:     }
1.166     albertel  538:     return <<DONESCREEN;
                    539: <h2>$title</h2>
                    540: <p> Files submitted: $files </p>
                    541: <p> You are now done with this Bridge Task </p>
                    542: <hr />
                    543: <p> <a href="/adm/logout">Logout</a> </p>
                    544: <p> <a href="/adm/roles">Change to a different course</a> </p>
1.167     albertel  545: $message_status
                    546: $comment_status
1.166     albertel  547: DONESCREEN
                    548: 
                    549: }
                    550: 
1.1       albertel  551: sub start_Task {
1.87      albertel  552:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel  553: 
1.4       albertel  554:     my ($status,$accessmsg,$slot);
1.179     albertel  555:     &Apache::structuretags::init_problem_globals('Task');
1.16      albertel  556:     if ($target ne 'webgrade') {
                    557: 	&Apache::structuretags::initialize_storage();
                    558: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.74      albertel  559: 	if ($env{'request.state'} eq 'construct') {
                    560: 	    &Apache::structuretags::setup_rndseed($safeeval);
                    561: 	}
1.16      albertel  562:     } 
                    563: 
1.4       albertel  564:     $Apache::lonhomework::parsing_a_task=1;
1.141     albertel  565: 
                    566:     my $name;
                    567:     if ($target eq 'web' || $target eq 'webgrade') {
                    568: 	$name = &Apache::structuretags::get_resource_name($parstack,$safeeval);
                    569:     }
                    570: 
1.145     albertel  571:     my ($result,$form_tag_start);
                    572:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'tex'
                    573: 	|| $target eq 'edit') {
                    574: 	($result,$form_tag_start) =
                    575: 	    &Apache::structuretags::page_start($target,$token,$tagstack,
                    576: 					       $parstack,$parser,$safeeval,
1.146     albertel  577: 					       $name,&style($target));
1.145     albertel  578: 	$result .= '<div class="LC_task">'."\n";
                    579:     }
1.123     albertel  580: 
1.74      albertel  581:     if ($target eq 'web' && $env{'request.state'} ne 'construct') {
1.147     albertel  582: 	if ($Apache::lonhomework::queuegrade
                    583: 	    || $Apache::lonhomework::modifygrades) {
1.141     albertel  584: 	    $result.='<form name="gradesubmission" method="post" action="';
1.13      albertel  585: 	    my $uri=$env{'request.uri'};
                    586: 	    if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
1.141     albertel  587: 	    $result.=$uri.'">'.&add_grading_button()."</form>";
1.38      albertel  588: 	    my $symb=&Apache::lonnet::symbread();
1.40      albertel  589: 	    if (&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
1.141     albertel  590: 		$result.='<form method="post" name="slotrequest" action="/adm/slotrequest">'.
1.40      albertel  591: 		    '<input type="hidden" name="symb" value="'.$symb.'" />'.
                    592: 		    '<input type="hidden" name="command" value="showslots" />'.
                    593: 		    '<input type="submit" name="requestattempt" value="'.
                    594: 		    &mt('Show Slot list').'" />'.
                    595: 		    '</form>';
1.108     albertel  596: 		my $target_id = 
                    597: 		    &Apache::lonstathelpers::make_target_id({symb => $symb,
                    598: 							     part => '0'});
1.141     albertel  599: 		$result.='<form method="post" name="gradingstatus" action="/adm/statistics">'.
1.108     albertel  600: 		    '<input type="hidden" name="problemchoice" value="'.$target_id.'" />'.
                    601: 		    '<input type="hidden" name="reportSelected" value="grading_analysis" />'.
                    602: 		    '<input type="submit" name="grading" value="'.
                    603: 		    &mt('Show Grading Status').'" />'.
                    604: 		    '</form>';
1.40      albertel  605: 	    }
1.13      albertel  606: 	}
1.8       albertel  607:     }
1.74      albertel  608:     if ($target eq 'web' && $env{'request.state'} eq 'construct') {
                    609: 	$form_tag_start.=&Apache::structuretags::problem_web_to_edit_header($env{'form.rndseed'});
                    610:     }
1.163     albertel  611:     if ($target eq 'web' 
                    612: 	|| ($target eq 'grade' && !$env{'form.webgrade'}) 
                    613: 	|| $target eq 'answer' 
                    614: 	|| $target eq 'tex') {
1.29      albertel  615: 	my ($version,$previous)=&get_version();
1.14      albertel  616: 	($status,$accessmsg,my $slot_name,$slot) = 
1.81      albertel  617: 	    &Apache::lonhomework::check_slot_access('0','Task');
1.152     albertel  618: 	if ($status eq 'CAN_ANSWER' && $version eq '') {
1.177     albertel  619: 	    # CAN_ANSWER mode, and no current version, unproctored access
1.174     albertel  620: 	    # thus self-checkedin
1.177     albertel  621: 	    &check_in('Task',undef,undef,$slot_name);
1.152     albertel  622: 	    &add_to_queue('gradingqueue',{'type' => 'Task',
                    623: 					  'time' => time,
                    624: 					  'slot' => $slot_name});
1.150     albertel  625: 	    ($version,$previous)=&get_version();
                    626: 	}
                    627: 
1.9       albertel  628: 	push(@Apache::inputtags::status,$status);
1.14      albertel  629: 	$Apache::inputtags::slot_name=$slot_name;
1.1       albertel  630: 	my $expression='$external::datestatus="'.$status.'";';
1.89      albertel  631: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$version.0.solved"}.'";';
1.1       albertel  632: 	&Apache::run::run($expression,$safeeval);
                    633: 	&Apache::lonxml::debug("Got $status");
1.141     albertel  634: 	$result.=&add_previous_version_button($status);
1.54      albertel  635: 	if (!&show_task($status,$previous)) {
1.87      albertel  636: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.1       albertel  637: 	    if ( $target eq "web" ) {
1.74      albertel  638: 		if ($env{'request.state'} eq 'construct') {
                    639: 		    $result.=$form_tag_start;
                    640: 		}
1.4       albertel  641: 		my $msg;
1.1       albertel  642: 		if ($status eq 'UNAVAILABLE') {
                    643: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3       albertel  644: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    645: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.38      albertel  646: 		    $msg.=&add_request_another_attempt_button("Sign up for time to work.");
1.4       albertel  647: 		} elsif ($status eq 'NEEDS_CHECKIN') {
                    648: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
                    649: 			'</h1>'.&proctor_validation_screen($slot);
1.22      albertel  650: 		} elsif ($status eq 'WAITING_FOR_GRADE') {
                    651: 		    $msg.='<h1>'.&mt('Your submission is in the grading queue.').'</h1>';
1.64      albertel  652: 		} elsif ($env{'form.donescreen'}) {
1.167     albertel  653: 		    $result .= &done_screen($version);
1.1       albertel  654: 		} elsif ($status ne 'NOT_YET_VIEWED') {
                    655: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
                    656: 		}
                    657: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
                    658: 		    $msg.='The problem '.$accessmsg;
                    659: 		}
                    660: 		$result.=$msg.'<br />';
                    661: 	    } elsif ($target eq 'tex') {
                    662: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
                    663: 		if ($status eq 'UNAVAILABLE') {
                    664: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
                    665: 		} else {
                    666: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
                    667: 		}
1.22      albertel  668: 	    } elsif ($target eq 'grade' && !$env{'form.webgrade'}) {
1.4       albertel  669: 		if ($status eq 'NEEDS_CHECKIN') {
1.83      albertel  670: 		    if(&proctor_check_auth($slot_name,$slot,'Task')
                    671: 		       && defined($Apache::inputtags::slot_name)) {
1.148     albertel  672: 			my $result=
                    673: 			    &add_to_queue('gradingqueue',
1.152     albertel  674: 					  {'type' => 'Task',
1.148     albertel  675: 					   'time' => time,
                    676: 					   'slot' => 
                    677: 					       $Apache::inputtags::slot_name});
1.77      albertel  678: 			&Apache::lonxml::debug("add_to_queue said $result");
                    679: 		    }
1.4       albertel  680: 		}
1.1       albertel  681: 	    }
                    682: 	} elsif ($target eq 'web') {
1.141     albertel  683: 
1.57      albertel  684: 	    $result.=&preserve_grade_info();
1.194     albertel  685: 	    $result.=&internal_location(); 
1.36      albertel  686: 	    $result.=$form_tag_start.
                    687: 		'<input type="hidden" name="submitted" value="yes" />';
1.54      albertel  688: 	    &Apache::lonxml::startredirection();
1.1       albertel  689: 	}
1.21      albertel  690:     } elsif ( ($target eq 'grade' && $env{'form.webgrade'}) ||
                    691: 	      $target eq 'webgrade') {
1.32      albertel  692: 	my $webgrade='yes';
1.21      albertel  693: 	if ($target eq 'webgrade') {
1.141     albertel  694: 	    $result.= "\n".'<div class="LC_GRADING_task">'."\n".
1.124     albertel  695: 		'<script type="text/javascript" 
1.126     albertel  696:                          src="/res/adm/includes/task_grading.js"></script>';
1.49      albertel  697: 	    #$result.='<br />Review'.&show_queue('reviewqueue');
                    698: 	    #$result.='<br />Grade'.&show_queue('gradingqueue');
1.30      albertel  699: 	}
1.194     albertel  700: 
1.105     albertel  701: 	my ($todo,$status_code,$msg)=&get_key_todo($target);
1.33      albertel  702: 
                    703: 	if ($todo) {
                    704: 	    &setup_env_for_other_user($todo,$safeeval);
                    705: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
                    706: 	    $result.="\n".'<table><tr><td>Found '.
                    707: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
                    708: 	    $form_tag_start.=
                    709: 		'<input type="hidden" name="gradingkey" value="'.
1.158     www       710: 		&escape($todo).'" />';
1.33      albertel  711: 	    $Apache::bridgetask::queue_key=$todo;
                    712: 	    &Apache::structuretags::initialize_storage();
                    713: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.110     albertel  714: 	    if ($target eq 'webgrade' && $status_code eq 'selected') {
                    715: 		$form_tag_start.=
                    716: 		    '<input type="hidden" name="queuemode" value="selected" />';
1.33      albertel  717: 	    }
1.15      albertel  718: 	} else {
1.33      albertel  719: 	    if ($target eq 'webgrade') {
                    720: 		$result.="\n";
1.81      albertel  721: 		my $back='<p><a href="/adm/flip?postdata=return:">'.
                    722: 		    &mt('Return to resource').'</a></p>';
1.33      albertel  723: 		if      ($status_code eq 'stop') {
1.81      albertel  724: 		    $result.='<b>'.&mt("Stopped grading.").'</b>'.$back;
1.163     albertel  725: 		} elsif ($status_code eq 'cancel') {
                    726: 		    $result.='<b>'.&mt("Cancelled grading.").'</b>'.$back;
1.164     albertel  727: 		} elsif ($status_code eq 'never_versioned') {
                    728: 		    $result.='<b>'.
                    729: 			&mt("Requested user has never accessed the task.").
                    730: 			'</b>'.$back;
1.165     albertel  731: 		} elsif ($status_code =~ /still_open:(.*)/) {
                    732: 		    my $date = &Apache::lonlocal::locallocaltime($1);
                    733: 		    $result.='<b>'.
                    734: 			&mt("Task is still open, will close at [_1].",$date).
                    735: 			'</b>'.$back;
1.33      albertel  736: 		} elsif ($status_code eq 'lock_failed') {
1.105     albertel  737: 		    $result.='<b>'.&mt("Failed to lock the requested record.")
1.81      albertel  738: 			.'</b>'.$back;
1.33      albertel  739: 		} elsif ($status_code eq 'unlock') {
1.81      albertel  740: 		    $result.='<b>'.&mt("Unlocked the requested record.")
                    741: 			.'</b>'.$back;
1.33      albertel  742: 		    $result.=&show_queue($env{'form.queue'},1);
                    743: 		} elsif ($status_code eq 'show_list') {
                    744: 		    $result.=&show_queue($env{'form.queue'},1);
1.49      albertel  745: 		} elsif ($status_code eq 'select_user') {
                    746: 		    $result.=&select_user();
1.95      albertel  747: 		} elsif ($status_code eq 'unable') {
                    748: 		    $result.='<b>'.&mt("Unable to aqcuire a user to grade.").'</b>'.$back;
1.105     albertel  749: 		} elsif ($status_code eq 'not_allowed') {
                    750: 		    $result.='<b>'.&mt('Not allowed to grade the requested user.').' '.$msg.'</b>'.$back;
1.33      albertel  751: 		} else {
1.81      albertel  752: 		    $result.='<b>'.&mt("No user to be graded.").'</b>'.$back;
1.32      albertel  753: 		}
1.21      albertel  754: 	    }
1.33      albertel  755: 	    $webgrade='no';
1.163     albertel  756: 	}
                    757: 	if (!$todo || $env{'form.cancel'}) {
1.87      albertel  758: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser,$style);
1.32      albertel  759: 	}
                    760: 	if ($target eq 'webgrade' && defined($env{'form.queue'})) {
1.61      albertel  761: 	    if ($webgrade eq 'yes') {
                    762: 		$result.=&submission_time_stamp();
                    763: 	    }
1.32      albertel  764: 	    $result.=$form_tag_start;
                    765: 	    $result.='<input type="hidden" name="webgrade" value="'.
                    766: 		$webgrade.'" />';
                    767: 	    $result.='<input type="hidden" name="queue" value="'.
                    768: 		$env{'form.queue'}.'" />';
1.52      albertel  769: 	    if ($env{'form.regrade'}) {
                    770: 		$result.='<input type="hidden" name="regrade" value="'.
                    771: 		    $env{'form.regrade'}.'" />';
                    772: 	    }
1.62      albertel  773: 	    if ($env{'form.chosensections'}) {
                    774: 		my @chosen_sections=
                    775: 		    &Apache::loncommon::get_env_multiple('form.chosensections');
                    776: 		foreach my $sec (@chosen_sections) {
                    777: 		    $result.='<input type="hidden" name="chosensections" 
                    778:                                value="'.$sec.'" />';
                    779: 		}
                    780: 	    }
1.70      albertel  781: 	    if ($webgrade eq 'yes') { $result.=&webgrade_standard_info(); }
1.15      albertel  782: 	}
1.110     albertel  783: 	if ($target eq 'webgrade') {
1.120     albertel  784: 	    $result.="\n".'<div id="LC_GRADING_criterialist">';
1.194     albertel  785: 	    &Apache::lonxml::startredirection();
1.110     albertel  786: 	}
1.74      albertel  787:     } elsif ($target eq 'edit') {
1.141     albertel  788: 	$result.=$form_tag_start.
1.74      albertel  789: 	    &Apache::structuretags::problem_edit_header();
                    790: 	$Apache::lonxml::warnings_error_header=
                    791: 	    &mt("Editor Errors - these errors might not effect the running of the problem, but they will likely cause problems with further use of the Edit mode. Please use the EditXML mode to fix these errors.")."<br />";
                    792: 	my $temp=&Apache::edit::insertlist($target,$token);
                    793: 	$result.=$temp;
1.1       albertel  794:     } else {
                    795: 	# page_start returned a starting result, delete it if we don't need it
                    796: 	$result = '';
                    797:     }
                    798:     return $result;
                    799: }
                    800: 
1.165     albertel  801: sub get_task_end_time {
                    802:     my ($queue_entry,$symb,$udom,$uname) = @_;
                    803: 
                    804:     my $end_time;
                    805:     if (my $slot = &slotted_access($queue_entry)) {
                    806: 	my %slot_data=&Apache::lonnet::get_slot($slot);
                    807: 	$end_time = $slot_data{'endtime'};
                    808:     } else {
                    809: 	$end_time = &Apache::lonhomework::due_date('0',$symb,
                    810: 						   $udom,$uname);
                    811:     }
                    812:     return $end_time;
                    813: }
                    814: 
1.32      albertel  815: sub get_key_todo {
                    816:     my ($target)=@_;
                    817:     my $todo;
1.33      albertel  818: 
                    819:     if (defined($env{'form.reviewasubmission'})) {
1.54      albertel  820: 	&Apache::lonxml::debug("review a submission....");
1.33      albertel  821: 	$env{'form.queue'}='reviewqueue';
                    822: 	return (undef,'show_list');
                    823:     }
                    824: 
                    825:     if (defined($env{'form.reviewagrading'})) {
                    826: 	&Apache::lonxml::debug("review a grading....");
                    827: 	$env{'form.queue'}='gradingqueue';
                    828: 	return (undef,'show_list');
                    829:     }
                    830: 
1.49      albertel  831:     if (defined($env{'form.regradeasubmission'})) {
                    832: 	&Apache::lonxml::debug("regrade a grading....");
                    833: 	$env{'form.queue'}='none';
                    834: 	return (undef,'select_user');
                    835:     }
                    836: 
1.105     albertel  837: 
1.138     albertel  838:     my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.105     albertel  839: 
                    840:     #need to try both queues..
                    841:     if (defined($env{'form.regradeaspecificsubmission'}) &&
                    842: 	defined($env{'form.gradinguser'})               &&
                    843: 	defined($env{'form.gradingdomain'})               ) {
1.185     albertel  844: 	my ($symb,$cid)=&Apache::lonnet::whichuser();
1.105     albertel  845: 	my $cnum  = $env{'course.'.$cid.'.num'};
                    846: 	my $cdom  = $env{'course.'.$cid.'.domain'};
                    847: 	my $uname = $env{'form.gradinguser'};
                    848: 	my $udom  = $env{'form.gradingdomain'};
                    849: 
                    850: 	my $gradingkey=&encode_queue_key($symb,$udom,$uname);
                    851: 
                    852: 	my $queue;
                    853: 
                    854: 	if      (&in_queue('gradingqueue',$symb,$cdom,$cnum,$udom,$uname)) {
                    855: 	    $env{'form.queue'} = $queue = 'gradingqueue';
                    856: 	} elsif (&in_queue('reviewqueue' ,$symb,$cdom,$cnum,$udom,$uname)) {
                    857: 	    $env{'form.queue'} = $queue = 'reviewqueue';
                    858: 	}
                    859: 	
                    860: 	if (!$queue) {
                    861: 	    $env{'form.queue'} = $queue = 'none';
                    862: 	    #not queued so doing either a re or pre grade
1.164     albertel  863: 	    my %status = &Apache::lonnet::restore($symb,$cid,$udom,$uname);
                    864: 	    if ($status{'resource.0.version'} < 1) {
                    865: 		return (undef,'never_versioned');
                    866: 	    }
1.105     albertel  867: 	    return ($gradingkey);
                    868: 	}
                    869: 
1.165     albertel  870: 	if ($queue) {
                    871: 	    my $queue_entry = &get_queue_data($queue,$udom,$uname);
                    872: 	
                    873: 	    my $end_time = &get_task_end_time($queue_entry,$symb,
                    874: 					      $udom,$uname);
                    875: 	    if ($end_time > time) {
                    876: 		return (undef,"still_open:$end_time");
                    877: 	    }
                    878: 	}
                    879: 
1.105     albertel  880: 	my $who=&queue_key_locked($queue,$gradingkey);
                    881: 	if ($who eq $me) {
                    882: 	    #already have the lock
1.158     www       883: 	    $env{'form.gradingkey'}=&escape($gradingkey);
1.163     albertel  884: 	    &Apache::lonxml::debug("already locked");
1.105     albertel  885: 	    return ($gradingkey);
                    886: 	}
                    887: 	
                    888: 	if (!defined($who)) {
                    889: 	    if (&lock_key($queue,$gradingkey)) {
1.163     albertel  890: 		&Apache::lonxml::debug("newly locked");
1.105     albertel  891: 		return ($gradingkey);
                    892: 	    } else {
                    893: 		return (undef,'lock_failed');
                    894: 	    }
                    895: 	}
                    896: 
                    897: 	#otherwise (defined($who) && $who ne $me) some else has it...
                    898: 	return (undef,'not_allowed',
                    899: 		&mt('Another user ([_1]) currently has the record for [_2] locked.',
1.138     albertel  900: 		    $who,$env{'form.gradinguser'}.':'.$env{'form.gradingdomain'}));
1.105     albertel  901:     }
                    902: 
                    903: 
1.32      albertel  904:     my $queue=$env{'form.queue'};
1.33      albertel  905: 
1.32      albertel  906:     if (!defined($queue)) {
                    907: 	$env{'form.queue'}=$queue='gradingqueue';
                    908:     }
1.33      albertel  909: 
1.158     www       910:     my $gradingkey=&unescape($env{'form.gradingkey'});
1.33      albertel  911: 
1.49      albertel  912:     if ($env{'form.queue'} eq 'none') {
                    913: 	if (defined($env{'form.gradingkey'})) {
                    914: 	    if ($target eq 'webgrade') {
                    915: 		if ($env{'form.stop'}) {
                    916: 		    return (undef,'stop');
1.163     albertel  917: 		} elsif ($env{'form.cancel'}) {
                    918: 		    return (undef,'cancel');
1.49      albertel  919: 		} elsif ($env{'form.next'}) {
1.59      albertel  920: 		    return (undef,'select_user');
1.49      albertel  921: 		}
                    922: 	    }
                    923: 	    return ($gradingkey,'selected');
                    924: 	} else {
1.59      albertel  925: 	    return (undef,'select_user');
1.49      albertel  926: 	}
                    927:     }
1.32      albertel  928:     if (defined($env{'form.queue'}) && defined($env{'form.gradingkey'})
1.33      albertel  929: 	&& !defined($env{'form.gradingaction'}) 
                    930: 	&& $env{'form.queuemode'} eq 'selected') {
                    931: 	
                    932: 	my $who=&queue_key_locked($queue,$gradingkey);
                    933: 	if ($who eq $me) {
                    934: 	    &Apache::lonxml::debug("Found a key was given to me");
                    935: 	    return ($gradingkey,'selected');
                    936: 	} else {
                    937: 	    return (undef,'show_list');
                    938: 	}
                    939: 
                    940:     }
                    941: 
                    942:     if ($target eq 'webgrade' && $env{'form.queuemode'} eq 'selected') {
                    943: 	if ($env{'form.gradingaction'} eq 'resume') {
                    944: 	    delete($env{'form.gradingaction'});
                    945: 	    &Apache::lonxml::debug("Resuming a key");
1.32      albertel  946: 	    return ($gradingkey);
1.33      albertel  947: 	} elsif ($env{'form.gradingaction'} eq 'unlock') {
                    948: 	    &Apache::lonxml::debug("Unlocking a key ".
                    949: 				     &check_queue_unlock($queue,$gradingkey,1));
                    950: 	    return (undef,'unlock');
                    951: 	} elsif ($env{'form.gradingaction'} eq 'select') {
                    952: 	    &Apache::lonxml::debug("Locking a key");
                    953: 	    if (&lock_key($queue,$gradingkey)) {
                    954: 		&Apache::lonxml::debug("Success $queue");
                    955: 		return ($gradingkey);
                    956: 	    }
                    957: 	    &Apache::lonxml::debug("Failed $queue");
                    958: 	    return (undef,'lock_failed');
1.32      albertel  959: 	}
                    960:     }
1.33      albertel  961: 
                    962:     if ($env{'form.queuemode'} ne 'selected') {
                    963: 	# don't get something new from the queue if they hit the stop button
1.163     albertel  964:     	if (!(($env{'form.cancel'} || $env{'form.stop'}) 
                    965: 	      && $target eq 'webgrade') 
1.33      albertel  966: 	    && !$env{'form.gradingaction'}) {
                    967: 	    &Apache::lonxml::debug("Getting anew $queue");
                    968: 	    return (&get_from_queue($queue));
                    969: 	} else {
                    970: 	    return (undef,'stop');
                    971: 	}
1.32      albertel  972:     }
1.33      albertel  973:     return (undef,undef)
1.32      albertel  974: }
1.94      albertel  975: 
                    976: sub minimize_storage {
                    977:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    978: 	if ($key =~ /regrader$/) { next; }
                    979: 	if ($Apache::lonhomework::results{$key} eq
                    980: 	    $Apache::lonhomework::history{$key}) {
                    981: 	    delete($Apache::lonhomework::results{$key});
                    982: 	}
                    983:     }
                    984: }
                    985: 
1.1       albertel  986: sub end_Task {
                    987:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    988:     my $result='';
                    989:     my $status=$Apache::inputtags::status['-1'];
1.29      albertel  990:     my ($version,$previous)=&get_version();
1.1       albertel  991:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.15      albertel  992: 	$target eq 'tex') {
1.69      albertel  993: 	if ($target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.1       albertel  994: 	    if ($target eq 'web') {
1.54      albertel  995: 		if (&show_task($status,$previous)) {
                    996: 		    $result.=&Apache::lonxml::endredirection();
                    997: 		}
1.64      albertel  998: 		if ($status eq 'CAN_ANSWER' && !$previous && 
                    999: 		    !$env{'form.donescreen'}) {
1.15      albertel 1000: 		    $result.="\n".'<table border="1">'.
1.28      albertel 1001: 			&Apache::inputtags::file_selector("$version.0",
                   1002: 							  "bridgetask","*",
1.46      albertel 1003: 							  'portfolioonly',
                   1004: 							  '
                   1005: <h2>'.&mt('Submit Portfolio Files for Grading').'</h2>
                   1006: <p>'.&mt('Indicate the files from your portfolio to be evaluated in grading this task.').'</p>').
1.9       albertel 1007: 							  "</table>";
1.77      albertel 1008: 		}
1.78      albertel 1009: 		if (!$previous && $status ne 'SHOW_ANSWER' &&
                   1010: 		    &show_task($status,$previous)) {
1.9       albertel 1011: 		    $result.=&Apache::inputtags::gradestatus('0');
1.64      albertel 1012: 		    $result.='</form>';
1.116     albertel 1013: 		    my $action = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.64      albertel 1014: 		    $result.=<<DONEBUTTON;
1.115     albertel 1015: <form name="done" method="post" action="$action">
1.64      albertel 1016:    <input type="hidden" name="donescreen" value="1" />
                   1017:    <input type="submit" value="Done" />
                   1018: </form>
                   1019: DONEBUTTON
1.77      albertel 1020:                 }
1.56      albertel 1021: 		if (&show_task($status,$previous) &&
1.89      albertel 1022: 		    $Apache::lonhomework::history{"resource.$version.0.status"} =~ /^(pass|fail)$/) {
                   1023: 		    my $bt_status=$Apache::lonhomework::history{"resource.$version.0.status"};
1.54      albertel 1024: 		    my $title=&Apache::lonnet::gettitle();
1.80      albertel 1025: 
1.149     albertel 1026: 		    my $start_time;
                   1027: 
1.80      albertel 1028: 		    my $slot_name=
1.89      albertel 1029: 			$Apache::lonhomework::history{"resource.$version.0.checkedin.slot"};
1.149     albertel 1030: 		    if ($slot_name) {
                   1031: 			my %slot=&Apache::lonnet::get_slot($slot_name);
                   1032: 
                   1033: 			$start_time=$slot{'starttime'}
                   1034: 		    } else {
                   1035: 			$start_time= 
                   1036: 			    &Apache::lonnet::EXT('resource.0.opendate');
                   1037: 		    }
                   1038: 		    $start_time=&Apache::lonlocal::locallocaltime($start_time);
1.54      albertel 1039: 
1.116     albertel 1040: 		    my $status = "\n<div class='LC_$bt_status LC_criteria'>\n";
1.54      albertel 1041: 		    
                   1042: 		    if ($bt_status eq 'pass')  {
                   1043: 			$status.='<h2>You passed the '.$title.' given on '.
1.80      albertel 1044: 			    $start_time.'</h2>';
1.54      albertel 1045: 		    }
                   1046: 		    if ($bt_status eq 'fail')  {
                   1047: 			$status.='<h2>You did not pass the '.$title.' given on '.
1.80      albertel 1048: 			    $start_time.'</h2>';
1.54      albertel 1049: 			if (!$previous) {
                   1050: 			    $status.=&add_request_another_attempt_button();
                   1051: 			}
                   1052: 		    }
1.194     albertel 1053: 		    $status.='</div>';
1.54      albertel 1054: 		    my $man_count=0;
1.194     albertel 1055: 		    my $man_passed=0;
1.54      albertel 1056: 		    my $opt_count=0;
                   1057: 		    my $opt_passed=0;
1.194     albertel 1058: 		    my $dim = $top;
                   1059: 		    foreach my $id (@{$dimension{$dim}{'criterias'}}) {
                   1060: 			my $status = &get_criteria('status',$version,$dim,$id);
                   1061: 			if ($dimension{$dim}{'criteria.'.$id.'.mandatory'}
1.54      albertel 1062: 			    eq 'N') {
                   1063: 			    $opt_count++;
1.194     albertel 1064: 			    if ($status eq 'pass') {
1.54      albertel 1065: 				$opt_passed++;
                   1066: 			    }
                   1067: 			} else {
                   1068: 			    $man_count++;
1.194     albertel 1069: 			    if ($status eq 'pass') { $man_passed++; }
1.54      albertel 1070: 			}
                   1071: 		    }
1.194     albertel 1072: 		    if ($man_passed eq $man_count) { $man_passed='all'; }
                   1073: 
1.54      albertel 1074: 		    my $opt_req=&Apache::lonxml::get_param('OptionalRequired',
1.194     albertel 1075: 							   $parstack,$safeeval);
1.54      albertel 1076: 		    if ($opt_req !~ /\S/) { $opt_req='0'; }
1.194     albertel 1077: 
                   1078: 		    $status.="\n<div class='LC_$bt_status LC_criteria LC_task_overall_status'>".&mt('You needed to pass all of the [_1] mandatory components and [_2] of the [_3] optional components, of which you passed [_4].',$man_count,$opt_req,$opt_count,$opt_passed)."</p></div>\n";
                   1079: 
                   1080: 
                   1081: 		    foreach my $id (@{$dimension{$dim}{'criterias'}}) {
                   1082: 			my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
                   1083: 			if ($type eq 'dimension') {
                   1084: 			    $result.=$dimension{$id}{'result'};
                   1085: 			    next;
                   1086: 			}
                   1087: 			my $criteria = 
                   1088: 			    &nested_parse(\$dimension{$dim}{'criteria.'.$id},
                   1089: 					  [@_]);
                   1090: 			$status .= &layout_web_Criteria($dim,$id,$criteria);
                   1091: 		    }
1.54      albertel 1092: 
                   1093: 		    my $internal_location=&internal_location();
                   1094: 		    $result=~s/\Q$internal_location\E/$status/;
                   1095: 		}
1.142     albertel 1096: 		$result.="\n</div>\n".
                   1097: 		    &Apache::loncommon::end_page({'discussion' => 1});
1.1       albertel 1098: 	    }
                   1099: 	}
1.181     albertel 1100: 
                   1101: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
                   1102: 	my %queue_data = ('type' => 'Task',
                   1103: 			  'time' => time,);
                   1104: 	if (defined($Apache::inputtags::slot_name)) {
                   1105: 	    $queue_data{'slot'} = $Apache::inputtags::slot_name;
                   1106: 	} elsif (defined($Apache::lonhomework::history{"resource.$version.0.checkedin.slot"})) {
                   1107: 	    $queue_data{'slot'} = $Apache::lonhomework::history{"resource.$version.0.checkedin.slot"};
                   1108: 	}
                   1109: 	
                   1110: 
1.29      albertel 1111: 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
1.12      albertel 1112: 	    my $award='SUBMITTED';
1.28      albertel 1113: 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
1.20      albertel 1114: 						    'portfiles',\$award);
1.14      albertel 1115: 	    if ($award eq 'SUBMITTED' &&
1.28      albertel 1116: 		$Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}) {
                   1117: 		$Apache::lonhomework::results{"resource.0.tries"}=
                   1118: 		    $Apache::lonhomework::results{"resource.$version.0.tries"}=
                   1119: 		    1+$Apache::lonhomework::history{"resource.$version.0.tries"};
                   1120: 
                   1121: 		$Apache::lonhomework::results{"resource.0.award"}=
                   1122: 		    $Apache::lonhomework::results{"resource.$version.0.award"}=
                   1123: 		    $award;
1.51      albertel 1124: 		$Apache::lonhomework::results{"resource.0.submission"}=
                   1125: 		    $Apache::lonhomework::results{"resource.$version.0.submission"}='';
1.64      albertel 1126: 	    } else {
                   1127: 		delete($Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"});
1.77      albertel 1128: 		$award = '';
1.10      albertel 1129: 	    }
1.4       albertel 1130: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                   1131: 	    &Apache::structuretags::finalize_storage();
1.148     albertel 1132: 	    if ($award eq 'SUBMITTED') {
1.181     albertel 1133: 		&add_to_queue('gradingqueue',\%queue_data);
1.14      albertel 1134: 	    }
1.1       albertel 1135: 	}
1.163     albertel 1136: 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes' 
                   1137: 	    && exists($env{'form.cancel'})) {
                   1138: 	    &check_queue_unlock($env{'form.queue'});
                   1139: 	    &Apache::lonxml::debug(" cancelled grading .".$env{'form.queue'});
                   1140: 	} elsif ($target eq 'grade' && $env{'form.webgrade'} eq 'yes' 
                   1141: 		 && !exists($env{'form.cancel'})) {
1.20      albertel 1142: 	    my $optional_required=
                   1143: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
                   1144: 					   $safeeval);
                   1145: 	    my $optional_passed=0;
                   1146: 	    my $mandatory_failed=0;
                   1147: 	    my $ungraded=0;
                   1148: 	    my $review=0;   
1.21      albertel 1149: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.194     albertel 1150: 	    my $dim = $top;
                   1151: 	    foreach my $id (@{$dimension{$dim}{'criterias'}}) {
                   1152: 		my $link=&link($id);
                   1153: 
                   1154: 		my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
                   1155: 
                   1156: 		if ($type eq 'criteria') {
                   1157: 		    # dimensional 'criteria' don't get assigned grades
                   1158: 		    $Apache::lonhomework::results{"resource.$version.0.$id.status"}=$env{'form.HWVAL_'.$link};
                   1159: 		    $Apache::lonhomework::results{"resource.$version.0.$id.comment"}=$env{'form.HWVAL_comment_'.$link};
                   1160: 		} 
1.20      albertel 1161: 		my $status=
1.194     albertel 1162: 		    $Apache::lonhomework::results{"resource.$version.0.$id.status"};
                   1163: 		my $mandatory=($dimension{$dim}{'criteria.'.$id.'.mandatory'} ne 'N');
                   1164: 
1.20      albertel 1165: 		if ($status eq 'pass') {
                   1166: 		    if (!$mandatory) { $optional_passed++; }
                   1167: 		} elsif ($status eq 'fail') {
                   1168: 		    if ($mandatory) { $mandatory_failed++; }
1.194     albertel 1169: 		} elsif ($status eq 'review') {
                   1170: 		    $review++;
1.20      albertel 1171: 		} elsif ($status eq 'ungraded') {
                   1172: 		    $ungraded++;
1.49      albertel 1173: 		} else {
                   1174: 		    $ungraded++;
                   1175: 		}
1.20      albertel 1176: 	    }
                   1177: 	    if ($optional_passed < $optional_required) {
                   1178: 		$mandatory_failed++;
                   1179: 	    }
1.194     albertel 1180: 	    &Apache::lonxml::debug(" task results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
1.89      albertel 1181: 	    $Apache::lonhomework::results{'resource.0.regrader'}=
1.138     albertel 1182: 		$env{'user.name'}.':'.$env{'user.domain'};
1.20      albertel 1183: 	    if ($review) {
1.89      albertel 1184: 		$Apache::lonhomework::results{"resource.$version.0.status"}='review';
1.20      albertel 1185: 	    } elsif ($ungraded) {
1.89      albertel 1186: 		$Apache::lonhomework::results{"resource.$version.0.status"}='ungraded';
1.20      albertel 1187: 	    } elsif ($mandatory_failed) {
1.89      albertel 1188: 		$Apache::lonhomework::results{"resource.$version.0.status"}='fail';
1.25      albertel 1189: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='incorrect_by_override';
                   1190: 		$Apache::lonhomework::results{"resource.$version.0.award"}='INCORRECT';
                   1191: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='0';
1.185     albertel 1192: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonnet::whichuser();
1.52      albertel 1193: 		
                   1194: 		if ($env{'form.regrade'} ne 'yes') {
                   1195: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                   1196: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                   1197: 		    &Apache::grades::version_portfiles(
                   1198: 						       \%Apache::lonhomework::results,
                   1199: 						       ["$version.0.bridgetask"],$courseid,
                   1200: 						       $symb,$udom,$uname,
                   1201: 						       ["$version.0.bridgetask"]);
                   1202: 		}
1.20      albertel 1203: 	    } else {
1.89      albertel 1204: 		$Apache::lonhomework::results{"resource.$version.0.status"}='pass';
1.25      albertel 1205: 		$Apache::lonhomework::results{"resource.$version.0.solved"}='correct_by_override';
                   1206: 		$Apache::lonhomework::results{"resource.$version.0.award"}='EXACT_ANS';
                   1207: 		$Apache::lonhomework::results{"resource.$version.0.awarded"}='1';
1.185     albertel 1208: 		my ($symb,$courseid,$udom,$uname)=&Apache::lonnet::whichuser();
1.52      albertel 1209: 		if ($env{'form.regrade'} ne 'yes') {
                   1210: 		    $Apache::lonhomework::results{"resource.$version.0.bridgetask.portfiles"}=
                   1211: 			$Apache::lonhomework::history{"resource.$version.0.bridgetask.portfiles"};
                   1212: 		    &Apache::grades::version_portfiles(
                   1213: 						       \%Apache::lonhomework::results,
                   1214: 						       ["$version.0.bridgetask"],$courseid,
                   1215: 						       $symb,$udom,$uname,
                   1216: 						       ["$version.0.bridgetask"]);
                   1217: 		}
1.20      albertel 1218: 	    }
1.89      albertel 1219: 	    $Apache::lonhomework::results{"resource.0.status"}=
                   1220: 		$Apache::lonhomework::results{"resource.$version.0.status"};
1.28      albertel 1221: 	    if (defined($Apache::lonhomework::results{"resource.$version.0.awarded"})) {
1.26      albertel 1222: 		$Apache::lonhomework::results{"resource.0.award"}=
1.50      albertel 1223: 		    $Apache::lonhomework::results{"resource.$version.0.award"};
1.26      albertel 1224: 		$Apache::lonhomework::results{"resource.0.awarded"}=
1.50      albertel 1225: 		    $Apache::lonhomework::results{"resource.$version.0.awarded"};
1.26      albertel 1226: 		$Apache::lonhomework::results{"resource.0.solved"}=
1.50      albertel 1227: 		    $Apache::lonhomework::results{"resource.$version.0.solved"};
1.25      albertel 1228: 	    }
1.94      albertel 1229: 	    &minimize_storage();
1.21      albertel 1230: 	    &Apache::structuretags::finalize_storage();
1.188     albertel 1231: 	    
                   1232: 	    # data stored, now handle queue
                   1233: 	    if ($review) {
                   1234: 		if ($env{'form.queue'} eq 'reviewqueue') {
                   1235: 		    &check_queue_unlock($env{'form.queue'});
                   1236: 		    &Apache::lonxml::debug(" still needs review not changing status.");
                   1237: 		} else {
                   1238: 		    if ($env{'form.queue'} ne 'none') {
                   1239: 			&move_between_queues($env{'form.queue'},'reviewqueue');
                   1240: 		    } else {
                   1241: 			&add_to_queue('reviewqueue',\%queue_data);
                   1242: 		    }
                   1243: 		}
                   1244: 	    } elsif ($ungraded) {
                   1245: 		if ($env{'form.queue'} eq 'reviewqueue') {
                   1246: 		    &Apache::lonxml::debug("moving back.");
                   1247: 		    &move_between_queues($env{'form.queue'},
                   1248: 					 'gradingqueue');
                   1249: 		} elsif ($env{'form.queue'} eq 'none' ) {
                   1250: 		    &add_to_queue('gradingqueue',\%queue_data);	
                   1251: 		} else {
                   1252: 		    &check_queue_unlock($env{'form.queue'});
                   1253: 		}
                   1254: 	    } elsif ($mandatory_failed) {
                   1255: 		&remove_from_queue($env{'form.queue'}); 
                   1256: 	    } else {
                   1257: 		&remove_from_queue($env{'form.queue'});
                   1258: 	    }
1.20      albertel 1259: 	}
1.184     albertel 1260: 	if (exists($Apache::lonhomework::results{'INTERNAL_store'})) {
                   1261: 	    # instance generation occured and hasn't yet been stored
                   1262: 	    &Apache::structuretags::finalize_storage();
                   1263: 	}
1.15      albertel 1264:     } elsif ($target eq 'webgrade') {
1.194     albertel 1265: 	$result.=&Apache::lonxml::endredirection();
                   1266: 	my $dim = $top;
                   1267: 	foreach my $id (@{$dimension{$dim}{'criterias'}} ) {
                   1268: 	    my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
                   1269: 	    if ($type eq 'dimension') {
                   1270: 		# dimensional 'criteria' don't get assigned grades
                   1271: 		next;
                   1272: 	    } else {
                   1273: 		my $criteria =&nested_parse(\$dimension{$dim}{'criteria.'.$id},
                   1274: 					     [@_]);
                   1275: 		$criteria = &layout_webgrade_Criteria($dim,$id,$criteria);
                   1276: 		my $internal_location=&internal_location($id);
                   1277: 		$result=~s/\Q$internal_location\E/$criteria/;
                   1278: 	    }
                   1279: 	}
                   1280:         $result.="</div>";
1.20      albertel 1281: 	#$result.='<input type="submit" name="next" value="'.
                   1282: 	#    &mt('Save &amp; Next').'" /> ';
                   1283: 	#$result.='<input type="submit" name="end" value="'.
                   1284: 	#    &mt('Save &amp; Stop Grading').'" /> ';
                   1285: 	#$result.='<input type="submit" name="throwaway" value="'.
                   1286: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
                   1287: 	#$result.='<input type="submit" name="save" value="'.
                   1288: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.124     albertel 1289: 	$result.='</form>'."\n</div>\n</div>\n".
1.140     albertel 1290: 	    &Apache::loncommon::end_page();
1.1       albertel 1291:     } elsif ($target eq 'meta') {
1.70      albertel 1292: 	$result.=&Apache::response::meta_package_write('Task');
1.77      albertel 1293:         $result.=&Apache::response::meta_stores_write('solved','string',
                   1294: 						      'Problem Status');
                   1295: 	$result.=&Apache::response::meta_stores_write('tries','int_zeropos',
                   1296: 						      'Number of Attempts');
                   1297: 	$result.=&Apache::response::meta_stores_write('awarded','float',
                   1298: 						      'Partial Credit Factor');
                   1299: 	$result.=&Apache::response::meta_stores_write('status','string',
                   1300: 						      'Bridge Task Status');
1.182     albertel 1301:     } elsif ($target eq 'edit') {
                   1302: 	&Apache::structuretags::reset_problem_globals('Task');
                   1303: 	undef($Apache::lonhomework::parsing_a_task);
                   1304: 	return ('','no');
1.1       albertel 1305:     }
1.179     albertel 1306:     &Apache::structuretags::reset_problem_globals('Task');
1.4       albertel 1307:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel 1308:     return $result;
                   1309: }
                   1310: 
1.31      albertel 1311: sub move_between_queues {
                   1312:     my ($src_queue,$dest_queue)=@_;
1.49      albertel 1313:     my $cur_data;
                   1314:     if ($src_queue ne 'none') {
                   1315: 	$cur_data=&get_queue_data($src_queue);
                   1316: 	if (!$cur_data) { return 'not_exist'; }
                   1317:     } else {
                   1318: 	$cur_data = ['none'];
                   1319:     }
1.148     albertel 1320:     my $result=&add_to_queue($dest_queue,$cur_data);
1.31      albertel 1321:     if ($result ne 'ok') {
                   1322: 	return $result;
                   1323:     }
                   1324:     &check_queue_unlock($src_queue);
                   1325:     return &remove_from_queue($src_queue);
1.21      albertel 1326: }
                   1327: 
                   1328: sub check_queue_unlock {
1.32      albertel 1329:     my ($queue,$key,$allow_not_me)=@_;
1.49      albertel 1330:     if ($queue eq 'none') { return 'ok'; }
1.185     albertel 1331:     my ($symb,$cid,$udom,$uname)=&Apache::lonnet::whichuser();
1.32      albertel 1332:     if (!defined($key)) {
1.138     albertel 1333: 	$key="$symb\0queue\0$uname:$udom";
1.32      albertel 1334:     }
1.30      albertel 1335:     my $cnum=$env{'course.'.$cid.'.num'};
                   1336:     my $cdom=$env{'course.'.$cid.'.domain'};
1.138     albertel 1337:     my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.30      albertel 1338:     my $who=&queue_key_locked($queue,$key,$cdom,$cnum);
                   1339:     if  ($who eq $me) {
1.163     albertel 1340: 	&Apache::lonxml::debug("unlocking my own $who");
1.32      albertel 1341: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
                   1342:     } elsif ($allow_not_me) {
1.33      albertel 1343: 	&Apache::lonxml::debug("unlocking $who by $me");
1.32      albertel 1344: 	return &Apache::lonnet::del($queue,["$key\0locked"],$cdom,$cnum);
1.30      albertel 1345:     }
1.32      albertel 1346:     return 'not_owner';
1.21      albertel 1347: }
                   1348: 
1.88      albertel 1349: sub in_queue {
                   1350:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
                   1351:     if ($queue eq 'none') { return 0; }
                   1352:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1353: 	|| !defined($udom) || !defined($uname)) {
1.185     albertel 1354: 	($symb,my $cid,$udom,$uname)=&Apache::lonnet::whichuser();
1.88      albertel 1355: 	$cnum=$env{'course.'.$cid.'.num'};
                   1356: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1357:     }
                   1358: 
                   1359:     my $key=&encode_queue_key($symb,$udom,$uname);
                   1360:     my %results = &Apache::lonnet::get($queue,[$key],$cdom,$cnum);
                   1361: 
                   1362:     if (defined($results{$key})) {
                   1363: 	return 1;
                   1364:     }
                   1365:     return 0;
                   1366: }
                   1367: 
1.21      albertel 1368: sub remove_from_queue {
1.86      albertel 1369:     my ($queue,$symb,$cdom,$cnum,$udom,$uname)=@_;
1.49      albertel 1370:     if ($queue eq 'none') { return 'ok'; }
1.86      albertel 1371:     if (!defined($symb) || !defined($cdom) || !defined($cnum)
                   1372: 	|| !defined($udom) || !defined($uname)) {
1.185     albertel 1373: 	($symb,my $cid,$udom,$uname)=&Apache::lonnet::whichuser();
1.86      albertel 1374: 	$cnum=$env{'course.'.$cid.'.num'};
                   1375: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1376:     }
1.88      albertel 1377:     if (!&in_queue($queue,$symb,$cdom,$cnum,$udom,$uname)) {
                   1378: 	return 'ok';
                   1379:     }
1.86      albertel 1380:     my $key=&encode_queue_key($symb,$udom,$uname);
1.27      albertel 1381:     my @keys=($key,"$key\0locked");
1.31      albertel 1382:     return &Apache::lonnet::del($queue,\@keys,$cdom,$cnum);
1.21      albertel 1383: }
                   1384: 
1.16      albertel 1385: sub setup_env_for_other_user {
                   1386:     my ($queue_key,$safeeval)=@_;
                   1387:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
1.30      albertel 1388:     &Apache::lonxml::debug("setup_env for $queue_key");
1.16      albertel 1389:     $env{'form.grade_symb'}=$symb;
                   1390:     $env{'form.grade_domain'}=$udom;
                   1391:     $env{'form.grade_username'}=$uname;
                   1392:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                   1393:     &Apache::lonxml::initialize_rndseed($safeeval);
                   1394: }
                   1395: 
1.31      albertel 1396: sub get_queue_data {
1.165     albertel 1397:     my ($queue,$udom,$uname)=@_;
1.185     albertel 1398:     my ($symb,$cid,$other_udom,$other_uname)=&Apache::lonnet::whichuser();
1.165     albertel 1399:     if (!$uname || !$udom) {
                   1400: 	$uname=$other_uname;
                   1401: 	$udom =$other_udom;
                   1402:     }
1.31      albertel 1403:     my $cnum=$env{'course.'.$cid.'.num'};
                   1404:     my $cdom=$env{'course.'.$cid.'.domain'};
1.138     albertel 1405:     my $todo="$symb\0queue\0$uname:$udom";
1.31      albertel 1406:     my ($key,$value)=&Apache::lonnet::get($queue,[$todo],$cdom,$cnum);
                   1407:     if ($key eq $todo && ref($value)) {
                   1408: 	return $value;
                   1409:     }
                   1410:     return undef;
                   1411: }
                   1412: 
1.84      albertel 1413: 
1.49      albertel 1414: sub check_queue_for_key {
1.84      albertel 1415:     my ($cdom,$cnum,$queue,$todo)=@_;
                   1416: 
1.49      albertel 1417:     my %results=
                   1418: 	&Apache::lonnet::get($queue,[$todo,"$todo\0locked"],$cdom,$cnum);
                   1419:     
                   1420:     if (exists($results{$todo}) && ref($results{$todo})) {
                   1421: 	if (defined($results{"$todo\0locked"})) {
                   1422: 	    return 'locked';
                   1423: 	}
1.148     albertel 1424: 	if (my $slot=&slotted_access($results{$todo})) {
1.86      albertel 1425: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1426: 	    if ($slot_data{'endtime'} > time) { 
                   1427: 		return 'in_progress';
                   1428: 	    }
1.148     albertel 1429: 	} else {
                   1430: 	    my ($symb) = &decode_queue_key($todo);
                   1431: 	    my $due_date = &Apache::lonhomework::due_date('0',$symb);
                   1432: 	    if ($due_date > time) {
                   1433: 		return 'in_progress';
                   1434: 	    }
1.58      albertel 1435: 	}
1.49      albertel 1436: 	return 'enqueued';
                   1437:     }
                   1438:     return undef;
                   1439: }
                   1440: 
1.14      albertel 1441: sub add_to_queue {
1.82      albertel 1442:     my ($queue,$user_data)=@_;
1.49      albertel 1443:     if ($queue eq 'none') { return 'ok'; }
1.185     albertel 1444:     my ($symb,$cid,$udom,$uname)=&Apache::lonnet::whichuser();
1.82      albertel 1445:     if (!$cid || $env{'request.state'} eq 'construct') {
                   1446: 	return 'no_queue';
                   1447:     }
1.14      albertel 1448:     my $cnum=$env{'course.'.$cid.'.num'};
                   1449:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1450:     my %data;
1.138     albertel 1451:     $data{"$symb\0queue\0$uname:$udom"}=$user_data;
1.83      albertel 1452:     return &Apache::lonnet::cput($queue,\%data,$cdom,$cnum);
1.14      albertel 1453: }
                   1454: 
1.156     albertel 1455: sub get_limited_classlist {
                   1456:     my ($sections) = @_;
                   1457: 
                   1458:     my $classlist = &Apache::loncoursedata::get_classlist();
1.157     albertel 1459:     foreach my $student (keys(%$classlist)) {
                   1460: 	if ( $classlist->{$student}[&Apache::loncoursedata::CL_STATUS()]
                   1461: 	     ne 'Active') {
                   1462: 	    delete($classlist->{$student});
                   1463:        	}
                   1464:     }
1.156     albertel 1465: 
1.157     albertel 1466:     if (ref($sections) && !grep('all',@{ $sections })) {
1.156     albertel 1467: 	foreach my $student (keys(%$classlist)) {
                   1468: 	    my $section  = 
                   1469: 		$classlist->{$student}[&Apache::loncoursedata::CL_SECTION()];
                   1470: 	    if (! grep($section,@{ $sections })) {
                   1471: 		delete($classlist->{$student});
                   1472: 	    }
                   1473: 	}
                   1474:     }
                   1475:     return $classlist;
                   1476: }
                   1477: 
                   1478: 
1.14      albertel 1479: sub show_queue {
1.32      albertel 1480:     my ($queue,$with_selects)=@_;
1.14      albertel 1481:     my $result;
1.185     albertel 1482:     my ($symb,$cid,$udom,$uname)=&Apache::lonnet::whichuser();
1.14      albertel 1483:     my $cnum=$env{'course.'.$cid.'.num'};
                   1484:     my $cdom=$env{'course.'.$cid.'.domain'};
1.59      albertel 1485: 
                   1486:     my @chosen_sections=
                   1487: 	&Apache::loncommon::get_env_multiple('form.chosensections');
1.156     albertel 1488: 
                   1489:     my $classlist = &get_limited_classlist(\@chosen_sections);
                   1490: 
1.63      albertel 1491:     if (!(grep(/^all$/,@chosen_sections))) {
                   1492: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1493: 	    '</tt>.</p> '."\n";
                   1494:     }
1.59      albertel 1495: 
1.156     albertel 1496:     my ($view,$view_section);
                   1497:     my $scope = $env{'request.course.id'};
                   1498:     if (!($view=&Apache::lonnet::allowed('vgr',$scope))) {
                   1499: 	$scope .= '/'.$env{'request.course.sec'};
                   1500: 	if ( $view = &Apache::lonnet::allowed('vgr',$scope)) {
                   1501: 	    $view_section=$env{'request.course.sec'};
                   1502: 	} else {
                   1503: 	    undef($view);
                   1504: 	}
                   1505:     }
                   1506: 
1.16      albertel 1507:     my $regexp="^$symb\0";
1.30      albertel 1508:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.31      albertel 1509:     my ($tmp)=%queue;
                   1510:     if ($tmp=~/^error: 2 /) {
1.159     albertel 1511: 	return "\n<h3>Current Queue - $queue</h3>".
                   1512: 	    &Apache::loncommon::start_data_table().
                   1513: 	    &Apache::loncommon::start_data_table_row().
                   1514: 	    '<td>'.&mt('Empty').'</td>'.
                   1515: 	    &Apache::loncommon::end_data_table_row().
                   1516: 	    &Apache::loncommon::end_data_table();
1.31      albertel 1517:     }
1.103     albertel 1518:     my $title=&Apache::lonnet::gettitle($symb);
1.159     albertel 1519:     $result.="\n<h3>Current Queue - $title $queue </h3>".
                   1520: 	&Apache::loncommon::start_data_table().
                   1521: 	&Apache::loncommon::start_data_table_header_row();
1.103     albertel 1522:     if ($with_selects) { $result.="<th>Status</th><th></th>"; }
1.159     albertel 1523:     $result.="<th>user</th><th>data</th>".
                   1524: 	&Apache::loncommon::end_data_table_header_row();
1.14      albertel 1525:     foreach my $key (sort(keys(%queue))) {
1.59      albertel 1526: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1527: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
1.156     albertel 1528: 	
                   1529: 	my $section = $classlist->{$uname.':'.$udom}[&Apache::loncoursedata::CL_SECTION()];
                   1530: 
                   1531: 	my $can_view=1;
                   1532: 	if (!$view
                   1533: 	    || ($view_section && !$section)
                   1534: 	    || ($view_section && $section && ($view_section ne $section))) {
                   1535: 	    $can_view=0;
                   1536: 	}
                   1537: 
1.32      albertel 1538: 	if ($key=~/locked$/ && !$with_selects) {
1.159     albertel 1539: 	    $result.= &Apache::loncommon::start_data_table_row().
                   1540: 		"<td>$uname</td>";
1.103     albertel 1541: 	    $result.='<td>'.$queue{$key}.'</td></tr>';
1.32      albertel 1542: 	} elsif ($key=~/timestamp$/ && !$with_selects) {
1.159     albertel 1543: 	    $result.=&Apache::loncommon::start_data_table_row()."<td></td>";
1.103     albertel 1544: 	    $result.='<td>'.
1.16      albertel 1545: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
1.32      albertel 1546: 	} elsif ($key!~/(timestamp|locked)$/) {
1.159     albertel 1547: 	    $result.= &Apache::loncommon::start_data_table_row();
1.148     albertel 1548: 	    my ($end_time,$slot_text);
                   1549: 	    if (my $slot=&slotted_access($queue{$key})) {
                   1550: 		my %slot_data=&Apache::lonnet::get_slot($slot);
                   1551: 		$end_time = $slot_data{'endtime'};
                   1552: 		$slot_text = &mt('Slot: [_1]',$slot);
                   1553: 	    } else {
                   1554: 		$end_time = &Apache::lonhomework::due_date('0',$symb);
                   1555: 		$slot_text = '';
                   1556: 	    }
1.32      albertel 1557: 	    if ($with_selects) {
1.158     www      1558: 		my $ekey=&escape($key);
1.103     albertel 1559: 		my ($action,$description,$status)=('select',&mt('Select'));
1.32      albertel 1560: 		if (exists($queue{"$key\0locked"})) {
1.138     albertel 1561: 		    my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.103     albertel 1562: 		    $status=&mt('Locked by <tt>[_1]</tt>',$queue{"$key\0locked"});
1.32      albertel 1563: 		    if ($me eq $queue{"$key\0locked"}) {
                   1564: 			($action,$description)=('resume',&mt('Resume'));
                   1565: 		    } else {
                   1566: 			($action,$description)=('unlock',&mt('Unlock'));
                   1567: 		    }
                   1568: 		}
1.62      albertel 1569: 		my $seclist;
                   1570: 		foreach my $sec (@chosen_sections) {
                   1571: 		    $seclist.='<input type="hidden" name="chosensections" 
                   1572:                                value="'.$sec.'" />';
                   1573: 		}
1.156     albertel 1574: 		if ($can_view && ($end_time ne '' && time > $end_time)) {
1.35      albertel 1575: 		    $result.=(<<FORM);
1.103     albertel 1576: <td>$status</td>
1.32      albertel 1577: <td>
1.115     albertel 1578: <form style="display: inline" method="post">
1.32      albertel 1579:  <input type="hidden" name="gradingkey" value="$ekey" />
                   1580:  <input type="hidden" name="queue" value="$queue" />
                   1581:  <input type="hidden" name="gradingaction" value="$action" />
                   1582:  <input type="hidden" name="webgrade" value="no" />
1.33      albertel 1583:  <input type="hidden" name="queuemode" value="selected" />
1.32      albertel 1584:  <input type="submit" name="submit" value="$description" />
1.62      albertel 1585:  $seclist
1.32      albertel 1586: </form>
                   1587: </td>
                   1588: FORM
1.156     albertel 1589:                 } elsif (!$can_view && ($end_time ne '' && time > $end_time)) {
                   1590: 		    $result.='<td>'.&mt("Not gradable").'</td><td>&nbsp;</td>'
1.35      albertel 1591:                 } else {
1.148     albertel 1592: 		    $result.='<td>'.&mt("In Progress").'</td><td>&nbsp;</td>'
1.35      albertel 1593: 		}
1.32      albertel 1594: 	    }
1.156     albertel 1595: 	    $result.= "<td>".$classlist->{$uname.':'.$udom}[&Apache::loncoursedata::CL_FULLNAME()].
1.138     albertel 1596: 		" <tt>($uname:$udom)</tt> </td>";
1.148     albertel 1597: 	    $result.='<td>'.$slot_text.' End time: '.
                   1598: 		&Apache::lonlocal::locallocaltime($end_time).
1.159     albertel 1599: 		"</td>".&Apache::loncommon::end_data_table_row();
1.16      albertel 1600: 	}
1.14      albertel 1601:     }
1.159     albertel 1602:     $result.= &Apache::loncommon::end_data_table()."<hr />\n";
1.14      albertel 1603:     return $result;
                   1604: }
                   1605: 
1.34      albertel 1606: sub get_queue_counts {
                   1607:     my ($queue)=@_;
                   1608:     my $result;
1.185     albertel 1609:     my ($symb,$cid,$udom,$uname)=&Apache::lonnet::whichuser();
1.34      albertel 1610:     my $cnum=$env{'course.'.$cid.'.num'};
                   1611:     my $cdom=$env{'course.'.$cid.'.domain'};
1.156     albertel 1612: 
1.157     albertel 1613:     my $classlist=&get_limited_classlist();
1.156     albertel 1614: 
1.34      albertel 1615:     my $regexp="^$symb\0";
                   1616:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1617:     my ($tmp)=%queue;
                   1618:     if ($tmp=~/^error: 2 /) {
                   1619: 	return (0,0,0);
                   1620:     }
                   1621:     my ($entries,$ready_to_grade,$locks)=(0,0,0);
1.96      albertel 1622:     my %slot_cache;
1.34      albertel 1623:     foreach my $key (sort(keys(%queue))) {
1.156     albertel 1624: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
                   1625: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
1.34      albertel 1626: 	if ($key=~/locked$/) {
                   1627: 	    $locks++;
                   1628: 	} elsif ($key=~/timestamp$/) {
                   1629: 	    #ignore
                   1630: 	} elsif ($key!~/(timestamp|locked)$/) {
                   1631: 	    $entries++;
1.148     albertel 1632: 	    if (my $slot=&slotted_access($queue{$key})) {
                   1633: 		if (!exists($slot_cache{$slot})) {
                   1634: 		    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1635: 		    $slot_cache{$slot} = \%slot_data;
                   1636: 		}
                   1637: 		if (time > $slot_cache{$slot}{'endtime'}) {
                   1638: 		    $ready_to_grade++;
                   1639: 		}
                   1640: 	    } else {
                   1641: 		my $due_date = &Apache::lonhomework::due_date('0',$symb);
                   1642: 		if ($due_date ne '' && time > $due_date) {
                   1643: 		    $ready_to_grade++;
                   1644: 		}
1.34      albertel 1645: 	    }
                   1646: 	}
                   1647:     }
                   1648:     return ($entries,$ready_to_grade,$locks);
                   1649: }
                   1650: 
1.49      albertel 1651: sub encode_queue_key {
                   1652:     my ($symb,$udom,$uname)=@_;
1.138     albertel 1653:     return "$symb\0queue\0$uname:$udom";
1.49      albertel 1654: }
                   1655: 
1.14      albertel 1656: sub decode_queue_key {
                   1657:     my ($key)=@_;
                   1658:     my ($symb,undef,$user) = split("\0",$key);
1.138     albertel 1659:     my ($uname,$udom) = split(':',$user);
1.14      albertel 1660:     return ($symb,$uname,$udom);
                   1661: }
                   1662: 
                   1663: sub queue_key_locked {
1.30      albertel 1664:     my ($queue,$key,$cdom,$cnum)=@_;
1.33      albertel 1665:     if (!defined($cdom) || !defined($cnum)) {
1.185     albertel 1666: 	my (undef,$cid)=&Apache::lonnet::whichuser();
1.33      albertel 1667: 	$cnum=$env{'course.'.$cid.'.num'};
                   1668: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1669:     }
1.14      albertel 1670:     my ($key_locked,$value)=
1.30      albertel 1671: 	&Apache::lonnet::get($queue,["$key\0locked"],$cdom,$cnum);
1.14      albertel 1672:     if ($key_locked eq "$key\0locked") {
                   1673: 	return $value;
                   1674:     }
                   1675:     return undef;
                   1676: }
                   1677: 
1.148     albertel 1678: sub slotted_access {
                   1679:     my ($queue_entry) = @_;
                   1680:     if (ref($queue_entry) eq 'ARRAY') {
                   1681: 	if (defined($queue_entry->[0])) {
                   1682: 	    return $queue_entry->[0];
                   1683: 	}
                   1684: 	return undef;
                   1685:     } elsif (ref($queue_entry) eq 'HASH') {
                   1686: 	if (defined($queue_entry->{'slot'})) {
                   1687: 	    return $queue_entry->{'slot'};
                   1688: 	}
                   1689: 	return undef;
                   1690:     }
                   1691:     return undef;
                   1692: }
                   1693: 
1.14      albertel 1694: sub pick_from_queue_data {
1.156     albertel 1695:     my ($queue,$check_section,$queuedata,$cdom,$cnum,$classlist)=@_;
1.98      albertel 1696:     my @possible; # will hold queue entries that are valid to be selected
1.30      albertel 1697:     foreach my $key (keys(%$queuedata)) {
1.68      albertel 1698: 	if ($key =~ /\0locked$/) { next; }
                   1699: 	if ($key =~ /\0timestamp$/) { next; }
1.156     albertel 1700: 
1.14      albertel 1701: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
1.156     albertel 1702: 	if (!defined($classlist->{$uname.':'.$udom})) { next; }
                   1703: 
1.14      albertel 1704: 	if ($check_section) {
1.156     albertel 1705: 	    my $section =
                   1706: 		$classlist->{$uname.':'.$udom}[&Apache::loncoursedata::CL_SECTION()];
1.17      albertel 1707: 	    if ($section eq $check_section) {
1.33      albertel 1708: 		&Apache::lonxml::debug("my sec");
1.15      albertel 1709: 		next;
                   1710: 	    }
1.14      albertel 1711: 	}
1.148     albertel 1712: 	my $end_time;
                   1713: 	if (my $slot=&slotted_access($queuedata->{$key})) {
1.154     albertel 1714: 	    &Apache::lonxml::debug("looking at slot $slot");
1.148     albertel 1715: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
                   1716: 	    if ($slot_data{'endtime'} < time) { 
                   1717: 		$end_time = $slot_data{'endtime'};
1.154     albertel 1718: 	    } else {
                   1719: 		&Apache::lonxml::debug("not time ".$slot_data{'endtime'});
                   1720: 		next;
1.148     albertel 1721: 	    }
                   1722: 	} else {
                   1723: 	    my $due_date = &Apache::lonhomework::due_date('0',$symb);
1.154     albertel 1724: 	    if ($due_date < time) {
1.148     albertel 1725: 		$end_time = $due_date;
1.154     albertel 1726: 	    } else {
                   1727: 		&Apache::lonxml::debug("not time $due_date");
                   1728: 		next;
1.148     albertel 1729: 	    }
                   1730: 	}
                   1731: 	
1.98      albertel 1732: 	if (exists($queuedata->{"$key\0locked"})) {
1.33      albertel 1733: 	    &Apache::lonxml::debug("someone already has um.");
1.15      albertel 1734: 	    next;
                   1735: 	}
1.148     albertel 1736: 	push(@possible,[$key,$end_time]);
1.98      albertel 1737:     }
                   1738:     if (@possible) {
                   1739:         # sort entries in order by slot end time
                   1740: 	@possible = sort { $a->[1] <=> $b->[1] } @possible;
1.137     albertel 1741: 	# pick one of the entries in the top 10% in small queues and one
                   1742: 	# of the first ten entries in large queues
1.139     albertel 1743: 	#my $ten_percent = int($#possible * 0.1);
                   1744: 	#if ($ten_percent < 1 ) { $ten_percent = 1;  }
                   1745: 	#if ($ten_percent > 10) { $ten_percent = 10; }
                   1746: 	#my $max=($#possible < $ten_percent) ? $#possible : $ten_percent;
1.137     albertel 1747: 	
1.139     albertel 1748: 	#return $possible[int(rand($max))][0];
                   1749: 	return $possible[0][0];
1.14      albertel 1750:     }
                   1751:     return undef;
                   1752: }
                   1753: 
1.15      albertel 1754: sub find_mid_grade {
1.30      albertel 1755:     my ($queue,$symb,$cdom,$cnum)=@_;
1.158     www      1756:     my $todo=&unescape($env{'form.gradingkey'});
1.138     albertel 1757:     my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.15      albertel 1758:     if ($todo) {
1.30      albertel 1759: 	my $who=&queue_key_locked($queue,$todo,$cdom,$cnum);
1.15      albertel 1760: 	if ($who eq $me) { return $todo; }
                   1761:     }
                   1762:     my $regexp="^$symb\0.*\0locked\$";
1.30      albertel 1763:     my %locks=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.15      albertel 1764:     foreach my $key (keys(%locks)) {
                   1765: 	my $who=$locks{$key};
                   1766: 	if ($who eq $me) {
                   1767: 	    $todo=$key;
                   1768: 	    $todo=~s/\0locked$//;
                   1769: 	    return $todo;
                   1770: 	}
                   1771:     }
                   1772:     return undef;
                   1773: }
                   1774: 
1.32      albertel 1775: sub lock_key {
                   1776:     my ($queue,$todo)=@_;
1.138     albertel 1777:     my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.185     albertel 1778:     my (undef,$cid)=&Apache::lonnet::whichuser();
1.32      albertel 1779:     my $cnum=$env{'course.'.$cid.'.num'};
                   1780:     my $cdom=$env{'course.'.$cid.'.domain'};
                   1781:     my $success=&Apache::lonnet::newput($queue,{"$todo\0locked"=> $me},
                   1782: 					$cdom,$cnum);
1.33      albertel 1783:     &Apache::lonxml::debug("success $success $todo");
1.32      albertel 1784:     if ($success eq 'ok') {
                   1785: 	return 1;
                   1786:     }
                   1787:     return 0;
                   1788: }
                   1789: 
1.86      albertel 1790: sub get_queue_symb_status {
1.85      albertel 1791:     my ($queue,$symb,$cdom,$cnum) = @_;
                   1792:     if (!defined($cdom) || !defined($cnum)) {
1.185     albertel 1793: 	my (undef,$cid)=&Apache::lonnet::whichuser();
1.85      albertel 1794: 	$cnum=$env{'course.'.$cid.'.num'};
                   1795: 	$cdom=$env{'course.'.$cid.'.domain'};
                   1796:     }
1.157     albertel 1797:     my $classlist=&get_limited_classlist();
1.156     albertel 1798: 
1.85      albertel 1799:     my $regexp="^$symb\0";
                   1800:     my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
                   1801:     my ($tmp)=%queue;
                   1802:     if ($tmp=~/^error: 2 /) { return; }
                   1803:     my @users;
                   1804:     foreach my $key (sort(keys(%queue))) {
                   1805: 	next if ($key=~/locked$/);
                   1806: 	next if ($key=~/timestamp$/);
                   1807: 	my ($symb,$uname,$udom) = &decode_queue_key($key);
1.156     albertel 1808: 	next if (!defined($classlist->{$uname.':'.$udom}));
1.85      albertel 1809: 	push(@users,"$uname:$udom");
                   1810:     }
                   1811:     return @users;
                   1812: }
                   1813: 
1.14      albertel 1814: sub get_from_queue {
1.30      albertel 1815:     my ($queue)=@_;
1.14      albertel 1816:     my $result;
1.185     albertel 1817:     my ($symb,$cid,$udom,$uname)=&Apache::lonnet::whichuser();
1.14      albertel 1818:     my $cnum=$env{'course.'.$cid.'.num'};
                   1819:     my $cdom=$env{'course.'.$cid.'.domain'};
1.32      albertel 1820:     my $todo=&find_mid_grade($queue,$symb,$cdom,$cnum);
1.33      albertel 1821:     &Apache::lonxml::debug("found ".join(':',&decode_queue_key($todo)));
1.16      albertel 1822:     if ($todo) { return $todo; }
1.95      albertel 1823:     my $attempts=0;
1.156     albertel 1824: 
1.157     albertel 1825:     my $classlist=&get_limited_classlist();
1.156     albertel 1826: 
1.14      albertel 1827:     while (1) {
1.95      albertel 1828: 	if ($attempts > 2) {
                   1829: 	    # tried twice to get a queue entry, giving up
                   1830: 	    return (undef,'unable');
                   1831: 	}
1.14      albertel 1832: 	my $starttime=time;
1.83      albertel 1833: 	&Apache::lonnet::cput($queue,{"$symb\0timestamp"=>$starttime},
                   1834: 			      $cdom,$cnum);
1.33      albertel 1835: 	&Apache::lonxml::debug("$starttime");
1.14      albertel 1836: 	my $regexp="^$symb\0queue\0";
1.156     albertel 1837: 	#my $range= ($attempts < 1 ) ? '0-100' : '0-400';
1.97      albertel 1838: 
1.98      albertel 1839: 	my %queue=&Apache::lonnet::dump($queue,$cdom,$cnum,$regexp);
1.33      albertel 1840: 	#make a pass looking for a user _not_ in my section
1.14      albertel 1841: 	if ($env{'request.course.sec'}) {
1.33      albertel 1842: 	    &Apache::lonxml::debug("sce");
1.30      albertel 1843: 	    $todo=&pick_from_queue_data($queue,$env{'request.course.sec'},
1.156     albertel 1844: 					\%queue,$cdom,$cnum,$classlist);
1.33      albertel 1845: 	    &Apache::lonxml::debug("sce $todo");
1.14      albertel 1846: 	}
1.33      albertel 1847: 	# no one _not_ in our section so look for any user that is
                   1848: 	# ready for grading
1.14      albertel 1849: 	if (!$todo) {
1.33      albertel 1850: 	    &Apache::lonxml::debug("no sce");
1.156     albertel 1851: 	    $todo=&pick_from_queue_data($queue,undef,\%queue,$cdom,$cnum,
                   1852: 					$classlist);
1.33      albertel 1853: 	    &Apache::lonxml::debug("no sce $todo");
1.14      albertel 1854: 	}
                   1855: 	# no user to grade 
                   1856: 	if (!$todo) { last; }
1.33      albertel 1857: 	&Apache::lonxml::debug("got $todo");
1.14      albertel 1858: 	# otherwise found someone so lets try to lock them
1.32      albertel 1859: 	# unless someone else already picked them
1.95      albertel 1860: 	if (!&lock_key($queue,$todo)) {
                   1861: 	    $attempts++;
                   1862: 	    next;
                   1863: 	}
1.14      albertel 1864: 	my (undef,$endtime)=
1.30      albertel 1865: 	    &Apache::lonnet::get($queue,["$symb\0timestamp"],
1.14      albertel 1866: 				 $cdom,$cnum);
1.33      albertel 1867: 	&Apache::lonxml::debug("emd  $endtime");
1.14      albertel 1868: 	# someone else already modified the queue, 
                   1869: 	# perhaps our picked user wass already fully graded between
                   1870: 	# when we picked him and when we locked his record? so lets
                   1871: 	# double check.
                   1872: 	if ($endtime != $starttime) {
                   1873: 	    my ($key,$value)=
1.30      albertel 1874: 		&Apache::lonnet::get($queue,["$todo"],
1.14      albertel 1875: 				     $cdom,$cnum);
1.33      albertel 1876: 	    &Apache::lonxml::debug("check  $key .. $value");
1.14      albertel 1877: 	    if ($key eq $todo && ref($value)) {
                   1878: 	    } else {
1.30      albertel 1879: 		&Apache::lonnet::del($queue,["$todo\0locked"],
1.14      albertel 1880: 				     $cdom,$cnum);
1.33      albertel 1881: 		&Apache::lonxml::debug("del");
1.95      albertel 1882: 		$attempts++;
1.14      albertel 1883: 		next;
                   1884: 	    }
                   1885: 	}
1.33      albertel 1886: 	&Apache::lonxml::debug("last $todo");
1.14      albertel 1887: 	last;
                   1888:     }
                   1889:     return $todo;
                   1890: }
                   1891: 
1.49      albertel 1892: sub select_user {
1.185     albertel 1893:     my ($symb,$cid)=&Apache::lonnet::whichuser();
1.49      albertel 1894: 
1.59      albertel 1895:     my @chosen_sections=
                   1896: 	&Apache::loncommon::get_env_multiple('form.chosensections');
1.156     albertel 1897: 
                   1898:     my $classlist = &get_limited_classlist(\@chosen_sections);
1.63      albertel 1899:     
                   1900:     my $result;
                   1901:     if (!(grep(/^all$/,@chosen_sections))) {
                   1902: 	$result.='<p> Showing only sections <tt>'.join(', ',@chosen_sections).
                   1903: 	    '</tt>.</p> '."\n";
                   1904:     }
1.159     albertel 1905:     $result.=&Apache::loncommon::start_data_table();
1.49      albertel 1906: 
1.156     albertel 1907:     foreach my $student (sort {lc($classlist->{$a}[&Apache::loncoursedata::CL_FULLNAME()]) cmp lc($classlist->{$b}[&Apache::loncoursedata::CL_FULLNAME()]) } (keys(%$classlist))) {
1.49      albertel 1908: 	my ($uname,$udom) = split(/:/,$student);
1.59      albertel 1909: 	
1.84      albertel 1910: 	my $cnum=$env{'course.'.$cid.'.num'};
                   1911: 	my $cdom=$env{'course.'.$cid.'.domain'};
1.88      albertel 1912: 	my %status = &get_student_status($symb,$cdom,$cnum,$udom,$uname,
                   1913: 					 'Task');
1.49      albertel 1914: 	my $queue = 'none';
1.58      albertel 1915: 	my $cannot_grade;
                   1916: 	if ($status{'reviewqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1917: 	    $queue = 'reviewqueue';
1.58      albertel 1918: 	    if ($status{'reviewqueue'} eq 'in_progress') {
                   1919: 		$cannot_grade=1;
                   1920: 	    }
                   1921: 	} elsif ($status{'gradingqueue'} =~ /^(in_progress|enqueue)$/) {
1.49      albertel 1922: 	    $queue = 'gradingqueue';
1.58      albertel 1923: 	    if ($status{'gradingqueue'} eq 'in_progress') {
                   1924: 		$cannot_grade=1;
                   1925: 	    }
1.49      albertel 1926: 	}
                   1927: 	my $todo = 
1.158     www      1928: 	    &escape(&encode_queue_key($symb,$udom,$uname));
1.58      albertel 1929: 	if ($cannot_grade) {
1.159     albertel 1930: 	    $result.=&Apache::loncommon::start_data_table_row().
                   1931: 		'<td>&nbsp;</td><td>'.$classlist->{$student}[&Apache::loncoursedata::CL_FULLNAME()].
1.58      albertel 1932: 		'</td><td>';
                   1933: 	} else {
1.62      albertel 1934: 	    my $seclist;
                   1935: 	    foreach my $sec (@chosen_sections) {
                   1936: 		$seclist.='<input type="hidden" name="chosensections" 
                   1937:                                value="'.$sec.'" />';
                   1938: 	    }
1.159     albertel 1939: 	    $result.=&Apache::loncommon::start_data_table_row();
1.58      albertel 1940: 	    $result.=<<RESULT;
1.49      albertel 1941:   <td>
1.115     albertel 1942:     <form style="display: inline" method="post">
1.49      albertel 1943:       <input type="hidden" name="gradingkey" value="$todo" />
                   1944:       <input type="hidden" name="queue" value="$queue" />
                   1945:       <input type="hidden" name="webgrade" value="no" />
1.52      albertel 1946:       <input type="hidden" name="regrade" value="yes" />
1.62      albertel 1947:       <input type="submit" name="submit" value="Regrade" />
                   1948:       $seclist
1.49      albertel 1949:     </form>
1.155     albertel 1950:   <td>$classlist->{$student}[&Apache::loncoursedata::CL_FULLNAME()] <tt>($student)</tt></td>
1.49      albertel 1951:   <td>
                   1952: RESULT
1.58      albertel 1953:         }
1.49      albertel 1954:         if ($status{'status'} eq 'pass') {
                   1955: 	    $result .= '<font color="green">'.&mt('Passed').'</font>';
                   1956: 	} elsif ($status{'status'} eq 'fail') {
                   1957: 	    $result .= '<font color="red">'.&mt('Failed').'</font>';
                   1958: 	} elsif ($status{'status'} eq 'review') {
                   1959: 	    $result .= '<font color="blue">'.&mt('Under Review').'</font>';
                   1960: 	} elsif ($status{'status'} eq 'ungraded') {
                   1961: 	    $result .= &mt('Ungraded');
                   1962: 	} elsif ($status{'status'} ne '') {
                   1963: 	    $result .= '<font color="orange">'.&mt('Unknown Status').'</font>';
                   1964: 	} else {
                   1965: 	    $result.="&nbsp;";
                   1966: 	}
                   1967: 	if ($status{'version'}) {
                   1968: 	    $result .= ' '.&mt('Version').' '.$status{'version'};
                   1969: 	}
1.101     albertel 1970: 	if ($status{'grader'}) {
                   1971: 	    $result .= ' '.&mt('(Graded by [_1])',$status{'grader'}).' ';
                   1972: 	}
1.49      albertel 1973: 	$result.= '</td><td>';
                   1974: 	if ($status{'reviewqueue'} eq 'enqueued') {
                   1975: 	    $result .= &mt('Awaiting Review');
                   1976: 	} elsif ($status{'reviewqueue'} eq 'locked') {
                   1977: 	    $result .= &mt('Under Review');
1.58      albertel 1978: 	} elsif ($status{'reviewqueue'} eq 'in_progress') {
                   1979: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1980: 	} elsif ($status{'gradingqueue'} eq 'enqueued') {
                   1981: 	    $result .= &mt('Awaiting Grading');
                   1982: 	} elsif ($status{'gradingqueue'} eq 'locked') {
                   1983: 	    $result .= &mt('Being Graded');
1.58      albertel 1984: 	} elsif ($status{'gradingqueue'} eq 'in_progress') {
                   1985: 	    $result .= &mt('Still being worked on.');
1.49      albertel 1986: 	} else {
                   1987: 	    $result.="&nbsp;";
                   1988: 	}
1.159     albertel 1989: 	$result.= '</td>'.&Apache::loncommon::end_data_table_row();
1.49      albertel 1990:     }
1.159     albertel 1991:     $result.=&Apache::loncommon::end_data_table();
1.49      albertel 1992:     return $result;
                   1993: }
                   1994: 
                   1995: sub get_student_status {
1.86      albertel 1996:     my ($symb,$cdom,$cnum,$udom,$uname,$type)=@_;
                   1997: 
                   1998:     my %status;
                   1999: 
                   2000:     if ($type eq 'Task') {
                   2001: 	my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},
1.49      albertel 2002: 					  $udom,$uname);
1.89      albertel 2003: 	$status{'status'}=$record{'resource.0.status'};
                   2004: 	$status{'version'}=$record{'resource.0.version'};
                   2005: 	$status{'grader'}=$record{'resource.0.regrader'};
1.86      albertel 2006:     }
                   2007:     $status{'reviewqueue'}=
                   2008: 	&check_queue_for_key($cdom,$cnum,'reviewqueue',
                   2009: 			     &encode_queue_key($symb,$udom,$uname));
                   2010:     $status{'gradingqueue'}=
                   2011: 	&check_queue_for_key($cdom,$cnum,'gradingqueue',
                   2012: 			     &encode_queue_key($symb,$udom,$uname));
1.49      albertel 2013:     return %status;
                   2014: }
                   2015: 
1.1       albertel 2016: sub start_ClosingParagraph {
                   2017:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2018:     my $result;
                   2019:     if ($target eq 'web') {
1.13      albertel 2020:     } elsif ($target eq 'webgrade') {
                   2021: 	&Apache::lonxml::startredirection();
1.1       albertel 2022:     }
                   2023:     return $result;
                   2024: }
                   2025: 
                   2026: sub end_ClosingParagraph {
                   2027:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2028:     my $result;
                   2029:     if ($target eq 'web') {
1.13      albertel 2030:     } elsif ($target eq 'webgrade') {
                   2031: 	&Apache::lonxml::endredirection();
1.1       albertel 2032:     }
                   2033:     return $result;
                   2034: }
                   2035: 
1.168     albertel 2036: sub get_dim_id {
1.194     albertel 2037:     if (@Apache::bridgetask::dimension) {
                   2038: 	return $Apache::bridgetask::dimension[-1];
                   2039:     } else {
                   2040: 	return $top;
                   2041:     }
1.168     albertel 2042: }
                   2043: 
1.19      albertel 2044: sub get_id {
                   2045:     my ($parstack,$safeeval)=@_;
                   2046:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                   2047:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                   2048:     return $id;
                   2049: }
                   2050: 
1.162     albertel 2051: sub start_Setup {
                   2052:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.168     albertel 2053:     #undef(%dimension);
                   2054:     my $dim = &get_id($parstack,$safeeval);
                   2055:     push(@Apache::bridgetask::dimension,$dim);
1.162     albertel 2056:     &Apache::lonxml::startredirection();
1.168     albertel 2057:     return &internal_location($dim);
1.162     albertel 2058: }
1.173     albertel 2059: 
                   2060: {
                   2061:     my @allowed;
                   2062:     sub enable_dimension_parsing {
                   2063: 	my ($id) = @_;
                   2064: 	push(@allowed,$id);
                   2065:     }
                   2066:     sub disable_dimension_parsing {
                   2067: 	pop(@allowed);
                   2068:     }
                   2069:     sub skip_dimension_parsing {
                   2070: 	my ($check) = @_;
                   2071: 	if (!@allowed) { return 0;}
                   2072: 	# if unspecified allow any id
                   2073: 	if ($allowed[-1] eq undef) { return 0;}
                   2074: 
                   2075: 	return ($check ne $allowed[-1]);
                   2076:     }
                   2077: }
                   2078: 
1.151     albertel 2079: sub start_Question { return &start_Dimension(@_); }
1.1       albertel 2080: sub start_Dimension {
1.173     albertel 2081:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.168     albertel 2082:     my $dim = &get_id($parstack,$safeeval);
                   2083:     my $previous_dim;
                   2084:     if (@Apache::bridgetask::dimension) {
                   2085: 	$previous_dim = $Apache::bridgetask::dimension[-1];
                   2086: 	push(@{$Apache::bridgetask::dimension{$previous_dim}{'contains'}},
                   2087: 	     $dim);
1.173     albertel 2088: 	if(&skip_dimension_parsing($dim)) {
                   2089: 	    $dimension{$previous_dim}{'criteria.'.$dim} =
                   2090: 		$token->[4]
                   2091: 		.&Apache::lonxml::get_all_text('/'.$tagstack->[-1],$parser,
                   2092: 					      $style)
                   2093: 		.'</'.$tagstack->[-1].'>';
                   2094: 	}
1.169     albertel 2095: 	$dimension{$previous_dim}{'criteria.'.$dim.'.type'}='dimension';
                   2096: 	$dimension{$previous_dim}{'criteria.'.$dim.'.mandatory'}=
                   2097: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
                   2098: 	push(@{$dimension{$previous_dim}{'criterias'}},$dim);
1.171     albertel 2099: 	$dimension{$dim}{'nested'}=$previous_dim;
1.172     albertel 2100: 	&Apache::lonxml::debug("adding $dim as criteria to $previous_dim");
1.168     albertel 2101:     } else {
1.194     albertel 2102: 	$dimension{$top}{'criteria.'.$dim.'.type'}='dimension';
                   2103: 	$dimension{$top}{'criteria.'.$dim.'.mandatory'}=
1.168     albertel 2104: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.194     albertel 2105: 	push(@{$dimension{$top}{'criterias'}},$dim);
1.168     albertel 2106:     }
                   2107:     push(@Apache::bridgetask::dimension,$dim);
1.54      albertel 2108:     &Apache::lonxml::startredirection();
1.173     albertel 2109:     &enable_dimension_parsing($dim);
1.168     albertel 2110:     return &internal_location($dim);
1.1       albertel 2111: }
                   2112: 
1.160     albertel 2113: sub start_QuestionText {
                   2114:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.168     albertel 2115:     my $dim = &get_dim_id();
1.160     albertel 2116:     my $text=&Apache::lonxml::get_all_text('/questiontext',$parser,$style);
                   2117:     if ($target eq 'grade' || $target eq 'web' || $target eq 'webgrade') {
1.169     albertel 2118: 	$dimension{$dim}{'questiontext'}=$text;
1.160     albertel 2119:     }
                   2120:     return '';
                   2121: }
                   2122: 
                   2123: sub end_QuestionText {
                   2124:     return '';
                   2125: }
                   2126: 
1.13      albertel 2127: sub get_instance {
1.75      albertel 2128:     my ($dim)=@_;
                   2129:     my $rand_alg=&Apache::lonnet::get_rand_alg();
                   2130:     if (!$rand_alg || $rand_alg eq '32bit' || $rand_alg eq '64bit' ||
                   2131: 	$rand_alg eq '64bit2' || $rand_alg eq '64bit3' ||
                   2132: 	$rand_alg eq '64bit4' ) {
                   2133: 	&Apache::response::pushrandomnumber();
1.169     albertel 2134: 	my @order=&Math::Random::random_permutation(@{$dimension{$dim}{'instances'}});
1.75      albertel 2135: 	my $num=@order;
                   2136: 	my $version=&get_version();
                   2137: 	my $which=($version-1)%$num;
                   2138: 	return $order[$which];
                   2139:     } else {
                   2140: 	my ($version,$previous) = &get_version();
                   2141: 	my $instance = 
                   2142: 	    $Apache::lonhomework::history{"resource.$version.0.$dim.instance"};
                   2143: 	if (defined($instance)) { return $instance; }
                   2144: 
                   2145: 	&Apache::response::pushrandomnumber();
1.173     albertel 2146: 	if (ref($dimension{$dim}{'instances'}) eq 'ARRAY') {
                   2147: 	    my @instances = @{$dimension{$dim}{'instances'}};
                   2148: 	    # remove disabled instances
                   2149: 	    for (my $i=0; $i < $#instances; $i++) {
                   2150: 		if ($dimension{$dim}{$instances[$i].'.disabled'}) {
                   2151: 		    splice(@instances,$i,1);
                   2152: 		    $i--;
                   2153: 		}
                   2154: 	    }
                   2155: 	    @instances = &Math::Random::random_permutation(@instances);
                   2156: 	    $instance  = $instances[($version-1)%scalar(@instances)];
                   2157: 	    if ($version =~ /^\d$/) {
                   2158: 		$Apache::lonhomework::results{"resource.$version.0.$dim.instance"} = 
                   2159: 		    $instance;
                   2160: 		$Apache::lonhomework::results{'INTERNAL_store'} = 1; 
1.75      albertel 2161: 	    }
                   2162: 	}
                   2163: 	&Apache::response::poprandomnumber();
                   2164: 	return $instance;
                   2165:     }
1.13      albertel 2166: }
                   2167: 
1.169     albertel 2168: sub get_criteria {
                   2169:     my ($what,$version,$dim,$id) = @_;
                   2170:     my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
1.194     albertel 2171:     my $prefix = ($type eq 'criteria' && $dim ne $top) ? "$dim.$id"
                   2172: 	                                               : "$id";
1.169     albertel 2173:     my $entry = "resource.$version.0.$prefix.$what";
                   2174:     if (exists($Apache::lonhomework::results{$entry})) {
                   2175: 	return $Apache::lonhomework::results{$entry};
                   2176:     }
                   2177:     return $Apache::lonhomework::history{$entry};
                   2178: }
                   2179: 
1.194     albertel 2180: sub link {
                   2181:     my ($id) = @_;
                   2182:     $id =~ s/\./_/g;
                   2183:     return 'LC_GRADING_criteria_'.$id;
                   2184: }
                   2185: sub end_Question { return &end_Dimension(@_); }
                   2186: sub end_Dimension {
                   2187:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2188:     my $result=&Apache::lonxml::endredirection();
                   2189:     my $dim=&get_id($parstack,$safeeval);
                   2190:     if (&skip_dimension_parsing($dim)) {
                   2191: 	&disable_dimension_parsing($dim);
                   2192: 	pop(@Apache::bridgetask::dimension);
                   2193: 	return;
1.122     albertel 2194:     }
1.194     albertel 2195:     my $instance=&get_instance($dim);
                   2196:     my $version=&get_version();
                   2197:     if ($target eq 'web') {
                   2198: 	$result .= &nested_parse(\$dimension{$dim}{'intro'},[@_]);
                   2199: 	my @instances = $instance;
                   2200: 	if (&Apache::response::showallfoils()) {
                   2201: 	    @instances = @{$dimension{$dim}{'instances'}};
1.173     albertel 2202: 	}
1.194     albertel 2203: 	my $shown_question_text;
                   2204: 	foreach my $instance (@instances) {
                   2205: 	    $result .= &nested_parse(\$dimension{$dim}{$instance.'.text'},
                   2206: 				     [@_]);
                   2207: 	    $result .= &nested_parse(\$dimension{$dim}{'questiontext'},
                   2208: 				     [@_],{'set_dim_id' => undef});
                   2209: 	    my $task_status = 
                   2210: 		$Apache::lonhomework::history{"resource.$version.0.status"};
                   2211: 	    if ($task_status ne 'pass' && $task_status ne 'fail') {
                   2212: 		
                   2213: 		foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
                   2214: 				@{$dimension{$dim}{'criterias'}}) {
                   2215: 		    my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
                   2216: 		    &Apache::lonxml::debug("$id is $type");
                   2217: 		    if ($type eq 'dimension') {
                   2218: 			$result.=
                   2219: 			    &nested_parse(\$dimension{$dim}{'criteria.'.$id},
                   2220: 					  [@_],{'set_dim_id' => $id});
1.173     albertel 2221: 		    }
1.194     albertel 2222: 		}
                   2223: 	    } else {
                   2224: 		my $dim_status=$Apache::lonhomework::history{"resource.$version.0.$dim.status"};
                   2225: 		my $mandatory='Mandatory';
                   2226: 		if (&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval) eq 'N') {
                   2227: 		    $mandatory='Optional';
                   2228: 		}
                   2229: 		my $dim_info="<div class='LC_$dim_status LC_question_grade'>\n";
                   2230: 		if ($dim_status eq 'pass') {
                   2231: 		    $dim_info.='<h3>Question : you passed this '.$mandatory.' question</h3>';
                   2232: 		}
                   2233: 		if ($dim_status eq 'fail') {
                   2234: 		    $dim_info.='<h3>Question : you did not pass this '.$mandatory.' question</h3>';
                   2235: 		}
                   2236: 		my $man_count=0;
                   2237: 		my $man_passed=0;
                   2238: 		my $opt_count=0;
                   2239: 		my $opt_passed=0;
                   2240: 		foreach my $id ( @{$dimension{$dim}{$instance.'.criterias'}},
                   2241: 				 @{$dimension{$dim}{'criterias'}} ) {
                   2242: 		    my $status = &get_criteria('status',$version,$dim,$id);
                   2243: 		    if ($dimension{$dim}{'criteria.'.$id.'.mandatory'} 
                   2244: 			eq 'N') {
                   2245: 			$opt_count++;
                   2246: 			if ($status eq 'pass') { $opt_passed++; }
                   2247: 		    } else {
                   2248: 			$man_count++;
                   2249: 			if ($status eq 'pass') { $man_passed++; }
1.22      albertel 2250: 		    }
                   2251: 		}
1.194     albertel 2252: 		if ($man_passed eq $man_count) { $man_passed='all'; }
                   2253: 		
                   2254: 		my $opt_req=$dimension{$dim}{$instance.'.optionalrequired'};
                   2255: 		if ($opt_req !~ /\S/) {
                   2256: 		    $opt_req=
                   2257: 			&Apache::lonxml::get_param('OptionalRequired',
                   2258: 						   $parstack,$safeeval);
                   2259: 		    if ($opt_req !~ /\S/) { $opt_req = 0; }
1.171     albertel 2260: 		}
1.194     albertel 2261: 		$dim_info.="\n<p>".&mt('You passed [_1] of the [_2] mandatory components and [_3] of the [_4] optional components, of which you were required to pass [_5].',$man_passed,$man_count,$opt_passed,$opt_count,$opt_req)."</p>\n</div>";
                   2262: 		
                   2263: 		foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
                   2264: 				@{$dimension{$dim}{'criterias'}}) {
                   2265: 		    my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
                   2266: 		    if ($type eq 'dimension') {
                   2267: 			$result.=$dimension{$id}{'result'};
                   2268: 			next;
                   2269: 		    }
                   2270: 		    my $criteria = 
1.173     albertel 2271: 			&nested_parse(\$dimension{$dim}{'criteria.'.$id},
1.194     albertel 2272: 				      [@_]);
                   2273: 		    $dim_info .= &layout_web_Criteria($dim,$id,$criteria);
1.169     albertel 2274: 		}
                   2275: 
1.194     albertel 2276: 		my $internal_location=&internal_location($dim);
                   2277: 		$result=~s/\Q$internal_location\E/$dim_info/;
1.19      albertel 2278: 	    }
1.194     albertel 2279: 	}
                   2280:     } elsif ($target eq 'webgrade') {
                   2281: 	# in case of any side effects that we need
                   2282: 	&nested_parse(\$dimension{$dim}{'intro'},[@_]);
                   2283: 	&nested_parse(\$dimension{$dim}{$instance.'.text'},[@_]);
                   2284: 	$result.=
                   2285: 	    &nested_parse(\$dimension{$dim}{'questiontext'},[@_],
                   2286: 			  {'set_dim_id'          => undef,
1.195   ! albertel 2287: 			   'delayed_dim_results' => 1});
1.194     albertel 2288: 	foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
                   2289: 			@{$dimension{$dim}{'criterias'}} ) {
                   2290: 	    my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
                   2291: 	    if ($type eq 'dimension') {
                   2292: 		# dimensional 'criteria' don't get assigned grades
                   2293: 		$result.=
                   2294: 		    &nested_parse(\$dimension{$dim}{'criteria.'.$id},
                   2295: 				  [@_],{'set_dim_id' => $id});
                   2296: 		next;
                   2297: 	    } else {
                   2298: 		my $criteria =&nested_parse(\$dimension{$dim}{'criteria.'.$id},
                   2299: 					     [@_]);
                   2300: 		$criteria = &layout_webgrade_Criteria($dim,$id,$criteria);
                   2301: 		my $internal_location=&internal_location($id);
                   2302: 		$result=~s/\Q$internal_location\E/$criteria/;
1.151     albertel 2303: 	    }
1.194     albertel 2304: 	}
                   2305: 	if (&nest()) {
                   2306: 	    &Apache::lonxml::debug(" for $dim stashing results into ".$dimension{$dim}{'nested'});
                   2307: 	    $dimension{$dimension{$dim}{'nested'}}{'result'}.=$result;
                   2308: 	    undef($result);
                   2309: 	}
                   2310:     } elsif ($target eq 'grade' && $env{'form.webgrade'}) {
                   2311: 	my $optional_passed=0;
                   2312: 	my $mandatory_failed=0;
                   2313: 	my $ungraded=0;
                   2314: 	my $review=0;
                   2315: 	
                   2316: 	$result .= &nested_parse(\$dimension{$dim}{'intro'},[@_]);
                   2317: 	$result .= &nested_parse(\$dimension{$dim}{$instance.'.text'},
                   2318: 				 [@_]);
                   2319: 	$result .= &nested_parse(\$dimension{$dim}{'questiontext'},
                   2320: 				 [@_],{'set_dim_id' => undef});
                   2321: 	
                   2322: 	foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
                   2323: 			@{$dimension{$dim}{'criterias'}}) {
                   2324: 	    my $link=&link($id);
                   2325: 	    
                   2326: 	    my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
                   2327: 	    if ($type eq 'criteria') {
                   2328: 		# dimensional 'criteria' don't get assigned grades
                   2329: 		$Apache::lonhomework::results{"resource.$version.0.$dim.$id.status"}=$env{'form.HWVAL_'.$link};
                   2330: 		$Apache::lonhomework::results{"resource.$version.0.$dim.$id.comment"}=$env{'form.HWVAL_comment_'.$link};
                   2331: 	    } else {
                   2332: 		$result .=
                   2333: 		    &nested_parse(\$dimension{$dim}{'criteria.'.$id},
                   2334: 				  [@_],{'set_dim_id' => $id});
1.20      albertel 2335: 	    }
1.194     albertel 2336: 	    my $status= &get_criteria('status',$version,$dim,$id);
                   2337: 	    
                   2338: 	    my $mandatory=($dimension{$dim}{'criteria.'.$id.'.mandatory'} ne 'N');
                   2339: 	    if ($status eq 'pass') {
                   2340: 		if (!$mandatory) { $optional_passed++; }
                   2341: 	    } elsif ($status eq 'fail') {
                   2342: 		if ($mandatory) { $mandatory_failed++; }
                   2343: 	    } elsif ($status eq 'review') {
                   2344: 		$review++;
                   2345: 	    } elsif ($status eq 'ungraded') {
                   2346: 		$ungraded++;
1.20      albertel 2347: 	    } else {
1.194     albertel 2348: 		$ungraded++;
1.20      albertel 2349: 	    }
1.194     albertel 2350: 	}
                   2351: 
                   2352: 	my $opt_req=$dimension{$dim}{$instance.'.optionalrequired'};
                   2353: 	if ($opt_req !~ /\S/) {
                   2354: 	    $opt_req=
                   2355: 		&Apache::lonxml::get_param('OptionalRequired',
                   2356: 					   $parstack,$safeeval);
                   2357: 	    if ($opt_req !~ /\S/) { $opt_req = 0; }
                   2358: 	}
                   2359: 	if ($optional_passed < $opt_req) {
                   2360: 	    $mandatory_failed++;
                   2361: 	}
                   2362: 	&Apache::lonxml::debug("all instance ".join(':',@{$dimension{$dim}{$instance.'.criterias'}})." results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
                   2363: 	if ($review) {
                   2364: 	    $Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
                   2365: 		'review';
                   2366: 	} elsif ($ungraded) {
                   2367: 	    $Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
                   2368: 		'ungraded';
                   2369: 	} elsif ($mandatory_failed) {
                   2370: 	    $Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
                   2371: 		'fail';
1.69      albertel 2372: 	} else {
1.194     albertel 2373: 	    $Apache::lonhomework::results{"resource.$version.0.$dim.status"}=
                   2374: 		'pass';
1.13      albertel 2375: 	}
1.194     albertel 2376:     } else {
                   2377: 	# any other targets no output
                   2378: 	undef($result);
1.1       albertel 2379:     }
1.194     albertel 2380:     &disable_dimension_parsing();
                   2381:     pop(@Apache::bridgetask::dimension);
                   2382:     return $result;
                   2383: }
1.162     albertel 2384: 
1.194     albertel 2385: sub end_Setup {
                   2386:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2387:     my $result=&Apache::lonxml::endredirection();
                   2388:     my $dim=&get_id($parstack,$safeeval);
                   2389:     my $instance=&get_instance($dim);
                   2390:     my $version=&get_version();
                   2391:     if ($target eq 'web') {
                   2392: 	@Apache::scripttag::parser_env = @_;
                   2393: 	$result.=&Apache::scripttag::xmlparse($dimension{$dim}{'intro'});
                   2394: 	my @instances = $instance;
                   2395: 	if (&Apache::response::showallfoils()) {
                   2396: 	    @instances = @{$dimension{$dim}{'instances'}};
                   2397: 	}
                   2398: 	foreach my $instance (@instances) {
1.162     albertel 2399: 	    @Apache::scripttag::parser_env = @_;
1.194     albertel 2400: 	    $result.=&Apache::scripttag::xmlparse($dimension{$dim}{$instance.'.text'});
1.162     albertel 2401: 	    @Apache::scripttag::parser_env = @_;
1.194     albertel 2402: 	    $result.=&Apache::scripttag::xmlparse($dimension{$dim}{'questiontext'});
1.162     albertel 2403: 	}
1.194     albertel 2404:     } elsif ($target eq 'webgrade' 
                   2405: 	     || $target eq 'grade' && $env{'form.webgrade'}) {
                   2406: 	# in case of any side effects that we need
                   2407: 	@Apache::scripttag::parser_env = @_;
                   2408: 	&Apache::scripttag::xmlparse($dimension{$dim}{'intro'});
                   2409: 	@Apache::scripttag::parser_env = @_;
                   2410: 	&Apache::scripttag::xmlparse($dimension{$dim}{$instance.'.text'});
                   2411: 	@Apache::scripttag::parser_env = @_;
                   2412: 	&Apache::scripttag::xmlparse($dimension{$dim}{'questiontext'});
                   2413:     } else {
                   2414: 	# any other targets no output
                   2415: 	undef($result);
1.162     albertel 2416:     }
1.194     albertel 2417:     pop(@Apache::bridgetask::dimension);
                   2418:     return $result;
1.1       albertel 2419: }
                   2420: 
1.113     albertel 2421: sub grading_history {
1.151     albertel 2422:     my ($version,$dim,$id) = @_;
1.113     albertel 2423:     if (!&Apache::lonnet::allowed('mgq',$env{'request.course.id'})) {
                   2424: 	return '';
                   2425:     }
                   2426:     my ($result,$grader);
1.194     albertel 2427:     my $scope="resource.$version.0.";
                   2428:     $scope .= ($dim ne $top) ? "$dim.$id"
                   2429: 	                     : "$id";
1.113     albertel 2430:     foreach my $t (1..$Apache::lonhomework::history{'version'}) {
                   2431: 	if (exists($Apache::lonhomework::history{$t.':resource.0.regrader'})) {
                   2432: 	    my ($gname,$gdom) = 
1.138     albertel 2433: 		split(':',$Apache::lonhomework::history{$t.':resource.0.regrader'});
1.113     albertel 2434: 	    my $fullname = &Apache::loncommon::plainname($gname,$gdom);
                   2435: 	    $grader = &Apache::loncommon::aboutmewrapper($fullname,
                   2436: 							 $gname,$gdom);
                   2437: 	}
                   2438: 	my $entry;
                   2439: 	if (exists($Apache::lonhomework::history{"$t:$scope.status"})) {
                   2440: 	    $entry.="<tt>".$Apache::lonhomework::history{"$t:$scope.status"}.'</tt>';
                   2441: 	}
                   2442: 	if (exists($Apache::lonhomework::history{"$t:$scope.comment"})) {
                   2443: 	    $entry.=' comment: "'.$Apache::lonhomework::history{"$t:$scope.comment"}.'"';
                   2444: 	}
                   2445: 	if ($entry) {
                   2446: 	    $result.= "<li>$grader : $entry </li>";
                   2447: 	}
                   2448:     }
                   2449:     if ($result) {
1.120     albertel 2450: 	return '<ul class="LC_GRADING_pastgrading">'.$result.'</ul>';
1.113     albertel 2451:     }
                   2452:     return '';
                   2453: }
                   2454: 
1.1       albertel 2455: sub start_IntroParagraph {
1.87      albertel 2456:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel 2457:     my $result;
1.168     albertel 2458:     my $dim = &get_dim_id();
1.153     albertel 2459:     if ($target eq 'grade' || $target eq 'web' || $target eq 'webgrade') {
1.151     albertel 2460: 	if ($tagstack->[-2] eq 'Dimension' || $tagstack->[-2] eq 'Question' ) {
1.169     albertel 2461: 	    $dimension{$dim}{'intro'}=
1.151     albertel 2462: 		&Apache::lonxml::get_all_text('/introparagraph',
                   2463: 					      $parser,$style);
                   2464:        	} elsif ($tagstack->[-2] eq 'Task' && $target eq 'webgrade') {
1.127     albertel 2465: 	    &Apache::lonxml::startredirection();
1.1       albertel 2466: 	}
1.47      albertel 2467: 	
1.1       albertel 2468:     }
                   2469:     return $result;
                   2470: }
                   2471: 
                   2472: sub end_IntroParagraph {
1.127     albertel 2473:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.128     albertel 2474:     if ($tagstack->[-2] eq 'Task' && $target eq 'webgrade') {
1.127     albertel 2475: 	my $result = &Apache::lonxml::endredirection();
                   2476:     }
1.1       albertel 2477: }
                   2478: 
                   2479: sub start_Instance {
                   2480:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.168     albertel 2481:     my $dim = &get_dim_id();
                   2482:     my $id  = &get_id($parstack,$safeeval);
1.169     albertel 2483:     push(@{$dimension{$dim}{'instances'}},$id);
1.168     albertel 2484:     push(@{$Apache::bridgetask::instance{$dim}},$id);
1.19      albertel 2485:     push(@Apache::bridgetask::instancelist,$id);
1.169     albertel 2486:     $dimension{$dim}{$id.'.optionalrequired'}=
1.19      albertel 2487: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.75      albertel 2488:     my $disabled = &Apache::lonxml::get_param('Disabled',$parstack,$safeeval);
                   2489:     if (lc($disabled) eq 'yes') {
1.169     albertel 2490: 	$dimension{$dim}{$id.'.disabled'}='1';
1.75      albertel 2491:     }
1.1       albertel 2492:     return '';
                   2493: }
                   2494: 
                   2495: sub end_Instance {
                   2496: }
                   2497: 
                   2498: sub start_InstanceText {
1.87      albertel 2499:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.168     albertel 2500:     my $dim = &get_dim_id();
                   2501:     my $instance_id=$Apache::bridgetask::instance{$dim}[-1];
1.87      albertel 2502:     my $text=&Apache::lonxml::get_all_text('/instancetext',$parser,$style);
1.153     albertel 2503:     if ($target eq 'grade' || $target eq 'web' || $target eq 'webgrade') {
1.169     albertel 2504: 	$dimension{$dim}{$instance_id.'.text'}=$text;
1.1       albertel 2505:     }
                   2506:     return '';
                   2507: }
                   2508: 
                   2509: sub end_InstanceText {
                   2510:     return '';
                   2511: }
                   2512: 
                   2513: sub start_Criteria {
1.87      albertel 2514:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2515:     my $criteria=&Apache::lonxml::get_all_text('/criteria',$parser,$style);
1.190     albertel 2516:     my $result = '';
1.21      albertel 2517:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
1.168     albertel 2518: 	my $dim = &get_dim_id();
1.19      albertel 2519: 	my $id=&get_id($parstack,$safeeval);
1.194     albertel 2520: 	if ($target eq 'web' || $target eq 'webgrade') {
1.195   ! albertel 2521: 	    if ($target eq 'webgrade' && $dim ne 'top') {
        !          2522: 		&Apache::lonxml::debug(" for $dim $id stashing results into $dim ");
        !          2523: 		$dimension{$dim}{'result'} .= &internal_location($id);
        !          2524: 	    } else {
        !          2525: 		&Apache::lonxml::debug(" not stashing $dim $id");
        !          2526: 		$result .= &internal_location($id);
        !          2527: 	    }
1.194     albertel 2528: 	}
1.169     albertel 2529: 	&Apache::lonxml::debug("Criteria $id with $dim");
1.151     albertel 2530: 	if (&Apache::londefdef::is_inside_of($tagstack,'Instance')) {
1.168     albertel 2531: 	    my $instance_id=$Apache::bridgetask::instance{$dim}[-1];
1.169     albertel 2532: 	    $dimension{$dim}{"criteria.$instance_id.$id"}=$criteria;
                   2533: 	    $dimension{$dim}{"criteria.$instance_id.$id.type"}='criteria';
                   2534: 	    $dimension{$dim}{"criteria.$instance_id.$id.mandatory"}=
1.151     albertel 2535: 		&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.169     albertel 2536: 	    push(@{$dimension{$dim}{$instance_id.'.criterias'}},"$instance_id.$id");
1.151     albertel 2537: 	} else {
1.169     albertel 2538: 	    $dimension{$dim}{'criteria.'.$id}=$criteria;
                   2539: 	    $dimension{$dim}{'criteria.'.$id.'.type'}='criteria';
                   2540: 	    $dimension{$dim}{'criteria.'.$id.'.mandatory'}=
1.151     albertel 2541: 		&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.169     albertel 2542: 	    push(@{$dimension{$dim}{'criterias'}},$id);
1.194     albertel 2543: 	}
                   2544:     }
                   2545:     return $result;
                   2546: }
                   2547: 
                   2548: sub layout_web_Criteria {
                   2549:     my ($dim,$id,$criteria) = @_;
1.190     albertel 2550: 
1.194     albertel 2551:     my $version = &get_version();
                   2552:     my $status= &get_criteria('status', $version,$dim,$id);
                   2553:     my $comment=&get_criteria('comment',$version,$dim,$id);
                   2554:     my $mandatory=($dimension{$dim}{'criteria.'.$id.'.mandatory'} ne 'N');
                   2555:     if ($mandatory) {
                   2556: 	$mandatory='Mandatory';
                   2557:     } else {
                   2558: 	$mandatory='Optional';
1.1       albertel 2559:     }
1.194     albertel 2560:     my $status_display=$status;
                   2561:     $status_display=~s/^([a-z])/uc($1)/e;
                   2562:     my $criteria_info.=
                   2563: 	'<div class="LC_'.$status.' LC_criteria"><h4>'
                   2564: 	.$mandatory.' Criteria</h4><p>';
                   2565:     $criteria_info.= $criteria;
                   2566:     $criteria_info.='</p><p class="LC_grade">'.$status_display.'</p>';
                   2567:     if ($comment =~ /\w/) {
                   2568: 	$criteria_info.='<p class="LC_comment">'.
                   2569: 	    &mt('Comment: [_1]',$comment).'</p>';
                   2570:     }
                   2571:     $criteria_info.='</div>';
                   2572:     return $criteria_info;
                   2573: }
                   2574: 
                   2575: sub layout_webgrade_Criteria {
                   2576:     my ($dim,$id,$criteria) = @_;
                   2577:     my $link=&link($id);
                   2578:     my $version = &get_version();
                   2579:     my $status  = &get_criteria('status',$version,$dim,$id);
                   2580:     my $result = 
                   2581: 	'<div class="LC_GRADING_criteria">'."\n".
                   2582: 	'<div class="LC_GRADING_criteriatext">'."\n".
                   2583: 	$criteria.
                   2584: 	'</div>'."\n".
                   2585: 	'<div class="LC_GRADING_grade">'."\n".
                   2586: 	'<label class="LC_GRADING_ungraded">
                   2587:             <input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.
                   2588: 	    &mt('Ungraded').
                   2589: 	'</label>'."\n".
                   2590: 	'<label class="LC_GRADING_fail">
                   2591:             <input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.
                   2592: 	    &mt('Fail').
                   2593: 	 '</label>'."\n".
                   2594: 	 '<label class="LC_GRADING_pass">
                   2595:              <input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />'.
                   2596: 	     &mt('Pass').
                   2597: 	  '</label>'."\n".
                   2598: 	  '<label class="LC_GRADING_review">
                   2599:              <input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />'.
                   2600: 	     &mt('Review').
                   2601: 	  '</label>'."\n".
                   2602: 	  '</div>'."\n".
                   2603: 	  '<label class="LC_GRADING_comment">'.&mt('Additional Comment for Student')."\n".
                   2604: 	  '<textarea class="LC_GRADING_comment_area" name="HWVAL_comment_'.$link.'">'.&HTML::Entities::encode(&get_criteria('comment',$version,$dim,$id),'<>"&').'</textarea>'."\n".
                   2605: 	  '</label>'."\n".
                   2606: 	  '</div>'."\n".
                   2607: 	  &grading_history($version,$dim,$id);
1.190     albertel 2608:     return $result;
1.1       albertel 2609: }
                   2610: 
1.47      albertel 2611: sub end_Criteria {
                   2612: }
                   2613: 
1.186     albertel 2614: sub start_GraderNote {
                   2615:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2616:     
                   2617:     if ($target eq 'webgrade') {
1.187     albertel 2618: 	return '<div class="LC_GRADING_gradernote"><b>'.
                   2619: 	    &mt('Note to graders:').'</b>';
1.186     albertel 2620:     }
                   2621:     my $note=&Apache::lonxml::get_all_text('/gradernote',$parser,$style); 
                   2622:     return;
                   2623: }
                   2624: 
                   2625: sub end_GraderNote {
                   2626:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2627: 
                   2628:     if ($target eq 'webgrade') {
                   2629: 	return '</div>';
                   2630:     }
                   2631:     return;
                   2632: }
                   2633: 
                   2634: 
                   2635: 
1.4       albertel 2636: sub proctor_validation_screen {
                   2637:     my ($slot) = @_;
1.185     albertel 2638:     my (undef,undef,$domain,$user) = &Apache::lonnet::whichuser();
1.5       albertel 2639:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.44      albertel 2640:     my $name=&Apache::loncommon::plainname($user,$domain);
                   2641:     
1.4       albertel 2642:     my $msg;
1.11      albertel 2643:     if ($env{'form.proctorpassword'}) {
1.4       albertel 2644: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                   2645: 	    .'</font></p>';
                   2646:     }
1.47      albertel 2647:     if (!$env{'form.proctordomain'}) { $env{'form.proctordomain'}=$domain; }
1.4       albertel 2648:     my $result= (<<ENDCHECKOUT);
                   2649: <h2>Proctor Validation</h2>
                   2650:     <p>Your room's proctor needs to validate your access to this resource.</p>
                   2651:     $msg
1.115     albertel 2652: <form name="checkout" method="post" action="$env{'request.uri'}">
1.4       albertel 2653: <input type="hidden" name="validate" value="yes" />
                   2654: <input type="hidden" name="submitted" value="yes" />
                   2655: <table>
1.44      albertel 2656:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$env{'form.proctorname'}" /></td></tr>
1.4       albertel 2657:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.46      albertel 2658:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$env{'form.proctordomain'}" /></td></tr>
1.4       albertel 2659: </table>
                   2660: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
1.44      albertel 2661: <table border="1">
                   2662:   <tr><td>
                   2663:     <table>
                   2664:       <tr><td colspan="2">Student who should be logged in is:</td></tr>
                   2665:       <tr><td>Name:</td><td>$name</td></tr>
1.45      albertel 2666:       <tr><td>Student ID:</td><td>$env{'environment.id'}</td></tr>
1.138     albertel 2667:       <tr><td>Usename</td><td>$user:$domain</td></tr>
1.44      albertel 2668:       <tr><td colspan="2"><img src="$url" /></td></tr>
                   2669:     </table>
                   2670:   </tr></td>
                   2671: </table>
1.4       albertel 2672: </form>
                   2673: ENDCHECKOUT
                   2674:     return $result;
                   2675: }
                   2676: 
1.1       albertel 2677: 1;
                   2678: __END__

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