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