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

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

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