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

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

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