File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.159: download - view: text, annotated - select for diffs
Thu Apr 3 14:31:58 2003 UTC (21 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- retabinate

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: structuretags.pm,v 1.159 2003/04/03 14:31:58 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 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" || $target eq 'modified') { @result=('','no'); }
  577:     return (@result);
  578: }
  579: 
  580: sub start_while {
  581:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  582: 
  583:     my $code = $token->[2]->{'condition'};
  584: 
  585:     push( @Apache::structuretags::whileconds, $code); 
  586:     if (!$Apache::lonxml::default_homework_loaded) {
  587: 	&Apache::lonxml::default_homework_load($safeeval);
  588:     }
  589:     my $result = &Apache::run::run($code,$safeeval);
  590:     my $bodytext=$$parser[$#$parser]->get_text("/while");
  591:     push( @Apache::structuretags::whilebody, $bodytext);
  592:     if ( $result ) {
  593: 	&Apache::lonxml::newparser($parser,\$bodytext);
  594:     }
  595:     return "";
  596: }
  597: 
  598: sub end_while {
  599:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  600:     my $code = pop @Apache::structuretags::whileconds;
  601:     my $bodytext = pop @Apache::structuretags::whilebody;
  602:     my $result = &Apache::run::run($code,$safeeval);
  603:     if ( $result ) {
  604: 	&Apache::lonxml::newparser($parser,\$bodytext);
  605:     }
  606:     return "";
  607: }
  608: 
  609: # <randomlist show="1"> 
  610: #  <tag1>..</tag1>
  611: #  <tag2>..</tag2>
  612: #  <tag3>..</tag3>
  613: #  ... 
  614: # </randomlist>
  615: sub start_randomlist {
  616:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  617:     my $result;
  618:     if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
  619: 	$target eq 'tex' || $target eq 'analyze') {
  620: 	my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
  621: 	my $b_parser= HTML::TokeParser->new(\$body);
  622: 	my $b_tok;
  623: 	my @randomlist;
  624: 	my $list_item;
  625: 	while($b_tok = $b_parser->get_token() ) {
  626: 	    if($b_tok->[0] eq 'S') { # start tag
  627: 		# get content of the tag until matching end tag
  628: 		# get all text upto the matching tag
  629: 		# and push the content into @randomlist
  630: 		$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
  631: 							   $b_parser);
  632: 		$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  633: 		push(@randomlist,$list_item);
  634: 		#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
  635:                 #         $list_item</b>";
  636: 	    }
  637: 	    if($b_tok->[0] eq 'T') { # text
  638: 		# what to do with text in between tags?
  639: 		#  print "<b>TEXT $b_tok->[1]</b><br />";
  640: 	    }
  641: 	    # if($b_tok->[0] eq 'E') { # end tag, should not happen
  642: 	    #  print "<b>END-TAG $b_tok->[1]</b><br />";
  643: 	    # }
  644: 	}
  645: 	my @idx_arr = (0 .. $#randomlist);
  646: 	&Apache::structuretags::shuffle(\@idx_arr);
  647: 	my $bodytext = '';
  648: 	my $show=$#randomlist;
  649: 	my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
  650: 	$showarg--;
  651: 	if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
  652: 	for(0 .. $show) {
  653: 	    $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  654: 	}
  655: 	&Apache::lonxml::newparser($parser,\$bodytext);
  656:     } elsif ($target eq 'edit' ) {
  657: 	$result .=&Apache::edit::tag_start($target,$token);
  658: 	$result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
  659: 					   $token,5);
  660: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  661:     } elsif ($target eq 'modified' ) {
  662: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  663: 						     $safeeval,'show');
  664: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  665:     }
  666:     return $result;
  667: }
  668: 
  669: sub shuffle {
  670:     my $a=shift;
  671:     my $i;
  672:     if (defined(@$a)) {
  673: 	&Apache::response::setrandomnumber();
  674: 	for($i=@$a;--$i;) {
  675: 	    my $j=int(&Math::Random::random_uniform() * ($i+1));
  676: 	    next if $i == $j;
  677: 	    @$a[$i,$j] = @$a[$j,$i];
  678: 	}
  679:     }
  680: }
  681: 
  682: sub end_randomlist {
  683:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  684:     my $result;
  685:     if ($target eq 'edit' ) {
  686: 	$result=&Apache::edit::tag_end($target,$token,
  687: 				       'End Randomly Parsed Block');
  688:     }
  689:     return $result;
  690: }
  691: 
  692: sub start_part {
  693:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  694:     my $result='';
  695:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  696:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  697:     $Apache::inputtags::part=$id;
  698:     @Apache::inputtags::responselist = ();
  699:     @Apache::inputtags::previous=();
  700:     @Apache::inputtags::previous_version=();
  701:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
  702: 
  703:     if ($target eq 'meta') {
  704: 	return &Apache::response::mandatory_part_meta;
  705:     } elsif ($target eq 'web' || $target eq 'grade' ||
  706: 	     $target eq 'answer' || $target eq 'tex') {
  707: 	if ($hidden) {
  708: 	    my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
  709: 	} else {
  710: 	    my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  711: 	    push (@Apache::inputtags::status,$status);
  712: 	    my $expression='$external::datestatus="'.$status.'";';
  713: 	    $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  714: 	    &Apache::run::run($expression,$safeeval);
  715: 	    if ( $status eq 'CLOSED' ) {
  716: 		my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
  717: 		if ( $target eq "web" ) {
  718: 		    $result="<br />Part is not open to be viewed. It $accessmsg<br />";
  719: 		} elsif ( $target eq 'tex' ) {
  720: 		    $result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
  721: 		}
  722: 	    } else {
  723: 		if ($target eq 'tex') {
  724: 		    $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
  725: 		    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  726: 		    if ($Apache::lonhomework::type eq 'exam') { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  727: 		}
  728: 	    }
  729: 	}
  730:     } elsif ($target eq 'edit') {
  731: 	$result.=&Apache::edit::tag_start($target,$token);
  732: 	$result.=&Apache::edit::text_arg('Part ID:','id',$token).
  733: 	    &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
  734: 		&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  735:     } elsif ($target eq 'modified') {
  736: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  737: 						     $safeeval,'id');
  738: 	if ($constructtag) {
  739: 	    $result = &Apache::edit::rebuild_tag($token);
  740: 	    $result.=&Apache::edit::handle_insert();
  741: 	}
  742:     }
  743:     return $result;
  744: }
  745: 
  746: sub end_part {
  747:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  748:     &Apache::lonxml::debug("in end_part $target ");
  749:     my $status=$Apache::inputtags::status['-1'];
  750:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
  751:     my $result='';
  752:     if ( $target eq 'meta' ) {
  753: 	$result='';
  754:     } elsif ( $target eq 'grade' && $status eq 'CAN_ANSWER' && !$hidden) {
  755: 	$result=&Apache::inputtags::grade;
  756:     } elsif (($target eq 'web' || $target eq 'tex') && !$hidden ) {
  757: 	my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
  758: 							$target);
  759: 	if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
  760: 	$result=$gradestatus;
  761:     }
  762:     pop @Apache::inputtags::status;
  763:     $Apache::inputtags::part='';
  764:     return $result;
  765: }
  766: 
  767: sub start_preduedate {
  768:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  769:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  770: 	if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  771: 	    $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' &&
  772: 	    $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  773: 	    &Apache::lonxml::get_all_text("/preduedate",$parser);
  774: 	}
  775:     }
  776:     return '';
  777: }
  778: 
  779: sub end_preduedate {
  780:     return '';
  781: }
  782: 
  783: sub start_postanswerdate {
  784:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  785:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
  786: 	if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  787: 	    &Apache::lonxml::get_all_text("/postanswerdate",$parser);
  788: 	}
  789:     } elsif ($target eq 'tex') {
  790: 	return '\vskip 0 mm \noindent';
  791:     }
  792:     return '';
  793: }
  794: 
  795: sub end_postanswerdate {
  796:     return '';
  797: }
  798: 
  799: sub start_notsolved {
  800:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  801:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  802: 	$target eq 'tex') {
  803: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  804: 	&Apache::lonxml::debug("not solved has :$gradestatus:");
  805: 	if ($gradestatus =~ /^correct/) {
  806: 	    &Apache::lonxml::debug("skipping");
  807: 	    &Apache::lonxml::get_all_text("/notsolved",$parser);
  808: 	}
  809:     }
  810:     return '';
  811: }
  812: 
  813: sub end_notsolved {
  814:     return '';
  815: }
  816: 
  817: sub start_solved {
  818:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  819:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  820: 	$target eq 'tex') {
  821: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  822: 	if ($gradestatus !~ /^correct/) {
  823: 	    &Apache::lonxml::get_all_text("/solved",$parser);
  824: 	}
  825:     }
  826:     return '';
  827: }
  828: 
  829: sub end_solved {
  830:     return '';
  831: }
  832: 
  833: sub start_startouttext {
  834:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  835:     my @result=(''.'');
  836:     if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  837:     return (@result);
  838: }
  839: 
  840: sub end_startouttext {
  841:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  842:     my $result='';
  843:     my $text='';
  844: 
  845:     if ($target eq 'edit') {
  846: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser);
  847: 	$result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  848: <td>Delete:".
  849:                  &Apache::edit::deletelist($target,$token)
  850: 		 ."</td>
  851: <td>".
  852:                  &Apache::edit::insertlist($target,$token).
  853: 		 &Apache::edit::end_row().
  854:                  &Apache::edit::start_spanning_row()."\n"
  855: 		 .'<table><tr><td>'.
  856: 		 &Apache::loncommon::help_open_topic("Greek_Symbols",
  857: 						     'Greek Symbols',
  858: 						     undef,undef,600)
  859: 		 .'</td><td>'.
  860: 		 &Apache::loncommon::help_open_topic("Other_Symbols",
  861: 						     'Other Symbols',
  862: 						     undef,undef,600)
  863: 		 .'</td></tr></table>'.
  864: 		 &Apache::edit::editfield($token->[1],$text,"",80,4);
  865:     }
  866:     if ($target eq 'modified') {
  867: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser);
  868: 	$result='<startouttext />'.&Apache::edit::modifiedfield();
  869:     }
  870:     if ($target eq 'tex') {
  871: 	$result .= '\noindent ';
  872:     }
  873:     return $result;
  874: }
  875: 
  876: sub start_endouttext {
  877:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  878:     my $result='';
  879:     if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  880:     if ($target eq "modified") {
  881: 	$result='<endouttext />'.
  882: 	    &Apache::edit::handle_insertafter('startouttext'); }
  883:     return $result;
  884: }
  885: 
  886: sub end_endouttext {
  887:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  888:     my @result=('','');
  889:     if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  890:     return (@result);
  891: }
  892: 
  893: sub delete_startouttext {
  894:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  895:     #  my $text=&Apache::lonxml::get_all_text("endouttext",$parser);
  896:     my $text=$$parser['-1']->get_text("/endouttext");
  897:     my $ntoken=$$parser['-1']->get_token();
  898:     &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
  899:     &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
  900:     # Deleting 2 parallel tag pairs, but we need the numbers later to look like
  901:     # they did the last time round
  902:     &Apache::lonxml::increasedepth($ntoken);
  903:     &Apache::lonxml::decreasedepth($ntoken);
  904:     return 1;
  905: }
  906: 
  907: 1;
  908: __END__

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