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