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

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.136   ! albertel    4: # $Id: structuretags.pm,v 1.135 2002/11/11 22:37:23 www 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:     }
1.136   ! albertel  141:     &Apache::lonxml::debug("Setting rndseed to $rndseed");
1.105     albertel  142:     &Apache::run::run('$external::randomseed='.$rndseed.';',$safeeval);
                    143:   }
                    144:   return $rndseed;
                    145: }
                    146: 
                    147: sub problem_edit_header {
                    148:   return '<input type="hidden" name="submitted" value="edit" />
                    149:        <input type="hidden" name="problemmode" value="Edit" />
                    150:        <input type="submit" name="problemmode" value="Discard Edits and View" />
                    151:        <input type="submit" name="problemmode" value="EditXML" />
                    152:        <input type="submit" name="Undo" value="undo" /> <hr />
1.135     www       153:        <input type="submit" name="submit" value="Submit Changes and Edit" />
                    154:        <input type="submit" name="submit" value="Submit Changes and View" /><br /><p>&nbsp;</p><table border="0"><tr><td bgcolor="#DDDDDD">
1.105     albertel  155:       ';
                    156: }
                    157: 
                    158: sub problem_edit_footer {
1.135     www       159:   return '</td></tr></table><br /><input type="submit" name="submit" value="Submit Changes and Edit" />
1.105     albertel  160:     <input type="submit" name="submit" value="Submit Changes and View" />';
                    161: }
                    162: 
                    163: sub problem_web_to_edit_header {
                    164:   my ($rndseed)=@_;
                    165:   my $result.='<input type="hidden" name="problemmode" value="View" />
                    166:              <input type="submit" name="problemmode" value="Edit" />
                    167:              <input type="submit" name="problemmode" value="EditXML" />
                    168:              Random Seed:<input type="text" name="rndseed" width="10" value="'.
                    169: 	       $rndseed.'" />
                    170:              <input type="submit" name="changerandseed" value="Change" />
                    171:              <input type="submit" name="resetdata" value="Reset Submissions" />
                    172:              <input type="checkbox" name="showallfoils" ';
                    173:   if (defined($ENV{'form.showallfoils'})) { $result.='checked="on"'; }
1.115     albertel  174:   $result.= ' />&nbsp;Show&nbsp;All&nbsp;Foils
1.105     albertel  175:              <hr />';
1.126     albertel  176:   my $numtoanalyze=$ENV{'form.numtoanalyze'};
                    177:   if (!$numtoanalyze) { $numtoanalyze=100; }
1.136   ! albertel  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.136   ! albertel  284:   if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
1.98      sakharuk  285:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.67      albertel  286:     #handle exam checkout
                    287:     if ($Apache::lonhomework::type eq 'exam') {
                    288:       my $token=$Apache::lonhomework::history{"resource.0.outtoken"};
                    289:       if (($ENV{'form.doescheckout'}) && (!$token)) {
                    290: 	$token=&Apache::lonxml::maketoken();
                    291: 	$Apache::lonhomework::history{"resource.0.outtoken"}=$token;
                    292:       }
                    293:       $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
                    294:     }
                    295: 
                    296:     #handle rand seed in construction space
1.105     albertel  297:     my $rndseed=&setup_rndseed($safeeval);
1.55      www       298:     ($status,$accessmsg) = &Apache::lonhomework::check_access('0');
1.22      albertel  299:     push (@Apache::inputtags::status,$status);
1.24      albertel  300:     my $expression='$external::datestatus="'.$status.'";';
                    301:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                    302:     &Apache::run::run($expression,$safeeval);
1.132     albertel  303:     &Apache::lonxml::debug("Got $status");
1.54      www       304:     if (( $status eq 'CLOSED' ) ||
                    305:         ( $status eq 'UNCHECKEDOUT') ||
                    306:         ( $status eq 'BANNED')) {
1.132     albertel  307:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
1.21      albertel  308:       if ( $target eq "web" ) {
1.41      albertel  309: 	$result.= $head_tag_start.'</head>';
1.54      www       310:         my $msg=$body_tag_start.
                    311: 	    '<h1>Not open to be viewed</h1>';
                    312:         if ($status eq 'CLOSED') {
1.55      www       313: 	    $msg.='The problem '.$accessmsg;
1.54      www       314: 	} elsif ($status eq 'UNCHECKEDOUT') {
1.65      albertel  315:             $msg.=&checkout_msg;
1.54      www       316:         }
1.67      albertel  317: 	$result.=$msg.'<br />';
1.117     sakharuk  318:       } elsif ($target eq 'tex') {
                    319: 	  $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 ";
                    320:       } 
1.67      albertel  321:     } elsif ($target eq 'web') {
1.105     albertel  322:       my $name= &get_resource_name($parstack,$safeeval);
1.67      albertel  323:       if ($status eq 'CAN_ANSWER') {
                    324: 	# create a page header and exit
1.105     albertel  325: 	$result.="$head_tag_start<title>$name</title></head>
                    326:               $body_tag_start \n $form_tag_start".	
1.36      albertel  327: 		'<input type="hidden" name="submitted" value="yes" />';
1.67      albertel  328: 	if ($ENV{'request.state'} eq "construct") {
1.105     albertel  329: 	  $result.= &problem_web_to_edit_header($rndseed);
1.67      albertel  330: 	}
                    331: 	# if we are viewing someone else preserve that info
                    332: 	if (defined $ENV{'form.grade_symb'}) {
                    333: 	  foreach my $field ('symb','courseid','domain','username') {
                    334: 	    $result .= '<input type="hidden" name="grade_'.$field.
                    335: 	      '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
                    336: 	  }
1.65      albertel  337: 	}
1.67      albertel  338:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
                    339: 	       || $status eq 'CLOSED') {
                    340: 	$result.=$head_tag_start.
                    341: 	  "<title>$name</title></head>\n$body_tag_start\n";
1.65      albertel  342:       }
1.99      sakharuk  343:     } elsif ($target eq 'tex') {
1.103     sakharuk  344: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    345: 	if ($name eq '') { 
                    346: 	    $name=&Apache::lonnet::EXT('resource.title');
                    347: 	    if ($name eq 'con_lost') { $name = ''; }
                    348: 	}
                    349: 	$Apache::lonhomework::name=$name;
1.99      sakharuk  350: 	my $id = $Apache::inputtags::part;
                    351: 	my $duedate = &Apache::lonnet::EXT("resource.$id.duedate"); 
                    352: 	$duedate = POSIX::strftime("%c",localtime($duedate));
1.101     sakharuk  353: 	my $temp_file;
                    354: 	my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
                    355:         if (-e $filename) {
                    356: 	    $temp_file = Apache::File->new($filename); 
1.99      sakharuk  357: 	} else {
1.101     sakharuk  358: 	    $temp_file = Apache::File->new('>>'.$filename); 
                    359: 	}
                    360: 	my @due_file_content = <$temp_file>;
                    361: 	my $due_file_content = $due_file_content[$#due_file_content];
                    362:         chomp $due_file_content;
                    363:         if ($due_file_content ne $duedate) {	    
                    364: 	$temp_file = Apache::File->new('>'.$filename); 
1.104     sakharuk  365: 	    print $temp_file "$duedate\n";	    
1.101     sakharuk  366: 	    if (not $ENV{'request.symb'} =~ m/\.page_/) {
1.104     sakharuk  367: 		if(not $duedate=~m/1969/) {
1.119     sakharuk  368: 		    $result .= '\begin{document} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm\noindent \begin{minipage}{\textwidth}';	
1.104     sakharuk  369: 		} else {
1.120     sakharuk  370: 		    $result .= '\begin{document} \noindent \vskip 1 mm \noindent\begin{minipage}{\textwidth}';
1.104     sakharuk  371: 		}
1.101     sakharuk  372: 	    } else {
1.117     sakharuk  373: 		$result .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
1.101     sakharuk  374: 	    } 
                    375: 	} else {
                    376: 	    if (not $ENV{'request.symb'} =~ m/\.page_/) {
1.119     sakharuk  377: 		$result .= '\begin{document} \noindent \vskip 1 mm\noindent\begin{minipage}{\textwidth}';	
1.101     sakharuk  378: 	    } else {
1.117     sakharuk  379: 		$result .= '\vskip 1mm \\\\\\\\';
1.101     sakharuk  380: 	    } 
1.99      sakharuk  381: 	}
                    382:     }
1.67      albertel  383:   } elsif ($target eq 'edit') {
1.105     albertel  384:     $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
                    385:       &problem_edit_header();
1.39      albertel  386:     my $temp=&Apache::edit::insertlist($target,$token);
1.36      albertel  387:     $result.=$temp;
1.67      albertel  388:   } elsif ($target eq 'modified') {
1.37      albertel  389:     $result=$token->[4];
                    390:     $result.=&Apache::edit::handle_insert();
1.67      albertel  391:   } else {
                    392:     # page_start returned a starting result, delete it if we don't need it
                    393:     $result = '';
1.37      albertel  394:   }
1.67      albertel  395:   return $result;
1.9       albertel  396: }
                    397: 
                    398: sub end_problem {
1.40      albertel  399:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.16      albertel  400:   my $result='';
1.24      albertel  401:   my $status=$Apache::inputtags::status['-1'];
1.106     sakharuk  402:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.83      albertel  403:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
1.19      albertel  404:       # if part is zero, no <part>s existed, so we need to the grading
                    405:       &Apache::inputtags::grade;
1.106     sakharuk  406:     } elsif ( ($target eq 'web' || $target eq 'tex') && $Apache::inputtags::part eq '0' && 
1.91      albertel  407: 	      $status ne 'UNCHECKEDOUT') {
1.19      albertel  408:       # if part is zero, no <part>s existed, so we need show the current 
                    409:       # grading status
1.122     sakharuk  410:       my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
1.119     sakharuk  411:       if ($Apache::lonhomework::type ne 'exam') {$result.= $gradestatus;}
1.41      albertel  412:     }
1.68      albertel  413:     if (
1.112     albertel  414: 	(($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
                    415: 	($target eq 'answer') || ($target eq 'tex')
1.68      albertel  416:        ) {
1.22      albertel  417:       if ($status eq 'CAN_ANSWER') {
1.106     sakharuk  418: 	  if ($target ne 'tex') {
                    419: 	      $result.="</form></body>\n";
                    420: 	  } 
1.91      albertel  421:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
                    422: 	       $status eq 'UNCHECKEDOUT' ) {
1.106     sakharuk  423: 	  if ($target ne 'tex') {
                    424: 	      $result.="</body>\n";
                    425: 	  }
                    426:       }
                    427:       if ($target ne 'tex') {
                    428: 	  $result.=&Apache::lonxml::xmlend();
                    429:       } else {
                    430: 	      $result .= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
                    431: 	      if (not $ENV{'request.symb'} =~ m/\.page_/) {
1.117     sakharuk  432: 		  $result .= '\end{minipage}\end{document} ';
1.106     sakharuk  433: 	      } else {
1.117     sakharuk  434: 		  $result .= '';
1.106     sakharuk  435: 	      }
1.41      albertel  436:       }
1.22      albertel  437:     }
1.65      albertel  438:     if ($target eq 'grade') { 
                    439:       &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    440:       &finalize_storage();
                    441:     }
                    442:   } elsif ($target eq 'meta') {
1.18      albertel  443:     if ($Apache::inputtags::part eq '0') {
1.16      albertel  444:       $result=&Apache::response::mandatory_part_meta;
                    445:     }
1.65      albertel  446:   } elsif ($target eq 'edit') {
1.34      albertel  447:     &Apache::lonxml::debug("in end_problem with $target, edit");
1.105     albertel  448:     $result = &problem_edit_footer();
1.106     sakharuk  449:   } 
1.48      albertel  450:   return $result;
                    451: }
                    452: 
