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

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={};
1.6     ! albertel   29:   return '';
1.1       albertel   30: }
                     31: 
1.5       albertel   32: sub setrandomnumber {
                     33:   my $rndseed=&Apache::lonnet::rndseed();
                     34:   $rndseed=unpack("%32i",$rndseed);
                     35:   $rndseed=$rndseed
                     36:     +&Apache::lonnet::numval($Apache::inputtags::part)
                     37:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
                     38:   srand($rndseed);
                     39:   return '';
                     40: }
                     41: 
1.4       albertel   42: #FIXME needs to stablely do random picks
1.1       albertel   43: sub end_foilgroup {
                     44:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
                     45:   my $name;
                     46:   my $result;
1.5       albertel   47:   &setrandomnumber();
1.6     ! albertel   48:   my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
        !            49:   my $count=0;
        !            50:   # we will add in 1 of the true statements
        !            51:   if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
        !            52:   my $answer = int(rand ($count));
        !            53:   &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
1.5       albertel   54:   if ($target eq 'web') {
                     55:     $result=&displayfoils($max,$answer);
                     56:   } elsif ( $target eq 'grade') {
                     57:     if ( defined $ENV{'form.submit'}) {
                     58:       my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
                     59:       if ( $response =~ /[^\s]/) {
                     60: 	my $id = $Apache::inputtags::response['-1'];
                     61: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
                     62: 	&Apache::lonxml::debug("submitted a $response<br>\n");
                     63: 	if ($response == $answer) {
                     64: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
                     65: 	} else {
                     66: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
                     67: 	}
                     68:       }
                     69:     }
                     70:   }
                     71:   return $result;
1.6     ! albertel   72: }
        !            73: 
        !            74: sub getfoilcounts {
        !            75:   my ($parstack,$safeeval)=@_;
        !            76:   my $rrargs ='';
        !            77:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
        !            78:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
        !            79:   my @names = @{ $Apache::response::foilgroup{'names'} };
        !            80:   my $truecnt=0;
        !            81:   my $falsecnt=0;
        !            82:   my $name;
        !            83: 
        !            84:   foreach $name (@names) {
        !            85:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
        !            86:       $truecnt++;
        !            87:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
        !            88:       $falsecnt++;
        !            89:     }
        !            90:   }
        !            91:   return ($truecnt,$falsecnt,$max);
1.5       albertel   92: }
                     93: 
                     94: sub displayfoils {
                     95:   my ($max,$answer)=@_;
1.3       albertel   96:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     97:   my @truelist;
                     98:   my @falselist;
1.5       albertel   99:   my $result;
                    100:   my $name;
                    101: 
1.3       albertel  102:   foreach $name (@names) {
1.5       albertel  103:     #result.="<br><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
1.3       albertel  104:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    105:       push (@truelist,$name);
                    106:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    107:       push (@falselist,$name);
                    108:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    109:     } else {
1.5       albertel  110:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3       albertel  111:     }
1.1       albertel  112:   }
1.3       albertel  113:   my $whichtrue = rand $#truelist;
1.4       albertel  114:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    115:   my @whichfalse =();
                    116:   while ((($#whichfalse+2) < $max) && ($#falselist > -1)) {
                    117:     my $afalse=rand $#falselist;
                    118:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    119:     $afalse=splice(@falselist,$afalse,1);
                    120:     &Apache::lonxml::debug("Picked $afalse");
                    121:     push (@whichfalse,$afalse);
                    122:   }
1.5       albertel  123:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
                    124:   my $temp=0;
                    125:   &Apache::lonxml::debug("the true statement is $answer");
                    126:   foreach $name (@whichfalse) {
                    127:     $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\">".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    128:     $temp++;
1.4       albertel  129:   }
1.5       albertel  130:   return $result."<br />";
1.1       albertel  131: }
                    132: 
                    133: sub start_foil {
1.4       albertel  134:   $Apache::lonxml::redirection--;
1.5       albertel  135:   return '';
1.1       albertel  136: }
                    137: 
                    138: sub end_foil {
                    139:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel  140:   if ($target eq 'web' || $target eq 'grade') {
                    141:     my $args ='';
                    142:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    143:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
                    144:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    145:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
                    146:     $Apache::response::foilgroup{"$name.value"} = $value;
                    147:     $Apache::response::foilgroup{"$name.text"} = $Apache::lonxml::outputstack;
                    148:   }
                    149:   
1.4       albertel  150:   $Apache::lonxml::redirection++;
                    151:   if ($Apache::lonxml::redirection == 1) { 
                    152:     $Apache::lonxml::outputstack=''; 
                    153:   }
1.1       albertel  154:   return '';
                    155: }
                    156: 
                    157: 1;
                    158: __END__
                    159:  

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