Annotation of loncom/homework/imageresponse.pm, revision 1.7

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # iimage click response style
                      3: 
1.3       albertel    4: #FIXME assumes multiple possbile submissions but only one is possible currently
                      5: 
1.1       albertel    6: package Apache::imageresponse;
                      7: use strict;
                      8: 
                      9: sub BEGIN {
                     10:   &Apache::lonxml::register('Apache::imageresponse',('imageresponse'));
                     11: }
                     12: 
                     13: sub start_imageresponse {
                     14:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                     15:   #when in a radiobutton response use these
1.7     ! albertel   16:   &Apache::lonxml::register('Apache::imageresponse',('foilgroup','foil','text','image','rectangle','conceptgroup'));
1.6       albertel   17:   push (@Apache::lonxml::namespace,'imageresponse');
1.1       albertel   18:   my $id = &Apache::response::start_response($parstack,$safeeval);
                     19:   return '';
                     20: }
                     21: 
                     22: sub end_imageresponse {
                     23:   &Apache::response::end_response;
1.6       albertel   24:   pop @Apache::lonxml::namespace;
1.1       albertel   25:   return '';
                     26: }
                     27: 
                     28: %Apache::response::foilgroup={};
                     29: sub start_foilgroup {
                     30:   %Apache::response::foilgroup={};
1.7     ! albertel   31:   $Apache::imageresponse::conceptgroup=0;
1.5       albertel   32:   &Apache::response::setrandomnumber();
1.1       albertel   33:   return '';
                     34: }
                     35: 
1.2       albertel   36: sub getfoilcounts {
                     37:   my ($parstack,$safeeval)=@_;
1.7     ! albertel   38: 
        !            39:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
        !            40:   # +1 since instructors will count from 1
        !            41:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.2       albertel   42:   return ($count,$max);
                     43: }
                     44: 
                     45: sub whichfoils {
                     46:   my ($max)=@_;
                     47:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     48:   my @whichopt =();
1.7     ! albertel   49:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
        !            50:     &Apache::lonxml::debug("Have $#whichopt max is $max");
        !            51:     my $aopt=int(rand($#names+1));
1.2       albertel   52:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
                     53:     $aopt=splice(@names,$aopt,1);
                     54:     &Apache::lonxml::debug("Picked $aopt");
                     55:     push (@whichopt,$aopt);
                     56:   }
                     57:   return @whichopt;
                     58: }
                     59: 
                     60: sub displayfoils {
                     61:   my (@whichopt) = @_;
                     62:   my $result ='';
                     63:   my $name;
                     64:   my $temp=1;
                     65:   foreach $name (@whichopt) {
                     66:     $result.=$Apache::response::foilgroup{"$name.text"}."<br />\n";
                     67:     my $image=$Apache::response::foilgroup{"$name.image"};
1.3       albertel   68:     if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
                     69:       $result.="<img src=\"$image\"/> <br />\n";
                     70:     } else {
                     71:       $result.="<input type=\"image\" name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\" src=\"$image\"/> <br />\n";
                     72:     }
1.2       albertel   73:     $temp++;
                     74:   }
                     75:   return $result;
                     76: }
                     77: 