1.108     albertel  453: 
1.48      albertel  454: sub start_library {
                    455:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.109     albertel  456:   my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
                    457: 
1.48      albertel  458:   if ($target eq 'edit') {
1.109     albertel  459:     ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    460:       &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.105     albertel  461:     $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
                    462:       &problem_edit_header();
1.48      albertel  463:     my $temp=&Apache::edit::insertlist($target,$token);
                    464:     $result.=$temp;
1.105     albertel  465:   } elsif ($target eq 'modified') {
1.48      albertel  466:     $result=$token->[4];
                    467:     $result.=&Apache::edit::handle_insert();
1.110     albertel  468:   } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
                    469: 	   $ENV{'request.state'} eq "construct" ) {
1.109     albertel  470:     ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    471:       &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.105     albertel  472:     my $name=&get_resource_name($parstack,$safeeval);
                    473:     my $rndseed=&setup_rndseed($safeeval);
                    474:     $result.="$head_tag_start<title>$name</title></head>
                    475:               $body_tag_start \n $form_tag_start".	
                    476: 		'<input type="hidden" name="submitted" value="yes" />';
                    477:     $result.=&problem_web_to_edit_header($rndseed);
1.48      albertel  478:   }
1.105     albertel  479:   return $result;
1.48      albertel  480: }
                    481: 
                    482: sub end_library {
                    483:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    484:   my $result='';
                    485:   if ($target eq 'edit') {
1.105     albertel  486:     $result=&problem_edit_footer();
1.114     albertel  487:   } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
                    488: 	   $ENV{'request.state'} eq "construct") {
1.105     albertel  489:     $result.='</form></body>'.&Apache::lonxml::xmlend();
1.34      albertel  490:   }
1.16      albertel  491:   return $result;
1.1       albertel  492: }
                    493: 
                    494: sub start_block {
1.131     albertel  495:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    496: 
                    497:     my $result;
1.1       albertel  498: 
1.131     albertel  499:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || 
                    500: 	$target eq 'tex' || $target eq 'analyze') {
                    501: 	my $code = @$parstack[$#$parstack];
                    502: 	if ($code) {
                    503: 	    $code =~ s/\"//g;
                    504: 	    $code .=';return $condition;';
                    505: 	    $result = &Apache::run::run($code,$safeeval);
                    506: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
                    507: 	} else {
                    508: 	    $result='1';
                    509: 	}
                    510: 	if ( ! $result ) {
                    511: 	    my $skip=&Apache::lonxml::get_all_text("/block",$$parser[-1]);
                    512: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                    513: 	}
                    514: 	$result='';
                    515:     } elsif ($target eq 'edit') {
                    516: 	$result .=&Apache::edit::tag_start($target,$token);
                    517: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
                    518: 					  $token,40);
                    519: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    520:     } elsif ($target eq 'modified') {
                    521: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    522: 						     $safeeval,'condition');
                    523: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38      albertel  524:     }
