File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.17: download - view: text, annotated - select for diffs
Fri May 4 21:19:37 2001 UTC (23 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- redid random number generator for <i/rb/o/response> tags
     - should be unstable in preview mode and more random in normal mode

    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:   &Apache::response::setrandomnumber();
   30:   return '';
   31: }
   32: 
   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: 
   47: sub end_foilgroup {
   48:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   49:   
   50:   my $result;
   51:   if ($target ne 'meta') {
   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: 	    }
   83: 	  }
   84: 	}
   85:       }
   86:     }
   87:   }
   88:   return $result;
   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);
  109: }
  110: 
  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\" ";
  122:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
  123:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  124:       $temp++;
  125:     }
  126:   }
  127:   return $result;
  128: }
  129: 
  130: sub displayfoils {
  131:   my ($max,$answer)=@_;
  132:   my @names = @{ $Apache::response::foilgroup{'names'} };
  133:   my @truelist;
  134:   my @falselist;
  135:   my $result;
  136:   my $name;
  137: 
  138:   foreach $name (@names) {
  139:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  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 {
  146:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
  147:     }
  148:   }
  149:   my $whichtrue = rand $#truelist;
  150:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  151:   my @whichfalse =();
  152:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
  153:     my $afalse=rand $#falselist;
  154:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  155:     $afalse=splice(@falselist,$afalse,1);
  156:     &Apache::lonxml::debug("Picked $afalse");
  157:     push (@whichfalse,$afalse);
  158:   }
  159:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  160:   &Apache::lonxml::debug("the true statement is $answer");
  161:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  162:     foreach $name (@whichfalse) {
  163:       $result.="<br />";
  164:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  165: 	$result.='Correct';
  166:       } else {
  167: 	$result.='Incorrect';
  168:       }
  169:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  170:     }
  171:   } else {
  172:     my $temp=0;
  173:     my $id=$Apache::inputtags::response['-1'];
  174:     my $part=$Apache::inputtags::part;
  175:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  176:     foreach $name (@whichfalse) {
  177:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  178:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
  179:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  180:       $temp++;
  181:     }
  182:   }
  183:   return $result."<br />";
  184: }
  185: 
  186: sub start_conceptgroup {
  187:   $Apache::optionresponse::conceptgroup=1;  
  188:   %Apache::response::conceptgroup={};
  189:   return '';
  190: }
  191: 
  192: sub end_conceptgroup {
  193:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  194:   $Apache::optionresponse::conceptgroup=0;  
  195:   if ($target eq 'web' || $target eq 'grade') {
  196:     my @names = @{ $Apache::response::conceptgroup{'names'} };
  197:     my $pick=int rand $#names+1;
  198:     my $name=$names[$pick];
  199:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  200:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  201:     $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
  202:     my $args;
  203:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  204:     my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval);
  205:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  206:     &Apache::lonxml::debug("Selecting $name in $concept");
  207:   }
  208:   return '';
  209: }
  210: 
  211: sub start_foil {
  212:   &Apache::lonxml::startredirection;
  213:   return '';
  214: }
  215: 
  216: sub end_foil {
  217:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  218:   my $foil=&Apache::lonxml::endredirection;
  219:   if ($target eq 'web' || $target eq 'grade') {
  220:     my $args ='';
  221:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  222:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  223:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  224:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  225:     $Apache::response::foilgroup{"$name.value"} = $value;
  226:     $Apache::response::foilgroup{"$name.text"} = $foil;
  227:   }
  228:   return '';
  229: }
  230: 
  231: 1;
  232: __END__
  233:  

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