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

1.1       albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
                      3: #
1.20    ! albertel    4: # $Id: bridgetask.pm,v 1.19 2005/05/09 21:55:01 albertel Exp $
1.1       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: ###
                     29: 
                     30: 
                     31: package Apache::bridgetask; 
                     32: 
                     33: use strict;
                     34: use Apache::lonnet;
                     35: use Apache::File();
                     36: use Apache::lonmenu;
                     37: use Apache::lonlocal;
                     38: use Apache::lonxml;
                     39: use Time::HiRes qw( gettimeofday tv_interval );
1.9       albertel   40: 
1.1       albertel   41: BEGIN {
                     42:     &Apache::lonxml::register('Apache::bridgetask',('Task','IntroParagraph','Dimension','Instance','InstanceText','Criteria','ClosingParagraph'));
                     43: }
                     44: 
1.9       albertel   45: sub initialize_bridgetask {
                     46:     # id of current Dimension, 0 means that no dimension is current 
                     47:     # (inside <Task> only)
                     48:     $Apache::bridgetask::dimension='';
                     49:     # list of all Dimension ids seen
                     50:     @Apache::bridgetask::dimensionlist=();
1.20    ! albertel   51:     # mandatory attribute of all Dimensions seen
        !            52:     %Apache::bridgetask::dimensionmandatory=();
1.9       albertel   53:     # list of all current Instance ids
                     54:     @Apache::bridgetask::instance=();
                     55:     # list of all Instance ids seen in this problem
                     56:     @Apache::bridgetask::instancelist=();
1.15      albertel   57:     # key of queud user data that we are currently grading
                     58:     $Apache::bridgetask::queue_key='';
1.9       albertel   59: }
                     60: 
1.4       albertel   61: sub proctor_check_auth {
                     62:     my ($slot)=@_;
1.11      albertel   63:     my $user=$env{'form.proctorname'};
                     64:     my $domain=$env{'form.proctordomain'};
1.4       albertel   65:     
                     66:     my @allowed=split(",",$slot->{'proctor'});
                     67:     foreach my $possible (@allowed) {
                     68: 	my ($puser,$pdom)=(split('@',$possible));
                     69: 	if ($puser eq $user && $pdom eq $domain) {
1.11      albertel   70: 	    my $authhost=&Apache::lonnet::authenticate($puser,$env{'form.proctorpassword'},$pdom);
1.4       albertel   71: 	    if ($authhost ne 'no_host') {
                     72: 		$Apache::lonhomework::results{'resource.checkedin'}=
                     73: 		    $user.'@'.$domain;
                     74: 		return 1;
                     75: 	    }
                     76: 	}
                     77:     }
                     78:     return 0;
                     79: }
                     80: 
