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

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.20    ! albertel   13:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.3       albertel   14:   #when in a radiobutton response use these
1.18      albertel   15:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
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.18      albertel   28:   $Apache::radiobuttonresponse::conceptgroup=0;
1.17      albertel   29:   &Apache::response::setrandomnumber();
1.5       albertel   30:   return '';
                     31: }
                     32: 
1.15      albertel   33: sub storesurvey {
                     34:   if ( defined $ENV{'form.submitted'}) {
                     35:     my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
                     36:     &Apache::lonxml::debug("Here I am!:$response:");
                     37:     if ( $response =~ /[^\s]/) {
                     38:       my $id = $Apache::inputtags::response['-1'];
                     39:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
                     40:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
                     41:       &Apache::lonxml::debug("submitted a $response<br />\n");
                     42:     }
                     43:   }
                     44:   return '';
                     45: }
                     46: 
1.1       albertel   47: sub end_foilgroup {
1.20    ! albertel   48:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7       albertel   49:   
1.1       albertel   50:   my $result;
1.7       albertel   51:   if ($target ne 'meta') {
1.19      albertel   52:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval,'-2');
1.15      albertel   53:     if ( $style eq 'survey' ) {
                     54:       if ($target eq 'web') {
                     55: 	$result=&displayallfoils();
                     56:       } elsif ( $target eq 'grade' ) {
                     57: 	$result=&storesurvey();
                     58:       }
                     59:     } else {
                     60:       my $name;
                     61:       my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
                     62:       my $count=0;
                     63:       # we will add in 1 of the true statements
                     64:       if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
                     65:       my $answer = int(rand ($count));
                     66:       &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
                     67:       if ($target eq 'web') {
                     68: 	$result=&displayfoils($max,$answer);
                     69:       } elsif ( $target eq 'grade') {
                     70: 	if ( defined $ENV{'form.submitted'}) {
                     71: 	  my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
                     72: 	  if ( $response =~ /[^\s]/) {
                     73: 	    my $id = $Apache::inputtags::response['-1'];
                     74: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
                     75: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
                     76: 	    if ($response == $answer) {
                     77: 	      $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
                     78: 	    } else {
                     79: 	      $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
                     80: 	    }
1.7       albertel   81: 	  }
1.5       albertel   82: 	}
                     83:       }
                     84:     }
                     85:   }
                     86:   return $result;
1.6       albertel   87: }
                     88: 
                     89: sub getfoilcounts {
                     90:   my ($parstack,$safeeval)=@_;
1.19      albertel   91:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
1.6       albertel   92:   my @names = @{ $Apache::response::foilgroup{'names'} };
                     93:   my $truecnt=0;
                     94:   my $falsecnt=0;
                     95:   my $name;
                     96: 
                     97:   foreach $name (@names) {
                     98:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                     99:       $truecnt++;
                    100:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    101:       $falsecnt++;
                    102:     }
                    103:   }
                    104:   return ($truecnt,$falsecnt,$max);
1.5       albertel  105: }
                    106: 
1.15      albertel  107: sub displayallfoils {
                    108:   my $result;
                    109:   &Apache::lonxml::debug("survey style display");
                    110:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    111:   my $temp=0;
                    112:   my $id=$Apache::inputtags::response['-1'];
                    113:   my $part=$Apache::inputtags::part;
                    114:   my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    115:   foreach my $name (@names) {
                    116:     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
                    117:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.16      albertel  118:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
1.15      albertel  119:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    120:       $temp++;
                    121:     }
                    122:   }
                    123:   return $result;
                    124: }
                    125: 
1.5       albertel  126: sub displayfoils {
                    127:   my ($max,$answer)=@_;
1.3       albertel  128:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    129:   my @truelist;
                    130:   my @falselist;
1.5       albertel  131:   my $result;
                    132:   my $name;
                    133: 
1.3       albertel  134:   foreach $name (@names) {
1.12      albertel  135:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
1.3       albertel  136:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    137:       push (@truelist,$name);
                    138:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    139:       push (@falselist,$name);
                    140:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    141:     } else {
1.5       albertel  142:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3       albertel  143:     }
1.1       albertel  144:   }
1.18      albertel  145:   my $whichtrue = int(rand($#truelist+1));
1.4       albertel  146:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    147:   my @whichfalse =();
1.15      albertel  148:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
1.18      albertel  149:     &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    150:     my $afalse=int(rand($#falselist+1));
1.4       albertel  151:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    152:     $afalse=splice(@falselist,$afalse,1);
                    153:     &Apache::lonxml::debug("Picked $afalse");
                    154:     push (@whichfalse,$afalse);
                    155:   }
1.5       albertel  156:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
                    157:   &Apache::lonxml::debug("the true statement is $answer");
1.10      albertel  158:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
                    159:     foreach $name (@whichfalse) {
1.11      albertel  160:       $result.="<br />";
                    161:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
                    162: 	$result.='Correct';
                    163:       } else {
                    164: 	$result.='Incorrect';
                    165:       }
                    166:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
1.10      albertel  167:     }
                    168:   } else {
                    169:     my $temp=0;
1.15      albertel  170:     my $id=$Apache::inputtags::response['-1'];
                    171:     my $part=$Apache::inputtags::part;
                    172:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.10      albertel  173:     foreach $name (@whichfalse) {
1.15      albertel  174:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.16      albertel  175:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
1.15      albertel  176:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
1.10      albertel  177:       $temp++;
                    178:     }
1.4       albertel  179:   }
1.5       albertel  180:   return $result."<br />";
1.14      albertel  181: }
                    182: 
                    183: sub start_conceptgroup {
1.18      albertel  184:   $Apache::radiobuttonresponse::conceptgroup=1;
1.14      albertel  185:   %Apache::response::conceptgroup={};
                    186:   return '';
                    187: }
                    188: 
                    189: sub end_conceptgroup {
1.20    ! albertel  190:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.18      albertel  191:   $Apache::radiobuttonresponse::conceptgroup=0;
1.14      albertel  192:   if ($target eq 'web' || $target eq 'grade') {
                    193:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    194:     my $pick=int rand $#names+1;
                    195:     my $name=$names[$pick];
                    196:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    197:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
                    198:     $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
1.19      albertel  199:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
1.14      albertel  200:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    201:     &Apache::lonxml::debug("Selecting $name in $concept");
                    202:   }
                    203:   return '';
1.1       albertel  204: }
                    205: 
                    206: sub start_foil {
1.13      albertel  207:   &Apache::lonxml::startredirection;
1.5       albertel  208:   return '';
1.1       albertel  209: }
                    210: 
                    211: sub end_foil {
1.20    ! albertel  212:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.18      albertel  213:   my $text='';
                    214:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.3       albertel  215:   if ($target eq 'web' || $target eq 'grade') {
1.19      albertel  216:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18      albertel  217:     if ($value ne 'unused') {
1.19      albertel  218:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.18      albertel  219:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
                    220: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    221: 	$Apache::response::conceptgroup{"$name.value"} = $value;
                    222: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
                    223:       } else {
                    224: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    225: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    226: 	$Apache::response::foilgroup{"$name.text"} = $text;
                    227:       }
                    228:     }
1.4       albertel  229:   }
1.1       albertel  230:   return '';
                    231: }
                    232: 
                    233: 1;
                    234: __END__
                    235:  

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