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

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

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