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

1.1       tsai        1: # The LearningOnline Network with CAPA
                      2: # random labelling tool
1.8       albertel    3: #
1.61    ! albertel    4: # $Id: randomlabel.pm,v 1.60 2004/03/12 21:01:14 albertel Exp $
1.8       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.3       tsai       28: # 7/20/2001 Isaac Tsai, initial syntax
                     29: # 8/10/2001 Isaac Tsai, 
                     30: # 8/30/2001 Isaac Tsai, 
1.1       tsai       31: # SYNTAX:
1.4       albertel   32: # <randomlabel bgimg="URL" width="12" height="45" texwidth="50">
                     33: #    <labelgroup name="GroupOne" type="image">
                     34: #      <location x="123" y="456" />
                     35: #      <location x="321" y="654" />
                     36: #      <location x="213" y="546" />
                     37: #      <label description="TEXT-1">IMG-URL</label>
                     38: #      <label description="TEXT-2">IMG-URL</label>
                     39: #      <label description="TEXT-3">IMG-URL</label>
1.1       tsai       40: #    </labelgroup>
1.4       albertel   41: #    <labelgroup name="GroupTwo" type="text">
                     42: #      <location x="12" y="45" />
                     43: #      <location x="32" y="65" />
                     44: #      <location x="21" y="54" />
1.3       tsai       45: #      <label>TEXT-1</label>
                     46: #      <label>TEXT-2</label>
                     47: #      <label>TEXT-3</label>
1.1       tsai       48: #    </labelgroup>
                     49: #   </randomlabel>
                     50: #  ===========================================
1.3       tsai       51: #  side effect:
                     52: #    location (123,456): $GroupOne[0] = 2  # images give out indexes
                     53: #             (321,654): $GroupOne[1] = 1
                     54: #             (213,546): $GroupOne[2] = 0
                     55: #    location (12,45)  : $GroupTwo[0] = "TEXT-3"
                     56: #             (32,65)  : $GroupTwo[1] = "TEXT-1"
                     57: #             (21,54)  : $GroupTwo[2] = "TEXT-2"
1.1       tsai       58: #  ===========================================
                     59: package Apache::randomlabel;
1.24      sakharuk   60: use Apache::lonnet;
1.1       tsai       61: use strict;
1.13      sakharuk   62: use Apache::edit;
1.36      sakharuk   63: use Apache::File();
1.38      sakharuk   64: use Apache::Constants qw(:common :http);
1.1       tsai       65: 
1.53      albertel   66: my %args;
                     67: my $cgi_id;
                     68: 
1.13      sakharuk   69: BEGIN {
1.57      albertel   70:     &Apache::lonxml::register('Apache::randomlabel',('randomlabel','labelgroup','location','label','bgimg'));
1.1       tsai       71: }
                     72: 
1.23      matthew    73: sub check_int {
                     74:     # utility function to do error checking on a integer.
                     75:     my ($num,$default) = @_;
                     76:     $default = 100 if (! defined($default));
                     77:     $num =~ s/\s+//g;  # We dont need no stinkin white space!
                     78:     # If it is a real, just grab the integer part.
                     79:     ($num,undef) = split (/\./,$num) if ($num =~ /\./); 
                     80:     # set to default if what we have left doesn't match anything...
                     81:     $num = $default unless ($num =~/^\d+$/);
                     82:     return $num;
                     83: }
                     84: 
1.1       tsai       85: sub start_randomlabel {
1.47      albertel   86:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     87:     my $result='';
                     88:     push (@Apache::lonxml::namespace,'randomlabel');
                     89:     my $bgimg= &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
1.60      albertel   90:     if ( defined($bgimg) && $bgimg !~ /^http:/ ) {
1.47      albertel   91: 	$bgimg=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$bgimg);
                     92: 	if (&Apache::lonnet::repcopy($bgimg) ne OK) {
                     93: 	    $bgimg='/home/httpd/html/adm/lonKaputt/lonlogo_broken.gif';
                     94: 	}
1.42      www        95:     }
1.59      albertel   96:     $Apache::randomlabel::obj_cnt=0;
1.47      albertel   97:     if ($target eq 'web') {
1.53      albertel   98: 	$cgi_id=&Apache::loncommon::get_cgi_id();
                     99: 	%args=();
                    100: 	$args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
1.47      albertel  101:     } elsif ($target eq 'tex') {
1.57      albertel  102: 	my $w= &check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval));
                    103: 	my $h= &check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval));
                    104: 	my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
                    105: 	if (!$texwidth) { $texwidth=90; }
                    106: 	$result.=&make_eps_image($bgimg,$texwidth,$h,$w);
