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

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

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