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