File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.38: download - view: text, annotated - select for diffs
Thu Apr 25 21:35:30 2002 UTC (22 years ago) by sakharuk
Branches: MAIN
CVS tags: stable_2002_april, HEAD
itemize was changed on enumerate

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

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