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

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

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