File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.162: download - view: text, annotated - select for diffs
Thu Apr 3 20:05:21 2003 UTC (21 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- can now try out problems in res space,

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: structuretags.pm,v 1.162 2003/04/03 20:05:21 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: # 2/19 Guy
   29: # 6/26/2001 fixed extra web display at end of <web></web> tags
   30: # 8/17,8/18,8/20 Gerd Kortemeyer
   31: 
   32: 
   33: package Apache::structuretags; 
   34: 
   35: use strict;
   36: use Apache::lonnet;
   37: use Apache::File();
   38: use Apache::lonmenu;
   39: 
   40: BEGIN {
   41:     &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
   42: }
   43: 
   44: sub start_web {
   45:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   46:     my $bodytext=&Apache::lonxml::get_all_text("/web",$parser);
   47:     if ($target eq 'web') {
   48: 	return $bodytext;
   49:     }
   50:     return '';
   51: }
   52: 
   53: sub end_web {
   54:     return '';
   55: }
   56: 
   57: sub start_tex {
   58:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   59:     my $bodytext=&Apache::lonxml::get_all_text("/tex",$parser);
   60:     if ($target eq 'tex') {
   61: 	return $bodytext.' ';
   62:     }
   63:     return '';
   64: }
   65: 
   66: sub end_tex {
   67:     return '';
   68: }
   69: 
   70: sub page_start {
   71:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   72:     my %found;
   73:     foreach my $taginside ($tagstack) {
   74: 	foreach my $taglookedfor ('html','body','form') {
   75: 	    if ($taginside =~ /^$taglookedfor$/i) {$found{$taglookedfor} = 1;}
   76: 	}
   77:     }
   78: 
   79:     my $result;
   80:     my $head_tag_start;
   81:     if (!defined($found{'html'})) {
   82: 	$result=&Apache::londefdef::start_html($target,$token,$tagstack,
   83: 					       $parstack,$parser,$safeeval);
   84: 	$head_tag_start='<head>'.&Apache::lonmenu::registerurl(undef,$target);
   85:     }
   86:     my $body_tag_start;
   87:     if (!defined($found{'body'})) {
   88: 	$body_tag_start='<body onLoad="'.&Apache::lonmenu::loadevents().'" '.
   89: 	    'onUnload="'.&Apache::lonmenu::unloadevents().'" ';
   90: 	my $background=&Apache::lonxml::get_param('background',$parstack,
   91: 						  $safeeval);
   92: 	if ($background) {
   93: 	    $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
   94: 		$background;
   95: 	    $body_tag_start.='background="'.$background.'" ';
   96: 	} else {
   97: 	    my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
   98: 						   $safeeval);
   99: 	    if ($bgcolor) {
  100: 		$body_tag_start.='bgcolor="'.$bgcolor.'" ';
  101: 	    } else {
  102: 		$body_tag_start.='bgcolor="#ffffff"';
  103: 	    }
  104: 	}
  105: 	$body_tag_start.='>'.&Apache::lonmenu::menubuttons(undef,$target,1);
  106: 	if ($target eq 'web' && $ENV{'request.state'} ne 'construct') {
  107: 	    my ($symb)=&Apache::lonxml::whichuser();
  108: 	    if ($symb eq '') {
  109: 		my $help = &Apache::loncommon::help_open_topic("Ambiguous_Reference");
  110: 		$help="Browsing resource, all submissions are temporary.<br />";
  111: 		$body_tag_start.=$help;
  112: 	    }
  113: 	}
  114:     }
  115:     my $form_tag_start;
  116:     if (!defined($found{'form'})) {
  117: 	$form_tag_start='<form name="lonhomework" method="POST" action="'.
  118: 	    $ENV{'request.uri'}.'">';
  119:     }
  120:     return ($result,$head_tag_start,$body_tag_start,$form_tag_start);
  121: }
  122: 
  123: #use Time::HiRes();
  124: sub get_resource_name {
  125:     my ($parstack,$safeeval)=@_;
  126:     my $name=&Apache::lonnet::gettitle();
  127:     if ($name eq '') {
  128: 	$name=&Apache::lonnet::EXT('resource.title');
  129: 	if ($name eq 'con_lost') { $name = ''; }
  130:     }
  131:     $Apache::lonhomework::name=$name;
  132:     return $name;
  133: }
  134: 
  135: sub setup_rndseed {
  136:     my ($safeeval)=@_;
  137:     my $rndseed;
  138:     my ($symb)=&Apache::lonxml::whichuser();
  139:     if ($ENV{'request.state'} eq "construct" || $symb eq '') {
  140: 	$rndseed=$ENV{'form.rndseed'};
  141: 	if (!$rndseed) {
  142: 	    $rndseed=$Apache::lonhomework::history{'rndseed'};
  143: 	    if (!$rndseed) {
  144: 		$rndseed=time;
  145: 		$ENV{'form.rndseed'}=$rndseed;
  146: 	    }
  147: 	}
  148: 	if ($ENV{'form.resetdata'} eq 'New Problem Variation') {
  149: 	    $rndseed=time;
  150: 	    $ENV{'form.rndseed'}=$rndseed;
  151: 	}
  152: 	&Apache::lonxml::debug("Setting rndseed to $rndseed");
  153: 	&Apache::run::run('$external::randomseed='.$rndseed.';',$safeeval);
  154:     }
  155:     return $rndseed;
  156: }
  157: 
  158: sub problem_edit_header {
  159:     return '<input type="hidden" name="submitted" value="edit" />
  160:        <input type="hidden" name="problemmode" value="Edit" />
  161:        <input type="submit" name="problemmode" value="Discard Edits and View" />
  162:        <input type="submit" name="problemmode" value="EditXML" />
  163:        <input type="submit" name="Undo" value="undo" /> <hr />
  164:        <input type="submit" name="submit" value="Submit Changes and Edit" />
  165:        <input type="submit" name="submit" value="Submit Changes and View" /><br /><p>&nbsp;</p><table border="0"><tr><td bgcolor="#DDDDDD">
  166:       ';
  167: }
  168: 
  169: sub problem_edit_footer {
  170:     return '</td></tr></table><br /><input type="submit" name="submit" value="Submit Changes and Edit" />
  171:     <input type="submit" name="submit" value="Submit Changes and View" />';
  172: }
  173: 
  174: sub problem_web_to_edit_header {
  175:     my ($rndseed)=@_;
  176:     my $result.='<input type="hidden" name="problemmode" value="View" />
  177:              <input type="submit" name="problemmode" value="Edit" />
  178:              <input type="submit" name="problemmode" value="EditXML" />
  179:              Random Seed:<input type="text" name="rndseed" width="10" value="'.
  180: 	       $rndseed.'" />
  181:              <input type="submit" name="changerandseed" value="Change" />
  182:              <input type="submit" name="resetdata" value="Reset Submissions" />
  183:              <input type="checkbox" name="showallfoils" ';
  184:     if (defined($ENV{'form.showallfoils'})) { $result.='checked="on"'; }
  185:     $result.= ' />&nbsp;Show&nbsp;All&nbsp;Foils
  186:              <hr />';
  187:     my $numtoanalyze=$ENV{'form.numtoanalyze'};
  188:     if (!$numtoanalyze) { $numtoanalyze=100; }
  189:     $result.= '<input type="submit" name="problemmode" value="Answer Distribution" />
  190:              <input type="text" name="numtoanalyze" value="'.
  191: 		 $numtoanalyze.'" size="5" /> <hr />';
  192:     return $result;
  193: }
  194: 
  195: sub initialize_storage {
  196:     %Apache::lonhomework::results=();
  197:     %Apache::lonhomework::history=();
  198:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  199:     if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
  200: 	%Apache::lonhomework::history=
  201: 	    &Apache::lonnet::tmprestore($ENV{'request.uri'},'',$domain,$name);
  202: 	my ($temp)=keys %Apache::lonhomework::history ;
  203: 	&Apache::lonxml::debug("Return message of $temp");
  204:     } else {
  205: 	%Apache::lonhomework::history=
  206: 	    &Apache::lonnet::restore($symb,$courseid,$domain,$name);
  207:     }
  208:     #ignore error conditions
  209:     my ($temp)=keys %Apache::lonhomework::history ;
  210:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
  211: }
  212: 
  213: # -------------------------------------------------------------finalize_storage
  214: # Stores away the result has to a student's environment
  215: # checks form.grade_ for specific values, other wises stores
  216: # to the running users environment
  217: sub finalize_storage {
  218:     my $result;
  219:     my ($temp) = keys %Apache::lonhomework::results;
  220:     if ( $temp ne '' ) {
  221: 	my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  222: 	if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
  223: 	    $Apache::lonhomework::results{'rndseed'}=$ENV{'form.rndseed'};
  224: 	    $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
  225: 					$ENV{'request.uri'},'',$domain,$name);
  226: 	    &Apache::lonxml::debug('Construct Store return message:'.$result);
  227: 	} else {
  228: 	    $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
  229: 					    $symb,$courseid,$domain,$name);
  230: 	    &Apache::lonxml::debug('Store return message:'.$result);
  231: 	}
  232:     }
  233:     return $result;
  234: }
  235: 
  236: sub checkout_msg {
  237:     return (<<ENDCHECKOUT);
  238: <h2>The resource needs to be checked out</h2>
  239: As a resource gets checked out, a unique timestamped ID is given to it, and a
  240: permanent record is left in the system.<p />
  241: <font color=red>
  242: Checking out resources is subject to course policies, and may exclude future
  243: credit even if done erroneously.<p />
  244: </font>
  245: <form name="checkout" method="POST" action="$ENV{'request.uri'}">
  246: <input type="hidden" name="doescheckout" value="yes" />
  247: <input type="button" name="checkoutbutton" value="Check out Exam for Viewing" onClick="javascript:if (confirm('Check out Exam?')) { document.checkout.submit(); }" />
  248: </form>
  249: ENDCHECKOUT
  250: }
  251: 
  252: sub start_problem {
  253:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  254: 
  255:     $Apache::lonhomework::parsing_a_problem=1;
  256:     # meta is called from lonpublisher, which doesn't uses the normal
  257:     # lonhomework method of parsing the file which means that inputtags 
  258:     # won't get reset
  259:     if ( $Apache::inputtags::part ne '' && $target != 'meta' ) {
  260: 	&Apache::lonxml::error('Only one problem allowed in a .problem file');
  261: 	my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
  262: 	return '';
  263:     }
  264: #initialize globals
  265:     $Apache::inputtags::part='0';
  266:     @Apache::inputtags::responselist = ();
  267:     @Apache::inputtags::previous=();
  268:     @Apache::inputtags::previous_version=();
  269:     $Apache::structuretags::printanswer='No';
  270:     @Apache::structuretags::whileconds=();
  271:     @Apache::structuretags::whilebody=();
  272:     @Apache::structuretags::whileline=();
  273: 
  274:     if ($target ne 'analyze') {
  275: 	&initialize_storage();
  276: 	if ($target eq 'web') {
  277: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
  278: 	}
  279: 	$Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
  280: 	&Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
  281:     }
  282:     if ($Apache::lonhomework::type eq '') {
  283: 	my $uri=$ENV{'request.uri'};
  284: 	if ($uri=~/\.(\w+)$/) {
  285: 	    $Apache::lonhomework::type=$1;
  286: 	    &Apache::lonxml::debug("Using type of $1");
  287: 	} else {
  288: 	    $Apache::lonhomework::type='problem';
  289: 	    &Apache::lonxml::debug("Using default type, problem, :$uri:");
  290: 	}
  291:     }
  292: 
  293:     #added vars to the scripting enviroment
  294:     my $expression='$external::part='.$Apache::inputtags::part.';';
  295:     &Apache::run::run($expression,$safeeval);
  296:     my $status;
  297:     my $accessmsg;
  298: 
  299:     #should get back a <html> or the neccesary stuff to start XML/MathML
  300:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  301: 	&page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  302:     if ($target eq 'tex' and $ENV{'request.symb'} =~ m/\.page_/) {$result='';}
  303: 
  304:     if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
  305:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  306: 	$target eq 'tex') {
  307: 	#handle exam checkout
  308: 	if ($Apache::lonhomework::type eq 'exam') {
  309: 	    my $token=
  310: 		$Apache::lonhomework::history{"resource.0.outtoken"};
  311: 	    if (($ENV{'form.doescheckout'}) && (!$token)) {
  312: 		$token=&Apache::lonxml::maketoken();
  313: 		$Apache::lonhomework::history{"resource.0.outtoken"}=
  314: 		    $token;
  315: 	    }
  316: 	    $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
  317: 	}
  318: 
  319: 	#handle rand seed in construction space
  320: 	my $rndseed=&setup_rndseed($safeeval);
  321: 	my ($symb)=&Apache::lonxml::whichuser();
  322: 	if ($ENV{'request.state'} ne "construct" || $symb eq '') {
  323: 	    $form_tag_start.='<input type="hidden" name="rndseed" value="'.
  324: 		$rndseed.'" />'.
  325: 		    '<input type="submit" name="resetdata"
  326:                              value="New Problem Variation" />';
  327: 	}
  328: 	($status,$accessmsg) = &Apache::lonhomework::check_access('0');
  329: 	push (@Apache::inputtags::status,$status);
  330: 	my $expression='$external::datestatus="'.$status.'";';
  331: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
  332: 	&Apache::run::run($expression,$safeeval);
  333: 	&Apache::lonxml::debug("Got $status");
  334: 	if (( $status eq 'CLOSED' ) ||
  335: 	    ( $status eq 'UNCHECKEDOUT') ||
  336: 	    ( $status eq 'BANNED') ||
  337: 	    ( $status eq 'UNAVAILABLE')) {
  338: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
  339: 	    if ( $target eq "web" ) {
  340: 		$result.= $head_tag_start.'</head>';
  341: 		my $msg=$body_tag_start;
  342: 		if ($status eq 'UNAVAILABLE') {
  343: 		    $result.='<h1>Unable to determine if this resource is open due to network problems. Please try again later.</h1>';
  344: 		} else {
  345: 		    $result.='<h1>Not open to be viewed</h1>';
  346: 		}
  347: 		if ($status eq 'CLOSED') {
  348: 		    $msg.='The problem '.$accessmsg;
  349: 		} elsif ($status eq 'UNCHECKEDOUT') {
  350: 		    $msg.=&checkout_msg;
  351: 		}
  352: 		$result.=$msg.'<br />';
  353: 	    } elsif ($target eq 'tex') {
  354: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
  355: 		if ($status eq 'UNAVAILABLE') {
  356: 		    $result.='Unable to determine if this resource is open due to network problems. Please try again later.\vskip 0 mm ';
  357: 		} else {
  358: 		    $result.="Problem is not open to be viewed. It $accessmsg \\vskip 0 mm ";
  359: 		}
  360: 	    }
  361: 	} elsif ($target eq 'web') {
  362: 	    my $name= &get_resource_name($parstack,$safeeval);
  363: 	    if ($status eq 'CAN_ANSWER') {
  364: 		# create a page header and exit
  365: 		$result.="$head_tag_start<title>$name</title></head>
  366:               $body_tag_start \n $form_tag_start".	
  367: 		  '<input type="hidden" name="submitted" value="yes" />';
  368: 		if ($ENV{'request.state'} eq "construct") {
  369: 		    $result.= &problem_web_to_edit_header($rndseed);
  370: 		}
  371: 		# if we are viewing someone else preserve that info
  372: 		if (defined $ENV{'form.grade_symb'}) {
  373: 		    foreach my $field ('symb','courseid','domain','username') {
  374: 			$result .= '<input type="hidden" name="grade_'.$field.
  375: 			    '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
  376: 		    }
  377: 		}
  378: 	    } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
  379: 		     || $status eq 'CLOSED' || $status eq 'UNAVALAILABLE') {
  380: 		$result.=$head_tag_start.
  381: 		    "<title>$name</title></head>\n$body_tag_start\n";
  382: 	    }
  383: 	} elsif ($target eq 'tex') {
  384: 	    my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  385: 	    if ($name eq '') {
  386: 		$name=&Apache::lonnet::EXT('resource.title');
  387: 		if ($name eq 'con_lost') { $name = ''; }
  388: 	    }
  389: 	    $Apache::lonhomework::name=$name;
  390: 	    my $id = $Apache::inputtags::part;
  391: 	    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  392: 	    my $allkeys=&Apache::lonnet::metadata($ENV{'request.uri'},'keys');
  393: 	    my @allkeys = split /,/,$allkeys;
  394: 	    my $allow_print_points = 0;
  395: 	    foreach my $partial_key (@allkeys) {
  396: 		if ($partial_key=~m/weight/) {
  397: 		    $allow_print_points++;
  398: 		}
  399: 	    }
  400: 	    my $duedate = &Apache::lonnet::EXT("resource.$id.duedate"); 
  401: 	    $duedate = POSIX::strftime("%c",localtime($duedate));
  402: 	    my $temp_file;
  403: 	    my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
  404: 	    if (-e $filename) {
  405: 		$temp_file = Apache::File->new($filename);
  406: 	    } else {
  407: 		$temp_file = Apache::File->new('>>'.$filename);
  408: 	    }
  409: 	    my @due_file_content = <$temp_file>;
  410: 	    my $due_file_content = $due_file_content[$#due_file_content];
  411: 	    chomp $due_file_content;
  412: 	    my $name_of_resourse= &get_resource_name($parstack,$safeeval);
  413: 	    if ($due_file_content ne $duedate) {
  414: 		$temp_file = Apache::File->new('>'.$filename);
  415: 		print $temp_file "$duedate\n";
  416: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  417: 		    if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
  418: 			$result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm\noindent \begin{minipage}{\textwidth}';	
  419: 		    } else {
  420: 			$result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent \vskip 1 mm \noindent\begin{minipage}{\textwidth}';
  421: 			if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  422: 		    }
  423: 		} else {
  424: 		    $result .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
  425: 		}
  426: 	    } else {
  427: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  428: 		    $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent \vskip 1 mm\noindent\begin{minipage}{\textwidth}';	
  429: 		    if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  430: 		} else {
  431: 		    $result .= '\vskip 1mm \\\\\\\\';
  432: 		}
  433: 	    }
  434: 	}
  435:     } elsif ($target eq 'edit') {
  436: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  437: 	    &problem_edit_header();
  438: 	my $temp=&Apache::edit::insertlist($target,$token);
  439: 	$result.=$temp;
  440:     } elsif ($target eq 'modified') {
  441: 	$result=$token->[4];
  442: 	$result.=&Apache::edit::handle_insert();
  443:     } else {
  444: 	# page_start returned a starting result, delete it if we don't need it
  445: 	$result = '';
  446:     }
  447:     return $result;
  448: }
  449: 
  450: sub end_problem {
  451:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  452:     my $result='';
  453:     my $status=$Apache::inputtags::status['-1'];
  454:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  455: 	$target eq 'tex') {
  456: 	if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
  457: 	     $status eq 'CAN_ANSWER' ) {
  458: 	    # if part is zero, no <part>s existed, so we need to the grading
  459: 	    &Apache::inputtags::grade;
  460: 	} elsif ( ($target eq 'web' || $target eq 'tex') &&
  461: 		  $Apache::inputtags::part eq '0' &&
  462: 		  $status ne 'UNCHECKEDOUT') {
  463: 	    # if part is zero, no <part>s existed, so we need show the current
  464: 	    # grading status
  465: 	    my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
  466: 	    $result.= $gradestatus;
  467: 	}
  468: 	if (
  469: 	    (($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
  470: 	    ($target eq 'answer') || ($target eq 'tex')
  471: 	   ) {
  472: 	    if ($status eq 'CAN_ANSWER') {
  473: 		if ($target ne 'tex') {
  474: 		    $result.="</form></body>\n";
  475: 		}
  476: 	    } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
  477: 		     $status eq 'UNCHECKEDOUT' ) {
  478: 		if ($target ne 'tex') {
  479: 		    $result.="</body>\n";
  480: 		}
  481: 	    }
  482: 	    if ($target eq 'web') {
  483: 		$result.=&Apache::lonxml::xmlend();
  484: 	    } elsif ($target eq 'tex') {
  485: 		$result .= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
  486: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  487: 		    $result .= '\end{minipage}\end{document} ';
  488: 		} else {
  489: 		    $result .= '';
  490: 		}
  491: 	    }
  492: 	}
  493: 	if ($target eq 'grade') {
  494: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  495: 	    &finalize_storage();
  496: 	}
  497: 	if ($target eq 'answer' && ($ENV{'request.state'} eq 'construct') ) {
  498: 	    $result.='</html>'; #normally we get it from xmlend, but in CSTR
  499: 	                        # we always show answer mode too.
  500: 	}
  501:     } elsif ($target eq 'meta') {
  502: 	if ($Apache::inputtags::part eq '0') {
  503: 	    $result=&Apache::response::mandatory_part_meta;
  504: 	}
  505:     } elsif ($target eq 'edit') {
  506: 	&Apache::lonxml::debug("in end_problem with $target, edit");
  507: 	$result = &problem_edit_footer();
  508:     }
  509: 
  510:     undef(%Apache::lonhomework::history);
  511:     undef(%Apache::lonhomework::results);
  512:     undef($Apache::inputtags::part);
  513:     undef($Apache::lonhomework::parsing_a_problem);
  514: 
  515:     return $result;
  516: }
  517: 
  518: 
  519: sub start_library {
  520:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  521:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
  522: 
  523:     if ($target eq 'edit') {
  524: 	($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  525: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  526: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  527: 	    &problem_edit_header();
  528: 	my $temp=&Apache::edit::insertlist($target,$token);
  529: 	$result.=$temp;
  530:     } elsif ($target eq 'modified') {
  531: 	$result=$token->[4];
  532: 	$result.=&Apache::edit::handle_insert();
  533:     } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
  534: 	     $ENV{'request.state'} eq "construct" ) {
  535: 	($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  536: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  537: 	my $name=&get_resource_name($parstack,$safeeval);
  538: 	my $rndseed=&setup_rndseed($safeeval);
  539: 	$result.="$head_tag_start<title>$name</title></head>
  540:               $body_tag_start \n $form_tag_start".	
  541: 		  '<input type="hidden" name="submitted" value="yes" />';
  542: 	$result.=&problem_web_to_edit_header($rndseed);
  543:     }
  544:     return $result;
  545: }
  546: 
  547: sub end_library {
  548:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  549:     my $result='';
  550:     if ($target eq 'edit') {
  551: 	$result=&problem_edit_footer();
  552:     } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
  553: 	     $ENV{'request.state'} eq "construct") {
  554: 	$result.='</form></body>'.&Apache::lonxml::xmlend();
  555:     }
  556:     return $result;
  557: }
  558: 
  559: sub start_block {
  560:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  561: 
  562:     my $result;
  563: 
  564:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  565: 	$target eq 'tex' || $target eq 'analyze') {
  566: 	my $code = $token->[2]->{'condition'};
  567: 	if ($code) {
  568: 	    if (!$Apache::lonxml::default_homework_loaded) {
  569: 		&Apache::lonxml::default_homework_load($safeeval);
  570: 	    }
  571: 	    $result = &Apache::run::run($code,$safeeval);
  572: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
  573: 	} else {
  574: 	    $result='1';
  575: 	}
  576: 	if ( ! $result ) {
  577: 	    my $skip=&Apache::lonxml::get_all_text("/block",$parser);
  578: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
  579: 	}
  580: 	$result='';
  581:     } elsif ($target eq 'edit') {
  582: 	$result .=&Apache::edit::tag_start($target,$token);
  583: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
  584: 					  $token,40);
  585: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  586:     } elsif ($target eq 'modified') {
  587: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  588: 						     $safeeval,'condition');
  589: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  590:     }
  591:     return $result;
  592: }
  593: 
  594: sub end_block {
  595:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  596:     my $result;
  597:     if ($target eq "edit") {
  598: 	$result.= &Apache::edit::tag_end($target,$token,'');
  599:     }
  600:     return $result;
  601: }
  602: 
  603: sub start_while {
  604:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  605: 
  606:     my $result;
  607:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  608: 	$target eq 'tex' || $target eq 'analyze') {
  609: 	my $code = $token->[2]->{'condition'};
  610: 
  611: 	push( @Apache::structuretags::whileconds, $code);
  612: 	if (!$Apache::lonxml::default_homework_loaded) {
  613: 	    &Apache::lonxml::default_homework_load($safeeval);
  614: 	}
  615: 	my $result = &Apache::run::run($code,$safeeval);
  616: 	my $bodytext=&Apache::lonxml::get_all_text("/while",$parser);
  617: 	push( @Apache::structuretags::whilebody, $bodytext);
  618: 	push( @Apache::structuretags::whileline, $token->[5]);
  619: 	&Apache::lonxml::debug("s code $code got -$result-");
  620: 	if ( $result ) {
  621: 	    &Apache::lonxml::newparser($parser,\$bodytext);
  622: 	}
  623:     } elsif ($target eq 'edit') {
  624: 	$result .=&Apache::edit::tag_start($target,$token);
  625: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
  626: 					  $token,40);
  627: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  628:     } elsif ($target eq 'modified') {
  629: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  630: 						     $safeeval,'condition');
  631: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  632:     }
  633:     return $result;
  634: }
  635: 
  636: sub end_while {
  637:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  638:     my $result;
  639: 
  640:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  641: 	$target eq 'tex' || $target eq 'analyze') {
  642: 	my $code = pop(@Apache::structuretags::whileconds);
  643: 	my $bodytext = pop(@Apache::structuretags::whilebody);
  644: 	my $line = pop(@Apache::structuretags::whileline);
  645: 	my $return = &Apache::run::run($code,$safeeval);
  646: 	my $starttime=time;
  647: 	my $error=0;
  648: 	while ($return) {
  649: 	    if (time-$starttime >
  650: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}) {
  651: 		$return = 0; $error=1; next;
  652: 	    }
  653: 	    $result.=&Apache::scripttag::xmlparse($bodytext);
  654: 	    $return = &Apache::run::run($code,$safeeval);
  655: 	}
  656: 	if ($error) {
  657: 	    &Apache::lonxml::error('<pre>Code ran too long. It ran for more than '.$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds occured while running &lt;while$gt; on line '.$line.'</pre>');
  658: 	}
  659:     } elsif ($target eq "edit") {
  660: 	$result.= &Apache::edit::tag_end($target,$token,'');
  661:     }
  662:     return $result;
  663: }
  664: 
  665: # <randomlist show="1">
  666: #  <tag1>..</tag1>
  667: #  <tag2>..</tag2>
  668: #  <tag3>..</tag3>
  669: #  ...
  670: # </randomlist>
  671: sub start_randomlist {
  672:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  673:     my $result;
  674:     if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
  675: 	$target eq 'tex' || $target eq 'analyze') {
  676: 	my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
  677: 	my $b_parser= HTML::TokeParser->new(\$body);
  678: 	my $b_tok;
  679: 	my @randomlist;
  680: 	my $list_item;
  681: 	while($b_tok = $b_parser->get_token() ) {
  682: 	    if($b_tok->[0] eq 'S') { # start tag
  683: 		# get content of the tag until matching end tag
  684: 		# get all text upto the matching tag
  685: 		# and push the content into @randomlist
  686: 		$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
  687: 							   $b_parser);
  688: 		$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  689: 		push(@randomlist,$list_item);
  690: 		#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
  691:                 #         $list_item</b>";
  692: 	    }
  693: 	    if($b_tok->[0] eq 'T') { # text
  694: 		# what to do with text in between tags?
  695: 		#  print "<b>TEXT $b_tok->[1]</b><br />";
  696: 	    }
  697: 	    # if($b_tok->[0] eq 'E') { # end tag, should not happen
  698: 	    #  print "<b>END-TAG $b_tok->[1]</b><br />";
  699: 	    # }
  700: 	}
  701: 	my @idx_arr = (0 .. $#randomlist);
  702: 	&Apache::structuretags::shuffle(\@idx_arr);
  703: 	my $bodytext = '';
  704: 	my $show=$#randomlist;
  705: 	my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
  706: 	$showarg--;
  707: 	if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
  708: 	for(0 .. $show) {
  709: 	    $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  710: 	}
  711: 	&Apache::lonxml::newparser($parser,\$bodytext);
  712:     } elsif ($target eq 'edit' ) {
  713: 	$result .=&Apache::edit::tag_start($target,$token);
  714: 	$result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
  715: 					   $token,5);
  716: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  717:     } elsif ($target eq 'modified' ) {
  718: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  719: 						     $safeeval,'show');
  720: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  721:     }
  722:     return $result;
  723: }
  724: 
  725: sub shuffle {
  726:     my $a=shift;
  727:     my $i;
  728:     if (defined(@$a)) {
  729: 	&Apache::response::setrandomnumber();
  730: 	for($i=@$a;--$i;) {
  731: 	    my $j=int(&Math::Random::random_uniform() * ($i+1));
  732: 	    next if $i == $j;
  733: 	    @$a[$i,$j] = @$a[$j,$i];
  734: 	}
  735:     }
  736: }
  737: 
  738: sub end_randomlist {
  739:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  740:     my $result;
  741:     if ($target eq 'edit' ) {
  742: 	$result=&Apache::edit::tag_end($target,$token,
  743: 				       'End Randomly Parsed Block');
  744:     }
  745:     return $result;
  746: }
  747: 
  748: sub start_part {
  749:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  750:     my $result='';
  751:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  752:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  753:     $Apache::inputtags::part=$id;
  754:     @Apache::inputtags::responselist = ();
  755:     @Apache::inputtags::previous=();
  756:     @Apache::inputtags::previous_version=();
  757:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
  758: 
  759:     if ($target eq 'meta') {
  760: 	return &Apache::response::mandatory_part_meta;
  761:     } elsif ($target eq 'web' || $target eq 'grade' ||
  762: 	     $target eq 'answer' || $target eq 'tex') {
  763: 	if ($hidden) {
  764: 	    my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
  765: 	} else {
  766: 	    my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  767: 	    push (@Apache::inputtags::status,$status);
  768: 	    my $expression='$external::datestatus="'.$status.'";';
  769: 	    $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  770: 	    &Apache::run::run($expression,$safeeval);
  771: 	    if ( $status eq 'CLOSED' ) {
  772: 		my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
  773: 		if ( $target eq "web" ) {
  774: 		    $result="<br />Part is not open to be viewed. It $accessmsg<br />";
  775: 		} elsif ( $target eq 'tex' ) {
  776: 		    $result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
  777: 		}
  778: 	    } else {
  779: 		if ($target eq 'tex') {
  780: 		    $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
  781: 		    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  782: 		    if ($Apache::lonhomework::type eq 'exam') { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  783: 		}
  784: 	    }
  785: 	}
  786:     } elsif ($target eq 'edit') {
  787: 	$result.=&Apache::edit::tag_start($target,$token);
  788: 	$result.=&Apache::edit::text_arg('Part ID:','id',$token).
  789: 	    &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
  790: 		&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  791:     } elsif ($target eq 'modified') {
  792: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  793: 						     $safeeval,'id');
  794: 	if ($constructtag) {
  795: 	    $result = &Apache::edit::rebuild_tag($token);
  796: 	    $result.=&Apache::edit::handle_insert();
  797: 	}
  798:     }
  799:     return $result;
  800: }
  801: 
  802: sub end_part {
  803:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  804:     &Apache::lonxml::debug("in end_part $target ");
  805:     my $status=$Apache::inputtags::status['-1'];
  806:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
  807:     my $result='';
  808:     if ( $target eq 'meta' ) {
  809: 	$result='';
  810:     } elsif ( $target eq 'grade' && $status eq 'CAN_ANSWER' && !$hidden) {
  811: 	$result=&Apache::inputtags::grade;
  812:     } elsif (($target eq 'web' || $target eq 'tex') && !$hidden ) {
  813: 	my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
  814: 							$target);
  815: 	if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
  816: 	$result=$gradestatus;
  817:     }
  818:     pop @Apache::inputtags::status;
  819:     $Apache::inputtags::part='';
  820:     return $result;
  821: }
  822: 
  823: sub start_preduedate {
  824:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  825:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  826: 	if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  827: 	    $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' &&
  828: 	    $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  829: 	    &Apache::lonxml::get_all_text("/preduedate",$parser);
  830: 	}
  831:     }
  832:     return '';
  833: }
  834: 
  835: sub end_preduedate {
  836:     return '';
  837: }
  838: 
  839: sub start_postanswerdate {
  840:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  841:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
  842: 	if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  843: 	    &Apache::lonxml::get_all_text("/postanswerdate",$parser);
  844: 	}
  845:     } elsif ($target eq 'tex') {
  846: 	return '\vskip 0 mm \noindent';
  847:     }
  848:     return '';
  849: }
  850: 
  851: sub end_postanswerdate {
  852:     return '';
  853: }
  854: 
  855: sub start_notsolved {
  856:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  857:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  858: 	$target eq 'tex') {
  859: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  860: 	&Apache::lonxml::debug("not solved has :$gradestatus:");
  861: 	if ($gradestatus =~ /^correct/) {
  862: 	    &Apache::lonxml::debug("skipping");
  863: 	    &Apache::lonxml::get_all_text("/notsolved",$parser);
  864: 	}
  865:     }
  866:     return '';
  867: }
  868: 
  869: sub end_notsolved {
  870:     return '';
  871: }
  872: 
  873: sub start_solved {
  874:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  875:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  876: 	$target eq 'tex') {
  877: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  878: 	if ($gradestatus !~ /^correct/) {
  879: 	    &Apache::lonxml::get_all_text("/solved",$parser);
  880: 	}
  881:     }
  882:     return '';
  883: }
  884: 
  885: sub end_solved {
  886:     return '';
  887: }
  888: 
  889: sub start_startouttext {
  890:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  891:     my @result=(''.'');
  892:     if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  893:     return (@result);
  894: }
  895: 
  896: sub end_startouttext {
  897:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  898:     my $result='';
  899:     my $text='';
  900: 
  901:     if ($target eq 'edit') {
  902: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser);
  903: 	$result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  904: <td>Delete:".
  905:                  &Apache::edit::deletelist($target,$token)
  906: 		 ."</td>
  907: <td>".
  908:                  &Apache::edit::insertlist($target,$token).
  909: 		 &Apache::edit::end_row().
  910:                  &Apache::edit::start_spanning_row()."\n"
  911: 		 .'<table><tr><td>'.
  912: 		 &Apache::loncommon::help_open_topic("Greek_Symbols",
  913: 						     'Greek Symbols',
  914: 						     undef,undef,600)
  915: 		 .'</td><td>'.
  916: 		 &Apache::loncommon::help_open_topic("Other_Symbols",
  917: 						     'Other Symbols',
  918: 						     undef,undef,600)
  919: 		 .'</td></tr></table>'.
  920: 		 &Apache::edit::editfield($token->[1],$text,"",80,4);
  921:     }
  922:     if ($target eq 'modified') {
  923: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser);
  924: 	$result='<startouttext />'.&Apache::edit::modifiedfield();
  925:     }
  926:     if ($target eq 'tex') {
  927: 	$result .= '\noindent ';
  928:     }
  929:     return $result;
  930: }
  931: 
  932: sub start_endouttext {
  933:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  934:     my $result='';
  935:     if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  936:     if ($target eq "modified") {
  937: 	$result='<endouttext />'.
  938: 	    &Apache::edit::handle_insertafter('startouttext'); }
  939:     return $result;
  940: }
  941: 
  942: sub end_endouttext {
  943:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  944:     my @result=('','');
  945:     if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  946:     return (@result);
  947: }
  948: 
  949: sub delete_startouttext {
  950:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  951:     #  my $text=&Apache::lonxml::get_all_text("endouttext",$parser);
  952:     my $text=$$parser['-1']->get_text("/endouttext");
  953:     my $ntoken=$$parser['-1']->get_token();
  954:     &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
  955:     &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
  956:     # Deleting 2 parallel tag pairs, but we need the numbers later to look like
  957:     # they did the last time round
  958:     &Apache::lonxml::increasedepth($ntoken);
  959:     &Apache::lonxml::decreasedepth($ntoken);
  960:     return 1;
  961: }
  962: 
  963: 1;
  964: __END__

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