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

1.34      albertel    1: # The LearningOnline Network with CAPA 
                      2: # definition of tags that give a structure to a document
1.74      albertel    3: #
1.333   ! albertel    4: # $Id: structuretags.pm,v 1.332 2006/01/27 23:36:48 albertel Exp $
1.74      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.254     www        28: ###
1.54      www        29: 
1.133     sakharuk   30: 
1.1       albertel   31: package Apache::structuretags; 
                     32: 
                     33: use strict;
                     34: use Apache::lonnet;
1.101     sakharuk   35: use Apache::File();
1.147     www        36: use Apache::lonmenu;
1.210     albertel   37: use Apache::lonlocal;
1.231     sakharuk   38: use Apache::lonxml;
1.267     albertel   39: use Time::HiRes qw( gettimeofday tv_interval );
1.78      harris41   40: BEGIN {
1.248     albertel   41:     &Apache::lonxml::register('Apache::structuretags',('block','languageblock','instructorcomment','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','problemtype','startouttext','endouttext','simpleeditbutton','definetag'));
1.10      albertel   42: }
                     43: 
                     44: sub start_web {
1.326     albertel   45:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     46:     my $bodytext=&Apache::lonxml::get_all_text("/web",$parser,$style);
1.200     albertel   47:     if ($target eq 'web') {
                     48: 	return $bodytext;
1.159     albertel   49:     }
                     50:     return '';
1.10      albertel   51: }
                     52: 
                     53: sub end_web {
1.44      ng         54:     return '';
1.10      albertel   55: }
                     56: 
                     57: sub start_tex {
1.326     albertel   58:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.198     sakharuk   59:     my $result='';
1.326     albertel   60:     my $bodytext=&Apache::lonxml::get_all_text("/tex",$parser,$style);
1.200     albertel   61:     if ($target eq 'tex') {
                     62: 	return $bodytext.' ';
1.159     albertel   63:     }
1.198     sakharuk   64:     return $result;;
1.10      albertel   65: }
                     66: 
                     67: sub end_tex {
1.44      ng         68:     return '';
1.9       albertel   69: }
                     70: 
1.290     albertel   71: sub body_tag_start {
                     72:     my ($target,$background,$bgcolor)=@_;
                     73:     my $body_tag_start='<body onload="'.&Apache::lonmenu::loadevents().'" '.
                     74: 	'onunload="'.&Apache::lonmenu::unloadevents().'" ';
                     75:     if ($env{'browser.imagesuppress'} eq 'on') { $background=''; }
                     76:     if ($background) {
1.306     albertel   77: 	&Apache::lonxml::extlink($background);
1.290     albertel   78: 	$body_tag_start.='background="'.$background.'" ';
                     79:     } else {
                     80: 	if (($bgcolor) && ($env{'browser.blackwhite'} ne 'on')) {
                     81: 	    $body_tag_start.='bgcolor="'.$bgcolor.'" ';
                     82: 	} else {
                     83: 	    $body_tag_start.='bgcolor="#ffffff"';
                     84: 	}
                     85:     }
                     86:     if ($env{'browser.fontenhance'} eq 'on') {
                     87: 	$body_tag_start.=' style="font-size: x-large;" ';
                     88:     }
                     89:     $body_tag_start.='>'.&Apache::lonmenu::menubuttons(undef,$target,1);
1.297     albertel   90:     
                     91:     &Apache::lontexconvert::jsMath_reset();
1.290     albertel   92:     if ($env{'environment.texengine'} eq 'jsMath') {
1.297     albertel   93: 	$body_tag_start.=&Apache::lontexconvert::jsMath_header();
1.290     albertel   94:     }
                     95:     return $body_tag_start;
                     96: }
1.291     albertel   97: 
1.48      albertel   98: sub page_start {
1.159     albertel   99:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    100:     my %found;
1.207     albertel  101:     foreach my $taginside (@$tagstack) {
1.159     albertel  102: 	foreach my $taglookedfor ('html','body','form') {
                    103: 	    if ($taginside =~ /^$taglookedfor$/i) {$found{$taglookedfor} = 1;}
                    104: 	}
                    105:     }
                    106: 
                    107:     my $result;
                    108:     my $head_tag_start;
                    109:     if (!defined($found{'html'})) {
                    110: 	$result=&Apache::londefdef::start_html($target,$token,$tagstack,
                    111: 					       $parstack,$parser,$safeeval);
1.254     www       112: 	$head_tag_start='<head>'.&Apache::lonmenu::registerurl(undef,$target).
1.261     www       113: 	    &Apache::lonhtmlcommon::htmlareaheaders().
1.270     albertel  114: 	    &Apache::lonhtmlcommon::spellheader().
                    115: 	    &Apache::lonxml::fontsettings();     
1.327     albertel  116: 	if ($target eq 'edit') {
                    117: 	    $head_tag_start.=&Apache::edit::js_change_detection();
                    118: 	}
1.159     albertel  119:     }
                    120:     my $body_tag_start;
1.318     albertel  121:     if (!defined($found{'body'}) && $env{'request.state'} eq 'construct') {
                    122: 	if ($env{'environment.remote'} eq 'off') {
                    123: 	    if ($target eq 'web' || $target eq 'edit') {
                    124: 		$body_tag_start=&Apache::loncommon::bodytag();
                    125: 		$body_tag_start.=&Apache::lonxml::message_location();
                    126: 	    }
                    127: 	} else {
                    128: 	    if ($target eq 'web' || $target eq 'edit') {
                    129: 		$body_tag_start=&Apache::loncommon::bodytag(undef,undef,undef,1);
                    130: 		$body_tag_start.=&Apache::lonxml::message_location();
                    131: 	    }
1.297     albertel  132: 	}
1.272     albertel  133:     } elsif (!defined($found{'body'})) {
1.159     albertel  134: 	my $background=&Apache::lonxml::get_param('background',$parstack,
                    135: 						  $safeeval);
1.290     albertel  136: 	my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
                    137: 					       $safeeval);
1.299     albertel  138: 	$body_tag_start=&body_tag_start($target,$background,$bgcolor);
1.291     albertel  139: 	if ( ($target eq 'web' || $target eq 'webgrade')
                    140: 	     && $env{'request.state'} ne 'construct') {
1.165     albertel  141: 	    my ($symb,undef,undef,undef,$publicuser)=
                    142: 		&Apache::lonxml::whichuser();
                    143: 	    if ($symb eq '' && !$publicuser) {
1.159     albertel  144: 		my $help = &Apache::loncommon::help_open_topic("Ambiguous_Reference");
1.211     albertel  145: 		$help=&mt("Browsing resource, all submissions are temporary.")."<br />";
1.159     albertel  146: 		$body_tag_start.=$help;
                    147: 	    }
                    148: 	}
1.245     albertel  149: 	$body_tag_start.=&Apache::lonxml::message_location();
1.159     albertel  150:     }
                    151:     my $form_tag_start;
                    152:     if (!defined($found{'form'})) {
1.281     albertel  153: 	$form_tag_start='<form name="lonhomework" enctype="multipart/form-data" method="POST" action="';
1.284     albertel  154: 	my $uri=$env{'request.uri'};
                    155: 	if ($env{'request.enc'}) { $uri=&Apache::lonenc::encrypted($uri); }
1.327     albertel  156: 	$form_tag_start.=$uri.'" ';
                    157: 	if ($target eq 'edit') {
                    158: 	    $form_tag_start.=&Apache::edit::form_change_detection();
                    159: 	}
                    160: 	$form_tag_start.='>';
1.159     albertel  161:     }
                    162:     return ($result,$head_tag_start,$body_tag_start,$form_tag_start);
1.105     albertel  163: }
                    164: 
1.141     matthew   165: #use Time::HiRes();
1.105     albertel  166: sub get_resource_name {
1.159     albertel  167:     my ($parstack,$safeeval)=@_;
1.204     albertel  168:     if (defined($Apache::lonhomework::name)) {
                    169: 	return $Apache::lonhomework::name;
                    170:     }
1.279     albertel  171:     my ($symb)=&Apache::lonxml::whichuser();
                    172:     my $name=&Apache::lonnet::gettitle($symb);
1.159     albertel  173:     if ($name eq '') {
                    174: 	$name=&Apache::lonnet::EXT('resource.title');
                    175: 	if ($name eq 'con_lost') { $name = ''; }
                    176:     }
1.204     albertel  177:     if ($name!~/\S+/) {
1.284     albertel  178: 	$name=$env{'request.uri'};
1.205     albertel  179: 	$name=~s-.*/([^/]+)$-$1-;
1.204     albertel  180:     }
1.159     albertel  181:     $Apache::lonhomework::name=$name;
                    182:     return $name;
1.105     albertel  183: }
                    184: 
                    185: sub setup_rndseed {
1.159     albertel  186:     my ($safeeval)=@_;
                    187:     my $rndseed;
1.162     albertel  188:     my ($symb)=&Apache::lonxml::whichuser();
1.333   ! albertel  189:     if ($env{'request.state'} eq "construct" 
        !           190: 	|| $symb eq '' 
        !           191: 	|| $Apache::lonhomework::type eq 'practice'
        !           192: 	|| $Apache::lonhomework::history{'resource.CODE'}) {
1.316     www       193: 	&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.317     albertel  194: 						['rndseed']);
1.284     albertel  195: 	$rndseed=$env{'form.rndseed'};
1.159     albertel  196: 	if (!$rndseed) {
1.162     albertel  197: 	    $rndseed=$Apache::lonhomework::history{'rndseed'};
                    198: 	    if (!$rndseed) {
                    199: 		$rndseed=time;
                    200: 	    }
1.284     albertel  201: 	    $env{'form.rndseed'}=$rndseed;
1.162     albertel  202: 	}
1.284     albertel  203: 	if ($env{'form.resetdata'} eq &mt('New Problem Variation') ||
                    204: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
1.190     albertel  205: 	    srand(time);
                    206: 	    $rndseed=int(rand(2100000000));
1.284     albertel  207: 	    $env{'form.rndseed'}=$rndseed;
                    208: 	    delete($env{'form.resetdata'});
                    209: 	    delete($env{'form.newrandomization'});
1.159     albertel  210: 	}
1.187     albertel  211: 	if (defined($rndseed) && $rndseed ne int($rndseed)) {
1.307     albertel  212: 	    $rndseed=join(':',&Apache::lonnet::digest($rndseed));
1.187     albertel  213:         }
1.247     albertel  214:         if ($Apache::lonhomework::history{'resource.CODE'}) {
                    215: 	   $rndseed=&Apache::lonnet::rndseed();
                    216: 	}
1.221     albertel  217: 	if ($safeeval) {
                    218: 	    &Apache::lonxml::debug("Setting rndseed to $rndseed");
1.250     albertel  219: 	    &Apache::run::run('$external::randomseed="'.$rndseed.'";',$safeeval);
1.221     albertel  220: 	}
1.159     albertel  221:     }
                    222:     return $rndseed;
