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