File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.30: download - view: text, annotated - select for diffs
Fri Jan 11 16:32:29 2002 UTC (22 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Update to use edit::end_row() and edit::start_spanning_row() as a cleanup
for edit::select_arg and edit::select_or_text_arg.

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

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