1.105     albertel  223: }
                    224: 
1.268     albertel  225: sub remember_problem_state {
                    226:     return '
1.284     albertel  227:        <input type="hidden" name="problemstate" value="'.$env{'form.problemstate'}.'" />
                    228:        <input type="hidden" name="problemtype" value="'.$env{'form.problemtype'}.'" />
                    229:        <input type="hidden" name="problemstatus" value="'.$env{'form.problemstatus'}.'" />';
1.268     albertel  230: }
                    231: 
1.105     albertel  232: sub problem_edit_header {
1.268     albertel  233:     return '<input type="hidden" name="submitted" value="edit" />'.
                    234: 	&Apache::structuretags::remember_problem_state().'
1.210     albertel  235:        <input type="hidden" name="problemmode" value="'.&mt('Edit').'" />
1.243     matthew   236:        <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />
1.328     albertel  237:        <input '.&Apache::edit::submit_ask_anyway().' type="submit" name="problemmode" accesskey="x" value="'.&mt('EditXML').'" />
1.243     matthew   238:        <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" /> <hr />
                    239:        <input type="submit" name="submit" accesskey="s" value="'.&mt('Submit Changes and Edit').'" />
                    240:        <input type="submit" name="submit" accesskey="v" value="'.&mt('Submit Changes and View').'" /><table><tr><td>'.
1.260     albertel  241:        &Apache::loncommon::help_open_menu('','Problem Editing Help','Problem_Editor_XML_Index','',5,'Authoring',undef,undef,undef,'Problem Editing Help')
                    242:        .'</td></tr></table>'.
1.282     albertel  243:        '<table border="0" width="100%"><tr><td bgcolor="#DDDDDD">';
1.105     albertel  244: }
                    245: 
                    246: sub problem_edit_footer {
1.210     albertel  247:     return '</td></tr></table><br /><input type="submit" name="submit" value="'.&mt('Submit Changes and Edit').'" />
1.255     www       248:     <input type="submit" name="submit" value="'.&mt('Submit Changes and View').'" />'.
                    249:     &Apache::lonhtmlcommon::htmlareaselectactive(@Apache::lonxml::htmlareafields).
1.280     albertel  250:     "\n</form>\n".&Apache::loncommon::endbodytag()."\n</html>";
1.105     albertel  251: }
                    252: 
1.235     albertel  253: sub option {
                    254:     my ($value,$name) = @_;
                    255:     my $result ="<option value='".$value."' ";
1.284     albertel  256:     if ($env{'form.'.$name} eq $value) {
1.235     albertel  257: 	$result.=" selected='on' ";
                    258:     }
                    259:     $result.='>';
                    260:     return $result;
                    261: }
                    262: 
1.105     albertel  263: sub problem_web_to_edit_header {
1.159     albertel  264:     my ($rndseed)=@_;
1.313     albertel  265:     my $result.='<input type="hidden" name="problemmode" value="'.&mt('View').'" />';
                    266:     if (!$Apache::lonhomework::parsing_a_task) {
                    267: 	$result .= '<input type="submit" name="problemmode" accesskey="e" value="'.&mt('Edit').'" />';
                    268:     }
                    269:     $result .= '<input type="submit" name="problemmode" accesskey="x" value="'.&mt('EditXML').'" />
1.243     matthew   270:              <input type="submit" name="newrandomization" accesskey="a" value="'.&mt('New Randomization').'" />
                    271:              <input type="submit" name="resetdata" accesskey="r" value="'.&mt('Reset Submissions').'" />
1.210     albertel  272:              <nobr><input type="submit" name="changerandseed" value="'.&mt('Change Random Seed To:').'" />
1.270     albertel  273:               <input type="text" name="rndseed" size="10" value="'.
1.175     albertel  274: 	       $rndseed.'"
1.270     albertel  275:            onchange="javascript:document.lonhomework.changerandseed.click()" /></nobr>
1.281     albertel  276:              <label><input type="checkbox" name="showallfoils" ';
1.315     albertel  277:     my $show_all_foils_text = 
                    278: 	($Apache::lonhomework::parsing_a_task) ?
                    279: 	&mt('&nbsp;Show&nbsp;All&nbsp;Instances')
                    280: 	: &mt('&nbsp;Show&nbsp;All&nbsp;Foils');
                    281: 
1.284     albertel  282:     if (defined($env{'form.showallfoils'})) { $result.='checked="on"'; }
1.315     albertel  283:     $result.= ' />'.$show_all_foils_text.
1.228     www       284: 	&Apache::loncommon::help_open_topic('Problem_Editor_Testing_Area','Testing Problems').
1.281     albertel  285: 	'</label><hr />';
1.313     albertel  286:     if (!$Apache::lonhomework::parsing_a_task) {
                    287: 	$result.="
1.235     albertel  288: <nobr>
                    289: Problem Status:
                    290: <select name='problemstate'>
1.270     albertel  291:   <option value=''></option>
1.235     albertel  292:   ".&option('CLOSED'               ,'problemstate').&mt("Closed")."</option>
                    293:   ".&option('CAN_ANSWER'           ,'problemstate').&mt("Answerable")."</option>
                    294:   ".&option('CANNOT_ANSWER_tries'  ,'problemstate').&mt("Open with full tries")."</option>
                    295:   ".&option('CANNOT_ANSWER_correct','problemstate').&mt("Open and correct")."</option>
                    296:   ".&option('SHOW_ANSWER'          ,'problemstate').&mt("Show Answer")."</option>
                    297: </select>
                    298: </nobr>
                    299: <nobr>
                    300: Problem Type:
                    301: <select name='problemtype'>
1.270     albertel  302:   <option value=''></option>
1.242     albertel  303:   ".&option('exam'   ,'problemtype').&mt("Exam Problem")."</option>
                    304:   ".&option('problem','problemtype').&mt("Homework problem")."</option>
                    305:   ".&option('survey' ,'problemtype').&mt("Survey Question")."</option>
1.235     albertel  306: </select>
                    307: </nobr>
                    308: <nobr>
                    309: Feedback Mode:
                    310: <select name='problemstatus'>
                    311:   <option value=''></option>
1.242     albertel  312:   ".&option('yes','problemstatus').&mt("Show Feedback")."</option>
                    313:   ".&option('no', 'problemstatus').&mt("Don't Show Feedback")."</option>
1.235     albertel  314: </select>
                    315: </nobr>
1.242     albertel  316: <input type='submit' name='changeproblemmode' value='".&mt("Change")."' />
1.235     albertel  317: <hr />";
1.313     albertel  318: 	my $numtoanalyze=$env{'form.numtoanalyze'};
                    319: 	if (!$numtoanalyze) { $numtoanalyze=20; }
                    320: 	$result.= '<input type="submit" name="problemmode" value='.
                    321: 	    &mt('"Calculate answers').'" /> for
1.136     albertel  322:              <input type="text" name="numtoanalyze" value="'.
1.210     albertel  323: 	     $numtoanalyze.'" size="5" /> '.&mt('versions of this problem').
                    324: 	     '.'.&Apache::loncommon::help_open_topic("Analyze_Problem",
1.176     albertel  325: 						     '',undef,undef,300).
1.210     albertel  326: 						     '<hr />';
1.313     albertel  327:     }
1.159     albertel  328:     return $result;
1.48      albertel  329: }
                    330: 
1.65      albertel  331: sub initialize_storage {
1.159     albertel  332:     %Apache::lonhomework::results=();
1.162     albertel  333:     %Apache::lonhomework::history=();
1.159     albertel  334:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.333   ! albertel  335:     if ($env{'request.state'} eq 'construct' 
        !           336: 	|| $symb eq ''
        !           337: 	|| $Apache::lonhomework::type eq 'practice') {
        !           338: 	
        !           339: 	my $namespace = $symb || $env{'request.uri'};
        !           340: 	if ($env{'form.resetdata'} eq &mt('Reset Submissions') ||
        !           341: 	    $env{'form.resetdata'} eq &mt('New Problem Variation') ||
        !           342: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
        !           343: 	    &Apache::lonnet::tmpreset($namespace,'',$domain,$name);
        !           344: 	    &Apache::lonxml::debug("Attempt reset");
        !           345: 	}
1.159     albertel  346: 	%Apache::lonhomework::history=
1.333   ! albertel  347: 	    &Apache::lonnet::tmprestore($namespace,'',$domain,$name);
1.159     albertel  348: 	my ($temp)=keys %Apache::lonhomework::history ;
                    349: 	&Apache::lonxml::debug("Return message of $temp");
                    350:     } else {
                    351: 	%Apache::lonhomework::history=
                    352: 	    &Apache::lonnet::restore($symb,$courseid,$domain,$name);
                    353:     }
                    354:     #ignore error conditions
1.67      albertel  355:     my ($temp)=keys %Apache::lonhomework::history ;
1.159     albertel  356:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
1.65      albertel  357: }
                    358: 
                    359: # -------------------------------------------------------------finalize_storage
                    360: # Stores away the result has to a student's environment
                    361: # checks form.grade_ for specific values, other wises stores
                    362: # to the running users environment
1.285     raeburn   363: # Will increment totals for attempts, students, and corrects
                    364: # if running user has student role.  
