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