1.131     albertel  525:     return $result;
1.1       albertel  526: }
                    527: 
                    528: sub end_block {
1.38      albertel  529:   return '';
1.4       tsai      530: }
                    531: 
                    532: sub start_while {
1.40      albertel  533:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.4       tsai      534: 
                    535:   my $code = @$parstack[$#$parstack];
                    536:   $code =~ s/\"//g;
                    537:   $code .=';return $condition;';
                    538: 
1.5       tsai      539:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai      540:   my $result = &Apache::run::run($code,$safeeval);
                    541:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai      542:   push( @Apache::structuretags::whilebody, $bodytext);
                    543:   if ( $result ) { 
1.8       albertel  544:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai      545:   }
                    546:   return "";
                    547: }
                    548: 
                    549: sub end_while {
1.40      albertel  550:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.5       tsai      551:   my $code = pop @Apache::structuretags::whileconds;
                    552:   my $bodytext = pop @Apache::structuretags::whilebody;
                    553:   my $result = &Apache::run::run($code,$safeeval);
                    554:   if ( $result ) { 
1.8       albertel  555:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai      556:   } 
                    557:   return "";
1.1       albertel  558: }
1.6       tsai      559: 
1.110     albertel  560: # <randomlist show="1"> 
1.6       tsai      561: #  <tag1>..</tag1>
                    562: #  <tag2>..</tag2>
                    563: #  <tag3>..</tag3>
                    564: #  ... 
                    565: # </randomlist>
                    566: sub start_randomlist {
1.40      albertel  567:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.86      albertel  568:   my $result;
1.111     albertel  569:   if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
1.126     albertel  570:       $target eq 'tex' || $target eq 'analyze') {
1.71      albertel  571:     my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
                    572:     my $b_parser= HTML::TokeParser->new(\$body);
                    573:     my $b_tok;
                    574:     my @randomlist;
                    575:     my $list_item;
                    576:     while($b_tok = $b_parser->get_token() ) {
                    577:       if($b_tok->[0] eq 'S') { # start tag
                    578: 	# get content of the tag until matching end tag
                    579: 	# get all text upto the matching tag
                    580: 	# and push the content into @randomlist
                    581: 	$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
                    582: 	$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
                    583: 	push(@randomlist,$list_item);
                    584: 	#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
                    585:       }
                    586:       if($b_tok->[0] eq 'T') { # text
                    587: 	# what to do with text in between tags?
                    588: 	#  print "<b>TEXT $b_tok->[1]</b><br />";
                    589:       }
                    590:       # if($b_tok->[0] eq 'E') { # end tag, should not happen
                    591:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
                    592:       # }
                    593:     }
                    594:     my @idx_arr = (0 .. $#randomlist);
                    595:     &Apache::structuretags::shuffle(\@idx_arr);
                    596:     my $bodytext = '';
1.86      albertel  597:     my $show=$#randomlist;
                    598:     my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
                    599:     $showarg--;
                    600:     if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
                    601:     for(0 .. $show) {
1.71      albertel  602:       $bodytext .= "$randomlist[ $idx_arr[$_] ]";
1.6       tsai      603:     }
1.71      albertel  604:     &Apache::lonxml::newparser($parser,\$bodytext);
1.86      albertel  605:   } elsif ($target eq 'edit' ) {
                    606:     $result .= &Apache::edit::tag_start($target,$token);
                    607:     $result .= &Apache::edit::text_arg('Maximum Tags to Show:','show',$token,5);
                    608:     $result .= &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    609:   } elsif ($target eq 'modified' ) {
                    610:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    611: 						 'show');
                    612:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.7       tsai      613:   }
1.86      albertel  614:   return $result;
1.7       tsai      615: }
                    616: 
                    617: sub shuffle {
                    618:     my $a=shift;
                    619:     my $i;
1.70      albertel  620:     if (defined(@$a)) {
                    621:       &Apache::response::setrandomnumber();
                    622:       for($i=@$a;--$i;) {
1.118     albertel  623: 	my $j=int(&Math::Random::random_uniform() * ($i+1));
1.70      albertel  624: 	next if $i == $j;
                    625: 	@$a[$i,$j] = @$a[$j,$i];
                    626:       }
1.7       tsai      627:     }
1.6       tsai      628: }
                    629: 
                    630: sub end_randomlist {
1.86      albertel  631:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    632:   my $result;
                    633:   if ($target eq 'edit' ) {
                    634:     $result=&Apache::edit::tag_end($target,$token,'End Randomly Parsed Block');
                    635:   }
                    636:   return $result;
1.6       tsai      637: }
                    638: 
