File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.33: download - view: text, annotated - select for diffs
Fri Jan 25 16:12:18 2002 UTC (22 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: stable_2002_spring, HEAD
- English update

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

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