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

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

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