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