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