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

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

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