1.8       albertel   81: sub add_previous_version_button {
                     82:     my $result;
1.9       albertel   83:     $result.=&mt(' Show a previously done version: [_1]','<select name="previousversion">
1.8       albertel   84: <option>Pick one</option>
1.9       albertel   85: </select>');
1.8       albertel   86:     return $result;
                     87: }
                     88: 
1.13      albertel   89: sub add_grading_button {
                     90:     my $result;
                     91:     $result.=' <input type="submit" name="gradeasubmission" value="'.
                     92: 	&mt("Get a submission to grade").'" />';
                     93:     $result.='<input type="hidden" name="grade_target" value="webgrade" />';
                     94:     return $result;
                     95: }
                     96: 
1.1       albertel   97: sub start_Task {
                     98:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     99: 
1.4       albertel  100:     my ($status,$accessmsg,$slot);
1.16      albertel  101:     if ($target ne 'webgrade') {
                    102: 	&Apache::structuretags::initialize_storage();
                    103: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
                    104:     } 
                    105: 
1.4       albertel  106:     $Apache::lonhomework::parsing_a_task=1;
1.1       albertel  107:     #should get back a <html> or the neccesary stuff to start XML/MathML
                    108:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    109: 	&Apache::structuretags::page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.16      albertel  110:     
1.8       albertel  111:     if ($target eq 'web') {
                    112: 	$body_tag_start.=&add_previous_version_button();
1.13      albertel  113: 	if ($Apache::lonhomework::modifygrades) {
                    114: 	    $body_tag_start.='<form name="gradesubmission" method="POST" action="';
                    115: 	    my $uri=$env{'request.uri'};
                    116: 	    if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
                    117: 	    $body_tag_start.=$uri.'">'.&add_grading_button()."</form>";
                    118: 	}
1.8       albertel  119:     }
1.1       albertel  120:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    121: 	$target eq 'tex') {
1.14      albertel  122: 	($status,$accessmsg,my $slot_name,$slot) = 
1.4       albertel  123: 	    &Apache::lonhomework::check_task_access('0');
1.9       albertel  124: 	push(@Apache::inputtags::status,$status);
1.14      albertel  125: 	$Apache::inputtags::slot_name=$slot_name;
1.1       albertel  126: 	my $expression='$external::datestatus="'.$status.'";';
                    127: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                    128: 	&Apache::run::run($expression,$safeeval);
                    129: 	&Apache::lonxml::debug("Got $status");
                    130: 	if (( $status eq 'CLOSED' ) ||
                    131: 	    ( $status eq 'BANNED') ||
                    132: 	    ( $status eq 'UNAVAILABLE') ||
1.3       albertel  133: 	    ( $status eq 'NOT_IN_A_SLOT') ||
1.4       albertel  134: 	    ( $status eq 'NEEDS_CHECKIN') ||
1.1       albertel  135: 	    ( $status eq 'INVALID_ACCESS')) {
                    136: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
                    137: 	    if ( $target eq "web" ) {
1.4       albertel  138: 		$result.= $head_tag_start.'</head>'.$body_tag_start;
                    139: 		my $msg;
1.1       albertel  140: 		if ($status eq 'UNAVAILABLE') {
                    141: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3       albertel  142: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    143: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.4       albertel  144: 		} elsif ($status eq 'NEEDS_CHECKIN') {
                    145: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
                    146: 			'</h1>'.&proctor_validation_screen($slot);
1.1       albertel  147: 		} elsif ($status ne 'NOT_YET_VIEWED') {
                    148: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
                    149: 		}
                    150: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
                    151: 		    $msg.='The problem '.$accessmsg;
                    152: 		}
                    153: 		$result.=$msg.'<br />';
                    154: 	    } elsif ($target eq 'tex') {
                    155: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
                    156: 		if ($status eq 'UNAVAILABLE') {
                    157: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
                    158: 		} else {
                    159: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
                    160: 		}
1.4       albertel  161: 	    } elsif ($target eq 'grade') {
                    162: 		if ($status eq 'NEEDS_CHECKIN') {
                    163: 		    if (&proctor_check_auth($slot)) {
                    164: 			#FIXME immeadiatly add this to the grading queue
                    165:                         #      with slot->{'endtime'} for when grading can 
                    166:                         #      begin on this resource
                    167:  		    }
                    168: 		}
1.1       albertel  169: 	    }
                    170: 	} elsif ($target eq 'web') {
                    171: 	    my $name= &Apache::structuretags::get_resource_name($parstack,$safeeval);
                    172: 	    $result.="$head_tag_start<title>$name</title></head>
                    173:               $body_tag_start \n $form_tag_start".	
                    174: 	      '<input type="hidden" name="submitted" value="yes" />';
                    175: 	    # if we are viewing someone else preserve that info
1.11      albertel  176: 	    if (defined $env{'form.grade_symb'}) {
1.1       albertel  177: 		foreach my $field ('symb','courseid','domain','username') {
                    178: 		    $result .= '<input type="hidden" name="grade_'.$field.
1.11      albertel  179: 			'" value="'.$env{"form.grade_$field"}.'" />'."\n";
1.1       albertel  180: 		}
                    181: 	    }
                    182: 	}
1.13      albertel  183:     } elsif ($target eq 'webgrade') {
1.15      albertel  184: 	$result.=$head_tag_start.$body_tag_start.$form_tag_start;
1.20    ! albertel  185: 	$result.='<input type="hidden" name="webgrade" value="yes" />';
1.14      albertel  186: 	$result.=&show_queue();
1.15      albertel  187: 	my $todo=&get_from_queue();
                    188: 	if ($todo) {
1.16      albertel  189: 	    &setup_env_for_other_user($todo,$safeeval);
1.15      albertel  190: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
1.16      albertel  191: 	    $result.="\n".'<table><tr><td>Found '.
                    192: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
1.15      albertel  193: 	    $result.='<input type="hidden" name="gradingkey" value="'.
1.16      albertel  194: 		&Apache::lonnet::escape($todo).'" />';
1.15      albertel  195: 	    $Apache::bridgetask::queue_key=$todo;
1.16      albertel  196: 	    &Apache::structuretags::initialize_storage();
                    197: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.18      albertel  198: 	    $result.="\n".'<table width="100%" style="width:100%" border="1">';
1.15      albertel  199: 	} else {
                    200: 	    $result.="\n".
                    201: 		'<table><tr><td>No user to be graded.</td></tr></table>';
                    202: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
                    203: 	}
                    204: 	
1.1       albertel  205:     } else {
                    206: 	# page_start returned a starting result, delete it if we don't need it
                    207: 	$result = '';
                    208:     }
                    209:     return $result;
                    210: }
                    211: 
                    212: sub end_Task {
                    213:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    214:     my $result='';
                    215:     my $status=$Apache::inputtags::status['-1'];
                    216:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.15      albertel  217: 	$target eq 'tex') {
1.1       albertel  218: 	if (
1.11      albertel  219: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.1       albertel  220: 	    ($target eq 'answer') || ($target eq 'tex')
                    221: 	   ) {
                    222: 	    if ($target eq 'web') {
1.9       albertel  223: 		if ($status eq 'CAN_ANSWER') {
1.15      albertel  224: 		    $result.="\n".'<table border="1">'.
1.9       albertel  225: 			&Apache::inputtags::file_selector('0',"bridgetask","*",
                    226: 							  'portfolioonly').
                    227: 							  "</table>";
                    228: 		    $result.=&Apache::inputtags::gradestatus('0');
                    229: 		}
1.13      albertel  230: 	    } 
                    231: 	    if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  232: 		$result.=&Apache::lonxml::xmlend().'</html>';
                    233: 	    }
                    234: 	}
