File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.13: download - view: text, annotated - select for diffs
Thu Feb 22 00:49:03 2001 UTC (23 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changed redirection, and output stack to work more intelligently, checkount radiobuttonresponse for a good example, should have to muck with @Apache::lonxml::outputstack and $Apache::lonxml::redirection directly use startredirection and enredirection

    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:   return '';
   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: sub end_foilgroup {
   42:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   43:   
   44:   my $result;
   45:   if ($target ne 'meta') {
   46:     my $name;
   47:     &setrandomnumber();
   48:     my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
   49:     my $count=0;
   50:     # we will add in 1 of the true statements
   51:     if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
   52:     my $answer = int(rand ($count));
   53:     &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
   54:     if ($target eq 'web') {
   55:       $result=&displayfoils($max,$answer);
   56:     } elsif ( $target eq 'grade') {
   57:       if ( defined $ENV{'form.submitted'}) {
   58: 	my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   59: 	if ( $response =~ /[^\s]/) {
   60: 	  my $id = $Apache::inputtags::response['-1'];
   61: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
   62: 	  &Apache::lonxml::debug("submitted a $response<br />\n");
   63: 	  if ($response == $answer) {
   64: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
   65: 	  } else {
   66: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
   67: 	  }
   68: 	}
   69:       }
   70:     }
   71:   }
   72:   return $result;
   73: }
   74: 
   75: sub getfoilcounts {
   76:   my ($parstack,$safeeval)=@_;
   77:   my $rrargs ='';
   78:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
   79:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
   80:   my @names = @{ $Apache::response::foilgroup{'names'} };
   81:   my $truecnt=0;
   82:   my $falsecnt=0;
   83:   my $name;
   84: 
   85:   foreach $name (@names) {
   86:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
   87:       $truecnt++;
   88:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
   89:       $falsecnt++;
   90:     }
   91:   }
   92:   return ($truecnt,$falsecnt,$max);
   93: }
   94: 
   95: sub displayfoils {
   96:   my ($max,$answer)=@_;
   97:   my @names = @{ $Apache::response::foilgroup{'names'} };
   98:   my @truelist;
   99:   my @falselist;
  100:   my $result;
  101:   my $name;
  102: 
  103:   foreach $name (@names) {
  104:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  105:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  106:       push (@truelist,$name);
  107:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  108:       push (@falselist,$name);
  109:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  110:     } else {
  111:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
  112:     }
  113:   }
  114:   my $whichtrue = rand $#truelist;
  115:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  116:   my @whichfalse =();
  117:   while ((($#whichfalse+2) < $max) && ($#falselist > -1)) {
  118:     my $afalse=rand $#falselist;
  119:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  120:     $afalse=splice(@falselist,$afalse,1);
  121:     &Apache::lonxml::debug("Picked $afalse");
  122:     push (@whichfalse,$afalse);
  123:   }
  124:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  125:   &Apache::lonxml::debug("the true statement is $answer");
  126:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  127:     foreach $name (@whichfalse) {
  128:       $result.="<br />";
  129:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  130: 	$result.='Correct';
  131:       } else {
  132: 	$result.='Incorrect';
  133:       }
  134:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  135:     }
  136:   } else {
  137:     my $temp=0;
  138:     foreach $name (@whichfalse) {
  139:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\">".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  140:       $temp++;
  141:     }
  142:   }
  143:   return $result."<br />";
  144: }
  145: 
  146: sub start_foil {
  147:   &Apache::lonxml::startredirection;
  148:   return '';
  149: }
  150: 
  151: sub end_foil {
  152:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  153:   my $foil=&Apache::lonxml::endredirection;
  154:   if ($target eq 'web' || $target eq 'grade') {
  155:     my $args ='';
  156:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  157:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  158:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  159:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  160:     $Apache::response::foilgroup{"$name.value"} = $value;
  161:     $Apache::response::foilgroup{"$name.text"} = $foil;
  162:   }
  163:   return '';
  164: }
  165: 
  166: 1;
  167: __END__
  168:  

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