1.11      albertel  639: sub start_part {
1.40      albertel  640:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.95      albertel  641:   my $result='';
1.39      albertel  642:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
1.47      albertel  643:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
1.14      albertel  644:   $Apache::inputtags::part=$id;
1.18      albertel  645:   @Apache::inputtags::responselist = ();
1.51      albertel  646:   @Apache::inputtags::previous=();
1.15      www       647:   if ($target eq 'meta') {
1.16      albertel  648:     return &Apache::response::mandatory_part_meta;
1.95      albertel  649:   } elsif ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.55      www       650:     my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
1.23      albertel  651:     push (@Apache::inputtags::status,$status);
                    652:     my $expression='$external::datestatus="'.$status.'";';
                    653:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
                    654:     &Apache::run::run($expression,$safeeval);
                    655:     if ( $status eq 'CLOSED' ) {
1.21      albertel  656:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
                    657:       if ( $target eq "web" ) {
1.95      albertel  658: 	$result="<br />Part is not open to be viewed. It $accessmsg<br />";
                    659:       } elsif ( $target eq 'tex' ) {
1.124     sakharuk  660: 	$result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
1.95      albertel  661:       }
                    662:     } else {
                    663:       if ($target eq 'tex') {
1.129     sakharuk  664: 	if ($$tagstack[-2] ne 'problem') {
                    665: 	  $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
                    666: 	}
1.21      albertel  667:       }
                    668:     }
1.134     albertel  669:   } elsif ($target eq 'edit') {
                    670:       $result.=&Apache::edit::tag_start($target,$token);
                    671:       $result.=&Apache::edit::text_arg('Part ID:','id',$token).
                    672: 	  &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
                    673: 	      &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    674: 
                    675:   } elsif ($target eq 'modified') {
                    676:       my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    677: 						   'id');
                    678:       if ($constructtag) {
                    679: 	  $result = &Apache::edit::rebuild_tag($token);
                    680: 	  $result.=&Apache::edit::handle_insert();
                    681:       }
