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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.13      albertel    3: # 2/21 Guy
1.1       albertel    4: 
                      5: package Apache::radiobuttonresponse;
                      6: use strict;
                      7: 
                      8: sub BEGIN {
1.3       albertel    9:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1       albertel   10: }
                     11: 
                     12: sub start_radiobuttonresponse {
1.4       albertel   13:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel   14:   #when in a radiobutton response use these
                     15:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil'));
1.4       albertel   16:   my $id = &Apache::response::start_response($parstack,$safeeval);
                     17:   return '';
1.1       albertel   18: }
                     19: 
                     20: sub end_radiobuttonresponse {
1.4       albertel   21:   &Apache::response::end_response;
                     22:   return '';
1.1       albertel   23: }
                     24: 
                     25: %Apache::response::foilgroup={};
                     26: sub start_foilgroup {
                     27:   %Apache::response::foilgroup={};
1.14    ! albertel   28:   $Apache::optionresponse::conceptgroup=0;
        !            29:   &setrandomnumber();
1.6       albertel   30:   return '';
1.1       albertel   31: }
                     32: 
1.5       albertel   33: sub setrandomnumber {
                     34:   my $rndseed=&Apache::lonnet::rndseed();
                     35:   $rndseed=unpack("%32i",$rndseed);
                     36:   $rndseed=$rndseed
                     37:     +&Apache::lonnet::numval($Apache::inputtags::part)
                     38:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
                     39:   srand($rndseed);
                     40:   return '';
                     41: }
                     42: 
1.1       albertel   43: sub end_foilgroup {
                     44:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel   45:   
1.1       albertel   46:   my $result;
1.7       albertel   47:   if ($target ne 'meta') {
                     48:     my $name;
                     49:     my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
                     50:     my $count=0;
                     51:     # we will add in 1 of the true statements
                     52:     if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
                     53:     my $answer = int(rand ($count));
                     54:     &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
                     55:     if ($target eq 'web') {
                     56:       $result=&displayfoils($max,$answer);
                     57:     } elsif ( $target eq 'grade') {
1.8       albertel   58:       if ( defined $ENV{'form.submitted'}) {
1.7       albertel   59: 	my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
                     60: 	if ( $response =~ /[^\s]/) {
                     61: 	  my $id = $Apache::inputtags::response['-1'];
                     62: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
1.12      albertel   63: 	  &Apache::lonxml::debug("submitted a $response<br />\n");
1.7       albertel   64: 	  if ($response == $answer) {
                     65: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
                     66: 	  } else {
                     67: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
                     68: 	  }
1.5       albertel   69: 	}
                     70:       }
                     71:     }
                     72:   }
                     73:   return $result;
1.6       albertel   74: }
                     75: 
                     76: sub getfoilcounts {
                     77:   my ($parstack,$safeeval)=@_;
                     78:   my $rrargs ='';
                     79:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
                     80:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
                     81:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     82:   my $truecnt=0;
                     83:   my $falsecnt=0;
                     84:   my $name;
                     85: 
                     86:   foreach $name (@names) {
                     87:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                     88:       $truecnt++;
                     89:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                     90:       $falsecnt++;
                     91:     }
                     92:   }
                     93:   return ($truecnt,$falsecnt,$max);
1.5       albertel   94: }
                     95: 
                     96: sub displayfoils {
                     97:   my ($max,$answer)=@_;
1.3       albertel   98:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     99:   my @truelist;
                    100:   my @falselist;
1.5       albertel  101:   my $result;
                    102:   my $name;
                    103: 
1.3       albertel  104:   foreach $name (@names) {
1.12      albertel  105:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
1.3       albertel  106:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    107:       push (@truelist,$name);
                    108:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    109:       push (@falselist,$name);
                    110:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    111:     } else {
1.5       albertel  112:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3       albertel  113:     }
1.1       albertel  114:   }
1.3       albertel  115:   my $whichtrue = rand $#truelist;
1.4       albertel  116:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    117:   my @whichfalse =();
                    118:   while ((($#whichfalse+2) < $max) && ($#falselist > -1)) {
                    119:     my $afalse=rand $#falselist;
                    120:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    121:     $afalse=splice(@falselist,$afalse,1);
                    122:     &Apache::lonxml::debug("Picked $afalse");
                    123:     push (@whichfalse,$afalse);
                    124:   }
1.5       albertel  125:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
                    126:   &Apache::lonxml::debug("the true statement is $answer");
1.10      albertel  127:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
                    128:     foreach $name (@whichfalse) {
1.11      albertel  129:       $result.="<br />";
                    130:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
                    131: 	$result.='Correct';
                    132:       } else {
                    133: 	$result.='Incorrect';
                    134:       }
                    135:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
1.10      albertel  136:     }
                    137:   } else {
                    138:     my $temp=0;
                    139:     foreach $name (@whichfalse) {
                    140:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\">".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    141:       $temp++;
                    142:     }
1.4       albertel  143:   }
1.5       albertel  144:   return $result."<br />";
1.14    ! albertel  145: }
        !           146: 
        !           147: sub start_conceptgroup {
        !           148:   $Apache::optionresponse::conceptgroup=1;  
        !           149:   %Apache::response::conceptgroup={};
        !           150:   return '';
        !           151: }
        !           152: 
        !           153: sub end_conceptgroup {
        !           154:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
        !           155:   $Apache::optionresponse::conceptgroup=0;  
        !           156:   if ($target eq 'web' || $target eq 'grade') {
        !           157:     my @names = @{ $Apache::response::conceptgroup{'names'} };
        !           158:     my $pick=int rand $#names+1;
        !           159:     my $name=$names[$pick];
        !           160:     push @{ $Apache::response::foilgroup{'names'} }, $name;
        !           161:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
        !           162:     $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
        !           163:     my $args;
        !           164:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
        !           165:     my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval);
        !           166:     $Apache::response::foilgroup{"$name.concept"} = $concept;
        !           167:     &Apache::lonxml::debug("Selecting $name in $concept");
        !           168:   }
        !           169:   return '';
1.1       albertel  170: }
                    171: 
                    172: sub start_foil {
1.13      albertel  173:   &Apache::lonxml::startredirection;
1.5       albertel  174:   return '';
1.1       albertel  175: }
                    176: 
                    177: sub end_foil {
                    178:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.13      albertel  179:   my $foil=&Apache::lonxml::endredirection;
1.3       albertel  180:   if ($target eq 'web' || $target eq 'grade') {
                    181:     my $args ='';
                    182:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    183:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
                    184:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    185:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
                    186:     $Apache::response::foilgroup{"$name.value"} = $value;
1.13      albertel  187:     $Apache::response::foilgroup{"$name.text"} = $foil;
1.4       albertel  188:   }
1.1       albertel  189:   return '';
                    190: }
                    191: 
                    192: 1;
                    193: __END__
                    194:  

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