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

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

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