1: # The LearningOnline Network with CAPA
2: # random labelling tool
3: # 7/20/2001 Isaac Tsai, initial syntax
4: # 8/10/2001 Isaac Tsai,
5: # 8/30/2001 Isaac Tsai,
6: # SYNTAX:
7: # <randomlabel bgimg=URL code=JAVACLASS codebase=URL width=12 height=45>
8: # <labelgroup name=GroupOne type=image>
9: # <location x=123 y=456 />
10: # <location x=321 y=654 />
11: # <location x=213 y=546 />
12: # <label>IMG-URL</label>
13: # <label>IMG-URL</label>
14: # <label>IMG-URL</label>
15: # </labelgroup>
16: # <labelgroup name=GroupTwo type=text>
17: # <location x=12 y=45 />
18: # <location x=32 y=65 />
19: # <location x=21 y=54 />
20: # <label>TEXT-1</label>
21: # <label>TEXT-2</label>
22: # <label>TEXT-3</label>
23: # </labelgroup>
24: # </randomlabel>
25: # ===========================================
26: # side effect:
27: # location (123,456): $GroupOne[0] = 2 # images give out indexes
28: # (321,654): $GroupOne[1] = 1
29: # (213,546): $GroupOne[2] = 0
30: # location (12,45) : $GroupTwo[0] = "TEXT-3"
31: # (32,65) : $GroupTwo[1] = "TEXT-1"
32: # (21,54) : $GroupTwo[2] = "TEXT-2"
33: # ===========================================
34: package Apache::randomlabel;
35: use strict;
36:
37: sub BEGIN {
38: &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label'));
39: }
40:
41: sub start_randomlabel {
42: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
43: my $result='';
44: my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
45: my $code = &Apache::lonxml::get_param('code',$parstack,$safeeval);
46: my $codebase = &Apache::lonxml::get_param('codebase',$parstack,$safeeval);
47: my $w= &Apache::lonxml::get_param('width',$parstack,$safeeval);
48: my $h= &Apache::lonxml::get_param('height',$parstack,$safeeval);
49:
50: $Apache::randomlabel::tlabel_cnt=0;
51: $Apache::randomlabel::ilabel_cnt=0;
52: if ($target eq 'web') {
53: $result.="<applet code=$code codebase=$codebase width=\"$w\" height=\"$h\">";
54: $result.="<param name=\"bgimg\" value=\"$bgimg\">";
55: } elsif ($target eq 'edit') {
56: } elsif ($target eq 'modified') {
57: } else {
58: }
59: return $result;
60: }
61:
62: sub end_randomlabel {
63: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
64: my $result='';
65: my $count;
66:
67: $count = $Apache::randomlabel::tlabel_cnt;
68: if( $count != 0) {
69: $result.= "<param name=\"count\" value=\"$count\">";
70: }
71: $count = $Apache::randomlabel::ilabel_cnt;
72: if( $count != 0) {
73: $result.= "<param name=\"icount\" value=\"$count\">";
74: }
75: if ($target eq 'web') {
76: $result .= "</applet><BR />";
77: }
78: return $result;
79: }
80:
81: sub start_labelgroup {
82: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
83: my $result='';
84: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
85: my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
86: $type =~tr/A-Z/a-z/;
87: $Apache::randomlabel::groupname=$name;
88: $Apache::randomlabel::type=$type;
89: @Apache::randomlabel::xcoord = ();
90: @Apache::randomlabel::ycoord = ();
91: @Apache::randomlabel::label_arr = ();
92: return $result;
93: }
94:
95: # begin to assign labels to locations
96: sub end_labelgroup {
97: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
98: my $gname = $Apache::randomlabel::groupname;
99: my $count;
100: my $type = $Apache::randomlabel::type;
101: my $code;
102: my $out;
103: my $label;
104: my $x;
105: my $y;
106: my $text='';
107: my $str;
108: my $xstr;
109: my $ystr;
110:
111: my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
112: &Apache::structuretags::shuffle(\@idx_arr);
113: for(0 .. $#Apache::randomlabel::label_arr) {
114: $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
115: if( $type eq 'text') {
116: $code = "push(\@$gname, $label);" ;
117: $str = 'LB'.$Apache::randomlabel::tlabel_cnt;
118: $xstr = 'X'.$Apache::randomlabel::tlabel_cnt;
119: $ystr = 'Y'.$Apache::randomlabel::tlabel_cnt;
120: $Apache::randomlabel::tlabel_cnt += 1;
121: } elsif ( $type eq 'image') {
122: $code = "push(\@$gname, $idx_arr[$_]);" ;
123: $str = 'LB'.$Apache::randomlabel::ilabel_cnt;
124: $xstr = 'X'.$Apache::randomlabel::ilabel_cnt;
125: $ystr = 'Y'.$Apache::randomlabel::ilabel_cnt;
126: $Apache::randomlabel::ilabel_cnt += 1;
127: } else {
128: }
129: # $x = pop @Apache::randomlabel::xcoord;
130: # $y = pop @Apache::randomlabel::ycoord;
131: $x = $Apache::randomlabel::xcoord[$_];
132: $y = $Apache::randomlabel::ycoord[$_];
133: $text .= "<param name=\"" . $str . "\" value=\"$label\">";
134: $text .= "<param name=\"" . $xstr . "\" value=\"$x\"> ";
135: $text .= "<param name=\"" . $ystr . "\" value=\"$y\">";
136: $out=Apache::run::run($code,$safeeval);
137: }
138: return $text;
139: }
140:
141: # <location x=123 y=456 />
142: sub start_location {
143: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
144: my $x= &Apache::lonxml::get_param('x',$parstack,$safeeval);
145: my $y= &Apache::lonxml::get_param('y',$parstack,$safeeval);
146: my $result='';
147:
148: push(@Apache::randomlabel::xcoord,$x);
149: push(@Apache::randomlabel::ycoord,$y);
150: return $result;
151: }
152:
153: sub end_location {
154: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
155: my $result='';
156: return $result;
157: }
158:
159: # <label>$var_abc</label>
160: sub start_label {
161: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
162: my $ltext=&Apache::lonxml::get_all_text("/label",$$parser[$#$parser]);
163: my $result='';
164:
165: push(@Apache::randomlabel::label_arr,$ltext);
166: return $result;
167: }
168:
169: sub end_label {
170: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
171: my $result='';
172:
173: return $result;
174: }
175:
176:
177:
178: 1;
179: __END__
180:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>