Annotation of loncom/homework/structuretags.pm, revision 1.96

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

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