1.15      www       682:   }
1.95      albertel  683:   return $result;
1.11      albertel  684: }
                    685: 
                    686: sub end_part {
1.40      albertel  687:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.18      albertel  688:   &Apache::lonxml::debug("in end_part $target ");
1.28      albertel  689:   my $status=$Apache::inputtags::status['-1'];
1.23      albertel  690:   pop @Apache::inputtags::status;
1.19      albertel  691:   if ( $target eq 'meta' ) { return ''; }
1.37      albertel  692:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
                    693:     return &Apache::inputtags::grade;
1.28      albertel  694:   }
1.108     albertel  695:   if ($target eq 'web' || $target eq 'tex' ) {
1.121     sakharuk  696:     my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
1.120     sakharuk  697:     if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
1.108     albertel  698:     return $gradestatus;
1.37      albertel  699:   }
                    700:   return '';
1.11      albertel  701: }
1.1       albertel  702: 
1.25      albertel  703: sub start_preduedate {
1.40      albertel  704:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.113     albertel  705:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.29      albertel  706:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1.133     sakharuk  707: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' && 
                    708:         $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1.26      albertel  709:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
1.24      albertel  710:     }
                    711:   }
                    712:   return '';
                    713: }
                    714: 
1.25      albertel  715: sub end_preduedate {
1.24      albertel  716:   return '';
                    717: }
                    718: 
