Annotation of loncom/homework/structuretags.pm, revision 1.7
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.6 tsai 9: &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist'));
1.1 albertel 10: }
11:
12: sub start_block {
13: my ($target,$token,$parstack,$parser,$safeeval)=@_;
14:
15: my $code = @$parstack[$#$parstack];
16: $code =~ s/\"//g;
17: $code .=';return $condition;';
1.3 albertel 18: # print "<br>$code<br>";
1.1 albertel 19: my $result = &Apache::run::run($code,$safeeval);
20: if ( ! $result ) {
1.3 albertel 21: # my $skip=$$parser[$#$parser]->get_text("/block");
22: my $skip=&Apache::lonxml::get_all_text("block",$$parser[$#$parser]);
23: # print "skipped $skip";
1.1 albertel 24: }
25: return "";
26: }
27:
28: sub end_block {
1.4 tsai 29: }
30:
31: sub start_while {
32: my ($target,$token,$parstack,$parser,$safeeval)=@_;
33:
34: my $code = @$parstack[$#$parstack];
35: $code =~ s/\"//g;
36: $code .=';return $condition;';
37:
1.5 tsai 38: push( @Apache::structuretags::whileconds, $code);
1.4 tsai 39: my $result = &Apache::run::run($code,$safeeval);
40: my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5 tsai 41: push( @Apache::structuretags::whilebody, $bodytext);
42: if ( $result ) {
1.4 tsai 43: my $tempparser=HTML::TokeParser->new(\$bodytext);
44: push (@$parser,$tempparser);
45: }
46: return "";
47: }
48:
49: sub end_while {
1.5 tsai 50: my ($target,$token,$parstack,$parser,$safeeval)=@_;
51: my $code = pop @Apache::structuretags::whileconds;
52: my $bodytext = pop @Apache::structuretags::whilebody;
53: my $result = &Apache::run::run($code,$safeeval);
54: if ( $result ) {
55: my $tempparser=HTML::TokeParser->new(\$bodytext);
56: push (@$parser,$tempparser);
57: }
58: return "";
1.1 albertel 59: }
1.6 tsai 60:
61: # <randomlist>
62: # <tag1>..</tag1>
63: # <tag2>..</tag2>
64: # <tag3>..</tag3>
65: # ...
66: # </randomlist>
67: sub start_randomlist {
68: my ($target,$token,$parstack,$parser,$safeeval)=@_;
69: my $body= &Apache::lonxml::get_all_text("randomlist",$$parser[$#$parser]);
70: my $b_parser= HTML::TokeParser->new(\$body);
71: my $b_tok;
72: my @randomlist;
73: my $list_item;
74:
75: while($b_tok = $b_parser->get_token() ) {
76: if($b_tok->[0] eq 'S') { # start tag
77: # get content of the tag until matching end tag
78: # get all text upto the matching tag
79: # and push the content into @randomlist
80: $list_item = &Apache::lonxml::get_all_text($b_tok->[1],$b_parser);
1.7 ! tsai 81: $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6 tsai 82: push(@randomlist,$list_item);
1.7 ! tsai 83: # print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6 tsai 84: }
85: if($b_tok->[0] eq 'T') { # text
86: # what to do with text in between tags?
87: # print "<B>TEXT $b_tok->[1]</B><BR>";
88: }
89: # if($b_tok->[0] eq 'E') { # end tag, should not happen
90: # print "<B>END-TAG $b_tok->[1]</B><BR>";
91: # }
92: }
1.7 ! tsai 93: my @idx_arr = (0 .. $#randomlist);
! 94: &Apache::structuretags::shuffle(\@idx_arr);
! 95: my $bodytext = '';
! 96: for(0 .. $#randomlist) {
! 97: $bodytext .= "$randomlist[ $idx_arr[$_] ]";
! 98: }
1.6 tsai 99: my $tempparser=HTML::TokeParser->new(\$bodytext);
100: push (@$parser,$tempparser);
101: return "";
1.7 ! tsai 102: }
! 103:
! 104: sub shuffle {
! 105: my $a=shift;
! 106: my $i;
! 107: for($i=@$a;--$i;) {
! 108: my $j=int rand($i+1);
! 109: next if $i == $j;
! 110: @$a[$i,$j] = @$a[$j,$i];
! 111: }
1.6 tsai 112: }
113:
114: sub end_randomlist {
115: }
116:
1.1 albertel 117:
118: 1;
119: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>