File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.14: download - view: text, annotated - select for diffs
Thu Mar 1 01:20:04 2001 UTC (23 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <conceptgroup>

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: # 2/21 Guy
    4: 
    5: package Apache::radiobuttonresponse;
    6: use strict;
    7: 
    8: sub BEGIN {
    9:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   10: }
   11: 
   12: sub start_radiobuttonresponse {
   13:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   14:   #when in a radiobutton response use these
   15:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil'));
   16:   my $id = &Apache::response::start_response($parstack,$safeeval);
   17:   return '';
   18: }
   19: 
   20: sub end_radiobuttonresponse {
   21:   &Apache::response::end_response;
   22:   return '';
   23: }
   24: 
   25: %Apache::response::foilgroup={};
   26: sub start_foilgroup {
   27:   %Apache::response::foilgroup={};
   28:   $Apache::optionresponse::conceptgroup=0;
   29:   &setrandomnumber();
   30:   return '';
   31: }
   32: 
   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: 
   43: sub end_foilgroup {
   44:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   45:   
   46:   my $result;
   47:   if ($target ne 'meta') {
   48:     my $name;
   49:     my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
   50:     my $count=0;
   51:     # we will add in 1 of the true statements
   52:     if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
   53:     my $answer = int(rand ($count));
   54:     &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
   55:     if ($target eq 'web') {
   56:       $result=&displayfoils($max,$answer);
   57:     } elsif ( $target eq 'grade') {
   58:       if ( defined $ENV{'form.submitted'}) {
   59: 	my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   60: 	if ( $response =~ /[^\s]/) {
   61: 	  my $id = $Apache::inputtags::response['-1'];
   62: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
   63: 	  &Apache::lonxml::debug("submitted a $response<br />\n");
   64: 	  if ($response == $answer) {
   65: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
   66: 	  } else {
   67: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
   68: 	  }
   69: 	}
   70:       }
   71:     }
   72:   }
   73:   return $result;
   74: }
   75: 
   76: sub getfoilcounts {
   77:   my ($parstack,$safeeval)=@_;
   78:   my $rrargs ='';
   79:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
   80:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
   81:   my @names = @{ $Apache::response::foilgroup{'names'} };
   82:   my $truecnt=0;
   83:   my $falsecnt=0;
   84:   my $name;
   85: 
   86:   foreach $name (@names) {
   87:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
   88:       $truecnt++;
   89:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
   90:       $falsecnt++;
   91:     }
   92:   }
   93:   return ($truecnt,$falsecnt,$max);
   94: }
   95: 
   96: sub displayfoils {
   97:   my ($max,$answer)=@_;
   98:   my @names = @{ $Apache::response::foilgroup{'names'} };
   99:   my @truelist;
  100:   my @falselist;
  101:   my $result;
  102:   my $name;
  103: 
  104:   foreach $name (@names) {
  105:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  106:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  107:       push (@truelist,$name);
  108:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  109:       push (@falselist,$name);
  110:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  111:     } else {
  112:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
  113:     }
  114:   }
  115:   my $whichtrue = rand $#truelist;
  116:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  117:   my @whichfalse =();
  118:   while ((($#whichfalse+2) < $max) && ($#falselist > -1)) {
  119:     my $afalse=rand $#falselist;
  120:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  121:     $afalse=splice(@falselist,$afalse,1);
  122:     &Apache::lonxml::debug("Picked $afalse");
  123:     push (@whichfalse,$afalse);
  124:   }
  125:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  126:   &Apache::lonxml::debug("the true statement is $answer");
  127:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  128:     foreach $name (@whichfalse) {
  129:       $result.="<br />";
  130:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  131: 	$result.='Correct';
  132:       } else {
  133: 	$result.='Incorrect';
  134:       }
  135:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  136:     }
  137:   } else {
  138:     my $temp=0;
  139:     foreach $name (@whichfalse) {
  140:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\">".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  141:       $temp++;
  142:     }
  143:   }
  144:   return $result."<br />";
  145: }
  146: 
  147: sub start_conceptgroup {
  148:   $Apache::optionresponse::conceptgroup=1;  
  149:   %Apache::response::conceptgroup={};
  150:   return '';
  151: }
  152: 
  153: sub end_conceptgroup {
  154:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  155:   $Apache::optionresponse::conceptgroup=0;  
  156:   if ($target eq 'web' || $target eq 'grade') {
  157:     my @names = @{ $Apache::response::conceptgroup{'names'} };
  158:     my $pick=int rand $#names+1;
  159:     my $name=$names[$pick];
  160:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  161:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  162:     $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
  163:     my $args;
  164:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  165:     my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval);
  166:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  167:     &Apache::lonxml::debug("Selecting $name in $concept");
  168:   }
  169:   return '';
  170: }
  171: 
  172: sub start_foil {
  173:   &Apache::lonxml::startredirection;
  174:   return '';
  175: }
  176: 
  177: sub end_foil {
  178:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  179:   my $foil=&Apache::lonxml::endredirection;
  180:   if ($target eq 'web' || $target eq 'grade') {
  181:     my $args ='';
  182:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  183:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  184:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  185:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  186:     $Apache::response::foilgroup{"$name.value"} = $value;
  187:     $Apache::response::foilgroup{"$name.text"} = $foil;
  188:   }
  189:   return '';
  190: }
  191: 
  192: 1;
  193: __END__
  194:  

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