1.65      albertel  365: sub finalize_storage {
1.159     albertel  366:     my $result;
1.289     albertel  367:     if (%Apache::lonhomework::results) {
1.323     albertel  368: 	my @remove = grep(/^INTERNAL_/,keys(%Apache::lonhomework::results));
                    369: 	delete(@Apache::lonhomework::results{@remove});
1.159     albertel  370: 	my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.333   ! albertel  371: 	if ($env{'request.state'} eq 'construct' 
        !           372: 	    || $symb eq ''
        !           373: 	    || $Apache::lonhomework::type eq 'practice') {
        !           374: 	    my $namespace = $symb || $env{'request.uri'};
1.284     albertel  375: 	    $Apache::lonhomework::results{'rndseed'}=$env{'form.rndseed'};
1.159     albertel  376: 	    $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
1.333   ! albertel  377: 					      $namespace,'',$domain,$name);
1.159     albertel  378: 	    &Apache::lonxml::debug('Construct Store return message:'.$result);
                    379: 	} else {
                    380: 	    $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
                    381: 					    $symb,$courseid,$domain,$name);
                    382: 	    &Apache::lonxml::debug('Store return message:'.$result);
1.285     raeburn   383:             if ($env{'request.role'} =~/^st/) {
                    384:                 &store_aggregates($symb,$courseid);
                    385:             }
1.159     albertel  386: 	}
1.323     albertel  387:     } else {
                    388: 	&Apache::lonxml::debug('Nothing to store');
1.67      albertel  389:     }
1.159     albertel  390:     return $result;
1.65      albertel  391: }
1.289     albertel  392: use Data::Dumper;
1.65      albertel  393: 
1.285     raeburn   394: # -------------------------------------------------------------store_aggregates
                    395: # Sends hash of values to be incremented in nohist_resourcetracker.db
                    396: # for the course. Increments total number of attempts, unique students 
                    397: # and corrects for each part for an instance of a problem, as appropriate.
                    398: sub store_aggregates {
                    399:     my ($symb,$courseid) = @_;
1.286     albertel  400:     my %aggregate;
                    401:     my @parts;
1.288     albertel  402:     my $cdomain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    403:     my $cname = $env{'course.'.$env{'request.course.id'}.'.num'};
1.286     albertel  404:     foreach my $key (keys(%Apache::lonhomework::results)) {
1.287     albertel  405:         if ($key =~ /resource\.([^\.]+)\.tries/) {
1.286     albertel  406:             push(@parts, $1);
1.285     raeburn   407:         }
                    408:     }
1.286     albertel  409:     foreach my $part (@parts) {
                    410:         if ($Apache::lonhomework::results{'resource.'.$part.'.award'}
                    411: 	    eq 'APPROX_ANS' ||
                    412: 	    $Apache::lonhomework::results{'resource.'.$part.'.award'}
                    413: 	    eq 'EXACT_ANS') {
1.287     albertel  414:             $aggregate{$symb."\0".$part."\0correct"} = 1;
1.285     raeburn   415:         }
1.286     albertel  416:         if ($Apache::lonhomework::results{'resource.'.$part.'.tries'} == 1) {
1.287     albertel  417:             $aggregate{$symb."\0".$part."\0users"} = 1;
1.292     raeburn   418:         } else {
1.293     albertel  419:             my (undef,$last_reset) = &Apache::grades::get_last_resets($symb,$env{'request.course.id'},[$part]); 
1.292     raeburn   420:             if ($last_reset) {
1.293     albertel  421:                 if (&Apache::grades::get_num_tries(\%Apache::lonhomework::history,$last_reset,$part) == 0) {
1.292     raeburn   422:                     $aggregate{$symb."\0".$part."\0users"} = 1;
                    423:                 }
                    424:             }
1.285     raeburn   425:         }
1.287     albertel  426:         $aggregate{$symb."\0".$part."\0attempts"} = 1;
1.285     raeburn   427:     }
1.292     raeburn   428:     if (keys (%aggregate) > 0) {
1.289     albertel  429: 	&Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
1.292     raeburn   430:                             $cdomain,$cname);
                    431:     }
                    432: }
1.289     albertel  433: 
1.65      albertel  434: sub checkout_msg {
1.211     albertel  435:     my %lt=&Apache::lonlocal::texthash( 
                    436: 		'resource'=>'The resource needs to be checked out',
                    437: 		'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.',
                    438:                 'warning'=>'Checking out resources is subject to course policies, and may exclude future credit even if done erroneously.',
                    439:                 'checkout'=>'Check out Exam for Viewing',
                    440: 		'checkout?'=>'Check out Exam?');
1.159     albertel  441:     return (<<ENDCHECKOUT);
1.211     albertel  442: <h2>$lt{'resource'}</h2>
                    443:     <p>$lt{'id_expln'}</p>
                    444: <font color="red">
                    445: <p>$lt{'warning'}</p>
1.91      albertel  446: </font>
1.284     albertel  447: <form name="checkout" method="POST" action="$env{'request.uri'}">
1.91      albertel  448: <input type="hidden" name="doescheckout" value="yes" />
1.211     albertel  449: <input type="button" name="checkoutbutton" value="$lt{'checkout'}" onClick="javascript:if (confirm('$lt{'checkout?'}')) { document.checkout.submit(); }" />
1.65      albertel  450: </form>
                    451: ENDCHECKOUT
                    452: }
                    453: 
1.252     albertel  454: sub firstaccess_msg {
1.253     albertel  455:     my ($time,$symb)=@_;
                    456:     my ($map)=&Apache::lonnet::decode_symb($symb);
                    457:     my $foldertitle=&Apache::lonnet::gettitle($map);
                    458:     &Apache::lonxml::debug("map is $map title is $foldertitle");
1.252     albertel  459:     return (<<ENDCHECKOUT);
1.253     albertel  460: <h2>The resources in "$foldertitle" are open for a limited time. Once you click the 'Show Resource' button below you have $time to complete all resources "$foldertitle".</h2>
1.284     albertel  461: <form name="markaccess" method="POST" action="$env{'request.uri'}">
1.252     albertel  462: <input type="hidden" name="markaccess" value="yes" />
                    463: <input type="button" name="accessbutton" value="Show Resource" onClick="javascript:if (confirm('Start Timer?')) { document.markaccess.submit(); }" />
                    464: </form>
                    465: ENDCHECKOUT
                    466: }
                    467: 
1.204     albertel  468: sub init_problem_globals {
                    469:     my ($type)=@_;
                    470:     #initialize globals
1.308     foxr      471:     #   For problems, we start out in part 0 (outside a <part> tag).
                    472:     #   and part 0 is used to describe the main body of the <problem>
                    473:     #
1.204     albertel  474:     if ($type eq 'problem') {
                    475: 	$Apache::inputtags::part='0';
                    476: 	@Apache::inputtags::partlist=('0');
1.266     albertel  477: 	$Apache::lonhomework::problemstatus=&get_problem_status('0');
                    478: 	$Apache::lonhomework::ignore_response_errors=0;
1.308     foxr      479: 
1.266     albertel  480:     } elsif ($type eq 'library') {
1.204     albertel  481: 	$Apache::inputtags::part='';
                    482: 	@Apache::inputtags::partlist=();
                    483: 	$Apache::lonhomework::problemstatus='';	
1.266     albertel  484: 	$Apache::lonhomework::ignore_response_errors=1;
1.308     foxr      485: 
1.304     albertel  486:     } elsif ($type eq 'Task') {
                    487: 	$Apache::inputtags::part='0';
                    488: 	@Apache::inputtags::partlist=('0');
                    489: 	$Apache::lonhomework::problemstatus='';	
                    490: 	$Apache::lonhomework::ignore_response_errors=1;
1.204     albertel  491:     }
                    492:     @Apache::inputtags::responselist = ();
                    493:     @Apache::inputtags::importlist = ();
                    494:     @Apache::inputtags::previous=();
                    495:     @Apache::inputtags::previous_version=();
                    496:     $Apache::structuretags::printanswer='No';
                    497:     @Apache::structuretags::whileconds=();
                    498:     @Apache::structuretags::whilebody=();
                    499:     @Apache::structuretags::whileline=();
                    500:     $Apache::lonhomework::scantronmode=0;
                    501:     undef($Apache::lonhomework::name);
                    502: 
                    503: }
                    504: 
                    505: sub reset_problem_globals {
                    506:     my ($type)=@_;
                    507:     undef(%Apache::lonhomework::history);
                    508:     undef(%Apache::lonhomework::results);
                    509:     undef($Apache::inputtags::part);
1.208     albertel  510: #don't undef this, lonhomework.pm takes care of this, we use this to 
                    511: #detect if we try to do 2 problems in one file
                    512: #   undef($Apache::lonhomework::parsing_a_problem);
1.204     albertel  513:     undef($Apache::lonhomework::name);
                    514: }
                    515: 
1.241     albertel  516: sub set_problem_state {
1.240     albertel  517:     my ($part)=@_;
1.284     albertel  518:     if ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct') {
1.240     albertel  519: 	$Apache::lonhomework::history{"resource.$part.solved"}=
                    520: 	    'correct_by_student';
                    521:     }
                    522: }
                    523: 
1.241     albertel  524: sub get_problem_status {
                    525:     my ($part)=@_;
1.267     albertel  526:     my $problem_status;
1.284     albertel  527:     if ($env{'request.state'} eq 'construct' &&
                    528: 	defined($env{'form.problemstatus'})) {
                    529: 	$problem_status=$env{'form.problemstatus'};
1.267     albertel  530:     } else {
                    531: 	$problem_status=&Apache::lonnet::EXT("resource.$part.problemstatus");
                    532: 	&Apache::lonxml::debug("problem status for $part is $problem_status");
1.284     albertel  533: 	&Apache::lonxml::debug("env probstat is ".$env{'form.problemstatus'});
1.241     albertel  534:     }
                    535:     return $problem_status;
                    536: }
                    537: 
