File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.1: download - view: text, annotated - select for diffs
Fri Jan 19 21:44:51 2001 UTC (23 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- new response type <optionresponse>

    1: # The LearningOnline Network with CAPA
    2: # option list style responses
    3: 
    4: package Apache::optionresponse;
    5: use strict;
    6: 
    7: sub BEGIN {
    8:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
    9: }
   10: 
   11: sub start_optionresponse {
   12:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   13:   #when in a radiobutton response use these
   14:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil'));
   15:   my $id = &Apache::response::start_response($parstack,$safeeval);
   16:   return '';
   17: }
   18: 
   19: sub end_optionresponse {
   20:   &Apache::response::end_response;
   21:   return '';
   22: }
   23: 
   24: %Apache::response::foilgroup={};
   25: sub start_foilgroup {
   26:   %Apache::response::foilgroup={};
   27:   return '';
   28: }
   29: 
   30: sub setrandomnumber {
   31:   my $rndseed=&Apache::lonnet::rndseed();
   32:   $rndseed=unpack("%32i",$rndseed);
   33:   $rndseed=$rndseed
   34:     +&Apache::lonnet::numval($Apache::inputtags::part)
   35:       +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
   36:   srand($rndseed);
   37:   return '';
   38: }
   39: 
   40: sub end_foilgroup {
   41:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
   42:   
   43:   my $result;
   44:   if ($target ne 'meta') {
   45:     my $name;
   46:     &setrandomnumber();
   47:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
   48:     if ($count>$max) { $count=$max } 
   49:     &Apache::lonxml::debug("Count is $count from $max");
   50:     my $args ='';
   51:     if ( $#$parstack > 0 ) { $args=$$parstack['-1']; }
   52:     my @opt;
   53:     eval '@opt ='.&Apache::run::run("{$args;".'return $options}',$safeeval);
   54:     if ($target eq 'web') {
   55:       $result=&displayfoils($count,@opt);
   56:     } elsif ( $target eq 'grade') {
   57:       if ( defined $ENV{'form.submitted'}) {
   58: 	my @whichopt = &whichfoils($count);
   59: 	my $temp=1;my $name;
   60: 	my $allresponse;
   61: 	my $right=0;
   62: 	my $wrong=0;
   63: 	foreach $name (@whichopt) {
   64: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
   65: 	  $allresponse.="$response:";
   66: 	  if ( $response =~ /[^\s]/) {
   67: 	    &Apache::lonxml::debug("submitted a $response<br>\n");
   68: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
   69: 	    if ($value eq $response) {$right++;} else {$wrong++;}
   70: 	  }
   71: 	  $temp++;
   72: 	}
   73: 	my $id = $Apache::inputtags::response['-1'];
   74: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
   75: 	&Apache::lonxml::debug("Got $right right and $wrong wrong");
   76: 	if ($wrong==0) {
   77: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
   78: 	} else {
   79: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
   80: 	}
   81:       }
   82:     }
   83:   }
   84:   return $result;
   85: }
   86: 
   87: sub getfoilcounts {
   88:   my ($parstack,$safeeval)=@_;
   89:   my $rrargs ='';
   90:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
   91:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
   92:   my $count = $#{ $Apache::response::foilgroup{'names'} };
   93:   return ($count,$max);
   94: }
   95: 
   96: sub whichfoils {
   97:   my ($max)=@_;
   98:   my @names = @{ $Apache::response::foilgroup{'names'} };
   99:   my @whichopt =();
  100:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
  101:     my $aopt=int rand $#names;
  102:     &Apache::lonxml::debug("From $#names elms, picking $aopt");
  103:     $aopt=splice(@names,$aopt,1);
  104:     &Apache::lonxml::debug("Picked $aopt");
  105:     push (@whichopt,$aopt);
  106:   }
  107:   return @whichopt;
  108: }
  109: 
  110: sub displayfoils {
  111:   my ($max,@opt)=@_;
  112:   my @names = @{ $Apache::response::foilgroup{'names'} };
  113:   my @truelist;
  114:   my @falselist;
  115:   my $result;
  116:   my $name;
  117:   my @whichopt = &whichfoils($max);
  118:   my $optionlist="<option></option>\n";
  119:   my $option;
  120:   foreach $option (@opt) {
  121:     $optionlist.="<option>$option</option>\n";
  122:   }
  123:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  124:     foreach $name (@whichopt) {
  125:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  126: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
  127:     }
  128:   } else {
  129:     my $temp=1;
  130:     foreach $name (@whichopt) {
  131:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
  132: 	.$optionlist
  133: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  134:       $temp++;
  135:     }
  136:   }
  137:   return $result."<br />";
  138: }
  139: 
  140: sub start_foil {
  141:   $Apache::lonxml::redirection--;
  142:   return '';
  143: }
  144: 
  145: sub end_foil {
  146:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  147:   if ($target eq 'web' || $target eq 'grade') {
  148:     my $args ='';
  149:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  150:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  151:     if ($value ne 'unused') {
  152:       my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  153:       push @{ $Apache::response::foilgroup{'names'} }, $name;
  154:       $Apache::response::foilgroup{"$name.value"} = $value;
  155:       $Apache::response::foilgroup{"$name.text"} = $Apache::lonxml::outputstack;
  156:     }
  157:   }
  158:   
  159:   $Apache::lonxml::redirection++;
  160:   if ($Apache::lonxml::redirection == 1) { 
  161:     $Apache::lonxml::outputstack=''; 
  162:   }
  163:   return '';
  164: }
  165: 
  166: 1;
  167: __END__
  168:  

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