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