1.47      albertel  107:     } elsif ($target eq 'edit') {
                    108: 	$result.=&Apache::edit::tag_start($target,$token);
                    109: 	$Apache::edit::bgimgsrc=
                    110: 	    &Apache::lonxml::get_param('bgimg',$parstack,$safeeval);
                    111: 	$Apache::edit::bgimgsrccurdepth=$Apache::lonxml::curdepth;
                    112: 	$result.=&Apache::edit::text_arg('Image:','bgimg',$token,75).' ';
                    113: 	$result.=&Apache::edit::browse('bgimg').' ';
                    114: 	$result.=&Apache::edit::search('bgimg').'<br />'.
                    115: 	    &Apache::edit::text_arg('Width(pixel):' ,'width'   ,$token,6).
                    116: 	    &Apache::edit::text_arg('Height(pixel):','height'  ,$token,6).
                    117: 	    &Apache::edit::text_arg('TeXWidth(mm):' ,'texwidth',$token,6).
                    118: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();     
                    119:     } elsif ($target eq 'modified') {
                    120: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    121: 						     $safeeval,'bgimg','width',
                    122: 						     'height','texwidth');
                    123: 	if ($constructtag) {
                    124: 	    $result = &Apache::edit::rebuild_tag($token);
                    125: 	    $result.=&Apache::edit::handle_insert();
                    126: 	}
1.31      sakharuk  127:     }
1.47      albertel  128:     return $result;
1.1       tsai      129: }
                    130: 
                    131: sub end_randomlabel {
1.47      albertel  132:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    133:     my $result='';
                    134:     my $count;
                    135:     pop @Apache::lonxml::namespace;
                    136:     if ($target eq 'web') {
1.59      albertel  137: 	$count = $Apache::randomlabel::obj_cnt;
                    138: 	if( $count != 0) { $args{"cgi.$cgi_id.OBJCOUNT"}=$count; }
1.53      albertel  139: 	$result.='<img src="/adm/randomlabel.png?token='.$cgi_id.'" /><br />'."\n";
                    140: 	&Apache::lonnet::appenv(%args);
1.47      albertel  141:     } elsif ($target eq 'tex') {
                    142: 	$result='\end{picture}\\\\';
                    143: 	my $height=&Apache::lonxml::get_param('height',$parstack,$safeeval);
                    144: 	my $width=&Apache::lonxml::get_param('width',$parstack,$safeeval);
1.50      sakharuk  145: 	my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,1));
1.49      albertel  146:         if (!$texwidth) { $texwidth=90; }
1.47      albertel  147: 	my $howtoskipback = $texwidth*$height/$width;
                    148: 	$result.= ' \vskip -'.$howtoskipback.' mm }  \\\\ ';
                    149:     } elsif ($target eq 'edit') {
                    150: 	$result.=&Apache::edit::end_table;
                    151:     }
                    152:     return $result;
1.1       tsai      153: }
                    154: 
