File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.8: download - view: text, annotated - select for diffs
Mon May 21 19:45:28 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- bug allowed blank answers to be bounted as correct

    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:   push (@Apache::lonxml::namespace,'optionresponse');
   17:   my $id = &Apache::response::start_response($parstack,$safeeval);
   18:   return '';
   19: }
   20: 
   21: sub end_optionresponse {
   22:   &Apache::response::end_response;
   23:   pop @Apache::lonxml::namespace;
   24:   return '';
   25: }
   26: 
   27: sub insert_optionresponse {
   28:   return '
   29: <optionresponse max="10">
   30:     <foilgroup options=\"\">
   31:     </foilgroup>
   32: </optionresponse>';
   33: }
   34: 
   35: %Apache::response::foilgroup={};
   36: sub start_foilgroup {
   37:   %Apache::response::foilgroup={};
   38:   $Apache::optionresponse::conceptgroup=0;  
   39:   &Apache::response::setrandomnumber();
   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 ($count,$max) = &getfoilcounts($parstack,$safeeval);
   50:     if ($count>$max) { $count=$max } 
   51:     &Apache::lonxml::debug("Count is $count from $max");
   52:     my $args ='';
   53:     if ( $#$parstack > 0 ) { $args=$$parstack['-1']; }
   54:     my @opt;
   55:     eval '@opt ='.&Apache::run::run("{$args;".'return $options}',$safeeval);
   56:     if ($target eq 'web') {
   57:       $result=&displayfoils($count,@opt);
   58:     } elsif ( $target eq 'grade') {
   59:       if ( defined $ENV{'form.submitted'}) {
   60: 	my @whichopt = &whichfoils($count);
   61: 	my $temp=1;my $name;
   62: 	my $allresponse;
   63: 	my $right=0;
   64: 	my $wrong=0;
   65: 	my $ignored=0;
   66: 	foreach $name (@whichopt) {
   67: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
   68: 	  $allresponse.="$response:";
   69: 	  if ( $response =~ /[^\s]/) {
   70: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
   71: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
   72: 	    if ($value eq $response) {$right++;} else {$wrong++;}
   73: 	  } else {
   74: 	    $ignored++;
   75: 	  }
   76: 	  $temp++;
   77: 	}
   78: 	my $id = $Apache::inputtags::response['-1'];
   79: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
   80: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
   81: 	if ($wrong==0 && $ignored==0) {
   82: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
   83: 	} else {
   84: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
   85: 	}
   86:       }
   87:     }
   88:   }
   89:   return $result;
   90: }
   91: 
   92: sub getfoilcounts {
   93:   my ($parstack,$safeeval)=@_;
   94:   my $rrargs ='';
   95:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
   96:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
   97:   my $count = $#{ $Apache::response::foilgroup{'names'} };
   98:   return ($count,$max);
   99: }
  100: 
  101: sub whichfoils {
  102:   my ($max)=@_;
  103:   my @names = @{ $Apache::response::foilgroup{'names'} };
  104:   my @whichopt =();
  105:   while ((($#whichopt) < $max) && ($#names > -1)) {
  106:     my $aopt=int rand $#names;
  107:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  108:     $aopt=splice(@names,$aopt,1);
  109:     &Apache::lonxml::debug("Picked $aopt");
  110:     push (@whichopt,$aopt);
  111:   }
  112:   return @whichopt;
  113: }
  114: 
  115: sub displayfoils {
  116:   my ($max,@opt)=@_;
  117:   my @names = @{ $Apache::response::foilgroup{'names'} };
  118:   my @truelist;
  119:   my @falselist;
  120:   my $result;
  121:   my $name;
  122:   my @whichopt = &whichfoils($max);
  123:   my $optionlist="<option></option>\n";
  124:   my $option;
  125:   foreach $option (@opt) {
  126:     $optionlist.="<option>$option</option>\n";
  127:   }
  128:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  129:     foreach $name (@whichopt) {
  130:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  131: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
  132:     }
  133:   } else {
  134:     my $temp=1;
  135:     foreach $name (@whichopt) {
  136:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
  137: 	.$optionlist
  138: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  139:       $temp++;
  140:     }
  141:   }
  142:   return $result."<br />";
  143: }
  144: 
  145: 
  146: sub start_conceptgroup {
  147:   $Apache::optionresponse::conceptgroup=1;  
  148:   %Apache::response::conceptgroup={};
  149:   return '';
  150: }
  151: 
  152: sub end_conceptgroup {
  153:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  154:   $Apache::optionresponse::conceptgroup=0;  
  155:   if ($target eq 'web' || $target eq 'grade') {
  156:     my @names = @{ $Apache::response::conceptgroup{'names'} };
  157:     my $pick=int rand $#names+1;
  158:     my $name=$names[$pick];
  159:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  160:     $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
  161:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  162:     my $args;
  163:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  164:     my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval);
  165:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  166:     &Apache::lonxml::debug("Selecting $name in $concept");
  167:   }
  168:   return '';
  169: }
  170: 
  171: sub start_foil {
  172:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  173:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  174:   return '';
  175: }
  176: 
  177: sub end_foil {
  178:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  179:   my $text ='';
  180:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  181:   if ($target eq 'web' || $target eq 'grade') {
  182:     my $args ='';
  183:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  184:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  185:     if ($value ne 'unused') {
  186:       my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  187:       if ( $Apache::optionresponse::conceptgroup ) {
  188: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  189: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  190: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  191:       } else {
  192: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  193: 	$Apache::response::foilgroup{"$name.value"} = $value;
  194: 	$Apache::response::foilgroup{"$name.text"} = $text;
  195:       }
  196:     }
  197:   }
  198:   if ($target eq 'edit') {
  199:     my $args ='';
  200:     if ( $#$parstack > 1 ) { $args=$$parstack['-2']; }
  201:     my $options=&Apache::run::run("{$args;".'return $options}',$safeeval);
  202:     if (!$options && $#$parstack > 2 ) { $args=$$parstack['-2']; }
  203:     my @opt;
  204:     #eval '@opt ='.
  205:   }
  206:   return '';
  207: }
  208: 
  209: sub insert_foil {
  210:   return '
  211: <foil name="" value="">
  212: </foil>';
  213: }
  214: 1;
  215: __END__
  216:  

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