File:  [LON-CAPA] / loncom / homework / bridgetask.pm
Revision 1.24: download - view: text, annotated - select for diffs
Fri May 13 15:34:50 2005 UTC (19 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- err whoops

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

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