File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.161: download - view: text, annotated - select for diffs
Thu Apr 3 17:44:12 2003 UTC (21 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <while> actually works
- <while> preveted from becoming infinite loops

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

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