File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.7: download - view: text, annotated - select for diffs
Tue May 15 20:48:43 2001 UTC (23 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changes to allow tag namespaces

    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: 	foreach $name (@whichopt) {
   66: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
   67: 	  $allresponse.="$response:";
   68: 	  if ( $response =~ /[^\s]/) {
   69: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
   70: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
   71: 	    if ($value eq $response) {$right++;} else {$wrong++;}
   72: 	  }
   73: 	  $temp++;
   74: 	}
   75: 	my $id = $Apache::inputtags::response['-1'];
   76: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
   77: 	&Apache::lonxml::debug("Got $right right and $wrong wrong");
   78: 	if ($wrong==0) {
   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:   return $result;
   87: }
   88: 
   89: sub getfoilcounts {
   90:   my ($parstack,$safeeval)=@_;
   91:   my $rrargs ='';
   92:   if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
   93:   my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
   94:   my $count = $#{ $Apache::response::foilgroup{'names'} };
   95:   return ($count,$max);
   96: }
   97: 
   98: sub whichfoils {
   99:   my ($max)=@_;
  100:   my @names = @{ $Apache::response::foilgroup{'names'} };
  101:   my @whichopt =();
  102:   while ((($#whichopt) < $max) && ($#names > -1)) {
  103:     my $aopt=int rand $#names;
  104:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  105:     $aopt=splice(@names,$aopt,1);
  106:     &Apache::lonxml::debug("Picked $aopt");
  107:     push (@whichopt,$aopt);
  108:   }
  109:   return @whichopt;
  110: }
  111: 
  112: sub displayfoils {
  113:   my ($max,@opt)=@_;
  114:   my @names = @{ $Apache::response::foilgroup{'names'} };
  115:   my @truelist;
  116:   my @falselist;
  117:   my $result;
  118:   my $name;
  119:   my @whichopt = &whichfoils($max);
  120:   my $optionlist="<option></option>\n";
  121:   my $option;
  122:   foreach $option (@opt) {
  123:     $optionlist.="<option>$option</option>\n";
  124:   }
  125:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  126:     foreach $name (@whichopt) {
  127:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  128: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
  129:     }
  130:   } else {
  131:     my $temp=1;
  132:     foreach $name (@whichopt) {
  133:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
  134: 	.$optionlist
  135: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  136:       $temp++;
  137:     }
  138:   }
  139:   return $result."<br />";
  140: }
  141: 
  142: 
  143: sub start_conceptgroup {
  144:   $Apache::optionresponse::conceptgroup=1;  
  145:   %Apache::response::conceptgroup={};
  146:   return '';
  147: }
  148: 
  149: sub end_conceptgroup {
  150:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  151:   $Apache::optionresponse::conceptgroup=0;  
  152:   if ($target eq 'web' || $target eq 'grade') {
  153:     my @names = @{ $Apache::response::conceptgroup{'names'} };
  154:     my $pick=int rand $#names+1;
  155:     my $name=$names[$pick];
  156:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  157:     $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
  158:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  159:     my $args;
  160:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  161:     my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval);
  162:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  163:     &Apache::lonxml::debug("Selecting $name in $concept");
  164:   }
  165:   return '';
  166: }
  167: 
  168: sub start_foil {
  169:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  170:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  171:   return '';
  172: }
  173: 
  174: sub end_foil {
  175:   my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
  176:   my $text ='';
  177:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  178:   if ($target eq 'web' || $target eq 'grade') {
  179:     my $args ='';
  180:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  181:     my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
  182:     if ($value ne 'unused') {
  183:       my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
  184:       if ( $Apache::optionresponse::conceptgroup ) {
  185: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  186: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  187: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  188:       } else {
  189: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  190: 	$Apache::response::foilgroup{"$name.value"} = $value;
  191: 	$Apache::response::foilgroup{"$name.text"} = $text;
  192:       }
  193:     }
  194:   }
  195:   if ($target eq 'edit') {
  196:     my $args ='';
  197:     if ( $#$parstack > 1 ) { $args=$$parstack['-2']; }
  198:     my $options=&Apache::run::run("{$args;".'return $options}',$safeeval);
  199:     if (!$options && $#$parstack > 2 ) { $args=$$parstack['-2']; }
  200:     my @opt;
  201:     #eval '@opt ='.
  202:   }
  203:   return '';
  204: }
  205: 
  206: sub insert_foil {
  207:   return '
  208: <foil name="" value="">
  209: </foil>';
  210: }
  211: 1;
  212: __END__
  213:  

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