File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.120: download - view: text, annotated - select for diffs
Tue Oct 1 20:44:44 2002 UTC (21 years, 7 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Removed gradestatus output for printing exams.

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

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