File:  [LON-CAPA] / loncom / homework / bridgetask.pm
Revision 1.9: download - view: text, annotated - select for diffs
Mon Apr 4 18:14:58 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- making bridge tasks randomize whihc instance in a dimension is shown

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: bridgetask.pm,v 1.9 2005/04/04 18:14:58 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: 
   31: package Apache::bridgetask; 
   32: 
   33: use strict;
   34: use Apache::lonnet;
   35: use Apache::File();
   36: use Apache::lonmenu;
   37: use Apache::lonlocal;
   38: use Apache::lonxml;
   39: use Time::HiRes qw( gettimeofday tv_interval );
   40: 
   41: BEGIN {
   42:     &Apache::lonxml::register('Apache::bridgetask',('Task','IntroParagraph','Dimension','Instance','InstanceText','Criteria','ClosingParagraph'));
   43: }
   44: 
   45: sub initialize_bridgetask {
   46:     # id of current Dimension, 0 means that no dimension is current 
   47:     # (inside <Task> only)
   48:     $Apache::bridgetask::dimension='';
   49:     # list of all Dimension ids seen
   50:     @Apache::bridgetask::dimensionlist=();
   51:     # 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: 
   57: sub proctor_check_auth {
   58:     my ($slot)=@_;
   59:     my $user=$ENV{'form.proctorname'};
   60:     my $domain=$ENV{'form.proctordomain'};
   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) {
   66: 	    my $authhost=&Apache::lonnet::authenticate($puser,$ENV{'form.proctorpassword'},$pdom);
   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: 
   77: sub add_previous_version_button {
   78:     my $result;
   79:     $result.=&mt(' Show a previously done version: [_1]','<select name="previousversion">
   80: <option>Pick one</option>
   81: </select>');
   82:     return $result;
   83: }
   84: 
   85: sub start_Task {
   86:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   87: 
   88:     &Apache::structuretags::initialize_storage();
   89:     &Apache::lonhomework::showhash(%Apache::lonhomework::history);
   90: 
   91:     my ($status,$accessmsg,$slot);
   92:     $Apache::lonhomework::parsing_a_task=1;
   93:     #should get back a <html> or the neccesary stuff to start XML/MathML
   94:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
   95: 	&Apache::structuretags::page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
   96: 
   97:     if ($target eq 'web') {
   98: 	$body_tag_start.=&add_previous_version_button();
   99:     }
  100:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  101: 	$target eq 'tex') {
  102: 	($status,$accessmsg,$slot) = 
  103: 	    &Apache::lonhomework::check_task_access('0');
  104: 	push(@Apache::inputtags::status,$status);
  105: 	my $expression='$external::datestatus="'.$status.'";';
  106: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
  107: 	&Apache::run::run($expression,$safeeval);
  108: 	&Apache::lonxml::debug("Got $status");
  109: 	if (( $status eq 'CLOSED' ) ||
  110: 	    ( $status eq 'BANNED') ||
  111: 	    ( $status eq 'UNAVAILABLE') ||
  112: 	    ( $status eq 'NOT_IN_A_SLOT') ||
  113: 	    ( $status eq 'NEEDS_CHECKIN') ||
  114: 	    ( $status eq 'INVALID_ACCESS')) {
  115: 	    my $bodytext=&Apache::lonxml::get_all_text("/task",$parser);
  116: 	    if ( $target eq "web" ) {
  117: 		$result.= $head_tag_start.'</head>'.$body_tag_start;
  118: 		my $msg;
  119: 		if ($status eq 'UNAVAILABLE') {
  120: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
  121: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
  122: 		    $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
  123: 		} elsif ($status eq 'NEEDS_CHECKIN') {
  124: 		    $msg.='<h1>'.&mt('You need the Proctor to validate you.').
  125: 			'</h1>'.&proctor_validation_screen($slot);
  126: 		} elsif ($status ne 'NOT_YET_VIEWED') {
  127: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
  128: 		}
  129: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
  130: 		    $msg.='The problem '.$accessmsg;
  131: 		}
  132: 		$result.=$msg.'<br />';
  133: 	    } elsif ($target eq 'tex') {
  134: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
  135: 		if ($status eq 'UNAVAILABLE') {
  136: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
  137: 		} else {
  138: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
  139: 		}
  140: 	    } elsif ($target eq 'grade') {
  141: 		if ($status eq 'NEEDS_CHECKIN') {
  142: 		    if (&proctor_check_auth($slot)) {
  143: 			#FIXME immeadiatly add this to the grading queue
  144:                         #      with slot->{'endtime'} for when grading can 
  145:                         #      begin on this resource
  146:  		    }
  147: 		}
  148: 	    }
  149: 	} elsif ($target eq 'web') {
  150: 	    my $name= &Apache::structuretags::get_resource_name($parstack,$safeeval);
  151: 	    $result.="$head_tag_start<title>$name</title></head>
  152:               $body_tag_start \n $form_tag_start".	
  153: 	      '<input type="hidden" name="submitted" value="yes" />';
  154: 	    # if we are viewing someone else preserve that info
  155: 	    if (defined $ENV{'form.grade_symb'}) {
  156: 		foreach my $field ('symb','courseid','domain','username') {
  157: 		    $result .= '<input type="hidden" name="grade_'.$field.
  158: 			'" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
  159: 		}
  160: 	    }
  161: 	}
  162:     } else {
  163: 	# page_start returned a starting result, delete it if we don't need it
  164: 	$result = '';
  165:     }
  166:     return $result;
  167: }
  168: 
  169: sub end_Task {
  170:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  171:     my $result='';
  172:     my $status=$Apache::inputtags::status['-1'];
  173:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  174: 	$target eq 'tex') {
  175: 	if (
  176: 	    (($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
  177: 	    ($target eq 'answer') || ($target eq 'tex')
  178: 	   ) {
  179: 	    if ($target eq 'web') {
  180: 		if ($status eq 'CAN_ANSWER') {
  181: 		    $result.='<table border="1">'.
  182: 			&Apache::inputtags::file_selector('0',"bridgetask","*",
  183: 							  'portfolioonly').
  184: 							  "</table>";
  185: 		    $result.=&Apache::inputtags::gradestatus('0');
  186: 		}
  187: 		$result.=&Apache::lonxml::xmlend().'</html>';
  188: 	    }
  189: 	}
  190: 	if ($target eq 'grade') {
  191: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  192: 	    &Apache::structuretags::finalize_storage();
  193: 	}
  194:     } elsif ($target eq 'meta') {
  195: 	$result.='<parameter part="0" package="Task"></parameter>'."\n";
  196: 	#$result.=&Apache::response::meta_part_order();
  197: 	#$result.=&Apache::response::meta_response_order();
  198:     }
  199:     undef($Apache::lonhomework::parsing_a_task);
  200:     return $result;
  201: }
  202: 
  203: sub start_ClosingParagraph {
  204:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  205:     my $result;
  206:     if ($target eq 'web') {
  207:     }
  208:     return $result;
  209: }
  210: 
  211: sub end_ClosingParagraph {
  212:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  213:     my $result;
  214:     if ($target eq 'web') {
  215:     }
  216:     return $result;
  217: }
  218: 
  219: my %dimension;
  220: sub start_Dimension {
  221:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  222:     undef(%dimension);
  223:     my $dim_id=$Apache::lonxml::curdepth;
  224:     $Apache::bridgetask::dimension=$dim_id;
  225:     push(@Apache::bridgetask::dimensionlist,$dim_id);
  226:     undef(@Apache::bridgetask::instance);
  227:     return '';
  228: }
  229: 
  230: sub end_Dimension {
  231:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  232:     my $result;
  233:     if ($target eq 'web') {
  234: 	#FIXME just grabbing the first one for now, need
  235: 	#to randomly pick one until all have been seen
  236: 	#then start repicking
  237: 	&Apache::response::pushrandomnumber();
  238: 	my $num_instances=scalar(@{$dimension{'instances'}});
  239: 	my $which=&Math::Random::random_uniform_integer(1,0,$num_instances-1);
  240: 	my $instance=$dimension{'instances'}->[$which];
  241: 	$result=$dimension{'intro'}.$dimension{$instance.'.text'};
  242: 	#FIXME: don't show the criteria yet
  243: 	#foreach my $id (@{$dimension{$instance.'.criterias'}}) {
  244: 	#    $result.=$dimension{$instance.'.criteria.'.$id};
  245: 	#}
  246:     }
  247:     return $result;
  248: }
  249: 
  250: sub start_IntroParagraph {
  251:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  252:     my $result;
  253:     if ($target eq 'web') {
  254: 	if ($tagstack->[-2] eq 'Dimension') {
  255: 	    &Apache::lonxml::startredirection();
  256: 	}
  257:     }
  258:     return $result;
  259: }
  260: 
  261: sub end_IntroParagraph {
  262:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  263:     my $result;
  264:     if ($target eq 'web') {
  265: 	if ($tagstack->[-2] eq 'Dimension') {
  266: 	    $dimension{'intro'}=&Apache::lonxml::endredirection();
  267: 	}
  268:     }
  269:     return $result;
  270: }
  271: 
  272: sub start_Instance {
  273:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  274:     push(@{$dimension{'instances'}},$Apache::lonxml::curdepth);
  275:     push(@Apache::bridgetask::instance,$Apache::lonxml::curdepth);
  276:     push(@Apache::bridgetask::instancelist,$Apache::lonxml::curdepth);
  277:     return '';
  278: }
  279: 
  280: sub end_Instance {
  281:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  282:     return '';
  283: }
  284: 
  285: sub start_InstanceText {
  286:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  287:     if ($target eq 'web') {
  288: 	&Apache::lonxml::startredirection();
  289:     }
  290:     return '';
  291: }
  292: 
  293: sub end_InstanceText {
  294:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  295:     my $instance_id=$Apache::bridgetask::instance[-1];
  296:     if ($target eq 'web') {
  297: 	$dimension{$instance_id.'.text'}=&Apache::lonxml::endredirection();
  298:     }
  299:     return '';
  300: }
  301: 
  302: sub start_Criteria {
  303:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  304:     if ($target eq 'web') {
  305: 	&Apache::lonxml::startredirection();
  306:     }
  307:     return '';
  308: }
  309: 
  310: sub end_Criteria {
  311:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  312:     my $instance_id=$Apache::bridgetask::instance[-1];
  313:     if ($target eq 'web') {
  314: 	my $criteria=&Apache::lonxml::endredirection();
  315: 	my $id=$Apache::lonxml::curdepth;
  316: 	$dimension{$instance_id.'.criteria.'.$id}=$criteria;
  317: 	push(@{$dimension{$instance_id.'.criterias'}},$id);
  318:     }
  319:     return '';
  320: }
  321: 
  322: sub proctor_validation_screen {
  323:     my ($slot) = @_;
  324:     my (undef,undef,$domain,$user) = &Apache::lonxml::whichuser();
  325:     my $url=&Apache::lonnet::studentphoto($domain,$user,'jpg');
  326:     $user=$ENV{'form.proctorname'};
  327:     if ($ENV{'form.proctordomain'}) { $domain=$ENV{'form.proctordomain'}; }
  328:     my $msg;
  329:     if ($ENV{'form.proctorpassword'}) {
  330: 	$msg='<p><font color="red">'.&mt("Failed to authenticate the proctor.")
  331: 	    .'</font></p>';
  332:     }
  333:     my $result= (<<ENDCHECKOUT);
  334: <h2>Proctor Validation</h2>
  335:     <p>Your room's proctor needs to validate your access to this resource.</p>
  336:     $msg
  337: <form name="checkout" method="POST" action="$ENV{'request.uri'}">
  338: <input type="hidden" name="validate" value="yes" />
  339: <input type="hidden" name="submitted" value="yes" />
  340: <table>
  341:   <tr><td>Proctor's Username:</td><td><input type="string" name="proctorname" value="$user" /></td></tr>
  342:   <tr><td>Password:</td><td><input type="password" name="proctorpassword" value="" /></td></tr>
  343:   <tr><td>Proctor's Domain:</td><td><input type="string" name="proctordomain" value="$domain" /></td></tr>
  344: </table>
  345: <input type="submit" name="checkoutbutton" value="Validate"  /><br />
  346: Student who should be logged in is:<br />
  347: <img src="$url" /><br />
  348: </form>
  349: ENDCHECKOUT
  350:     return $result;
  351: }
  352: 
  353: 1;
  354: __END__

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