1: # The LearningOnline Network with CAPA # <script> definiton
2:
3: package Apache::structuretags;
4:
5: use strict;
6: use Apache::lonnet;
7:
8: sub BEGIN {
9: &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part'));
10: }
11:
12: sub start_web {
13: my ($target,$token,$parstack,$parser,$safeeval)=@_;
14: my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
15: if ($target eq 'web') {
16: return $bodytext;
17: }
18: return '';
19: }
20:
21: sub end_web {
22: }
23:
24: sub start_tex {
25: my ($target,$token,$parstack,$parser,$safeeval)=@_;
26: my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
27: if ($target eq 'tex') {
28: return $bodytext
29: }
30: return '';
31: }
32:
33: sub end_tex {
34: }
35:
36: sub start_problem {
37: my ($target,$token,$parstack,$parser,$safeeval)=@_;
38:
39: #intialize globals
40: $Apache::inputtags::part='0';
41: @Apache::inputtags::responselist = ();
42:
43: #adeed vars to the scripting enviroment
44: my $expression='$external::part='.$Apache::inputtags::part.';';
45: &Apache::run::run($expression,$safeeval);
46:
47: if ($target eq 'web') {
48: # create a page header and exit
49: my $args ='';
50: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
51: my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
52: return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">";
53: }
54: return '';
55: }
56:
57: sub end_problem {
58: my ($target,$token,$parstack,$parser,$safeeval)=@_;
59: my $result='';
60: unless ($target eq 'meta') {
61: if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
62: # if part is zero, no <part>s existed, so we need to the grading
63: &Apache::inputtags::grade;
64: } elsif ($Apache::inputtags::part eq '0') {
65: # if part is zero, no <part>s existed, so we need show the current
66: # grading status
67: $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
68: }
69: if ($target eq 'web') { $result.="</form></body>\n"; }
70: } else {
71: if ($Apache::inputtags::part eq '0') {
72: $result=&Apache::response::mandatory_part_meta;
73: }
74: }
75: return $result;
76: }
77:
78: sub start_block {
79: my ($target,$token,$parstack,$parser,$safeeval)=@_;
80:
81: my $code = @$parstack[$#$parstack];
82: $code =~ s/\"//g;
83: $code .=';return $condition;';
84: # print "<br>$code<br>";
85: my $result = &Apache::run::run($code,$safeeval);
86: if ( ! $result ) {
87: # my $skip=$$parser[$#$parser]->get_text("/block");
88: my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
89: # print "skipped $skip";
90: }
91: return "";
92: }
93:
94: sub end_block {
95: }
96:
97: sub start_while {
98: my ($target,$token,$parstack,$parser,$safeeval)=@_;
99:
100: my $code = @$parstack[$#$parstack];
101: $code =~ s/\"//g;
102: $code .=';return $condition;';
103:
104: push( @Apache::structuretags::whileconds, $code);
105: my $result = &Apache::run::run($code,$safeeval);
106: my $bodytext=$$parser[$#$parser]->get_text("/while");
107: push( @Apache::structuretags::whilebody, $bodytext);
108: if ( $result ) {
109: &Apache::lonxml::newparser($parser,\$bodytext);
110: }
111: return "";
112: }
113:
114: sub end_while {
115: my ($target,$token,$parstack,$parser,$safeeval)=@_;
116: my $code = pop @Apache::structuretags::whileconds;
117: my $bodytext = pop @Apache::structuretags::whilebody;
118: my $result = &Apache::run::run($code,$safeeval);
119: if ( $result ) {
120: &Apache::lonxml::newparser($parser,\$bodytext);
121: }
122: return "";
123: }
124:
125: # <randomlist>
126: # <tag1>..</tag1>
127: # <tag2>..</tag2>
128: # <tag3>..</tag3>
129: # ...
130: # </randomlist>
131: sub start_randomlist {
132: my ($target,$token,$parstack,$parser,$safeeval)=@_;
133: my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
134: my $b_parser= HTML::TokeParser->new(\$body);
135: my $b_tok;
136: my @randomlist;
137: my $list_item;
138:
139: while($b_tok = $b_parser->get_token() ) {
140: if($b_tok->[0] eq 'S') { # start tag
141: # get content of the tag until matching end tag
142: # get all text upto the matching tag
143: # and push the content into @randomlist
144: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
145: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
146: push(@randomlist,$list_item);
147: # print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
148: }
149: if($b_tok->[0] eq 'T') { # text
150: # what to do with text in between tags?
151: # print "<B>TEXT $b_tok->[1]</B><BR>";
152: }
153: # if($b_tok->[0] eq 'E') { # end tag, should not happen
154: # print "<B>END-TAG $b_tok->[1]</B><BR>";
155: # }
156: }
157: my @idx_arr = (0 .. $#randomlist);
158: &Apache::structuretags::shuffle(\@idx_arr);
159: my $bodytext = '';
160: for(0 .. $#randomlist) {
161: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
162: }
163:
164: &Apache::lonxml::newparser($parser,\$bodytext);
165: return "";
166: }
167:
168: sub shuffle {
169: my $a=shift;
170: my $i;
171: for($i=@$a;--$i;) {
172: my $j=int rand($i+1);
173: next if $i == $j;
174: @$a[$i,$j] = @$a[$j,$i];
175: }
176: }
177:
178: sub end_randomlist {
179: }
180:
181: sub start_part {
182: my ($target,$token,$parstack,$parser,$safeeval)=@_;
183: my $args ='';
184: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
185: my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
186: $Apache::inputtags::part=$id;
187: @Apache::inputtags::responselist = ();
188: if ($target eq 'meta') {
189: return &Apache::response::mandatory_part_meta;
190: }
191: return '';
192: }
193:
194: sub end_part {
195: my ($target,$token,$parstack,$parser,$safeeval)=@_;
196: &Apache::lonxml::debug("in end_part $target ");
197: if ( $target eq 'meta' ) { return ''; }
198: if ( $target eq 'grade' ) { return &Apaceh::inputtags::grade; }
199: return &Apache::inputtags::gradestatus($Apache::inputtags::part);
200: }
201:
202: 1;
203: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>