1.20    ! albertel  235: 	if ($target eq 'grade' && !$env{'form.webgrade'}) {
1.12      albertel  236: 	    my $award='SUBMITTED';
1.20    ! albertel  237: 	    &Apache::essayresponse::file_submission('0','bridgetask',
        !           238: 						    'portfiles',\$award);
1.14      albertel  239: 	    if ($award eq 'SUBMITTED' &&
                    240: 		$Apache::lonhomework::results{"resource.0.bridgetask.portfiles"}) {
1.10      albertel  241: 		$Apache::lonhomework::results{"resource.0.tries"}=
                    242: 		    1+$Apache::lonhomework::history{"resource.0.tries"};
                    243: 	    }
1.14      albertel  244: 	    $Apache::lonhomework::results{"resource.0.award"}=$award;
1.4       albertel  245: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    246: 	    &Apache::structuretags::finalize_storage();
1.14      albertel  247: 	    if ($award eq 'SUBMITTED') {
                    248: 		&add_to_queue();
                    249: 	    }
1.1       albertel  250: 	}
1.20    ! albertel  251: 	if ($target eq 'grade' && $env{'form.webgrade'}) {
        !           252: 	    my $optional_required=
        !           253: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
        !           254: 					   $safeeval);
        !           255: 	    my $optional_passed=0;
        !           256: 	    my $mandatory_failed=0;
        !           257: 	    my $ungraded=0;
        !           258: 	    my $review=0;   
        !           259: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
        !           260: 		my $status=
        !           261: 		    $Apache::lonhomeork::results{"resource.$dim_id.status"};
        !           262: 		my $mandatory=
        !           263: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
        !           264: 		if ($status eq 'pass') {
        !           265: 		    if (!$mandatory) { $optional_passed++; }
        !           266: 		} elsif ($status eq 'fail') {
        !           267: 		    if ($mandatory) { $mandatory_failed++; }
        !           268: 		} elsif ($status eq 'ungraded') {
        !           269: 		    $ungraded++;
        !           270: 		} elsif ($status eq 'review') {
        !           271: 		    $review++;
        !           272: 		}	
        !           273: 	    }
        !           274: 	    if ($optional_passed < $optional_required) {
        !           275: 		$mandatory_failed++;
        !           276: 	    }
        !           277: 	    $Apache::lonhomeork::results{'resource.grader'}=
        !           278: 		$env{'user.name'}.'@'.$env{'user.domain'};
        !           279: 	    if ($review) {
        !           280: 		$Apache::lonhomeork::results{'resource.status'}='review';
        !           281: 		&move_to_review_queue();
        !           282: 	    } elsif ($ungraded) {
        !           283: 		$Apache::lonhomeork::results{'resource.status'}='ungraded';
        !           284: 		&check_queue_unlock();
        !           285: 	    } elsif ($mandatory_failed) {
        !           286: 		$Apache::lonhomeork::results{'resource.status'}='fail';
        !           287: 		$Apache::lonhomework::results{"resource.0.award"}='EXACT_ANS';
        !           288: 		&remove_from_queue();
        !           289: 	    } else {
        !           290: 		$Apache::lonhomeork::results{'resource.status'}='pass';
        !           291: 		$Apache::lonhomework::results{"resource.0.award"}='INCORRECT';
        !           292: 		&remove_from_queue();
        !           293: 	    }
        !           294: 	}
