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

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

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