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