1.15      albertel  295:     } elsif ($target eq 'webgrade') {
1.18      albertel  296: 	$result.="</table>\n<hr />";
1.20    ! albertel  297: 	#$result.='<input type="submit" name="next" value="'.
        !           298: 	#    &mt('Save &amp; Next').'" /> ';
        !           299: 	#$result.='<input type="submit" name="end" value="'.
        !           300: 	#    &mt('Save &amp; Stop Grading').'" /> ';
        !           301: 	#$result.='<input type="submit" name="throwaway" value="'.
        !           302: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
        !           303: 	#$result.='<input type="submit" name="save" value="'.
        !           304: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
1.15      albertel  305: 	$result.='</form>'.&Apache::loncommon::endbodytag().'</html>';
1.1       albertel  306:     } elsif ($target eq 'meta') {
1.2       albertel  307: 	$result.='<parameter part="0" package="Task"></parameter>'."\n";
1.1       albertel  308: 	#$result.=&Apache::response::meta_part_order();
                    309: 	#$result.=&Apache::response::meta_response_order();
                    310:     }
1.4       albertel  311:     undef($Apache::lonhomework::parsing_a_task);
1.1       albertel  312:     return $result;
                    313: }
                    314: 
1.16      albertel  315: sub setup_env_for_other_user {
                    316:     my ($queue_key,$safeeval)=@_;
                    317:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
                    318:     $env{'form.grade_symb'}=$symb;
                    319:     $env{'form.grade_domain'}=$udom;
                    320:     $env{'form.grade_username'}=$uname;
                    321:     $env{'form.grade_courseid'}=$env{'request.course.id'};
                    322:     &Apache::lonxml::initialize_rndseed($safeeval);
                    323: }
                    324: 
