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