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