1.57      albertel  155: sub start_bgimg {
                    156:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    157:     my $result='';
                    158:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
                    159: 	&Apache::lonxml::startredirection; 
                    160:     }
                    161:     return $result;
                    162: }
                    163: 
                    164: sub end_bgimg {
                    165:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    166:     my $result='';
                    167:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
                    168: 	my $bgimg=&Apache::lonxml::endredirection; 
                    169: 	if ($target eq 'web') {
                    170: 	    $bgimg=&Apache::imageresponse::clean_up_image($bgimg);
                    171: 	    $args{"cgi.$cgi_id.BGIMG"}=&Apache::lonnet::escape($bgimg);
                    172: 	} elsif ($target eq 'tex') {
                    173: 	    my $w=&check_int(&Apache::lonxml::get_param('width',$parstack,$safeeval,-2));
                    174: 	    my $h=&check_int(&Apache::lonxml::get_param('height',$parstack,$safeeval,-2));
                    175: 	    my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,undef,-2));
                    176: 	    if (!$texwidth) { $texwidth=90; }
                    177: 	    $result.=&make_eps_image($bgimg,$texwidth,$h,$w);
                    178: 	}
                    179:     }
                    180:     return $result;
                    181: }
                    182: 
                    183: sub make_eps_image {
                    184:     my ($bgimg,$texwidth,$h,$w)=@_;
                    185:     my $newbgimg = $bgimg;
                    186:     $bgimg=~s/\.(gif|jpg|png|jpeg)$/\.eps/i;
                    187:     $bgimg= &Apache::lonnet::filelocation($bgimg);
                    188:     $bgimg=~s|http:/[^/]*|/home/httpd/html|;
                    189:     $bgimg=~s|/$||;
                    190:     #if no eps file try to replicate it
                    191:     if (not-e $bgimg) {
                    192: 	if (&Apache::lonnet::repcopy($bgimg) ne OK ) {
                    193: 	    #if replication failed try to find ps file
                    194: 	    $bgimg=~s/\.eps$/\.ps/;
                    195: 	    #if no ps file try to replicate it
                    196: 	    if (not -e $bgimg &&
                    197: 		&Apache::lonnet::repcopy($bgimg) ne OK) {
                    198: 		#if replication failed try to produce eps file dynamically
                    199: 		$bgimg=~s/\.ps$/\.eps/;
                    200: 		my $temp_file;
                    201: 		my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.dat";
                    202: 		$temp_file = Apache::File->new('>>'.$filename);
                    203: 		$newbgimg =~ s|(.*)/res/|/home/httpd/html/res/|;
                    204: 		print $temp_file "$newbgimg\n";
                    205: 		$bgimg=~s|/home/httpd/html/res|/home/httpd/prtspool|;
                    206: 		$bgimg=~s|/home/([^/]*)/public_html/|/home/httpd/prtspool/$1/|;
                    207: 	    }
                    208: 	}
                    209:     }
                    210:     $bgimg=~s|/$||;
                    211:     my $dirtywidth=$texwidth+5;
                    212:     my $result.='\vspace*{2mm}\noindent \parbox{'.$dirtywidth.
                    213: 	' mm}{  \noindent \epsfxsize='.$texwidth.' mm \epsffile{'.$bgimg.
                    214: 	'}\setlength{\unitlength}{1mm}  \begin{picture}('.
                    215: 	$texwidth.','.$texwidth*$h/$w.')(0,-'.$texwidth*$h/$w.')';
                    216:     return $result;
                    217: }
                    218: 
1.50      sakharuk  219: sub adjust_textwidth {
                    220:     my $texwidth=shift; 
                    221:     my $pagewidth=$ENV{'form.textwidth'};
                    222:     $pagewidth=~s/\s*mm\s*$//;
                    223:     if ($texwidth>$pagewidth) {$texwidth=$pagewidth;}
                    224:     return $texwidth;
                    225: }
                    226: 
1.1       tsai      227: sub start_labelgroup {
1.47      albertel  228:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    229:     my $result='';
                    230:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    231:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
                    232:     $type =~tr/A-Z/a-z/;
1.48      albertel  233:     if ($target ne 'modified' && ($name =~ /\W/ || $name =~ /^[0-9]/)) {
                    234: 	&Apache::lonxml::error("Only _ a-z A-Z and 0-9 are allowed in the name to a labelgroup, and the first character can not be a number.<br />");
                    235:     }
1.47      albertel  236:     if ($target eq 'web' || $target eq 'tex' ||
                    237: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
                    238: 	$Apache::randomlabel::groupname=$name;
                    239: 	$Apache::randomlabel::type=$type;
                    240: 	@Apache::randomlabel::xcoord = ();
                    241: 	@Apache::randomlabel::ycoord = ();
                    242: 	@Apache::randomlabel::value = ();
                    243: 	@Apache::randomlabel::label_arr  = ();
                    244: 	@Apache::randomlabel::decription  = ();
                    245:     } elsif ($target eq 'edit') {
                    246: 	$result.=&Apache::edit::tag_start($target,$token);
                    247: 	$result.=&Apache::edit::text_arg('Name:','name',$token).
                    248: 	    &Apache::edit::select_arg('Type:','type',['text','image'],$token).
                    249: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    250:     } elsif ($target eq 'modified') {
                    251: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    252: 						     $safeeval,'name','type');
                    253: 	if ($constructtag) {
                    254: 	    $result = &Apache::edit::rebuild_tag($token);
                    255: 	    $result.=&Apache::edit::handle_insert();
                    256: 	}
1.4       albertel  257:     }
1.47      albertel  258:     return $result;
1.1       tsai      259: }
                    260: 