1.9       albertel  538: sub start_problem {
1.326     albertel  539:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.19      albertel  540: 
1.311     foxr      541:     # We'll use the redirection to fix up printing of duedates.
1.321     albertel  542:     if (!$Apache::lonxml::metamode) {
                    543: 	&Apache::lonxml::startredirection();
                    544:     }
1.311     foxr      545: 
1.308     foxr      546:     # Problems don't nest and we don't allow more than one <problem> in
                    547:     # a .problem file.
                    548:     #
1.184     albertel  549:     if ( $Apache::inputtags::part ne '' ||
                    550: 	 $Apache::lonhomework::parsing_a_problem) {
                    551: 	&Apache::lonxml::error('Only one &lt;problem&gt; allowed in a .problem file');
1.326     albertel  552: 	#my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,$style);
1.159     albertel  553: 	return '';
                    554:     }
1.184     albertel  555: 
                    556:     $Apache::lonhomework::parsing_a_problem=1;
1.204     albertel  557:     &init_problem_globals('problem');
1.166     albertel  558: 
1.284     albertel  559:     if (defined($env{'scantron.maxquest'})) {
1.166     albertel  560: 	$Apache::lonhomework::scantronmode=1;
                    561:     }
1.161     albertel  562: 
1.246     albertel  563:     &initialize_storage();
1.159     albertel  564:     if ($target ne 'analyze') {
                    565: 	if ($target eq 'web') {
                    566: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
1.238     albertel  567: 	}
1.284     albertel  568:        	if ($env{'request.state'} eq 'construct') { &set_problem_state('0'); }
1.159     albertel  569: 	$Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
1.284     albertel  570: 	if (($env{'request.state'} eq 'construct') &&
                    571: 	    defined($env{'form.problemtype'})) {
                    572: 	    $Apache::lonhomework::type=$env{'form.problemtype'};
1.237     albertel  573: 	}
1.332     albertel  574: 	&Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
1.159     albertel  575:     }
1.164     albertel  576:     if ($Apache::lonhomework::type eq '' ) {
1.284     albertel  577: 	my $uri=$env{'request.uri'};
1.159     albertel  578: 	if ($uri=~/\.(\w+)$/) {
                    579: 	    $Apache::lonhomework::type=$1;
                    580: 	    &Apache::lonxml::debug("Using type of $1");
                    581: 	} else {
                    582: 	    $Apache::lonhomework::type='problem';
                    583: 	    &Apache::lonxml::debug("Using default type, problem, :$uri:");
                    584: 	}
1.87      albertel  585:     }
1.301     albertel  586:     $Apache::lonhomework::default_type = $Apache::lonhomework::type;
1.58      www       587: 
1.159     albertel  588:     #added vars to the scripting enviroment
1.213     albertel  589:     my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.248     albertel  590:     $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.24      albertel  591:     &Apache::run::run($expression,$safeeval);
1.159     albertel  592:     my $status;
                    593:     my $accessmsg;
                    594: 
                    595:     #should get back a <html> or the neccesary stuff to start XML/MathML
                    596:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    597: 	&page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
1.284     albertel  598:     if ($target eq 'tex' and $env{'request.symb'} =~ m/\.page_/) {$result='';}
1.159     albertel  599: 
                    600:     if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
                    601:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    602: 	$target eq 'tex') {
                    603: 	#handle exam checkout
                    604: 	if ($Apache::lonhomework::type eq 'exam') {
                    605: 	    my $token=
                    606: 		$Apache::lonhomework::history{"resource.0.outtoken"};
1.284     albertel  607: 	    if (($env{'form.doescheckout'}) && (!$token)) {
1.159     albertel  608: 		$token=&Apache::lonxml::maketoken();
                    609: 		$Apache::lonhomework::history{"resource.0.outtoken"}=
                    610: 		    $token;
                    611: 	    }
                    612: 	    $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
1.142     albertel  613: 	}
1.284     albertel  614: 	if ($env{'form.markaccess'}) {
1.252     albertel  615: 	    &Apache::lonnet::set_first_access('map');
                    616: 	}
1.159     albertel  617: 	#handle rand seed in construction space
                    618: 	my $rndseed=&setup_rndseed($safeeval);
1.162     albertel  619: 	my ($symb)=&Apache::lonxml::whichuser();
1.333   ! albertel  620: 	if ($env{'request.state'} ne "construct" && 
        !           621: 	    ($symb eq '' || $Apache::lonhomework::type eq 'practice')) {
1.162     albertel  622: 	    $form_tag_start.='<input type="hidden" name="rndseed" value="'.
                    623: 		$rndseed.'" />'.
                    624: 		    '<input type="submit" name="resetdata"
1.211     albertel  625:                              value="'.&mt('New Problem Variation').'" />'.
1.164     albertel  626: 		    '<input type="hidden" name="username"
1.284     albertel  627:                              value="'.$env{'form.username'}.'" />';
1.333   ! albertel  628: 	    if ($env{'request.role.adv'}) {
1.267     albertel  629: 		$form_tag_start.=
1.300     albertel  630: 		    ' <label><input type="checkbox" name="showallfoils" ';
1.284     albertel  631: 		if (defined($env{'form.showallfoils'})) {
1.267     albertel  632: 		    $form_tag_start.='checked="on"';
                    633: 		}
1.300     albertel  634: 		$form_tag_start.= ' />'.&mt('&nbsp;Show&nbsp;All&nbsp;Foils').
                    635: 		    '</label>';
1.267     albertel  636: 	    }
                    637: 	    $form_tag_start.='<hr />';
1.162     albertel  638: 	}
1.324     albertel  639: 
                    640: 	($status,$accessmsg,my $slot_name,my $slot) = 
                    641: 	    &Apache::lonhomework::check_slot_access('0','problem');
1.159     albertel  642: 	push (@Apache::inputtags::status,$status);
1.324     albertel  643: 
1.159     albertel  644: 	my $expression='$external::datestatus="'.$status.'";';
                    645: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                    646: 	&Apache::run::run($expression,$safeeval);
                    647: 	&Apache::lonxml::debug("Got $status");
1.324     albertel  648: 
1.159     albertel  649: 	if (( $status eq 'CLOSED' ) ||
                    650: 	    ( $status eq 'UNCHECKEDOUT') ||
1.252     albertel  651: 	    ( $status eq 'NOT_YET_VIEWED') ||
1.159     albertel  652: 	    ( $status eq 'BANNED') ||
1.216     albertel  653: 	    ( $status eq 'UNAVAILABLE') ||
1.324     albertel  654: 	    ( $status eq 'NOT_IN_A_SLOT') ||
1.216     albertel  655: 	    ( $status eq 'INVALID_ACCESS')) {
1.326     albertel  656: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,
                    657: 						       $style);
1.159     albertel  658: 	    if ( $target eq "web" ) {
                    659: 		$result.= $head_tag_start.'</head>';
                    660: 		my $msg=$body_tag_start;
                    661: 		if ($status eq 'UNAVAILABLE') {
1.245     albertel  662: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.253     albertel  663: 		} elsif ($status ne 'NOT_YET_VIEWED') {
1.245     albertel  664: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
1.159     albertel  665: 		}
1.216     albertel  666: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
1.159     albertel  667: 		    $msg.='The problem '.$accessmsg;
                    668: 		} elsif ($status eq 'UNCHECKEDOUT') {
                    669: 		    $msg.=&checkout_msg;
1.252     albertel  670: 		} elsif ($status eq 'NOT_YET_VIEWED') {
1.253     albertel  671: 		    $msg.=&firstaccess_msg($accessmsg,$symb);
1.325     albertel  672: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    673: 		    $msg.=&Apache::bridgetask::add_request_another_attempt_button("Sign up for time to work.");
1.159     albertel  674: 		}
                    675: 		$result.=$msg.'<br />';
                    676: 	    } elsif ($target eq 'tex') {
1.332     albertel  677: 		my $startminipage = ($env{'form.problem_split'}=~/yes/i)? ''
                    678: 		                    : '\begin{minipage}{\textwidth}';
                    679: 		$result.='\begin{document}\noindent \vskip 1 mm '.
                    680: 		    $startminipage.'\vskip 0 mm';
1.159     albertel  681: 		if ($status eq 'UNAVAILABLE') {
1.211     albertel  682: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
1.159     albertel  683: 		} else {
1.211     albertel  684: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
1.159     albertel  685: 		}
                    686: 	    }
1.324     albertel  687: 	} elsif ($status eq 'NEEDS_CHECKIN') {
1.326     albertel  688: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,
                    689: 						       $style);
1.324     albertel  690: 	    if ($target eq 'web') {
                    691: 		$result .= $head_tag_start.'</head>';
                    692: 		$result .= $body_tag_start;
                    693: 		$result .= &Apache::bridgetask::proctor_validation_screen();
                    694: 	    } elsif ($target eq 'grade') {
                    695: 		&Apache::bridgetask::proctor_check_auth($slot_name,$slot,
                    696: 							'problem');
                    697: 	    }
