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

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.434   ! foxr        4: # $Id: structuretags.pm,v 1.433 2008/10/24 16:22:54 bisitz 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.434   ! foxr       39: use Apache::londefdef;
1.338     albertel   40: use Apache::lonenc();
1.267     albertel   41: use Time::HiRes qw( gettimeofday tv_interval );
1.356     www        42: use lib '/home/httpd/lib/perl/';
                     43: use LONCAPA;
                     44:  
1.78      harris41   45: BEGIN {
1.397     albertel   46:     &Apache::lonxml::register('Apache::structuretags',('block','languageblock','translated','instructorcomment','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','problemtype','startouttext','endouttext','simpleeditbutton','definetag'));
1.10      albertel   47: }
                     48: 
                     49: sub start_web {
1.326     albertel   50:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.383     albertel   51:     if ($target ne 'edit' && $target ne 'modified') {
                     52: 	my $bodytext=&Apache::lonxml::get_all_text("/web",$parser,$style);
                     53: 	if ($target eq 'web' || $target eq 'webgrade') {
                     54: 	    return $bodytext;
                     55: 	}
                     56:     } elsif ($target eq "edit" ) {
                     57: 	my $bodytext = 
                     58: 	    &Apache::lonxml::get_all_text_unbalanced("/web",$parser);
                     59: 	my $result = &Apache::edit::tag_start($target,$token);
                     60: 	$result .= &Apache::edit::editfield($token->[1],$bodytext,'',80,1);
                     61: 	return $result;
                     62:     } elsif ( $target eq "modified" ) {
                     63: 	return $token->[4].&Apache::edit::modifiedfield("/web",$parser);
1.159     albertel   64:     }
                     65:     return '';
1.10      albertel   66: }
                     67: 
                     68: sub end_web {
1.44      ng         69:     return '';
1.10      albertel   70: }
                     71: 
                     72: sub start_tex {
1.326     albertel   73:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.198     sakharuk   74:     my $result='';
1.383     albertel   75:     if ($target ne 'edit' && $target ne 'modified') {
                     76: 	my $bodytext=&Apache::lonxml::get_all_text("/tex",$parser,$style);
                     77: 	if ($target eq 'tex') {
1.434   ! foxr       78: 	    
        !            79: 	    # If inside a table, occurrences of \\ must be removed;
        !            80: 	    # else the table blows up.
        !            81: 
        !            82: 	    if (&Apache::londefdef::is_inside_of($tagstack, "table")) {
        !            83: 		$bodytext =~ s/\\\\//g;
        !            84: 	    }
1.432     foxr       85: 	    return $bodytext.'{}';
1.383     albertel   86: 	}
                     87:     } elsif ($target eq "edit" ) {
                     88: 	my $bodytext = 
                     89: 	    &Apache::lonxml::get_all_text_unbalanced("/tex",$parser);
                     90: 	my $result = &Apache::edit::tag_start($target,$token);
                     91: 	$result .= &Apache::edit::editfield($token->[1],$bodytext,'',80,1);
                     92: 	return $result;
                     93:     } elsif ( $target eq "modified" ) {
                     94: 	return $token->[4].&Apache::edit::modifiedfield("/tex",$parser);
1.159     albertel   95:     }
1.198     sakharuk   96:     return $result;;
1.10      albertel   97: }
                     98: 
                     99: sub end_tex {
1.44      ng        100:     return '';
1.9       albertel  101: }
                    102: 
1.400     albertel  103: sub homework_js {
                    104:     return &Apache::loncommon::resize_textarea_js().
1.416     raeburn   105:            &setmode_javascript().
1.400     albertel  106: 	<<'JS';
                    107: <script type="text/javascript">
                    108: function setSubmittedPart (part) {
                    109:    this.document.lonhomework.submitted.value="part_"+part;
                    110: }
                    111: 
                    112: function image_response_click (which, e) {
                    113:     init_geometry();
                    114:     if (!e) { e = window.event; } //IE
                    115:     var input_element = document.lonhomework.elements[which];
1.401     albertel  116:     var token_element = document.lonhomework.elements[which+'_token'];
1.400     albertel  117:     var token = token_element.value;
1.401     albertel  118:     var img_element   = document.getElementById(which+'_imageresponse');
1.400     albertel  119:     var x= e.clientX-getX(img_element)+Geometry.getHorizontalScroll();
                    120:     var y= e.clientY-getY(img_element)+Geometry.getVerticalScroll();
                    121:     var click = x+':'+y;
                    122:     input_element.value = click;
                    123:     img_element.src = '/adm/randomlabel.png?token='+token+'&clickdata='+click;
                    124: }
1.416     raeburn   125: 
1.400     albertel  126: </script>
                    127: JS
                    128: }
                    129: 
1.416     raeburn   130: sub setmode_javascript {
                    131:     return <<"ENDSCRIPT";
                    132: <script type="text/javascript">
                    133: function setmode(form,probmode) {
                    134:     form.problemmode.value = probmode;
                    135:     form.submit();
                    136: }
                    137: </script>
                    138: ENDSCRIPT
                    139: }
                    140: 
1.48      albertel  141: sub page_start {
1.345     albertel  142:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$name,
                    143: 	$extra_head)=@_;
1.159     albertel  144:     my %found;
1.207     albertel  145:     foreach my $taginside (@$tagstack) {
1.159     albertel  146: 	foreach my $taglookedfor ('html','body','form') {
                    147: 	    if ($taginside =~ /^$taglookedfor$/i) {$found{$taglookedfor} = 1;}
                    148: 	}
                    149:     }
                    150: 
1.343     albertel  151:     if ($target eq 'tex') {
                    152: 	return
                    153: 	    &Apache::londefdef::start_html($target,$token,$tagstack,
                    154: 					   $parstack,$parser,$safeeval);
                    155:     }
                    156: 
1.400     albertel  157:     $extra_head .= &homework_js();
1.374     albertel  158: 
1.434   ! foxr      159:     if ($env{'environment.wysiwygeditor'} eq 'on') {
        !           160: 	$extra_head .= &Apache::lonhtmlcommon::dragmath_js("FCKEditMathPopup");
        !           161:     } else {
        !           162:         $extra_head .= &Apache::lonhtmlcommon::dragmath_js("EditMathPopup");
1.425     raeburn   163:     }
                    164: 
1.344     albertel  165:     my %body_args;
                    166:     if (defined($found{'html'})) {
                    167: 	$body_args{'skip_phases'}{'head'}=1;
                    168:     } else {
1.343     albertel  169: 	
1.345     albertel  170: 	$extra_head .= &Apache::lonhtmlcommon::spellheader();
1.343     albertel  171: 
1.379     albertel  172: 	$extra_head .= &Apache::londefdef::generate_css_links();
                    173: 
1.384     albertel  174: 	if ($env{'request.state'} eq 'construct') {
1.343     albertel  175: 	    $extra_head.=&Apache::edit::js_change_detection().
                    176: 		"<script type=\"text/javascript\">\n".
                    177: 		"if (typeof swmenu != 'undefined') {swmenu.currentURL=null;}\n".
                    178: 		&Apache::loncommon::browser_and_searcher_javascript().
                    179:                 "\n</script>\n";
                    180: 	}
1.159     albertel  181:     }
1.343     albertel  182: 
1.344     albertel  183:     if (defined($found{'body'})) {
                    184: 	$body_args{'skip_phases'}{'body'}=1;
                    185:     } elsif (!defined($found{'body'}) 
                    186: 	     && $env{'request.state'} eq 'construct') {
1.343     albertel  187: 	if ($target eq 'web' || $target eq 'edit') {
1.402     albertel  188: 	    if ($env{'environment.remote'} ne 'off') {
                    189: 		$body_args{'only_body'}  = 1;
                    190: 	    }
1.297     albertel  191: 	}
1.272     albertel  192:     } elsif (!defined($found{'body'})) {
1.343     albertel  193: 	my %add_entries;
1.159     albertel  194: 	my $background=&Apache::lonxml::get_param('background',$parstack,
                    195: 						  $safeeval);
1.343     albertel  196: 	if ($background ne '' ) {
                    197: 	    $add_entries{'background'} = $background;
                    198: 	}
1.344     albertel  199: 
1.290     albertel  200: 	my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
                    201: 					       $safeeval);
1.344     albertel  202:        	if ($bgcolor eq '' ) { $bgcolor = '#FFFFFF'; }
                    203: 
                    204: 	$body_args{'bgcolor'}        = $bgcolor;
                    205: 	$body_args{'no_title'}       = 1;
                    206: 	$body_args{'force_register'} = 1;
                    207: 	$body_args{'add_entries'}    = \%add_entries;	
1.391     albertel  208: 	if ($env{'environment.remote'} eq 'off'
                    209: 	    && $env{'request.state'} eq   'construct') {
                    210: 	    $body_args{'only_body'}  = 1;
                    211: 	}
1.344     albertel  212:     }
1.365     albertel  213:     $body_args{'no_auto_mt_title'} = 1;
1.344     albertel  214:     my $page_start = &Apache::loncommon::start_page($name,$extra_head,
                    215: 						    \%body_args);
                    216: 
                    217:     if (!defined($found{'body'}) 
                    218: 	&& $env{'request.state'} ne 'construct'
                    219: 	&& ($target eq 'web' || $target eq 'webgrade')) {
                    220: 
1.367     albertel  221: 	my ($symb,undef,undef,undef,$publicuser)= &Apache::lonnet::whichuser();
1.344     albertel  222: 	if ($symb eq '' && !$publicuser) {
                    223: 	    my $help = &Apache::loncommon::help_open_topic("Ambiguous_Reference");
                    224: 	    $help=&mt("Browsing resource, all submissions are temporary.")."<br />";
                    225: 	    $page_start .= $help;
1.159     albertel  226: 	}
1.344     albertel  227:     }
                    228: 
1.409     albertel  229:     if (!defined($found{'body'}) && $env{'request.state'} ne 'construct') {
1.343     albertel  230: 	$page_start .= &Apache::lonxml::message_location();
1.159     albertel  231:     }
1.344     albertel  232:     
1.159     albertel  233:     my $form_tag_start;
                    234:     if (!defined($found{'form'})) {
1.337     albertel  235: 	$form_tag_start='<form name="lonhomework" enctype="multipart/form-data" method="post" action="';
1.338     albertel  236: 	my $uri = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.327     albertel  237: 	$form_tag_start.=$uri.'" ';
                    238: 	if ($target eq 'edit') {
                    239: 	    $form_tag_start.=&Apache::edit::form_change_detection();
                    240: 	}
1.368     albertel  241: 	$form_tag_start.='>'."\n";
1.355     albertel  242: 
                    243: 	my $symb=&Apache::lonnet::symbread();
                    244: 	if ($symb =~ /\S/) {
                    245: 	    $symb=
                    246: 		&HTML::Entities::encode(&Apache::lonenc::check_encrypt($symb));
                    247: 	    $form_tag_start.=
1.368     albertel  248: 		"\t".'<input type="hidden" name="symb" value="'.$symb.'" />'."\n";
1.355     albertel  249: 	}
1.159     albertel  250:     }
1.343     albertel  251:     return ($page_start,$form_tag_start);
1.105     albertel  252: }
                    253: 
1.141     matthew   254: #use Time::HiRes();
1.105     albertel  255: sub get_resource_name {
1.159     albertel  256:     my ($parstack,$safeeval)=@_;
1.388     foxr      257:     my $name;
1.204     albertel  258:     if (defined($Apache::lonhomework::name)) {
1.388     foxr      259: 	$name = $Apache::lonhomework::name;
                    260:     } else {
                    261: 	my ($symb)=&Apache::lonnet::whichuser();
1.392     albertel  262: 	$name=&Apache::lonnet::gettitle($symb);
1.388     foxr      263: 	if ($name eq '') {
                    264: 	    $name=&Apache::lonnet::EXT('resource.title');
                    265: 	    if ($name eq 'con_lost') { $name = ''; }
                    266: 	}
                    267: 	if ($name!~/\S+/) {
                    268: 	    $name=$env{'request.uri'};
                    269: 	    $name=~s-.*/([^/]+)$-$1-;
                    270: 	}
                    271: 	# The name has had html tags escaped:
                    272:        
                    273: 	$name=~s/&lt;/</gs;
                    274: 	$name=~s/&gt;/>/gs;
                    275: 
                    276: 	$Apache::lonhomework::name=$name;
1.204     albertel  277:     }
1.159     albertel  278:     return $name;
1.105     albertel  279: }
                    280: 
                    281: sub setup_rndseed {
1.159     albertel  282:     my ($safeeval)=@_;
                    283:     my $rndseed;
1.367     albertel  284:     my ($symb)=&Apache::lonnet::whichuser();
1.333     albertel  285:     if ($env{'request.state'} eq "construct" 
                    286: 	|| $symb eq '' 
                    287: 	|| $Apache::lonhomework::type eq 'practice'
                    288: 	|| $Apache::lonhomework::history{'resource.CODE'}) {
1.316     www       289: 	&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.317     albertel  290: 						['rndseed']);
1.284     albertel  291: 	$rndseed=$env{'form.rndseed'};
1.159     albertel  292: 	if (!$rndseed) {
1.162     albertel  293: 	    $rndseed=$Apache::lonhomework::history{'rndseed'};
                    294: 	    if (!$rndseed) {
                    295: 		$rndseed=time;
                    296: 	    }
1.284     albertel  297: 	    $env{'form.rndseed'}=$rndseed;
1.162     albertel  298: 	}
1.374     albertel  299: 	if ( ($env{'form.resetdata'} eq &mt('New Problem Variation')
                    300: 	      && $env{'form.submitted'} eq 'yes')  ||
1.284     albertel  301: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
1.190     albertel  302: 	    srand(time);
                    303: 	    $rndseed=int(rand(2100000000));
1.284     albertel  304: 	    $env{'form.rndseed'}=$rndseed;
                    305: 	    delete($env{'form.resetdata'});
                    306: 	    delete($env{'form.newrandomization'});
1.159     albertel  307: 	}
1.187     albertel  308: 	if (defined($rndseed) && $rndseed ne int($rndseed)) {
1.307     albertel  309: 	    $rndseed=join(':',&Apache::lonnet::digest($rndseed));
1.187     albertel  310:         }
1.247     albertel  311:         if ($Apache::lonhomework::history{'resource.CODE'}) {
                    312: 	   $rndseed=&Apache::lonnet::rndseed();
                    313: 	}
1.221     albertel  314: 	if ($safeeval) {
                    315: 	    &Apache::lonxml::debug("Setting rndseed to $rndseed");
1.250     albertel  316: 	    &Apache::run::run('$external::randomseed="'.$rndseed.'";',$safeeval);
1.221     albertel  317: 	}
1.159     albertel  318:     }
                    319:     return $rndseed;
