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