1.14      albertel  325: sub add_to_queue {
                    326:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    327:     my $cnum=$env{'course.'.$cid.'.num'};
                    328:     my $cdom=$env{'course.'.$cid.'.domain'};
                    329:     my %data;
                    330:     $data{"$symb\0queue\0$uname\@$udom"}=[$Apache::inputtags::slot_name];
                    331:     &Apache::lonnet::put('gradingqueue',\%data,$cdom,$cnum);
                    332: }
                    333: 
                    334: sub show_queue {
                    335:     my $result;
                    336:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    337:     my $cnum=$env{'course.'.$cid.'.num'};
                    338:     my $cdom=$env{'course.'.$cid.'.domain'};
1.16      albertel  339:     my $regexp="^$symb\0";
1.14      albertel  340:     my %queue=&Apache::lonnet::dump('gradingqueue',$cdom,$cnum,$regexp);
1.20    ! albertel  341:     $result.="\n<h3>Current Queue</h3><table><tr><th>resource</th>".
        !           342: 	"<th>user</th><th>type</th><th>data</th></tr>";
1.14      albertel  343:     foreach my $key (sort(keys(%queue))) {
1.16      albertel  344: 	if ($key=~/locked$/) {
                    345: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                    346: 	    my $title=&Apache::lonnet::gettitle($symb);
                    347: 	    $result.="<tr><td>$title</td><td>$uname</td><td>";
                    348: 	    $result.='<td>lock</td><td>'.$queue{$key}.'</td></tr>';
                    349: 	} elsif ($key=~/timestamp$/) {
                    350: 	    my ($symb,undef) = split("\0",$key);
                    351: 	    my $title=&Apache::lonnet::gettitle($symb);
                    352: 	    $result.="<tr><td>$title</td><td></td><td>";
                    353: 	    $result.='<td>last queue modification time</td><td>'.
                    354: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
                    355: 	} else {
                    356: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
                    357: 	    my $title=&Apache::lonnet::gettitle($symb);
                    358: 	    $result.="<tr><td>$title</td><td>$uname</td><td>";
                    359: 	    my $slot=$queue{$key}->[0];
                    360: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
1.20    ! albertel  361: 	    $result.='<td>queue entry</td><td>End time: '.
        !           362: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
        !           363: 		"</td></tr>";
1.16      albertel  364: 	}
1.14      albertel  365:     }
1.15      albertel  366:     $result.="</table><hr />\n";
1.14      albertel  367:     return $result;
                    368: }
                    369: 
                    370: sub decode_queue_key {
                    371:     my ($key)=@_;
                    372:     my ($symb,undef,$user) = split("\0",$key);
                    373:     my ($uname,$udom) = split('@',$user);
                    374:     return ($symb,$uname,$udom);
                    375: }
                    376: 
                    377: sub queue_key_locked {
1.15      albertel  378:     my ($key,$cdom,$cnum)=@_;
1.14      albertel  379:     my ($key_locked,$value)=
                    380: 	&Apache::lonnet::get('gradingqueue',["$key\0locked"],$cdom,$cnum);
                    381:     if ($key_locked eq "$key\0locked") {
                    382: 	return $value;
                    383:     }
                    384:     return undef;
                    385: }
                    386: 
                    387: sub pick_from_queue_data {
1.15      albertel  388:     my ($check_section,$queue,$cdom,$cnum)=@_;
1.16      albertel  389:     foreach my $key (keys(%$queue)) {
1.14      albertel  390: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
                    391: 	if ($check_section) {
                    392: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
1.17      albertel  393: 	    if ($section eq $check_section) {
                    394: 		&Apache::lonnet::logthis("my sec");
1.15      albertel  395: 		next;
                    396: 	    }
1.14      albertel  397: 	}
                    398: 	my $slot=$queue->{$key}[0];
                    399: 	my %slot_data=&Apache::lonnet::get_slot($slot);
1.15      albertel  400: 	if ($slot_data{'endtime'} > time) { 
                    401: 	    &Apache::lonnet::logthis("not time");
                    402: 	    next;
                    403: 	}
                    404: 	if (&queue_key_locked($key,$cdom,$cnum)) {
                    405: 	    &Apache::lonnet::logthis("someone already has um.");
                    406: 	    next;
                    407: 	}
1.14      albertel  408: 	return $key;
                    409:     }
                    410:     return undef;
                    411: }
                    412: 
1.15      albertel  413: sub find_mid_grade {
                    414:     my ($symb,$cdom,$cnum)=@_;
1.16      albertel  415:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
1.15      albertel  416:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
                    417:     if ($todo) {
                    418: 	my $who=&queue_key_locked($todo,$cdom,$cnum);
                    419: 	if ($who eq $me) { return $todo; }
                    420:     }
                    421:     my $regexp="^$symb\0.*\0locked\$";
                    422:     my %locks=&Apache::lonnet::dump('gradingqueue',$cdom,$cnum,$regexp);
                    423:     foreach my $key (keys(%locks)) {
                    424: 	my $who=$locks{$key};
                    425: 	if ($who eq $me) {
                    426: 	    $todo=$key;
                    427: 	    $todo=~s/\0locked$//;
                    428: 	    return $todo;
                    429: 	}
                    430:     }
                    431:     return undef;
                    432: }
                    433: 