1.105     albertel  320: }
                    321: 
1.268     albertel  322: sub remember_problem_state {
                    323:     return '
1.284     albertel  324:        <input type="hidden" name="problemstate" value="'.$env{'form.problemstate'}.'" />
                    325:        <input type="hidden" name="problemtype" value="'.$env{'form.problemtype'}.'" />
                    326:        <input type="hidden" name="problemstatus" value="'.$env{'form.problemstatus'}.'" />';
1.268     albertel  327: }
                    328: 
1.423     www       329: sub problem_edit_buttons {
                    330:    return  '
1.408     albertel  331: <div class="LC_edit_problem_discards">
1.416     raeburn   332:        <input type="button" name="submitmode" accesskey="d" value="'.&mt('Discard Edits and View').'" '.
1.423     www       333:        ' onclick="javscript:setmode(this.form,'."'discard'".')"  />
1.416     raeburn   334:        <input '.&Apache::edit::submit_ask_anyway('setmode(this.form,'."'editxml'".')').' type="button" name="submitmode" accesskey="x" value="'.&mt('EditXML').'" />
1.407     albertel  335:        <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />
1.408     albertel  336: </div>
                    337: <div class="LC_edit_problem_saves">
1.416     raeburn   338:        <input type="submit" name="submitbutton" accesskey="s" value="'.&mt('Save and Edit').'" />
                    339:        <input type="submit" name="submitbutton" accesskey="v" value="'.&mt('Save and View').'" />
1.423     www       340: </div>';
                    341: }
                    342: 
                    343: sub problem_edit_header {
                    344:     return '<input type="hidden" name="submitted" value="edit" /><input type="hidden" name="problemmode" value="edit" />'.
                    345: 	&Apache::structuretags::remember_problem_state().'
                    346: <div class="LC_edit_problem_header">
                    347: <div class="LC_edit_problem_header_title">
                    348: '.&mt('Problem Editing').&Apache::loncommon::help_open_menu('Problem Editing','Problem_Editor_XML_Index',5,'Authoring').'
                    349: </div>'.
                    350: &problem_edit_buttons().'
1.408     albertel  351: <hr class="LC_edit_problem_divide" />
1.409     albertel  352: '.&Apache::lonxml::message_location().'
1.408     albertel  353: </div>
                    354: '.
1.282     albertel  355:        '<table border="0" width="100%"><tr><td bgcolor="#DDDDDD">';
1.105     albertel  356: }
                    357: 
                    358: sub problem_edit_footer {
1.412     albertel  359:     return '</td></tr></table><br />
                    360: <div class="LC_edit_problem_footer">
1.423     www       361:   <hr class="LC_edit_problem_divide" />'.
                    362: &problem_edit_buttons().'
1.412     albertel  363:   <hr class="LC_edit_problem_divide" />
                    364: </div>
                    365: '.
                    366: 
1.348     albertel  367:     &Apache::lonhtmlcommon::htmlareaselectactive(&Apache::lonhtmlcommon::get_htmlareafields()).
1.342     albertel  368:     "\n</form>\n".&Apache::loncommon::end_page();
1.105     albertel  369: }
                    370: 
1.235     albertel  371: sub option {
                    372:     my ($value,$name) = @_;
                    373:     my $result ="<option value='".$value."' ";
1.284     albertel  374:     if ($env{'form.'.$name} eq $value) {
1.235     albertel  375: 	$result.=" selected='on' ";
                    376:     }
                    377:     $result.='>';
                    378:     return $result;
                    379: }
                    380: 
1.105     albertel  381: sub problem_web_to_edit_header {
1.159     albertel  382:     my ($rndseed)=@_;
1.406     albertel  383:     my $result .= '<div class="LC_edit_problem_header">';
                    384: 
                    385:     if (!$Apache::lonhomework::parsing_a_task) {
                    386: 	$result .= 
                    387: 	    '<div class="LC_edit_problem_header_title">'.
                    388: 	    &mt('Problem Testing').
                    389: 	    &Apache::loncommon::help_open_topic('Problem_Editor_Testing_Area').
                    390: 	    '</div>';
                    391:     } else {
                    392: 	$result .= 
                    393: 	    '<div class="LC_edit_problem_header_title">'.
                    394: 	    &mt('Task Testing').
                    395: 	    '</div>';
                    396:     }
                    397:     
1.315     albertel  398:     my $show_all_foils_text = 
                    399: 	($Apache::lonhomework::parsing_a_task) ?
                    400: 	&mt('&nbsp;Show&nbsp;All&nbsp;Instances')
                    401: 	: &mt('&nbsp;Show&nbsp;All&nbsp;Foils');
                    402: 
1.406     albertel  403:     my $show_all= '<span class="LC_nobreak"><label><input type="checkbox" name="showallfoils" ';
                    404:     if (defined($env{'form.showallfoils'})) { $show_all.='checked="checked"'; }
                    405:     $show_all.= ' />'.$show_all_foils_text.'</label></span>';
                    406: 
                    407: 
1.384     albertel  408: 
1.406     albertel  409:     $result .= '<div class="LC_edit_problem_header_status_row">';
1.313     albertel  410:     if (!$Apache::lonhomework::parsing_a_task) {
                    411: 	$result.="
1.406     albertel  412: <div class='LC_edit_problem_header_row1'>
                    413: <span class=\"LC_nobreak\">
1.405     albertel  414: ".&mt("Problem Status:")."
1.235     albertel  415: <select name='problemstate'>
1.270     albertel  416:   <option value=''></option>
1.235     albertel  417:   ".&option('CLOSED'               ,'problemstate').&mt("Closed")."</option>
                    418:   ".&option('CAN_ANSWER'           ,'problemstate').&mt("Answerable")."</option>
                    419:   ".&option('CANNOT_ANSWER_tries'  ,'problemstate').&mt("Open with full tries")."</option>
                    420:   ".&option('CANNOT_ANSWER_correct','problemstate').&mt("Open and correct")."</option>
                    421:   ".&option('SHOW_ANSWER'          ,'problemstate').&mt("Show Answer")."</option>
                    422: </select>
1.406     albertel  423: </span>
                    424: <span class=\"LC_nobreak\">
1.405     albertel  425: ".&mt("Problem Type:")."
1.235     albertel  426: <select name='problemtype'>
1.270     albertel  427:   <option value=''></option>
1.242     albertel  428:   ".&option('exam'   ,'problemtype').&mt("Exam Problem")."</option>
1.428     raeburn   429:   ".&option('problem','problemtype').&mt("Homework Problem")."</option>
1.242     albertel  430:   ".&option('survey' ,'problemtype').&mt("Survey Question")."</option>
1.428     raeburn   431:   ".&option('practice' ,'problemtype').&mt("Practice Problem")."</option>
1.235     albertel  432: </select>
1.406     albertel  433: </span>
                    434: $show_all
                    435: </div>
                    436: <div class='LC_edit_problem_header_row2'>
                    437: <span class=\"LC_nobreak\">
1.405     albertel  438: ".&mt("Feedback Mode:")."
1.235     albertel  439: <select name='problemstatus'>
                    440:   <option value=''></option>
1.242     albertel  441:   ".&option('yes','problemstatus').&mt("Show Feedback")."</option>
1.405     albertel  442:   ".&option('no', 'problemstatus').&mt("Don't Show Incorect/Correct Feedback")."</option>
                    443:   ".&option('no_feedback_ever', 'problemstatus').&mt("Don't Show Any Feedback")."</option>
1.235     albertel  444: </select>
1.406     albertel  445: </span>
                    446: ";
                    447: 
1.376     albertel  448:     } elsif ($Apache::lonhomework::parsing_a_task) {
                    449: 	$result.="
1.406     albertel  450: <div class='LC_edit_problem_header_row1'>
                    451: <span class=\"LC_nobreak\">
1.405     albertel  452: ".&mt("Problem Status:")."
1.376     albertel  453: <select name='problemstate'>
                    454:   <option value=''></option>
                    455:   ".&option('CLOSED'               ,'problemstate').&mt("Closed")."</option>
                    456:   ".&option('CAN_ANSWER'           ,'problemstate').&mt("Answerable")."</option>
                    457:   ".&option('WEB_GRADE'            ,'problemstate').&mt("Criteria Grading")."</option>
                    458:   ".&option('SHOW_ANSWER'          ,'problemstate').&mt("Show Feedback")."</option>
                    459: </select>
1.406     albertel  460: </span>
                    461: $show_all
                    462: ";
                    463:     }
                    464:     $result.='
                    465:        <span class="LC_nobreak">
                    466:        '.&mt('Apply style file: ').'
                    467:          <input type="text" name="style_file" value="'.&HTML::Entities::encode($env{'construct.style'},'"<>&').'" />
                    468:          <a href="javascript:openbrowser(\'lonhomework\',\'style_file\',\'sty\')">'.&mt('Select').'</a>
                    469:        </span>
1.422     www       470:      </div>
                    471:      <div class="LC_edit_problem_header_row1">'.
                    472:        &Apache::lonxml::renderingoptions().'
1.406     albertel  473:      </div>
                    474:      <input type="submit" name="changeproblemmode" value="'.&mt("Change View").'" />
                    475:      <input type="submit" name="clear_style_file" accesskey="d" value="'.&mt('Show Default View').'" />
                    476:      <input type="submit" name="resetdata" accesskey="r" value="'.&mt('Reset Submissions').'" />
                    477:    </div>
                    478:    <hr class="LC_edit_problem_divide" />
                    479:    <div class="LC_edit_problem_header_randomize_row">
                    480:      <input type="submit" name="newrandomization" accesskey="a" value="'.&mt('New Randomization').'" />
                    481:      <input type="submit" name="changerandseed" value="'.&mt('Change Random Seed To:').'" />
                    482:      <input type="text" name="rndseed" size="10" value="'.
                    483: 	       $rndseed.'"
                    484:              onchange="javascript:document.lonhomework.changerandseed.click()" />';
                    485: 
                    486:     if (!$Apache::lonhomework::parsing_a_task) {
                    487: 	my $numtoanalyze=$env{'form.numtoanalyze'};
                    488: 	if (!$numtoanalyze) { $numtoanalyze=20; }
1.408     albertel  489: 	$result .= '<span class="LC_nobreak">'.
                    490: 	    &mt('[_1] for [_2] versions.',
1.416     raeburn   491: 		'<input type="button" name="submitmode" value="'.&mt('Calculate answers').'" '.
1.419     bisitz    492:                 'onclick="javascript:setmode(this.form,'."'calcanswers'".')" />'
                    493:                ,'<input type="text" name="numtoanalyze" value="'.
1.408     albertel  494: 		$numtoanalyze.'" size="5" />').
                    495: 		&Apache::loncommon::help_open_topic("Analyze_Problem",'',undef,undef,300).
                    496: 		'</span>';
                    497: 						    
1.313     albertel  498:     }
1.406     albertel  499: 
                    500:     $result.='
                    501:    </div>
1.408     albertel  502:    <div class="LC_edit_problem_header_edit_row">';
1.416     raeburn   503:     $result.='<input type="hidden" name="problemmode" value="view" />';
                    504:     $result .= '<input type="button" name="submitmode" accesskey="e" value="'.&mt('Edit').'" '.
                    505:                'onclick="javascript:setmode(this.form,'."'edit'".')" />';
                    506:     $result .= '<input type="button" name="submitmode" accesskey="x" value="'.&mt('EditXML').'" '.
                    507:                'onclick="javascript:setmode(this.form,'."'editxml'".')" />';
1.408     albertel  508:     $result.='
                    509:    </div>
1.409     albertel  510:    '.&Apache::lonxml::message_location().'
1.406     albertel  511: </div>';
1.159     albertel  512:     return $result;
1.48      albertel  513: }
                    514: 
