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