1.3       albertel   78: sub gradefoils {
                     79:   my (@whichopt) = @_;
                     80:   my $result='';
                     81:   my $x;
                     82:   my $y;
                     83:   my $result;
                     84:   my $id=$Apache::inputtags::response['-1'];
                     85:   my $temp=1;
                     86:   foreach my $name (@whichopt) {
                     87:     $x=$ENV{"form.HWVAL_$id:$temp.x"};
                     88:     $y=$ENV{"form.HWVAL_$id:$temp.y"};
                     89:     &Apache::lonxml::debug("Got a x of $x and a y of $y for $name");
                     90:     my @areas = @{ $Apache::response::foilgroup{"$name.area"} };
                     91:     my $grade="INCORRECT";
                     92:     foreach my $area (@areas) {
                     93:       &Apache::lonxml::debug("Area is $area for $name");
                     94:       $area =~ m/([a-z]*):/;
                     95:       &Apache::lonxml::debug("Area of type $1");
                     96:       if ($1 eq 'rectangle') {
                     97: 	$grade=&grade_rectangle($area,$x,$y);
                     98:       } else {
                     99: 	&Apache::lonxml::error("Unknown area style $area");
                    100:       }
                    101:       &Apache::lonxml::debug("Area said $grade");
                    102:       if ($grade eq 'APPROX_ANS') { last; }
                    103:     }
                    104:     &Apache::lonxml::debug("Foil was $grade");
                    105:     if ($grade eq 'INCORRECT') { $result= 'INCORRECT'; }
                    106:     if (($grade eq 'APPROX_ANS') && ($result ne 'APPROX_ANS')) { $result=$grade; }
                    107:     &Apache::lonxml::debug("Question is $result");
                    108:     $temp++;
                    109:   }
                    110:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}="$x:$y";
                    111:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$result;
                    112:   return '';
                    113: }
                    114: 
