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