1.14      albertel  434: sub get_from_queue {
                    435:     my $result;
                    436:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
                    437:     my $cnum=$env{'course.'.$cid.'.num'};
                    438:     my $cdom=$env{'course.'.$cid.'.domain'};
                    439:     my $todo;
1.15      albertel  440:     # FIXME need to find if I am 'mid grading'
                    441:     $todo=&find_mid_grade($symb,$cdom,$cnum);
                    442:     &Apache::lonnet::logthis("found ".join(':',&decode_queue_key($todo)));
1.16      albertel  443:     if ($todo) { return $todo; }
1.14      albertel  444:     while (1) {
                    445: 	my $starttime=time;
                    446: 	&Apache::lonnet::put('gradingqueue',{"$symb\0timestamp"=>$starttime},
                    447: 			     $cdom,$cnum);
1.15      albertel  448: 	&Apache::lonnet::logthis("$starttime");
1.14      albertel  449: 	my $regexp="^$symb\0queue\0";
                    450: 	my %queue=&Apache::lonnet::dump('gradingqueue',$cdom,$cnum,$regexp);
                    451: 	#make a pass looking for a user in my section
                    452: 	if ($env{'request.course.sec'}) {
1.15      albertel  453: 	    &Apache::lonnet::logthis("sce");
                    454: 	    $todo=&pick_from_queue_data($env{'request.course.sec'},\%queue,
                    455: 					$cdom,$cnum);
                    456: 	    &Apache::lonnet::logthis("sce $todo");
1.14      albertel  457: 	}
                    458: 	# no one in our section so look for any user that is ready for grading
                    459: 	if (!$todo) {
1.15      albertel  460: 	    &Apache::lonnet::logthis("no sce");
                    461: 	    $todo=&pick_from_queue_data($env{'request.course.sec'},\%queue,
                    462: 					$cdom,$cnum);
                    463: 	    &Apache::lonnet::logthis("no sce $todo");
1.14      albertel  464: 	}
                    465: 	# no user to grade 
                    466: 	if (!$todo) { last; }
1.15      albertel  467: 	&Apache::lonnet::logthis("got $todo");
1.14      albertel  468: 	# otherwise found someone so lets try to lock them
                    469: 	my $success=&Apache::lonnet::newput('gradingqueue',
                    470: 					    {"$todo\0locked"=>
                    471: 						 $env{'user.name'}.'@'.$env{'user.domain'}},
                    472: 					   $cdom,$cnum);
                    473: 	# someone else already picked them
1.15      albertel  474: 	&Apache::lonnet::logthis("success $todo");
1.14      albertel  475: 	if ($success ne 'ok') { next; }
                    476: 	my (undef,$endtime)=
                    477: 	    &Apache::lonnet::get('gradingqueue',["$symb\0timestamp"],
                    478: 				 $cdom,$cnum);
1.15      albertel  479: 	&Apache::lonnet::logthis("emd  $endtime");
1.14      albertel  480: 	# someone else already modified the queue, 
                    481: 	# perhaps our picked user wass already fully graded between
                    482: 	# when we picked him and when we locked his record? so lets
                    483: 	# double check.
                    484: 	if ($endtime != $starttime) {
                    485: 	    my ($key,$value)=
                    486: 		&Apache::lonnet::get('gradingqueue',["$todo"],
                    487: 				     $cdom,$cnum);
1.15      albertel  488: 	    &Apache::lonnet::logthis("check  $key .. $value");
1.14      albertel  489: 	    if ($key eq $todo && ref($value)) {
                    490: 	    } else {
                    491: 		&Apache::lonnet::del('gradingqueue',["$todo\0locked"],
                    492: 				     $cdom,$cnum);
1.15      albertel  493: 		&Apache::lonnet::logthis("del");
1.14      albertel  494: 		next;
                    495: 	    }
                    496: 	}
1.15      albertel  497: 	&Apache::lonnet::logthis("last $todo");
1.14      albertel  498: 	last;
                    499:     }
                    500:     return $todo;
                    501: }
                    502: 
1.1       albertel  503: sub start_ClosingParagraph {
                    504:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    505:     my $result;
                    506:     if ($target eq 'web') {
1.13      albertel  507:     } elsif ($target eq 'webgrade') {
                    508: 	&Apache::lonxml::startredirection();
1.1       albertel  509:     }
                    510:     return $result;
                    511: }
                    512: 
                    513: sub end_ClosingParagraph {
                    514:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    515:     my $result;
                    516:     if ($target eq 'web') {
1.13      albertel  517:     } elsif ($target eq 'webgrade') {
                    518: 	&Apache::lonxml::endredirection();
1.1       albertel  519:     }
                    520:     return $result;
                    521: }
                    522: 
1.19      albertel  523: sub get_id {
                    524:     my ($parstack,$safeeval)=@_;
                    525:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
                    526:     if (!$id) { $id=$Apache::lonxml::curdepth; }
                    527:     return $id;
                    528: }
                    529: 
1.1       albertel  530: my %dimension;
                    531: sub start_Dimension {
                    532:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    533:     undef(%dimension);
1.19      albertel  534:     my $dim_id=&get_id($parstack,$safeeval);
1.9       albertel  535:     $Apache::bridgetask::dimension=$dim_id;
                    536:     push(@Apache::bridgetask::dimensionlist,$dim_id);
                    537:     undef(@Apache::bridgetask::instance);
1.20    ! albertel  538:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
        !           539: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel  540:     return '';
                    541: }
                    542: 
