File:  [LON-CAPA] / loncom / homework / randomlabel.pm
Revision 1.3: download - view: text, annotated - select for diffs
Thu Aug 30 19:39:12 2001 UTC (22 years, 7 months ago) by tsai
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

# The LearningOnline Network with CAPA
# random labelling tool
# 7/20/2001 Isaac Tsai, initial syntax
# 8/10/2001 Isaac Tsai, 
# 8/30/2001 Isaac Tsai, 
# SYNTAX:
# <randomlabel bgimg=URL code=JAVACLASS codebase=URL width=12 height=45>
#    <labelgroup name=GroupOne type=image>
#      <location x=123 y=456 />
#      <location x=321 y=654 />
#      <location x=213 y=546 />
#      <label>IMG-URL</label>
#      <label>IMG-URL</label>
#      <label>IMG-URL</label>
#    </labelgroup>
#    <labelgroup name=GroupTwo type=text>
#      <location x=12 y=45 />
#      <location x=32 y=65 />
#      <location x=21 y=54 />
#      <label>TEXT-1</label>
#      <label>TEXT-2</label>
#      <label>TEXT-3</label>
#    </labelgroup>
#   </randomlabel>
#  ===========================================
#  side effect:
#    location (123,456): $GroupOne[0] = 2  # images give out indexes
#             (321,654): $GroupOne[1] = 1
#             (213,546): $GroupOne[2] = 0
#    location (12,45)  : $GroupTwo[0] = "TEXT-3"
#             (32,65)  : $GroupTwo[1] = "TEXT-1"
#             (21,54)  : $GroupTwo[2] = "TEXT-2"
#  ===========================================
package Apache::randomlabel;
use strict;

sub BEGIN {
  &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label'));
}

sub start_randomlabel {
  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  my $result='';
  my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
  my $code = &Apache::lonxml::get_param('code',$parstack,$safeeval);
  my $codebase = &Apache::lonxml::get_param('codebase',$parstack,$safeeval);
  my $w= &Apache::lonxml::get_param('width',$parstack,$safeeval);
  my $h= &Apache::lonxml::get_param('height',$parstack,$safeeval);
  
  $Apache::randomlabel::tlabel_cnt=0;
  $Apache::randomlabel::ilabel_cnt=0;
  if ($target eq 'web') {
    $result.="<applet code=$code codebase=$codebase width=\"$w\" height=\"$h\">";
    $result.="<param name=\"bgimg\" value=\"$bgimg\">";
  } elsif ($target eq 'edit') {
  } elsif ($target eq 'modified') {
  } else {
  }
  return $result;
}

sub end_randomlabel {
  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  my $result='';
  my $count;

  $count = $Apache::randomlabel::tlabel_cnt;
  if( $count != 0) {
    $result.= "<param name=\"count\" value=\"$count\">";
  }
  $count = $Apache::randomlabel::ilabel_cnt;
  if( $count != 0) {
    $result.= "<param name=\"icount\" value=\"$count\">";
  }
  if ($target eq 'web') {
    $result .= "</applet><BR />";
  }
  return $result;
}

sub start_labelgroup {
  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  my $result='';
  my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
  $type =~tr/A-Z/a-z/;
  $Apache::randomlabel::groupname=$name;
  $Apache::randomlabel::type=$type;
  @Apache::randomlabel::xcoord = ();
  @Apache::randomlabel::ycoord = ();
  @Apache::randomlabel::label_arr  = ();
  return $result;
}

# begin to assign labels to locations
sub end_labelgroup {
  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  my $gname = $Apache::randomlabel::groupname;
  my $count;
  my $type  = $Apache::randomlabel::type;
  my $code;
  my $out;
  my $label;
  my $x;
  my $y;
  my $text='';
  my $str;
  my $xstr;
  my $ystr;

  my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
  &Apache::structuretags::shuffle(\@idx_arr);
  for(0 .. $#Apache::randomlabel::label_arr) {
    $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
    if( $type eq 'text') {
      $code = "push(\@$gname, $label);" ;
      $str = 'LB'.$Apache::randomlabel::tlabel_cnt;
      $xstr = 'X'.$Apache::randomlabel::tlabel_cnt;
      $ystr = 'Y'.$Apache::randomlabel::tlabel_cnt;
      $Apache::randomlabel::tlabel_cnt += 1;
    } elsif ( $type eq 'image') {
      $code = "push(\@$gname, $idx_arr[$_]);" ;
      $str = 'LB'.$Apache::randomlabel::ilabel_cnt;
      $xstr = 'X'.$Apache::randomlabel::ilabel_cnt;
      $ystr = 'Y'.$Apache::randomlabel::ilabel_cnt;
      $Apache::randomlabel::ilabel_cnt += 1;
    } else {
    }
    # $x = pop @Apache::randomlabel::xcoord;
    # $y = pop @Apache::randomlabel::ycoord;
    $x = $Apache::randomlabel::xcoord[$_];
    $y = $Apache::randomlabel::ycoord[$_];
    $text .= "<param name=\"" . $str  . "\" value=\"$label\">";
    $text .= "<param name=\"" . $xstr . "\" value=\"$x\"> ";
    $text .= "<param name=\"" . $ystr . "\" value=\"$y\">";
    $out=Apache::run::run($code,$safeeval);
  }
  return $text;
}

# <location x=123 y=456 />
sub start_location {
  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  my $x= &Apache::lonxml::get_param('x',$parstack,$safeeval);
  my $y= &Apache::lonxml::get_param('y',$parstack,$safeeval);
  my $result='';
  
  push(@Apache::randomlabel::xcoord,$x);
  push(@Apache::randomlabel::ycoord,$y); 
  return $result;
}

sub end_location {
  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  my $result='';
  return $result;
}

# <label>$var_abc</label>
sub start_label {
  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  my $ltext=&Apache::lonxml::get_all_text("/label",$$parser[$#$parser]);
  my $result='';

  push(@Apache::randomlabel::label_arr,$ltext);
  return $result;
}

sub end_label {
  my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  my $result='';

  return $result;
}



1;
__END__
 

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