1.65      albertel  515: sub initialize_storage {
1.357     albertel  516:     my ($given_symb) = @_;
1.353     albertel  517:     undef(%Apache::lonhomework::results);
                    518:     undef(%Apache::lonhomework::history);
1.357     albertel  519:     my ($symb,$courseid,$domain,$name) = 
1.367     albertel  520: 	&Apache::lonnet::whichuser($given_symb);
1.353     albertel  521:     
                    522:     # anonymous users (CODEd exams) have no data
                    523:     if ($name eq 'anonymous' 
                    524: 	&& !defined($domain)) {
                    525: 	return;
                    526:     }
                    527: 
1.333     albertel  528:     if ($env{'request.state'} eq 'construct' 
                    529: 	|| $symb eq ''
                    530: 	|| $Apache::lonhomework::type eq 'practice') {
                    531: 	
                    532: 	my $namespace = $symb || $env{'request.uri'};
                    533: 	if ($env{'form.resetdata'} eq &mt('Reset Submissions') ||
1.374     albertel  534: 	    ($env{'form.resetdata'} eq &mt('New Problem Variation')
                    535: 	     && $env{'form.submitted'} eq 'yes') ||
1.333     albertel  536: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
                    537: 	    &Apache::lonnet::tmpreset($namespace,'',$domain,$name);
                    538: 	    &Apache::lonxml::debug("Attempt reset");
                    539: 	}
1.159     albertel  540: 	%Apache::lonhomework::history=
1.333     albertel  541: 	    &Apache::lonnet::tmprestore($namespace,'',$domain,$name);
1.159     albertel  542: 	my ($temp)=keys %Apache::lonhomework::history ;
                    543: 	&Apache::lonxml::debug("Return message of $temp");
                    544:     } else {
                    545: 	%Apache::lonhomework::history=
                    546: 	    &Apache::lonnet::restore($symb,$courseid,$domain,$name);
                    547:     }
1.353     albertel  548: 
1.159     albertel  549:     #ignore error conditions
1.67      albertel  550:     my ($temp)=keys %Apache::lonhomework::history ;
1.159     albertel  551:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
1.65      albertel  552: }
                    553: 
                    554: # -------------------------------------------------------------finalize_storage
                    555: # Stores away the result has to a student's environment
                    556: # checks form.grade_ for specific values, other wises stores
                    557: # to the running users environment
1.285     raeburn   558: # Will increment totals for attempts, students, and corrects
                    559: # if running user has student role.  
1.65      albertel  560: sub finalize_storage {
1.357     albertel  561:     my ($given_symb) = @_;
1.159     albertel  562:     my $result;
1.289     albertel  563:     if (%Apache::lonhomework::results) {
1.323     albertel  564: 	my @remove = grep(/^INTERNAL_/,keys(%Apache::lonhomework::results));
                    565: 	delete(@Apache::lonhomework::results{@remove});
1.357     albertel  566: 	my ($symb,$courseid,$domain,$name) = 
1.367     albertel  567: 	    &Apache::lonnet::whichuser($given_symb);
1.333     albertel  568: 	if ($env{'request.state'} eq 'construct' 
                    569: 	    || $symb eq ''
                    570: 	    || $Apache::lonhomework::type eq 'practice') {
                    571: 	    my $namespace = $symb || $env{'request.uri'};
1.284     albertel  572: 	    $Apache::lonhomework::results{'rndseed'}=$env{'form.rndseed'};
1.159     albertel  573: 	    $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
1.333     albertel  574: 					      $namespace,'',$domain,$name);
1.159     albertel  575: 	    &Apache::lonxml::debug('Construct Store return message:'.$result);
                    576: 	} else {
                    577: 	    $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
                    578: 					    $symb,$courseid,$domain,$name);
                    579: 	    &Apache::lonxml::debug('Store return message:'.$result);
1.285     raeburn   580:             if ($env{'request.role'} =~/^st/) {
                    581:                 &store_aggregates($symb,$courseid);
                    582:             }
1.159     albertel  583: 	}
1.323     albertel  584:     } else {
                    585: 	&Apache::lonxml::debug('Nothing to store');
1.67      albertel  586:     }
1.159     albertel  587:     return $result;
1.65      albertel  588: }
                    589: 
1.285     raeburn   590: # -------------------------------------------------------------store_aggregates
                    591: # Sends hash of values to be incremented in nohist_resourcetracker.db
                    592: # for the course. Increments total number of attempts, unique students 
                    593: # and corrects for each part for an instance of a problem, as appropriate.
                    594: sub store_aggregates {
                    595:     my ($symb,$courseid) = @_;
1.286     albertel  596:     my %aggregate;
                    597:     my @parts;
1.288     albertel  598:     my $cdomain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    599:     my $cname = $env{'course.'.$env{'request.course.id'}.'.num'};
1.286     albertel  600:     foreach my $key (keys(%Apache::lonhomework::results)) {
1.287     albertel  601:         if ($key =~ /resource\.([^\.]+)\.tries/) {
1.286     albertel  602:             push(@parts, $1);
1.285     raeburn   603:         }
                    604:     }
1.286     albertel  605:     foreach my $part (@parts) {
                    606:         if ($Apache::lonhomework::results{'resource.'.$part.'.award'}
                    607: 	    eq 'APPROX_ANS' ||
                    608: 	    $Apache::lonhomework::results{'resource.'.$part.'.award'}
                    609: 	    eq 'EXACT_ANS') {
1.287     albertel  610:             $aggregate{$symb."\0".$part."\0correct"} = 1;
1.285     raeburn   611:         }
1.286     albertel  612:         if ($Apache::lonhomework::results{'resource.'.$part.'.tries'} == 1) {
1.287     albertel  613:             $aggregate{$symb."\0".$part."\0users"} = 1;
1.292     raeburn   614:         } else {
1.293     albertel  615:             my (undef,$last_reset) = &Apache::grades::get_last_resets($symb,$env{'request.course.id'},[$part]); 
1.292     raeburn   616:             if ($last_reset) {
1.293     albertel  617:                 if (&Apache::grades::get_num_tries(\%Apache::lonhomework::history,$last_reset,$part) == 0) {
1.292     raeburn   618:                     $aggregate{$symb."\0".$part."\0users"} = 1;
                    619:                 }
                    620:             }
1.285     raeburn   621:         }
1.287     albertel  622:         $aggregate{$symb."\0".$part."\0attempts"} = 1;
1.285     raeburn   623:     }
1.292     raeburn   624:     if (keys (%aggregate) > 0) {
1.289     albertel  625: 	&Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
1.292     raeburn   626:                             $cdomain,$cname);
                    627:     }
                    628: }
1.289     albertel  629: 
1.65      albertel  630: sub checkout_msg {
1.211     albertel  631:     my %lt=&Apache::lonlocal::texthash( 
                    632: 		'resource'=>'The resource needs to be checked out',
                    633: 		'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.',
                    634:                 'warning'=>'Checking out resources is subject to course policies, and may exclude future credit even if done erroneously.',
                    635:                 'checkout'=>'Check out Exam for Viewing',
                    636: 		'checkout?'=>'Check out Exam?');
1.352     albertel  637:     my $uri = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.159     albertel  638:     return (<<ENDCHECKOUT);
1.211     albertel  639: <h2>$lt{'resource'}</h2>
                    640:     <p>$lt{'id_expln'}</p>
                    641: <font color="red">
                    642: <p>$lt{'warning'}</p>
1.91      albertel  643: </font>
1.352     albertel  644: <form name="checkout" method="POST" action="$uri">
1.91      albertel  645: <input type="hidden" name="doescheckout" value="yes" />
1.211     albertel  646: <input type="button" name="checkoutbutton" value="$lt{'checkout'}" onClick="javascript:if (confirm('$lt{'checkout?'}')) { document.checkout.submit(); }" />
1.65      albertel  647: </form>
                    648: ENDCHECKOUT
                    649: }
                    650: 
1.252     albertel  651: sub firstaccess_msg {
1.253     albertel  652:     my ($time,$symb)=@_;
1.414     albertel  653:     my $result;
                    654:     my @interval=&Apache::lonnet::EXT("resource.0.interval");
                    655:     if ($interval[1] eq 'map') {
                    656: 	my ($map)=&Apache::lonnet::decode_symb($symb);
                    657: 	my $foldertitle=&Apache::lonnet::gettitle($map);
                    658:     
                    659: 	&Apache::lonxml::debug("map is $map title is $foldertitle");
1.418     bisitz    660: 	$result .= "<h2>".&mt('The resources in "[_1]" are open for a limited time.'
                    661:                              .' Once you click the "Show Resource" button below you have [_2] to complete all resources "[_1]".'
                    662:                              ,$foldertitle,$time)."</h2>";
1.414     albertel  663:     } elsif ($interval[1] eq 'course') {
                    664: 	my $course = $env{'course.'.$env{'request.course.id'}.'.description'};
1.418     bisitz    665:         $result .= "<h2>".&mt('The resources in "[_1]" are open for a limited time.'
                    666:                              .' Once you click the "Show Resource" button below you have [_2] to complete all resources "[_1]".'
                    667:                              ,$course,$time)."</h2>";
1.414     albertel  668:     } else {
                    669: 	my $title=&Apache::lonnet::gettitle($symb);
1.418     bisitz    670:         $result .= "<h2>".&mt('This resource "[_1]" is open for a limited time.'
                    671:                              .' Once you click the "Show Resource" button below you have [_2] to complete this resource "[_1]".'
                    672:                              ,$title,$time)."</h2>";
1.414     albertel  673:     }
1.352     albertel  674:     my $uri = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.418     bisitz    675:     my $buttontext = &mt('Show Resource');
                    676:     my $timertext = &mt('Start Timer?');
1.414     albertel  677:     $result .= (<<ENDCHECKOUT);
1.352     albertel  678: <form name="markaccess" method="POST" action="$uri">
1.252     albertel  679: <input type="hidden" name="markaccess" value="yes" />
1.418     bisitz    680: <input type="button" name="accessbutton" value="$buttontext" onClick="javascript:if (confirm('$timertext')) { document.markaccess.submit(); }" />
1.252     albertel  681: </form>
                    682: ENDCHECKOUT
1.414     albertel  683:     return $result;
1.252     albertel  684: }
                    685: 
1.204     albertel  686: sub init_problem_globals {
                    687:     my ($type)=@_;
                    688:     #initialize globals
1.308     foxr      689:     #   For problems, we start out in part 0 (outside a <part> tag).
                    690:     #   and part 0 is used to describe the main body of the <problem>
                    691:     #
1.204     albertel  692:     if ($type eq 'problem') {
                    693: 	$Apache::inputtags::part='0';
                    694: 	@Apache::inputtags::partlist=('0');
1.405     albertel  695: 	&Apache::lonhomework::set_show_problem_status(&get_problem_status('0'));
1.266     albertel  696: 	$Apache::lonhomework::ignore_response_errors=0;
1.308     foxr      697: 
1.266     albertel  698:     } elsif ($type eq 'library') {
1.204     albertel  699: 	$Apache::inputtags::part='';
                    700: 	@Apache::inputtags::partlist=();
1.405     albertel  701: 	&Apache::lonhomework::reset_show_problem_status();
1.266     albertel  702: 	$Apache::lonhomework::ignore_response_errors=1;
1.308     foxr      703: 
1.304     albertel  704:     } elsif ($type eq 'Task') {
                    705: 	$Apache::inputtags::part='0';
                    706: 	@Apache::inputtags::partlist=('0');
1.405     albertel  707: 	&Apache::lonhomework::reset_show_problem_status();
1.304     albertel  708: 	$Apache::lonhomework::ignore_response_errors=1;
1.204     albertel  709:     }
                    710:     @Apache::inputtags::responselist = ();
                    711:     @Apache::inputtags::importlist = ();
                    712:     @Apache::inputtags::previous=();
                    713:     @Apache::inputtags::previous_version=();
                    714:     $Apache::structuretags::printanswer='No';
                    715:     @Apache::structuretags::whileconds=();
                    716:     @Apache::structuretags::whilebody=();
                    717:     @Apache::structuretags::whileline=();
                    718:     $Apache::lonhomework::scantronmode=0;
                    719:     undef($Apache::lonhomework::name);
1.358     albertel  720:     undef($Apache::lonhomework::default_type);
                    721:     undef($Apache::lonhomework::type);
1.204     albertel  722: }
                    723: 
                    724: sub reset_problem_globals {
                    725:     my ($type)=@_;
                    726:     undef(%Apache::lonhomework::history);
                    727:     undef(%Apache::lonhomework::results);
                    728:     undef($Apache::inputtags::part);
1.208     albertel  729: #don't undef this, lonhomework.pm takes care of this, we use this to 
                    730: #detect if we try to do 2 problems in one file
                    731: #   undef($Apache::lonhomework::parsing_a_problem);
1.204     albertel  732:     undef($Apache::lonhomework::name);
1.358     albertel  733:     undef($Apache::lonhomework::default_type);
                    734:     undef($Apache::lonhomework::type);
                    735:     undef($Apache::lonhomework::scantronmode);
                    736:     undef($Apache::lonhomework::ignore_response_errors);
1.405     albertel  737:     &Apache::lonhomework::reset_show_problem_status();
1.204     albertel  738: }
                    739: 
