File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.144: download - view: text, annotated - select for diffs
Thu Jan 9 19:34:27 2003 UTC (21 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: version_0_6_1, HEAD
- single part problems could be answered after hitting maxtries

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

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