Annotation of loncom/homework/structuretags.pm, revision 1.14
1.6 tsai 1: # The LearningOnline Network with CAPA # <script> definiton
1.1 albertel 2:
3: package Apache::structuretags;
4:
5: use strict;
6: use Apache::lonnet;
7:
8: sub BEGIN {
1.11 albertel 9: &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part'));
1.10 albertel 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 {
1.9 albertel 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);
1.14 ! albertel 37: $Apache::inputtags::part='0';
! 38: my $expression='$external::part='.$Apache::inputtags::part.';';
1.12 albertel 39: $safeeval->share_from('Apache::inputtags',['@part']);
1.14 ! albertel 40: &Apache::run::run($expression)
1.11 albertel 41: return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">";
1.9 albertel 42: }
43:
44: sub end_problem {
1.11 albertel 45: return "</form></body>\n";
1.1 albertel 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;';
1.3 albertel 54: # print "<br>$code<br>";
1.1 albertel 55: my $result = &Apache::run::run($code,$safeeval);
56: if ( ! $result ) {
1.3 albertel 57: # my $skip=$$parser[$#$parser]->get_text("/block");
1.9 albertel 58: my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
1.3 albertel 59: # print "skipped $skip";
1.1 albertel 60: }
61: return "";
62: }
63:
64: sub end_block {
1.4 tsai 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:
1.5 tsai 74: push( @Apache::structuretags::whileconds, $code);
1.4 tsai 75: my $result = &Apache::run::run($code,$safeeval);
76: my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5 tsai 77: push( @Apache::structuretags::whilebody, $bodytext);
78: if ( $result ) {
1.8 albertel 79: &Apache::lonxml::newparser($parser,\$bodytext);
1.4 tsai 80: }
81: return "";
82: }
83:
84: sub end_while {
1.5 tsai 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 ) {
1.8 albertel 90: &Apache::lonxml::newparser($parser,\$bodytext);
1.5 tsai 91: }
92: return "";
1.1 albertel 93: }
1.6 tsai 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)=@_;
1.9 albertel 103: my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6 tsai 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
1.9 albertel 114: $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7 tsai 115: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6 tsai 116: push(@randomlist,$list_item);
1.7 tsai 117: # print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6 tsai 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: }
1.7 tsai 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: }
1.8 albertel 133:
134: &Apache::lonxml::newparser($parser,\$bodytext);
1.6 tsai 135: return "";
1.7 tsai 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: }
1.6 tsai 146: }
147:
148: sub end_randomlist {
149: }
150:
1.11 albertel 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);
1.14 ! albertel 156: $Apache::inputtags::part=$id;
1.11 albertel 157: }
158:
159: sub end_part {
160: my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.14 ! albertel 161: $Apache::inputtags::part='0';
1.11 albertel 162: }
1.1 albertel 163:
164: 1;
165: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>