1.241     albertel  740: sub set_problem_state {
1.240     albertel  741:     my ($part)=@_;
1.284     albertel  742:     if ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct') {
1.240     albertel  743: 	$Apache::lonhomework::history{"resource.$part.solved"}=
                    744: 	    'correct_by_student';
                    745:     }
                    746: }
                    747: 
1.241     albertel  748: sub get_problem_status {
                    749:     my ($part)=@_;
1.267     albertel  750:     my $problem_status;
1.284     albertel  751:     if ($env{'request.state'} eq 'construct' &&
                    752: 	defined($env{'form.problemstatus'})) {
                    753: 	$problem_status=$env{'form.problemstatus'};
1.267     albertel  754:     } else {
                    755: 	$problem_status=&Apache::lonnet::EXT("resource.$part.problemstatus");
                    756: 	&Apache::lonxml::debug("problem status for $part is $problem_status");
1.284     albertel  757: 	&Apache::lonxml::debug("env probstat is ".$env{'form.problemstatus'});
1.241     albertel  758:     }
                    759:     return $problem_status;
                    760: }
                    761: 
1.9       albertel  762: sub start_problem {
1.326     albertel  763:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.19      albertel  764: 
1.311     foxr      765:     # We'll use the redirection to fix up printing of duedates.
1.321     albertel  766:     if (!$Apache::lonxml::metamode) {
                    767: 	&Apache::lonxml::startredirection();
                    768:     }
1.311     foxr      769: 
1.308     foxr      770:     # Problems don't nest and we don't allow more than one <problem> in
                    771:     # a .problem file.
                    772:     #
1.184     albertel  773:     if ( $Apache::inputtags::part ne '' ||
                    774: 	 $Apache::lonhomework::parsing_a_problem) {
                    775: 	&Apache::lonxml::error('Only one &lt;problem&gt; allowed in a .problem file');
1.326     albertel  776: 	#my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,$style);
1.159     albertel  777: 	return '';
                    778:     }
1.184     albertel  779: 
                    780:     $Apache::lonhomework::parsing_a_problem=1;
1.204     albertel  781:     &init_problem_globals('problem');
1.166     albertel  782: 
1.284     albertel  783:     if (defined($env{'scantron.maxquest'})) {
1.166     albertel  784: 	$Apache::lonhomework::scantronmode=1;
                    785:     }
1.161     albertel  786: 
1.159     albertel  787:     if ($target ne 'analyze') {
1.415     raeburn   788:         my $type = &Apache::lonnet::EXT('resource.0.type');
                    789: 	$Apache::lonhomework::type=$type;
1.284     albertel  790: 	if (($env{'request.state'} eq 'construct') &&
1.410     albertel  791: 	    $env{'form.problemtype'} =~ /\S/) {
1.284     albertel  792: 	    $Apache::lonhomework::type=$env{'form.problemtype'};
1.237     albertel  793: 	}
1.332     albertel  794: 	&Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
1.159     albertel  795:     }
1.164     albertel  796:     if ($Apache::lonhomework::type eq '' ) {
1.284     albertel  797: 	my $uri=$env{'request.uri'};
1.159     albertel  798: 	if ($uri=~/\.(\w+)$/) {
                    799: 	    $Apache::lonhomework::type=$1;
                    800: 	    &Apache::lonxml::debug("Using type of $1");
                    801: 	} else {
                    802: 	    $Apache::lonhomework::type='problem';
                    803: 	    &Apache::lonxml::debug("Using default type, problem, :$uri:");
                    804: 	}
1.87      albertel  805:     }
1.301     albertel  806:     $Apache::lonhomework::default_type = $Apache::lonhomework::type;
1.58      www       807: 
1.363     albertel  808:     &initialize_storage();
1.389     albertel  809:     if ($target ne 'analyze'
                    810:        	&& $env{'request.state'} eq 'construct') {
                    811: 	&set_problem_state('0');
                    812:     }
                    813: 
1.366     albertel  814:     if ($target eq 'web') {
                    815: 	&Apache::lonxml::debug(" grading history ");
                    816: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
                    817:     }
1.363     albertel  818: 
1.159     albertel  819:     #added vars to the scripting enviroment
1.213     albertel  820:     my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.248     albertel  821:     $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.24      albertel  822:     &Apache::run::run($expression,$safeeval);
1.159     albertel  823:     my $status;
                    824:     my $accessmsg;
                    825: 
1.343     albertel  826:     my $name= &get_resource_name($parstack,$safeeval);
1.350     albertel  827:     my ($result,$form_tag_start);
1.354     albertel  828:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'tex'
                    829: 	|| $target eq 'edit') {
1.350     albertel  830: 	($result,$form_tag_start) =
                    831: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                    832: 			$name);
                    833:     }
                    834: 
1.284     albertel  835:     if ($target eq 'tex' and $env{'request.symb'} =~ m/\.page_/) {$result='';}
1.159     albertel  836: 
                    837:     if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
                    838:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    839: 	$target eq 'tex') {
                    840: 	#handle exam checkout
                    841: 	if ($Apache::lonhomework::type eq 'exam') {
                    842: 	    my $token=
                    843: 		$Apache::lonhomework::history{"resource.0.outtoken"};
1.284     albertel  844: 	    if (($env{'form.doescheckout'}) && (!$token)) {
1.159     albertel  845: 		$token=&Apache::lonxml::maketoken();
                    846: 		$Apache::lonhomework::history{"resource.0.outtoken"}=
                    847: 		    $token;
                    848: 	    }
1.343     albertel  849: 	    $result.=&Apache::lonxml::printtokenheader($target,$token);
1.142     albertel  850: 	}
1.284     albertel  851: 	if ($env{'form.markaccess'}) {
1.414     albertel  852: 	    my @interval=&Apache::lonnet::EXT("resource.0.interval");
                    853: 	    &Apache::lonnet::set_first_access($interval[1]);
1.252     albertel  854: 	}
1.159     albertel  855: 	#handle rand seed in construction space
                    856: 	my $rndseed=&setup_rndseed($safeeval);
1.367     albertel  857: 	my ($symb)=&Apache::lonnet::whichuser();
1.333     albertel  858: 	if ($env{'request.state'} ne "construct" && 
                    859: 	    ($symb eq '' || $Apache::lonhomework::type eq 'practice')) {
1.162     albertel  860: 	    $form_tag_start.='<input type="hidden" name="rndseed" value="'.
                    861: 		$rndseed.'" />'.
                    862: 		    '<input type="submit" name="resetdata"
1.334     albertel  863:                              value="'.&mt('New Problem Variation').'" />';
                    864: 	    if (exists($env{'form.username'})) {
                    865: 		$form_tag_start.=
1.164     albertel  866: 		    '<input type="hidden" name="username"
1.284     albertel  867:                              value="'.$env{'form.username'}.'" />';
1.334     albertel  868: 	    }
1.333     albertel  869: 	    if ($env{'request.role.adv'}) {
1.267     albertel  870: 		$form_tag_start.=
1.300     albertel  871: 		    ' <label><input type="checkbox" name="showallfoils" ';
1.284     albertel  872: 		if (defined($env{'form.showallfoils'})) {
1.267     albertel  873: 		    $form_tag_start.='checked="on"';
                    874: 		}
1.300     albertel  875: 		$form_tag_start.= ' />'.&mt('&nbsp;Show&nbsp;All&nbsp;Foils').
                    876: 		    '</label>';
1.267     albertel  877: 	    }
1.417     www       878:             if ($Apache::lonhomework::type eq 'practice') {
1.428     raeburn   879:                 $form_tag_start.=&practice_problem_header();
1.417     www       880:             }
1.267     albertel  881: 	    $form_tag_start.='<hr />';
1.162     albertel  882: 	}
1.324     albertel  883: 
                    884: 	($status,$accessmsg,my $slot_name,my $slot) = 
                    885: 	    &Apache::lonhomework::check_slot_access('0','problem');
1.159     albertel  886: 	push (@Apache::inputtags::status,$status);
1.324     albertel  887: 
1.159     albertel  888: 	my $expression='$external::datestatus="'.$status.'";';
                    889: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                    890: 	&Apache::run::run($expression,$safeeval);
                    891: 	&Apache::lonxml::debug("Got $status");
1.324     albertel  892: 
1.159     albertel  893: 	if (( $status eq 'CLOSED' ) ||
                    894: 	    ( $status eq 'UNCHECKEDOUT') ||
1.252     albertel  895: 	    ( $status eq 'NOT_YET_VIEWED') ||
1.159     albertel  896: 	    ( $status eq 'BANNED') ||
1.216     albertel  897: 	    ( $status eq 'UNAVAILABLE') ||
1.324     albertel  898: 	    ( $status eq 'NOT_IN_A_SLOT') ||
1.216     albertel  899: 	    ( $status eq 'INVALID_ACCESS')) {
1.326     albertel  900: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,
                    901: 						       $style);
1.159     albertel  902: 	    if ( $target eq "web" ) {
1.343     albertel  903: 		my $msg;
1.159     albertel  904: 		if ($status eq 'UNAVAILABLE') {
1.245     albertel  905: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.253     albertel  906: 		} elsif ($status ne 'NOT_YET_VIEWED') {
1.245     albertel  907: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
1.159     albertel  908: 		}
1.216     albertel  909: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
1.393     www       910: 		    $msg.=&mt('The problem ').$accessmsg;
1.159     albertel  911: 		} elsif ($status eq 'UNCHECKEDOUT') {
1.343     albertel  912: 		    $msg.=&checkout_msg();
1.252     albertel  913: 		} elsif ($status eq 'NOT_YET_VIEWED') {
1.253     albertel  914: 		    $msg.=&firstaccess_msg($accessmsg,$symb);
1.325     albertel  915: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
                    916: 		    $msg.=&Apache::bridgetask::add_request_another_attempt_button("Sign up for time to work.");
1.159     albertel  917: 		}
                    918: 		$result.=$msg.'<br />';
                    919: 	    } elsif ($target eq 'tex') {
1.332     albertel  920: 		my $startminipage = ($env{'form.problem_split'}=~/yes/i)? ''
                    921: 		                    : '\begin{minipage}{\textwidth}';
                    922: 		$result.='\begin{document}\noindent \vskip 1 mm '.
                    923: 		    $startminipage.'\vskip 0 mm';
1.159     albertel  924: 		if ($status eq 'UNAVAILABLE') {
1.211     albertel  925: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
1.159     albertel  926: 		} else {
1.211     albertel  927: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
1.159     albertel  928: 		}
                    929: 	    }
1.324     albertel  930: 	} elsif ($status eq 'NEEDS_CHECKIN') {
1.326     albertel  931: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,
                    932: 						       $style);
1.324     albertel  933: 	    if ($target eq 'web') {
1.375     albertel  934: 		$result .= 
                    935: 		    &Apache::bridgetask::proctor_validation_screen($slot);
1.324     albertel  936: 	    } elsif ($target eq 'grade') {
                    937: 		&Apache::bridgetask::proctor_check_auth($slot_name,$slot,
                    938: 							'problem');
                    939: 	    }
