File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.155: download - view: text, annotated - select for diffs
Fri Mar 7 23:41:29 2003 UTC (21 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- clean up the globals in end_problem
- new global $Apache::lonhomework::parsing_a_problem set to 1 while between start_problem and end_problem

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

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