Annotation of loncom/homework/optionresponse.pm, revision 1.4

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # option list style responses
1.4     ! albertel    3: # 2/21 Guy
1.1       albertel    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
1.2       albertel   14:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
1.1       albertel   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]/) {
1.3       albertel   67: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
1.1       albertel   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: 
1.2       albertel  140: sub start_conceptgroup {
                    141: }
                    142: 
                    143: sub end_conceptgroup {
                    144: }
                    145: 
1.1       albertel  146: sub start_foil {
1.2       albertel  147:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.4     ! albertel  148:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
1.1       albertel  149:   return '';
                    150: }
                    151: 
                    152: sub end_foil {
                    153:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.4     ! albertel  154:   my $text ='';
        !           155:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.1       albertel  156:   if ($target eq 'web' || $target eq 'grade') {
                    157:     my $args ='';
                    158:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    159:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
                    160:     if ($value ne 'unused') {
                    161:       my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
                    162:       push @{ $Apache::response::foilgroup{'names'} }, $name;
                    163:       $Apache::response::foilgroup{"$name.value"} = $value;
1.4     ! albertel  164:       $Apache::response::foilgroup{"$name.text"} = $text;
1.2       albertel  165:     }
1.1       albertel  166:   }
                    167:   return '';
                    168: }
                    169: 
                    170: 1;
                    171: __END__
                    172:  

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