Annotation of loncom/homework/optionresponse.pm, revision 1.33

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # option list style responses
1.27      albertel    3: #
1.33    ! albertel    4: # $Id: optionresponse.pm,v 1.32 2002/01/18 16:37:05 sakharuk Exp $
1.27      albertel    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: #
1.22      albertel   28: # 2/21 Guy
1.32      sakharuk   29: # 01/18 Alex
1.1       albertel   30: package Apache::optionresponse;
                     31: use strict;
1.6       albertel   32: use Apache::response;
1.1       albertel   33: 
1.31      harris41   34: BEGIN {
1.1       albertel   35:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
                     36: }
                     37: 
                     38: sub start_optionresponse {
1.22      albertel   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     40:   my $result='';
1.29      albertel   41:   #when in a option response use these
1.22      albertel   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') {
1.23      albertel   46:     $result.=&Apache::edit::start_table($token).
1.30      matthew    47: 	"<tr><td>Multiple Option Response Question</td><td>Delete:".
1.23      albertel   48: 	&Apache::edit::deletelist($target,$token)
1.30      matthew    49: 	."</td><td>&nbsp;".
                     50:         &Apache::edit::end_row().
                     51:         &Apache::edit::start_spanning_row().
                     52:         "\n";
1.33    ! albertel   53:     $result.=&Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
1.30      matthew    54:         &Apache::edit::end_row().
                     55: 	&Apache::edit::start_spanning_row();
1.22      albertel   56:   }
                     57:   if ($target eq 'modified') {
1.23      albertel   58:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                     59: 						 $safeeval,'max');
1.22      albertel   60:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.23      albertel   61:   }
                     62:   if ($target eq 'meta') {
                     63:     $result=&Apache::response::meta_package_write('optionresponse');
1.22      albertel   64:   }
1.32      sakharuk   65: #  if ($target eq 'tex') {$result .= '\begin{itemize}';}
1.22      albertel   66:   return $result;
1.1       albertel   67: }
                     68: 
                     69: sub end_optionresponse {
1.22      albertel   70:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     71:   &Apache::response::end_response;
                     72:   pop @Apache::lonxml::namespace;
1.29      albertel   73:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
1.22      albertel   74:   my $result;
                     75:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.32      sakharuk   76: #  if ($target eq 'tex') {$result .= '\end{itemize}';}
1.22      albertel   77:   return $result;
1.1       albertel   78: }
                     79: 
1.22      albertel   80: %Apache::response::foilgroup={};
1.1       albertel   81: sub start_foilgroup {
1.22      albertel   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>
1.13      albertel  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
1.30      matthew   108:     $result.= &Apache::edit::end_row();
                    109:     $result.= &Apache::edit::start_spanning_row();
                    110:     $result.= $insertlist.'<br />';
1.22      albertel  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:   }
1.32      sakharuk  142:   if ($target eq 'tex') {$result .= '\begin{itemize}';}
1.22      albertel  143:   return $result;
1.1       albertel  144: }
                    145: 
                    146: sub end_foilgroup {
1.22      albertel  147:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    148:   
                    149:   my $result;
1.32      sakharuk  150:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.22      albertel  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");
1.32      sakharuk  158:     if ($target eq 'web' || $target eq 'tex') {
                    159:       $result.=&displayfoils($target,$count,@opt);
1.25      albertel  160:     } elsif ( $target eq 'answer') {
                    161:       $result.=&displayanswers($count,@opt);
1.22      albertel  162:     } elsif ( $target eq 'grade') {
                    163:       if ( defined $ENV{'form.submitted'}) {
                    164: 	my @whichopt = &whichfoils($count);
                    165: 	my $temp=1;my $name;
1.26      albertel  166: 	my %responsehash;
1.22      albertel  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"};
1.26      albertel  172: 	  $responsehash{$name}=$response;
1.22      albertel  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: 	}
1.28      albertel  182: 	my $part=$Apache::inputtags::part;
1.22      albertel  183: 	my $id = $Apache::inputtags::response['-1'];
1.28      albertel  184: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    185: 	my %previous=&Apache::response::check_for_previous($responsestr,
                    186: 							   $part,$id);
1.22      albertel  187: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.28      albertel  188: 	my $ad;
1.22      albertel  189: 	if ($wrong==0 && $ignored==0) {
1.28      albertel  190: 	  $ad='EXACT_ANS';
                    191: 	} elsif ($wrong==0 && $right==0) {
                    192: 	  #nothing submitted
1.22      albertel  193: 	} else {
1.28      albertel  194: 	  $ad='INCORRECT';
1.1       albertel  195: 	}
1.28      albertel  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);
1.22      albertel  201:       }
1.1       albertel  202:     }
1.25      albertel  203:   } elsif ($target eq 'edit') {
1.22      albertel  204:     $result.=&Apache::edit::end_table();
1.32      sakharuk  205:   }  
                    206:   if ($target eq 'tex') {$result .= '\end{itemize}';}
