File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.205: download - view: text, annotated - select for diffs
Wed Aug 13 18:49:41 2003 UTC (20 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fix my breakage of Alex's regexp
- remove extra code that was badly replicating get_resource_name

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: structuretags.pm,v 1.205 2003/08/13 18:49:41 albertel Exp $
    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: #
   28: # 2/19 Guy
   29: # 6/26/2001 fixed extra web display at end of <web></web> tags
   30: # 8/17,8/18,8/20 Gerd Kortemeyer
   31: 
   32: 
   33: package Apache::structuretags; 
   34: 
   35: use strict;
   36: use Apache::lonnet;
   37: use Apache::File();
   38: use Apache::lonmenu;
   39: 
   40: BEGIN {
   41:     &Apache::lonxml::register('Apache::structuretags',('block','languageblock','instructorcomment','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext',
   42: 'simpleeditbutton','definetag'));
   43: }
   44: 
   45: sub start_web {
   46:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   47:     my $bodytext=&Apache::lonxml::get_all_text("/web",$parser);
   48:     if ($target eq 'web') {
   49: 	return $bodytext;
   50:     }
   51:     return '';
   52: }
   53: 
   54: sub end_web {
   55:     return '';
   56: }
   57: 
   58: sub start_tex {
   59:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   60:     my $result='';
   61:     my $bodytext=&Apache::lonxml::get_all_text("/tex",$parser);
   62:     if ($target eq 'tex') {
   63: 	return $bodytext.' ';
   64:     }
   65:     return $result;;
   66: }
   67: 
   68: sub end_tex {
   69:     return '';
   70: }
   71: 
   72: sub page_start {
   73:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   74:     my %found;
   75:     foreach my $taginside ($tagstack) {
   76: 	foreach my $taglookedfor ('html','body','form') {
   77: 	    if ($taginside =~ /^$taglookedfor$/i) {$found{$taglookedfor} = 1;}
   78: 	}
   79:     }
   80: 
   81:     my $result;
   82:     my $head_tag_start;
   83:     if (!defined($found{'html'})) {
   84: 	$result=&Apache::londefdef::start_html($target,$token,$tagstack,
   85: 					       $parstack,$parser,$safeeval);
   86: 	$head_tag_start='<head>'.&Apache::lonmenu::registerurl(undef,$target);
   87:     }
   88:     my $body_tag_start;
   89:     if (!defined($found{'body'})) {
   90: 	$body_tag_start='<body onLoad="'.&Apache::lonmenu::loadevents().'" '.
   91: 	    'onUnload="'.&Apache::lonmenu::unloadevents().'" ';
   92: 	my $background=&Apache::lonxml::get_param('background',$parstack,
   93: 						  $safeeval);
   94: 	if ($ENV{'browser.imagesuppress'} eq 'on') { $background=''; }
   95: 	if ($background) {
   96: 	    $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
   97: 		$background;
   98: 	    $body_tag_start.='background="'.$background.'" ';
   99: 	} else {
  100: 	    my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
  101: 						   $safeeval);
  102: 	    if (($bgcolor) && ($ENV{'browser.blackwhite'} ne 'on')) {
  103: 		$body_tag_start.='bgcolor="'.$bgcolor.'" ';
  104: 	    } else {
  105: 		$body_tag_start.='bgcolor="#ffffff"';
  106: 	    }
  107: 	}
  108: 	if ($ENV{'browser.fontenhance'} eq 'on') {
  109: 	    $body_tag_start.=' style="font-size: x-large;" ';
  110: 	}
  111: 	$body_tag_start.='>'.&Apache::lonmenu::menubuttons(undef,$target,1);
  112: 	if ($target eq 'web' && $ENV{'request.state'} ne 'construct') {
  113: 	    my ($symb,undef,undef,undef,$publicuser)=
  114: 		&Apache::lonxml::whichuser();
  115: 	    if ($symb eq '' && !$publicuser) {
  116: 		my $help = &Apache::loncommon::help_open_topic("Ambiguous_Reference");
  117: 		$help="Browsing resource, all submissions are temporary.<br />";
  118: 		$body_tag_start.=$help;
  119: 	    }
  120: 	}
  121:     }
  122:     my $form_tag_start;
  123:     if (!defined($found{'form'})) {
  124: 	$form_tag_start='<form name="lonhomework" method="POST" action="'.
  125: 	    $ENV{'request.uri'}.'">';
  126:     }
  127:     return ($result,$head_tag_start,$body_tag_start,$form_tag_start);
  128: }
  129: 
  130: #use Time::HiRes();
  131: sub get_resource_name {
  132:     my ($parstack,$safeeval)=@_;
  133:     if (defined($Apache::lonhomework::name)) {
  134: 	return $Apache::lonhomework::name;
  135:     }
  136:     my $name=&Apache::lonnet::gettitle();
  137:     if ($name eq '') {
  138: 	$name=&Apache::lonnet::EXT('resource.title');
  139: 	if ($name eq 'con_lost') { $name = ''; }
  140:     }
  141:     if ($name!~/\S+/) {
  142: 	$name=$ENV{'request.uri'};
  143: 	$name=~s-.*/([^/]+)$-$1-;
  144:     }
  145:     $Apache::lonhomework::name=$name;
  146:     return $name;
  147: }
  148: 
  149: sub setup_rndseed {
  150:     my ($safeeval)=@_;
  151:     my $rndseed;
  152:     my ($symb)=&Apache::lonxml::whichuser();
  153:     if ($ENV{'request.state'} eq "construct" || $symb eq '') {
  154: 	$rndseed=$ENV{'form.rndseed'};
  155: 	if (!$rndseed) {
  156: 	    $rndseed=$Apache::lonhomework::history{'rndseed'};
  157: 	    if (!$rndseed) {
  158: 		$rndseed=time;
  159: 		$ENV{'form.rndseed'}=$rndseed;
  160: 	    }
  161: 	}
  162: 	if ($ENV{'form.resetdata'} eq 'New Problem Variation' ||
  163: 	    $ENV{'form.newrandomization'} eq 'New Randomization') {
  164: 	    srand(time);
  165: 	    $rndseed=int(rand(2100000000));
  166: 	    $ENV{'form.rndseed'}=$rndseed;
  167: 	    delete($ENV{'form.resetdata'});
  168: 	    delete($ENV{'form.newrandomization'});
  169: 	}
  170: 	if (defined($rndseed) && $rndseed ne int($rndseed)) {
  171: 	   $rndseed=join(',',&Math::Random::random_seed_from_phrase($rndseed));
  172:         }
  173: 	&Apache::lonxml::debug("Setting rndseed to $rndseed");
  174: 	&Apache::run::run('$external::randomseed='.$rndseed.';',$safeeval);
  175:     }
  176:     return $rndseed;
  177: }
  178: 
  179: sub problem_edit_header {
  180:     return '<input type="hidden" name="submitted" value="edit" />
  181:        <input type="hidden" name="problemmode" value="Edit" />
  182:        <input type="submit" name="problemmode" value="Discard Edits and View" />
  183:        <input type="submit" name="problemmode" value="EditXML" />
  184:        <input type="submit" name="Undo" value="undo" /> <hr />
  185:        <input type="submit" name="submit" value="Submit Changes and Edit" />
  186:        <input type="submit" name="submit" value="Submit Changes and View" /><br /><p>&nbsp;</p><table border="0"><tr><td bgcolor="#DDDDDD">
  187:       ';
  188: }
  189: 
  190: sub problem_edit_footer {
  191:     return '</td></tr></table><br /><input type="submit" name="submit" value="Submit Changes and Edit" />
  192:     <input type="submit" name="submit" value="Submit Changes and View" />';
  193: }
  194: 
  195: sub problem_web_to_edit_header {
  196:     my ($rndseed)=@_;
  197:     my $result.='<input type="hidden" name="problemmode" value="View" />
  198:              <input type="submit" name="problemmode" value="Edit" />
  199:              <input type="submit" name="problemmode" value="EditXML" />
  200:              <input type="submit" name="newrandomization" value="New Randomization" />
  201:              <input type="submit" name="resetdata" value="Reset Submissions" />
  202:              <nobr><input type="submit" name="changerandseed" value="Change Random Seed To:" />
  203:               <input type="text" name="rndseed" width="10" value="'.
  204: 	       $rndseed.'"
  205:            onChange="javascript:document.lonhomework.changerandseed.click()" /></nobr>
  206:              <input type="checkbox" name="showallfoils" ';
  207:     if (defined($ENV{'form.showallfoils'})) { $result.='checked="on"'; }
  208:     $result.= ' />&nbsp;Show&nbsp;All&nbsp;Foils
  209:              <hr />';
  210:     my $numtoanalyze=$ENV{'form.numtoanalyze'};
  211:     if (!$numtoanalyze) { $numtoanalyze=20; }
  212:     $result.= '<input type="submit" name="problemmode" value="Calculate answers" /> for
  213:              <input type="text" name="numtoanalyze" value="'.
  214: 		 $numtoanalyze.'" size="5" /> versions of this problem.'.
  215: 		     &Apache::loncommon::help_open_topic("Analyze_Problem",
  216: 						     '',undef,undef,300).
  217: 		     '<hr />';
  218:     return $result;
  219: }
  220: 
  221: sub initialize_storage {
  222:     %Apache::lonhomework::results=();
  223:     %Apache::lonhomework::history=();
  224:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  225:     if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
  226: 	%Apache::lonhomework::history=
  227: 	    &Apache::lonnet::tmprestore($ENV{'request.uri'},'',$domain,$name);
  228: 	my ($temp)=keys %Apache::lonhomework::history ;
  229: 	&Apache::lonxml::debug("Return message of $temp");
  230:     } else {
  231: 	%Apache::lonhomework::history=
  232: 	    &Apache::lonnet::restore($symb,$courseid,$domain,$name);
  233:     }
  234:     #ignore error conditions
  235:     my ($temp)=keys %Apache::lonhomework::history ;
  236:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
  237: }
  238: 
  239: # -------------------------------------------------------------finalize_storage
  240: # Stores away the result has to a student's environment
  241: # checks form.grade_ for specific values, other wises stores
  242: # to the running users environment
  243: sub finalize_storage {
  244:     my $result;
  245:     my ($temp) = keys %Apache::lonhomework::results;
  246:     if ( $temp ne '' ) {
  247: 	my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  248: 	if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
  249: 	    $Apache::lonhomework::results{'rndseed'}=$ENV{'form.rndseed'};
  250: 	    $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
  251: 					$ENV{'request.uri'},'',$domain,$name);
  252: 	    &Apache::lonxml::debug('Construct Store return message:'.$result);
  253: 	} else {
  254: 	    $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
  255: 					    $symb,$courseid,$domain,$name);
  256: 	    &Apache::lonxml::debug('Store return message:'.$result);
  257: 	}
  258:     }
  259:     return $result;
  260: }
  261: 
  262: sub checkout_msg {
  263:     return (<<ENDCHECKOUT);
  264: <h2>The resource needs to be checked out</h2>
  265: As a resource gets checked out, a unique timestamped ID is given to it, and a
  266: permanent record is left in the system.<p />
  267: <font color=red>
  268: Checking out resources is subject to course policies, and may exclude future
  269: credit even if done erroneously.<p />
  270: </font>
  271: <form name="checkout" method="POST" action="$ENV{'request.uri'}">
  272: <input type="hidden" name="doescheckout" value="yes" />
  273: <input type="button" name="checkoutbutton" value="Check out Exam for Viewing" onClick="javascript:if (confirm('Check out Exam?')) { document.checkout.submit(); }" />
  274: </form>
  275: ENDCHECKOUT
  276: }
  277: 
  278: sub init_problem_globals {
  279:     my ($type)=@_;
  280:     #initialize globals
  281:     if ($type eq 'problem') {
  282: 	$Apache::inputtags::part='0';
  283: 	@Apache::inputtags::partlist=('0');
  284: 	$Apache::lonhomework::problemstatus=
  285: 	    &Apache::lonnet::EXT('resource.0.problemstatus');
  286:     } else {
  287: 	$Apache::inputtags::part='';
  288: 	@Apache::inputtags::partlist=();
  289: 	$Apache::lonhomework::problemstatus='';	
  290:     }
  291:     @Apache::inputtags::responselist = ();
  292:     @Apache::inputtags::importlist = ();
  293:     @Apache::inputtags::previous=();
  294:     @Apache::inputtags::previous_version=();
  295:     $Apache::structuretags::printanswer='No';
  296:     @Apache::structuretags::whileconds=();
  297:     @Apache::structuretags::whilebody=();
  298:     @Apache::structuretags::whileline=();
  299:     $Apache::lonhomework::scantronmode=0;
  300:     undef($Apache::lonhomework::name);
  301: 
  302: }
  303: 
  304: sub reset_problem_globals {
  305:     my ($type)=@_;
  306:     undef(%Apache::lonhomework::history);
  307:     undef(%Apache::lonhomework::results);
  308:     undef($Apache::inputtags::part);
  309:     undef($Apache::lonhomework::parsing_a_problem);
  310:     undef($Apache::lonhomework::name);
  311: }
  312: 
  313: sub start_problem {
  314:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  315: 
  316:     if ( $Apache::inputtags::part ne '' ||
  317: 	 $Apache::lonhomework::parsing_a_problem) {
  318: 	&Apache::lonxml::error('Only one &lt;problem&gt; allowed in a .problem file');
  319: 	#my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
  320: 	return '';
  321:     }
  322: 
  323:     $Apache::lonhomework::parsing_a_problem=1;
  324:     &init_problem_globals('problem');
  325: 
  326:     if (defined($ENV{'scantron.maxquest'})) {
  327: 	$Apache::lonhomework::scantronmode=1;
  328:     }
  329: 
  330:     if ($target ne 'analyze') {
  331: 	&initialize_storage();
  332: 	if ($target eq 'web') {
  333: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
  334: 	}
  335: 	$Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
  336: 	&Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
  337:     }
  338:     if ($Apache::lonhomework::type eq '' ) {
  339: 	my $uri=$ENV{'request.uri'};
  340: 	if ($uri=~/\.(\w+)$/) {
  341: 	    $Apache::lonhomework::type=$1;
  342: 	    &Apache::lonxml::debug("Using type of $1");
  343: 	} else {
  344: 	    $Apache::lonhomework::type='problem';
  345: 	    &Apache::lonxml::debug("Using default type, problem, :$uri:");
  346: 	}
  347:     }
  348: 
  349:     #added vars to the scripting enviroment
  350:     my $expression='$external::part='.$Apache::inputtags::part.';';
  351:     &Apache::run::run($expression,$safeeval);
  352:     my $status;
  353:     my $accessmsg;
  354: 
  355:     #should get back a <html> or the neccesary stuff to start XML/MathML
  356:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  357: 	&page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  358:     if ($target eq 'tex' and $ENV{'request.symb'} =~ m/\.page_/) {$result='';}
  359: 
  360:     if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
  361:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  362: 	$target eq 'tex') {
  363: 	#handle exam checkout
  364: 	if ($Apache::lonhomework::type eq 'exam') {
  365: 	    my $token=
  366: 		$Apache::lonhomework::history{"resource.0.outtoken"};
  367: 	    if (($ENV{'form.doescheckout'}) && (!$token)) {
  368: 		$token=&Apache::lonxml::maketoken();
  369: 		$Apache::lonhomework::history{"resource.0.outtoken"}=
  370: 		    $token;
  371: 	    }
  372: 	    $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
  373: 	}
  374: 
  375: 	#handle rand seed in construction space
  376: 	my $rndseed=&setup_rndseed($safeeval);
  377: 	my ($symb)=&Apache::lonxml::whichuser();
  378: 	if ($ENV{'request.state'} ne "construct" && $symb eq '') {
  379: 	    $form_tag_start.='<input type="hidden" name="rndseed" value="'.
  380: 		$rndseed.'" />'.
  381: 		    '<input type="submit" name="resetdata"
  382:                              value="New Problem Variation" />'.
  383: 		    '<input type="hidden" name="username"
  384:                              value="'.$ENV{'form.username'}.'" /> <br />';
  385: 	}
  386: 	($status,$accessmsg) = &Apache::lonhomework::check_access('0');
  387: 	push (@Apache::inputtags::status,$status);
  388: 	my $expression='$external::datestatus="'.$status.'";';
  389: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
  390: 	&Apache::run::run($expression,$safeeval);
  391: 	&Apache::lonxml::debug("Got $status");
  392: 	if (( $status eq 'CLOSED' ) ||
  393: 	    ( $status eq 'UNCHECKEDOUT') ||
  394: 	    ( $status eq 'BANNED') ||
  395: 	    ( $status eq 'UNAVAILABLE')) {
  396: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
  397: 	    if ( $target eq "web" ) {
  398: 		$result.= $head_tag_start.'</head>';
  399: 		my $msg=$body_tag_start;
  400: 		if ($status eq 'UNAVAILABLE') {
  401: 		    $result.='<h1>Unable to determine if this resource is open due to network problems. Please try again later.</h1>';
  402: 		} else {
  403: 		    $result.='<h1>Not open to be viewed</h1>';
  404: 		}
  405: 		if ($status eq 'CLOSED') {
  406: 		    $msg.='The problem '.$accessmsg;
  407: 		} elsif ($status eq 'UNCHECKEDOUT') {
  408: 		    $msg.=&checkout_msg;
  409: 		}
  410: 		$result.=$msg.'<br />';
  411: 	    } elsif ($target eq 'tex') {
  412: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
  413: 		if ($status eq 'UNAVAILABLE') {
  414: 		    $result.='Unable to determine if this resource is open due to network problems. Please try again later.\vskip 0 mm ';
  415: 		} else {
  416: 		    $result.="Problem is not open to be viewed. It $accessmsg \\vskip 0 mm ";
  417: 		}
  418: 	    }
  419: 	} elsif ($target eq 'web') {
  420: 	    my $name= &get_resource_name($parstack,$safeeval);
  421: 	    if ($status eq 'CAN_ANSWER') {
  422: 		# create a page header and exit
  423: 		$result.="$head_tag_start<title>$name</title></head>
  424:               $body_tag_start \n $form_tag_start".	
  425: 		  '<input type="hidden" name="submitted" value="yes" />';
  426: 		if ($ENV{'request.state'} eq "construct") {
  427: 		    $result.= &problem_web_to_edit_header($ENV{'form.rndseed'});
  428: 		}
  429: 		# if we are viewing someone else preserve that info
  430: 		if (defined $ENV{'form.grade_symb'}) {
  431: 		    foreach my $field ('symb','courseid','domain','username') {
  432: 			$result .= '<input type="hidden" name="grade_'.$field.
  433: 			    '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
  434: 		    }
  435: 		}
  436: 	    } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
  437: 		     || $status eq 'CLOSED' || $status eq 'UNAVALAILABLE') {
  438: 		$result.=$head_tag_start.
  439: 		    "<title>$name</title></head>\n$body_tag_start\n";
  440: 	    }
  441: 	} elsif ($target eq 'tex') {
  442: 	    my $startminipage = '';
  443: 	    if (not $ENV{'form.problem_split'}=~/yes/) {
  444: 		$startminipage = '\begin{minipage}{\textwidth}';
  445: 	    }
  446: 	    my $id = $Apache::inputtags::part;
  447: 	    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  448: 	    my $allkeys=&Apache::lonnet::metadata($ENV{'request.uri'},'keys');
  449: 	    my @allkeys = split /,/,$allkeys;
  450: 	    my $allow_print_points = 0;
  451: 	    foreach my $partial_key (@allkeys) {
  452: 		if ($partial_key=~m/weight/) {
  453: 		    $allow_print_points++;
  454: 		}
  455: 	    }
  456: 	    my $duedate = &Apache::lonnet::EXT("resource.$id.duedate"); 
  457: 	    $duedate = POSIX::strftime("%c",localtime($duedate));
  458: 	    my $temp_file;
  459: 	    my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
  460: 	    if (-e $filename) {
  461: 		$temp_file = Apache::File->new($filename);
  462: 	    } else {
  463: 		$temp_file = Apache::File->new('>>'.$filename);
  464: 	    }
  465: 	    my @due_file_content = <$temp_file>;
  466: 	    my $due_file_content = $due_file_content[$#due_file_content];
  467: 	    chomp $due_file_content;
  468: 	    my $name_of_resourse= &get_resource_name($parstack,$safeeval);
  469: 	    if ($due_file_content ne $duedate) {
  470: 		$temp_file = Apache::File->new('>'.$filename);
  471: 		print $temp_file "$duedate\n";
  472: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  473: 		    if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
  474: 			$result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND}\addcontentsline{toc}{subsection}{'.$name_of_resourse.'} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm\noindent '.$startminipage;	
  475: 		    } else {
  476: 			$result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND}\addcontentsline{toc}{subsection}{'.$name_of_resourse.'} \noindent \vskip 1 mm \noindent'.$startminipage;
  477: 			if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  478: 		    }
  479: 		} else {
  480: 		    $result .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
  481: 		}
  482: 	    } else {
  483: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  484: 		    $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND}\addcontentsline{toc}{subsection}{'.$name_of_resourse.'} \noindent \vskip 1 mm\noindent'.$startminipage;	
  485: 		    if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  486: 		} else {
  487: 		    $result .= '\vskip 1mm \\\\\\\\';
  488: 		}
  489: 	    }
  490: 	}
  491:     } elsif ($target eq 'edit') {
  492: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  493: 	    &problem_edit_header();
  494: 	my $temp=&Apache::edit::insertlist($target,$token);
  495: 	$result.=$temp;
  496:     } elsif ($target eq 'modified') {
  497: 	$result=$token->[4];
  498: 	$result.=&Apache::edit::handle_insert();
  499:     } else {
  500: 	# page_start returned a starting result, delete it if we don't need it
  501: 	$result = '';
  502:     }
  503:     return $result;
  504: }
  505: 
  506: sub end_problem {
  507:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  508:     my $result='';
  509:     my $status=$Apache::inputtags::status['-1'];
  510:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  511: 	$target eq 'tex') {
  512: 	if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
  513: 	     $status eq 'CAN_ANSWER' ) {
  514: 	    # if part is zero, no <part>s existed, so we need to the grading
  515: 	    &Apache::inputtags::grade;
  516: 	} elsif ( ($target eq 'web' || $target eq 'tex') &&
  517: 		  $Apache::inputtags::part eq '0' &&
  518: 		  $status ne 'UNCHECKEDOUT') {
  519: 	    # if part is zero, no <part>s existed, so we need show the current
  520: 	    # grading status
  521: 	    my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
  522: 	    $result.= $gradestatus;
  523: 	}
  524: 	if (
  525: 	    (($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
  526: 	    ($target eq 'answer') || ($target eq 'tex')
  527: 	   ) {
  528: 	    if ($status eq 'CAN_ANSWER') {
  529: 		if ($target ne 'tex' &&
  530: 		    $ENV{'form.answer_output_mode'} ne 'tex') {
  531: 		    $result.="</form></body>\n";
  532: 		}
  533: 	    } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
  534: 		     $status eq 'UNCHECKEDOUT' ) {
  535: 		if ($target ne 'tex' &&
  536: 		    $ENV{'form.answer_output_mode'} ne 'tex') {
  537: 		    $result.="</body>\n";
  538: 		}
  539: 	    }
  540: 	    if ($target eq 'web') {
  541: 		$result.=&Apache::lonxml::xmlend();
  542: 	    } elsif ($target eq 'tex') {
  543: 		my $endminipage = '';
  544: 		if (not $ENV{'form.problem_split'}=~/yes/) {
  545: 		    $endminipage = '\end{minipage}';
  546: 		}
  547: 		$result .= '\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
  548: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  549: 		    $result .= $endminipage.'\end{document} ';
  550: 		} else {
  551: 		    $result .= '';
  552: 		}
  553: 	    }
  554: 	}
  555: 	if ($target eq 'grade') {
  556: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  557: 	    &finalize_storage();
  558: 	}
  559: 	if ($target eq 'answer' && ($ENV{'request.state'} eq 'construct')
  560: 	    && $ENV{'form.answer_output_mode'} ne 'tex') {
  561: 	    $result.='</html>'; # normally we get it from xmlend, but in CSTR
  562: 	                        # we always show answer mode too.
  563: 	}
  564:     } elsif ($target eq 'meta') {
  565: 	if ($Apache::inputtags::part eq '0') {
  566: 	    @Apache::inputtags::response=();
  567: 	    $result=&Apache::response::mandatory_part_meta;
  568: 	}
  569:     } elsif ($target eq 'edit') {
  570: 	&Apache::lonxml::debug("in end_problem with $target, edit");
  571: 	$result = &problem_edit_footer();
  572:     }
  573: 
  574:     if ($ENV{'request.state'} eq 'construct' && $target eq 'web') {
  575: 	&Apache::inputtags::check_for_duplicate_ids();
  576:     }
  577: 
  578:     &reset_problem_globals('problem');
  579: 
  580:     return $result;
  581: }
  582: 
  583: 
  584: sub start_library {
  585:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  586:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
  587:     if ($$tagstack[0] eq 'library') { &init_problem_globals('library') };
  588:     if ($target eq 'edit') {
  589: 	($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  590: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  591: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  592: 	    &problem_edit_header();
  593: 	my $temp=&Apache::edit::insertlist($target,$token);
  594: 	$result.=$temp;
  595:     } elsif ($target eq 'modified') {
  596: 	$result=$token->[4];
  597: 	$result.=&Apache::edit::handle_insert();
  598:     } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
  599: 	     $ENV{'request.state'} eq "construct" ) {
  600: 	($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  601: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  602: 	my $name=&get_resource_name($parstack,$safeeval);
  603: 	my $rndseed=&setup_rndseed($safeeval);
  604: 	$result.="$head_tag_start<title>$name</title></head>
  605:               $body_tag_start \n $form_tag_start".	
  606: 		  '<input type="hidden" name="submitted" value="yes" />';
  607: 	$result.=&problem_web_to_edit_header($rndseed);
  608:     }
  609:     return $result;
  610: }
  611: 
  612: sub end_library {
  613:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  614:     my $result='';
  615:     if ($target eq 'edit') {
  616: 	$result=&problem_edit_footer();
  617:     } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
  618: 	     $ENV{'request.state'} eq "construct") {
  619: 	$result.='</form></body>'.&Apache::lonxml::xmlend();
  620:     }
  621:     if ($$tagstack[0] eq 'library') { &reset_problem_globals('library') };
  622:     return $result;
  623: }
  624: 
  625: sub start_definetag {
  626:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  627: 
  628:     my $result;
  629: 
  630:     my $name = $token->[2]->{'name'};
  631:     my $skip=&Apache::lonxml::get_all_text("/definetag",$parser);
  632:     if ($name=~/^\//) {
  633: 	$result=
  634:  '<br /><table bgcolor="#FFBBBB"><tr><th>END <tt>'.$name.'</tt></th></tr>';
  635:     } else {
  636: 	$result=
  637:  '<br /><table bgcolor="#BBFFBB"><tr><th>BEGIN <tt>'.$name.'</tt></th></tr>';
  638:     }
  639:     $skip=~s/\</\&lt\;/gs;
  640:     $skip=~s/\>/\&gt\;/gs;
  641:     $result.='<tr><td><pre>'.$skip.'</pre></td></tr></table>';
  642:     return $result;
  643: }
  644: 
  645: sub end_definetag {
  646:     return '';
  647: }
  648: 
  649: sub start_block {
  650:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  651: 
  652:     my $result;
  653: 
  654:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  655: 	$target eq 'tex' || $target eq 'analyze') {
  656: 	my $code = $token->[2]->{'condition'};
  657: 	if (defined($code)) {
  658: 	    if (!$Apache::lonxml::default_homework_loaded) {
  659: 		&Apache::lonxml::default_homework_load($safeeval);
  660: 	    }
  661: 	    $result = &Apache::run::run($code,$safeeval);
  662: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
  663: 	} else {
  664: 	    $result='1';
  665: 	}
  666: 	if ( ! $result ) {
  667: 	    my $skip=&Apache::lonxml::get_all_text("/block",$parser,$style);
  668: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
  669: 	}
  670: 	$result='';
  671:     } elsif ($target eq 'edit') {
  672: 	$result .=&Apache::edit::tag_start($target,$token);
  673: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
  674: 					  $token,40);
  675: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  676:     } elsif ($target eq 'modified') {
  677: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  678: 						     $safeeval,'condition');
  679: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  680:     }
  681:     return $result;
  682: }
  683: 
  684: sub end_block {
  685:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  686:     my $result;
  687:     if ($target eq "edit") {
  688: 	$result.= &Apache::edit::tag_end($target,$token,'');
  689:     }
  690:     return $result;
  691: }
  692: 
  693: sub start_languageblock {
  694:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  695: 
  696:     my $result;
  697: 
  698:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  699: 	$target eq 'tex' || $target eq 'analyze') {
  700: 	my $include = $token->[2]->{'include'};
  701: 	my $exclude = $token->[2]->{'exclude'};
  702:         my %languages=&Apache::loncommon::display_languages();
  703:         $result='1';
  704: 	if ($include) {
  705:             $result='';
  706:             foreach (split(/\,/,$include)) {
  707:                 if ($languages{$_}) { $result='1'; }
  708:             }
  709: 	}
  710:         if ($exclude) {
  711:             foreach (split(/\,/,$exclude)) {
  712:                 if ($languages{$_}) { $result='0'; }
  713:             }
  714: 	}
  715: 	if ( ! $result ) {
  716: 	    my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser,
  717: 						   $style);
  718: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
  719: 	}
  720: 	$result='';
  721:     } elsif ($target eq 'edit') {
  722: 	$result .=&Apache::edit::tag_start($target,$token);
  723: 	$result .=&Apache::edit::text_arg('Include Language:','include',
  724: 					  $token,40);
  725: 	$result .=&Apache::edit::text_arg('Exclude Language:','exclude',
  726: 					  $token,40);
  727: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  728:     } elsif ($target eq 'modified') {
  729: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  730: 						     $safeeval,'include',
  731: 						     'exclude');
  732: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  733:     }
  734:     return $result;
  735: }
  736: 
  737: sub end_languageblock {
  738:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  739:     my $result;
  740:     if ($target eq "edit") {
  741: 	$result.= &Apache::edit::tag_end($target,$token,'');
  742:     }
  743:     return $result;
  744: }
  745: 
  746: sub start_instructorcomment {
  747:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  748: 
  749:     my $result;
  750: 
  751:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  752: 	$target eq 'tex' || $target eq 'analyze') {
  753:         $result=($ENV{'request.role'}=~/^(in|cc|au|ca|li)/);
  754: 	if ( ! $result ) {
  755: 	    my $skip=&Apache::lonxml::get_all_text("/instructorcomment",
  756: 						   $parser,$style);
  757: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
  758: 	}
  759: 	$result='';
  760:     } elsif ($target eq 'edit') {
  761: 	$result .=&Apache::edit::tag_start($target,$token);
  762: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  763:     }
  764:     return $result;
  765: }
  766: 
  767: sub end_instructorcomment {
  768:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  769:     my $result;
  770:     if ($target eq "edit") {
  771: 	$result.= &Apache::edit::tag_end($target,$token,'');
  772:     }
  773:     return $result;
  774: }
  775: 
  776: sub start_while {
  777:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  778: 
  779:     my $result;
  780:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  781: 	$target eq 'tex' || $target eq 'analyze') {
  782: 	my $code = $token->[2]->{'condition'};
  783: 
  784: 	push( @Apache::structuretags::whileconds, $code);
  785: 	if (!$Apache::lonxml::default_homework_loaded) {
  786: 	    &Apache::lonxml::default_homework_load($safeeval);
  787: 	}
  788: 	my $result = &Apache::run::run($code,$safeeval);
  789: 	my $bodytext=&Apache::lonxml::get_all_text("/while",$parser);
  790: 	push( @Apache::structuretags::whilebody, $bodytext);
  791: 	push( @Apache::structuretags::whileline, $token->[5]);
  792: 	&Apache::lonxml::debug("s code $code got -$result-");
  793: 	if ( $result ) {
  794: 	    &Apache::lonxml::newparser($parser,\$bodytext);
  795: 	}
  796:     } elsif ($target eq 'edit') {
  797: 	$result .=&Apache::edit::tag_start($target,$token);
  798: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
  799: 					  $token,40);
  800: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  801:     } elsif ($target eq 'modified') {
  802: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  803: 						     $safeeval,'condition');
  804: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  805:     }
  806:     return $result;
  807: }
  808: 
  809: sub end_while {
  810:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  811:     my $result;
  812: 
  813:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  814: 	$target eq 'tex' || $target eq 'analyze') {
  815: 	my $code = pop(@Apache::structuretags::whileconds);
  816: 	my $bodytext = pop(@Apache::structuretags::whilebody);
  817: 	my $line = pop(@Apache::structuretags::whileline);
  818: 	my $return = &Apache::run::run($code,$safeeval);
  819: 	my $starttime=time;
  820: 	my $error=0;
  821: 	while ($return) {
  822: 	    if (time-$starttime >
  823: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}) {
  824: 		$return = 0; $error=1; next;
  825: 	    }
  826: 	    $result.=&Apache::scripttag::xmlparse($bodytext);
  827: 	    $return = &Apache::run::run($code,$safeeval);
  828: 	}
  829: 	if ($error) {
  830: 	    &Apache::lonxml::error('<pre>Code ran too long. It ran for more than '.$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds occured while running &lt;while$gt; on line '.$line.'</pre>');
  831: 	}
  832:     } elsif ($target eq "edit") {
  833: 	$result.= &Apache::edit::tag_end($target,$token,'');
  834:     }
  835:     return $result;
  836: }
  837: 
  838: # <randomlist show="1">
  839: #  <tag1>..</tag1>
  840: #  <tag2>..</tag2>
  841: #  <tag3>..</tag3>
  842: #  ...
  843: # </randomlist>
  844: sub start_randomlist {
  845:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  846:     my $result;
  847:     if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
  848: 	$target eq 'tex' || $target eq 'analyze') {
  849: 	my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
  850: 	my $b_parser= HTML::TokeParser->new(\$body);
  851: 	my $b_tok;
  852: 	my @randomlist;
  853: 	my $list_item;
  854: 	while($b_tok = $b_parser->get_token() ) {
  855: 	    if($b_tok->[0] eq 'S') { # start tag
  856: 		# get content of the tag until matching end tag
  857: 		# get all text upto the matching tag
  858: 		# and push the content into @randomlist
  859: 		$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
  860: 							   $b_parser);
  861: 		$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  862: 		push(@randomlist,$list_item);
  863: 		#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
  864:                 #         $list_item</b>";
  865: 	    }
  866: 	    if($b_tok->[0] eq 'T') { # text
  867: 		# what to do with text in between tags?
  868: 		#  print "<b>TEXT $b_tok->[1]</b><br />";
  869: 	    }
  870: 	    # if($b_tok->[0] eq 'E') { # end tag, should not happen
  871: 	    #  print "<b>END-TAG $b_tok->[1]</b><br />";
  872: 	    # }
  873: 	}
  874: 	my @idx_arr = (0 .. $#randomlist);
  875: 	&Apache::structuretags::shuffle(\@idx_arr);
  876: 	my $bodytext = '';
  877: 	my $show=$#randomlist;
  878: 	my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
  879: 	$showarg--;
  880: 	if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
  881: 	for(0 .. $show) {
  882: 	    $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  883: 	}
  884: 	&Apache::lonxml::newparser($parser,\$bodytext);
  885:     } elsif ($target eq 'edit' ) {
  886: 	$result .=&Apache::edit::tag_start($target,$token);
  887: 	$result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
  888: 					   $token,5);
  889: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  890:     } elsif ($target eq 'modified' ) {
  891: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  892: 						     $safeeval,'show');
  893: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  894:     }
  895:     return $result;
  896: }
  897: 
  898: sub shuffle {
  899:     my $a=shift;
  900:     my $i;
  901:     if (defined(@$a)) {
  902: 	&Apache::response::setrandomnumber();
  903: 	for($i=@$a;--$i;) {
  904: 	    my $j=int(&Math::Random::random_uniform() * ($i+1));
  905: 	    next if $i == $j;
  906: 	    @$a[$i,$j] = @$a[$j,$i];
  907: 	}
  908:     }
  909: }
  910: 
  911: sub end_randomlist {
  912:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  913:     my $result;
  914:     if ($target eq 'edit' ) {
  915: 	$result=&Apache::edit::tag_end($target,$token,
  916: 				       'End Randomly Parsed Block');
  917:     }
  918:     return $result;
  919: }
  920: 
  921: sub start_part {
  922:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  923:     my $result='';
  924:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  925:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  926:     $Apache::inputtags::part=$id;
  927:     push(@Apache::inputtags::partlist,$id);
  928:     @Apache::inputtags::response=();
  929:     @Apache::inputtags::previous=();
  930:     @Apache::inputtags::previous_version=();
  931:     $Apache::lonhomework::problemstatus=
  932: 	&Apache::lonnet::EXT("resource.$id.problemstatus");
  933:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
  934: 
  935:     if ($target eq 'meta') {
  936: 	return &Apache::response::mandatory_part_meta;
  937:     } elsif ($target eq 'web' || $target eq 'grade' ||
  938: 	     $target eq 'answer' || $target eq 'tex') {
  939: 	if ($hidden) {
  940: 	    my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
  941: 	} else {
  942: 	    my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  943: 	    push (@Apache::inputtags::status,$status);
  944: 	    my $expression='$external::datestatus="'.$status.'";';
  945: 	    $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  946: 	    &Apache::run::run($expression,$safeeval);
  947: 	    if ( $status eq 'CLOSED' ) {
  948: 		my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
  949: 		if ( $target eq "web" ) {
  950: 		    $result="<br />Part is not open to be viewed. It $accessmsg<br />";
  951: 		} elsif ( $target eq 'tex' ) {
  952: 		    if (not $ENV{'form.problem_split'}=~/yes/) {
  953: 			$result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
  954: 		    } else {
  955: 			$result="\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\";
  956: 		    }
  957: 		}
  958: 	    } else {
  959: 		if ($target eq 'tex') {
  960: 		    if (not $ENV{'form.problem_split'}=~/yes/) {
  961: 			$result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
  962: 		    }
  963: 		    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  964: 		    if ($Apache::lonhomework::type eq 'exam') { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  965: 		}
  966: 	    }
  967: 	}
  968:     } elsif ($target eq 'edit') {
  969: 	$result.=&Apache::edit::tag_start($target,$token);
  970: 	$result.=&Apache::edit::text_arg('Part ID:','id',$token).
  971: 	    &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
  972: 		&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  973:     } elsif ($target eq 'modified') {
  974: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  975: 						     $safeeval,'id');
  976: 	if ($constructtag) {
  977: 	    $result = &Apache::edit::rebuild_tag($token);
  978: 	    $result.=&Apache::edit::handle_insert();
  979: 	}
  980:     }
  981:     return $result;
  982: }
  983: 
  984: sub end_part {
  985:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  986:     &Apache::lonxml::debug("in end_part $target ");
  987:     my $status=$Apache::inputtags::status['-1'];
  988:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
  989:     my $result='';
  990:     if ( $target eq 'meta' ) {
  991: 	$result='';
  992:     } elsif ( $target eq 'grade' && $status eq 'CAN_ANSWER' && !$hidden) {
  993: 	$result=&Apache::inputtags::grade;
  994:     } elsif (($target eq 'web' || $target eq 'tex') && !$hidden ) {
  995: 	my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
  996: 							$target);
  997: 	if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
  998: 	$result=$gradestatus;
  999:     } elsif ($target eq 'edit') {
 1000: 	$result=&Apache::edit::end_table();
 1001:     }
 1002:     pop @Apache::inputtags::status;
 1003:     $Apache::inputtags::part='';
 1004:     return $result;
 1005: }
 1006: 
 1007: sub start_preduedate {
 1008:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1009:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
 1010: 	if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
 1011: 	    $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' &&
 1012: 	    $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
 1013: 	    &Apache::lonxml::get_all_text("/preduedate",$parser);
 1014: 	}
 1015:     }
 1016:     return '';
 1017: }
 1018: 
 1019: sub end_preduedate {
 1020:     return '';
 1021: }
 1022: 
 1023: sub start_postanswerdate {
 1024:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1025:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
 1026: 	if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
 1027: 	    &Apache::lonxml::get_all_text("/postanswerdate",$parser);
 1028: 	}
 1029:     } elsif ($target eq 'tex') {
 1030: 	return '\vskip 0 mm \noindent';
 1031:     }
 1032:     return '';
 1033: }
 1034: 
 1035: sub end_postanswerdate {
 1036:     return '';
 1037: }
 1038: 
 1039: sub start_notsolved {
 1040:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1041:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
 1042: 	$target eq 'tex') {
 1043: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
 1044: 	&Apache::lonxml::debug("not solved has :$gradestatus:");
 1045: 	if ($gradestatus =~ /^correct/) {
 1046: 	    &Apache::lonxml::debug("skipping");
 1047: 	    &Apache::lonxml::get_all_text("/notsolved",$parser);
 1048: 	}
 1049:     }
 1050:     return '';
 1051: }
 1052: 
 1053: sub end_notsolved {
 1054:     return '';
 1055: }
 1056: 
 1057: sub start_solved {
 1058:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1059:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
 1060: 	$target eq 'tex') {
 1061: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
 1062: 	if ($gradestatus !~ /^correct/) {
 1063: 	    &Apache::lonxml::get_all_text("/solved",$parser);
 1064: 	}
 1065:     }
 1066:     return '';
 1067: }
 1068: 
 1069: sub end_solved {
 1070:     return '';
 1071: }
 1072: 
 1073: sub start_startouttext {
 1074:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1075:     my @result=(''.'');
 1076:     if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
 1077:     return (@result);
 1078: }
 1079: 
 1080: sub end_startouttext {
 1081:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1082:     my $result='';
 1083:     my $text='';
 1084: 
 1085:     if ($target eq 'edit') {
 1086: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser);
 1087: 	$result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
 1088: <td>Delete:".
 1089:                  &Apache::edit::deletelist($target,$token)
 1090: 		 ."</td>
 1091: <td>".
 1092:                  &Apache::edit::insertlist($target,$token).
 1093: 		 &Apache::edit::end_row().
 1094:                  &Apache::edit::start_spanning_row()."\n"
 1095: 		 . &Apache::loncommon::helpLatexCheatsheet () .
 1096: 		 &Apache::edit::editfield($token->[1],$text,"",80,4);
 1097:     }
 1098:     if ($target eq 'modified') {
 1099: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser);
 1100: 	$result='<startouttext />'.&Apache::edit::modifiedfield();
 1101:     }
 1102:     if ($target eq 'tex') {
 1103: 	$result .= '\noindent ';
 1104:     }
 1105:     return $result;
 1106: }
 1107: 
 1108: sub start_endouttext {
 1109:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1110:     my $result='';
 1111:     if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
 1112:     if ($target eq "modified") {
 1113: 	$result='<endouttext />'.
 1114: 	    &Apache::edit::handle_insertafter('startouttext'); }
 1115:     return $result;
 1116: }
 1117: 
 1118: sub end_endouttext {
 1119:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1120:     my @result=('','');
 1121:     if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
 1122:     return (@result);
 1123: }
 1124: 
 1125: sub delete_startouttext {
 1126:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1127:     #  my $text=&Apache::lonxml::get_all_text("endouttext",$parser);
 1128:     my $text=$$parser['-1']->get_text("/endouttext");
 1129:     my $ntoken=$$parser['-1']->get_token();
 1130:     &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
 1131:     &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
 1132:     # Deleting 2 parallel tag pairs, but we need the numbers later to look like
 1133:     # they did the last time round
 1134:     &Apache::lonxml::increasedepth($ntoken);
 1135:     &Apache::lonxml::decreasedepth($ntoken);
 1136:     return 1;
 1137: }
 1138: 
 1139: sub start_simpleeditbutton {
 1140:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1141:     my $result='';
 1142:     if (($target eq 'web') &&
 1143:         (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
 1144:         my $url=$ENV{'REQUEST_URI'};
 1145:         $url=~s/\?.*$//;
 1146: 	$result='<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
 1147:                 '<a href="'.$url.'/smpedit">Simple Problem Editor</a> - Note: it can take up to 10 minutes for changes to take effect for all users.'.
 1148: &Apache::loncommon::help_open_topic('Caching').'</td></tr></table><br />';
 1149:     }
 1150:     return $result;
 1151: }
 1152: 
 1153: sub end_simpleeditbutton {
 1154:     return '';
 1155: }
 1156: 
 1157: 1;
 1158: __END__

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