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