1.159     albertel  940: 	} elsif ($target eq 'web') {
1.360     albertel  941: 	    if ($status eq 'CAN_ANSWER' 
                    942: 		&& $slot_name ne ''
                    943: 		&& $Apache::lonhomework::history{'resource.0.checkedin'} eq '') {
                    944: 		# unproctored slot access, self checkin
                    945: 		&Apache::bridgetask::check_in('problem',undef,undef,
                    946: 					      $slot_name);
                    947: 	    }
1.368     albertel  948: 	    $result.="\n $form_tag_start \t".	
1.227     albertel  949: 	      '<input type="hidden" name="submitted" value="yes" />';
                    950: 	    # create a page header and exit
1.284     albertel  951: 	    if ($env{'request.state'} eq "construct") {
                    952: 		$result.= &problem_web_to_edit_header($env{'form.rndseed'});
1.428     raeburn   953:                 if ($Apache::lonhomework::type eq 'practice') {
                    954:                     $result.= '<input type="submit" name="resetdata" '.
                    955:                               'value="'.&mt('New Problem Variation').'" />'.
                    956:                               &practice_problem_header().'<hr />';
                    957:                 }
1.227     albertel  958: 	    }
                    959: 	    # if we are viewing someone else preserve that info
1.284     albertel  960: 	    if (defined $env{'form.grade_symb'}) {
1.227     albertel  961: 		foreach my $field ('symb','courseid','domain','username') {
                    962: 		    $result .= '<input type="hidden" name="grade_'.$field.
1.284     albertel  963: 			'" value="'.$env{"form.grade_$field"}.'" />'."\n";
1.159     albertel  964: 		}
                    965: 	    }
                    966: 	} elsif ($target eq 'tex') {
1.319     foxr      967: 	    $result .= 'INSERTTEXFRONTMATTERHERE';
                    968: 
1.99      sakharuk  969: 	}
1.159     albertel  970:     } elsif ($target eq 'edit') {
1.343     albertel  971: 	$result .= $form_tag_start.&problem_edit_header();
1.226     albertel  972: 	$Apache::lonxml::warnings_error_header=
                    973: 	    &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  974: 	my $temp=&Apache::edit::insertlist($target,$token);
                    975: 	$result.=$temp;
                    976:     } elsif ($target eq 'modified') {
                    977: 	$result=$token->[4];
                    978:     } else {
                    979: 	# page_start returned a starting result, delete it if we don't need it
                    980: 	$result = '';
1.99      sakharuk  981:     }
1.159     albertel  982:     return $result;
1.9       albertel  983: }
                    984: 
                    985: sub end_problem {
1.159     albertel  986:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.321     albertel  987:     my $result;
1.310     foxr      988: 
1.321     albertel  989:     if (!$Apache::lonxml::metamode) {
                    990: 	$result = &Apache::lonxml::endredirection(); #started in &start_problem
1.329     albertel  991: 	$Apache::lonxml::post_evaluate=0;
1.321     albertel  992:     }
1.319     foxr      993: 
                    994:     if ($target eq 'tex') {
1.321     albertel  995: 	# Figure out the front matter and replace the
                    996: 	# INSERTTEXFRONTMATTERHERE in result with it.  note that we do
                    997: 	# this in end_problem because whether or not we display due
                    998: 	# dates depends on whether due dates have already been
                    999: 	# displayed in the problem parts.
                   1000: 
1.319     foxr     1001: 	my $frontmatter   = '';
                   1002: 	my $startminipage = '';
                   1003: 	if (not $env{'form.problem_split'}=~/yes/) {
                   1004: 	    $startminipage = '\begin{minipage}{\textwidth}';
                   1005: 	}
                   1006: 	my $id = $Apache::inputtags::part;
                   1007: 	my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                   1008: 	my $packages=&Apache::lonnet::metadata($env{'request.uri'},'packages');
                   1009: 	my @packages = split /,/,$packages;
                   1010: 	my $allow_print_points = 0;
                   1011: 	foreach my $partial_key (@packages) {
                   1012: 	    if ($partial_key=~m/^part_0$/) {
                   1013: 		$allow_print_points=1;
                   1014: 	    }
                   1015: 	}
                   1016: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1017: 	if (defined($maxtries) && $maxtries < 0) { $allow_print_points=0; }
                   1018: 	if (lc($env{'course.'.$env{'request.course.id'}.
                   1019: 			'.disableexampointprint'}) eq 'yes') {
                   1020: 	    $allow_print_points=0;
                   1021: 	}
                   1022: 	my $name_of_resourse= &Apache::lonxml::latex_special_symbols(&get_resource_name($parstack,$safeeval),'header');
                   1023: 	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 ';
                   1024: 	my $toc_line='\vskip 1 mm\noindent '.$startminipage.
                   1025: 	    '\addcontentsline{toc}{subsection}{'.$name_of_resourse.'}';
                   1026: 	
                   1027: 	#  Figure out what the due date is and if we need to print
                   1028: 	#  it in the problem header.  We have been logging the
                   1029: 	#  last due date written to file. 
                   1030: 	
                   1031: 	my $duetime = &Apache::lonnet::EXT("resource.$id.duedate"); 
                   1032: 	my $duedate = POSIX::strftime("%c",localtime($duetime));
                   1033: 	my $temp_file;
                   1034: 	my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.due";
                   1035: 	
                   1036: 	# Figure out what the last printed due date is or set it
                   1037: 	# to the epoch if no duedates have been printed.
                   1038: 	
                   1039: 	my $due_file_content = 0;      #   If the file does not yet exist, time is the epoch.
                   1040: 	if (-e $filename) {
                   1041: 	    $temp_file = Apache::File->new($filename);
                   1042: 	    my @due_file      = <$temp_file>;
                   1043: 	    $due_file_content = $due_file[$#due_file];
                   1044: 	    chomp $due_file_content;
                   1045: 	} 
                   1046: 	
                   1047: 	# We display the due date iff it is not the same as the last
                   1048: 	# duedate in problem header ($due_file_content), and
                   1049: 	# none of our parts displayed a duedate.
                   1050: 	#
                   1051: 	my $parts_with_displayduedate;
                   1052: 	if (defined $Apache::outputtags::showonce{'displayduedate'}) {
                   1053: 	    $parts_with_displayduedate = 
                   1054: 		scalar(@{$Apache::outputtags::showonce{'displayduedate'}});
                   1055: 	} else {
                   1056: 	    $parts_with_displayduedate = 0;
                   1057: 	}
                   1058: 	if (($due_file_content != $duetime) && ($parts_with_displayduedate == 0) ) {
                   1059: 	    $temp_file = Apache::File->new('>'.$filename);
                   1060: 	    print $temp_file "$duetime\n";
                   1061: 	    if (not $env{'request.symb'} =~ m/\.page_/) {
                   1062: 		if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
                   1063: 		    $frontmatter .= $begin_doc.
                   1064: 			'\textit{Due date: '.$duedate.'} '.$toc_line;
                   1065: 		} else {
                   1066: 		    $frontmatter.= $begin_doc.$toc_line;
                   1067: 		    if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { $frontmatter .= '\fbox{\textit{'.$weight.' pt}}';}
                   1068: 		}
                   1069: 	    } else {
1.381     albertel 1070: 		$frontmatter .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\'.$startminipage;
1.319     foxr     1071: 	    }
                   1072: 	} else {
                   1073: 	    if (not $env{'request.symb'} =~ m/\.page_/) {
                   1074: 		$frontmatter .= $begin_doc.$toc_line;
                   1075: 		if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { $frontmatter .= '\fbox{\textit{'.$weight.' pt}}';}
                   1076: 	    } else {
1.381     albertel 1077: 		$frontmatter .= '\vskip 1mm \\\\\\\\'.$startminipage;
1.319     foxr     1078: 	    }
                   1079: 	}
                   1080: 	$result =~ s/INSERTTEXFRONTMATTERHERE/$frontmatter/;
                   1081:     }
                   1082: 
1.159     albertel 1083:     my $status=$Apache::inputtags::status['-1'];
                   1084:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                   1085: 	$target eq 'tex') {
1.249     albertel 1086: 	if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
1.159     albertel 1087: 	    # if part is zero, no <part>s existed, so we need to the grading
1.249     albertel 1088: 	    if ($status eq 'CAN_ANSWER' ||$Apache::lonhomework::scantronmode) {
                   1089: 		&Apache::inputtags::grade;
1.324     albertel 1090: 	    } elsif ($status eq 'NEEDS_CHECKIN') {
                   1091: 		# no need to grade, and don't want to hide data
1.249     albertel 1092: 	    } else {
                   1093: 		# move any submission data to .hidden
                   1094: 		&Apache::inputtags::hidealldata($Apache::inputtags::part);
                   1095: 	    }
1.159     albertel 1096: 	} elsif ( ($target eq 'web' || $target eq 'tex') &&
                   1097: 		  $Apache::inputtags::part eq '0' &&
1.252     albertel 1098: 		  $status ne 'UNCHECKEDOUT' && $status ne 'NOT_YET_VIEWED') {
1.159     albertel 1099: 	    # if part is zero, no <part>s existed, so we need show the current
                   1100: 	    # grading status
                   1101: 	    my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
                   1102: 	    $result.= $gradestatus;
                   1103: 	}
                   1104: 	if (
1.284     albertel 1105: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.159     albertel 1106: 	    ($target eq 'answer') || ($target eq 'tex')
                   1107: 	   ) {
1.227     albertel 1108: 	    if ($target ne 'tex' &&
1.284     albertel 1109: 		$env{'form.answer_output_mode'} ne 'tex') {
1.254     www      1110: 		$result.="</form>";
1.348     albertel 1111: 		$result.= &Apache::lonhtmlcommon::htmlareaselectactive(&Apache::lonhtmlcommon::get_htmlareafields());
1.159     albertel 1112: 	    }
                   1113: 	    if ($target eq 'web') {
1.346     albertel 1114: 		$result.= &Apache::loncommon::end_page({'discussion' => 1});
1.159     albertel 1115: 	    } elsif ($target eq 'tex') {
1.178     sakharuk 1116: 		my $endminipage = '';
1.284     albertel 1117: 		if (not $env{'form.problem_split'}=~/yes/) {
1.178     sakharuk 1118: 		    $endminipage = '\end{minipage}';
                   1119: 		}
1.284     albertel 1120:                 if ($env{'form.print_discussions'} eq 'yes') {
1.263     sakharuk 1121: 		    $result.=&Apache::lonxml::xmlend($target,$parser);
1.159     albertel 1122: 		} else {
1.262     sakharuk 1123: 		    $result .= '\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
1.284     albertel 1124: 		    if (not $env{'request.symb'} =~ m/\.page_/) {
1.262     sakharuk 1125: 			$result .= $endminipage.'\end{document} ';
                   1126: 		    } else {
1.382     albertel 1127: 			$result .= $endminipage;
1.262     sakharuk 1128: 		    }
1.159     albertel 1129: 		}
                   1130: 	    }
                   1131: 	}
                   1132: 	if ($target eq 'grade') {
                   1133: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                   1134: 	    &finalize_storage();
                   1135: 	}
1.284     albertel 1136: 	if ($target eq 'answer' && ($env{'request.state'} eq 'construct')
                   1137: 	    && $env{'form.answer_output_mode'} ne 'tex') {
1.346     albertel 1138: 	    $result.=&Apache::loncommon::end_page({'discussion' => 1});
1.294     albertel 1139: 	                        # normally we get it from above, but in CSTR
1.172     albertel 1140: 	                        # we always show answer mode too.
1.159     albertel 1141: 	}
                   1142:     } elsif ($target eq 'meta') {
                   1143: 	if ($Apache::inputtags::part eq '0') {
1.179     albertel 1144: 	    @Apache::inputtags::response=();
1.159     albertel 1145: 	    $result=&Apache::response::mandatory_part_meta;
                   1146: 	}
1.215     albertel 1147: 	$result.=&Apache::response::meta_part_order();
1.258     albertel 1148: 	$result.=&Apache::response::meta_response_order();
1.159     albertel 1149:     } elsif ($target eq 'edit') {
                   1150: 	&Apache::lonxml::debug("in end_problem with $target, edit");
1.314     albertel 1151: 	$result .= &problem_edit_footer();
1.320     albertel 1152:     } elsif ($target eq 'modified') {
                   1153: 	 $result .= $token->[2];
1.159     albertel 1154:     }
1.155     albertel 1155: 
1.284     albertel 1156:     if ($env{'request.state'} eq 'construct' && $target eq 'web') {
1.177     albertel 1157: 	&Apache::inputtags::check_for_duplicate_ids();
                   1158:     }
1.204     albertel 1159: 
                   1160:     &reset_problem_globals('problem');
1.159     albertel 1161: 
                   1162:     return $result;
1.48      albertel 1163: }
                   1164: 
1.108     albertel 1165: 
1.48      albertel 1166: sub start_library {
1.159     albertel 1167:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.343     albertel 1168:     my ($result,$form_tag_start);
1.371     albertel 1169:     if ($#$tagstack eq 0 && $$tagstack[0] eq 'library') {
1.244     albertel 1170: 	&init_problem_globals('library');
                   1171: 	$Apache::lonhomework::type='problem';
                   1172:     }
1.159     albertel 1173:     if ($target eq 'edit') {
1.343     albertel 1174: 	($result,$form_tag_start)=
                   1175: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                   1176: 			'Edit');
                   1177: 	$result.=$form_tag_start.&problem_edit_header();
1.159     albertel 1178: 	my $temp=&Apache::edit::insertlist($target,$token);
                   1179: 	$result.=$temp;
                   1180:     } elsif ($target eq 'modified') {
                   1181: 	$result=$token->[4];
1.340     albertel 1182:     } elsif (($target eq 'web' || $target eq 'webgrade')
1.371     albertel 1183: 	     && ($#$tagstack eq 0 && $$tagstack[0] eq 'library')
1.340     albertel 1184: 	     && $env{'request.state'} eq "construct" ) {
1.159     albertel 1185: 	my $name=&get_resource_name($parstack,$safeeval);
1.343     albertel 1186: 	($result,$form_tag_start)=
                   1187: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                   1188: 			$name);
1.159     albertel 1189: 	my $rndseed=&setup_rndseed($safeeval);
1.343     albertel 1190: 	$result.=" \n $form_tag_start".	
1.159     albertel 1191: 		  '<input type="hidden" name="submitted" value="yes" />';
                   1192: 	$result.=&problem_web_to_edit_header($rndseed);
1.428     raeburn  1193:         if ($Apache::lonhomework::type eq 'practice') {
                   1194:             $result.= '<input type="submit" name="resetdata" '.
                   1195:                       'value="'.&mt('New Problem Variation').'" />'.
                   1196:                       &practice_problem_header().'<hr />';
                   1197:         }
1.159     albertel 1198:     }
                   1199:     return $result;