1.159     albertel  698: 	} elsif ($target eq 'web') {
                    699: 	    my $name= &get_resource_name($parstack,$safeeval);
1.227     albertel  700: 	    $result.="$head_tag_start<title>$name</title></head>
1.158     sakharuk  701:               $body_tag_start \n $form_tag_start".	
1.227     albertel  702: 	      '<input type="hidden" name="submitted" value="yes" />';
                    703: 	    # create a page header and exit
1.284     albertel  704: 	    if ($env{'request.state'} eq "construct") {
                    705: 		$result.= &problem_web_to_edit_header($env{'form.rndseed'});
1.227     albertel  706: 	    }
                    707: 	    # if we are viewing someone else preserve that info
1.284     albertel  708: 	    if (defined $env{'form.grade_symb'}) {
1.227     albertel  709: 		foreach my $field ('symb','courseid','domain','username') {
                    710: 		    $result .= '<input type="hidden" name="grade_'.$field.
1.284     albertel  711: 			'" value="'.$env{"form.grade_$field"}.'" />'."\n";
1.159     albertel  712: 		}
                    713: 	    }
                    714: 	} elsif ($target eq 'tex') {
1.319     foxr      715: 	    $result .= 'INSERTTEXFRONTMATTERHERE';
                    716: 
1.99      sakharuk  717: 	}
1.159     albertel  718:     } elsif ($target eq 'edit') {
                    719: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
                    720: 	    &problem_edit_header();
1.226     albertel  721: 	$Apache::lonxml::warnings_error_header=
                    722: 	    &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 />";
1.159     albertel  723: 	my $temp=&Apache::edit::insertlist($target,$token);
                    724: 	$result.=$temp;
                    725:     } elsif ($target eq 'modified') {
                    726: 	$result=$token->[4];
                    727: 	$result.=&Apache::edit::handle_insert();
                    728:     } else {
                    729: 	# page_start returned a starting result, delete it if we don't need it
                    730: 	$result = '';
1.99      sakharuk  731:     }
1.159     albertel  732:     return $result;
1.9       albertel  733: }
                    734: 
                    735: sub end_problem {
1.159     albertel  736:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.321     albertel  737:     my $result;
1.310     foxr      738: 
1.321     albertel  739:     if (!$Apache::lonxml::metamode) {
                    740: 	$result = &Apache::lonxml::endredirection(); #started in &start_problem
1.329     albertel  741: 	$Apache::lonxml::post_evaluate=0;
1.321     albertel  742:     }
1.319     foxr      743: 
                    744:     if ($target eq 'tex') {
1.321     albertel  745: 
                    746: 	# Figure out the front matter and replace the
                    747: 	# INSERTTEXFRONTMATTERHERE in result with it.  note that we do
                    748: 	# this in end_problem because whether or not we display due
                    749: 	# dates depends on whether due dates have already been
                    750: 	# displayed in the problem parts.
                    751: 
1.319     foxr      752: 	my $frontmatter   = '';
                    753: 	my $startminipage = '';
                    754: 	if (not $env{'form.problem_split'}=~/yes/) {
                    755: 	    $startminipage = '\begin{minipage}{\textwidth}';
                    756: 	}
                    757: 	my $id = $Apache::inputtags::part;
                    758: 	my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                    759: 	my $packages=&Apache::lonnet::metadata($env{'request.uri'},'packages');
                    760: 	my @packages = split /,/,$packages;
                    761: 	my $allow_print_points = 0;
                    762: 	foreach my $partial_key (@packages) {
                    763: 	    if ($partial_key=~m/^part_0$/) {
                    764: 		$allow_print_points=1;
                    765: 	    }
                    766: 	}
                    767: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    768: 	if (defined($maxtries) && $maxtries < 0) { $allow_print_points=0; }
                    769: 	if (lc($env{'course.'.$env{'request.course.id'}.
                    770: 			'.disableexampointprint'}) eq 'yes') {
                    771: 	    $allow_print_points=0;
                    772: 	}
                    773: 	my $name_of_resourse= &Apache::lonxml::latex_special_symbols(&get_resource_name($parstack,$safeeval),'header');
                    774: 	my $begin_doc='\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$env{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent ';
                    775: 	my $toc_line='\vskip 1 mm\noindent '.$startminipage.
                    776: 	    '\addcontentsline{toc}{subsection}{'.$name_of_resourse.'}';
                    777: 	
                    778: 	#  Figure out what the due date is and if we need to print
                    779: 	#  it in the problem header.  We have been logging the
                    780: 	#  last due date written to file. 
                    781: 	
                    782: 	my $duetime = &Apache::lonnet::EXT("resource.$id.duedate"); 
                    783: 	my $duedate = POSIX::strftime("%c",localtime($duetime));
                    784: 	my $temp_file;
                    785: 	my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.due";
                    786: 	
                    787: 	# Figure out what the last printed due date is or set it
                    788: 	# to the epoch if no duedates have been printed.
                    789: 	
                    790: 	my $due_file_content = 0;      #   If the file does not yet exist, time is the epoch.
                    791: 	if (-e $filename) {
                    792: 	    $temp_file = Apache::File->new($filename);
                    793: 	    my @due_file      = <$temp_file>;
                    794: 	    $due_file_content = $due_file[$#due_file];
                    795: 	    chomp $due_file_content;
                    796: 	} 
                    797: 	
                    798: 	# We display the due date iff it is not the same as the last
                    799: 	# duedate in problem header ($due_file_content), and
                    800: 	# none of our parts displayed a duedate.
                    801: 	#
                    802: 	my $parts_with_displayduedate;
                    803: 	if (defined $Apache::outputtags::showonce{'displayduedate'}) {
                    804: 	    $parts_with_displayduedate = 
                    805: 		scalar(@{$Apache::outputtags::showonce{'displayduedate'}});
                    806: 	} else {
                    807: 	    $parts_with_displayduedate = 0;
                    808: 	}
                    809: 	if (($due_file_content != $duetime) && ($parts_with_displayduedate == 0) ) {
                    810: 	    $temp_file = Apache::File->new('>'.$filename);
                    811: 	    print $temp_file "$duetime\n";
                    812: 	    if (not $env{'request.symb'} =~ m/\.page_/) {
                    813: 		if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
                    814: 		    $frontmatter .= $begin_doc.
                    815: 			'\textit{Due date: '.$duedate.'} '.$toc_line;
                    816: 		} else {
                    817: 		    $frontmatter.= $begin_doc.$toc_line;
                    818: 		    if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { $frontmatter .= '\fbox{\textit{'.$weight.' pt}}';}
                    819: 		}
                    820: 	    } else {
                    821: 		$frontmatter .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
                    822: 	    }
                    823: 	} else {
                    824: 	    if (not $env{'request.symb'} =~ m/\.page_/) {
                    825: 		$frontmatter .= $begin_doc.$toc_line;
                    826: 		if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { $frontmatter .= '\fbox{\textit{'.$weight.' pt}}';}
                    827: 	    } else {
                    828: 		$frontmatter .= '\vskip 1mm \\\\\\\\';
                    829: 	    }
                    830: 	}
                    831: 	$result =~ s/INSERTTEXFRONTMATTERHERE/$frontmatter/;
                    832:     }
                    833: 
1.159     albertel  834:     my $status=$Apache::inputtags::status['-1'];
                    835:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    836: 	$target eq 'tex') {
1.249     albertel  837: 	if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
1.159     albertel  838: 	    # if part is zero, no <part>s existed, so we need to the grading
1.249     albertel  839: 	    if ($status eq 'CAN_ANSWER' ||$Apache::lonhomework::scantronmode) {
                    840: 		&Apache::inputtags::grade;
1.324     albertel  841: 	    } elsif ($status eq 'NEEDS_CHECKIN') {
                    842: 		# no need to grade, and don't want to hide data
1.249     albertel  843: 	    } else {
                    844: 		# move any submission data to .hidden
                    845: 		&Apache::inputtags::hidealldata($Apache::inputtags::part);
                    846: 	    }
1.159     albertel  847: 	} elsif ( ($target eq 'web' || $target eq 'tex') &&
                    848: 		  $Apache::inputtags::part eq '0' &&
1.252     albertel  849: 		  $status ne 'UNCHECKEDOUT' && $status ne 'NOT_YET_VIEWED') {
1.159     albertel  850: 	    # if part is zero, no <part>s existed, so we need show the current
                    851: 	    # grading status
                    852: 	    my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
                    853: 	    $result.= $gradestatus;
                    854: 	}
                    855: 	if (
1.284     albertel  856: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.159     albertel  857: 	    ($target eq 'answer') || ($target eq 'tex')
                    858: 	   ) {
1.227     albertel  859: 	    if ($target ne 'tex' &&
1.284     albertel  860: 		$env{'form.answer_output_mode'} ne 'tex') {
1.254     www       861: 		$result.="</form>";
                    862: 		$result.=&Apache::lonhtmlcommon::htmlareaselectactive(@Apache::lonxml::htmlareafields);
1.159     albertel  863: 	    }
                    864: 	    if ($target eq 'web') {
1.281     albertel  865: 		$result.=&Apache::lonxml::xmlend().'</html>';
1.159     albertel  866: 	    } elsif ($target eq 'tex') {
1.178     sakharuk  867: 		my $endminipage = '';
1.284     albertel  868: 		if (not $env{'form.problem_split'}=~/yes/) {
1.178     sakharuk  869: 		    $endminipage = '\end{minipage}';
                    870: 		}
1.284     albertel  871:                 if ($env{'form.print_discussions'} eq 'yes') {
1.263     sakharuk  872: 		    $result.=&Apache::lonxml::xmlend($target,$parser);
1.257     sakharuk  873: 		    $result=~s/<\/html>//;
1.159     albertel  874: 		} else {
1.262     sakharuk  875: 		    $result .= '\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
1.284     albertel  876: 		    if (not $env{'request.symb'} =~ m/\.page_/) {
1.262     sakharuk  877: 			$result .= $endminipage.'\end{document} ';
                    878: 		    } else {
                    879: 			$result .= '';
                    880: 		    }
1.159     albertel  881: 		}
                    882: 	    }
                    883: 	}
                    884: 	if ($target eq 'grade') {
                    885: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                    886: 	    &finalize_storage();
                    887: 	}
1.284     albertel  888: 	if ($target eq 'answer' && ($env{'request.state'} eq 'construct')
                    889: 	    && $env{'form.answer_output_mode'} ne 'tex') {
1.294     albertel  890: 	    $result.=&Apache::lonxml::xmlend().'</html>';
                    891: 	                        # normally we get it from above, but in CSTR
1.172     albertel  892: 	                        # we always show answer mode too.
1.159     albertel  893: 	}
                    894:     } elsif ($target eq 'meta') {
                    895: 	if ($Apache::inputtags::part eq '0') {
1.179     albertel  896: 	    @Apache::inputtags::response=();
1.159     albertel  897: 	    $result=&Apache::response::mandatory_part_meta;
                    898: 	}
1.215     albertel  899: 	$result.=&Apache::response::meta_part_order();
1.258     albertel  900: 	$result.=&Apache::response::meta_response_order();
1.159     albertel  901:     } elsif ($target eq 'edit') {
                    902: 	&Apache::lonxml::debug("in end_problem with $target, edit");
1.314     albertel  903: 	$result .= &problem_edit_footer();
1.320     albertel  904:     } elsif ($target eq 'modified') {
                    905: 	 $result .= $token->[2];
                    906: 	 $result.=&Apache::edit::handle_insertafter($token->[1]);
1.159     albertel  907:     }
1.155     albertel  908: 
1.284     albertel  909:     if ($env{'request.state'} eq 'construct' && $target eq 'web') {
1.177     albertel  910: 	&Apache::inputtags::check_for_duplicate_ids();
                    911:     }
1.204     albertel  912: 
                    913:     &reset_problem_globals('problem');
1.159     albertel  914: 
                    915:     return $result;
1.48      albertel  916: }
                    917: 
