File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.28: download - view: text, annotated - select for diffs
Fri Dec 7 23:58:35 2001 UTC (22 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- restores previous submission
- notes blank answers and doesn't grade them
- notes previous submissions and doesn't charge for them

    1: # The LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.28 2001/12/07 23:58:35 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 2/21 Guy
   29: package Apache::optionresponse;
   30: use strict;
   31: use Apache::response;
   32: 
   33: sub BEGIN {
   34:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
   35: }
   36: 
   37: sub start_optionresponse {
   38:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   39:   my $result='';
   40:   #when in a radiobutton response use these
   41:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
   42:   push (@Apache::lonxml::namespace,'optionresponse');
   43:   my $id = &Apache::response::start_response($parstack,$safeeval);
   44:   if ($target eq 'edit') {
   45:     $result.=&Apache::edit::start_table($token).
   46:       "<tr><td>Multiple Option Response Question</td><td>Delete:".
   47: 	&Apache::edit::deletelist($target,$token)
   48: 	  ."</td><td>&nbsp;</td></tr><tr><td colspan=\"3\">\n";
   49:     $result.=&Apache::edit::text_arg('Max Number Of Foils:','max',$token,'4').
   50:       "</td></tr>";
   51:     $result.="<tr><td colspan=\"3\">\n";
   52:   }
   53:   if ($target eq 'modified') {
   54:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   55: 						 $safeeval,'max');
   56:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   57:   }
   58:   if ($target eq 'meta') {
   59:     $result=&Apache::response::meta_package_write('optionresponse');
   60:   }
   61:   return $result;
   62: }
   63: 
   64: sub end_optionresponse {
   65:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   66:   &Apache::response::end_response;
   67:   pop @Apache::lonxml::namespace;
   68:   my $result;
   69:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   70:   return $result;
   71: }
   72: 
   73: %Apache::response::foilgroup={};
   74: sub start_foilgroup {
   75:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   76: 
   77:   my $result='';
   78:   %Apache::response::foilgroup={};
   79:   $Apache::optionresponse::conceptgroup=0;
   80:   &Apache::response::setrandomnumber();
   81:   if ($target eq 'edit') {
   82:     my $optionlist="<option></option>\n";
   83:     my $option;
   84:     my @opt;
   85:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
   86:     my $count=1;
   87:     foreach $option (@opt) {
   88:       $optionlist.="<option value=\"$count\">$option</option>\n";
   89:       $count++;
   90:     }
   91:     my $insertlist=&Apache::edit::insertlist($target,$token);
   92:     $result.=&Apache::edit::start_table($token);
   93:     $result.= (<<ENDTABLE);
   94:       <tr><td>Select Options</td>
   95:         <td>
   96: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
   97:         </td>
   98:         <td>Delete an Option:
   99: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
  100:         </td>
  101:      </tr>
  102:      <tr><td colspan="3">$insertlist<br />
  103: ENDTABLE
  104:   }
  105:   if ($target eq 'modified') {
  106:     my @options;
  107:     my $optchanged=0;
  108:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  109:     if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
  110:       my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
  111:       &Apache::lonxml::debug("Deleting :$delopt:");
  112:       splice(@options,$delopt-1,1);
  113:       $optchanged=1;
  114:     }
  115:     if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
  116:       my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
  117:       if ($options[0]) {
  118: 	push(@options,$newopt);
  119:       } else {
  120: 	$options[0]=$newopt;
  121:       }
  122:       $optchanged=1;
  123:     }
  124:     if ($optchanged) {
  125:       $result = "<foilgroup options=\"(";
  126:       foreach my $option (@options) {
  127: 	$option=~s/\'/\\\'/;
  128: 	&Apache::lonxml::debug("adding option :$option:");
  129: 	$result .="'".$option."',";
  130:       }
  131:       chop $result;
  132:       $result.=')">';
  133:     } # else nothing changed so just use the default mechanism
  134:   }
  135:   return $result;
  136: }
  137: 
  138: sub end_foilgroup {
  139:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  140:   
  141:   my $result;
  142:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
  143:     my $name;
  144:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  145:     if ($count>$max) { $count=$max } 
  146:     &Apache::lonxml::debug("Count is $count from $max");
  147:     my @opt;
  148:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  149:     &Apache::lonxml::debug("Options are $#opt");
  150:     if ($target eq 'web') {
  151:       $result.=&displayfoils($count,@opt);
  152:     } elsif ( $target eq 'answer') {
  153:       $result.=&displayanswers($count,@opt);
  154:     } elsif ( $target eq 'grade') {
  155:       if ( defined $ENV{'form.submitted'}) {
  156: 	my @whichopt = &whichfoils($count);
  157: 	my $temp=1;my $name;
  158: 	my %responsehash;
  159: 	my $right=0;
  160: 	my $wrong=0;
  161: 	my $ignored=0;
  162: 	foreach $name (@whichopt) {
  163: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
  164: 	  $responsehash{$name}=$response;
  165: 	  if ( $response =~ /[^\s]/) {
  166: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
  167: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  168: 	    if ($value eq $response) {$right++;} else {$wrong++;}
  169: 	  } else {
  170: 	    $ignored++;
  171: 	  }
  172: 	  $temp++;
  173: 	}
  174: 	my $part=$Apache::inputtags::part;
  175: 	my $id = $Apache::inputtags::response['-1'];
  176: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  177: 	my %previous=&Apache::response::check_for_previous($responsestr,
  178: 							   $part,$id);
  179: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  180: 	my $ad;
  181: 	if ($wrong==0 && $ignored==0) {
  182: 	  $ad='EXACT_ANS';
  183: 	} elsif ($wrong==0 && $right==0) {
  184: 	  #nothing submitted
  185: 	} else {
  186: 	  $ad='INCORRECT';
  187: 	}
  188: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  189: 	  $responsestr;
  190: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
  191: 	  $ad;
  192: 	&Apache::response::handle_previous(\%previous,$ad);
  193:       }
  194:     }
  195:   } elsif ($target eq 'edit') {
  196:     $result.=&Apache::edit::end_table();
  197:   }
  198:   return $result;
  199: }
  200: 
  201: sub getfoilcounts {
  202:   my ($parstack,$safeeval)=@_;
  203:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  204:   # +1 since instructors will count from 1
  205:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  206:   return ($count,$max);
  207: }
  208: 
  209: sub whichfoils {
  210:   my ($max)=@_;
  211:   my @names = @{ $Apache::response::foilgroup{'names'} };
  212:   my @whichopt =();
  213:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
  214:     &Apache::lonxml::debug("Have $#whichopt max is $max");
  215:     my $aopt=int(rand($#names+1));
  216:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  217:     $aopt=splice(@names,$aopt,1);
  218:     &Apache::lonxml::debug("Picked $aopt");
  219:     push (@whichopt,$aopt);
  220:   }
  221:   return @whichopt;
  222: }
  223: 
  224: sub displayanswers {
  225:   my ($max,@opt)=@_;
  226:   my @names = @{ $Apache::response::foilgroup{'names'} };
  227:   my @whichopt = &whichfoils($max);
  228:   my $result=&Apache::response::answer_header('optionresponse');
  229:   foreach my $name (@whichopt) {
  230:     $result.=&Apache::response::answer_part('optionresponse',
  231: 		     $Apache::response::foilgroup{$name.'.value'})
  232:   }
  233:   $result.=&Apache::response::answer_footer('optionresponse');
  234:   return $result;
  235: }
  236: 
  237: sub displayfoils {
  238:   my ($max,@opt)=@_;
  239:   my @names = @{ $Apache::response::foilgroup{'names'} };
  240:   my @truelist;
  241:   my @falselist;
  242:   my $result;
  243:   my $name;
  244:   my @whichopt = &whichfoils($max);
  245:   my $part=$Apache::inputtags::part;
  246:   my $id=$Apache::inputtags::response[-1];
  247:   if (($Apache::lonhomework::history{"resource.$part.solved"} =~ /^correct/)  || ($Apache::inputtags::status[-1] eq  'SHOW_ANSWER')) {
  248:     foreach $name (@whichopt) {
  249:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  250: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
  251:     }
  252:   } else {
  253:     my $temp=1;
  254:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  255:     foreach $name (@whichopt) {
  256:       my $lastopt=$lastresponse{$name};
  257:       my $optionlist="<option></option>\n";
  258:       my $option;
  259:       foreach $option (@opt) {
  260: 	if ($option eq $lastopt) {
  261: 	  $optionlist.="<option selected=\"on\">$option</option>\n";
  262: 	} else {
  263: 	  $optionlist.="<option>$option</option>\n";
  264: 	}
  265:       }
  266:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
  267: 	.$optionlist
  268: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  269:       $temp++;
  270:     }
  271:   }
  272:   return $result."<br />";
  273: }
  274: 
  275: 
  276: sub start_conceptgroup {
  277:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  278:   $Apache::optionresponse::conceptgroup=1;
  279:   %Apache::response::conceptgroup={};
  280:   my $result;
  281:   if ($target eq 'edit') {
  282:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  283:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50')."</td></tr>";
  284:     $result.="<tr><td colspan=\"3\">\n";
  285:   }
  286:   if ($target eq 'modified') {
  287:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  288:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  289:   }
  290:   return $result;
  291: }
  292: 
  293: sub end_conceptgroup {
  294:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  295:   $Apache::optionresponse::conceptgroup=0;
  296:   my $result='';
  297:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  298:     #if not there aren't any foils to display and thus no question
  299:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  300:       my @names = @{ $Apache::response::conceptgroup{'names'} };
  301:       my $pick=int rand $#names+1;
  302:       my $name=$names[$pick];
  303:       push @{ $Apache::response::foilgroup{'names'} }, $name;
  304:       $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
  305:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  306:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  307:       $Apache::response::foilgroup{"$name.concept"} = $concept;
  308:       &Apache::lonxml::debug("Selecting $name in $concept");
  309:     }
  310:   }
  311:   if ($target eq 'edit') {
  312:     $result=&Apache::edit::end_table();
  313:   }
  314:   return $result;
  315: }
  316: 
  317: sub insert_conceptgroup {
  318:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  319:   return $result;
  320: }
  321: 
  322: sub start_foil {
  323:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  324:   my $result='';
  325:   if ($target eq 'web') {
  326:     &Apache::lonxml::startredirection;
  327:   } elsif ($target eq 'edit') {
  328:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  329:     my $level='-2';
  330:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  331:     my @opt;
  332:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
  333:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  334:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',(@opt)],$token,'15');
  335:     $result .= '</td></tr><tr><td colspan="3">';
  336:   } elsif ($target eq 'modified') {
  337:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'value','name');
  338:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  339:   }
  340:   return $result;
  341: }
  342: 
  343: sub end_foil {
  344:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  345:   my $text ='';
  346:   my $result = '';
  347:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  348:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  349:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  350:     if ($value ne 'unused') {
  351:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  352:       &Apache::lonxml::debug("Got a name of :$name:");
  353:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  354:       &Apache::lonxml::debug("Using a name of :$name:");
  355:       if ( $Apache::optionresponse::conceptgroup ) {
  356: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  357: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  358: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  359:       } else {
  360: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  361: 	$Apache::response::foilgroup{"$name.value"} = $value;
  362: 	$Apache::response::foilgroup{"$name.text"} = $text;
  363:       }
  364:     }
  365:   }
  366:   if ($target eq 'edit') {
  367:     $result.= &Apache::edit::tag_end($target,$token,'');
  368:   }
  369:   return $result;
  370: }
  371: 
  372: sub insert_foil {
  373:   return '
  374: <foil name="" value="unused">
  375: <startouttext />
  376: <endouttext />
  377: </foil>';
  378: }
  379: 1;
  380: __END__
  381:  

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