1.13      albertel  543: sub get_instance {
                    544:     #FIXME just grabbing the first one for now, need
                    545:     #to randomly pick one until all have been seen
                    546:     #then start repicking
                    547:     &Apache::response::pushrandomnumber();
                    548:     my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
                    549:     return $order[0];
                    550: }
                    551: 
1.18      albertel  552: {
                    553:     my $last_link;
                    554:     sub end_Dimension {
                    555: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    556: 	my $result;
1.19      albertel  557: 	my $instance=&get_instance();
1.18      albertel  558: 	if ($target eq 'web') {
                    559: 	    $result=$dimension{'intro'}.$dimension{$instance.'.text'};
                    560: 	} elsif ($target eq 'webgrade') {
                    561: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
                    562: 		my $link='criteria_'.$instance.'_'.$id;
1.20    ! albertel  563: 		my $status=$Apache::lonhomeork::history{'resource.'.$instance.'.'.$id.'.status'};
1.19      albertel  564: 		$result.='<tr><td width="100%" valign="top">'.
1.18      albertel  565: 		    '<a name="'.$link.'" />'.
                    566: 		    '<a name="next_'.$last_link.'" />'.
                    567: 		    '<br /><textarea enabled="false" style="width:100%" rows="8" width="25" wrap="hard">'.$dimension{$instance.'.criteria.'.$id}.'</textarea>'.
                    568: 		    #$dimension{$instance.'.criteria.'.$id}.
                    569: 		    '</td>'.
1.19      albertel  570: 		    '<td><nobr>Additional Comment for Student</nobr> <br />'.
1.20    ! albertel  571: 		    '<textarea style="width:100%" rows="8" width="25" wrap="hard" name="HWVAL_comment_'.$link.'">'.&HTML::Entities::encode($Apache::lonhomeork::history{'resource.'.$instance.'.'.$id.'.comment'}).'</textarea>'.
1.18      albertel  572: 		    '</td>'.
                    573: 		    '<td>'.
1.20    ! albertel  574: 		    '<nobr><label><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').'/>Ungraded</label></nobr><br />'.
        !           575: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />Pass</label><br />'.
        !           576: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />Fail</label><br />'.
        !           577: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />Review</label><br />'.
1.18      albertel  578: 		    '</td>'.
                    579: 		    '<td>'.
                    580: 		    '<a href="#'.$last_link.'">Prev</a><br />'.
1.20    ! albertel  581: 		    '<a href="#next_'.$link.'">Next</a><br /><br /><br />'.
        !           582: 		    '<input type="submit" name="next" value="'.
        !           583: 		    &mt('Done').'" /> '.
1.18      albertel  584: 		    '</td></tr>';
                    585: 		$last_link=$link;
                    586: 	    }
1.19      albertel  587: 	} elsif ($target eq 'grade') {
                    588: 	    my $optional_passed=0;
1.20    ! albertel  589: 	    my $mandatory_failed=0;
        !           590: 	    my $ungraded=0;
        !           591: 	    my $review=0;
1.19      albertel  592: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
1.20    ! albertel  593: 		my $status=$Apache::lonhomeork::results{'resource.'.$instance.'.'.$id.'.status'}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
        !           594: 		$Apache::lonhomeork::results{'resource.'.$instance.'.'.$id.'.comment'}=$env{'form.HWVAL_comment_'.$instance.'_'.$id};
        !           595: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
        !           596: 		if ($status eq 'pass') {
        !           597: 		    if (!$mandatory) { $optional_passed++; }
        !           598: 		} elsif ($status eq 'fail') {
        !           599: 		    if ($mandatory) { $mandatory_failed++; }
        !           600: 		} elsif ($status eq 'ungraded') {
        !           601: 		    $ungraded++;
        !           602: 		} elsif ($status eq 'review') {
        !           603: 		    $review++;
1.19      albertel  604: 		}
                    605: 	    }
1.20    ! albertel  606: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
        !           607: 		$mandatory_failed++;
        !           608: 	    }
        !           609: 	    if ($review) {
        !           610: 		$Apache::lonhomeork::results{'resource.'.$instance.'.status'}='review';
        !           611: 	    } elsif ($ungraded) {
        !           612: 		$Apache::lonhomeork::results{'resource.'.$instance.'.status'}='ungraded';
        !           613: 	    } elsif ($mandatory_failed) {
        !           614: 		$Apache::lonhomeork::results{'resource.'.$instance.'.status'}='fail';
        !           615: 	    } else {
        !           616: 		$Apache::lonhomeork::results{'resource.'.$instance.'.status'}='pass';
        !           617: 	    }
