Annotation of loncom/homework/randomlabel.pm, revision 1.3

1.1       tsai        1: # The LearningOnline Network with CAPA
                      2: # random labelling tool
1.3     ! tsai        3: # 7/20/2001 Isaac Tsai, initial syntax
        !             4: # 8/10/2001 Isaac Tsai, 
        !             5: # 8/30/2001 Isaac Tsai, 
1.1       tsai        6: # SYNTAX:
                      7: # <randomlabel bgimg=URL code=JAVACLASS codebase=URL width=12 height=45>
1.2       tsai        8: #    <labelgroup name=GroupOne type=image>
1.1       tsai        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 />
1.3     ! tsai       20: #      <label>TEXT-1</label>
        !            21: #      <label>TEXT-2</label>
        !            22: #      <label>TEXT-3</label>
1.1       tsai       23: #    </labelgroup>
                     24: #   </randomlabel>
                     25: #  ===========================================
1.3     ! tsai       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"
1.1       tsai       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:   
1.3     ! tsai       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 {
1.1       tsai       58:   }
                     59:   return $result;
                     60: }
                     61: 
                     62: sub end_randomlabel {
1.3     ! tsai       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;
1.1       tsai       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);
1.2       tsai       86:   $type =~tr/A-Z/a-z/;
1.1       tsai       87:   $Apache::randomlabel::groupname=$name;
1.2       tsai       88:   $Apache::randomlabel::type=$type;
1.1       tsai       89:   @Apache::randomlabel::xcoord = ();
                     90:   @Apache::randomlabel::ycoord = ();
                     91:   @Apache::randomlabel::label_arr  = ();
1.3     ! tsai       92:   return $result;
1.1       tsai       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;
1.3     ! tsai       99:   my $count;
1.2       tsai      100:   my $type  = $Apache::randomlabel::type;
1.1       tsai      101:   my $code;
                    102:   my $out;
                    103:   my $label;
                    104:   my $x;
                    105:   my $y;
1.3     ! tsai      106:   my $text='';
1.2       tsai      107:   my $str;
                    108:   my $xstr;
                    109:   my $ystr;
                    110: 
1.1       tsai      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[$_] ]";
1.3     ! tsai      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\">";
1.1       tsai      136:     $out=Apache::run::run($code,$safeeval);
                    137:   }
                    138:   return $text;
                    139: }
                    140: 
1.2       tsai      141: # <location x=123 y=456 />
1.1       tsai      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);
1.3     ! tsai      146:   my $result='';
1.1       tsai      147:   
                    148:   push(@Apache::randomlabel::xcoord,$x);
                    149:   push(@Apache::randomlabel::ycoord,$y); 
1.3     ! tsai      150:   return $result;
1.1       tsai      151: }
                    152: 
                    153: sub end_location {
1.3     ! tsai      154:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
        !           155:   my $result='';
        !           156:   return $result;
1.1       tsai      157: }
                    158: 
1.2       tsai      159: # <label>$var_abc</label>
1.1       tsai      160: sub start_label {
                    161:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    162:   my $ltext=&Apache::lonxml::get_all_text("/label",$$parser[$#$parser]);
1.3     ! tsai      163:   my $result='';
1.1       tsai      164: 
                    165:   push(@Apache::randomlabel::label_arr,$ltext);
1.3     ! tsai      166:   return $result;
1.1       tsai      167: }
                    168: 
                    169: sub end_label {
1.3     ! tsai      170:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
        !           171:   my $result='';
        !           172: 
        !           173:   return $result;
1.1       tsai      174: }
                    175: 
                    176: 
                    177: 
                    178: 1;
                    179: __END__
                    180:  

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>