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

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

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