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

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

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