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

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

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