1.48      albertel 1200: }
                   1201: 
                   1202: sub end_library {
1.159     albertel 1203:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1204:     my $result='';
                   1205:     if ($target eq 'edit') {
                   1206: 	$result=&problem_edit_footer();
1.371     albertel 1207:     } elsif ($target eq 'web' 
                   1208: 	     && ($#$tagstack eq 0 && $$tagstack[0] eq 'library') 
                   1209: 	     && $env{'request.state'} eq "construct") {
1.349     albertel 1210: 	$result.='</form>'.&Apache::loncommon::end_page({'discussion' => 1});
1.159     albertel 1211:     }
1.371     albertel 1212:     if ( $#$tagstack eq 0 && $$tagstack[0] eq 'library') {
                   1213: 	&reset_problem_globals('library');
                   1214:     }
1.159     albertel 1215:     return $result;
1.197     www      1216: }
                   1217: 
                   1218: sub start_definetag {
1.326     albertel 1219:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.197     www      1220: 
                   1221:     my $result;
                   1222: 
                   1223:     my $name = $token->[2]->{'name'};
1.326     albertel 1224:     my $skip=&Apache::lonxml::get_all_text("/definetag",$parser,$style);
1.396     albertel 1225:     if ($target eq 'web') {
                   1226: 	if ($name=~/^\//) {
                   1227: 	    $result=
                   1228: 		'<br /><table class="LC_sty_end"><tr><th>'.
                   1229: 		&mt('END [_1]'.'<tt>'.$name.'</tt>').'</th></tr>';
                   1230: 	} else {
                   1231: 	    $result=
                   1232: 		'<br /><table class="LC_sty_begin"><tr><th>'.
                   1233: 		&mt('BEGIN [_1]'.'<tt>'.$name.'</tt>').'</th></tr>';
                   1234: 	}
                   1235: 	$skip = &HTML::Entities::encode($skip, '<>&"');
                   1236: 	$result.='<tr><td><pre>'.$skip.'</pre></td></tr></table>';
1.197     www      1237:     }
                   1238:     return $result;
                   1239: }
                   1240: 
                   1241: sub end_definetag {
                   1242:     return '';
1.1       albertel 1243: }
                   1244: 
                   1245: sub start_block {
1.201     albertel 1246:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.131     albertel 1247: 
                   1248:     my $result;
1.1       albertel 1249: 
1.339     albertel 1250:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer'  ||
                   1251: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.159     albertel 1252: 	my $code = $token->[2]->{'condition'};
1.385     albertel 1253: 	if (defined($code) && $code ne '') {
1.137     albertel 1254: 	    if (!$Apache::lonxml::default_homework_loaded) {
                   1255: 		&Apache::lonxml::default_homework_load($safeeval);
                   1256: 	    }
1.131     albertel 1257: 	    $result = &Apache::run::run($code,$safeeval);
                   1258: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
                   1259: 	} else {
                   1260: 	    $result='1';
                   1261: 	}
                   1262: 	if ( ! $result ) {
1.201     albertel 1263: 	    my $skip=&Apache::lonxml::get_all_text("/block",$parser,$style);
1.131     albertel 1264: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1265: 	}
                   1266: 	$result='';
                   1267:     } elsif ($target eq 'edit') {
                   1268: 	$result .=&Apache::edit::tag_start($target,$token);
                   1269: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
                   1270: 					  $token,40);
                   1271: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1272:     } elsif ($target eq 'modified') {
                   1273: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1274: 						     $safeeval,'condition');
                   1275: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38      albertel 1276:     }
1.131     albertel 1277:     return $result;
1.1       albertel 1278: }
                   1279: 
                   1280: sub end_block {
1.167     www      1281:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1282:     my $result;
                   1283:     if ($target eq "edit") {
                   1284: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1285:     }
                   1286:     return $result;
                   1287: }
                   1288: 
                   1289: sub start_languageblock {
1.201     albertel 1290:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.167     www      1291: 
                   1292:     my $result;
                   1293: 
1.339     albertel 1294:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1295: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.201     albertel 1296: 	my $include = $token->[2]->{'include'};
                   1297: 	my $exclude = $token->[2]->{'exclude'};
1.398     www      1298:         my @preferred_languages=&Apache::loncommon::preferred_languages();
1.394     www      1299: # This should not even happen, since we should at least have the server language
1.398     www      1300:         if (!$preferred_languages[0]) { $preferred_languages[0]='en'; }
                   1301: # Now loop over all languages in order of preference
                   1302:         foreach my $preferred_language (@preferred_languages) {
1.394     www      1303: # If the languageblock has no arguments, show the contents
1.399     www      1304:            $result=1;
                   1305:            my $found=0;
1.394     www      1306: # Do we have an include argument?
1.398     www      1307: 	   if ($include) {
1.394     www      1308: # If include is specified, by default, don't render the block
1.399     www      1309:               $result=0;
1.398     www      1310:               foreach my $included_language (split(/\,/,$include)) {
1.394     www      1311: # ... but if my preferred language is included, render it
1.398     www      1312:                  if ($included_language eq $preferred_language) {
1.399     www      1313:                     $result=1; 
                   1314:                     $found=1; 
1.398     www      1315:                  }
                   1316:               }
                   1317: 	   }
1.394     www      1318: # Do we have an exclude argument?
1.398     www      1319:            if ($exclude) {
1.399     www      1320:               $result=1;
1.398     www      1321:               foreach my $excluded_language (split(/\,/,$exclude)) {
                   1322:                  if ($excluded_language eq $preferred_language) {
1.399     www      1323:                     $result=0;
                   1324:                     $found=1;
1.398     www      1325:                  }
                   1326:               }
                   1327: 	   }
1.399     www      1328:            if ($found) { last; }
1.398     www      1329:         }
1.201     albertel 1330: 	if ( ! $result ) {
                   1331: 	    my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser,
                   1332: 						   $style);
                   1333: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1334: 	}
                   1335: 	$result='';
1.167     www      1336:     } elsif ($target eq 'edit') {
                   1337: 	$result .=&Apache::edit::tag_start($target,$token);
1.211     albertel 1338: 	$result .=&Apache::edit::text_arg(&mt('Include Language:'),'include',
1.167     www      1339: 					  $token,40);
1.211     albertel 1340: 	$result .=&Apache::edit::text_arg(&mt('Exclude Language:'),'exclude',
1.167     www      1341: 					  $token,40);
                   1342: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1343:     } elsif ($target eq 'modified') {
                   1344: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.168     albertel 1345: 						     $safeeval,'include',
                   1346: 						     'exclude');
1.167     www      1347: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1348:     }
                   1349:     return $result;
                   1350: }
                   1351: 
                   1352: sub end_languageblock {
1.170     www      1353:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1354:     my $result;
1.201     albertel 1355:     if ($target eq "edit") {
1.170     www      1356: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1357:     }
                   1358:     return $result;
                   1359: }
                   1360: 
1.397     albertel 1361: {
                   1362:     my %available_texts;
                   1363:     sub start_translated {
                   1364: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1365: 	&Apache::lonxml::register('Apache::structuretags',('lang'));
                   1366: 	undef(%available_texts);
                   1367:     }
                   1368:     
                   1369:     sub end_translated {
                   1370: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1371: 	my $result;
                   1372: 	#show the translation on viewable targets
                   1373: 	if ($target eq 'web'     || $target eq 'tex' || $target eq 'webgrade'||
                   1374: 	    # or non-viewable targets, if it's embedded in something that
                   1375: 	    # wants the output
                   1376: 	    (($target eq 'answer' || $target eq 'analyze'|| $target eq 'grade')
                   1377: 	     && &Apache::lonxml::in_redirection() ) ) {
                   1378: 	    my @possibilities = keys(%available_texts);
                   1379: 	    my $which = 
                   1380: 		&Apache::loncommon::languages(\@possibilities) || 'default';
                   1381: 	    $result = $available_texts{$which};
                   1382: 	}
                   1383: 	undef(%available_texts);
                   1384: 	&Apache::lonxml::deregister('Apache::structuretags',('lang'));
                   1385: 	return $result;
                   1386:     }
                   1387: 
                   1388: 
                   1389:     sub start_lang {
                   1390: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1391: 	if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1392: 	    $target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
                   1393: 	    &Apache::lonxml::startredirection();
                   1394: 	}
                   1395: 	return '';
                   1396:     }
                   1397: 
                   1398:     sub end_lang {
                   1399: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1400: 	if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1401: 	    $target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
                   1402: 	    my $result = &Apache::lonxml::endredirection();
                   1403: 	    my $which = &Apache::lonxml::get_param('which',$parstack,
                   1404: 						   $safeeval);
1.431     raeburn  1405:             if ($which=~/\w/) {
                   1406:                 $available_texts{$which} = $result;
                   1407:             }
                   1408:             my $otherlangs = &Apache::lonxml::get_param('other',$parstack,
                   1409:                                                         $safeeval);
                   1410:             foreach my $language (split(/\s*\,\s*/,$otherlangs)) {
                   1411:                 if ($language=~/\w/) {
                   1412:                     $available_texts{$language} = $result;
                   1413:                 }
1.427     bisitz   1414:             }
                   1415: 
1.397     albertel 1416: 	}
                   1417: 	return '';
                   1418:     }
                   1419: }
                   1420: 
1.170     www      1421: sub start_instructorcomment {
1.201     albertel 1422:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.170     www      1423: 
                   1424:     my $result;
                   1425: 
1.339     albertel 1426:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1427: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.284     albertel 1428:         $result=($env{'request.role'}=~/^(in|cc|au|ca|li)/);
                   1429: 	if ( (! $result) or ($env{'form.instructor_comments'} eq 'hide')) {
1.201     albertel 1430: 	    my $skip=&Apache::lonxml::get_all_text("/instructorcomment",
                   1431: 						   $parser,$style);
1.170     www      1432: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1433: 	}
                   1434: 	$result='';
                   1435:     } elsif ($target eq 'edit') {
                   1436: 	$result .=&Apache::edit::tag_start($target,$token);
                   1437: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1438:     }
                   1439:     return $result;
                   1440: }
                   1441: 
                   1442: sub end_instructorcomment {
1.159     albertel 1443:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160     albertel 1444:     my $result;
                   1445:     if ($target eq "edit") {
                   1446: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1447:     }
                   1448:     return $result;
1.4       tsai     1449: }
                   1450: 
                   1451: sub start_while {
1.326     albertel 1452:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1453: 
1.160     albertel 1454:     my $result;
1.339     albertel 1455:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1456: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.160     albertel 1457: 	my $code = $token->[2]->{'condition'};
1.4       tsai     1458: 
1.160     albertel 1459: 	push( @Apache::structuretags::whileconds, $code);
                   1460: 	if (!$Apache::lonxml::default_homework_loaded) {
                   1461: 	    &Apache::lonxml::default_homework_load($safeeval);
                   1462: 	}
                   1463: 	my $result = &Apache::run::run($code,$safeeval);
1.326     albertel 1464: 	my $bodytext=&Apache::lonxml::get_all_text("/while",$parser,$style);
1.160     albertel 1465: 	push( @Apache::structuretags::whilebody, $bodytext);
1.161     albertel 1466: 	push( @Apache::structuretags::whileline, $token->[5]);
                   1467: 	&Apache::lonxml::debug("s code $code got -$result-");
1.160     albertel 1468: 	if ( $result ) {
                   1469: 	    &Apache::lonxml::newparser($parser,\$bodytext);
                   1470: 	}
                   1471:     } elsif ($target eq 'edit') {
                   1472: 	$result .=&Apache::edit::tag_start($target,$token);
1.211     albertel 1473: 	$result .=&Apache::edit::text_arg(&mt('Test Condition:'),'condition',
1.160     albertel 1474: 					  $token,40);
                   1475: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1476:     } elsif ($target eq 'modified') {
                   1477: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1478: 						     $safeeval,'condition');
                   1479: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.159     albertel 1480:     }
1.160     albertel 1481:     return $result;
1.4       tsai     1482: }
                   1483: 
                   1484: sub end_while {
1.159     albertel 1485:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160     albertel 1486:     my $result;
                   1487: 
1.339     albertel 1488:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1489: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.160     albertel 1490: 	my $code = pop(@Apache::structuretags::whileconds);
                   1491: 	my $bodytext = pop(@Apache::structuretags::whilebody);
1.161     albertel 1492: 	my $line = pop(@Apache::structuretags::whileline);
                   1493: 	my $return = &Apache::run::run($code,$safeeval);
                   1494: 	my $starttime=time;
                   1495: 	my $error=0;
                   1496: 	while ($return) {
                   1497: 	    if (time-$starttime >
                   1498: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}) {
1.378     albertel 1499: 		$return = 0; $error=1; next;
1.161     albertel 1500: 	    }
                   1501: 	    $result.=&Apache::scripttag::xmlparse($bodytext);
1.380     albertel 1502: 	    if ($target eq 'grade' || $target eq 'answer' ||
                   1503: 		$target eq 'analyze') {
                   1504: 		# grade/answer/analyze should produce no output but if we
                   1505: 		# are redirecting, the redirecter should know what to do
                   1506: 		# with the output
                   1507: 		if (!$Apache::lonxml::redirection) { undef($result); }
                   1508: 	    }
1.161     albertel 1509: 	    $return = &Apache::run::run($code,$safeeval);
                   1510: 	}
                   1511: 	if ($error) {
1.430     bisitz   1512: 	    &Apache::lonxml::error('<pre>'.&mt('Code ran too long. It ran for more than').' '.$Apache::lonnet::perlvar{'lonScriptTimeout'}.' '.&mt('seconds occurred while running &lt;while&gt; on line').' '.$line.'</pre>');
1.160     albertel 1513: 	}
                   1514:     } elsif ($target eq "edit") {
                   1515: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.159     albertel 1516:     }
