1: # The LearningOnline Network with CAPA
2: # definition of tags that give a structure to a document
3: #
4: # $Id: structuretags.pm,v 1.168 2003/05/04 08:00:31 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','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_languageblock {
612: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
613:
614: my $result;
615:
616: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
617: $target eq 'tex' || $target eq 'analyze') {
618: my $include = $token->[2]->{'include'};
619: my $exclude = $token->[2]->{'exclude'};
620: my %languages=&Apache::loncommon::display_languages();
621: $result='1';
622: if ($include) {
623: $result='';
624: foreach (split(/\,/,$include)) {
625: if ($languages{$_}) { $result='1'; }
626: }
627: }
628: if ($exclude) {
629: foreach (split(/\,/,$exclude)) {
630: if ($languages{$_}) { $result='0'; }
631: }
632: }
633: if ( ! $result ) {
634: my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser);
635: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
636: }
637: $result='';
638: } elsif ($target eq 'edit') {
639: $result .=&Apache::edit::tag_start($target,$token);
640: $result .=&Apache::edit::text_arg('Include Language:','include',
641: $token,40);
642: $result .=&Apache::edit::text_arg('Exclude Language:','exclude',
643: $token,40);
644: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
645: } elsif ($target eq 'modified') {
646: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
647: $safeeval,'include',
648: 'exclude');
649: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
650: }
651: return $result;
652: }
653:
654: sub end_languageblock {
655: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
656: my $result;
657: if ($target eq "edit") {
658: $result.= &Apache::edit::tag_end($target,$token,'');
659: }
660: return $result;
661: }
662:
663: sub start_while {
664: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
665:
666: my $result;
667: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
668: $target eq 'tex' || $target eq 'analyze') {
669: my $code = $token->[2]->{'condition'};
670:
671: push( @Apache::structuretags::whileconds, $code);
672: if (!$Apache::lonxml::default_homework_loaded) {
673: &Apache::lonxml::default_homework_load($safeeval);
674: }
675: my $result = &Apache::run::run($code,$safeeval);
676: my $bodytext=&Apache::lonxml::get_all_text("/while",$parser);
677: push( @Apache::structuretags::whilebody, $bodytext);
678: push( @Apache::structuretags::whileline, $token->[5]);
679: &Apache::lonxml::debug("s code $code got -$result-");
680: if ( $result ) {
681: &Apache::lonxml::newparser($parser,\$bodytext);
682: }
683: } elsif ($target eq 'edit') {
684: $result .=&Apache::edit::tag_start($target,$token);
685: $result .=&Apache::edit::text_arg('Test Condition:','condition',
686: $token,40);
687: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
688: } elsif ($target eq 'modified') {
689: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
690: $safeeval,'condition');
691: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
692: }
693: return $result;
694: }
695:
696: sub end_while {
697: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
698: my $result;
699:
700: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
701: $target eq 'tex' || $target eq 'analyze') {
702: my $code = pop(@Apache::structuretags::whileconds);
703: my $bodytext = pop(@Apache::structuretags::whilebody);
704: my $line = pop(@Apache::structuretags::whileline);
705: my $return = &Apache::run::run($code,$safeeval);
706: my $starttime=time;
707: my $error=0;
708: while ($return) {
709: if (time-$starttime >
710: $Apache::lonnet::perlvar{'lonScriptTimeout'}) {
711: $return = 0; $error=1; next;
712: }
713: $result.=&Apache::scripttag::xmlparse($bodytext);
714: $return = &Apache::run::run($code,$safeeval);
715: }
716: if ($error) {
717: &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>');
718: }
719: } elsif ($target eq "edit") {
720: $result.= &Apache::edit::tag_end($target,$token,'');
721: }
722: return $result;
723: }
724:
725: # <randomlist show="1">
726: # <tag1>..</tag1>
727: # <tag2>..</tag2>
728: # <tag3>..</tag3>
729: # ...
730: # </randomlist>
731: sub start_randomlist {
732: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
733: my $result;
734: if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
735: $target eq 'tex' || $target eq 'analyze') {
736: my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
737: my $b_parser= HTML::TokeParser->new(\$body);
738: my $b_tok;
739: my @randomlist;
740: my $list_item;
741: while($b_tok = $b_parser->get_token() ) {
742: if($b_tok->[0] eq 'S') { # start tag
743: # get content of the tag until matching end tag
744: # get all text upto the matching tag
745: # and push the content into @randomlist
746: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
747: $b_parser);
748: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
749: push(@randomlist,$list_item);
750: # print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
751: # $list_item</b>";
752: }
753: if($b_tok->[0] eq 'T') { # text
754: # what to do with text in between tags?
755: # print "<b>TEXT $b_tok->[1]</b><br />";
756: }
757: # if($b_tok->[0] eq 'E') { # end tag, should not happen
758: # print "<b>END-TAG $b_tok->[1]</b><br />";
759: # }
760: }
761: my @idx_arr = (0 .. $#randomlist);
762: &Apache::structuretags::shuffle(\@idx_arr);
763: my $bodytext = '';
764: my $show=$#randomlist;
765: my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
766: $showarg--;
767: if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
768: for(0 .. $show) {
769: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
770: }
771: &Apache::lonxml::newparser($parser,\$bodytext);
772: } elsif ($target eq 'edit' ) {
773: $result .=&Apache::edit::tag_start($target,$token);
774: $result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
775: $token,5);
776: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
777: } elsif ($target eq 'modified' ) {
778: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
779: $safeeval,'show');
780: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
781: }
782: return $result;
783: }
784:
785: sub shuffle {
786: my $a=shift;
787: my $i;
788: if (defined(@$a)) {
789: &Apache::response::setrandomnumber();
790: for($i=@$a;--$i;) {
791: my $j=int(&Math::Random::random_uniform() * ($i+1));
792: next if $i == $j;
793: @$a[$i,$j] = @$a[$j,$i];
794: }
795: }
796: }
797:
798: sub end_randomlist {
799: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
800: my $result;
801: if ($target eq 'edit' ) {
802: $result=&Apache::edit::tag_end($target,$token,
803: 'End Randomly Parsed Block');
804: }
805: return $result;
806: }
807:
808: sub start_part {
809: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
810: my $result='';
811: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
812: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
813: $Apache::inputtags::part=$id;
814: @Apache::inputtags::responselist = ();
815: @Apache::inputtags::previous=();
816: @Apache::inputtags::previous_version=();
817: my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
818:
819: if ($target eq 'meta') {
820: return &Apache::response::mandatory_part_meta;
821: } elsif ($target eq 'web' || $target eq 'grade' ||
822: $target eq 'answer' || $target eq 'tex') {
823: if ($hidden) {
824: my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
825: } else {
826: my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
827: push (@Apache::inputtags::status,$status);
828: my $expression='$external::datestatus="'.$status.'";';
829: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
830: &Apache::run::run($expression,$safeeval);
831: if ( $status eq 'CLOSED' ) {
832: my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
833: if ( $target eq "web" ) {
834: $result="<br />Part is not open to be viewed. It $accessmsg<br />";
835: } elsif ( $target eq 'tex' ) {
836: $result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
837: }
838: } else {
839: if ($target eq 'tex') {
840: $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
841: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
842: if ($Apache::lonhomework::type eq 'exam') { $result .= '\fbox{\textit{'.$weight.' pt}}';}
843: }
844: }
845: }
846: } elsif ($target eq 'edit') {
847: $result.=&Apache::edit::tag_start($target,$token);
848: $result.=&Apache::edit::text_arg('Part ID:','id',$token).
849: &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
850: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
851: } elsif ($target eq 'modified') {
852: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
853: $safeeval,'id');
854: if ($constructtag) {
855: $result = &Apache::edit::rebuild_tag($token);
856: $result.=&Apache::edit::handle_insert();
857: }
858: }
859: return $result;
860: }
861:
862: sub end_part {
863: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
864: &Apache::lonxml::debug("in end_part $target ");
865: my $status=$Apache::inputtags::status['-1'];
866: my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
867: my $result='';
868: if ( $target eq 'meta' ) {
869: $result='';
870: } elsif ( $target eq 'grade' && $status eq 'CAN_ANSWER' && !$hidden) {
871: $result=&Apache::inputtags::grade;
872: } elsif (($target eq 'web' || $target eq 'tex') && !$hidden ) {
873: my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
874: $target);
875: if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
876: $result=$gradestatus;
877: }
878: pop @Apache::inputtags::status;
879: $Apache::inputtags::part='';
880: return $result;
881: }
882:
883: sub start_preduedate {
884: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
885: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
886: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
887: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' &&
888: $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
889: &Apache::lonxml::get_all_text("/preduedate",$parser);
890: }
891: }
892: return '';
893: }
894:
895: sub end_preduedate {
896: return '';
897: }
898:
899: sub start_postanswerdate {
900: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
901: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
902: if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
903: &Apache::lonxml::get_all_text("/postanswerdate",$parser);
904: }
905: } elsif ($target eq 'tex') {
906: return '\vskip 0 mm \noindent';
907: }
908: return '';
909: }
910:
911: sub end_postanswerdate {
912: return '';
913: }
914:
915: sub start_notsolved {
916: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
917: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
918: $target eq 'tex') {
919: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
920: &Apache::lonxml::debug("not solved has :$gradestatus:");
921: if ($gradestatus =~ /^correct/) {
922: &Apache::lonxml::debug("skipping");
923: &Apache::lonxml::get_all_text("/notsolved",$parser);
924: }
925: }
926: return '';
927: }
928:
929: sub end_notsolved {
930: return '';
931: }
932:
933: sub start_solved {
934: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
935: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
936: $target eq 'tex') {
937: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
938: if ($gradestatus !~ /^correct/) {
939: &Apache::lonxml::get_all_text("/solved",$parser);
940: }
941: }
942: return '';
943: }
944:
945: sub end_solved {
946: return '';
947: }
948:
949: sub start_startouttext {
950: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
951: my @result=(''.'');
952: if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
953: return (@result);
954: }
955:
956: sub end_startouttext {
957: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
958: my $result='';
959: my $text='';
960:
961: if ($target eq 'edit') {
962: $text=&Apache::lonxml::get_all_text("endouttext",$parser);
963: $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
964: <td>Delete:".
965: &Apache::edit::deletelist($target,$token)
966: ."</td>
967: <td>".
968: &Apache::edit::insertlist($target,$token).
969: &Apache::edit::end_row().
970: &Apache::edit::start_spanning_row()."\n"
971: .'<table><tr><td>'.
972: &Apache::loncommon::help_open_topic("Greek_Symbols",
973: 'Greek Symbols',
974: undef,undef,600)
975: .'</td><td>'.
976: &Apache::loncommon::help_open_topic("Other_Symbols",
977: 'Other Symbols',
978: undef,undef,600)
979: .'</td></tr></table>'.
980: &Apache::edit::editfield($token->[1],$text,"",80,4);
981: }
982: if ($target eq 'modified') {
983: $text=&Apache::lonxml::get_all_text("endouttext",$parser);
984: $result='<startouttext />'.&Apache::edit::modifiedfield();
985: }
986: if ($target eq 'tex') {
987: $result .= '\noindent ';
988: }
989: return $result;
990: }
991:
992: sub start_endouttext {
993: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
994: my $result='';
995: if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
996: if ($target eq "modified") {
997: $result='<endouttext />'.
998: &Apache::edit::handle_insertafter('startouttext'); }
999: return $result;
1000: }
1001:
1002: sub end_endouttext {
1003: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1004: my @result=('','');
1005: if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
1006: return (@result);
1007: }
1008:
1009: sub delete_startouttext {
1010: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1011: # my $text=&Apache::lonxml::get_all_text("endouttext",$parser);
1012: my $text=$$parser['-1']->get_text("/endouttext");
1013: my $ntoken=$$parser['-1']->get_token();
1014: &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
1015: &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
1016: # Deleting 2 parallel tag pairs, but we need the numbers later to look like
1017: # they did the last time round
1018: &Apache::lonxml::increasedepth($ntoken);
1019: &Apache::lonxml::decreasedepth($ntoken);
1020: return 1;
1021: }
1022:
1023: 1;
1024: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>