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

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

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