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

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

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