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