File:  [LON-CAPA] / loncom / homework / bridgetask.pm
Revision 1.21: download - view: text, annotated - select for diffs
Tue May 10 16:44:35 2005 UTC (19 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- got a successful grading done

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: bridgetask.pm,v 1.21 2005/05/10 16:44:35 albertel Exp $
    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 );
   40: 
   41: BEGIN {
   42:     &Apache::lonxml::register('Apache::bridgetask',('Task','IntroParagraph','Dimension','Instance','InstanceText','Criteria','ClosingParagraph'));
   43: }
   44: 
   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=();
   51:     # mandatory attribute of all Dimensions seen
   52:     %Apache::bridgetask::dimensionmandatory=();
   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=();
   57:     # key of queud user data that we are currently grading
   58:     $Apache::bridgetask::queue_key='';
   59: }
   60: 
   61: sub proctor_check_auth {
   62:     my ($slot)=@_;
   63:     my $user=$env{'form.proctorname'};
   64:     my $domain=$env{'form.proctordomain'};
   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) {
   70: 	    my $authhost=&Apache::lonnet::authenticate($puser,$env{'form.proctorpassword'},$pdom);
   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: 
   81: sub add_previous_version_button {
   82:     my $result;
   83:     $result.=&mt(' Show a previously done version: [_1]','<select name="previousversion">
   84: <option>Pick one</option>
   85: </select>');
   86:     return $result;
   87: }
   88: 
   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: 
   97: sub start_Task {
   98:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   99: 
  100:     my ($status,$accessmsg,$slot);
  101:     if ($target ne 'webgrade') {
  102: 	&Apache::structuretags::initialize_storage();
  103: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
  104:     } 
  105: 
  106:     $Apache::lonhomework::parsing_a_task=1;
  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);
  110:     
  111:     if ($target eq 'web') {
  112: 	$body_tag_start.=&add_previous_version_button();
  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: 	}
  119:     }
  120:     if ($target eq 'web' || ($target eq 'grade' && !$env{'form.webgrade'}) || $target eq 'answer' ||
  121: 	$target eq 'tex') {
  122: 	($status,$accessmsg,my $slot_name,$slot) = 
  123: 	    &Apache::lonhomework::check_task_access('0');
  124: 	push(@Apache::inputtags::status,$status);
  125: 	$Apache::inputtags::slot_name=$slot_name;
  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') ||
  133: 	    ( $status eq 'NOT_IN_A_SLOT') ||
  134: 	    ( $status eq 'NEEDS_CHECKIN') ||
  135: 	    ( $status eq 'INVALID_ACCESS')) {
  136: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
  137: 	    if ( $target eq "web" ) {
  138: 		$result.= $head_tag_start.'</head>'.$body_tag_start;
  139: 		my $msg;
  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>';
  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>';
  144: 		} elsif ($status eq 'NEEDS_CHECKIN') {
  145: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
  146: 			'</h1>'.&proctor_validation_screen($slot);
  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: 		}
  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: 		}
  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
  176: 	    if (defined $env{'form.grade_symb'}) {
  177: 		foreach my $field ('symb','courseid','domain','username') {
  178: 		    $result .= '<input type="hidden" name="grade_'.$field.
  179: 			'" value="'.$env{"form.grade_$field"}.'" />'."\n";
  180: 		}
  181: 	    }
  182: 	}
  183:     } elsif ( ($target eq 'grade' && $env{'form.webgrade'}) ||
  184: 	      $target eq 'webgrade') {
  185: 	if ($target eq 'webgrade') {
  186: 	    $result.=$head_tag_start.$body_tag_start.$form_tag_start;
  187: 	    $result.='<input type="hidden" name="webgrade" value="yes" />';
  188: 	    $result.=&show_queue();
  189: 	}
  190: 	my $todo=&get_from_queue();
  191: 	if ($todo) {
  192: 	    &setup_env_for_other_user($todo,$safeeval);
  193: 	    my ($symb,$uname,$udom)=&decode_queue_key($todo);
  194: 	    $result.="\n".'<table><tr><td>Found '.
  195: 		&Apache::lonnet::gettitle($symb).' for '.$uname.' at '.$udom.'</td></tr></table>';
  196: 	    $result.='<input type="hidden" name="gradingkey" value="'.
  197: 		&Apache::lonnet::escape($todo).'" />';
  198: 	    $Apache::bridgetask::queue_key=$todo;
  199: 	    &Apache::structuretags::initialize_storage();
  200: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
  201: 	    $result.="\n".'<table width="100%" style="width:100%" border="1">';
  202: 	} else {
  203: 	    if ($target eq 'webgrade') {
  204: 		$result.="\n".
  205: 		    '<table><tr><td>No user to be graded.</td></tr></table>';
  206: 	    }
  207: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
  208: 	}
  209:     } else {
  210: 	# page_start returned a starting result, delete it if we don't need it
  211: 	$result = '';
  212:     }
  213:     return $result;
  214: }
  215: 
  216: sub end_Task {
  217:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  218:     my $result='';
  219:     my $status=$Apache::inputtags::status['-1'];
  220:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  221: 	$target eq 'tex') {
  222: 	if (
  223: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
  224: 	    ($target eq 'answer') || ($target eq 'tex')
  225: 	   ) {
  226: 	    if ($target eq 'web') {
  227: 		if ($status eq 'CAN_ANSWER') {
  228: 		    $result.="\n".'<table border="1">'.
  229: 			&Apache::inputtags::file_selector('0',"bridgetask","*",
  230: 							  'portfolioonly').
  231: 							  "</table>";
  232: 		    $result.=&Apache::inputtags::gradestatus('0');
  233: 		}
  234: 	    } 
  235: 	    if ($target eq 'web' || $target eq 'webgrade') {
  236: 		$result.=&Apache::lonxml::xmlend().'</html>';
  237: 	    }
  238: 	}
  239: 	if ($target eq 'grade' && !$env{'form.webgrade'}) {
  240: 	    my $award='SUBMITTED';
  241: 	    &Apache::essayresponse::file_submission('0','bridgetask',
  242: 						    'portfiles',\$award);
  243: 	    if ($award eq 'SUBMITTED' &&
  244: 		$Apache::lonhomework::results{"resource.0.bridgetask.portfiles"}) {
  245: 		$Apache::lonhomework::results{"resource.0.tries"}=
  246: 		    1+$Apache::lonhomework::history{"resource.0.tries"};
  247: 	    }
  248: 	    $Apache::lonhomework::results{"resource.0.award"}=$award;
  249: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  250: 	    &Apache::structuretags::finalize_storage();
  251: 	    if ($award eq 'SUBMITTED') {
  252: 		&add_to_queue();
  253: 	    }
  254: 	}
  255: 	if ($target eq 'grade' && $env{'form.webgrade'}) {
  256: 	    my $optional_required=
  257: 		&Apache::lonxml::get_param('OptionalRequired',$parstack,
  258: 					   $safeeval);
  259: 	    my $optional_passed=0;
  260: 	    my $mandatory_failed=0;
  261: 	    my $ungraded=0;
  262: 	    my $review=0;   
  263: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  264: 	    foreach my $dim_id (@Apache::bridgetask::dimensionlist) {
  265: 		my $status=
  266: 		    $Apache::lonhomework::results{"resource.$dim_id.status"};
  267: 		my $mandatory=
  268: 		    ($Apache::bridgetask::dimensionmandatory{$dim_id} ne 'N');
  269: 		if ($status eq 'pass') {
  270: 		    if (!$mandatory) { $optional_passed++; }
  271: 		} elsif ($status eq 'fail') {
  272: 		    if ($mandatory) { $mandatory_failed++; }
  273: 		} elsif ($status eq 'ungraded') {
  274: 		    $ungraded++;
  275: 		} elsif ($status eq 'review') {
  276: 		    $review++;
  277: 		}	
  278: 	    }
  279: 	    if ($optional_passed < $optional_required) {
  280: 		$mandatory_failed++;
  281: 	    }
  282: 	    &Apache::lonxml::debug("all dim ".join(':',@Apache::bridgetask::dimensionlist)."results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
  283: 	    $Apache::lonhomework::results{'resource.grader'}=
  284: 		$env{'user.name'}.'@'.$env{'user.domain'};
  285: 	    if ($review) {
  286: 		$Apache::lonhomework::results{'resource.status'}='review';
  287: 		&move_to_review_queue();
  288: 	    } elsif ($ungraded) {
  289: 		$Apache::lonhomework::results{'resource.status'}='ungraded';
  290: 		&check_queue_unlock();
  291: 	    } elsif ($mandatory_failed) {
  292: 		$Apache::lonhomework::results{'resource.status'}='fail';
  293: 		$Apache::lonhomework::results{"resource.0.award"}='EXACT_ANS';
  294: 		&remove_from_queue();
  295: 	    } else {
  296: 		$Apache::lonhomework::results{'resource.status'}='pass';
  297: 		$Apache::lonhomework::results{"resource.0.award"}='INCORRECT';
  298: 		&remove_from_queue();
  299: 	    }
  300: 	    &Apache::structuretags::finalize_storage();
  301: 	}
  302:     } elsif ($target eq 'webgrade') {
  303: 	$result.="</table>\n<hr />";
  304: 	#$result.='<input type="submit" name="next" value="'.
  305: 	#    &mt('Save &amp; Next').'" /> ';
  306: 	#$result.='<input type="submit" name="end" value="'.
  307: 	#    &mt('Save &amp; Stop Grading').'" /> ';
  308: 	#$result.='<input type="submit" name="throwaway" value="'.
  309: 	#    &mt('Throw Away &amp; Stop Grading').'" /> ';
  310: 	#$result.='<input type="submit" name="save" value="'.
  311: 	#    &mt('Save Partial Grade and Continue Grading').'" /> ';
  312: 	$result.='</form>'.&Apache::loncommon::endbodytag().'</html>';
  313:     } elsif ($target eq 'meta') {
  314: 	$result.='<parameter part="0" package="Task"></parameter>'."\n";
  315: 	#$result.=&Apache::response::meta_part_order();
  316: 	#$result.=&Apache::response::meta_response_order();
  317:     }
  318:     undef($Apache::lonhomework::parsing_a_task);
  319:     return $result;
  320: }
  321: 
  322: sub move_to_review_queue {
  323:     &Apache::lonxml::debug("Want to move");
  324: }
  325: 
  326: sub check_queue_unlock {
  327:     &Apache::lonxml::debug("Want to unlock?");
  328: }
  329: 
  330: sub remove_from_queue {
  331:     &Apache::lonxml::debug("Want to remove. Done?");
  332: }
  333: 
  334: sub setup_env_for_other_user {
  335:     my ($queue_key,$safeeval)=@_;
  336:     my ($symb,$uname,$udom)=&decode_queue_key($queue_key);
  337:     $env{'form.grade_symb'}=$symb;
  338:     $env{'form.grade_domain'}=$udom;
  339:     $env{'form.grade_username'}=$uname;
  340:     $env{'form.grade_courseid'}=$env{'request.course.id'};
  341:     &Apache::lonxml::initialize_rndseed($safeeval);
  342: }
  343: 
  344: sub add_to_queue {
  345:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
  346:     my $cnum=$env{'course.'.$cid.'.num'};
  347:     my $cdom=$env{'course.'.$cid.'.domain'};
  348:     my %data;
  349:     $data{"$symb\0queue\0$uname\@$udom"}=[$Apache::inputtags::slot_name];
  350:     &Apache::lonnet::put('gradingqueue',\%data,$cdom,$cnum);
  351: }
  352: 
  353: sub show_queue {
  354:     my $result;
  355:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
  356:     my $cnum=$env{'course.'.$cid.'.num'};
  357:     my $cdom=$env{'course.'.$cid.'.domain'};
  358:     my $regexp="^$symb\0";
  359:     my %queue=&Apache::lonnet::dump('gradingqueue',$cdom,$cnum,$regexp);
  360:     $result.="\n<h3>Current Queue</h3><table><tr><th>resource</th>".
  361: 	"<th>user</th><th>type</th><th>data</th></tr>";
  362:     foreach my $key (sort(keys(%queue))) {
  363: 	if ($key=~/locked$/) {
  364: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
  365: 	    my $title=&Apache::lonnet::gettitle($symb);
  366: 	    $result.="<tr><td>$title</td><td>$uname</td><td>";
  367: 	    $result.='<td>lock</td><td>'.$queue{$key}.'</td></tr>';
  368: 	} elsif ($key=~/timestamp$/) {
  369: 	    my ($symb,undef) = split("\0",$key);
  370: 	    my $title=&Apache::lonnet::gettitle($symb);
  371: 	    $result.="<tr><td>$title</td><td></td><td>";
  372: 	    $result.='<td>last queue modification time</td><td>'.
  373: 		&Apache::lonlocal::locallocaltime($queue{$key})."</td></tr>";
  374: 	} else {
  375: 	    my ($symb,$uname,$udom) = &decode_queue_key($key);
  376: 	    my $title=&Apache::lonnet::gettitle($symb);
  377: 	    $result.="<tr><td>$title</td><td>$uname</td><td>";
  378: 	    my $slot=$queue{$key}->[0];
  379: 	    my %slot_data=&Apache::lonnet::get_slot($slot);
  380: 	    $result.='<td>queue entry</td><td>End time: '.
  381: 		&Apache::lonlocal::locallocaltime($slot_data{'endtime'}).
  382: 		"</td></tr>";
  383: 	}
  384:     }
  385:     $result.="</table><hr />\n";
  386:     return $result;
  387: }
  388: 
  389: sub decode_queue_key {
  390:     my ($key)=@_;
  391:     my ($symb,undef,$user) = split("\0",$key);
  392:     my ($uname,$udom) = split('@',$user);
  393:     return ($symb,$uname,$udom);
  394: }
  395: 
  396: sub queue_key_locked {
  397:     my ($key,$cdom,$cnum)=@_;
  398:     my ($key_locked,$value)=
  399: 	&Apache::lonnet::get('gradingqueue',["$key\0locked"],$cdom,$cnum);
  400:     if ($key_locked eq "$key\0locked") {
  401: 	return $value;
  402:     }
  403:     return undef;
  404: }
  405: 
  406: sub pick_from_queue_data {
  407:     my ($check_section,$queue,$cdom,$cnum)=@_;
  408:     foreach my $key (keys(%$queue)) {
  409: 	my ($symb,$uname,$udom)=&decode_queue_key($key);
  410: 	if ($check_section) {
  411: 	    my $section=&Apache::lonnet::getsection($uname,$udom);
  412: 	    if ($section eq $check_section) {
  413: 		&Apache::lonnet::logthis("my sec");
  414: 		next;
  415: 	    }
  416: 	}
  417: 	my $slot=$queue->{$key}[0];
  418: 	my %slot_data=&Apache::lonnet::get_slot($slot);
  419: 	if ($slot_data{'endtime'} > time) { 
  420: 	    &Apache::lonnet::logthis("not time");
  421: 	    next;
  422: 	}
  423: 	if (&queue_key_locked($key,$cdom,$cnum)) {
  424: 	    &Apache::lonnet::logthis("someone already has um.");
  425: 	    next;
  426: 	}
  427: 	return $key;
  428:     }
  429:     return undef;
  430: }
  431: 
  432: sub find_mid_grade {
  433:     my ($symb,$cdom,$cnum)=@_;
  434:     my $todo=&Apache::lonnet::unescape($env{'form.gradingkey'});
  435:     my $me=$env{'user.name'}.'@'.$env{'user.domain'};
  436:     if ($todo) {
  437: 	my $who=&queue_key_locked($todo,$cdom,$cnum);
  438: 	if ($who eq $me) { return $todo; }
  439:     }
  440:     my $regexp="^$symb\0.*\0locked\$";
  441:     my %locks=&Apache::lonnet::dump('gradingqueue',$cdom,$cnum,$regexp);
  442:     foreach my $key (keys(%locks)) {
  443: 	my $who=$locks{$key};
  444: 	if ($who eq $me) {
  445: 	    $todo=$key;
  446: 	    $todo=~s/\0locked$//;
  447: 	    return $todo;
  448: 	}
  449:     }
  450:     return undef;
  451: }
  452: 
  453: sub get_from_queue {
  454:     my $result;
  455:     my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
  456:     my $cnum=$env{'course.'.$cid.'.num'};
  457:     my $cdom=$env{'course.'.$cid.'.domain'};
  458:     my $todo;
  459:     # FIXME need to find if I am 'mid grading'
  460:     $todo=&find_mid_grade($symb,$cdom,$cnum);
  461:     &Apache::lonnet::logthis("found ".join(':',&decode_queue_key($todo)));
  462:     if ($todo) { return $todo; }
  463:     while (1) {
  464: 	my $starttime=time;
  465: 	&Apache::lonnet::put('gradingqueue',{"$symb\0timestamp"=>$starttime},
  466: 			     $cdom,$cnum);
  467: 	&Apache::lonnet::logthis("$starttime");
  468: 	my $regexp="^$symb\0queue\0";
  469: 	my %queue=&Apache::lonnet::dump('gradingqueue',$cdom,$cnum,$regexp);
  470: 	#make a pass looking for a user in my section
  471: 	if ($env{'request.course.sec'}) {
  472: 	    &Apache::lonnet::logthis("sce");
  473: 	    $todo=&pick_from_queue_data($env{'request.course.sec'},\%queue,
  474: 					$cdom,$cnum);
  475: 	    &Apache::lonnet::logthis("sce $todo");
  476: 	}
  477: 	# no one in our section so look for any user that is ready for grading
  478: 	if (!$todo) {
  479: 	    &Apache::lonnet::logthis("no sce");
  480: 	    $todo=&pick_from_queue_data($env{'request.course.sec'},\%queue,
  481: 					$cdom,$cnum);
  482: 	    &Apache::lonnet::logthis("no sce $todo");
  483: 	}
  484: 	# no user to grade 
  485: 	if (!$todo) { last; }
  486: 	&Apache::lonnet::logthis("got $todo");
  487: 	# otherwise found someone so lets try to lock them
  488: 	my $success=&Apache::lonnet::newput('gradingqueue',
  489: 					    {"$todo\0locked"=>
  490: 						 $env{'user.name'}.'@'.$env{'user.domain'}},
  491: 					   $cdom,$cnum);
  492: 	# someone else already picked them
  493: 	&Apache::lonnet::logthis("success $todo");
  494: 	if ($success ne 'ok') { next; }
  495: 	my (undef,$endtime)=
  496: 	    &Apache::lonnet::get('gradingqueue',["$symb\0timestamp"],
  497: 				 $cdom,$cnum);
  498: 	&Apache::lonnet::logthis("emd  $endtime");
  499: 	# someone else already modified the queue, 
  500: 	# perhaps our picked user wass already fully graded between
  501: 	# when we picked him and when we locked his record? so lets
  502: 	# double check.
  503: 	if ($endtime != $starttime) {
  504: 	    my ($key,$value)=
  505: 		&Apache::lonnet::get('gradingqueue',["$todo"],
  506: 				     $cdom,$cnum);
  507: 	    &Apache::lonnet::logthis("check  $key .. $value");
  508: 	    if ($key eq $todo && ref($value)) {
  509: 	    } else {
  510: 		&Apache::lonnet::del('gradingqueue',["$todo\0locked"],
  511: 				     $cdom,$cnum);
  512: 		&Apache::lonnet::logthis("del");
  513: 		next;
  514: 	    }
  515: 	}
  516: 	&Apache::lonnet::logthis("last $todo");
  517: 	last;
  518:     }
  519:     return $todo;
  520: }
  521: 
  522: sub start_ClosingParagraph {
  523:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  524:     my $result;
  525:     if ($target eq 'web') {
  526:     } elsif ($target eq 'webgrade') {
  527: 	&Apache::lonxml::startredirection();
  528:     }
  529:     return $result;
  530: }
  531: 
  532: sub end_ClosingParagraph {
  533:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  534:     my $result;
  535:     if ($target eq 'web') {
  536:     } elsif ($target eq 'webgrade') {
  537: 	&Apache::lonxml::endredirection();
  538:     }
  539:     return $result;
  540: }
  541: 
  542: sub get_id {
  543:     my ($parstack,$safeeval)=@_;
  544:     my $id=&Apache::lonxml::get_param('id',$parstack,$safeeval);
  545:     if (!$id) { $id=$Apache::lonxml::curdepth; }
  546:     return $id;
  547: }
  548: 
  549: my %dimension;
  550: sub start_Dimension {
  551:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  552:     undef(%dimension);
  553:     my $dim_id=&get_id($parstack,$safeeval);
  554:     $Apache::bridgetask::dimension=$dim_id;
  555:     push(@Apache::bridgetask::dimensionlist,$dim_id);
  556:     undef(@Apache::bridgetask::instance);
  557:     $Apache::bridgetask::dimensionmandatory{$dim_id}=
  558: 	&Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
  559:     return '';
  560: }
  561: 
  562: sub get_instance {
  563:     #FIXME just grabbing the first one for now, need
  564:     #to randomly pick one until all have been seen
  565:     #then start repicking
  566:     &Apache::response::pushrandomnumber();
  567:     my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
  568:     return $order[0];
  569: }
  570: 
  571: {
  572:     my $last_link;
  573:     sub end_Dimension {
  574: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  575: 	my $result;
  576: 	my $dim_id=&get_id($parstack,$safeeval);
  577: 	my $instance=&get_instance();
  578: 	if ($target eq 'web') {
  579: 	    $result=$dimension{'intro'}.$dimension{$instance.'.text'};
  580: 	} elsif ($target eq 'webgrade') {
  581: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
  582: 		my $link='criteria_'.$instance.'_'.$id;
  583: 		my $status=$Apache::lonhomework::history{'resource.'.$instance.'.'.$id.'.status'};
  584: 		$result.='<tr><td width="100%" valign="top">'.
  585: 		    '<a name="'.$link.'" />'.
  586: 		    '<a name="next_'.$last_link.'" />'.
  587: 		    '<br /><textarea enabled="false" style="width:100%" rows="8" width="25" wrap="hard">'.$dimension{$instance.'.criteria.'.$id}.'</textarea>'.
  588: 		    #$dimension{$instance.'.criteria.'.$id}.
  589: 		    '</td>'.
  590: 		    '<td><nobr>Additional Comment for Student</nobr> <br />'.
  591: 		    '<textarea style="width:100%" rows="8" width="25" wrap="hard" name="HWVAL_comment_'.$link.'">'.&HTML::Entities::encode($Apache::lonhomework::history{'resource.'.$instance.'.'.$id.'.comment'}).'</textarea>'.
  592: 		    '</td>'.
  593: 		    '<td>'.
  594: 		    '<nobr><label><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').'/>Ungraded</label></nobr><br />'.
  595: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="pass" '.($status eq 'pass' ? 'checked="checked"':'').' />Pass</label><br />'.
  596: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />Fail</label><br />'.
  597: 		    '<label><input type="radio" name="HWVAL_'.$link.'" value="review" '.($status eq 'review' ? 'checked="checked"':'').' />Review</label><br />'.
  598: 		    '</td>'.
  599: 		    '<td>'.
  600: 		    '<a href="#'.$last_link.'">Prev</a><br />'.
  601: 		    '<a href="#next_'.$link.'">Next</a><br /><br /><br />'.
  602: 		    '<input type="submit" name="next" value="'.
  603: 		    &mt('Done').'" /> '.
  604: 		    '</td></tr>';
  605: 		$last_link=$link;
  606: 	    }
  607: 	} elsif ($target eq 'grade') {
  608: 	    my $optional_passed=0;
  609: 	    my $mandatory_failed=0;
  610: 	    my $ungraded=0;
  611: 	    my $review=0;
  612: 	    foreach my $id (@{$dimension{$instance.'.criterias'}}) {
  613: 		my $status=$Apache::lonhomework::results{'resource.'.$instance.'.'.$id.'.status'}=$env{'form.HWVAL_criteria_'.$instance.'_'.$id};
  614: 		$Apache::lonhomework::results{'resource.'.$instance.'.'.$id.'.comment'}=$env{'form.HWVAL_comment_criteria_'.$instance.'_'.$id};
  615: 		my $mandatory=($dimension{$instance.'.criteria.'.$id.'.mandatory'} ne 'N');
  616: 		if ($status eq 'pass') {
  617: 		    if (!$mandatory) { $optional_passed++; }
  618: 		} elsif ($status eq 'fail') {
  619: 		    if ($mandatory) { $mandatory_failed++; }
  620: 		} elsif ($status eq 'review') {
  621: 		    $review++;
  622: 		} elsif ($status eq 'ungraded') {
  623: 		    $ungraded++;
  624: 		} else {
  625: 		    &Apache::lonxml::error("got wierd status --$status--");
  626: 		}
  627: 	    }
  628: 	    if ($optional_passed < $dimension{$instance.'.optionalrequired'}) {
  629: 		$mandatory_failed++;
  630: 	    }
  631: 	    &Apache::lonxml::debug("all instance ".join(':',@{$dimension{$instance.'.criterias'}})." results -> m_f $mandatory_failed o_p $optional_passed u $ungraded r $review");
  632: 	    if ($review) {
  633: 		$Apache::lonhomework::results{'resource.'.$dim_id.'.status'}='review';
  634: 	    } elsif ($ungraded) {
  635: 		$Apache::lonhomework::results{'resource.'.$dim_id.'.status'}='ungraded';
  636: 	    } elsif ($mandatory_failed) {
  637: 		$Apache::lonhomework::results{'resource.'.$dim_id.'.status'}='fail';
  638: 	    } else {
  639: 		$Apache::lonhomework::results{'resource.'.$dim_id.'.status'}='pass';
  640: 	    }
  641: 	}
  642: 	return $result;
  643:     }
  644: }
  645: 
  646: sub start_IntroParagraph {
  647:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  648:     my $result;
  649:     if ($target eq 'web' || $target eq 'webgrade') {
  650: 	if ($tagstack->[-2] eq 'Dimension' || $target eq 'webgrade') {
  651: 	    &Apache::lonxml::startredirection();
  652: 	}
  653:     }
  654:     return $result;
  655: }
  656: 
  657: sub end_IntroParagraph {
  658:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  659:     my $result;
  660:     if ($target eq 'web' || $target eq 'webgrade') {
  661: 	if ($tagstack->[-2] eq 'Dimension' || $target eq 'webgrade') {
  662: 	    $dimension{'intro'}=&Apache::lonxml::endredirection();
  663: 	}
  664:     }
  665:     return $result;
  666: }
  667: 
  668: sub start_Instance {
  669:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  670:     my $id=&get_id($parstack,$safeeval);
  671:     push(@{$dimension{'instances'}},$id);
  672:     push(@Apache::bridgetask::instance,$id);
  673:     push(@Apache::bridgetask::instancelist,$id);
  674:     $dimension{$id.'.optionalrequired'}=
  675: 	&Apache::lonxml::get_param('OptionalRequired',$parstack,$safeeval);
  676:     return '';
  677: }
  678: 
  679: sub end_Instance {
  680:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  681:     return '';
  682: }
  683: 
  684: sub start_InstanceText {
  685:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  686:     if ($target eq 'web' || $target eq 'webgrade') {
  687: 	&Apache::lonxml::startredirection();
  688:     }
  689:     return '';
  690: }
  691: 
  692: sub end_InstanceText {
  693:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  694:     my $instance_id=$Apache::bridgetask::instance[-1];
  695:     if ($target eq 'web' || $target eq 'webgrade') {
  696: 	$dimension{$instance_id.'.text'}=&Apache::lonxml::endredirection();
  697:     }
  698:     return '';
  699: }
  700: 
  701: sub start_Criteria {
  702:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  703:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
  704: 	&Apache::lonxml::startredirection();
  705:     }
  706:     return '';
  707: }
  708: 
  709: sub end_Criteria {
  710:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  711:     my $instance_id=$Apache::bridgetask::instance[-1];
  712:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'grade') {
  713: 	my $criteria=&Apache::lonxml::endredirection();
  714: 	my $id=&get_id($parstack,$safeeval);
  715: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
  716: 	$dimension{$instance_id.'.criteria.'.$id.'.mandatory'}=
  717: 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
  718: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
  719:     }
  720:     return '';
  721: }
  722: 
  723: sub proctor_validation_screen {
  724:     my ($slot) = @_;
  725:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
  726:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
  727:     $user=$env{'form.proctorname'};
  728:     if ($env{'form.proctordomain'}) { $domain=$env{'form.proctordomain'}; }
  729:     my $msg;
  730:     if ($env{'form.proctorpassword'}) {
  731: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
  732: 	    .'</font></p>';
  733:     }
  734:     my $result= (<<ENDCHECKOUT);
  735: <h2>Proctor Validation</h2>
  736:     <p>Your room's proctor needs to validate your access to this resource.</p>
  737:     $msg
  738: <form name="checkout" method="POST" action="$env{'request.uri'}">
  739: <input type="hidden" name="validate" value="yes" />
  740: <input type="hidden" name="submitted" value="yes" />
  741: <table>
  742:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$user" /></td></tr>
  743:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
  744:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$domain" /></td></tr>
  745: </table>
  746: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
  747: Student who should be logged in is:<br />
  748: <img src="$url" /><br />
  749: </form>
  750: ENDCHECKOUT
  751:     return $result;
  752: }
  753: 
  754: 1;
  755: __END__

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