File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.106: download - view: text, annotated - select for diffs
Mon Aug 26 20:29:30 2002 UTC (21 years, 8 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Statements now can be printed correctly, for example: You are correct. Your receipt is 596-2276.

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: structuretags.pm,v 1.106 2002/08/26 20:29:30 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.= ' /> Show All 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:       }
  301:     } elsif ($target eq 'web') {
  302:       my $name= &get_resource_name($parstack,$safeeval);
  303:       if ($status eq 'CAN_ANSWER') {
  304: 	# create a page header and exit
  305: 	$result.="$head_tag_start<title>$name</title></head>
  306:               $body_tag_start \n $form_tag_start".	
  307: 		'<input type="hidden" name="submitted" value="yes" />';
  308: 	if ($ENV{'request.state'} eq "construct") {
  309: 	  $result.= &problem_web_to_edit_header($rndseed);
  310: 	}
  311: 	# if we are viewing someone else preserve that info
  312: 	if (defined $ENV{'form.grade_symb'}) {
  313: 	  foreach my $field ('symb','courseid','domain','username') {
  314: 	    $result .= '<input type="hidden" name="grade_'.$field.
  315: 	      '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
  316: 	  }
  317: 	}
  318:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
  319: 	       || $status eq 'CLOSED') {
  320: 	$result.=$head_tag_start.
  321: 	  "<title>$name</title></head>\n$body_tag_start\n";
  322:       }
  323:     } elsif ($target eq 'tex') {
  324: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  325: 	if ($name eq '') { 
  326: 	    $name=&Apache::lonnet::EXT('resource.title');
  327: 	    if ($name eq 'con_lost') { $name = ''; }
  328: 	}
  329: 	$Apache::lonhomework::name=$name;
  330: 	my $id = $Apache::inputtags::part;
  331: 	my $duedate = &Apache::lonnet::EXT("resource.$id.duedate"); 
  332: 	$duedate = POSIX::strftime("%c",localtime($duedate));
  333: 	my $temp_file;
  334: 	my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
  335:         if (-e $filename) {
  336: 	    $temp_file = Apache::File->new($filename); 
  337: 	} else {
  338: 	    $temp_file = Apache::File->new('>>'.$filename); 
  339: 	}
  340: 	my @due_file_content = <$temp_file>;
  341: 	my $due_file_content = $due_file_content[$#due_file_content];
  342:         chomp $due_file_content;
  343:         if ($due_file_content ne $duedate) {	    
  344: 	$temp_file = Apache::File->new('>'.$filename); 
  345: 	    print $temp_file "$duedate\n";	    
  346: 	    if (not $ENV{'request.symb'} =~ m/\.page_/) {
  347: 		if(not $duedate=~m/1969/) {
  348: 		    $result .= '\begin{document} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm';	
  349: 		} else {
  350: 		    $result .= '\begin{document} \noindent \vskip 1 mm';
  351: 		}
  352: 	    } else {
  353: 		$result .= '\parbox{\minipagewidth}{\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
  354: 	    } 
  355: 	} else {
  356: 	    if (not $ENV{'request.symb'} =~ m/\.page_/) {
  357: 		$result .= '\begin{document} \noindent \vskip 1 mm';	
  358: 	    } else {
  359: 		$result .= '\parbox{\minipagewidth}{\vskip 1mm \\\\\\\\';
  360: 	    } 
  361: 	}
  362:     }
  363:   } elsif ($target eq 'edit') {
  364:     $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  365:       &problem_edit_header();
  366:     my $temp=&Apache::edit::insertlist($target,$token);
  367:     $result.=$temp;
  368:   } elsif ($target eq 'modified') {
  369:     $result=$token->[4];
  370:     $result.=&Apache::edit::handle_insert();
  371:   } else {
  372:     # page_start returned a starting result, delete it if we don't need it
  373:     $result = '';
  374:   }
  375:   return $result;
  376: }
  377: 
  378: sub end_problem {
  379:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  380:   my $result='';
  381:   my $status=$Apache::inputtags::status['-1'];
  382:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
  383:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
  384:       # if part is zero, no <part>s existed, so we need to the grading
  385:       &Apache::inputtags::grade;
  386:     } elsif ( ($target eq 'web' || $target eq 'tex') && $Apache::inputtags::part eq '0' && 
  387: 	      $status ne 'UNCHECKEDOUT') {
  388:       # if part is zero, no <part>s existed, so we need show the current 
  389:       # grading status
  390:       my $gradesatement = &Apache::inputtags::gradestatus($Apache::inputtags::part);
  391:       if ($target eq 'tex') {
  392: 	  $gradesatement =~ s/<table>//;
  393: 	  $gradesatement =~ s/<\/table>//;
  394: 	  $gradesatement =~ s/<tr([^>]*)>//g;
  395: 	  $gradesatement =~ s/<\/tr>//g;
  396: 	  $gradesatement =~ s/<td([^>]*)>//g;
  397: 	  $gradesatement =~ s/<\/td>//g;
  398: 	  $gradesatement =~ s/<b>/\\textbf{/g;
  399: 	  $gradesatement =~ s/<\/b>/}/g;
  400: 	  $gradesatement =~ s/<br \/>/\\vskip 0 mm /g;
  401: 	  $gradesatement =~ s/<input([^>]*)>//g;
  402:       }
  403:       $result.= $gradesatement;
  404:       if ($target eq 'tex') {
  405: 	  $result .= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
  406: 	  if (not $ENV{'request.symb'} =~ m/\.page_/) {
  407: 	      $result .= '\end{document} ';
  408: 	  } else {
  409: 	      $result .= '} ';
  410: 	  }
  411:       }
  412:     }
  413:     if (
  414: 	(($target eq 'web' || $target eq 'tex') && ($ENV{'request.state'} ne 'construct')) ||
  415: 	($target eq 'answer')
  416:        ) {
  417:       if ($status eq 'CAN_ANSWER') {
  418: 	  if ($target ne 'tex') {
  419: 	      $result.="</form></body>\n";
  420: 	  } 
  421:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
  422: 	       $status eq 'UNCHECKEDOUT' ) {
  423: 	  if ($target ne 'tex') {
  424: 	      $result.="</body>\n";
  425: 	  }
  426:       }
  427:       if ($target ne 'tex') {
  428: 	  $result.=&Apache::lonxml::xmlend();
  429:       } else {
  430: 	      $result .= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
  431: 	      if (not $ENV{'request.symb'} =~ m/\.page_/) {
  432: 		  $result .= '\end{document} ';
  433: 	      } else {
  434: 		  $result .= '} ';
  435: 	      }
  436:       }
  437:     }
  438:     if ($target eq 'grade') { 
  439:       &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  440:       &finalize_storage();
  441:     }
  442:   } elsif ($target eq 'meta') {
  443:     if ($Apache::inputtags::part eq '0') {
  444:       $result=&Apache::response::mandatory_part_meta;
  445:     }
  446:   } elsif ($target eq 'edit') {
  447:     &Apache::lonxml::debug("in end_problem with $target, edit");
  448:     $result = &problem_edit_footer();
  449:   } 
  450:   return $result;
  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:     &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  457:   if ($target eq 'edit') {
  458:     $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  459:       &problem_edit_header();
  460:     my $temp=&Apache::edit::insertlist($target,$token);
  461:     $result.=$temp;
  462:   } elsif ($target eq 'modified') {
  463:     $result=$token->[4];
  464:     $result.=&Apache::edit::handle_insert();
  465:   } elsif ($target eq 'web' && $ENV{'request.state'} eq "construct" ) {
  466:     my $name=&get_resource_name($parstack,$safeeval);
  467:     my $rndseed=&setup_rndseed($safeeval);
  468:     $result.="$head_tag_start<title>$name</title></head>
  469:               $body_tag_start \n $form_tag_start".	
  470: 		'<input type="hidden" name="submitted" value="yes" />';
  471:     $result.=&problem_web_to_edit_header($rndseed);
  472:   }
  473:   return $result;
  474: }
  475: 
  476: sub end_library {
  477:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  478:   my $result='';
  479:   if ($target eq 'edit') {
  480:     $result=&problem_edit_footer();
  481:   } elsif ($target eq 'web' && $ENV{'request.state'} eq "construct") {
  482:     $result.='</form></body>'.&Apache::lonxml::xmlend();
  483:   }
  484:   return $result;
  485: }
  486: 
  487: sub start_block {
  488:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  489: 
  490:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  491:     my $code = @$parstack[$#$parstack];
  492:     $code =~ s/\"//g;
  493:     $code .=';return $condition;';
  494:     #  print "<br />$code<br />";
  495:     my $result = &Apache::run::run($code,$safeeval);
  496:     &Apache::lonxml::debug("block :$code: returned :$result:");
  497:     if ( ! $result ) { 
  498:       my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
  499:       &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
  500:     }
  501:   }
  502:   return "";
  503: }
  504: 
  505: sub end_block {
  506:   return '';
  507: }
  508: 
  509: sub start_while {
  510:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  511: 
  512:   my $code = @$parstack[$#$parstack];
  513:   $code =~ s/\"//g;
  514:   $code .=';return $condition;';
  515: 
  516:   push( @Apache::structuretags::whileconds, $code); 
  517:   my $result = &Apache::run::run($code,$safeeval);
  518:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  519:   push( @Apache::structuretags::whilebody, $bodytext);
  520:   if ( $result ) { 
  521:     &Apache::lonxml::newparser($parser,\$bodytext);
  522:   }
  523:   return "";
  524: }
  525: 
  526: sub end_while {
  527:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  528:   my $code = pop @Apache::structuretags::whileconds;
  529:   my $bodytext = pop @Apache::structuretags::whilebody;
  530:   my $result = &Apache::run::run($code,$safeeval);
  531:   if ( $result ) { 
  532:     &Apache::lonxml::newparser($parser,\$bodytext);
  533:   } 
  534:   return "";
  535: }
  536: 
  537: # <randomlist> 
  538: #  <tag1>..</tag1>
  539: #  <tag2>..</tag2>
  540: #  <tag3>..</tag3>
  541: #  ... 
  542: # </randomlist>
  543: sub start_randomlist {
  544:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  545:   my $result;
  546:   if ($target eq 'answer' || $target eq 'grade' || $target eq 'web') {
  547:     my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  548:     my $b_parser= HTML::TokeParser->new(\$body);
  549:     my $b_tok;
  550:     my @randomlist;
  551:     my $list_item;
  552:     while($b_tok = $b_parser->get_token() ) {
  553:       if($b_tok->[0] eq 'S') { # start tag
  554: 	# get content of the tag until matching end tag
  555: 	# get all text upto the matching tag
  556: 	# and push the content into @randomlist
  557: 	$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  558: 	$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  559: 	push(@randomlist,$list_item);
  560: 	#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
  561:       }
  562:       if($b_tok->[0] eq 'T') { # text
  563: 	# what to do with text in between tags?
  564: 	#  print "<b>TEXT $b_tok->[1]</b><br />";
  565:       }
  566:       # if($b_tok->[0] eq 'E') { # end tag, should not happen
  567:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
  568:       # }
  569:     }
  570:     my @idx_arr = (0 .. $#randomlist);
  571:     &Apache::structuretags::shuffle(\@idx_arr);
  572:     my $bodytext = '';
  573:     my $show=$#randomlist;
  574:     my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
  575:     $showarg--;
  576:     if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
  577:     for(0 .. $show) {
  578:       $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  579:     }
  580:     &Apache::lonxml::newparser($parser,\$bodytext);
  581:   } elsif ($target eq 'edit' ) {
  582:     $result .= &Apache::edit::tag_start($target,$token);
  583:     $result .= &Apache::edit::text_arg('Maximum Tags to Show:','show',$token,5);
  584:     $result .= &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  585:   } elsif ($target eq 'modified' ) {
  586:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  587: 						 'show');
  588:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  589:   }
  590:   return $result;
  591: }
  592: 
  593: sub shuffle {
  594:     my $a=shift;
  595:     my $i;
  596:     if (defined(@$a)) {
  597:       &Apache::response::setrandomnumber();
  598:       for($i=@$a;--$i;) {
  599: 	my $j=int rand($i+1);
  600: 	next if $i == $j;
  601: 	@$a[$i,$j] = @$a[$j,$i];
  602:       }
  603:     }
  604: }
  605: 
  606: sub end_randomlist {
  607:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  608:   my $result;
  609:   if ($target eq 'edit' ) {
  610:     $result=&Apache::edit::tag_end($target,$token,'End Randomly Parsed Block');
  611:   }
  612:   return $result;
  613: }
  614: 
  615: sub start_part {
  616:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  617:   my $result='';
  618:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  619:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  620:   $Apache::inputtags::part=$id;
  621:   @Apache::inputtags::responselist = ();
  622:   @Apache::inputtags::previous=();
  623:   if ($target eq 'meta') {
  624:     return &Apache::response::mandatory_part_meta;
  625:   } elsif ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  626:     my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  627:     push (@Apache::inputtags::status,$status);
  628:     my $expression='$external::datestatus="'.$status.'";';
  629:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  630:     &Apache::run::run($expression,$safeeval);
  631:     if ( $status eq 'CLOSED' ) {
  632:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  633:       if ( $target eq "web" ) {
  634: 	$result="<br />Part is not open to be viewed. It $accessmsg<br />";
  635:       } elsif ( $target eq 'tex' ) {
  636: 	$result="\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\";
  637:       }
  638:     } else {
  639:       if ($target eq 'tex') {
  640: 	$result='\vskip 0 mm';
  641:       }
  642:     }
  643:   }
  644:   return $result;
  645: }
  646: 
  647: sub end_part {
  648:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  649:   &Apache::lonxml::debug("in end_part $target ");
  650:   my $status=$Apache::inputtags::status['-1'];
  651:   pop @Apache::inputtags::status;
  652:   if ( $target eq 'meta' ) { return ''; }
  653:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
  654:     return &Apache::inputtags::grade;
  655:   }
  656:   if ($target eq 'web') {
  657:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  658:   }
  659:   return '';
  660: }
  661: 
  662: sub start_preduedate {
  663:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  664:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  665:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  666: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  667:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  668:     }
  669:   }
  670:   return '';
  671: }
  672: 
  673: sub end_preduedate {
  674:   return '';
  675: }
  676: 
  677: sub start_postanswerdate {
  678:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  679:   if ($target eq 'web' || $target eq 'grade') {
  680:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  681:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  682:     }
  683:   } elsif ($target eq 'tex') {
  684:       return '\vskip 0 mm \noindent';
  685:   }
  686:   return '';
  687: }
  688: 
  689: sub end_postanswerdate {
  690:   return '';
  691: }
  692: 
  693: sub start_notsolved {
  694:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  695:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  696:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  697:     &Apache::lonxml::debug("not solved has :$gradestatus:");
  698:     if ($gradestatus =~ /^correct/) {
  699:       &Apache::lonxml::debug("skipping");
  700:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  701:     }
  702:   }
  703:   return '';
  704: }
  705: 
  706: sub end_notsolved {
  707:   return '';
  708: }
  709: 
  710: sub start_solved {
  711:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  712:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  713:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  714:     if ($gradestatus !~ /^correct/) {
  715:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
  716:     }
  717:   }
  718:   return '';
  719: }
  720: 
  721: sub end_solved {
  722:   return '';
  723: }
  724: 
  725: sub start_startouttext {
  726:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  727:   my @result=(''.'');
  728:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  729:   return (@result);
  730: }
  731: sub end_startouttext {
  732:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  733:   my $result='';
  734:   my $text='';
  735: 
  736:   if ($target eq 'edit') {
  737:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[-1]);
  738:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  739: <td>Delete:".
  740:   &Apache::edit::deletelist($target,$token)
  741:     ."</td>
  742: <td>".
  743:   &Apache::edit::insertlist($target,$token).
  744:     &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n".
  745:       &Apache::edit::editfield($token->[1],$text,"",50,4);
  746:   }
  747:   if ($target eq 'modified') {
  748:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  749:     $result='<startouttext />'.&Apache::edit::modifiedfield();
  750:   }
  751:   if ($target eq 'tex') {
  752:       $result .= '\noindent ';
  753:   }
  754:   return $result;
  755: }
  756: sub start_endouttext {
  757:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  758:   my $result='';
  759:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  760:   if ($target eq "modified") { $result='<endouttext />'; }
  761:   return $result;
  762: }
  763: sub end_endouttext {
  764:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  765:   my @result=('','');
  766:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  767:   return (@result);
  768: }
  769: sub delete_startouttext {
  770:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  771: #  my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  772:   my $text=$$parser['-1']->get_text("/endouttext");
  773:   my $ntoken=$$parser['-1']->get_token();
  774:   &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
  775:   &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
  776:   # Deleting 2 parallel tag pairs, but we need the numbers later to look like 
  777:   # they did the last time round
  778:   &Apache::lonxml::increasedepth($ntoken);
  779:   &Apache::lonxml::decreasedepth($ntoken);
  780:   return 1;
  781: }
  782: 
  783: 1;
  784: __END__

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