File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.232: download - view: text, annotated - select for diffs
Fri Feb 6 22:36:40 2004 UTC (20 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- always accept scantron answers
- check if the grade also changed on the problem before dicarding previous submissions

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

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