1.13      albertel  618: 	}
1.18      albertel  619: 	return $result;
1.1       albertel  620:     }
                    621: }
                    622: 
                    623: sub start_IntroParagraph {
                    624:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    625:     my $result;
1.13      albertel  626:     if ($target eq 'web' || $target eq 'webgrade') {
                    627: 	if ($tagstack->[-2] eq 'Dimension' || $target eq 'webgrade') {
1.1       albertel  628: 	    &Apache::lonxml::startredirection();
                    629: 	}
                    630:     }
                    631:     return $result;
                    632: }
                    633: 
                    634: sub end_IntroParagraph {
                    635:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    636:     my $result;
1.13      albertel  637:     if ($target eq 'web' || $target eq 'webgrade') {
                    638: 	if ($tagstack->[-2] eq 'Dimension' || $target eq 'webgrade') {
1.1       albertel  639: 	    $dimension{'intro'}=&Apache::lonxml::endredirection();
                    640: 	}
                    641:     }
                    642:     return $result;
                    643: }
                    644: 
                    645: sub start_Instance {
                    646:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19      albertel  647:     my $id=&get_id($parstack,$safeeval);
                    648:     push(@{$dimension{'instances'}},$id);
                    649:     push(@Apache::bridgetask::instance,$id);
                    650:     push(@Apache::bridgetask::instancelist,$id);
1.20    ! albertel  651:     $dimension{$id.'.optionalrequired'}=
1.19      albertel  652: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
1.1       albertel  653:     return '';
                    654: }
                    655: 
                    656: sub end_Instance {
                    657:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    658:     return '';
                    659: }
                    660: 
                    661: sub start_InstanceText {
                    662:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.13      albertel  663:     if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  664: 	&Apache::lonxml::startredirection();
                    665:     }
                    666:     return '';
                    667: }
                    668: 
                    669: sub end_InstanceText {
                    670:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel  671:     my $instance_id=$Apache::bridgetask::instance[-1];
1.13      albertel  672:     if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  673: 	$dimension{$instance_id.'.text'}=&Apache::lonxml::endredirection();
                    674:     }
                    675:     return '';
                    676: }
                    677: 
                    678: sub start_Criteria {
                    679:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.13      albertel  680:     if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  681: 	&Apache::lonxml::startredirection();
                    682:     }
                    683:     return '';
                    684: }
                    685: 
                    686: sub end_Criteria {
                    687:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9       albertel  688:     my $instance_id=$Apache::bridgetask::instance[-1];
1.13      albertel  689:     if ($target eq 'web' || $target eq 'webgrade') {
1.1       albertel  690: 	my $criteria=&Apache::lonxml::endredirection();
1.19      albertel  691: 	my $id=&get_id($parstack,$safeeval);
1.1       albertel  692: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
1.19      albertel  693: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
                    694: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
1.1       albertel  695: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
                    696:     }
                    697:     return '';
                    698: }
                    699: 
1.4       albertel  700: sub proctor_validation_screen {
                    701:     my ($slot) = @_;
                    702:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5       albertel  703:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.11      albertel  704:     $user=$env{'form.proctorname'};
                    705:     if ($env{'form.proctordomain'}) { $domain=$env{'form.proctordomain'}; }
1.4       albertel  706:     my $msg;
1.11      albertel  707:     if ($env{'form.proctorpassword'}) {
1.4       albertel  708: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
                    709: 	    .'</font></p>';
                    710:     }
                    711:     my $result= (<<ENDCHECKOUT);
                    712: <h2>Proctor Validation</h2>
                    713:     <p>Your room's proctor needs to validate your access to this resource.</p>
                    714:     $msg
1.11      albertel  715: <form name="checkout" method="POST" action="$env{'request.uri'}">
1.4       albertel  716: <input type="hidden" name="validate" value="yes" />
                    717: <input type="hidden" name="submitted" value="yes" />
                    718: <table>
                    719:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$user" /></td></tr>
                    720:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.6       albertel  721:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$domain" /></td></tr>
1.4       albertel  722: </table>
                    723: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
                    724: Student who should be logged in is:<br />
1.5       albertel  725: <img src="$url" /><br />
1.4       albertel  726: </form>
                    727: ENDCHECKOUT
                    728:     return $result;
                    729: }
                    730: 
1.1       albertel  731: 1;
                    732: __END__

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