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