1: # The LearningOnline Network with CAPA
2: # definition of tags that give a structure to a document
3: #
4: # $Id: structuretags.pm,v 1.136 2002/11/12 05:05:27 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: $result = &Apache::run::run($code,$safeeval);
506: &Apache::lonxml::debug("block :$code: returned :$result:");
507: } else {
508: $result='1';
509: }
510: if ( ! $result ) {
511: my $skip=&Apache::lonxml::get_all_text("/block",$$parser[-1]);
512: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
513: }
514: $result='';
515: } elsif ($target eq 'edit') {
516: $result .=&Apache::edit::tag_start($target,$token);
517: $result .=&Apache::edit::text_arg('Test Condition:','condition',
518: $token,40);
519: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
520: } elsif ($target eq 'modified') {
521: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
522: $safeeval,'condition');
523: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
524: }
525: return $result;
526: }
527:
528: sub end_block {
529: return '';
530: }
531:
532: sub start_while {
533: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
534:
535: my $code = @$parstack[$#$parstack];
536: $code =~ s/\"//g;
537: $code .=';return $condition;';
538:
539: push( @Apache::structuretags::whileconds, $code);
540: my $result = &Apache::run::run($code,$safeeval);
541: my $bodytext=$$parser[$#$parser]->get_text("/while");
542: push( @Apache::structuretags::whilebody, $bodytext);
543: if ( $result ) {
544: &Apache::lonxml::newparser($parser,\$bodytext);
545: }
546: return "";
547: }
548:
549: sub end_while {
550: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
551: my $code = pop @Apache::structuretags::whileconds;
552: my $bodytext = pop @Apache::structuretags::whilebody;
553: my $result = &Apache::run::run($code,$safeeval);
554: if ( $result ) {
555: &Apache::lonxml::newparser($parser,\$bodytext);
556: }
557: return "";
558: }
559:
560: # <randomlist show="1">
561: # <tag1>..</tag1>
562: # <tag2>..</tag2>
563: # <tag3>..</tag3>
564: # ...
565: # </randomlist>
566: sub start_randomlist {
567: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
568: my $result;
569: if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
570: $target eq 'tex' || $target eq 'analyze') {
571: my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
572: my $b_parser= HTML::TokeParser->new(\$body);
573: my $b_tok;
574: my @randomlist;
575: my $list_item;
576: while($b_tok = $b_parser->get_token() ) {
577: if($b_tok->[0] eq 'S') { # start tag
578: # get content of the tag until matching end tag
579: # get all text upto the matching tag
580: # and push the content into @randomlist
581: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
582: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
583: push(@randomlist,$list_item);
584: # print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
585: }
586: if($b_tok->[0] eq 'T') { # text
587: # what to do with text in between tags?
588: # print "<b>TEXT $b_tok->[1]</b><br />";
589: }
590: # if($b_tok->[0] eq 'E') { # end tag, should not happen
591: # print "<b>END-TAG $b_tok->[1]</b><br />";
592: # }
593: }
594: my @idx_arr = (0 .. $#randomlist);
595: &Apache::structuretags::shuffle(\@idx_arr);
596: my $bodytext = '';
597: my $show=$#randomlist;
598: my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
599: $showarg--;
600: if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
601: for(0 .. $show) {
602: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
603: }
604: &Apache::lonxml::newparser($parser,\$bodytext);
605: } elsif ($target eq 'edit' ) {
606: $result .= &Apache::edit::tag_start($target,$token);
607: $result .= &Apache::edit::text_arg('Maximum Tags to Show:','show',$token,5);
608: $result .= &Apache::edit::end_row().&Apache::edit::start_spanning_row();
609: } elsif ($target eq 'modified' ) {
610: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
611: 'show');
612: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
613: }
614: return $result;
615: }
616:
617: sub shuffle {
618: my $a=shift;
619: my $i;
620: if (defined(@$a)) {
621: &Apache::response::setrandomnumber();
622: for($i=@$a;--$i;) {
623: my $j=int(&Math::Random::random_uniform() * ($i+1));
624: next if $i == $j;
625: @$a[$i,$j] = @$a[$j,$i];
626: }
627: }
628: }
629:
630: sub end_randomlist {
631: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
632: my $result;
633: if ($target eq 'edit' ) {
634: $result=&Apache::edit::tag_end($target,$token,'End Randomly Parsed Block');
635: }
636: return $result;
637: }
638:
639: sub start_part {
640: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
641: my $result='';
642: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
643: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
644: $Apache::inputtags::part=$id;
645: @Apache::inputtags::responselist = ();
646: @Apache::inputtags::previous=();
647: if ($target eq 'meta') {
648: return &Apache::response::mandatory_part_meta;
649: } elsif ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
650: my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
651: push (@Apache::inputtags::status,$status);
652: my $expression='$external::datestatus="'.$status.'";';
653: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
654: &Apache::run::run($expression,$safeeval);
655: if ( $status eq 'CLOSED' ) {
656: my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
657: if ( $target eq "web" ) {
658: $result="<br />Part is not open to be viewed. It $accessmsg<br />";
659: } elsif ( $target eq 'tex' ) {
660: $result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
661: }
662: } else {
663: if ($target eq 'tex') {
664: if ($$tagstack[-2] ne 'problem') {
665: $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
666: }
667: }
668: }
669: } elsif ($target eq 'edit') {
670: $result.=&Apache::edit::tag_start($target,$token);
671: $result.=&Apache::edit::text_arg('Part ID:','id',$token).
672: &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
673: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
674:
675: } elsif ($target eq 'modified') {
676: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
677: 'id');
678: if ($constructtag) {
679: $result = &Apache::edit::rebuild_tag($token);
680: $result.=&Apache::edit::handle_insert();
681: }
682: }
683: return $result;
684: }
685:
686: sub end_part {
687: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
688: &Apache::lonxml::debug("in end_part $target ");
689: my $status=$Apache::inputtags::status['-1'];
690: pop @Apache::inputtags::status;
691: if ( $target eq 'meta' ) { return ''; }
692: if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
693: return &Apache::inputtags::grade;
694: }
695: if ($target eq 'web' || $target eq 'tex' ) {
696: my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
697: if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
698: return $gradestatus;
699: }
700: return '';
701: }
702:
703: sub start_preduedate {
704: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
705: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
706: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
707: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' &&
708: $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
709: &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
710: }
711: }
712: return '';
713: }
714:
715: sub end_preduedate {
716: return '';
717: }
718:
719: sub start_postanswerdate {
720: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
721: if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
722: if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
723: &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
724: }
725: } elsif ($target eq 'tex') {
726: return '\vskip 0 mm \noindent';
727: }
728: return '';
729: }
730:
731: sub end_postanswerdate {
732: return '';
733: }
734:
735: sub start_notsolved {
736: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
737: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
738: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
739: &Apache::lonxml::debug("not solved has :$gradestatus:");
740: if ($gradestatus =~ /^correct/) {
741: &Apache::lonxml::debug("skipping");
742: &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
743: }
744: }
745: return '';
746: }
747:
748: sub end_notsolved {
749: return '';
750: }
751:
752: sub start_solved {
753: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
754: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
755: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
756: if ($gradestatus !~ /^correct/) {
757: &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
758: }
759: }
760: return '';
761: }
762:
763: sub end_solved {
764: return '';
765: }
766:
767: sub start_startouttext {
768: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
769: my @result=(''.'');
770: if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
771: return (@result);
772: }
773: sub end_startouttext {
774: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
775: my $result='';
776: my $text='';
777:
778: if ($target eq 'edit') {
779: $text=&Apache::lonxml::get_all_text("endouttext",$$parser[-1]);
780: $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
781: <td>Delete:".
782: &Apache::edit::deletelist($target,$token)
783: ."</td>
784: <td>".
785: &Apache::edit::insertlist($target,$token).
786: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n".
787: &Apache::edit::editfield($token->[1],$text,"",80,4);
788: }
789: if ($target eq 'modified') {
790: $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
791: $result='<startouttext />'.&Apache::edit::modifiedfield();
792: }
793: if ($target eq 'tex') {
794: $result .= '\noindent ';
795: }
796: return $result;
797: }
798: sub start_endouttext {
799: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
800: my $result='';
801: if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
802: if ($target eq "modified") {
803: $result='<endouttext />'.
804: &Apache::edit::handle_insertafter('startouttext'); }
805: return $result;
806: }
807: sub end_endouttext {
808: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
809: my @result=('','');
810: if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
811: return (@result);
812: }
813: sub delete_startouttext {
814: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
815: # my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
816: my $text=$$parser['-1']->get_text("/endouttext");
817: my $ntoken=$$parser['-1']->get_token();
818: &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
819: &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
820: # Deleting 2 parallel tag pairs, but we need the numbers later to look like
821: # they did the last time round
822: &Apache::lonxml::increasedepth($ntoken);
823: &Apache::lonxml::decreasedepth($ntoken);
824: return 1;
825: }
826:
827: 1;
828: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>