1.108     albertel  918: 
1.48      albertel  919: sub start_library {
1.159     albertel  920:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    921:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
1.244     albertel  922:     if ($$tagstack[0] eq 'library') {
                    923: 	&init_problem_globals('library');
                    924: 	$Apache::lonhomework::type='problem';
                    925:     }
1.159     albertel  926:     if ($target eq 'edit') {
                    927: 	($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    928: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
                    929: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
                    930: 	    &problem_edit_header();
                    931: 	my $temp=&Apache::edit::insertlist($target,$token);
                    932: 	$result.=$temp;
                    933:     } elsif ($target eq 'modified') {
                    934: 	$result=$token->[4];
                    935: 	$result.=&Apache::edit::handle_insert();
                    936:     } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
1.284     albertel  937: 	     $env{'request.state'} eq "construct" ) {
1.159     albertel  938: 	($result,$head_tag_start,$body_tag_start,$form_tag_start)=
                    939: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
                    940: 	my $name=&get_resource_name($parstack,$safeeval);
                    941: 	my $rndseed=&setup_rndseed($safeeval);
                    942: 	$result.="$head_tag_start<title>$name</title></head>
1.105     albertel  943:               $body_tag_start \n $form_tag_start".	
1.159     albertel  944: 		  '<input type="hidden" name="submitted" value="yes" />';
                    945: 	$result.=&problem_web_to_edit_header($rndseed);
                    946:     }
                    947:     return $result;
1.48      albertel  948: }
                    949: 
                    950: sub end_library {
1.159     albertel  951:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    952:     my $result='';
                    953:     if ($target eq 'edit') {
                    954: 	$result=&problem_edit_footer();
                    955:     } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
1.284     albertel  956: 	     $env{'request.state'} eq "construct") {
1.280     albertel  957: 	$result.='</form>'.&Apache::loncommon::endbodytag().
                    958: 	    &Apache::lonxml::xmlend();
1.159     albertel  959:     }
1.204     albertel  960:     if ($$tagstack[0] eq 'library') { &reset_problem_globals('library') };
1.159     albertel  961:     return $result;
1.197     www       962: }
                    963: 
                    964: sub start_definetag {
1.326     albertel  965:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.197     www       966: 
                    967:     my $result;
                    968: 
                    969:     my $name = $token->[2]->{'name'};
1.326     albertel  970:     my $skip=&Apache::lonxml::get_all_text("/definetag",$parser,$style);
1.197     www       971:     if ($name=~/^\//) {
                    972: 	$result=
                    973:  '<br /><table bgcolor="#FFBBBB"><tr><th>END <tt>'.$name.'</tt></th></tr>';
                    974:     } else {
                    975: 	$result=
                    976:  '<br /><table bgcolor="#BBFFBB"><tr><th>BEGIN <tt>'.$name.'</tt></th></tr>';
                    977:     }
                    978:     $skip=~s/\</\&lt\;/gs;
                    979:     $skip=~s/\>/\&gt\;/gs;
                    980:     $result.='<tr><td><pre>'.$skip.'</pre></td></tr></table>';
                    981:     return $result;
                    982: }
                    983: 
                    984: sub end_definetag {
                    985:     return '';
1.1       albertel  986: }
                    987: 
                    988: sub start_block {
1.201     albertel  989:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.131     albertel  990: 
                    991:     my $result;
1.1       albertel  992: 
1.160     albertel  993:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.131     albertel  994: 	$target eq 'tex' || $target eq 'analyze') {
1.159     albertel  995: 	my $code = $token->[2]->{'condition'};
1.201     albertel  996: 	if (defined($code)) {
1.137     albertel  997: 	    if (!$Apache::lonxml::default_homework_loaded) {
                    998: 		&Apache::lonxml::default_homework_load($safeeval);
                    999: 	    }
1.131     albertel 1000: 	    $result = &Apache::run::run($code,$safeeval);
                   1001: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
                   1002: 	} else {
                   1003: 	    $result='1';
                   1004: 	}
                   1005: 	if ( ! $result ) {
1.201     albertel 1006: 	    my $skip=&Apache::lonxml::get_all_text("/block",$parser,$style);
1.131     albertel 1007: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1008: 	}
                   1009: 	$result='';
                   1010:     } elsif ($target eq 'edit') {
                   1011: 	$result .=&Apache::edit::tag_start($target,$token);
                   1012: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
                   1013: 					  $token,40);
                   1014: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1015:     } elsif ($target eq 'modified') {
                   1016: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1017: 						     $safeeval,'condition');
                   1018: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38      albertel 1019:     }
1.131     albertel 1020:     return $result;
1.1       albertel 1021: }
                   1022: 
                   1023: sub end_block {
1.167     www      1024:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1025:     my $result;
                   1026:     if ($target eq "edit") {
                   1027: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1028:     }
                   1029:     return $result;
                   1030: }
                   1031: 
                   1032: sub start_languageblock {
1.201     albertel 1033:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.167     www      1034: 
                   1035:     my $result;
                   1036: 
                   1037:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1038: 	$target eq 'tex' || $target eq 'analyze') {
1.201     albertel 1039: 	my $include = $token->[2]->{'include'};
                   1040: 	my $exclude = $token->[2]->{'exclude'};
                   1041:         my %languages=&Apache::loncommon::display_languages();
                   1042:         $result='1';
                   1043: 	if ($include) {
                   1044:             $result='';
                   1045:             foreach (split(/\,/,$include)) {
                   1046:                 if ($languages{$_}) { $result='1'; }
                   1047:             }
                   1048: 	}
                   1049:         if ($exclude) {
                   1050:             foreach (split(/\,/,$exclude)) {
                   1051:                 if ($languages{$_}) { $result='0'; }
                   1052:             }
                   1053: 	}
                   1054: 	if ( ! $result ) {
                   1055: 	    my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser,
                   1056: 						   $style);
                   1057: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1058: 	}
                   1059: 	$result='';
1.167     www      1060:     } elsif ($target eq 'edit') {
                   1061: 	$result .=&Apache::edit::tag_start($target,$token);
1.211     albertel 1062: 	$result .=&Apache::edit::text_arg(&mt('Include Language:'),'include',
1.167     www      1063: 					  $token,40);
1.211     albertel 1064: 	$result .=&Apache::edit::text_arg(&mt('Exclude Language:'),'exclude',
1.167     www      1065: 					  $token,40);
                   1066: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1067:     } elsif ($target eq 'modified') {
                   1068: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.168     albertel 1069: 						     $safeeval,'include',
                   1070: 						     'exclude');
1.167     www      1071: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1072:     }
                   1073:     return $result;
                   1074: }
                   1075: 
                   1076: sub end_languageblock {
1.170     www      1077:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1078:     my $result;
1.201     albertel 1079:     if ($target eq "edit") {
1.170     www      1080: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1081:     }
                   1082:     return $result;
                   1083: }
                   1084: 
                   1085: sub start_instructorcomment {
1.201     albertel 1086:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.170     www      1087: 
                   1088:     my $result;
                   1089: 
                   1090:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1091: 	$target eq 'tex' || $target eq 'analyze') {
1.284     albertel 1092:         $result=($env{'request.role'}=~/^(in|cc|au|ca|li)/);
                   1093: 	if ( (! $result) or ($env{'form.instructor_comments'} eq 'hide')) {
1.201     albertel 1094: 	    my $skip=&Apache::lonxml::get_all_text("/instructorcomment",
                   1095: 						   $parser,$style);
1.170     www      1096: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1097: 	}
                   1098: 	$result='';
                   1099:     } elsif ($target eq 'edit') {
                   1100: 	$result .=&Apache::edit::tag_start($target,$token);
                   1101: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1102:     }
                   1103:     return $result;
                   1104: }
                   1105: 
                   1106: sub end_instructorcomment {
1.159     albertel 1107:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160     albertel 1108:     my $result;
                   1109:     if ($target eq "edit") {
                   1110: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1111:     }
                   1112:     return $result;
1.4       tsai     1113: }
                   1114: 
                   1115: sub start_while {
1.326     albertel 1116:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1117: 
1.160     albertel 1118:     my $result;
1.161     albertel 1119:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.160     albertel 1120: 	$target eq 'tex' || $target eq 'analyze') {
                   1121: 	my $code = $token->[2]->{'condition'};
1.4       tsai     1122: 
1.160     albertel 1123: 	push( @Apache::structuretags::whileconds, $code);
                   1124: 	if (!$Apache::lonxml::default_homework_loaded) {
                   1125: 	    &Apache::lonxml::default_homework_load($safeeval);
                   1126: 	}
                   1127: 	my $result = &Apache::run::run($code,$safeeval);
1.326     albertel 1128: 	my $bodytext=&Apache::lonxml::get_all_text("/while",$parser,$style);
1.160     albertel 1129: 	push( @Apache::structuretags::whilebody, $bodytext);
1.161     albertel 1130: 	push( @Apache::structuretags::whileline, $token->[5]);
                   1131: 	&Apache::lonxml::debug("s code $code got -$result-");
1.160     albertel 1132: 	if ( $result ) {
                   1133: 	    &Apache::lonxml::newparser($parser,\$bodytext);
                   1134: 	}
                   1135:     } elsif ($target eq 'edit') {
                   1136: 	$result .=&Apache::edit::tag_start($target,$token);
1.211     albertel 1137: 	$result .=&Apache::edit::text_arg(&mt('Test Condition:'),'condition',
1.160     albertel 1138: 					  $token,40);
                   1139: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1140:     } elsif ($target eq 'modified') {
                   1141: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1142: 						     $safeeval,'condition');
                   1143: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.159     albertel 1144:     }
1.160     albertel 1145:     return $result;
1.4       tsai     1146: }
                   1147: 
                   1148: sub end_while {
1.159     albertel 1149:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160     albertel 1150:     my $result;
                   1151: 
                   1152:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1153: 	$target eq 'tex' || $target eq 'analyze') {
                   1154: 	my $code = pop(@Apache::structuretags::whileconds);
                   1155: 	my $bodytext = pop(@Apache::structuretags::whilebody);
1.161     albertel 1156: 	my $line = pop(@Apache::structuretags::whileline);
                   1157: 	my $return = &Apache::run::run($code,$safeeval);
                   1158: 	my $starttime=time;
                   1159: 	my $error=0;
                   1160: 	while ($return) {
                   1161: 	    if (time-$starttime >
                   1162: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}) {
1.270     albertel 1163: 		#$return = 0; $error=1; next;
1.161     albertel 1164: 	    }
                   1165: 	    $result.=&Apache::scripttag::xmlparse($bodytext);
                   1166: 	    $return = &Apache::run::run($code,$safeeval);
                   1167: 	}
                   1168: 	if ($error) {
1.270     albertel 1169: 	    &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>');
1.160     albertel 1170: 	}
                   1171:     } elsif ($target eq "edit") {
                   1172: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.159     albertel 1173:     }
1.160     albertel 1174:     return $result;
1.1       albertel 1175: }
1.6       tsai     1176: 
1.160     albertel 1177: # <randomlist show="1">
1.6       tsai     1178: #  <tag1>..</tag1>
                   1179: #  <tag2>..</tag2>
                   1180: #  <tag3>..</tag3>
1.160     albertel 1181: #  ...
1.6       tsai     1182: # </randomlist>
                   1183: sub start_randomlist {
1.326     albertel 1184:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1185:     my $result;
                   1186:     if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
                   1187: 	$target eq 'tex' || $target eq 'analyze') {
1.331     albertel 1188: 	my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
1.305     albertel 1189: 	my $b_parser= HTML::LCParser->new(\$body);
                   1190: 	$b_parser->xml_mode(1);
                   1191: 	$b_parser->marked_sections(1);
1.159     albertel 1192: 	my $b_tok;
                   1193: 	my @randomlist;
                   1194: 	my $list_item;
                   1195: 	while($b_tok = $b_parser->get_token() ) {
                   1196: 	    if($b_tok->[0] eq 'S') { # start tag
                   1197: 		# get content of the tag until matching end tag
                   1198: 		# get all text upto the matching tag
                   1199: 		# and push the content into @randomlist
                   1200: 		$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
                   1201: 							   $b_parser);
                   1202: 		$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
                   1203: 		push(@randomlist,$list_item);
                   1204: 		#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
                   1205:                 #         $list_item</b>";
                   1206: 	    }
                   1207: 	    if($b_tok->[0] eq 'T') { # text
                   1208: 		# what to do with text in between tags?
                   1209: 		#  print "<b>TEXT $b_tok->[1]</b><br />";
                   1210: 	    }
                   1211: 	    # if($b_tok->[0] eq 'E') { # end tag, should not happen
                   1212: 	    #  print "<b>END-TAG $b_tok->[1]</b><br />";
                   1213: 	    # }
                   1214: 	}
