File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.154: download - view: text, annotated - select for diffs
Thu Mar 6 21:05:27 2003 UTC (21 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- remove part info from global when </part> is hit

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

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