Annotation of loncom/homework/structuretags.pm, revision 1.36
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.1 albertel 4: package Apache::structuretags;
5:
6: use strict;
7: use Apache::lonnet;
8:
9: sub BEGIN {
1.34 albertel 10: &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
1.10 albertel 11: }
12:
13: sub start_web {
14: my ($target,$token,$parstack,$parser,$safeeval)=@_;
15: my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
1.19 albertel 16: if ($target eq 'web') {
17: return $bodytext;
18: }
19: return '';
1.10 albertel 20: }
21:
22: sub end_web {
23: }
24:
25: sub start_tex {
26: my ($target,$token,$parstack,$parser,$safeeval)=@_;
27: my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
1.19 albertel 28: if ($target eq 'tex') {
29: return $bodytext
30: }
1.10 albertel 31: return '';
32: }
33:
34: sub end_tex {
1.9 albertel 35: }
36:
37: sub start_problem {
38: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.19 albertel 39:
40: #intialize globals
41: $Apache::inputtags::part='0';
42: @Apache::inputtags::responselist = ();
43:
44: #adeed vars to the scripting enviroment
45: my $expression='$external::part='.$Apache::inputtags::part.';';
46: &Apache::run::run($expression,$safeeval);
1.22 albertel 47: my $status;
48: my $datemsg;
1.36 ! albertel 49:
! 50: my $result=&Apache::londefdef::start_html($target,$token,$parstack,$parser,$safeeval);
! 51:
1.34 albertel 52: if ($target eq 'web' || $target eq 'grade') {
1.22 albertel 53: ($status,$datemsg) = &Apache::lonhomework::check_date('0');
54: push (@Apache::inputtags::status,$status);
1.24 albertel 55: my $expression='$external::datestatus="'.$status.'";';
56: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
57: &Apache::run::run($expression,$safeeval);
1.22 albertel 58: if ( $status eq 'CLOSED' ) {
1.21 albertel 59: my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
60: if ( $target eq "web" ) {
1.36 ! albertel 61: return $result."<body bgcolor=\"#FFFFFF\"> <br />Problem is not open to be viewed. The problem $datemsg<br />";
1.21 albertel 62: }
1.22 albertel 63: }
1.21 albertel 64: }
1.19 albertel 65: if ($target eq 'web') {
1.16 albertel 66: my $args ='';
1.19 albertel 67: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
1.16 albertel 68: my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
1.30 albertel 69: if ($name eq '') {
70: $name=&Apache::lonnet::EXT('resource.title');
71: if ($name eq 'con_lost') { $name = ''; }
72: }
73: $Apache::lonhomework::name=$name;
1.22 albertel 74: if ($status eq 'CAN_ANSWER') {
75: # create a page header and exit
1.36 ! albertel 76: $result.="<head><title>$name</title></head>\n
! 77: <body bgcolor=\"#FFFFFF\">\n
! 78: <form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">".
! 79: '<input type="hidden" name="submitted" value="yes" />';
! 80: if ($ENV{'request.state'} eq "construct") {
! 81: $result.='<input type="hidden" name="problemmode" value="View" />
! 82: <input type="submit" name="problemmode" value="Edit" /><hr />';
! 83: }
! 84: return $result;
1.29 albertel 85: } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
1.36 ! albertel 86: return $result."<title>$name</title>\n<body bgcolor=\"#FFFFFF\">\n";
1.22 albertel 87: }
88: }
1.34 albertel 89: if ($target eq 'edit') {
1.36 ! albertel 90: $result.='<body bgcolor="#FFFFFF">
1.35 albertel 91: <form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
92: <input type="hidden" name="submitted" value="edit" />
1.36 ! albertel 93: <input type="hidden" name="problemmode" value="Edit" />
! 94: <input type="submit" name="problemmode" value="View" />
! 95: <input type="submit" name="Undo" value="undo" /> <hr />
1.35 albertel 96: ';
1.36 ! albertel 97: my $temp=&Apache::edit::insertlist($token,$target);
! 98: &Apache::lonxml::debug("edit gave me $temp");
! 99: $result.=$temp;
! 100: return $result;
1.34 albertel 101: }
1.19 albertel 102: return '';
1.9 albertel 103: }
104:
105: sub end_problem {
1.16 albertel 106: my ($target,$token,$parstack,$parser,$safeeval)=@_;
107: my $result='';
1.24 albertel 108: my $status=$Apache::inputtags::status['-1'];
1.34 albertel 109: if ($target eq 'grade' || $target eq'web' ) {
1.28 albertel 110: if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
111: $status eq 'CAN_ANSWER') {
1.19 albertel 112: # if part is zero, no <part>s existed, so we need to the grading
113: &Apache::inputtags::grade;
114: } elsif ($Apache::inputtags::part eq '0') {
115: # if part is zero, no <part>s existed, so we need show the current
116: # grading status
1.20 albertel 117: $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.19 albertel 118: }
1.22 albertel 119: if ($target eq 'web') {
120: if ($status eq 'CAN_ANSWER') {
121: $result.="</form></body>\n";
1.28 albertel 122: } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.22 albertel 123: $result.="</body>\n";
124: }
125: }
1.34 albertel 126: }
127: if ($target eq 'meta') {
1.18 albertel 128: if ($Apache::inputtags::part eq '0') {
1.34 albertel 129:
1.16 albertel 130: $result=&Apache::response::mandatory_part_meta;
131: }
132: }
1.34 albertel 133: if ($target eq 'edit') {
134: &Apache::lonxml::debug("in end_problem with $target, edit");
135: $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
136: }
1.16 albertel 137: return $result;
1.1 albertel 138: }
139:
140: sub start_block {
141: my ($target,$token,$parstack,$parser,$safeeval)=@_;
142:
143: my $code = @$parstack[$#$parstack];
144: $code =~ s/\"//g;
145: $code .=';return $condition;';
1.32 albertel 146: # print "<br />$code<br />";
1.1 albertel 147: my $result = &Apache::run::run($code,$safeeval);
1.24 albertel 148: &Apache::lonxml::debug("block :$code: returned :$result:");
1.1 albertel 149: if ( ! $result ) {
1.9 albertel 150: my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
1.24 albertel 151: &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
1.1 albertel 152: }
153: return "";
154: }
155:
156: sub end_block {
1.4 tsai 157: }
158:
159: sub start_while {
160: my ($target,$token,$parstack,$parser,$safeeval)=@_;
161:
162: my $code = @$parstack[$#$parstack];
163: $code =~ s/\"//g;
164: $code .=';return $condition;';
165:
1.5 tsai 166: push( @Apache::structuretags::whileconds, $code);
1.4 tsai 167: my $result = &Apache::run::run($code,$safeeval);
168: my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5 tsai 169: push( @Apache::structuretags::whilebody, $bodytext);
170: if ( $result ) {
1.8 albertel 171: &Apache::lonxml::newparser($parser,\$bodytext);
1.4 tsai 172: }
173: return "";
174: }
175:
176: sub end_while {
1.5 tsai 177: my ($target,$token,$parstack,$parser,$safeeval)=@_;
178: my $code = pop @Apache::structuretags::whileconds;
179: my $bodytext = pop @Apache::structuretags::whilebody;
180: my $result = &Apache::run::run($code,$safeeval);
181: if ( $result ) {
1.8 albertel 182: &Apache::lonxml::newparser($parser,\$bodytext);
1.5 tsai 183: }
184: return "";
1.1 albertel 185: }
1.6 tsai 186:
187: # <randomlist>
188: # <tag1>..</tag1>
189: # <tag2>..</tag2>
190: # <tag3>..</tag3>
191: # ...
192: # </randomlist>
193: sub start_randomlist {
194: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.9 albertel 195: my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6 tsai 196: my $b_parser= HTML::TokeParser->new(\$body);
197: my $b_tok;
198: my @randomlist;
199: my $list_item;
200:
201: while($b_tok = $b_parser->get_token() ) {
202: if($b_tok->[0] eq 'S') { # start tag
203: # get content of the tag until matching end tag
204: # get all text upto the matching tag
205: # and push the content into @randomlist
1.9 albertel 206: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7 tsai 207: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6 tsai 208: push(@randomlist,$list_item);
1.32 albertel 209: # print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
1.6 tsai 210: }
211: if($b_tok->[0] eq 'T') { # text
212: # what to do with text in between tags?
1.32 albertel 213: # print "<b>TEXT $b_tok->[1]</b><br />";
1.6 tsai 214: }
215: # if($b_tok->[0] eq 'E') { # end tag, should not happen
1.32 albertel 216: # print "<b>END-TAG $b_tok->[1]</b><br />";
1.6 tsai 217: # }
218: }
1.7 tsai 219: my @idx_arr = (0 .. $#randomlist);
220: &Apache::structuretags::shuffle(\@idx_arr);
221: my $bodytext = '';
222: for(0 .. $#randomlist) {
223: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
224: }
1.8 albertel 225:
226: &Apache::lonxml::newparser($parser,\$bodytext);
1.6 tsai 227: return "";
1.7 tsai 228: }
229:
230: sub shuffle {
231: my $a=shift;
232: my $i;
233: for($i=@$a;--$i;) {
234: my $j=int rand($i+1);
235: next if $i == $j;
236: @$a[$i,$j] = @$a[$j,$i];
237: }
1.6 tsai 238: }
239:
240: sub end_randomlist {
241: }
242:
1.11 albertel 243: sub start_part {
244: my ($target,$token,$parstack,$parser,$safeeval)=@_;
245: my $args ='';
246: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
247: my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
1.14 albertel 248: $Apache::inputtags::part=$id;
1.18 albertel 249: @Apache::inputtags::responselist = ();
1.15 www 250: if ($target eq 'meta') {
1.16 albertel 251: return &Apache::response::mandatory_part_meta;
1.21 albertel 252: } else {
1.23 albertel 253: my ($status,$datemsg) = &Apache::lonhomework::check_date("OPEN_DATE",$id);
254: push (@Apache::inputtags::status,$status);
255: my $expression='$external::datestatus="'.$status.'";';
256: $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
257: &Apache::run::run($expression,$safeeval);
258: if ( $status eq 'CLOSED' ) {
1.21 albertel 259: my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
260: if ( $target eq "web" ) {
1.32 albertel 261: return "<br />Part is not open to be viewed. It $datemsg<br />";
1.21 albertel 262: }
263: }
1.15 www 264: }
1.19 albertel 265: return '';
1.11 albertel 266: }
267:
268: sub end_part {
269: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.18 albertel 270: &Apache::lonxml::debug("in end_part $target ");
1.28 albertel 271: my $status=$Apache::inputtags::status['-1'];
1.23 albertel 272: pop @Apache::inputtags::status;
1.19 albertel 273: if ( $target eq 'meta' ) { return ''; }
1.28 albertel 274: if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
275: return &Apache::inputtags::grade;
276: }
1.20 albertel 277: return &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.11 albertel 278: }
1.1 albertel 279:
1.25 albertel 280: sub start_preduedate {
1.24 albertel 281: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.34 albertel 282: if ($target eq 'web' || $target eq 'grade') {
1.29 albertel 283: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
284: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
1.26 albertel 285: &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
1.24 albertel 286: }
287: }
288: return '';
289: }
290:
1.25 albertel 291: sub end_preduedate {
1.24 albertel 292: return '';
293: }
294:
1.25 albertel 295: sub start_postanswerdate {
1.24 albertel 296: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.34 albertel 297: if ($target eq 'web' || $target eq 'grade') {
1.24 albertel 298: if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1.26 albertel 299: &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
1.24 albertel 300: }
301: }
302: return '';
303: }
304:
1.25 albertel 305: sub end_postanswerdate {
1.24 albertel 306: return '';
307: }
308:
1.25 albertel 309: sub start_notsolved {
1.24 albertel 310: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.34 albertel 311: if ($target eq 'web' || $target eq 'grade') {
1.24 albertel 312: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
313: &Apache::lonxml::debug("not solved has :$gradestatus:");
314: if ($gradestatus =~ /^correct/) {
315: &Apache::lonxml::debug("skipping");
1.26 albertel 316: &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
1.24 albertel 317: }
318: }
319: return '';
320: }
321:
1.25 albertel 322: sub end_notsolved {
1.24 albertel 323: return '';
324: }
325:
326: sub start_solved {
327: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.34 albertel 328: if ($target eq 'web' || $target eq 'grade') {
1.24 albertel 329: my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
330: if ($gradestatus !~ /^correct/) {
331: &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
332: }
333: }
334: return '';
335: }
336:
337: sub end_solved {
338: return '';
339: }
1.34 albertel 340:
341: sub start_startouttext {
342: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.35 albertel 343: my @result=(''.'');
344: if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
345: return (@result);
1.34 albertel 346: }
347: sub end_startouttext {
348: my ($target,$token,$parstack,$parser,$safeeval)=@_;
349: my $result='';
1.35 albertel 350: my $text='';
351:
1.34 albertel 352: if ($target eq 'edit') {
1.35 albertel 353: $text=&Apache::lonxml::get_all_text("endouttext",$$parser[$#$parser]);
354: $result=
355: &Apache::edit::tag_start("outtext").
356: &Apache::edit::editfield($token->[1],$text,"Text Block");
357: }
358: if ($target eq 'modified') {
359: $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
360: $result='<startouttext />'.&Apache::edit::modifiedfield();
1.34 albertel 361: }
362: return $result;
363: }
364: sub start_endouttext {
365: my ($target,$token,$parstack,$parser,$safeeval)=@_;
366: my $result='';
1.35 albertel 367: if ($target eq "edit" ) { $result=&Apache::edit::tag_end("outtext"); }
368: if ($target eq "modified") { $result='<endouttext />'; }
1.34 albertel 369: return $result;
370: }
371: sub end_endouttext {
372: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.35 albertel 373: my @result=('','');
374: if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
375: return (@result);
1.34 albertel 376: }
377:
378:
1.1 albertel 379: 1;
380: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>