1.5       albertel  261: sub add_vars {
1.47      albertel  262:     my ($name,$order,$label,$labelorder,$value,$image,$safeeval) = @_;
1.61    ! albertel  263:     if (!defined($name) || $name eq '') { return; }
1.47      albertel  264:     my $code = '${'.$name."}{'".($order+1)."'}='".$label."';";
                    265:     my $out=Apache::run::run($code,$safeeval);
                    266:     if ($value) {
                    267: 	$code = '${'.$name."}{'value_".($order+1)."'}='".$value."';";
                    268: 	$out=Apache::run::run($code,$safeeval);
                    269: 	$code = '${'.$name."}{'labelvalue_".($labelorder+1)."'}='".$value."';";
                    270: 	$out=Apache::run::run($code,$safeeval);
                    271:     }
                    272:     if ($image) {
                    273: 	my $code = '${'.$name."}{'image_".($order+1)."'}='".$image."';";
                    274: 	my $out=Apache::run::run($code,$safeeval);
                    275:     }
                    276:     $code = '${'.$name."}{'numlocations'}='".($order+1)."';";
1.5       albertel  277:     $out=Apache::run::run($code,$safeeval);
1.4       albertel  278: }
1.5       albertel  279: 
1.1       tsai      280: # begin to assign labels to locations
                    281: sub end_labelgroup {
1.47      albertel  282:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    283:     my $gname = $Apache::randomlabel::groupname;
                    284:     my $type  = $Apache::randomlabel::type;
                    285:     my $result='';
                    286:     if ($target eq 'web' || $target eq 'answer' || $target eq 'grade' ||
                    287: 	$target eq 'analyze') {
                    288: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
                    289: 	&Apache::structuretags::shuffle(\@idx_arr);
                    290: 	for(0 .. $#Apache::randomlabel::label_arr) {
                    291: 	    my $str;
                    292: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$_] ]";
                    293: 	    my $x = $Apache::randomlabel::xcoord[$_];
                    294: 	    my $y = $Apache::randomlabel::ycoord[$_];
                    295: 	    my $value = $Apache::randomlabel::value[$_];
1.59      albertel  296: 	    my $i=$Apache::randomlabel::obj_cnt++;
1.47      albertel  297: 	    if( $type eq 'text') {
                    298: 		&add_vars($gname,$_,$label,$idx_arr[$_],$value,'',$safeeval);
1.59      albertel  299: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
                    300: 		$args{"cgi.$cgi_id.OBJTYPE"}.='LABEL:';
1.47      albertel  301: 	    } elsif ( $type eq 'image') {
                    302: 		&add_vars($gname,$_,
                    303: 			  $Apache::randomlabel::description[$idx_arr[$_]],
                    304: 			  $idx_arr[$_],$value,$label,$safeeval);
1.59      albertel  305: 		$str = join(':',$x,$y,&Apache::lonnet::escape($label));
                    306: 		$args{"cgi.$cgi_id.OBJTYPE"}.='IMAGE:';
1.47      albertel  307: 	    } else {
                    308: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
                    309: 	    }
1.59      albertel  310: 	    if ($target eq 'web') { $args{"cgi.$cgi_id.OBJ$i"} =$str; }
1.47      albertel  311: 	}
                    312:     } elsif ($target eq 'tex') {
                    313: 	my $WX1=0; #  Web x-coord. of upper left corner (ULC)
                    314: 	my $WY1=0; #  Web y-coord. of (ULC)
                    315: 	my $wwidth=&Apache::lonxml::get_param('width',$parstack,$safeeval,-2);
                    316: 	my $wheight=&Apache::lonxml::get_param('height',$parstack,$safeeval,-2);
1.50      sakharuk  317: 	my $texwidth=&adjust_textwidth(&Apache::lonxml::get_param('texwidth',$parstack,$safeeval,-2,1));
1.49      albertel  318: 	if (!$texwidth) { $texwidth=90; }
1.47      albertel  319: 	my $TX1=0;
                    320: 	my $TY1=$texwidth*($wheight/$wwidth);
                    321: 	my $TX2=$texwidth;
                    322: 	my $TY2=0;
                    323: 
                    324: 
                    325: 	my $slopex=($wwidth-$WX1)/($TX2-$TX1);
                    326: 	my $slopey=($wheight-$WY1)/($TY2-($TY1-1.0));
                    327: 	my $cstx=$wwidth-$slopex*($TX2);
                    328: 	my $csty=$wheight-$slopey*($TY2);
                    329: 
                    330: 	my @idx_arr = (0 .. $#Apache::randomlabel::label_arr);
                    331: 	&Apache::structuretags::shuffle(\@idx_arr);
                    332: 
                    333: 	&Apache::lonxml::debug("Array is:".$#Apache::randomlabel::label_arr.":");
                    334: 	for(my $i=0;$i <= $#Apache::randomlabel::label_arr; $i++) {
                    335: 	    my $label = "$Apache::randomlabel::label_arr[ $idx_arr[$i] ]";
                    336: 	    my $x = $Apache::randomlabel::xcoord[$i];
                    337: 	    my $y = $Apache::randomlabel::ycoord[$i];
                    338: 	    my $value = $Apache::randomlabel::value[$i];
                    339: 	    #x latex coordinate
                    340: 	    my $tcX=($x)*($texwidth/$wwidth);
                    341: 	    #y latex coordinate
                    342:             #      my $ratio=($wwidth > 0 ? $wheight/$wwidth : 1 );
                    343: 	    my $tcY=$TY1-$y*($TY1/$wheight);
                    344: 	    $tcX=sprintf('%.2f',$tcX);
                    345: 	    $tcY=sprintf('%.2f',$tcY);
                    346: 	    $result.='\put('.$tcX.','.$tcY.'){\normalsize \bf '.$label.'}'."\n";
                    347: 	    if( $type eq 'text') {
                    348: 		&add_vars($gname,$i,$label,$idx_arr[$i],$value,'',$safeeval);
                    349: 	    } elsif ( $type eq 'image') {
                    350: 		&add_vars($gname,$i,
                    351: 			  $Apache::randomlabel::description[$idx_arr[$i]],
                    352: 			  $idx_arr[$i],$value,$label,$safeeval);
                    353: 	    } else {
                    354: 		&Apache::lonxml::error('Unknown type of label :'.$type.':');
                    355: 	    }
                    356: 	}
                    357:     } elsif ($target eq 'edit') {
                    358: 	$result.=&Apache::edit::end_table;
1.3       tsai      359:     }
1.47      albertel  360:     return $result;
1.1       tsai      361: }
                    362: 
1.5       albertel  363: # <location x="123" y="456" value="some value"/>
1.1       tsai      364: sub start_location {
1.47      albertel  365:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    366:     my $x= &check_int(&Apache::lonxml::get_param('x',$parstack,$safeeval),50);
                    367:     my $y= &check_int(&Apache::lonxml::get_param('y',$parstack,$safeeval),50);
                    368:     my $value= &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    369:     my $result='';
                    370:     push(@Apache::randomlabel::xcoord,$x);
                    371:     push(@Apache::randomlabel::ycoord,$y);
                    372:     push(@Apache::randomlabel::value,$value);
                    373:     if ($target eq 'edit') {
                    374: 	$result.=&Apache::edit::tag_start($target,$token);
                    375: 	$result.=&Apache::edit::text_arg('X:','x',$token,4).
                    376: 	    &Apache::edit::text_arg('Y:','y',$token,4).
                    377: 	    &Apache::edit::entercoords('x','y','attribute','width','height').'&nbsp;&nbsp;&nbsp;'.
                    378: 	    &Apache::edit::text_arg('Value:','value',$token).
                    379: 	    &Apache::edit::end_row();
                    380: 	$result.=&Apache::edit::end_table;
                    381:     } elsif ($target eq 'modified') {
                    382: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    383: 						    $safeeval,'x','y','value');
                    384: 	if ($constructtag) {
                    385: 	    $result = &Apache::edit::rebuild_tag($token);
                    386: 	    $result.=&Apache::edit::handle_insert();
                    387: 	}
1.4       albertel  388:     }
1.47      albertel  389:     return $result;
1.1       tsai      390: }
                    391: 
                    392: sub end_location {
1.47      albertel  393:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    394:     my @result;
                    395:     if ($target eq 'edit') { @result=('','no') }
                    396:     return @result;
1.1       tsai      397: }
                    398: 
