File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.29: download - view: text, annotated - select for diffs
Fri Dec 14 23:00:52 2001 UTC (22 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- makes use of the new deregister mechanism
- radiobutton response handles previous in survey mode now.

    1: # The LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.29 2001/12/14 23:00:52 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 option 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:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
   69:   my $result;
   70:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   71:   return $result;
   72: }
   73: 
   74: %Apache::response::foilgroup={};
   75: sub start_foilgroup {
   76:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   77: 
   78:   my $result='';
   79:   %Apache::response::foilgroup={};
   80:   $Apache::optionresponse::conceptgroup=0;
   81:   &Apache::response::setrandomnumber();
   82:   if ($target eq 'edit') {
   83:     my $optionlist="<option></option>\n";
   84:     my $option;
   85:     my @opt;
   86:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
   87:     my $count=1;
   88:     foreach $option (@opt) {
   89:       $optionlist.="<option value=\"$count\">$option</option>\n";
   90:       $count++;
   91:     }
   92:     my $insertlist=&Apache::edit::insertlist($target,$token);
   93:     $result.=&Apache::edit::start_table($token);
   94:     $result.= (<<ENDTABLE);
   95:       <tr><td>Select Options</td>
   96:         <td>
   97: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
   98:         </td>
   99:         <td>Delete an Option:
  100: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
  101:         </td>
  102:      </tr>
  103:      <tr><td colspan="3">$insertlist<br />
  104: ENDTABLE
  105:   }
  106:   if ($target eq 'modified') {
  107:     my @options;
  108:     my $optchanged=0;
  109:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  110:     if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
  111:       my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
  112:       &Apache::lonxml::debug("Deleting :$delopt:");
  113:       splice(@options,$delopt-1,1);
  114:       $optchanged=1;
  115:     }
  116:     if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
  117:       my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
  118:       if ($options[0]) {
  119: 	push(@options,$newopt);
  120:       } else {
  121: 	$options[0]=$newopt;
  122:       }
  123:       $optchanged=1;
  124:     }
  125:     if ($optchanged) {
  126:       $result = "<foilgroup options=\"(";
  127:       foreach my $option (@options) {
  128: 	$option=~s/\'/\\\'/;
  129: 	&Apache::lonxml::debug("adding option :$option:");
  130: 	$result .="'".$option."',";
  131:       }
  132:       chop $result;
  133:       $result.=')">';
  134:     } # else nothing changed so just use the default mechanism
  135:   }
  136:   return $result;
  137: }
  138: 
  139: sub end_foilgroup {
  140:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  141:   
  142:   my $result;
  143:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
  144:     my $name;
  145:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
  146:     if ($count>$max) { $count=$max } 
  147:     &Apache::lonxml::debug("Count is $count from $max");
  148:     my @opt;
  149:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  150:     &Apache::lonxml::debug("Options are $#opt");
  151:     if ($target eq 'web') {
  152:       $result.=&displayfoils($count,@opt);
  153:     } elsif ( $target eq 'answer') {
  154:       $result.=&displayanswers($count,@opt);
  155:     } elsif ( $target eq 'grade') {
  156:       if ( defined $ENV{'form.submitted'}) {
  157: 	my @whichopt = &whichfoils($count);
  158: 	my $temp=1;my $name;
  159: 	my %responsehash;
  160: 	my $right=0;
  161: 	my $wrong=0;
  162: 	my $ignored=0;
  163: 	foreach $name (@whichopt) {
  164: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
  165: 	  $responsehash{$name}=$response;
  166: 	  if ( $response =~ /[^\s]/) {
  167: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
  168: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  169: 	    if ($value eq $response) {$right++;} else {$wrong++;}
  170: 	  } else {
  171: 	    $ignored++;
  172: 	  }
  173: 	  $temp++;
  174: 	}
  175: 	my $part=$Apache::inputtags::part;
  176: 	my $id = $Apache::inputtags::response['-1'];
  177: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  178: 	my %previous=&Apache::response::check_for_previous($responsestr,
  179: 							   $part,$id);
  180: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  181: 	my $ad;
  182: 	if ($wrong==0 && $ignored==0) {
  183: 	  $ad='EXACT_ANS';
  184: 	} elsif ($wrong==0 && $right==0) {
  185: 	  #nothing submitted
  186: 	} else {
  187: 	  $ad='INCORRECT';
  188: 	}
  189: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  190: 	  $responsestr;
  191: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
  192: 	  $ad;
  193: 	&Apache::response::handle_previous(\%previous,$ad);
  194:       }
  195:     }
  196:   } elsif ($target eq 'edit') {
  197:     $result.=&Apache::edit::end_table();
  198:   }
  199:   return $result;
  200: }
  201: 
  202: sub getfoilcounts {
  203:   my ($parstack,$safeeval)=@_;
  204:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  205:   # +1 since instructors will count from 1
  206:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  207:   return ($count,$max);
  208: }
  209: 
  210: sub whichfoils {
  211:   my ($max)=@_;
  212:   my @names = @{ $Apache::response::foilgroup{'names'} };
  213:   my @whichopt =();
  214:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
  215:     &Apache::lonxml::debug("Have $#whichopt max is $max");
  216:     my $aopt=int(rand($#names+1));
  217:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  218:     $aopt=splice(@names,$aopt,1);
  219:     &Apache::lonxml::debug("Picked $aopt");
  220:     push (@whichopt,$aopt);
  221:   }
  222:   return @whichopt;
  223: }
  224: 
  225: sub displayanswers {
  226:   my ($max,@opt)=@_;
  227:   my @names = @{ $Apache::response::foilgroup{'names'} };
  228:   my @whichopt = &whichfoils($max);
  229:   my $result=&Apache::response::answer_header('optionresponse');
  230:   foreach my $name (@whichopt) {
  231:     $result.=&Apache::response::answer_part('optionresponse',
  232: 		     $Apache::response::foilgroup{$name.'.value'})
  233:   }
  234:   $result.=&Apache::response::answer_footer('optionresponse');
  235:   return $result;
  236: }
  237: 
  238: sub displayfoils {
  239:   my ($max,@opt)=@_;
  240:   my @names = @{ $Apache::response::foilgroup{'names'} };
  241:   my @truelist;
  242:   my @falselist;
  243:   my $result;
  244:   my $name;
  245:   my @whichopt = &whichfoils($max);
  246:   my $part=$Apache::inputtags::part;
  247:   my $id=$Apache::inputtags::response[-1];
  248:   if (($Apache::lonhomework::history{"resource.$part.solved"} =~ /^correct/)  || ($Apache::inputtags::status[-1] eq  'SHOW_ANSWER')) {
  249:     foreach $name (@whichopt) {
  250:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  251: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
  252:     }
  253:   } else {
  254:     my $temp=1;
  255:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  256:     foreach $name (@whichopt) {
  257:       my $lastopt=$lastresponse{$name};
  258:       my $optionlist="<option></option>\n";
  259:       my $option;
  260:       foreach $option (@opt) {
  261: 	if ($option eq $lastopt) {
  262: 	  $optionlist.="<option selected=\"on\">$option</option>\n";
  263: 	} else {
  264: 	  $optionlist.="<option>$option</option>\n";
  265: 	}
  266:       }
  267:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
  268: 	.$optionlist
  269: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  270:       $temp++;
  271:     }
  272:   }
  273:   return $result."<br />";
  274: }
  275: 
  276: 
  277: sub start_conceptgroup {
  278:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  279:   $Apache::optionresponse::conceptgroup=1;
  280:   %Apache::response::conceptgroup={};
  281:   my $result;
  282:   if ($target eq 'edit') {
  283:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  284:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50')."</td></tr>";
  285:     $result.="<tr><td colspan=\"3\">\n";
  286:   }
  287:   if ($target eq 'modified') {
  288:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  289:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  290:   }
  291:   return $result;
  292: }
  293: 
  294: sub end_conceptgroup {
  295:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  296:   $Apache::optionresponse::conceptgroup=0;
  297:   my $result='';
  298:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  299:     #if not there aren't any foils to display and thus no question
  300:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  301:       my @names = @{ $Apache::response::conceptgroup{'names'} };
  302:       my $pick=int rand $#names+1;
  303:       my $name=$names[$pick];
  304:       push @{ $Apache::response::foilgroup{'names'} }, $name;
  305:       $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
  306:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  307:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  308:       $Apache::response::foilgroup{"$name.concept"} = $concept;
  309:       &Apache::lonxml::debug("Selecting $name in $concept");
  310:     }
  311:   }
  312:   if ($target eq 'edit') {
  313:     $result=&Apache::edit::end_table();
  314:   }
  315:   return $result;
  316: }
  317: 
  318: sub insert_conceptgroup {
  319:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  320:   return $result;
  321: }
  322: 
  323: sub start_foil {
  324:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  325:   my $result='';
  326:   if ($target eq 'web') {
  327:     &Apache::lonxml::startredirection;
  328:   } elsif ($target eq 'edit') {
  329:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  330:     my $level='-2';
  331:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  332:     my @opt;
  333:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
  334:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  335:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',(@opt)],$token,'15');
  336:     $result .= '</td></tr><tr><td colspan="3">';
  337:   } elsif ($target eq 'modified') {
  338:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'value','name');
  339:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  340:   }
  341:   return $result;
  342: }
  343: 
  344: sub end_foil {
  345:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  346:   my $text ='';
  347:   my $result = '';
  348:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  349:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  350:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  351:     if ($value ne 'unused') {
  352:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  353:       &Apache::lonxml::debug("Got a name of :$name:");
  354:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  355:       &Apache::lonxml::debug("Using a name of :$name:");
  356:       if ( $Apache::optionresponse::conceptgroup ) {
  357: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  358: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  359: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  360:       } else {
  361: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  362: 	$Apache::response::foilgroup{"$name.value"} = $value;
  363: 	$Apache::response::foilgroup{"$name.text"} = $text;
  364:       }
  365:     }
  366:   }
  367:   if ($target eq 'edit') {
  368:     $result.= &Apache::edit::tag_end($target,$token,'');
  369:   }
  370:   return $result;
  371: }
  372: 
  373: sub insert_foil {
  374:   return '
  375: <foil name="" value="unused">
  376: <startouttext />
  377: <endouttext />
  378: </foil>';
  379: }
  380: 1;
  381: __END__
  382:  

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