File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.6: download - view: text, annotated - select for diffs
Fri May 4 21:19:37 2001 UTC (23 years, 1 month 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: # option list style responses
    3: # 2/21 Guy
    4: package Apache::optionresponse;
    5: use strict;
    6: use Apache::response;
    7: 
    8: sub BEGIN {
    9:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
   10: }
   11: 
   12: sub start_optionresponse {
   13:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   14:   #when in a radiobutton response use these
   15:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
   16:   my $id = &Apache::response::start_response($parstack,$safeeval);
   17:   return '';
   18: }
   19: 
   20: sub end_optionresponse {
   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 end_foilgroup {
   34:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   35:   
   36:   my $result;
   37:   if ($target ne 'meta') {
   38:     my $name;
   39:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
   40:     if ($count>$max) { $count=$max } 
   41:     &Apache::lonxml::debug("Count is $count from $max");
   42:     my $args ='';
   43:     if ( $#$parstack > 0 ) { $args=$$parstack['-1']; }
   44:     my @opt;
   45:     eval '@opt ='.&Apache::run::run("{$args;".'return $options}',$safeeval);
   46:     if ($target eq 'web') {
   47:       $result=&displayfoils($count,@opt);
   48:     } elsif ( $target eq 'grade') {
   49:       if ( defined $ENV{'form.submitted'}) {
   50: 	my @whichopt = &whichfoils($count);
   51: 	my $temp=1;my $name;
   52: 	my $allresponse;
   53: 	my $right=0;
   54: 	my $wrong=0;
   55: 	foreach $name (@whichopt) {
   56: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
   57: 	  $allresponse.="$response:";
   58: 	  if ( $response =~ /[^\s]/) {
   59: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
   60: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
   61: 	    if ($value eq $response) {$right++;} else {$wrong++;}
   62: 	  }
   63: 	  $temp++;
   64: 	}
   65: 	my $id = $Apache::inputtags::response['-1'];
   66: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
   67: 	&Apache::lonxml::debug("Got $right right and $wrong wrong");
   68: 	if ($wrong==0) {
   69: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
   70: 	} else {
   71: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
   72: 	}
   73:       }
   74:     }
   75:   }
   76:   return $result;
   77: }
   78: 
   79: sub getfoilcounts {
   80:   my ($parstack,$safeeval)=@_;
   81:   my $rrargs ='';
   82:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
   83:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
   84:   my $count = $#{ $Apache::response::foilgroup{'names'} };
   85:   return ($count,$max);
   86: }
   87: 
   88: sub whichfoils {
   89:   my ($max)=@_;
   90:   my @names = @{ $Apache::response::foilgroup{'names'} };
   91:   my @whichopt =();
   92:   while ((($#whichopt) < $max) && ($#names > -1)) {
   93:     my $aopt=int rand $#names;
   94:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
   95:     $aopt=splice(@names,$aopt,1);
   96:     &Apache::lonxml::debug("Picked $aopt");
   97:     push (@whichopt,$aopt);
   98:   }
   99:   return @whichopt;
  100: }
  101: 
  102: sub displayfoils {
  103:   my ($max,@opt)=@_;
  104:   my @names = @{ $Apache::response::foilgroup{'names'} };
  105:   my @truelist;
  106:   my @falselist;
  107:   my $result;
  108:   my $name;
  109:   my @whichopt = &whichfoils($max);
  110:   my $optionlist="<option></option>\n";
  111:   my $option;
  112:   foreach $option (@opt) {
  113:     $optionlist.="<option>$option</option>\n";
  114:   }
  115:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  116:     foreach $name (@whichopt) {
  117:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  118: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
  119:     }
  120:   } else {
  121:     my $temp=1;
  122:     foreach $name (@whichopt) {
  123:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
  124: 	.$optionlist
  125: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  126:       $temp++;
  127:     }
  128:   }
  129:   return $result."<br />";
  130: }
  131: 
  132: 
  133: sub start_conceptgroup {
  134:   $Apache::optionresponse::conceptgroup=1;  
  135:   %Apache::response::conceptgroup={};
  136:   return '';
  137: }
  138: 
  139: sub end_conceptgroup {
  140:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  141:   $Apache::optionresponse::conceptgroup=0;  
  142:   if ($target eq 'web' || $target eq 'grade') {
  143:     my @names = @{ $Apache::response::conceptgroup{'names'} };
  144:     my $pick=int rand $#names+1;
  145:     my $name=$names[$pick];
  146:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  147:     $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
  148:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  149:     my $args;
  150:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  151:     my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval);
  152:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  153:     &Apache::lonxml::debug("Selecting $name in $concept");
  154:   }
  155:   return '';
  156: }
  157: 
  158: sub start_foil {
  159:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  160:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  161:   return '';
  162: }
  163: 
  164: sub end_foil {
  165:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  166:   my $text ='';
  167:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  168:   if ($target eq 'web' || $target eq 'grade') {
  169:     my $args ='';
  170:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  171:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  172:     if ($value ne 'unused') {
  173:       my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  174:       if ( $Apache::optionresponse::conceptgroup ) {
  175: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  176: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  177: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  178:       } else {
  179: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  180: 	$Apache::response::foilgroup{"$name.value"} = $value;
  181: 	$Apache::response::foilgroup{"$name.text"} = $text;
  182:       }
  183:     }
  184:   }
  185:   if ($target eq 'edit') {
  186:     my $args ='';
  187:     if ( $#$parstack > 1 ) { $args=$$parstack['-2']; }
  188:     my $options=&Apache::run::run("{$args;".'return $options}',$safeeval);
  189:     if (!$options && $#$parstack > 2 ) { $args=$$parstack['-2']; }
  190:     my @opt;
  191:     #eval '@opt ='.
  192:   }
  193:   return '';
  194: }
  195: 
  196: 1;
  197: __END__
  198:  

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