Annotation of loncom/homework/structuretags.pm, revision 1.6
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: my $param;
! 75:
! 76: while($b_tok = $b_parser->get_token() ) {
! 77: if($b_tok->[0] eq 'S') { # start tag
! 78: # get content of the tag until matching end tag
! 79: # get all text upto the matching tag
! 80: # and push the content into @randomlist
! 81: $list_item = &Apache::lonxml::get_all_text($b_tok->[1],$b_parser);
! 82: $param='';
! 83: map {$param .= "$_=\"$b_tok->[2]->{$_}\" "} @{$b_tok->[3]};
! 84: $param=~ s/^\s*(\S+)\s*$/$1/;
! 85: $param= " $param" if $param ne "";
! 86: $list_item = "<$b_tok->[1]"."$param>"."$list_item"."</$b_tok->[1]>";
! 87: print "<B>START-TAG $b_tok->[1], $list_item</B><BR>";
! 88: push(@randomlist,$list_item);
! 89: }
! 90: if($b_tok->[0] eq 'T') { # text
! 91: # what to do with text in between tags?
! 92: # print "<B>TEXT $b_tok->[1]</B><BR>";
! 93: }
! 94: # if($b_tok->[0] eq 'E') { # end tag, should not happen
! 95: # print "<B>END-TAG $b_tok->[1]</B><BR>";
! 96: # }
! 97: }
! 98: # randomly select one item from the array @randomlist
! 99: # and created a new parser for it and push it onto the
! 100: # parser stack
! 101: my $bodytext = $randomlist[int(rand($#randomlist - 1)) + 1];
! 102: my $tempparser=HTML::TokeParser->new(\$bodytext);
! 103: print "SELECT-BODY<B>$bodytext</B><BR>";
! 104: push (@$parser,$tempparser);
! 105: return "";
! 106: }
! 107:
! 108: sub end_randomlist {
! 109: }
! 110:
1.1 albertel 111:
112: 1;
113: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>