1.303     albertel 1215: 	if (@randomlist) {
                   1216: 	    my @idx_arr = (0 .. $#randomlist);
                   1217: 	    &Apache::structuretags::shuffle(\@idx_arr);
                   1218: 	    my $bodytext = '';
                   1219: 	    my $show=$#randomlist;
                   1220: 	    my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
                   1221: 	    $showarg--;
                   1222: 	    if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
                   1223: 	    for(0 .. $show) {
                   1224: 		$bodytext .= "$randomlist[ $idx_arr[$_] ]";
                   1225: 	    }
                   1226: 	    &Apache::lonxml::newparser($parser,\$bodytext);
1.159     albertel 1227: 	}
                   1228:     } elsif ($target eq 'edit' ) {
                   1229: 	$result .=&Apache::edit::tag_start($target,$token);
                   1230: 	$result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
                   1231: 					   $token,5);
                   1232: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1233:     } elsif ($target eq 'modified' ) {
                   1234: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1235: 						     $safeeval,'show');
                   1236: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1237:     }
                   1238:     return $result;
1.7       tsai     1239: }
                   1240: 
                   1241: sub shuffle {
                   1242:     my $a=shift;
                   1243:     my $i;
1.303     albertel 1244:     if (ref($a) eq 'ARRAY' && @$a) {
1.251     albertel 1245: 	&Apache::response::pushrandomnumber();
1.159     albertel 1246: 	for($i=@$a;--$i;) {
                   1247: 	    my $j=int(&Math::Random::random_uniform() * ($i+1));
                   1248: 	    next if $i == $j;
                   1249: 	    @$a[$i,$j] = @$a[$j,$i];
                   1250: 	}
1.251     albertel 1251: 	&Apache::response::poprandomnumber();
1.7       tsai     1252:     }
1.6       tsai     1253: }
                   1254: 
                   1255: sub end_randomlist {
1.159     albertel 1256:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1257:     my $result;
                   1258:     if ($target eq 'edit' ) {
                   1259: 	$result=&Apache::edit::tag_end($target,$token,
                   1260: 				       'End Randomly Parsed Block');
                   1261:     }
                   1262:     return $result;
1.6       tsai     1263: }
                   1264: 
1.283     albertel 1265: sub ordered_show_check {
                   1266:     my $last_part=$Apache::inputtags::partlist[-2];
                   1267:     my $in_order=
                   1268: 	&Apache::lonnet::EXT('resource.'.$Apache::inputtags::part.'.ordered');
                   1269:     my $in_order_show=1;
                   1270:     if ($last_part ne '0' && lc($in_order) eq 'yes') {
                   1271: 	$in_order_show=&Apache::response::check_status($last_part);
                   1272:     }
                   1273:     return $in_order_show;
                   1274: }
                   1275: 
1.11      albertel 1276: sub start_part {
1.326     albertel 1277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.321     albertel 1278:     if (!$Apache::lonxml::metamode) {
                   1279: 	&Apache::lonxml::startredirection(); # we'll use redirection to fix up 
                   1280: 	                                     # duedates.
                   1281:     }
1.159     albertel 1282:     my $result='';
                   1283:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
1.296     albertel 1284:     if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
1.159     albertel 1285:     $Apache::inputtags::part=$id;
1.177     albertel 1286:     push(@Apache::inputtags::partlist,$id);
                   1287:     @Apache::inputtags::response=();
1.159     albertel 1288:     @Apache::inputtags::previous=();
                   1289:     @Apache::inputtags::previous_version=();
1.241     albertel 1290:     $Apache::lonhomework::problemstatus=&get_problem_status($id);
1.159     albertel 1291:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.259     albertel 1292:     my $newtype=&Apache::lonnet::EXT("resource.$id.type");
                   1293:     if ($newtype) { $Apache::lonhomework::type=$newtype; }
1.283     albertel 1294:     my $in_order_show=&ordered_show_check();
1.214     albertel 1295:     my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.259     albertel 1296:     $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.209     albertel 1297:     &Apache::run::run($expression,$safeeval);
1.159     albertel 1298: 
                   1299:     if ($target eq 'meta') {
1.224     www      1300: 	my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
                   1301: 	return &Apache::response::mandatory_part_meta.
                   1302: 	       &Apache::response::meta_parameter_write('display','string',$display,'Part Description');
1.159     albertel 1303:     } elsif ($target eq 'web' || $target eq 'grade' ||
                   1304: 	     $target eq 'answer' || $target eq 'tex') {
1.283     albertel 1305: 	if ($hidden || !$in_order_show) {
1.326     albertel 1306: 	    my $bodytext=&Apache::lonxml::get_all_text("/part",$parser,$style);
1.159     albertel 1307: 	} else {
                   1308: 	    my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
                   1309: 	    push (@Apache::inputtags::status,$status);
                   1310: 	    my $expression='$external::datestatus="'.$status.'";';
                   1311: 	    $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
                   1312: 	    &Apache::run::run($expression,$safeeval);
1.284     albertel 1313: 	    if ($env{'request.state'} eq 'construct') {
1.241     albertel 1314: 		&set_problem_state($Apache::inputtags::part); 
1.240     albertel 1315: 	    }
1.216     albertel 1316: 	    if (( $status eq 'CLOSED' ) ||
                   1317: 		( $status eq 'UNCHECKEDOUT') ||
1.252     albertel 1318: 		( $status eq 'NOT_YET_VIEWED') ||
1.216     albertel 1319: 		( $status eq 'BANNED') ||
                   1320: 		( $status eq 'UNAVAILABLE') ||
                   1321: 		( $status eq 'INVALID_ACCESS')) {
1.326     albertel 1322: 		my $bodytext=&Apache::lonxml::get_all_text("/part",$parser,
                   1323: 							   $style);
1.159     albertel 1324: 		if ( $target eq "web" ) {
1.211     albertel 1325: 		    $result="<br />".&mt('Part is not open to be viewed. It')." $accessmsg<br />";
1.159     albertel 1326: 		} elsif ( $target eq 'tex' ) {
1.284     albertel 1327: 		    if (not $env{'form.problem_split'}=~/yes/) {
1.211     albertel 1328: 			$result="\\end{minipage}\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\\\begin{minipage}{\\textwidth}";
1.195     sakharuk 1329: 		    } else {
1.211     albertel 1330: 			$result="\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\";
1.195     sakharuk 1331: 		    }
1.159     albertel 1332: 		}
                   1333: 	    } else {
                   1334: 		if ($target eq 'tex') {
1.284     albertel 1335: 		    if (not $env{'form.problem_split'}=~/yes/) {
1.264     sakharuk 1336: 			if ($$tagstack[-2] eq 'td') {
                   1337: 			    $result.='\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
                   1338: 			} else {
                   1339: 			    $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
                   1340: 			}
1.195     sakharuk 1341: 		    }
1.159     albertel 1342: 		    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.284     albertel 1343: 		    my $allkeys=&Apache::lonnet::metadata($env{'request.uri'},'packages');
1.222     sakharuk 1344: 		    my @allkeys = split /,/,$allkeys;
                   1345: 		    my $allow_print_points = 0;
                   1346: 		    foreach my $partial_key (@allkeys) {
1.230     albertel 1347: 			if ($partial_key=~m/^part_(.*)$/) {
1.222     sakharuk 1348: 			    if ($1 ne '0') {$allow_print_points=1;}
                   1349: 			}
                   1350: 		    }
1.275     albertel 1351: 		    my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1352: 		    if (defined($maxtries) && $maxtries < 0) {
                   1353: 			$allow_print_points=0;
                   1354: 		    }
1.302     albertel 1355: 		    if (lc($env{'course.'.$env{'request.course.id'}.
                   1356: 				    '.disableexampointprint'}) eq 'yes') {
                   1357: 			$allow_print_points=0;
                   1358: 		    }
1.222     sakharuk 1359: 		    if (($Apache::lonhomework::type eq 'exam') && ($allow_print_points)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
1.233     www      1360: 		} elsif ($target eq 'web') {
                   1361: 		    $result.='<a name="'.&Apache::lonnet::escape($Apache::inputtags::part).'" />';
1.159     albertel 1362: 		}
                   1363: 	    }
                   1364: 	}
                   1365:     } elsif ($target eq 'edit') {
                   1366: 	$result.=&Apache::edit::tag_start($target,$token);
                   1367: 	$result.=&Apache::edit::text_arg('Part ID:','id',$token).
                   1368: 	    &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
1.224     www      1369: 	    '&nbsp;&nbsp;'.
                   1370: &Apache::edit::text_arg('Displayed Part Description:','display',$token).
1.159     albertel 1371: 		&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1372:     } elsif ($target eq 'modified') {
                   1373: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.225     albertel 1374: 						     $safeeval,'id','display');
1.159     albertel 1375: 	if ($constructtag) {
1.225     albertel 1376: 	    #limiting ids to only letters numbers, and space
1.224     www      1377: 	    $token->[2]->{'id'}=~s/[^A-Za-z0-9 ]//gs;
1.159     albertel 1378: 	    $result = &Apache::edit::rebuild_tag($token);
                   1379: 	    $result.=&Apache::edit::handle_insert();
                   1380: 	}
                   1381:     }
                   1382:     return $result;
1.11      albertel 1383: }
                   1384: 
                   1385: sub end_part {
1.159     albertel 1386:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1387:     &Apache::lonxml::debug("in end_part $target ");
                   1388:     my $status=$Apache::inputtags::status['-1'];
                   1389:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.283     albertel 1390:     my $in_order_show=&ordered_show_check();
1.321     albertel 1391:     my $result;
                   1392:     if (!$Apache::lonxml::metamode) {
                   1393: 	$result = &Apache::lonxml::endredirection(); # started in &start_part
1.329     albertel 1394: 	$Apache::lonxml::post_evaluate=0;
1.321     albertel 1395:     }
1.312     albertel 1396:     if ($target eq 'grade') {
1.249     albertel 1397: 	if (($status eq 'CAN_ANSWER' || $Apache::lonhomework::scantronmode) &&
1.283     albertel 1398: 	    !$hidden && $in_order_show) {
1.311     foxr     1399: 	    $result.=&Apache::inputtags::grade;
1.249     albertel 1400: 	} else {
                   1401: 	    # move any submission data to .hidden
                   1402: 	    &Apache::inputtags::hidealldata($Apache::inputtags::part);
                   1403: 	}
1.283     albertel 1404:     } elsif (($target eq 'web' || $target eq 'tex') &&
                   1405: 	     !$hidden && $in_order_show) {
1.159     albertel 1406: 	my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
                   1407: 							$target);
1.212     albertel 1408: 	if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
                   1409: 	    $gradestatus='';
                   1410: 	}