1.25      albertel  719: sub start_postanswerdate {
1.40      albertel  720:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.113     albertel  721:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
1.24      albertel  722:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1.26      albertel  723:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
1.24      albertel  724:     }
1.104     sakharuk  725:   } elsif ($target eq 'tex') {
                    726:       return '\vskip 0 mm \noindent';
1.24      albertel  727:   }
                    728:   return '';
                    729: }
                    730: 
1.25      albertel  731: sub end_postanswerdate {
1.24      albertel  732:   return '';
                    733: }
                    734: 
1.25      albertel  735: sub start_notsolved {
1.40      albertel  736:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.113     albertel  737:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.24      albertel  738:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    739:     &Apache::lonxml::debug("not solved has :$gradestatus:");
                    740:     if ($gradestatus =~ /^correct/) {
                    741:       &Apache::lonxml::debug("skipping");
1.26      albertel  742:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
1.24      albertel  743:     }
                    744:   }
                    745:   return '';
                    746: }
                    747: 
1.25      albertel  748: sub end_notsolved {
1.24      albertel  749:   return '';
                    750: }
                    751: 
                    752: sub start_solved {
1.40      albertel  753:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.113     albertel  754:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.24      albertel  755:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    756:     if ($gradestatus !~ /^correct/) {
                    757:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
                    758:     }
                    759:   }
                    760:   return '';
                    761: }
                    762: 
                    763: sub end_solved {
                    764:   return '';
                    765: }
1.34      albertel  766: 
                    767: sub start_startouttext {
1.40      albertel  768:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.35      albertel  769:   my @result=(''.'');
                    770:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
                    771:   return (@result);
1.34      albertel  772: }
                    773: sub end_startouttext {
1.40      albertel  774:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  775:   my $result='';
1.35      albertel  776:   my $text='';
                    777: 
1.34      albertel  778:   if ($target eq 'edit') {
1.70      albertel  779:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[-1]);
1.43      albertel  780:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
1.42      albertel  781: <td>Delete:".
                    782:   &Apache::edit::deletelist($target,$token)
1.82      albertel  783:     ."</td>
1.42      albertel  784: <td>".
                    785:   &Apache::edit::insertlist($target,$token).
1.82      albertel  786:     &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n".
1.116     albertel  787:       &Apache::edit::editfield($token->[1],$text,"",80,4);
1.35      albertel  788:   }
                    789:   if ($target eq 'modified') {
                    790:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
                    791:     $result='<startouttext />'.&Apache::edit::modifiedfield();
1.85      sakharuk  792:   }
                    793:   if ($target eq 'tex') {
                    794:       $result .= '\noindent ';
1.34      albertel  795:   }
                    796:   return $result;
                    797: }
                    798: sub start_endouttext {
1.40      albertel  799:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.34      albertel  800:   my $result='';
1.43      albertel  801:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
1.130     www       802:   if ($target eq "modified") { 
                    803:      $result='<endouttext />'.
                    804:      &Apache::edit::handle_insertafter('startouttext'); }
1.34      albertel  805:   return $result;
                    806: }
                    807: sub end_endouttext {
1.40      albertel  808:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.35      albertel  809:   my @result=('','');
                    810:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
                    811:   return (@result);
1.34      albertel  812: }
1.45      albertel  813: sub delete_startouttext {
                    814:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    815: #  my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
                    816:   my $text=$$parser['-1']->get_text("/endouttext");
1.69      albertel  817:   my $ntoken=$$parser['-1']->get_token();
                    818:   &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
                    819:   &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
1.45      albertel  820:   # Deleting 2 parallel tag pairs, but we need the numbers later to look like 
                    821:   # they did the last time round
1.69      albertel  822:   &Apache::lonxml::increasedepth($ntoken);
                    823:   &Apache::lonxml::decreasedepth($ntoken);
1.45      albertel  824:   return 1;
                    825: }
1.34      albertel  826: 
1.1       albertel  827: 1;
                    828: __END__

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