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

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

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