1.311     foxr     1411: 	$result.=$gradestatus;
1.265     sakharuk 1412: 	if ($$tagstack[-2] eq 'td' and $target eq 'tex') {$result.='\end{minipage}';} 
1.181     albertel 1413:     } elsif ($target eq 'edit') {
1.311     foxr     1414: 	$result.=&Apache::edit::end_table();
1.322     albertel 1415:     } elsif ($target eq 'modified') {
                   1416: 	 $result .= $token->[2];
                   1417: 	 $result.=&Apache::edit::handle_insertafter($token->[1]);
1.159     albertel 1418:     }
                   1419:     pop @Apache::inputtags::status;
                   1420:     $Apache::inputtags::part='';
1.295     albertel 1421:     $Apache::lonhomework::type = $Apache::lonhomework::default_type;
1.159     albertel 1422:     return $result;
1.11      albertel 1423: }
1.1       albertel 1424: 
1.25      albertel 1425: sub start_preduedate {
1.326     albertel 1426:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1427:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.236     albertel 1428: 	&Apache::lonxml::debug("State in preduedate is ". $Apache::inputtags::status['-1']);
1.300     albertel 1429: 	if (!$Apache::lonhomework::scantronmode &&
                   1430: 	    $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1.236     albertel 1431: 	    $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                   1432: 	    &Apache::lonxml::debug("Wha? ". ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER'));
1.326     albertel 1433: 	    &Apache::lonxml::get_all_text("/preduedate",$parser,$style);
1.159     albertel 1434: 	}
1.24      albertel 1435:     }
1.159     albertel 1436:     return '';
1.24      albertel 1437: }
                   1438: 
1.25      albertel 1439: sub end_preduedate {
1.159     albertel 1440:     return '';
1.24      albertel 1441: }
                   1442: 
1.25      albertel 1443: sub start_postanswerdate {
1.326     albertel 1444:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.234     albertel 1445:     if ($target eq 'web' || $target eq 'grade') {
1.300     albertel 1446: 	if ($Apache::lonhomework::scantronmode ||
                   1447: 	    $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1.326     albertel 1448: 	    &Apache::lonxml::get_all_text("/postanswerdate",$parser,$style);
1.159     albertel 1449: 	}
                   1450:     } elsif ($target eq 'tex') {
1.326     albertel 1451: 	&Apache::lonxml::get_all_text("/postanswerdate",$parser,$style);
1.159     albertel 1452:     }
                   1453:     return '';
1.24      albertel 1454: }
                   1455: 
1.25      albertel 1456: sub end_postanswerdate {
1.159     albertel 1457:     return '';
1.24      albertel 1458: }
                   1459: 
1.25      albertel 1460: sub start_notsolved {
1.326     albertel 1461:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1462:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1463: 	$target eq 'tex') {
                   1464: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                   1465: 	&Apache::lonxml::debug("not solved has :$gradestatus:");
1.239     albertel 1466: 	if ($gradestatus =~ /^correct/ &&
                   1467: 	    &Apache::response::show_answer()) {
1.159     albertel 1468: 	    &Apache::lonxml::debug("skipping");
1.326     albertel 1469: 	    &Apache::lonxml::get_all_text("/notsolved",$parser,$style);
1.159     albertel 1470: 	}
1.24      albertel 1471:     }
1.159     albertel 1472:     return '';
1.24      albertel 1473: }
                   1474: 
1.25      albertel 1475: sub end_notsolved {
1.159     albertel 1476:     return '';
1.24      albertel 1477: }
                   1478: 
                   1479: sub start_solved {
1.326     albertel 1480:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1481:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1482: 	$target eq 'tex') {
                   1483: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1.239     albertel 1484: 	if ($gradestatus !~ /^correct/ ||
                   1485: 	    !&Apache::response::show_answer()) {
1.326     albertel 1486: 	    &Apache::lonxml::get_all_text("/solved",$parser,$style);
1.159     albertel 1487: 	}
1.24      albertel 1488:     }
1.159     albertel 1489:     return '';
1.24      albertel 1490: }
                   1491: 
                   1492: sub end_solved {
1.248     albertel 1493:     return '';
                   1494: }
                   1495: 
                   1496: sub start_problemtype {
1.326     albertel 1497:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.248     albertel 1498:     my $result;
                   1499:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1500: 	$target eq 'tex' || $target eq 'analyze') {
                   1501: 	my $mode=lc(&Apache::lonxml::get_param('mode',$parstack,$safeeval));
                   1502: 	if (!defined($mode)) { $mode='show'; }
                   1503: 	my $for=&Apache::lonxml::get_param('for',$parstack,$safeeval);
                   1504: 	my $found=0;
                   1505: 	foreach my $type (split(',',$for)) {
                   1506: 	    if ($Apache::lonhomework::type eq lc($type)) { $found=1; }
                   1507: 	}
                   1508: 	if ($mode eq 'show' && !$found) {
1.326     albertel 1509: 	    &Apache::lonxml::get_all_text("/problemtype",$parser,$style);
1.248     albertel 1510: 	}
                   1511: 	if ($mode eq 'hide' && $found) {
1.326     albertel 1512: 	    &Apache::lonxml::get_all_text("/problemtype",$parser,$style);
1.248     albertel 1513: 	}
                   1514:     } elsif ($target eq 'edit') {
                   1515: 	$result .=&Apache::edit::tag_start($target,$token);
                   1516: 	$result.=&Apache::edit::select_arg('Mode:','mode',
                   1517: 					   [['show','Show'],
                   1518: 					    ['hide','Hide']]
                   1519: 					   ,$token);
                   1520: 	$result .=&Apache::edit::checked_arg('When used as type(s):','for',
                   1521: 					     [ ['exam','Exam/Quiz Problem'],
                   1522: 					       ['survey','Survey'],
                   1523: 					       ['problem','Homework Problem'] ]
                   1524: 					     ,$token);
                   1525: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1526:     } elsif ($target eq 'modified') {
                   1527: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1528: 						     $safeeval,'mode','for');
                   1529: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1530:     }
                   1531:     return $result;
                   1532: }
                   1533: 
                   1534: sub end_problemtype {
1.159     albertel 1535:     return '';
1.24      albertel 1536: }
1.34      albertel 1537: 
                   1538: sub start_startouttext {
1.159     albertel 1539:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1540:     my @result=(''.'');
                   1541:     if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
                   1542:     return (@result);
1.34      albertel 1543: }
1.159     albertel 1544: 
1.34      albertel 1545: sub end_startouttext {
1.326     albertel 1546:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1547:     my $result='';
                   1548:     my $text='';
                   1549: 
                   1550:     if ($target eq 'edit') {
1.326     albertel 1551: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser,$style);
1.211     albertel 1552: 	$result.=&Apache::edit::start_table($token)."<tr><td>".&mt('Text Block')."</td>
                   1553: <td>".&mt('Delete:').
1.159     albertel 1554:                  &Apache::edit::deletelist($target,$token)
                   1555: 		 ."</td>
1.42      albertel 1556: <td>".
1.159     albertel 1557:                  &Apache::edit::insertlist($target,$token).
                   1558: 		 &Apache::edit::end_row().
                   1559:                  &Apache::edit::start_spanning_row()."\n"
1.188     bowersj2 1560: 		 . &Apache::loncommon::helpLatexCheatsheet () .
1.255     www      1561: 		 &Apache::edit::editfield($token->[1],$text,"",80,8,1);
1.159     albertel 1562:     }
                   1563:     if ($target eq 'modified') {
1.219     albertel 1564: 	$result='<startouttext />'.&Apache::edit::modifiedfield("endouttext",$parser);
1.159     albertel 1565:     }
                   1566:     if ($target eq 'tex') {
                   1567: 	$result .= '\noindent ';
                   1568:     }
                   1569:     return $result;
1.34      albertel 1570: }
1.159     albertel 1571: 
1.34      albertel 1572: sub start_endouttext {
1.159     albertel 1573:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1574:     my $result='';
                   1575:     if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
                   1576:     if ($target eq "modified") {
                   1577: 	$result='<endouttext />'.
                   1578: 	    &Apache::edit::handle_insertafter('startouttext'); }
                   1579:     return $result;
1.34      albertel 1580: }
1.159     albertel 1581: 
1.34      albertel 1582: sub end_endouttext {
1.159     albertel 1583:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1584:     my @result=('','');
                   1585:     if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
                   1586:     return (@result);
1.34      albertel 1587: }
1.159     albertel 1588: 
1.45      albertel 1589: sub delete_startouttext {
1.326     albertel 1590:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1591:     #  my $text=&Apache::lonxml::get_all_text("endouttext",$parser,$style);
1.159     albertel 1592:     my $text=$$parser['-1']->get_text("/endouttext");
                   1593:     my $ntoken=$$parser['-1']->get_token();
                   1594:     &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
                   1595:     &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
                   1596:     # Deleting 2 parallel tag pairs, but we need the numbers later to look like
                   1597:     # they did the last time round
                   1598:     &Apache::lonxml::increasedepth($ntoken);
                   1599:     &Apache::lonxml::decreasedepth($ntoken);
                   1600:     return 1;
1.193     www      1601: }
                   1602: 
                   1603: sub start_simpleeditbutton {
                   1604:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1605:     my $result='';
1.284     albertel 1606:     if (($env{'form.simple_edit_button'} ne 'off') &&
1.273     albertel 1607: 	($target eq 'web') &&
1.330     albertel 1608:         (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
1.284     albertel 1609:         my $url=$env{'request.noversionuri'};
1.193     www      1610:         $url=~s/\?.*$//;
1.278     albertel 1611: 	my ($symb) = &Apache::lonxml::whichuser();
1.194     www      1612: 	$result='<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
1.278     albertel 1613:                 '<a href="'.$url.'/smpedit?symb='.&Apache::lonnet::escape($symb).'">'.&mt('Edit').'</a> - '.&mt('Note: it can take up to 10 minutes for changes to take effect for all users.').
1.196     www      1614: &Apache::loncommon::help_open_topic('Caching').'</td></tr></table><br />';
1.193     www      1615:     }
                   1616:     return $result;
                   1617: }
                   1618: 
                   1619: sub end_simpleeditbutton {
                   1620:     return '';
1.45      albertel 1621: }
1.34      albertel 1622: 
1.1       albertel 1623: 1;
                   1624: __END__

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