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

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

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