1: # The LearningOnline Network with CAPA
2: # definition of tags that give a structure to a document
3: # 2/19 Guy
4: # 6/26/2001 fixed extra web display at end of <web></web> tags
5: # 8/17 Gerd Kortemeyer
6:
7: package Apache::structuretags;
8:
9: use strict;
10: use Apache::lonnet;
11:
12: sub BEGIN {
13: &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
14: # &Apache::lonxml::register_insert('problem','',('part','postanswerdate','preduedate'))
15: }
16:
17: sub start_web {
18: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
19: my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
20: if ($target eq 'web') {
21: return $bodytext;
22: }
23: return '';
24: }
25:
26: sub end_web {
27: return '';
28: }
29:
30: sub start_tex {
31: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
32: my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
33: if ($target eq 'tex') {
34: return $bodytext
35: }
36: return '';
37: }
38:
39: sub end_tex {
40: return '';
41: }
42:
43: sub page_start {
44: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
45: my $result=&Apache::londefdef::start_html($target,$token,$tagstack,$parstack,$parser,$safeeval);
46: my $head_tag_start='<head>'.&Apache::lonxml::registerurl();
47: my $body_tag_start='<body onLoad="'.&Apache::lonxml::loadevents().'" '.
48: 'onUnload="'.&Apache::lonxml::unloadevents().'" ';
49: my $background=&Apache::lonxml::get_param('background',$parstack,$safeeval);
50: if ($background) {
51: $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
52: $background;
53: $body_tag_start.='background="'.$background.'" ';
54: } else {
55: my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,$safeeval);
56: if ($bgcolor) {
57: $body_tag_start.='bgcolor="'.$bgcolor.'" ';
58: } else {
59: $body_tag_start.='bgcolor="#ffffff"';
60: }
61: }
62: $body_tag_start.='>';
63: return ($result,$head_tag_start,$body_tag_start);
64: }
65:
66: sub start_problem {
67: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
68:
69: #intialize globals
70: $Apache::inputtags::part='0';
71: @Apache::inputtags::responselist = ();
72: @Apache::inputtags::previous=();
73: $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
74: &Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
75: if ($Apache::lonhomework::type eq '') {
76: my $uri=$ENV{'request.uri'};
77: if ($uri=~/\.(\w+)$/) {
78: $Apache::lonhomework::type=$1;
79: &Apache::lonxml::debug("Using type of $1");
80: } else {
81: $Apache::lonhomework::type='problem';
82: &Apache::lonxml::debug("Using default type, problem, :$uri:");
83: }
84: }
85: #adeed vars to the scripting enviroment
86: my $expression='$external::part='.$Apache::inputtags::part.';';
87: &Apache::run::run($expression,$safeeval);
88: my $status;
89: my $accessmsg;
90:
91: #should get back a <html> or the neccesary stuff to start XML/MathML
92: my ($result,$head_tag_start,$body_tag_start)=
93: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
94:
95: if ($target eq 'web') {
96: if ($ENV{'form.doescheckout'}) {
97: $body_tag_start.=&Apache::lonxml::maketoken('web');
98: }
99: }
100: if ($target eq 'web' || $target eq 'grade') {
101: ($status,$accessmsg) = &Apache::lonhomework::check_access('0');
102: push (@Apache::inputtags::status,$status);
103: my $expression='$external::datestatus="'.$status.'";';
104: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
105: &Apache::run::run($expression,$safeeval);
106: if (( $status eq 'CLOSED' ) ||
107: ( $status eq 'UNCHECKEDOUT') ||
108: ( $status eq 'BANNED')) {
109: my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
110: if ( $target eq "web" ) {
111: $result.= $head_tag_start.'</head>';
112: my $msg=$body_tag_start.
113: '<h1>Not open to be viewed</h1>';
114: if ($status eq 'CLOSED') {
115: $msg.='The problem '.$accessmsg;
116: } elsif ($status eq 'UNCHECKEDOUT') {
117: $msg.=(<<ENDCHECKOUT);
118: <h2>The resource needs to be checked out</h2>
119: As a resource gets checked out, a unique timestamped ID is given to it, and a
120: permanent record is left in the system.<p />
121: <font color=red>
122: Checking out resources is subject to course policies, and may exclude future
123: credit even if done erroneously.<p />
124: </font>
125: <form method=post>
126: <input type=submit name="doescheckout"
127: value="Check out Exam for Viewing" />
128: </form>
129: ENDCHECKOUT
130: }
131: return $result.$msg.'<br />';
132: }
133: }
134: }
135: if ($target eq 'web') {
136: my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
137: if ($name eq '') {
138: $name=&Apache::lonnet::EXT('resource.title');
139: if ($name eq 'con_lost') { $name = ''; }
140: }
141: $Apache::lonhomework::name=$name;
142: if ($status eq 'CAN_ANSWER') {
143: # create a page header and exit
144: $result.="$head_tag_start<title>$name</title></head>\n
145: $body_tag_start\n
146: <form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">".
147: '<input type="hidden" name="submitted" value="yes" />';
148: if ($ENV{'request.state'} eq "construct") {
149: $result.='<input type="hidden" name="problemmode" value="View" />
150: <input type="submit" name="problemmode" value="Edit" /><hr />';
151: }
152: return $result;
153: } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
154: return $result.$head_tag_start."<title>$name</title></head>\n$body_tag_start\n";
155: }
156: }
157: if ($target eq 'edit') {
158: $result.=$head_tag_start."</head>".$body_tag_start.
159: '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
160: <input type="hidden" name="submitted" value="edit" />
161: <input type="hidden" name="problemmode" value="Edit" />
162: <input type="submit" name="problemmode" value="View" />
163: <input type="submit" name="Undo" value="undo" /> <hr />
164: <input type="submit" name="submit" value="Submit Changes" /><br />
165: ';
166: my $temp=&Apache::edit::insertlist($target,$token);
167: $result.=$temp;
168: return $result;
169: }
170: if ($target eq 'modified') {
171: $result=$token->[4];
172: $result.=&Apache::edit::handle_insert();
173: return $result;
174: }
175: return '';
176: }
177:
178: sub end_problem {
179: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
180: my $result='';
181: my $status=$Apache::inputtags::status['-1'];
182: if ($target eq 'grade' || $target eq 'web' ) {
183: if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
184: $status eq 'CAN_ANSWER') {
185: # if part is zero, no <part>s existed, so we need to the grading
186: &Apache::inputtags::grade;
187: } elsif ($Apache::inputtags::part eq '0') {
188: # if part is zero, no <part>s existed, so we need show the current
189: # grading status
190: $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
191: }
192: if ($target eq 'web') {
193: if ($status eq 'CAN_ANSWER') {
194: $result.="</form></body>\n";
195: } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
196: $result.="</body>\n";
197: }
198: $result.=&Apache::lonxml::xmlend();
199: }
200: }
201: if ($target eq 'meta') {
202: if ($Apache::inputtags::part eq '0') {
203: $result=&Apache::response::mandatory_part_meta;
204: }
205: }
206: if ($target eq 'edit') {
207: &Apache::lonxml::debug("in end_problem with $target, edit");
208: $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
209: }
210: return $result;
211: }
212:
213: sub start_library {
214: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
215: my ($result,$head_tag_start,$body_tag_start)=
216: &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
217: if ($target eq 'edit') {
218: $result.=$head_tag_start."</head>".$body_tag_start.
219: '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
220: <input type="hidden" name="submitted" value="edit" />
221: <input type="hidden" name="problemmode" value="Edit" />
222: <input type="submit" name="problemmode" value="View" />
223: <input type="submit" name="Undo" value="undo" /> <hr />
224: ';
225: my $temp=&Apache::edit::insertlist($target,$token);
226: $result.=$temp;
227: return $result;
228: }
229: if ($target eq 'modified') {
230: $result=$token->[4];
231: $result.=&Apache::edit::handle_insert();
232: return $result;
233: }
234: return '';
235: }
236:
237: sub end_library {
238: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
239: my $result='';
240: if ($target eq 'edit') {
241: $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
242: }
243: return $result;
244: }
245:
246: sub start_block {
247: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
248:
249: if ($target eq 'web' || $target eq 'grade') {
250: my $code = @$parstack[$#$parstack];
251: $code =~ s/\"//g;
252: $code .=';return $condition;';
253: # print "<br />$code<br />";
254: my $result = &Apache::run::run($code,$safeeval);
255: &Apache::lonxml::debug("block :$code: returned :$result:");
256: if ( ! $result ) {
257: my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
258: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
259: }
260: }
261: return "";
262: }
263:
264: sub end_block {
265: return '';
266: }
267:
268: sub start_while {
269: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
270:
271: my $code = @$parstack[$#$parstack];
272: $code =~ s/\"//g;
273: $code .=';return $condition;';
274:
275: push( @Apache::structuretags::whileconds, $code);
276: my $result = &Apache::run::run($code,$safeeval);
277: my $bodytext=$$parser[$#$parser]->get_text("/while");
278: push( @Apache::structuretags::whilebody, $bodytext);
279: if ( $result ) {
280: &Apache::lonxml::newparser($parser,\$bodytext);
281: }
282: return "";
283: }
284:
285: sub end_while {
286: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
287: my $code = pop @Apache::structuretags::whileconds;
288: my $bodytext = pop @Apache::structuretags::whilebody;
289: my $result = &Apache::run::run($code,$safeeval);
290: if ( $result ) {
291: &Apache::lonxml::newparser($parser,\$bodytext);
292: }
293: return "";
294: }
295:
296: # <randomlist>
297: # <tag1>..</tag1>
298: # <tag2>..</tag2>
299: # <tag3>..</tag3>
300: # ...
301: # </randomlist>
302: sub start_randomlist {
303: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
304: my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
305: my $b_parser= HTML::TokeParser->new(\$body);
306: my $b_tok;
307: my @randomlist;
308: my $list_item;
309:
310: while($b_tok = $b_parser->get_token() ) {
311: if($b_tok->[0] eq 'S') { # start tag
312: # get content of the tag until matching end tag
313: # get all text upto the matching tag
314: # and push the content into @randomlist
315: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
316: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
317: push(@randomlist,$list_item);
318: # print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
319: }
320: if($b_tok->[0] eq 'T') { # text
321: # what to do with text in between tags?
322: # print "<b>TEXT $b_tok->[1]</b><br />";
323: }
324: # if($b_tok->[0] eq 'E') { # end tag, should not happen
325: # print "<b>END-TAG $b_tok->[1]</b><br />";
326: # }
327: }
328: my @idx_arr = (0 .. $#randomlist);
329: &Apache::structuretags::shuffle(\@idx_arr);
330: my $bodytext = '';
331: for(0 .. $#randomlist) {
332: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
333: }
334:
335: &Apache::lonxml::newparser($parser,\$bodytext);
336: return "";
337: }
338:
339: sub shuffle {
340: my $a=shift;
341: my $i;
342: &Apache::response::setrandomnumber();
343: for($i=@$a;--$i;) {
344: my $j=int rand($i+1);
345: next if $i == $j;
346: @$a[$i,$j] = @$a[$j,$i];
347: }
348: }
349:
350: sub end_randomlist {
351: return '';
352: }
353:
354: sub start_part {
355: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
356: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
357: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
358: $Apache::inputtags::part=$id;
359: @Apache::inputtags::responselist = ();
360: @Apache::inputtags::previous=();
361: if ($target eq 'meta') {
362: return &Apache::response::mandatory_part_meta;
363: } elsif ($target eq 'web' || $target eq 'grade') {
364: my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
365: push (@Apache::inputtags::status,$status);
366: my $expression='$external::datestatus="'.$status.'";';
367: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
368: &Apache::run::run($expression,$safeeval);
369: if ( $status eq 'CLOSED' ) {
370: my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
371: if ( $target eq "web" ) {
372: return "<br />Part is not open to be viewed. It $accessmsg<br />";
373: }
374: }
375: }
376: return '';
377: }
378:
379: sub end_part {
380: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
381: &Apache::lonxml::debug("in end_part $target ");
382: my $status=$Apache::inputtags::status['-1'];
383: pop @Apache::inputtags::status;
384: if ( $target eq 'meta' ) { return ''; }
385: if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
386: return &Apache::inputtags::grade;
387: }
388: if ($target eq 'web') {
389: return &Apache::inputtags::gradestatus($Apache::inputtags::part);
390: }
391: return '';
392: }
393:
394: sub start_preduedate {
395: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
396: if ($target eq 'web' || $target eq 'grade') {
397: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
398: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
399: &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
400: }
401: }
402: return '';
403: }
404:
405: sub end_preduedate {
406: return '';
407: }
408:
409: sub start_postanswerdate {
410: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
411: if ($target eq 'web' || $target eq 'grade') {
412: if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
413: &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
414: }
415: }
416: return '';
417: }
418:
419: sub end_postanswerdate {
420: return '';
421: }
422:
423: sub start_notsolved {
424: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
425: if ($target eq 'web' || $target eq 'grade') {
426: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
427: &Apache::lonxml::debug("not solved has :$gradestatus:");
428: if ($gradestatus =~ /^correct/) {
429: &Apache::lonxml::debug("skipping");
430: &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
431: }
432: }
433: return '';
434: }
435:
436: sub end_notsolved {
437: return '';
438: }
439:
440: sub start_solved {
441: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
442: if ($target eq 'web' || $target eq 'grade') {
443: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
444: if ($gradestatus !~ /^correct/) {
445: &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
446: }
447: }
448: return '';
449: }
450:
451: sub end_solved {
452: return '';
453: }
454:
455: sub start_startouttext {
456: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
457: my @result=(''.'');
458: if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
459: return (@result);
460: }
461: sub end_startouttext {
462: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
463: my $result='';
464: my $text='';
465:
466: if ($target eq 'edit') {
467: $text=&Apache::lonxml::get_all_text("endouttext",$$parser[$#$parser]);
468: $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
469: <td>Delete:".
470: &Apache::edit::deletelist($target,$token)
471: ."</td>
472: <td>".
473: &Apache::edit::insertlist($target,$token).
474: "</td>
475: </tr><tr><td colspan=\"3\">\n".
476: &Apache::edit::editfield($token->[1],$text,"",50,4);
477: }
478: if ($target eq 'modified') {
479: $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
480: $result='<startouttext />'.&Apache::edit::modifiedfield();
481: }
482: return $result;
483: }
484: sub start_endouttext {
485: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
486: my $result='';
487: if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
488: if ($target eq "modified") { $result='<endouttext />'; }
489: return $result;
490: }
491: sub end_endouttext {
492: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
493: my @result=('','');
494: if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
495: return (@result);
496: }
497: sub delete_startouttext {
498: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
499: # my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
500: my $text=$$parser['-1']->get_text("/endouttext");
501: my $token=$$parser['-1']->get_token();
502: &Apache::lonxml::debug("Deleting :$text: and :$token->[0]:$token->[1]:$token->[2]: for startouttext");
503: &Apache::lonxml::end_tag($tagstack,$parstack,$token);
504: # Deleting 2 parallel tag pairs, but we need the numbers later to look like
505: # they did the last time round
506: &Apache::lonxml::increasedepth($token);
507: &Apache::lonxml::decreasedepth($token);
508: return 1;
509: }
510:
511: 1;
512: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>