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

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

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