File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.133: download - view: text, annotated - select for diffs
Thu Nov 7 16:10:47 2002 UTC (21 years, 6 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Fixed bug which did not allow to print and to show up on the screen
the options fron optionrespoonse problems in the exams after open due date.

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

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