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

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.22      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.22      albertel   21:   &Apache::response::end_response;
                     22:   return '';
1.1       albertel   23: }
                     24: 
1.22      albertel   25: %Apache::response::foilgroup={};
1.1       albertel   26: sub start_foilgroup {
1.22      albertel   27:   %Apache::response::foilgroup={};
                     28:   $Apache::radiobuttonresponse::conceptgroup=0;
                     29:   &Apache::response::setrandomnumber();
                     30:   return '';
1.5       albertel   31: }
                     32: 
1.15      albertel   33: sub storesurvey {
1.22      albertel   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");
1.15      albertel   42:     }
1.22      albertel   43:   }
                     44:   return '';
1.15      albertel   45: }
                     46: 
1.1       albertel   47: sub end_foilgroup {
1.22      albertel   48:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     49:   
                     50:   my $result;
1.23    ! albertel   51:   if ($target eq 'grade' || $target eq 'web') {
1.22      albertel   52:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval,'-2');
                     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';
1.15      albertel   80: 	    }
1.22      albertel   81: 	  }
1.5       albertel   82: 	}
1.22      albertel   83:       }
1.5       albertel   84:     }
1.22      albertel   85:   }
                     86:   return $result;
1.6       albertel   87: }
                     88: 
                     89: sub getfoilcounts {
1.22      albertel   90:   my ($parstack,$safeeval)=@_;
                     91:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                     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++;
1.6       albertel  102:     }
1.22      albertel  103:   }
                    104:   return ($truecnt,$falsecnt,$max);
1.5       albertel  105: }
                    106: 
1.15      albertel  107: sub displayallfoils {
1.22      albertel  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\" ";
                    118:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
                    119:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    120:       $temp++;
1.15      albertel  121:     }
1.22      albertel  122:   }
                    123:   return $result;
1.15      albertel  124: }
                    125: 
1.5       albertel  126: sub displayfoils {
1.22      albertel  127:   my ($max,$answer)=@_;
                    128:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    129:   my @truelist;
                    130:   my @falselist;
                    131:   my $result;
                    132:   my $name;
                    133: 
                    134:   foreach $name (@names) {
                    135:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
                    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 {
                    142:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3       albertel  143:     }
1.22      albertel  144:   }
                    145:   my $whichtrue = int(rand($#truelist+1));
                    146:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    147:   my @whichfalse =();
                    148:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
                    149:     &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    150:     my $afalse=int(rand($#falselist+1));
                    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:   }
                    156:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
                    157:   &Apache::lonxml::debug("the true statement is $answer");
                    158:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
                    159:     foreach $name (@whichfalse) {
                    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:     }
1.22      albertel  168:   } else {
                    169:     my $temp=0;
                    170:     my $id=$Apache::inputtags::response['-1'];
                    171:     my $part=$Apache::inputtags::part;
                    172:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    173:     foreach $name (@whichfalse) {
                    174:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
                    175:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
                    176:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    177:       $temp++;
1.10      albertel  178:     }
1.22      albertel  179:   }
                    180:   return $result."<br />";
1.14      albertel  181: }
                    182: 
                    183: sub start_conceptgroup {
1.22      albertel  184:   $Apache::radiobuttonresponse::conceptgroup=1;
                    185:   %Apache::response::conceptgroup={};
                    186:   return '';
1.14      albertel  187: }
                    188: 
                    189: sub end_conceptgroup {
1.22      albertel  190:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    191:   $Apache::radiobuttonresponse::conceptgroup=0;
                    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"};
                    199:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    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.22      albertel  207:   &Apache::lonxml::startredirection;
                    208:   return '';
1.1       albertel  209: }
                    210: 
                    211: sub end_foil {
1.22      albertel  212:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    213:   my $text='';
                    214:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
                    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>