Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.5

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
                      3: 
                      4: # 11/23,11/24,11/28 Gerd Kortemeyer
                      5: 
                      6: package Apache::radiobuttonresponse;
                      7: use strict;
                      8: 
                      9: sub BEGIN {
1.3       albertel   10:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1       albertel   11: }
                     12: 
                     13: sub start_radiobuttonresponse {
1.4       albertel   14:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel   15:   #when in a radiobutton response use these
                     16:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil'));
1.4       albertel   17:   my $id = &Apache::response::start_response($parstack,$safeeval);
                     18:   return '';
1.1       albertel   19: }
                     20: 
                     21: sub end_radiobuttonresponse {
1.4       albertel   22:   &Apache::response::end_response;
                     23:   return '';
1.1       albertel   24: }
                     25: 
                     26: %Apache::response::foilgroup={};
                     27: sub start_foilgroup {
                     28:   %Apache::response::foilgroup={};
                     29: }
                     30: 
1.5     ! albertel   31: sub setrandomnumber {
        !            32:   my $rndseed=&Apache::lonnet::rndseed();
        !            33:   $rndseed=unpack("%32i",$rndseed);
        !            34:   $rndseed=$rndseed
        !            35:     +&Apache::lonnet::numval($Apache::inputtags::part)
        !            36:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
        !            37:   srand($rndseed);
        !            38:   return '';
        !            39: }
        !            40: 
1.4       albertel   41: #FIXME needs to stablely do random picks
1.1       albertel   42: sub end_foilgroup {
                     43:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                     44:   my $name;
                     45:   my $result;
1.5     ! albertel   46:   &setrandomnumber();
        !            47:   my $rrargs ='';
        !            48:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
        !            49:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
        !            50:   my $answer = int(rand $max);
        !            51:   if ($target eq 'web') {
        !            52:     $result=&displayfoils($max,$answer);
        !            53:   } elsif ( $target eq 'grade') {
        !            54:     if ( defined $ENV{'form.submit'}) {
        !            55:       my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
        !            56:       if ( $response =~ /[^\s]/) {
        !            57: 	my $id = $Apache::inputtags::response['-1'];
        !            58: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
        !            59: 	&Apache::lonxml::debug("submitted a $response<br>\n");
        !            60: 	if ($response == $answer) {
        !            61: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
        !            62: 	} else {
        !            63: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
        !            64: 	}
        !            65:       }
        !            66:     }
        !            67:   }
        !            68:   return $result;
        !            69: }
        !            70: 
        !            71: sub displayfoils {
        !            72:   my ($max,$answer)=@_;
1.3       albertel   73:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     74:   my @truelist;
                     75:   my @falselist;
1.5     ! albertel   76:   my $result;
        !            77:   my $name;
        !            78: 
1.3       albertel   79:   foreach $name (@names) {
1.5     ! albertel   80:     #result.="<br><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
1.3       albertel   81:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                     82:       push (@truelist,$name);
                     83:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                     84:       push (@falselist,$name);
                     85:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                     86:     } else {
1.5     ! albertel   87:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3       albertel   88:     }
1.1       albertel   89:   }
1.3       albertel   90:   my $whichtrue = rand $#truelist;
1.4       albertel   91:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                     92:   my @whichfalse =();
                     93:   while ((($#whichfalse+2) < $max) && ($#falselist > -1)) {
                     94:     my $afalse=rand $#falselist;
                     95:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                     96:     $afalse=splice(@falselist,$afalse,1);
                     97:     &Apache::lonxml::debug("Picked $afalse");
                     98:     push (@whichfalse,$afalse);
                     99:   }
1.5     ! albertel  100:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
        !           101:   my $temp=0;
        !           102:   &Apache::lonxml::debug("the true statement is $answer");
        !           103:   foreach $name (@whichfalse) {
        !           104:     $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\">".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
        !           105:     $temp++;
1.4       albertel  106:   }
1.5     ! albertel  107:   return $result."<br />";
1.1       albertel  108: }
                    109: 
                    110: sub start_foil {
1.4       albertel  111:   $Apache::lonxml::redirection--;
1.5     ! albertel  112:   return '';
1.1       albertel  113: }
                    114: 
                    115: sub end_foil {
                    116:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel  117:   if ($target eq 'web' || $target eq 'grade') {
                    118:     my $args ='';
                    119:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    120:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
                    121:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    122:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
                    123:     $Apache::response::foilgroup{"$name.value"} = $value;
                    124:     $Apache::response::foilgroup{"$name.text"} = $Apache::lonxml::outputstack;
                    125:   }
                    126:   
1.4       albertel  127:   $Apache::lonxml::redirection++;
                    128:   if ($Apache::lonxml::redirection == 1) { 
                    129:     $Apache::lonxml::outputstack=''; 
                    130:   }
1.1       albertel  131:   return '';
                    132: }
                    133: 
                    134: 1;
                    135: __END__
                    136:  

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