Annotation of loncom/homework/bridgetask.pm, revision 1.14
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # definition of tags that give a structure to a document
3: #
1.14 ! albertel 4: # $Id: bridgetask.pm,v 1.13 2005/04/29 21:22:33 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=();
51: # list of all current Instance ids
52: @Apache::bridgetask::instance=();
53: # list of all Instance ids seen in this problem
54: @Apache::bridgetask::instancelist=();
55: }
56:
1.4 albertel 57: sub proctor_check_auth {
58: my ($slot)=@_;
1.11 albertel 59: my $user=$env{'form.proctorname'};
60: my $domain=$env{'form.proctordomain'};
1.4 albertel 61:
62: my @allowed=split(",",$slot->{'proctor'});
63: foreach my $possible (@allowed) {
64: my ($puser,$pdom)=(split('@',$possible));
65: if ($puser eq $user && $pdom eq $domain) {
1.11 albertel 66: my $authhost=&Apache::lonnet::authenticate($puser,$env{'form.proctorpassword'},$pdom);
1.4 albertel 67: if ($authhost ne 'no_host') {
68: $Apache::lonhomework::results{'resource.checkedin'}=
69: $user.'@'.$domain;
70: return 1;
71: }
72: }
73: }
74: return 0;
75: }
76:
1.8 albertel 77: sub add_previous_version_button {
78: my $result;
1.9 albertel 79: $result.=&mt(' Show a previously done version: [_1]','<select name="previousversion">
1.8 albertel 80: <option>Pick one</option>
1.9 albertel 81: </select>');
1.8 albertel 82: return $result;
83: }
84:
1.13 albertel 85: sub add_grading_button {
86: my $result;
87: $result.=' <input type="submit" name="gradeasubmission" value="'.
88: &mt("Get a submission to grade").'" />';
89: $result.='<input type="hidden" name="grade_target" value="webgrade" />';
90: return $result;
91: }
92:
1.1 albertel 93: sub start_Task {
94: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
95:
1.4 albertel 96: &Apache::structuretags::initialize_storage();
97: &Apache::lonhomework::showhash(%Apache::lonhomework::history);
98: my ($status,$accessmsg,$slot);
99: $Apache::lonhomework::parsing_a_task=1;
1.1 albertel 100: #should get back a <html> or the neccesary stuff to start XML/MathML
101: my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
102: &Apache::structuretags::page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
103:
1.8 albertel 104: if ($target eq 'web') {
105: $body_tag_start.=&add_previous_version_button();
1.13 albertel 106: if ($Apache::lonhomework::modifygrades) {
107: $body_tag_start.='<form name="gradesubmission" method="POST" action="';
108: my $uri=$env{'request.uri'};
109: if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
110: $body_tag_start.=$uri.'">'.&add_grading_button()."</form>";
111: }
1.8 albertel 112: }
1.1 albertel 113: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
114: $target eq 'tex') {
1.14 ! albertel 115: ($status,$accessmsg,my $slot_name,$slot) =
1.4 albertel 116: &Apache::lonhomework::check_task_access('0');
1.9 albertel 117: push(@Apache::inputtags::status,$status);
1.14 ! albertel 118: $Apache::inputtags::slot_name=$slot_name;
1.1 albertel 119: my $expression='$external::datestatus="'.$status.'";';
120: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
121: &Apache::run::run($expression,$safeeval);
122: &Apache::lonxml::debug("Got $status");
123: if (( $status eq 'CLOSED' ) ||
124: ( $status eq 'BANNED') ||
125: ( $status eq 'UNAVAILABLE') ||
1.3 albertel 126: ( $status eq 'NOT_IN_A_SLOT') ||
1.4 albertel 127: ( $status eq 'NEEDS_CHECKIN') ||
1.1 albertel 128: ( $status eq 'INVALID_ACCESS')) {
129: my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
130: if ( $target eq "web" ) {
1.4 albertel 131: $result.= $head_tag_start.'</head>'.$body_tag_start;
132: my $msg;
1.1 albertel 133: if ($status eq 'UNAVAILABLE') {
134: $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.3 albertel 135: } elsif ($status eq 'NOT_IN_A_SLOT') {
136: $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.4 albertel 137: } elsif ($status eq 'NEEDS_CHECKIN') {
138: $msg.='<h1>'.&mt('You need the Proctor to validate you.').
139: '</h1>'.&proctor_validation_screen($slot);
1.1 albertel 140: } elsif ($status ne 'NOT_YET_VIEWED') {
141: $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
142: }
143: if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
144: $msg.='The problem '.$accessmsg;
145: }
146: $result.=$msg.'<br />';
147: } elsif ($target eq 'tex') {
148: $result.='\begin{document}\noindent \vskip 1 mm \begin{minipage}{\textwidth}\vskip 0 mm';
149: if ($status eq 'UNAVAILABLE') {
150: $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
151: } else {
152: $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
153: }
1.4 albertel 154: } elsif ($target eq 'grade') {
155: if ($status eq 'NEEDS_CHECKIN') {
156: if (&proctor_check_auth($slot)) {
157: #FIXME immeadiatly add this to the grading queue
158: # with slot->{'endtime'} for when grading can
159: # begin on this resource
160: }
161: }
1.1 albertel 162: }
163: } elsif ($target eq 'web') {
164: my $name= &Apache::structuretags::get_resource_name($parstack,$safeeval);
165: $result.="$head_tag_start<title>$name</title></head>
166: $body_tag_start \n $form_tag_start".
167: '<input type="hidden" name="submitted" value="yes" />';
168: # if we are viewing someone else preserve that info
1.11 albertel 169: if (defined $env{'form.grade_symb'}) {
1.1 albertel 170: foreach my $field ('symb','courseid','domain','username') {
171: $result .= '<input type="hidden" name="grade_'.$field.
1.11 albertel 172: '" value="'.$env{"form.grade_$field"}.'" />'."\n";
1.1 albertel 173: }
174: }
175: }
1.13 albertel 176: } elsif ($target eq 'webgrade') {
177: $result.=$head_tag_start.$body_tag_start.$form_tag_start.
178: 'Yahoo!';
1.14 ! albertel 179: $result.=&show_queue();
! 180: $result.=&get_from_queue();
! 181:
1.1 albertel 182: } else {
183: # page_start returned a starting result, delete it if we don't need it
184: $result = '';
185: }
186: return $result;
187: }
188:
189: sub end_Task {
190: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
191: my $result='';
192: my $status=$Apache::inputtags::status['-1'];
193: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.13 albertel 194: $target eq 'tex' || $target eq 'webgrade') {
1.1 albertel 195: if (
1.11 albertel 196: (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.1 albertel 197: ($target eq 'answer') || ($target eq 'tex')
198: ) {
199: if ($target eq 'web') {
1.9 albertel 200: if ($status eq 'CAN_ANSWER') {
201: $result.='<table border="1">'.
202: &Apache::inputtags::file_selector('0',"bridgetask","*",
203: 'portfolioonly').
204: "</table>";
205: $result.=&Apache::inputtags::gradestatus('0');
206: }
1.13 albertel 207: }
208: if ($target eq 'web' || $target eq 'webgrade') {
1.1 albertel 209: $result.=&Apache::lonxml::xmlend().'</html>';
210: }
211: }
212: if ($target eq 'grade') {
1.12 albertel 213: my $award='SUBMITTED';
1.14 ! albertel 214: &Apache::essayresponse::file_submission('0','bridgetask','portfiles',\$award);
! 215: if ($award eq 'SUBMITTED' &&
! 216: $Apache::lonhomework::results{"resource.0.bridgetask.portfiles"}) {
1.10 albertel 217: $Apache::lonhomework::results{"resource.0.tries"}=
218: 1+$Apache::lonhomework::history{"resource.0.tries"};
219: }
1.14 ! albertel 220: $Apache::lonhomework::results{"resource.0.award"}=$award;
1.4 albertel 221: &Apache::lonhomework::showhash(%Apache::lonhomework::results);
222: &Apache::structuretags::finalize_storage();
1.14 ! albertel 223: if ($award eq 'SUBMITTED') {
! 224: &add_to_queue();
! 225: }
1.1 albertel 226: }
227: } elsif ($target eq 'meta') {
1.2 albertel 228: $result.='<parameter part="0" package="Task"></parameter>'."\n";
1.1 albertel 229: #$result.=&Apache::response::meta_part_order();
230: #$result.=&Apache::response::meta_response_order();
231: }
1.4 albertel 232: undef($Apache::lonhomework::parsing_a_task);
1.1 albertel 233: return $result;
234: }
235:
1.14 ! albertel 236: sub add_to_queue {
! 237: my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
! 238: my $cnum=$env{'course.'.$cid.'.num'};
! 239: my $cdom=$env{'course.'.$cid.'.domain'};
! 240: my %data;
! 241: #$data{"$symb\0timestamp"}=time;
! 242: $data{"$symb\0queue\0$uname\@$udom"}=[$Apache::inputtags::slot_name];
! 243: &Apache::lonnet::put('gradingqueue',\%data,$cdom,$cnum);
! 244: }
! 245:
! 246: sub show_queue {
! 247: my $result;
! 248: my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
! 249: my $cnum=$env{'course.'.$cid.'.num'};
! 250: my $cdom=$env{'course.'.$cid.'.domain'};
! 251: my $regexp="^$symb\0queue";
! 252: my %queue=&Apache::lonnet::dump('gradingqueue',$cdom,$cnum,$regexp);
! 253: $result.="<table><tr><th>resource</th><th>user</th><th>Time available for grading</th></tr>";
! 254: foreach my $key (sort(keys(%queue))) {
! 255: my ($symb,undef,$user) = split("\0",$key);
! 256: my ($uname,$udom) = split('@',$user);
! 257: my $title=&Apache::lonnet::gettitle($symb);
! 258: $result.="<tr><td>$title</td><td>$uname</td><td>";
! 259: my $slot=$queue{$key}->[0];
! 260: my %slot_data=&Apache::lonnet::get_slot($slot);
! 261: $result.="End time: ".&Apache::lonlocal::locallocaltime($slot_data{'endtime'})."</td></tr>";
! 262: }
! 263: $result.="</table>";
! 264: return $result;
! 265: }
! 266:
! 267: sub decode_queue_key {
! 268: my ($key)=@_;
! 269: my ($symb,undef,$user) = split("\0",$key);
! 270: my ($uname,$udom) = split('@',$user);
! 271: return ($symb,$uname,$udom);
! 272: }
! 273:
! 274: sub queue_key_locked {
! 275: my ($key)=@_;
! 276: my ($key_locked,$value)=
! 277: &Apache::lonnet::get('gradingqueue',["$key\0locked"],$cdom,$cnum);
! 278: if ($key_locked eq "$key\0locked") {
! 279: return $value;
! 280: }
! 281: return undef;
! 282: }
! 283:
! 284: sub pick_from_queue_data {
! 285: my ($check_section,$queue)=@_;
! 286: foreach my $key (sort(keys(%$queue))) {
! 287: my ($symb,$uname,$udom)=&decode_queue_key($key);
! 288: if ($check_section) {
! 289: my $section=&Apache::lonnet::getsection($uname,$udom);
! 290: if ($section ne $check_section) { next; }
! 291: }
! 292: my $slot=$queue->{$key}[0];
! 293: my %slot_data=&Apache::lonnet::get_slot($slot);
! 294: if ($slot_data{'endtime'} > time) { next; }
! 295: if (&queue_key_locked($key)) { next; }
! 296: return $key;
! 297: }
! 298: return undef;
! 299: }
! 300:
! 301: sub get_from_queue {
! 302: my $result;
! 303: my ($symb,$cid,$udom,$uname)=&Apache::lonxml::whichuser();
! 304: my $cnum=$env{'course.'.$cid.'.num'};
! 305: my $cdom=$env{'course.'.$cid.'.domain'};
! 306: my $todo;
! 307: while (1) {
! 308: my $starttime=time;
! 309: &Apache::lonnet::put('gradingqueue',{"$symb\0timestamp"=>$starttime},
! 310: $cdom,$cnum);
! 311: my $regexp="^$symb\0queue\0";
! 312: my %queue=&Apache::lonnet::dump('gradingqueue',$cdom,$cnum,$regexp);
! 313: #make a pass looking for a user in my section
! 314: if ($env{'request.course.sec'}) {
! 315: $todo=&pick_from_queue_data($env{'request.course.sec'},\%queue);
! 316: }
! 317: # no one in our section so look for any user that is ready for grading
! 318: if (!$todo) {
! 319: $todo=&pick_from_queue_data($env{'request.course.sec'},\%queue);
! 320: }
! 321: # no user to grade
! 322: if (!$todo) { last; }
! 323: # otherwise found someone so lets try to lock them
! 324: my $success=&Apache::lonnet::newput('gradingqueue',
! 325: {"$todo\0locked"=>
! 326: $env{'user.name'}.'@'.$env{'user.domain'}},
! 327: $cdom,$cnum);
! 328: # someone else already picked them
! 329: if ($success ne 'ok') { next; }
! 330: my (undef,$endtime)=
! 331: &Apache::lonnet::get('gradingqueue',["$symb\0timestamp"],
! 332: $cdom,$cnum);
! 333: # someone else already modified the queue,
! 334: # perhaps our picked user wass already fully graded between
! 335: # when we picked him and when we locked his record? so lets
! 336: # double check.
! 337: if ($endtime != $starttime) {
! 338: my ($key,$value)=
! 339: &Apache::lonnet::get('gradingqueue',["$todo"],
! 340: $cdom,$cnum);
! 341: if ($key eq $todo && ref($value)) {
! 342: } else {
! 343: &Apache::lonnet::del('gradingqueue',["$todo\0locked"],
! 344: $cdom,$cnum);
! 345: next;
! 346: }
! 347: }
! 348: last;
! 349: }
! 350: return $todo;
! 351: }
! 352:
1.1 albertel 353: sub start_ClosingParagraph {
354: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
355: my $result;
356: if ($target eq 'web') {
1.13 albertel 357: } elsif ($target eq 'webgrade') {
358: &Apache::lonxml::startredirection();
1.1 albertel 359: }
360: return $result;
361: }
362:
363: sub end_ClosingParagraph {
364: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
365: my $result;
366: if ($target eq 'web') {
1.13 albertel 367: } elsif ($target eq 'webgrade') {
368: &Apache::lonxml::endredirection();
1.1 albertel 369: }
370: return $result;
371: }
372:
373: my %dimension;
374: sub start_Dimension {
375: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
376: undef(%dimension);
1.9 albertel 377: my $dim_id=$Apache::lonxml::curdepth;
378: $Apache::bridgetask::dimension=$dim_id;
379: push(@Apache::bridgetask::dimensionlist,$dim_id);
380: undef(@Apache::bridgetask::instance);
1.1 albertel 381: return '';
382: }
383:
1.13 albertel 384: sub get_instance {
385: #FIXME just grabbing the first one for now, need
386: #to randomly pick one until all have been seen
387: #then start repicking
388: &Apache::response::pushrandomnumber();
389: my @order=&Math::Random::random_permutation(@{$dimension{'instances'}});
390: return $order[0];
391: }
392:
1.1 albertel 393: sub end_Dimension {
394: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
395: my $result;
396: if ($target eq 'web') {
1.13 albertel 397: my $instance=&get_instance();
1.9 albertel 398: $result=$dimension{'intro'}.$dimension{$instance.'.text'};
1.13 albertel 399: } elsif ($target eq 'webgrade') {
400: my $instance=&get_instance();
401: $result.='<table>';
402: foreach my $id (@{$dimension{$instance.'.criterias'}}) {
403: $result.='<tr><td>'.
404: $dimension{$instance.'.criteria.'.$id}.'</td></tr>';
405: }
406: $result.='</table>';
1.1 albertel 407: }
408: return $result;
409: }
410:
411: sub start_IntroParagraph {
412: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
413: my $result;
1.13 albertel 414: if ($target eq 'web' || $target eq 'webgrade') {
415: if ($tagstack->[-2] eq 'Dimension' || $target eq 'webgrade') {
1.1 albertel 416: &Apache::lonxml::startredirection();
417: }
418: }
419: return $result;
420: }
421:
422: sub end_IntroParagraph {
423: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
424: my $result;
1.13 albertel 425: if ($target eq 'web' || $target eq 'webgrade') {
426: if ($tagstack->[-2] eq 'Dimension' || $target eq 'webgrade') {
1.1 albertel 427: $dimension{'intro'}=&Apache::lonxml::endredirection();
428: }
429: }
430: return $result;
431: }
432:
433: sub start_Instance {
434: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
435: push(@{$dimension{'instances'}},$Apache::lonxml::curdepth);
1.9 albertel 436: push(@Apache::bridgetask::instance,$Apache::lonxml::curdepth);
437: push(@Apache::bridgetask::instancelist,$Apache::lonxml::curdepth);
1.1 albertel 438: return '';
439: }
440:
441: sub end_Instance {
442: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
443: return '';
444: }
445:
446: sub start_InstanceText {
447: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.13 albertel 448: if ($target eq 'web' || $target eq 'webgrade') {
1.1 albertel 449: &Apache::lonxml::startredirection();
450: }
451: return '';
452: }
453:
454: sub end_InstanceText {
455: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9 albertel 456: my $instance_id=$Apache::bridgetask::instance[-1];
1.13 albertel 457: if ($target eq 'web' || $target eq 'webgrade') {
1.1 albertel 458: $dimension{$instance_id.'.text'}=&Apache::lonxml::endredirection();
459: }
460: return '';
461: }
462:
463: sub start_Criteria {
464: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.13 albertel 465: if ($target eq 'web' || $target eq 'webgrade') {
1.1 albertel 466: &Apache::lonxml::startredirection();
467: }
468: return '';
469: }
470:
471: sub end_Criteria {
472: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.9 albertel 473: my $instance_id=$Apache::bridgetask::instance[-1];
1.13 albertel 474: if ($target eq 'web' || $target eq 'webgrade') {
1.1 albertel 475: my $criteria=&Apache::lonxml::endredirection();
476: my $id=$Apache::lonxml::curdepth;
477: $dimension{$instance_id.'.criteria.'.$id}=$criteria;
478: push(@{$dimension{$instance_id.'.criterias'}},$id);
479: }
480: return '';
481: }
482:
1.4 albertel 483: sub proctor_validation_screen {
484: my ($slot) = @_;
485: my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
1.5 albertel 486: my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
1.11 albertel 487: $user=$env{'form.proctorname'};
488: if ($env{'form.proctordomain'}) { $domain=$env{'form.proctordomain'}; }
1.4 albertel 489: my $msg;
1.11 albertel 490: if ($env{'form.proctorpassword'}) {
1.4 albertel 491: $msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
492: .'</font></p>';
493: }
494: my $result= (<<ENDCHECKOUT);
495: <h2>Proctor Validation</h2>
496: <p>Your room's proctor needs to validate your access to this resource.</p>
497: $msg
1.11 albertel 498: <form name="checkout" method="POST" action="$env{'request.uri'}">
1.4 albertel 499: <input type="hidden" name="validate" value="yes" />
500: <input type="hidden" name="submitted" value="yes" />
501: <table>
502: <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$user" /></td></tr>
503: <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
1.6 albertel 504: <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$domain" /></td></tr>
1.4 albertel 505: </table>
506: <input type="submit" name="checkoutbutton" value="Validate" /><br />
507: Student who should be logged in is:<br />
1.5 albertel 508: <img src="$url" /><br />
1.4 albertel 509: </form>
510: ENDCHECKOUT
511: return $result;
512: }
513:
1.1 albertel 514: 1;
515: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>