1.160     albertel 1517:     return $result;
1.1       albertel 1518: }
1.6       tsai     1519: 
1.160     albertel 1520: # <randomlist show="1">
1.6       tsai     1521: #  <tag1>..</tag1>
                   1522: #  <tag2>..</tag2>
                   1523: #  <tag3>..</tag3>
1.160     albertel 1524: #  ...
1.6       tsai     1525: # </randomlist>
                   1526: sub start_randomlist {
1.326     albertel 1527:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1528:     my $result;
1.339     albertel 1529:     if ($target eq 'answer' || $target eq 'grade'   || $target eq 'web' ||
                   1530: 	$target eq 'tex'    || $target eq 'analyze' || $target eq 'webgrade') {
1.331     albertel 1531: 	my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
1.305     albertel 1532: 	my $b_parser= HTML::LCParser->new(\$body);
                   1533: 	$b_parser->xml_mode(1);
                   1534: 	$b_parser->marked_sections(1);
1.159     albertel 1535: 	my $b_tok;
                   1536: 	my @randomlist;
                   1537: 	my $list_item;
                   1538: 	while($b_tok = $b_parser->get_token() ) {
                   1539: 	    if($b_tok->[0] eq 'S') { # start tag
                   1540: 		# get content of the tag until matching end tag
                   1541: 		# get all text upto the matching tag
                   1542: 		# and push the content into @randomlist
                   1543: 		$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
                   1544: 							   $b_parser);
                   1545: 		$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
                   1546: 		push(@randomlist,$list_item);
                   1547: 		#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
                   1548:                 #         $list_item</b>";
                   1549: 	    }
                   1550: 	    if($b_tok->[0] eq 'T') { # text
                   1551: 		# what to do with text in between tags?
                   1552: 		#  print "<b>TEXT $b_tok->[1]</b><br />";
                   1553: 	    }
                   1554: 	    # if($b_tok->[0] eq 'E') { # end tag, should not happen
                   1555: 	    #  print "<b>END-TAG $b_tok->[1]</b><br />";
                   1556: 	    # }
                   1557: 	}
1.303     albertel 1558: 	if (@randomlist) {
                   1559: 	    my @idx_arr = (0 .. $#randomlist);
                   1560: 	    &Apache::structuretags::shuffle(\@idx_arr);
                   1561: 	    my $bodytext = '';
                   1562: 	    my $show=$#randomlist;
                   1563: 	    my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
                   1564: 	    $showarg--;
                   1565: 	    if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
                   1566: 	    for(0 .. $show) {
                   1567: 		$bodytext .= "$randomlist[ $idx_arr[$_] ]";
                   1568: 	    }
                   1569: 	    &Apache::lonxml::newparser($parser,\$bodytext);
1.159     albertel 1570: 	}
                   1571:     } elsif ($target eq 'edit' ) {
                   1572: 	$result .=&Apache::edit::tag_start($target,$token);
                   1573: 	$result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
                   1574: 					   $token,5);
                   1575: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1576:     } elsif ($target eq 'modified' ) {
                   1577: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1578: 						     $safeeval,'show');
                   1579: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1580:     }
                   1581:     return $result;
1.7       tsai     1582: }
                   1583: 
                   1584: sub shuffle {
                   1585:     my $a=shift;
                   1586:     my $i;
1.303     albertel 1587:     if (ref($a) eq 'ARRAY' && @$a) {
1.251     albertel 1588: 	&Apache::response::pushrandomnumber();
1.159     albertel 1589: 	for($i=@$a;--$i;) {
                   1590: 	    my $j=int(&Math::Random::random_uniform() * ($i+1));
                   1591: 	    next if $i == $j;
                   1592: 	    @$a[$i,$j] = @$a[$j,$i];
                   1593: 	}
1.251     albertel 1594: 	&Apache::response::poprandomnumber();
1.7       tsai     1595:     }
1.6       tsai     1596: }
                   1597: 
                   1598: sub end_randomlist {
1.159     albertel 1599:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1600:     my $result;
                   1601:     if ($target eq 'edit' ) {
                   1602: 	$result=&Apache::edit::tag_end($target,$token,
                   1603: 				       'End Randomly Parsed Block');
                   1604:     }
                   1605:     return $result;
1.6       tsai     1606: }
                   1607: 
1.283     albertel 1608: sub ordered_show_check {
                   1609:     my $last_part=$Apache::inputtags::partlist[-2];
                   1610:     my $in_order=
                   1611: 	&Apache::lonnet::EXT('resource.'.$Apache::inputtags::part.'.ordered');
                   1612:     my $in_order_show=1;
                   1613:     if ($last_part ne '0' && lc($in_order) eq 'yes') {
                   1614: 	$in_order_show=&Apache::response::check_status($last_part);
                   1615:     }
                   1616:     return $in_order_show;
                   1617: }
                   1618: 
1.11      albertel 1619: sub start_part {
1.326     albertel 1620:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.321     albertel 1621:     if (!$Apache::lonxml::metamode) {
                   1622: 	&Apache::lonxml::startredirection(); # we'll use redirection to fix up 
                   1623: 	                                     # duedates.
                   1624:     }
1.159     albertel 1625:     my $result='';
1.386     albertel 1626:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.159     albertel 1627:     $Apache::inputtags::part=$id;
1.177     albertel 1628:     push(@Apache::inputtags::partlist,$id);
                   1629:     @Apache::inputtags::response=();
1.159     albertel 1630:     @Apache::inputtags::previous=();
                   1631:     @Apache::inputtags::previous_version=();
1.405     albertel 1632:     &Apache::lonhomework::set_show_problem_status(&get_problem_status($id));
1.403     albertel 1633:     &Apache::response::reset_params();
                   1634: 
1.159     albertel 1635:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.259     albertel 1636:     my $newtype=&Apache::lonnet::EXT("resource.$id.type");
                   1637:     if ($newtype) { $Apache::lonhomework::type=$newtype; }
1.283     albertel 1638:     my $in_order_show=&ordered_show_check();
1.214     albertel 1639:     my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.259     albertel 1640:     $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.209     albertel 1641:     &Apache::run::run($expression,$safeeval);
1.159     albertel 1642: 
                   1643:     if ($target eq 'meta') {
1.224     www      1644: 	my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
                   1645: 	return &Apache::response::mandatory_part_meta.
                   1646: 	       &Apache::response::meta_parameter_write('display','string',$display,'Part Description');
1.159     albertel 1647:     } elsif ($target eq 'web' || $target eq 'grade' ||
                   1648: 	     $target eq 'answer' || $target eq 'tex') {
1.283     albertel 1649: 	if ($hidden || !$in_order_show) {
1.326     albertel 1650: 	    my $bodytext=&Apache::lonxml::get_all_text("/part",$parser,$style);
1.159     albertel 1651: 	} else {
                   1652: 	    my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
                   1653: 	    push (@Apache::inputtags::status,$status);
                   1654: 	    my $expression='$external::datestatus="'.$status.'";';
                   1655: 	    $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
                   1656: 	    &Apache::run::run($expression,$safeeval);
1.284     albertel 1657: 	    if ($env{'request.state'} eq 'construct') {
1.241     albertel 1658: 		&set_problem_state($Apache::inputtags::part); 
1.240     albertel 1659: 	    }
1.216     albertel 1660: 	    if (( $status eq 'CLOSED' ) ||
                   1661: 		( $status eq 'UNCHECKEDOUT') ||
1.252     albertel 1662: 		( $status eq 'NOT_YET_VIEWED') ||
1.216     albertel 1663: 		( $status eq 'BANNED') ||
                   1664: 		( $status eq 'UNAVAILABLE') ||
                   1665: 		( $status eq 'INVALID_ACCESS')) {
1.326     albertel 1666: 		my $bodytext=&Apache::lonxml::get_all_text("/part",$parser,
                   1667: 							   $style);
1.159     albertel 1668: 		if ( $target eq "web" ) {
1.211     albertel 1669: 		    $result="<br />".&mt('Part is not open to be viewed. It')." $accessmsg<br />";
1.159     albertel 1670: 		} elsif ( $target eq 'tex' ) {
1.284     albertel 1671: 		    if (not $env{'form.problem_split'}=~/yes/) {
1.211     albertel 1672: 			$result="\\end{minipage}\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\\\begin{minipage}{\\textwidth}";
1.195     sakharuk 1673: 		    } else {
1.211     albertel 1674: 			$result="\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\";
1.195     sakharuk 1675: 		    }
1.159     albertel 1676: 		}
                   1677: 	    } else {
                   1678: 		if ($target eq 'tex') {
1.284     albertel 1679: 		    if (not $env{'form.problem_split'}=~/yes/) {
1.264     sakharuk 1680: 			if ($$tagstack[-2] eq 'td') {
1.388     foxr     1681: 			    $result.='\noindent \begin{minipage}{\textwidth}\noindent';
1.264     sakharuk 1682: 			} else {
                   1683: 			    $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
                   1684: 			}
1.195     sakharuk 1685: 		    }
1.159     albertel 1686: 		    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.284     albertel 1687: 		    my $allkeys=&Apache::lonnet::metadata($env{'request.uri'},'packages');
1.222     sakharuk 1688: 		    my @allkeys = split /,/,$allkeys;
                   1689: 		    my $allow_print_points = 0;
                   1690: 		    foreach my $partial_key (@allkeys) {
1.230     albertel 1691: 			if ($partial_key=~m/^part_(.*)$/) {
1.222     sakharuk 1692: 			    if ($1 ne '0') {$allow_print_points=1;}
                   1693: 			}
                   1694: 		    }
1.275     albertel 1695: 		    my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1696: 		    if (defined($maxtries) && $maxtries < 0) {
                   1697: 			$allow_print_points=0;
                   1698: 		    }
1.302     albertel 1699: 		    if (lc($env{'course.'.$env{'request.course.id'}.
                   1700: 				    '.disableexampointprint'}) eq 'yes') {
                   1701: 			$allow_print_points=0;
                   1702: 		    }
1.222     sakharuk 1703: 		    if (($Apache::lonhomework::type eq 'exam') && ($allow_print_points)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
1.233     www      1704: 		} elsif ($target eq 'web') {
1.356     www      1705: 		    $result.='<a name="'.&escape($Apache::inputtags::part).'" />';
1.159     albertel 1706: 		}
                   1707: 	    }
                   1708: 	}
                   1709:     } elsif ($target eq 'edit') {
                   1710: 	$result.=&Apache::edit::tag_start($target,$token);
                   1711: 	$result.=&Apache::edit::text_arg('Part ID:','id',$token).
                   1712: 	    &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
1.224     www      1713: 	    '&nbsp;&nbsp;'.
                   1714: &Apache::edit::text_arg('Displayed Part Description:','display',$token).
1.159     albertel 1715: 		&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1716:     } elsif ($target eq 'modified') {
                   1717: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.225     albertel 1718: 						     $safeeval,'id','display');
1.159     albertel 1719: 	if ($constructtag) {
1.225     albertel 1720: 	    #limiting ids to only letters numbers, and space
1.224     www      1721: 	    $token->[2]->{'id'}=~s/[^A-Za-z0-9 ]//gs;
1.159     albertel 1722: 	    $result = &Apache::edit::rebuild_tag($token);
                   1723: 	}
                   1724:     }
                   1725:     return $result;
1.11      albertel 1726: }
                   1727: 
                   1728: sub end_part {
1.159     albertel 1729:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1730:     &Apache::lonxml::debug("in end_part $target ");
                   1731:     my $status=$Apache::inputtags::status['-1'];
                   1732:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.283     albertel 1733:     my $in_order_show=&ordered_show_check();
1.321     albertel 1734:     my $result;
                   1735:     if (!$Apache::lonxml::metamode) {
                   1736: 	$result = &Apache::lonxml::endredirection(); # started in &start_part
1.329     albertel 1737: 	$Apache::lonxml::post_evaluate=0;
1.321     albertel 1738:     }
1.312     albertel 1739:     if ($target eq 'grade') {
1.249     albertel 1740: 	if (($status eq 'CAN_ANSWER' || $Apache::lonhomework::scantronmode) &&
1.283     albertel 1741: 	    !$hidden && $in_order_show) {
1.311     foxr     1742: 	    $result.=&Apache::inputtags::grade;
1.249     albertel 1743: 	} else {
                   1744: 	    # move any submission data to .hidden
                   1745: 	    &Apache::inputtags::hidealldata($Apache::inputtags::part);
                   1746: 	}
1.283     albertel 1747:     } elsif (($target eq 'web' || $target eq 'tex') &&
                   1748: 	     !$hidden && $in_order_show) {
1.159     albertel 1749: 	my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
                   1750: 							$target);
1.212     albertel 1751: 	if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
                   1752: 	    $gradestatus='';
                   1753: 	}
