File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.135: download - view: text, annotated - select for diffs
Mon Nov 11 22:37:23 2002 UTC (21 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
A little less colorful.

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

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