1.1       albertel  115: sub end_foilgroup {
                    116:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.2       albertel  117:   my $result='';
1.3       albertel  118:   my @whichopt;
1.2       albertel  119:   if ($target eq 'web' || $target eq 'grade') {
                    120:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    121:     if ($count>$max) { $count=$max }
                    122:     &Apache::lonxml::debug("Count is $count from $max");
1.3       albertel  123:     @whichopt = &whichfoils($max);
1.2       albertel  124:   }
                    125:   if ($target eq 'web') {
                    126:     $result=&displayfoils(@whichopt);
                    127:   }
                    128:   if ($target eq 'grade') {
1.3       albertel  129:     if ( defined $ENV{'form.submitted'}) {
                    130:       &gradefoils(@whichopt);
                    131:     }
1.2       albertel  132:   }
                    133:   return $result;
1.1       albertel  134: }
                    135: 
                    136: sub start_conceptgroup {
1.7     ! albertel  137:   $Apache::imageresponse::conceptgroup=1;
1.4       albertel  138:   %Apache::response::conceptgroup={};
                    139:   return '';
1.1       albertel  140: }
                    141: 
                    142: sub end_conceptgroup {
1.4       albertel  143:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7     ! albertel  144:   $Apache::imageresponse::conceptgroup=0;  
1.4       albertel  145:   if ($target eq 'web' || $target eq 'grade') {
                    146:     my @names = @{ $Apache::response::conceptgroup{'names'} };
1.7     ! albertel  147:     my $pick=int(rand($#names+1));
1.4       albertel  148:     my $name=$names[$pick];
                    149:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    150:     $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
                    151:     $Apache::response::foilgroup{"$name.image"} = $Apache::response::conceptgroup{"$name.image"};
                    152:     push(@{ $Apache::response::foilgroup{"$name.area"} }, @{ $Apache::response::conceptgroup{"$name.area"} });
1.7     ! albertel  153:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
1.4       albertel  154:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    155:     &Apache::lonxml::debug("Selecting $name in $concept");
                    156:   }
                    157:   return '';
1.1       albertel  158: }
                    159: 
                    160: $Apache::imageresponse::curname='';
                    161: sub start_foil {
                    162:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                    163:   if ($target eq 'web' || $target eq 'grade') {
1.7     ! albertel  164:     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.3       albertel  165:     if ($name eq '') { $name=$Apache::lonxml::curdepth; }
1.7     ! albertel  166:     if ( $Apache::imageresponse::conceptgroup ) {
        !           167:       push(@{ $Apache::response::conceptgroup{'names'} }, $name);
        !           168:     } else {
        !           169:       push(@{ $Apache::response::foilgroup{'names'} }, $name);
        !           170:     }
1.1       albertel  171:     $Apache::imageresponse::curname=$name;
                    172:   }
                    173:   return '';
                    174: }
                    175: 
                    176: sub end_foil {
                    177:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                    178:   return '';
                    179: }
                    180: 
                    181: sub start_text {
                    182:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel  183:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
1.1       albertel  184:   return '';
                    185: }
                    186: 
                    187: sub end_text {
                    188:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                    189:   if ($target eq 'web') {
                    190:     my $name = $Apache::imageresponse::curname;
1.7     ! albertel  191:     if ( $Apache::imageresponse::conceptgroup ) {
        !           192:       $Apache::response::conceptgroup{"$name.text"} = &Apache::lonxml::endredirection;
        !           193:     } else {
        !           194:       $Apache::response::foilgroup{"$name.text"} = &Apache::lonxml::endredirection;
        !           195:     }
1.1       albertel  196:   }
                    197:   return '';
                    198: }
                    199: 
                    200: sub start_image {
                    201:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel  202:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
1.2       albertel  203:   return '';
1.1       albertel  204: }
                    205: 
                    206: sub end_image {
                    207:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.2       albertel  208:   if ($target eq 'web') {
                    209:     my $name = $Apache::imageresponse::curname;
1.3       albertel  210:     my $image = &Apache::lonxml::endredirection;
                    211:     &Apache::lonxml::debug("out is $image");
1.7     ! albertel  212:     if ( $Apache::imageresponse::conceptgroup ) {
        !           213:       $Apache::response::conceptgroup{"$name.image"} = $image;
        !           214:     } else {
        !           215:       $Apache::response::foilgroup{"$name.image"} = $image;
        !           216:     }
1.2       albertel  217:   }
                    218:   return '';
1.1       albertel  219: }
                    220: 
                    221: sub start_rectangle {
                    222:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel  223:   if ($target eq 'web' || $target eq 'grade') { &Apache::lonxml::startredirection; }
1.2       albertel  224:   return '';
1.1       albertel  225: }
                    226: 
1.3       albertel  227: sub grade_rectangle {
                    228:   my ($spec,$x,$y) = @_;
                    229:   &Apache::lonxml::debug("Spec is $spec");
                    230:   $spec=~m/rectangle:\(([0-9]+),([0-9]+)\)\-\(([0-9]+),([0-9]+)\)/;
                    231:   my $x1=$1;
                    232:   my $y1=$2;
                    233:   my $x2=$3;
                    234:   my $y2=$4;
                    235:   &Apache::lonxml::debug("Point $x1,$y1,$x2,$y2");
                    236:   if ($x1 > $x2) { my $temp=$x1;$x1=$x2;$x2=$temp; }
                    237:   if ($y1 > $y2) { my $temp=$y1;$y1=$y2;$y2=$temp; }
                    238:   if ($x => $x1) { if ($x <= $x2) { if ($y => $y1) { if ($y <= $y2) { return 'APPROX_ANS'; } } } }
                    239:   return 'INCORRECT';
                    240: }
                    241: 
1.1       albertel  242: sub end_rectangle {
                    243:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel  244:   if ($target eq 'web' || $target eq 'grade') {
1.2       albertel  245:     my $name = $Apache::imageresponse::curname;
1.3       albertel  246:     my $area = &Apache::lonxml::endredirection;
                    247:     &Apache::lonxml::debug("out is $area for $name");
1.7     ! albertel  248:     if ( $Apache::imageresponse::conceptgroup ) {
        !           249:       push @{ $Apache::response::conceptgroup{"$name.area"} },"rectangle:$area";
        !           250:     } else {
        !           251:       push @{ $Apache::response::foilgroup{"$name.area"} },"rectangle:$area";
        !           252:     }
1.2       albertel  253:   }
1.3       albertel  254:   return '';
1.1       albertel  255: }
                    256: 1;
                    257: __END__
                    258:  

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