File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.160: download - view: text, annotated - select for diffs
Thu Apr 3 14:53:44 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- editing <block> and <while> actually works now

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

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