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

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

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