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

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

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