1.2       tsai      399: # <label>$var_abc</label>
1.1       tsai      400: sub start_label {
1.47      albertel  401:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    402:     my $result='';
                    403:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
                    404:     if ($target eq 'web' || $target eq 'tex' ||
                    405: 	$target eq 'grade' || $target eq 'answer' || $target eq 'analyze') {
1.57      albertel  406: 	&Apache::lonxml::startredirection; 
1.47      albertel  407:     } elsif ($target eq 'edit') {
                    408: 	$result.=&Apache::edit::tag_start($target,$token,"$type Label");
                    409: 	my $text=&Apache::lonxml::get_all_text("/label",$parser);
                    410: 	if ($type eq 'image') {
                    411: 	    $result.=&Apache::edit::end_row().
                    412: 		&Apache::edit::start_spanning_row();
                    413: 	    $result.=&Apache::edit::text_arg('Description:','description',
                    414: 					     $token);
                    415: 	}
                    416: 	if ($type eq 'text') { $result.="Label Text:"; }
                    417: 	if ($type eq 'image') { $result.="Path to image:&nbsp;"; }
                    418: 	$result.=&Apache::edit::editline('',$text,'',50).
                    419: 	    &Apache::edit::end_table();
                    420:     } elsif ($target eq 'modified') {
                    421: 	$result = '<label>';
                    422: 	if ($type eq 'image') {
                    423: 	    my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    424: 							 $safeeval,
                    425: 							 'description');
                    426: 	    if ($constructtag) {
                    427: 		$result = &Apache::edit::rebuild_tag($token);
                    428: 	    } else {
                    429: 		$result = $token->[4];
                    430: 	    }
                    431: 	}
1.52      albertel  432: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
1.26      albertel  433:     }
1.47      albertel  434:     return $result;
1.1       tsai      435: }
                    436: 
                    437: sub end_label {
1.47      albertel  438:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    439:     my @result;
1.57      albertel  440:     if ($target eq 'edit') {
                    441: 	@result=('','no') ;
                    442:     } elsif ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    443: 	     $target eq 'answer' || $target eq 'analyze') {
                    444: 	my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
                    445: 	my $ltext=&Apache::lonxml::endredirection; 
                    446: 	if ($type eq 'image') {
                    447: 	    &Apache::lonxml::debug("Turning $ltext, $Apache::lonxml::pwd[-1]");
                    448: 	    $ltext=&Apache::imageresponse::clean_up_image($ltext);
                    449: #	    $ltext=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],
                    450: #						 $ltext);
                    451: 	    &Apache::lonxml::debug("into $ltext");
                    452: 	    my $description = &Apache::lonxml::get_param('description',
                    453: 							 $parstack,$safeeval);
                    454: 	    push(@Apache::randomlabel::description,$description);
1.61    ! albertel  455: 	} else {
        !           456: 	    $ltext=~s/[\r\n]*//gs
1.57      albertel  457: 	}
                    458: 	push(@Apache::randomlabel::label_arr,$ltext);
                    459:     }
1.47      albertel  460:     return @result;
1.1       tsai      461: }
                    462: 
                    463: 1;
                    464: __END__
                    465:  

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