1: # The LearningOnline Network with CAPA
2: # definition of tags that give a structure to a document
3: #
4: # $Id: structuretags.pm,v 1.166 2003/04/21 20:59:02 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: $Apache::lonhomework::scantronmode=0;
275:
276: if (defined($ENV{'scantron.maxquest'})) {
277: $Apache::lonhomework::scantronmode=1;
278: }
279:
280: if ($target ne 'analyze') {
281: &initialize_storage();
282: if ($target eq 'web') {
283: &Apache::lonhomework::showhash(%Apache::lonhomework::history);
284: }
285: $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
286: &Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
287: }
288: if ($Apache::lonhomework::type eq '' ) {
289: my $uri=$ENV{'request.uri'};
290: if ($uri=~/\.(\w+)$/) {
291: $Apache::lonhomework::type=$1;
292: &Apache::lonxml::debug("Using type of $1");
293: } else {
294: $Apache::lonhomework::type='problem';
295: &Apache::lonxml::debug("Using default type, problem, :$uri:");
296: }
297: }
298:
299: #added vars to the scripting enviroment
300: my $expression='$external::part='.$Apache::inputtags::part.';';
301: &Apache::run::run($expression,$safeeval);
302: my $status;
303: my $accessmsg;
304:
305: #should get back a <html> or the neccesary stuff to start XML/MathML
306: my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
307: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
308: if ($target eq 'tex' and $ENV{'request.symb'} =~ m/\.page_/) {$result='';}
309:
310: if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
311: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
312: $target eq 'tex') {
313: #handle exam checkout
314: if ($Apache::lonhomework::type eq 'exam') {
315: my $token=
316: $Apache::lonhomework::history{"resource.0.outtoken"};
317: if (($ENV{'form.doescheckout'}) && (!$token)) {
318: $token=&Apache::lonxml::maketoken();
319: $Apache::lonhomework::history{"resource.0.outtoken"}=
320: $token;
321: }
322: $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
323: }
324:
325: #handle rand seed in construction space
326: my $rndseed=&setup_rndseed($safeeval);
327: my ($symb)=&Apache::lonxml::whichuser();
328: if ($ENV{'request.state'} ne "construct" && $symb eq '') {
329: $form_tag_start.='<input type="hidden" name="rndseed" value="'.
330: $rndseed.'" />'.
331: '<input type="submit" name="resetdata"
332: value="New Problem Variation" />'.
333: '<input type="hidden" name="username"
334: value="'.$ENV{'form.username'}.'" />';
335: }
336: ($status,$accessmsg) = &Apache::lonhomework::check_access('0');
337: push (@Apache::inputtags::status,$status);
338: my $expression='$external::datestatus="'.$status.'";';
339: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
340: &Apache::run::run($expression,$safeeval);
341: &Apache::lonxml::debug("Got $status");
342: if (( $status eq 'CLOSED' ) ||
343: ( $status eq 'UNCHECKEDOUT') ||
344: ( $status eq 'BANNED') ||
345: ( $status eq 'UNAVAILABLE')) {
346: my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
347: if ( $target eq "web" ) {
348: $result.= $head_tag_start.'</head>';
349: my $msg=$body_tag_start;
350: if ($status eq 'UNAVAILABLE') {
351: $result.='<h1>Unable to determine if this resource is open due to network problems. Please try again later.</h1>';
352: } else {
353: $result.='<h1>Not open to be viewed</h1>';
354: }
355: if ($status eq 'CLOSED') {
356: $msg.='The problem '.$accessmsg;
357: } elsif ($status eq 'UNCHECKEDOUT') {
358: $msg.=&checkout_msg;
359: }
360: $result.=$msg.'<br />';
361: } elsif ($target eq 'tex') {
362: $result.='\begin{document}\noindent \vskip 1 mm \begin{minipage}{\textwidth}\vskip 0 mm';
363: if ($status eq 'UNAVAILABLE') {
364: $result.='Unable to determine if this resource is open due to network problems. Please try again later.\vskip 0 mm ';
365: } else {
366: $result.="Problem is not open to be viewed. It $accessmsg \\vskip 0 mm ";
367: }
368: }
369: } elsif ($target eq 'web') {
370: my $name= &get_resource_name($parstack,$safeeval);
371: if ($status eq 'CAN_ANSWER') {
372: # create a page header and exit
373: $result.="$head_tag_start<title>$name</title></head>
374: $body_tag_start \n $form_tag_start".
375: '<input type="hidden" name="submitted" value="yes" />';
376: if ($ENV{'request.state'} eq "construct") {
377: $result.= &problem_web_to_edit_header($rndseed);
378: }
379: # if we are viewing someone else preserve that info
380: if (defined $ENV{'form.grade_symb'}) {
381: foreach my $field ('symb','courseid','domain','username') {
382: $result .= '<input type="hidden" name="grade_'.$field.
383: '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
384: }
385: }
386: } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
387: || $status eq 'CLOSED' || $status eq 'UNAVALAILABLE') {
388: $result.=$head_tag_start.
389: "<title>$name</title></head>\n$body_tag_start\n";
390: }
391: } elsif ($target eq 'tex') {
392: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
393: if ($name eq '') {
394: $name=&Apache::lonnet::EXT('resource.title');
395: if ($name eq 'con_lost') { $name = ''; }
396: }
397: $Apache::lonhomework::name=$name;
398: my $id = $Apache::inputtags::part;
399: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
400: my $allkeys=&Apache::lonnet::metadata($ENV{'request.uri'},'keys');
401: my @allkeys = split /,/,$allkeys;
402: my $allow_print_points = 0;
403: foreach my $partial_key (@allkeys) {
404: if ($partial_key=~m/weight/) {
405: $allow_print_points++;
406: }
407: }
408: my $duedate = &Apache::lonnet::EXT("resource.$id.duedate");
409: $duedate = POSIX::strftime("%c",localtime($duedate));
410: my $temp_file;
411: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
412: if (-e $filename) {
413: $temp_file = Apache::File->new($filename);
414: } else {
415: $temp_file = Apache::File->new('>>'.$filename);
416: }
417: my @due_file_content = <$temp_file>;
418: my $due_file_content = $due_file_content[$#due_file_content];
419: chomp $due_file_content;
420: my $name_of_resourse= &get_resource_name($parstack,$safeeval);
421: if ($due_file_content ne $duedate) {
422: $temp_file = Apache::File->new('>'.$filename);
423: print $temp_file "$duedate\n";
424: if (not $ENV{'request.symb'} =~ m/\.page_/) {
425: if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
426: $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}';
427: } else {
428: $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}';
429: if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
430: }
431: } else {
432: $result .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
433: }
434: } else {
435: if (not $ENV{'request.symb'} =~ m/\.page_/) {
436: $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}';
437: if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
438: } else {
439: $result .= '\vskip 1mm \\\\\\\\';
440: }
441: }
442: }
443: } elsif ($target eq 'edit') {
444: $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
445: &problem_edit_header();
446: my $temp=&Apache::edit::insertlist($target,$token);
447: $result.=$temp;
448: } elsif ($target eq 'modified') {
449: $result=$token->[4];
450: $result.=&Apache::edit::handle_insert();
451: } else {
452: # page_start returned a starting result, delete it if we don't need it
453: $result = '';
454: }
455: return $result;
456: }
457:
458: sub end_problem {
459: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
460: my $result='';
461: my $status=$Apache::inputtags::status['-1'];
462: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
463: $target eq 'tex') {
464: if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
465: $status eq 'CAN_ANSWER' ) {
466: # if part is zero, no <part>s existed, so we need to the grading
467: &Apache::inputtags::grade;
468: } elsif ( ($target eq 'web' || $target eq 'tex') &&
469: $Apache::inputtags::part eq '0' &&
470: $status ne 'UNCHECKEDOUT') {
471: # if part is zero, no <part>s existed, so we need show the current
472: # grading status
473: my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
474: $result.= $gradestatus;
475: }
476: if (
477: (($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
478: ($target eq 'answer') || ($target eq 'tex')
479: ) {
480: if ($status eq 'CAN_ANSWER') {
481: if ($target ne 'tex') {
482: $result.="</form></body>\n";
483: }
484: } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
485: $status eq 'UNCHECKEDOUT' ) {
486: if ($target ne 'tex') {
487: $result.="</body>\n";
488: }
489: }
490: if ($target eq 'web') {
491: $result.=&Apache::lonxml::xmlend();
492: } elsif ($target eq 'tex') {
493: $result .= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
494: if (not $ENV{'request.symb'} =~ m/\.page_/) {
495: $result .= '\end{minipage}\end{document} ';
496: } else {
497: $result .= '';
498: }
499: }
500: }
501: if ($target eq 'grade') {
502: &Apache::lonhomework::showhash(%Apache::lonhomework::results);
503: &finalize_storage();
504: }
505: if ($target eq 'answer' && ($ENV{'request.state'} eq 'construct') ) {
506: $result.='</html>'; #normally we get it from xmlend, but in CSTR
507: # we always show answer mode too.
508: }
509: } elsif ($target eq 'meta') {
510: if ($Apache::inputtags::part eq '0') {
511: $result=&Apache::response::mandatory_part_meta;
512: }
513: } elsif ($target eq 'edit') {
514: &Apache::lonxml::debug("in end_problem with $target, edit");
515: $result = &problem_edit_footer();
516: }
517:
518: undef(%Apache::lonhomework::history);
519: undef(%Apache::lonhomework::results);
520: undef($Apache::inputtags::part);
521: undef($Apache::lonhomework::parsing_a_problem);
522:
523: return $result;
524: }
525:
526:
527: sub start_library {
528: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
529: my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
530:
531: if ($target eq 'edit') {
532: ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
533: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
534: $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
535: &problem_edit_header();
536: my $temp=&Apache::edit::insertlist($target,$token);
537: $result.=$temp;
538: } elsif ($target eq 'modified') {
539: $result=$token->[4];
540: $result.=&Apache::edit::handle_insert();
541: } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
542: $ENV{'request.state'} eq "construct" ) {
543: ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
544: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
545: my $name=&get_resource_name($parstack,$safeeval);
546: my $rndseed=&setup_rndseed($safeeval);
547: $result.="$head_tag_start<title>$name</title></head>
548: $body_tag_start \n $form_tag_start".
549: '<input type="hidden" name="submitted" value="yes" />';
550: $result.=&problem_web_to_edit_header($rndseed);
551: }
552: return $result;
553: }
554:
555: sub end_library {
556: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
557: my $result='';
558: if ($target eq 'edit') {
559: $result=&problem_edit_footer();
560: } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
561: $ENV{'request.state'} eq "construct") {
562: $result.='</form></body>'.&Apache::lonxml::xmlend();
563: }
564: return $result;
565: }
566:
567: sub start_block {
568: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
569:
570: my $result;
571:
572: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
573: $target eq 'tex' || $target eq 'analyze') {
574: my $code = $token->[2]->{'condition'};
575: if ($code) {
576: if (!$Apache::lonxml::default_homework_loaded) {
577: &Apache::lonxml::default_homework_load($safeeval);
578: }
579: $result = &Apache::run::run($code,$safeeval);
580: &Apache::lonxml::debug("block :$code: returned :$result:");
581: } else {
582: $result='1';
583: }
584: if ( ! $result ) {
585: my $skip=&Apache::lonxml::get_all_text("/block",$parser);
586: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
587: }
588: $result='';
589: } elsif ($target eq 'edit') {
590: $result .=&Apache::edit::tag_start($target,$token);
591: $result .=&Apache::edit::text_arg('Test Condition:','condition',
592: $token,40);
593: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
594: } elsif ($target eq 'modified') {
595: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
596: $safeeval,'condition');
597: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
598: }
599: return $result;
600: }
601:
602: sub end_block {
603: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
604: my $result;
605: if ($target eq "edit") {
606: $result.= &Apache::edit::tag_end($target,$token,'');
607: }
608: return $result;
609: }
610:
611: sub start_while {
612: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
613:
614: my $result;
615: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
616: $target eq 'tex' || $target eq 'analyze') {
617: my $code = $token->[2]->{'condition'};
618:
619: push( @Apache::structuretags::whileconds, $code);
620: if (!$Apache::lonxml::default_homework_loaded) {
621: &Apache::lonxml::default_homework_load($safeeval);
622: }
623: my $result = &Apache::run::run($code,$safeeval);
624: my $bodytext=&Apache::lonxml::get_all_text("/while",$parser);
625: push( @Apache::structuretags::whilebody, $bodytext);
626: push( @Apache::structuretags::whileline, $token->[5]);
627: &Apache::lonxml::debug("s code $code got -$result-");
628: if ( $result ) {
629: &Apache::lonxml::newparser($parser,\$bodytext);
630: }
631: } elsif ($target eq 'edit') {
632: $result .=&Apache::edit::tag_start($target,$token);
633: $result .=&Apache::edit::text_arg('Test Condition:','condition',
634: $token,40);
635: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
636: } elsif ($target eq 'modified') {
637: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
638: $safeeval,'condition');
639: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
640: }
641: return $result;
642: }
643:
644: sub end_while {
645: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
646: my $result;
647:
648: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
649: $target eq 'tex' || $target eq 'analyze') {
650: my $code = pop(@Apache::structuretags::whileconds);
651: my $bodytext = pop(@Apache::structuretags::whilebody);
652: my $line = pop(@Apache::structuretags::whileline);
653: my $return = &Apache::run::run($code,$safeeval);
654: my $starttime=time;
655: my $error=0;
656: while ($return) {
657: if (time-$starttime >
658: $Apache::lonnet::perlvar{'lonScriptTimeout'}) {
659: $return = 0; $error=1; next;
660: }
661: $result.=&Apache::scripttag::xmlparse($bodytext);
662: $return = &Apache::run::run($code,$safeeval);
663: }
664: if ($error) {
665: &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>');
666: }
667: } elsif ($target eq "edit") {
668: $result.= &Apache::edit::tag_end($target,$token,'');
669: }
670: return $result;
671: }
672:
673: # <randomlist show="1">
674: # <tag1>..</tag1>
675: # <tag2>..</tag2>
676: # <tag3>..</tag3>
677: # ...
678: # </randomlist>
679: sub start_randomlist {
680: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
681: my $result;
682: if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
683: $target eq 'tex' || $target eq 'analyze') {
684: my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
685: my $b_parser= HTML::TokeParser->new(\$body);
686: my $b_tok;
687: my @randomlist;
688: my $list_item;
689: while($b_tok = $b_parser->get_token() ) {
690: if($b_tok->[0] eq 'S') { # start tag
691: # get content of the tag until matching end tag
692: # get all text upto the matching tag
693: # and push the content into @randomlist
694: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
695: $b_parser);
696: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
697: push(@randomlist,$list_item);
698: # print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
699: # $list_item</b>";
700: }
701: if($b_tok->[0] eq 'T') { # text
702: # what to do with text in between tags?
703: # print "<b>TEXT $b_tok->[1]</b><br />";
704: }
705: # if($b_tok->[0] eq 'E') { # end tag, should not happen
706: # print "<b>END-TAG $b_tok->[1]</b><br />";
707: # }
708: }
709: my @idx_arr = (0 .. $#randomlist);
710: &Apache::structuretags::shuffle(\@idx_arr);
711: my $bodytext = '';
712: my $show=$#randomlist;
713: my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
714: $showarg--;
715: if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
716: for(0 .. $show) {
717: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
718: }
719: &Apache::lonxml::newparser($parser,\$bodytext);
720: } elsif ($target eq 'edit' ) {
721: $result .=&Apache::edit::tag_start($target,$token);
722: $result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
723: $token,5);
724: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
725: } elsif ($target eq 'modified' ) {
726: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
727: $safeeval,'show');
728: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
729: }
730: return $result;
731: }
732:
733: sub shuffle {
734: my $a=shift;
735: my $i;
736: if (defined(@$a)) {
737: &Apache::response::setrandomnumber();
738: for($i=@$a;--$i;) {
739: my $j=int(&Math::Random::random_uniform() * ($i+1));
740: next if $i == $j;
741: @$a[$i,$j] = @$a[$j,$i];
742: }
743: }
744: }
745:
746: sub end_randomlist {
747: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
748: my $result;
749: if ($target eq 'edit' ) {
750: $result=&Apache::edit::tag_end($target,$token,
751: 'End Randomly Parsed Block');
752: }
753: return $result;
754: }
755:
756: sub start_part {
757: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
758: my $result='';
759: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
760: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
761: $Apache::inputtags::part=$id;
762: @Apache::inputtags::responselist = ();
763: @Apache::inputtags::previous=();
764: @Apache::inputtags::previous_version=();
765: my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
766:
767: if ($target eq 'meta') {
768: return &Apache::response::mandatory_part_meta;
769: } elsif ($target eq 'web' || $target eq 'grade' ||
770: $target eq 'answer' || $target eq 'tex') {
771: if ($hidden) {
772: my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
773: } else {
774: my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
775: push (@Apache::inputtags::status,$status);
776: my $expression='$external::datestatus="'.$status.'";';
777: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
778: &Apache::run::run($expression,$safeeval);
779: if ( $status eq 'CLOSED' ) {
780: my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
781: if ( $target eq "web" ) {
782: $result="<br />Part is not open to be viewed. It $accessmsg<br />";
783: } elsif ( $target eq 'tex' ) {
784: $result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
785: }
786: } else {
787: if ($target eq 'tex') {
788: $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
789: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
790: if ($Apache::lonhomework::type eq 'exam') { $result .= '\fbox{\textit{'.$weight.' pt}}';}
791: }
792: }
793: }
794: } elsif ($target eq 'edit') {
795: $result.=&Apache::edit::tag_start($target,$token);
796: $result.=&Apache::edit::text_arg('Part ID:','id',$token).
797: &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
798: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
799: } elsif ($target eq 'modified') {
800: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
801: $safeeval,'id');
802: if ($constructtag) {
803: $result = &Apache::edit::rebuild_tag($token);
804: $result.=&Apache::edit::handle_insert();
805: }
806: }
807: return $result;
808: }
809:
810: sub end_part {
811: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
812: &Apache::lonxml::debug("in end_part $target ");
813: my $status=$Apache::inputtags::status['-1'];
814: my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
815: my $result='';
816: if ( $target eq 'meta' ) {
817: $result='';
818: } elsif ( $target eq 'grade' && $status eq 'CAN_ANSWER' && !$hidden) {
819: $result=&Apache::inputtags::grade;
820: } elsif (($target eq 'web' || $target eq 'tex') && !$hidden ) {
821: my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
822: $target);
823: if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
824: $result=$gradestatus;
825: }
826: pop @Apache::inputtags::status;
827: $Apache::inputtags::part='';
828: return $result;
829: }
830:
831: sub start_preduedate {
832: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
833: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
834: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
835: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' &&
836: $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
837: &Apache::lonxml::get_all_text("/preduedate",$parser);
838: }
839: }
840: return '';
841: }
842:
843: sub end_preduedate {
844: return '';
845: }
846:
847: sub start_postanswerdate {
848: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
849: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
850: if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
851: &Apache::lonxml::get_all_text("/postanswerdate",$parser);
852: }
853: } elsif ($target eq 'tex') {
854: return '\vskip 0 mm \noindent';
855: }
856: return '';
857: }
858:
859: sub end_postanswerdate {
860: return '';
861: }
862:
863: sub start_notsolved {
864: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
865: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
866: $target eq 'tex') {
867: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
868: &Apache::lonxml::debug("not solved has :$gradestatus:");
869: if ($gradestatus =~ /^correct/) {
870: &Apache::lonxml::debug("skipping");
871: &Apache::lonxml::get_all_text("/notsolved",$parser);
872: }
873: }
874: return '';
875: }
876:
877: sub end_notsolved {
878: return '';
879: }
880:
881: sub start_solved {
882: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
883: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
884: $target eq 'tex') {
885: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
886: if ($gradestatus !~ /^correct/) {
887: &Apache::lonxml::get_all_text("/solved",$parser);
888: }
889: }
890: return '';
891: }
892:
893: sub end_solved {
894: return '';
895: }
896:
897: sub start_startouttext {
898: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
899: my @result=(''.'');
900: if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
901: return (@result);
902: }
903:
904: sub end_startouttext {
905: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
906: my $result='';
907: my $text='';
908:
909: if ($target eq 'edit') {
910: $text=&Apache::lonxml::get_all_text("endouttext",$parser);
911: $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
912: <td>Delete:".
913: &Apache::edit::deletelist($target,$token)
914: ."</td>
915: <td>".
916: &Apache::edit::insertlist($target,$token).
917: &Apache::edit::end_row().
918: &Apache::edit::start_spanning_row()."\n"
919: .'<table><tr><td>'.
920: &Apache::loncommon::help_open_topic("Greek_Symbols",
921: 'Greek Symbols',
922: undef,undef,600)
923: .'</td><td>'.
924: &Apache::loncommon::help_open_topic("Other_Symbols",
925: 'Other Symbols',
926: undef,undef,600)
927: .'</td></tr></table>'.
928: &Apache::edit::editfield($token->[1],$text,"",80,4);
929: }
930: if ($target eq 'modified') {
931: $text=&Apache::lonxml::get_all_text("endouttext",$parser);
932: $result='<startouttext />'.&Apache::edit::modifiedfield();
933: }
934: if ($target eq 'tex') {
935: $result .= '\noindent ';
936: }
937: return $result;
938: }
939:
940: sub start_endouttext {
941: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
942: my $result='';
943: if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
944: if ($target eq "modified") {
945: $result='<endouttext />'.
946: &Apache::edit::handle_insertafter('startouttext'); }
947: return $result;
948: }
949:
950: sub end_endouttext {
951: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
952: my @result=('','');
953: if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
954: return (@result);
955: }
956:
957: sub delete_startouttext {
958: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
959: # my $text=&Apache::lonxml::get_all_text("endouttext",$parser);
960: my $text=$$parser['-1']->get_text("/endouttext");
961: my $ntoken=$$parser['-1']->get_token();
962: &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
963: &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
964: # Deleting 2 parallel tag pairs, but we need the numbers later to look like
965: # they did the last time round
966: &Apache::lonxml::increasedepth($ntoken);
967: &Apache::lonxml::decreasedepth($ntoken);
968: return 1;
969: }
970:
971: 1;
972: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>