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