1.311     foxr     1754: 	$result.=$gradestatus;
1.265     sakharuk 1755: 	if ($$tagstack[-2] eq 'td' and $target eq 'tex') {$result.='\end{minipage}';} 
1.181     albertel 1756:     } elsif ($target eq 'edit') {
1.311     foxr     1757: 	$result.=&Apache::edit::end_table();
1.322     albertel 1758:     } elsif ($target eq 'modified') {
                   1759: 	 $result .= $token->[2];
1.159     albertel 1760:     }
                   1761:     pop @Apache::inputtags::status;
                   1762:     $Apache::inputtags::part='';
1.295     albertel 1763:     $Apache::lonhomework::type = $Apache::lonhomework::default_type;
1.159     albertel 1764:     return $result;
1.11      albertel 1765: }
1.1       albertel 1766: 
1.25      albertel 1767: sub start_preduedate {
1.326     albertel 1768:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.339     albertel 1769:     if ($target eq 'web' || $target eq 'grade'    || $target eq 'answer' ||
                   1770: 	$target eq 'tex' || $target eq 'webgrade') {
1.236     albertel 1771: 	&Apache::lonxml::debug("State in preduedate is ". $Apache::inputtags::status['-1']);
1.300     albertel 1772: 	if (!$Apache::lonhomework::scantronmode &&
                   1773: 	    $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1.236     albertel 1774: 	    $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                   1775: 	    &Apache::lonxml::debug("Wha? ". ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER'));
1.326     albertel 1776: 	    &Apache::lonxml::get_all_text("/preduedate",$parser,$style);
1.159     albertel 1777: 	}
1.24      albertel 1778:     }
1.159     albertel 1779:     return '';
1.24      albertel 1780: }
                   1781: 
1.25      albertel 1782: sub end_preduedate {
1.159     albertel 1783:     return '';
1.24      albertel 1784: }
                   1785: 
1.369     foxr     1786: # In all the modes where <postanswerdate> text is 
                   1787: # displayable,  all we do is eat up the text between the start/stop
                   1788: # tags if the conditions are not right to display it.
1.25      albertel 1789: sub start_postanswerdate {
1.326     albertel 1790:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.370     foxr     1791:     my $pav = &Apache::lonnet::allowed('pav', $env{'request.course.id'}) ||
                   1792: 	&Apache::lonnet::allowed('pav',
                   1793: 			   $env{'request.course.id'}.'/'.$env{'request.course.sec'});
1.369     foxr     1794:     if ($target eq 'web' || $target eq 'grade' || $target eq 'webgrade' ||
1.370     foxr     1795: 	$target eq 'tex' ) {
1.300     albertel 1796: 	if ($Apache::lonhomework::scantronmode ||
1.370     foxr     1797: 	    $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER' ||
                   1798: 	    (($target eq 'tex') && !$pav)) {
1.326     albertel 1799: 	    &Apache::lonxml::get_all_text("/postanswerdate",$parser,$style);
1.159     albertel 1800: 	}
                   1801:     }
                   1802:     return '';
1.24      albertel 1803: }
                   1804: 
1.25      albertel 1805: sub end_postanswerdate {
1.159     albertel 1806:     return '';
1.24      albertel 1807: }
                   1808: 
1.25      albertel 1809: sub start_notsolved {
1.326     albertel 1810:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1811:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.339     albertel 1812: 	$target eq 'tex' || $target eq 'webgrade') {
1.159     albertel 1813: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                   1814: 	&Apache::lonxml::debug("not solved has :$gradestatus:");
1.239     albertel 1815: 	if ($gradestatus =~ /^correct/ &&
                   1816: 	    &Apache::response::show_answer()) {
1.159     albertel 1817: 	    &Apache::lonxml::debug("skipping");
1.326     albertel 1818: 	    &Apache::lonxml::get_all_text("/notsolved",$parser,$style);
1.159     albertel 1819: 	}
1.24      albertel 1820:     }
1.159     albertel 1821:     return '';
1.24      albertel 1822: }
                   1823: 
1.25      albertel 1824: sub end_notsolved {
1.159     albertel 1825:     return '';
1.24      albertel 1826: }
                   1827: 
                   1828: sub start_solved {
1.326     albertel 1829:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1830:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1831: 	$target eq 'tex') {
                   1832: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1.239     albertel 1833: 	if ($gradestatus !~ /^correct/ ||
                   1834: 	    !&Apache::response::show_answer()) {
1.326     albertel 1835: 	    &Apache::lonxml::get_all_text("/solved",$parser,$style);
1.159     albertel 1836: 	}
1.24      albertel 1837:     }
1.159     albertel 1838:     return '';
1.24      albertel 1839: }
                   1840: 
                   1841: sub end_solved {
1.248     albertel 1842:     return '';
                   1843: }
                   1844: 
                   1845: sub start_problemtype {
1.326     albertel 1846:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.248     albertel 1847:     my $result;
1.339     albertel 1848:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1849: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.248     albertel 1850: 	my $mode=lc(&Apache::lonxml::get_param('mode',$parstack,$safeeval));
                   1851: 	if (!defined($mode)) { $mode='show'; }
                   1852: 	my $for=&Apache::lonxml::get_param('for',$parstack,$safeeval);
                   1853: 	my $found=0;
                   1854: 	foreach my $type (split(',',$for)) {
                   1855: 	    if ($Apache::lonhomework::type eq lc($type)) { $found=1; }
                   1856: 	}
                   1857: 	if ($mode eq 'show' && !$found) {
1.326     albertel 1858: 	    &Apache::lonxml::get_all_text("/problemtype",$parser,$style);
1.248     albertel 1859: 	}
                   1860: 	if ($mode eq 'hide' && $found) {
1.326     albertel 1861: 	    &Apache::lonxml::get_all_text("/problemtype",$parser,$style);
1.248     albertel 1862: 	}
                   1863:     } elsif ($target eq 'edit') {
                   1864: 	$result .=&Apache::edit::tag_start($target,$token);
                   1865: 	$result.=&Apache::edit::select_arg('Mode:','mode',
                   1866: 					   [['show','Show'],
                   1867: 					    ['hide','Hide']]
                   1868: 					   ,$token);
                   1869: 	$result .=&Apache::edit::checked_arg('When used as type(s):','for',
                   1870: 					     [ ['exam','Exam/Quiz Problem'],
                   1871: 					       ['survey','Survey'],
1.428     raeburn  1872: 					       ['problem','Homework Problem'],
                   1873:                                                ['practice','Practice Problem'] ]
1.248     albertel 1874: 					     ,$token);
                   1875: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1876:     } elsif ($target eq 'modified') {
                   1877: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1878: 						     $safeeval,'mode','for');
                   1879: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1880:     }
                   1881:     return $result;
                   1882: }
                   1883: 
                   1884: sub end_problemtype {
1.159     albertel 1885:     return '';
1.24      albertel 1886: }
1.34      albertel 1887: 
                   1888: sub start_startouttext {
1.159     albertel 1889:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1890:     my @result=(''.'');
                   1891:     if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
1.404     albertel 1892:     
                   1893:     my $nesting = 
                   1894: 	&Apache::lonxml::set_state('outtext',
                   1895: 				   &Apache::lonxml::get_state('outtext')+1);
                   1896:     if ($nesting > 1 && $env{'request.state'} eq 'construct') {
                   1897: 	&Apache::lonxml::error("Nesting of &lt;startouttext /&gt; not allowed, on line ".$token->[5]);
                   1898:     }
1.159     albertel 1899:     return (@result);
1.34      albertel 1900: }
1.159     albertel 1901: 
1.34      albertel 1902: sub end_startouttext {
1.326     albertel 1903:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1904:     my $result='';
                   1905:     my $text='';
                   1906:     if ($target eq 'edit') {
1.424     foxr     1907: 	my $areaid = 'homework_edit_'.$Apache::lonxml::curdepth;
1.326     albertel 1908: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser,$style);
1.434   ! foxr     1909: 
        !          1910: 	$result.=&Apache::edit::start_table($token)."<tr><td>".&mt('Text Block')."</td>
        !          1911: <td>".&mt('Delete:').
        !          1912:                  &Apache::edit::deletelist($target,$token)
        !          1913: 		 ."</td>";
1.425     raeburn  1914:         unless ($env{'environment.wysiwygeditor'} eq 'on') {
1.434   ! foxr     1915: 	    $result .= '<td align="left">'
        !          1916: 		.&Apache::lonhtmlcommon::dragmath_button($areaid,1)
        !          1917: 		.'</td>'
        !          1918: 		.'<td>'
        !          1919: 		.&Apache::edit::insertlist($target,$token)
        !          1920: 		.'</td>';
        !          1921: 	}
1.425     raeburn  1922: 	$result.='<td align="right" valign="top">' .
1.434   ! foxr     1923: 	    &Apache::loncommon::helpLatexCheatsheet().
1.159     albertel 1924: 		 &Apache::edit::end_row().
1.362     albertel 1925:                  &Apache::edit::start_spanning_row()."\n".
1.255     www      1926: 		 &Apache::edit::editfield($token->[1],$text,"",80,8,1);
1.159     albertel 1927:     }
                   1928:     if ($target eq 'modified') {
1.219     albertel 1929: 	$result='<startouttext />'.&Apache::edit::modifiedfield("endouttext",$parser);
1.159     albertel 1930:     }
                   1931:     if ($target eq 'tex') {
                   1932: 	$result .= '\noindent ';
                   1933:     }
                   1934:     return $result;
1.34      albertel 1935: }
1.159     albertel 1936: 
1.34      albertel 1937: sub start_endouttext {
1.159     albertel 1938:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1939:     my $result='';
                   1940:     if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
                   1941:     if ($target eq "modified") {
                   1942: 	$result='<endouttext />'.
1.377     albertel 1943: 	    &Apache::edit::handle_insertafter('startouttext');
                   1944:     }
1.404     albertel 1945: 
                   1946:     my $nesting = 
                   1947: 	&Apache::lonxml::set_state('outtext',
                   1948: 				   &Apache::lonxml::get_state('outtext')-1);
                   1949:     if ($nesting < 0 && $env{'request.state'} eq 'construct') {
                   1950: 	&Apache::lonxml::error(" Extraneous &lt;endouttext /&gt; not allowed on line ".$token->[5]);
                   1951: 	&Apache::lonxml::set_state('outtext', 0);
                   1952:     }
1.159     albertel 1953:     return $result;
1.34      albertel 1954: }
1.159     albertel 1955: 
1.34      albertel 1956: sub end_endouttext {
1.159     albertel 1957:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1958:     my @result=('','');
                   1959:     if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
                   1960:     return (@result);
1.34      albertel 1961: }
1.159     albertel 1962: 
1.45      albertel 1963: sub delete_startouttext {
1.326     albertel 1964:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1965:     #  my $text=&Apache::lonxml::get_all_text("endouttext",$parser,$style);
1.159     albertel 1966:     my $text=$$parser['-1']->get_text("/endouttext");
                   1967:     my $ntoken=$$parser['-1']->get_token();
                   1968:     &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
                   1969:     &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
                   1970:     # Deleting 2 parallel tag pairs, but we need the numbers later to look like
                   1971:     # they did the last time round
                   1972:     &Apache::lonxml::increasedepth($ntoken);
                   1973:     &Apache::lonxml::decreasedepth($ntoken);
                   1974:     return 1;
1.193     www      1975: }
                   1976: 
                   1977: sub start_simpleeditbutton {
                   1978:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1979:     my $result='';
1.284     albertel 1980:     if (($env{'form.simple_edit_button'} ne 'off') &&
1.273     albertel 1981: 	($target eq 'web') &&
1.330     albertel 1982:         (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
1.284     albertel 1983:         my $url=$env{'request.noversionuri'};
1.193     www      1984:         $url=~s/\?.*$//;
1.367     albertel 1985: 	my ($symb) = &Apache::lonnet::whichuser();
1.194     www      1986: 	$result='<table width="100%" bgcolor="#FFFFAA" border="2"><tr><td>'.
1.356     www      1987:                 '<a href="'.$url.'/smpedit?symb='.&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      1988: &Apache::loncommon::help_open_topic('Caching').'</td></tr></table><br />';
1.193     www      1989:     }
                   1990:     return $result;
                   1991: }
                   1992: 
                   1993: sub end_simpleeditbutton {
                   1994:     return '';
1.45      albertel 1995: }
1.34      albertel 1996: 
1.428     raeburn  1997: sub practice_problem_header {
                   1998:     return '<span class="LC_info"><h3>'.&mt('Practice Problem').'</h3></span>'.
                   1999:            '<span class="LC_info">'.&mt('Submissions are not permanently recorded').
                   2000:            '</span>';
                   2001: }
                   2002: 
1.1       albertel 2003: 1;
                   2004: __END__

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