File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.27: download - view: text, annotated - select for diffs
Tue Dec 4 15:17:56 2001 UTC (22 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- GPL headers

    1: # The LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.27 2001/12/04 15:17:56 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 $id = $Apache::inputtags::response['-1'];
  175: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
  176: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  177: 	if ($wrong==0 && $ignored==0) {
  178: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
  179: 	} else {
  180: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
  181: 	}
  182:       }
  183:     }
  184:   } elsif ($target eq 'edit') {
  185:     $result.=&Apache::edit::end_table();
  186:   }
  187:   return $result;
  188: }
  189: 
  190: sub getfoilcounts {
  191:   my ($parstack,$safeeval)=@_;
  192:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  193:   # +1 since instructors will count from 1
  194:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  195:   return ($count,$max);
  196: }
  197: 
  198: sub whichfoils {
  199:   my ($max)=@_;
  200:   my @names = @{ $Apache::response::foilgroup{'names'} };
  201:   my @whichopt =();
  202:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
  203:     &Apache::lonxml::debug("Have $#whichopt max is $max");
  204:     my $aopt=int(rand($#names+1));
  205:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  206:     $aopt=splice(@names,$aopt,1);
  207:     &Apache::lonxml::debug("Picked $aopt");
  208:     push (@whichopt,$aopt);
  209:   }
  210:   return @whichopt;
  211: }
  212: 
  213: sub displayanswers {
  214:   my ($max,@opt)=@_;
  215:   my @names = @{ $Apache::response::foilgroup{'names'} };
  216:   my @whichopt = &whichfoils($max);
  217:   my $result=&Apache::response::answer_header('optionresponse');
  218:   foreach my $name (@whichopt) {
  219:     $result.=&Apache::response::answer_part('optionresponse',
  220: 		     $Apache::response::foilgroup{$name.'.value'})
  221:   }
  222:   $result.=&Apache::response::answer_footer('optionresponse');
  223:   return $result;
  224: }
  225: 
  226: sub displayfoils {
  227:   my ($max,@opt)=@_;
  228:   my @names = @{ $Apache::response::foilgroup{'names'} };
  229:   my @truelist;
  230:   my @falselist;
  231:   my $result;
  232:   my $name;
  233:   my @whichopt = &whichfoils($max);
  234:   my $optionlist="<option></option>\n";
  235:   my $option;
  236:   foreach $option (@opt) {
  237:     $optionlist.="<option>$option</option>\n";
  238:   }
  239:   if (($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/)  || ($Apache::inputtags::status[-1] eq  'SHOW_ANSWER')) {
  240:     foreach $name (@whichopt) {
  241:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
  242: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
  243:     }
  244:   } else {
  245:     my $temp=1;
  246:     foreach $name (@whichopt) {
  247:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
  248: 	.$optionlist
  249: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
  250:       $temp++;
  251:     }
  252:   }
  253:   return $result."<br />";
  254: }
  255: 
  256: 
  257: sub start_conceptgroup {
  258:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  259:   $Apache::optionresponse::conceptgroup=1;
  260:   %Apache::response::conceptgroup={};
  261:   my $result;
  262:   if ($target eq 'edit') {
  263:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  264:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50')."</td></tr>";
  265:     $result.="<tr><td colspan=\"3\">\n";
  266:   }
  267:   if ($target eq 'modified') {
  268:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  269:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  270:   }
  271:   return $result;
  272: }
  273: 
  274: sub end_conceptgroup {
  275:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  276:   $Apache::optionresponse::conceptgroup=0;
  277:   my $result='';
  278:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  279:     #if not there aren't any foils to display and thus no question
  280:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  281:       my @names = @{ $Apache::response::conceptgroup{'names'} };
  282:       my $pick=int rand $#names+1;
  283:       my $name=$names[$pick];
  284:       push @{ $Apache::response::foilgroup{'names'} }, $name;
  285:       $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
  286:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  287:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  288:       $Apache::response::foilgroup{"$name.concept"} = $concept;
  289:       &Apache::lonxml::debug("Selecting $name in $concept");
  290:     }
  291:   }
  292:   if ($target eq 'edit') {
  293:     $result=&Apache::edit::end_table();
  294:   }
  295:   return $result;
  296: }
  297: 
  298: sub insert_conceptgroup {
  299:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  300:   return $result;
  301: }
  302: 
  303: sub start_foil {
  304:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  305:   my $result='';
  306:   if ($target eq 'web') {
  307:     &Apache::lonxml::startredirection;
  308:   } elsif ($target eq 'edit') {
  309:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  310:     my $level='-2';
  311:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  312:     my @opt;
  313:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
  314:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  315:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',(@opt)],$token,'15');
  316:     $result .= '</td></tr><tr><td colspan="3">';
  317:   } elsif ($target eq 'modified') {
  318:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'value','name');
  319:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  320:   }
  321:   return $result;
  322: }
  323: 
  324: sub end_foil {
  325:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  326:   my $text ='';
  327:   my $result = '';
  328:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  329:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  330:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  331:     if ($value ne 'unused') {
  332:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  333:       &Apache::lonxml::debug("Got a name of :$name:");
  334:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  335:       &Apache::lonxml::debug("Using a name of :$name:");
  336:       if ( $Apache::optionresponse::conceptgroup ) {
  337: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  338: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  339: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  340:       } else {
  341: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  342: 	$Apache::response::foilgroup{"$name.value"} = $value;
  343: 	$Apache::response::foilgroup{"$name.text"} = $text;
  344:       }
  345:     }
  346:   }
  347:   if ($target eq 'edit') {
  348:     $result.= &Apache::edit::tag_end($target,$token,'');
  349:   }
  350:   return $result;
  351: }
  352: 
  353: sub insert_foil {
  354:   return '
  355: <foil name="" value="unused">
  356: <startouttext />
  357: <endouttext />
  358: </foil>';
  359: }
  360: 1;
  361: __END__
  362:  

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