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