1.22      albertel  207:   return $result;
1.1       albertel  208: }
                    209: 
                    210: sub getfoilcounts {
1.22      albertel  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);
1.1       albertel  216: }
                    217: 
                    218: sub whichfoils {
1.22      albertel  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;
1.1       albertel  231: }
                    232: 
1.25      albertel  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: 
1.1       albertel  246: sub displayfoils {
1.32      sakharuk  247:   my ($target,$max,@opt)=@_;
1.22      albertel  248:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    249:   my @truelist;
                    250:   my @falselist;
                    251:   my $result;
                    252:   my $name;
1.32      sakharuk  253:   $Apache::optionresponse::displayoptionintex=0;
1.22      albertel  254:   my @whichopt = &whichfoils($max);
1.28      albertel  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')) {
1.22      albertel  258:     foreach $name (@whichopt) {
1.32      sakharuk  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";
1.22      albertel  266:     }
                    267:   } else {
                    268:     my $temp=1;
1.28      albertel  269:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
1.22      albertel  270:     foreach $name (@whichopt) {
1.28      albertel  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:       }
1.32      sakharuk  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:       } 
1.1       albertel  299:     }
1.32      sakharuk  300:   } 
                    301:   if ($target ne 'tex') {
                    302:       return $result."<br />";
                    303:   }
                    304:   else {
                    305:       return $result;
1.22      albertel  306:   }
1.1       albertel  307: }
                    308: 
1.22      albertel  309: 
1.2       albertel  310: sub start_conceptgroup {
1.22      albertel  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");
1.30      matthew   317:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    318:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22      albertel  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;
1.2       albertel  325: }
                    326: 
                    327: sub end_conceptgroup {
1.22      albertel  328:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    329:   $Apache::optionresponse::conceptgroup=0;
                    330:   my $result='';
1.32      sakharuk  331:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
                    332:       || $target eq 'tex') {
1.22      albertel  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;
1.2       albertel  350: }
                    351: 
1.16      albertel  352: sub insert_conceptgroup {
1.22      albertel  353:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    354:   return $result;
1.16      albertel  355: }
                    356: 
1.1       albertel  357: sub start_foil {
1.22      albertel  358:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    359:   my $result='';
1.32      sakharuk  360:   if ($target eq 'web' || $target eq 'tex') {
1.25      albertel  361:     &Apache::lonxml::startredirection;
                    362:   } elsif ($target eq 'edit') {
1.22      albertel  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');
1.30      matthew   370:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25      albertel  371:   } elsif ($target eq 'modified') {
1.22      albertel  372:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'value','name');
                    373:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32      sakharuk  374:   } 
                    375:   if ($target eq 'tex') {$result .= '\item ';}
1.22      albertel  376:   return $result;
1.1       albertel  377: }
                    378: 
                    379: sub end_foil {
1.22      albertel  380:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    381:   my $text ='';
                    382:   my $result = '';
1.32      sakharuk  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') {
1.22      albertel  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;
1.1       albertel  409: }
                    410: 
1.7       albertel  411: sub insert_foil {
1.22      albertel  412:   return '
1.15      albertel  413: <foil name="" value="unused">
1.14      albertel  414: <startouttext />
                    415: <endouttext />
1.7       albertel  416: </foil>';
                    417: }
1.1       albertel  418: 1;
                    419: __END__
                    420:  

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