File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.5: download - view: text, annotated - select for diffs
Fri Jan 12 01:07:13 2001 UTC (23 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- should be fully fuctional now

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: 
    4: # 11/23,11/24,11/28 Gerd Kortemeyer
    5: 
    6: package Apache::radiobuttonresponse;
    7: use strict;
    8: 
    9: sub BEGIN {
   10:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   11: }
   12: 
   13: sub start_radiobuttonresponse {
   14:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   15:   #when in a radiobutton response use these
   16:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil'));
   17:   my $id = &Apache::response::start_response($parstack,$safeeval);
   18:   return '';
   19: }
   20: 
   21: sub end_radiobuttonresponse {
   22:   &Apache::response::end_response;
   23:   return '';
   24: }
   25: 
   26: %Apache::response::foilgroup={};
   27: sub start_foilgroup {
   28:   %Apache::response::foilgroup={};
   29: }
   30: 
   31: sub setrandomnumber {
   32:   my $rndseed=&Apache::lonnet::rndseed();
   33:   $rndseed=unpack("%32i",$rndseed);
   34:   $rndseed=$rndseed
   35:     +&Apache::lonnet::numval($Apache::inputtags::part)
   36:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
   37:   srand($rndseed);
   38:   return '';
   39: }
   40: 
   41: #FIXME needs to stablely do random picks
   42: sub end_foilgroup {
   43:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   44:   my $name;
   45:   my $result;
   46:   &setrandomnumber();
   47:   my $rrargs ='';
   48:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
   49:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
   50:   my $answer = int(rand $max);
   51:   if ($target eq 'web') {
   52:     $result=&displayfoils($max,$answer);
   53:   } elsif ( $target eq 'grade') {
   54:     if ( defined $ENV{'form.submit'}) {
   55:       my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   56:       if ( $response =~ /[^\s]/) {
   57: 	my $id = $Apache::inputtags::response['-1'];
   58: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
   59: 	&Apache::lonxml::debug("submitted a $response<br>\n");
   60: 	if ($response == $answer) {
   61: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
   62: 	} else {
   63: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
   64: 	}
   65:       }
   66:     }
   67:   }
   68:   return $result;
   69: }
   70: 
   71: sub displayfoils {
   72:   my ($max,$answer)=@_;
   73:   my @names = @{ $Apache::response::foilgroup{'names'} };
   74:   my @truelist;
   75:   my @falselist;
   76:   my $result;
   77:   my $name;
   78: 
   79:   foreach $name (@names) {
   80:     #result.="<br><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
   81:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
   82:       push (@truelist,$name);
   83:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
   84:       push (@falselist,$name);
   85:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
   86:     } else {
   87:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
   88:     }
   89:   }
   90:   my $whichtrue = rand $#truelist;
   91:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
   92:   my @whichfalse =();
   93:   while ((($#whichfalse+2) < $max) && ($#falselist > -1)) {
   94:     my $afalse=rand $#falselist;
   95:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
   96:     $afalse=splice(@falselist,$afalse,1);
   97:     &Apache::lonxml::debug("Picked $afalse");
   98:     push (@whichfalse,$afalse);
   99:   }
  100:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  101:   my $temp=0;
  102:   &Apache::lonxml::debug("the true statement is $answer");
  103:   foreach $name (@whichfalse) {
  104:     $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\">".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  105:     $temp++;
  106:   }
  107:   return $result."<br />";
  108: }
  109: 
  110: sub start_foil {
  111:   $Apache::lonxml::redirection--;
  112:   return '';
  113: }
  114: 
  115: sub end_foil {
  116:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  117:   if ($target eq 'web' || $target eq 'grade') {
  118:     my $args ='';
  119:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  120:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  121:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  122:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  123:     $Apache::response::foilgroup{"$name.value"} = $value;
  124:     $Apache::response::foilgroup{"$name.text"} = $Apache::lonxml::outputstack;
  125:   }
  126:   
  127:   $Apache::lonxml::redirection++;
  128:   if ($Apache::lonxml::redirection == 1) { 
  129:     $Apache::lonxml::outputstack=''; 
  130:   }
  131:   return '';
  132: }
  133: 
  134: 1;
  135: __END__
  136:  

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