File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.25: download - view: text, annotated - select for diffs
Fri Sep 14 20:14:46 2001 UTC (22 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
- prints out correct answers for answer mode parsing

    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,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   14:   my $result='';
   15:   #when in a radiobutton response use these
   16:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
   17:   push (@Apache::lonxml::namespace,'optionresponse');
   18:   my $id = &Apache::response::start_response($parstack,$safeeval);
   19:   if ($target eq 'edit') {
   20:     $result.=&Apache::edit::start_table($token).
   21:       "<tr><td>Multiple Option Response Question</td><td>Delete:".
   22: 	&Apache::edit::deletelist($target,$token)
   23: 	  ."</td><td>&nbsp;</td></tr><tr><td colspan=\"3\">\n";
   24:     $result.=&Apache::edit::text_arg('Max Number Of Foils:','max',$token,'4').
   25:       "</td></tr>";
   26:     $result.="<tr><td colspan=\"3\">\n";
   27:   }
   28:   if ($target eq 'modified') {
   29:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   30: 						 $safeeval,'max');
   31:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   32:   }
   33:   if ($target eq 'meta') {
   34:     $result=&Apache::response::meta_package_write('optionresponse');
   35:   }
   36:   return $result;
   37: }
   38: 
   39: sub end_optionresponse {
   40:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   41:   &Apache::response::end_response;
   42:   pop @Apache::lonxml::namespace;
   43:   my $result;
   44:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   45:   return $result;
   46: }
   47: 
   48: %Apache::response::foilgroup={};
   49: sub start_foilgroup {
   50:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   51: 
   52:   my $result='';
   53:   %Apache::response::foilgroup={};
   54:   $Apache::optionresponse::conceptgroup=0;
   55:   &Apache::response::setrandomnumber();
   56:   if ($target eq 'edit') {
   57:     my $optionlist="<option></option>\n";
   58:     my $option;
   59:     my @opt;
   60:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
   61:     my $count=1;
   62:     foreach $option (@opt) {
   63:       $optionlist.="<option value=\"$count\">$option</option>\n";
   64:       $count++;
   65:     }
   66:     my $insertlist=&Apache::edit::insertlist($target,$token);
   67:     $result.=&Apache::edit::start_table($token);
   68:     $result.= (<<ENDTABLE);
   69:       <tr><td>Select Options</td>
   70:         <td>
   71: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
   72:         </td>
   73:         <td>Delete an Option:
   74: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
   75:         </td>
   76:      </tr>
   77:      <tr><td colspan="3">$insertlist<br />
   78: ENDTABLE
   79:   }
   80:   if ($target eq 'modified') {
   81:     my @options;
   82:     my $optchanged=0;
   83:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
   84:     if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
   85:       my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
   86:       &Apache::lonxml::debug("Deleting :$delopt:");
   87:       splice(@options,$delopt-1,1);
   88:       $optchanged=1;
   89:     }
   90:     if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
   91:       my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
   92:       if ($options[0]) {
   93: 	push(@options,$newopt);
   94:       } else {
   95: 	$options[0]=$newopt;
   96:       }
   97:       $optchanged=1;
   98:     }
   99:     if ($optchanged) {
  100:       $result = "<foilgroup options=\"(";
  101:       foreach my $option (@options) {
  102: 	$option=~s/\'/\\\'/;
  103: 	&Apache::lonxml::debug("adding option :$option:");
  104: 	$result .="'".$option."',";
  105:       }
  106:       chop $result;
  107:       $result.=')">';
  108:     } # else nothing changed so just use the default mechanism
  109:   }
  110:   return $result;
  111: }
  112: 
  113: sub end_foilgroup {
  114:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  115:   
  116:   my $result;
  117:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
  118:     my $name;
  119:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  120:     if ($count>$max) { $count=$max } 
  121:     &Apache::lonxml::debug("Count is $count from $max");
  122:     my @opt;
  123:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  124:     &Apache::lonxml::debug("Options are $#opt");
  125:     if ($target eq 'web') {
  126:       $result.=&displayfoils($count,@opt);
  127:     } elsif ( $target eq 'answer') {
  128:       $result.=&displayanswers($count,@opt);
  129:     } elsif ( $target eq 'grade') {
  130:       if ( defined $ENV{'form.submitted'}) {
  131: 	my @whichopt = &whichfoils($count);
  132: 	my $temp=1;my $name;
  133: 	my $allresponse;
  134: 	my $right=0;
  135: 	my $wrong=0;
  136: 	my $ignored=0;
  137: 	foreach $name (@whichopt) {
  138: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
  139: 	  $allresponse.="$response:";
  140: 	  if ( $response =~ /[^\s]/) {
  141: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
  142: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  143: 	    if ($value eq $response) {$right++;} else {$wrong++;}
  144: 	  } else {
  145: 	    $ignored++;
  146: 	  }
  147: 	  $temp++;
  148: 	}
  149: 	my $id = $Apache::inputtags::response['-1'];
  150: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
  151: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  152: 	if ($wrong==0 && $ignored==0) {
  153: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
  154: 	} else {
  155: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
  156: 	}
  157:       }
  158:     }
  159:   } elsif ($target eq 'edit') {
  160:     $result.=&Apache::edit::end_table();
  161:   }
  162:   return $result;
  163: }
  164: 
  165: sub getfoilcounts {
  166:   my ($parstack,$safeeval)=@_;
  167:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  168:   # +1 since instructors will count from 1
  169:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  170:   return ($count,$max);
  171: }
  172: 
  173: sub whichfoils {
  174:   my ($max)=@_;
  175:   my @names = @{ $Apache::response::foilgroup{'names'} };
  176:   my @whichopt =();
  177:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
  178:     &Apache::lonxml::debug("Have $#whichopt max is $max");
  179:     my $aopt=int(rand($#names+1));
  180:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  181:     $aopt=splice(@names,$aopt,1);
  182:     &Apache::lonxml::debug("Picked $aopt");
  183:     push (@whichopt,$aopt);
  184:   }
  185:   return @whichopt;
  186: }
  187: 
  188: sub displayanswers {
  189:   my ($max,@opt)=@_;
  190:   my @names = @{ $Apache::response::foilgroup{'names'} };
  191:   my @whichopt = &whichfoils($max);
  192:   my $result=&Apache::response::answer_header('optionresponse');
  193:   foreach my $name (@whichopt) {
  194:     $result.=&Apache::response::answer_part('optionresponse',
  195: 		     $Apache::response::foilgroup{$name.'.value'})
  196:   }
  197:   $result.=&Apache::response::answer_footer('optionresponse');
  198:   return $result;
  199: }
  200: 
  201: sub displayfoils {
  202:   my ($max,@opt)=@_;
  203:   my @names = @{ $Apache::response::foilgroup{'names'} };
  204:   my @truelist;
  205:   my @falselist;
  206:   my $result;
  207:   my $name;
  208:   my @whichopt = &whichfoils($max);
  209:   my $optionlist="<option></option>\n";
  210:   my $option;
  211:   foreach $option (@opt) {
  212:     $optionlist.="<option>$option</option>\n";
  213:   }
  214:   if (($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/)  || ($Apache::inputtags::status[-1] eq  'SHOW_ANSWER')) {
  215:     foreach $name (@whichopt) {
  216:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  217: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
  218:     }
  219:   } else {
  220:     my $temp=1;
  221:     foreach $name (@whichopt) {
  222:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
  223: 	.$optionlist
  224: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  225:       $temp++;
  226:     }
  227:   }
  228:   return $result."<br />";
  229: }
  230: 
  231: 
  232: sub start_conceptgroup {
  233:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  234:   $Apache::optionresponse::conceptgroup=1;
  235:   %Apache::response::conceptgroup={};
  236:   my $result;
  237:   if ($target eq 'edit') {
  238:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  239:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50')."</td></tr>";
  240:     $result.="<tr><td colspan=\"3\">\n";
  241:   }
  242:   if ($target eq 'modified') {
  243:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  244:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  245:   }
  246:   return $result;
  247: }
  248: 
  249: sub end_conceptgroup {
  250:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  251:   $Apache::optionresponse::conceptgroup=0;
  252:   my $result='';
  253:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  254:     #if not there aren't any foils to display and thus no question
  255:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  256:       my @names = @{ $Apache::response::conceptgroup{'names'} };
  257:       my $pick=int rand $#names+1;
  258:       my $name=$names[$pick];
  259:       push @{ $Apache::response::foilgroup{'names'} }, $name;
  260:       $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
  261:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  262:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  263:       $Apache::response::foilgroup{"$name.concept"} = $concept;
  264:       &Apache::lonxml::debug("Selecting $name in $concept");
  265:     }
  266:   }
  267:   if ($target eq 'edit') {
  268:     $result=&Apache::edit::end_table();
  269:   }
  270:   return $result;
  271: }
  272: 
  273: sub insert_conceptgroup {
  274:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  275:   return $result;
  276: }
  277: 
  278: sub start_foil {
  279:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  280:   my $result='';
  281:   if ($target eq 'web') {
  282:     &Apache::lonxml::startredirection;
  283:   } elsif ($target eq 'edit') {
  284:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  285:     my $level='-2';
  286:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  287:     my @opt;
  288:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
  289:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  290:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',(@opt)],$token,'15');
  291:     $result .= '</td></tr><tr><td colspan="3">';
  292:   } elsif ($target eq 'modified') {
  293:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'value','name');
  294:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  295:   }
  296:   return $result;
  297: }
  298: 
  299: sub end_foil {
  300:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  301:   my $text ='';
  302:   my $result = '';
  303:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  304:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  305:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  306:     if ($value ne 'unused') {
  307:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  308:       &Apache::lonxml::debug("Got a name of :$name:");
  309:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  310:       &Apache::lonxml::debug("Using a name of :$name:");
  311:       if ( $Apache::optionresponse::conceptgroup ) {
  312: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  313: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  314: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  315:       } else {
  316: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  317: 	$Apache::response::foilgroup{"$name.value"} = $value;
  318: 	$Apache::response::foilgroup{"$name.text"} = $text;
  319:       }
  320:     }
  321:   }
  322:   if ($target eq 'edit') {
  323:     $result.= &Apache::edit::tag_end($target,$token,'');
  324:   }
  325:   return $result;
  326: }
  327: 
  328: sub insert_foil {
  329:   return '
  330: <foil name="" value="unused">
  331: <startouttext />
  332: <endouttext />
  333: </foil>';
  334: }
  335: 1;
  336: __END__
  337:  

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