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

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # option list style responses
1.27      albertel    3: #
1.30    ! matthew     4: # $Id: optionresponse.pm,v 1.29 2001/12/14 23:00:52 albertel 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.1       albertel   29: package Apache::optionresponse;
                     30: use strict;
1.6       albertel   31: use Apache::response;
1.1       albertel   32: 
                     33: sub BEGIN {
                     34:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
                     35: }
                     36: 
                     37: sub start_optionresponse {
1.22      albertel   38:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     39:   my $result='';
1.29      albertel   40:   #when in a option response use these
1.22      albertel   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') {
1.23      albertel   45:     $result.=&Apache::edit::start_table($token).
1.30    ! matthew    46: 	"<tr><td>Multiple Option Response Question</td><td>Delete:".
1.23      albertel   47: 	&Apache::edit::deletelist($target,$token)
1.30    ! matthew    48: 	."</td><td>&nbsp;".
        !            49:         &Apache::edit::end_row().
        !            50:         &Apache::edit::start_spanning_row().
        !            51:         "\n";
1.23      albertel   52:     $result.=&Apache::edit::text_arg('Max Number Of Foils:','max',$token,'4').
1.30    ! matthew    53:         &Apache::edit::end_row().
        !            54: 	&Apache::edit::start_spanning_row();
1.22      albertel   55:   }
                     56:   if ($target eq 'modified') {
1.23      albertel   57:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                     58: 						 $safeeval,'max');
1.22      albertel   59:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.23      albertel   60:   }
                     61:   if ($target eq 'meta') {
                     62:     $result=&Apache::response::meta_package_write('optionresponse');
1.22      albertel   63:   }
                     64:   return $result;
1.1       albertel   65: }
                     66: 
                     67: sub end_optionresponse {
1.22      albertel   68:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     69:   &Apache::response::end_response;
                     70:   pop @Apache::lonxml::namespace;
1.29      albertel   71:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
1.22      albertel   72:   my $result;
                     73:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                     74:   return $result;
1.1       albertel   75: }
                     76: 
1.22      albertel   77: %Apache::response::foilgroup={};
1.1       albertel   78: sub start_foilgroup {
1.22      albertel   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>
1.13      albertel   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
1.30    ! matthew   105:     $result.= &Apache::edit::end_row();
        !           106:     $result.= &Apache::edit::start_spanning_row();
        !           107:     $result.= $insertlist.'<br />';
1.22      albertel  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;
1.1       albertel  140: }
                    141: 
                    142: sub end_foilgroup {
1.22      albertel  143:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    144:   
                    145:   my $result;
1.25      albertel  146:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
1.22      albertel  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') {
1.25      albertel  155:       $result.=&displayfoils($count,@opt);
                    156:     } elsif ( $target eq 'answer') {
                    157:       $result.=&displayanswers($count,@opt);
1.22      albertel  158:     } elsif ( $target eq 'grade') {
                    159:       if ( defined $ENV{'form.submitted'}) {
                    160: 	my @whichopt = &whichfoils($count);
                    161: 	my $temp=1;my $name;
1.26      albertel  162: 	my %responsehash;
1.22      albertel  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"};
1.26      albertel  168: 	  $responsehash{$name}=$response;
1.22      albertel  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: 	}
1.28      albertel  178: 	my $part=$Apache::inputtags::part;
1.22      albertel  179: 	my $id = $Apache::inputtags::response['-1'];
1.28      albertel  180: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    181: 	my %previous=&Apache::response::check_for_previous($responsestr,
                    182: 							   $part,$id);
1.22      albertel  183: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.28      albertel  184: 	my $ad;
1.22      albertel  185: 	if ($wrong==0 && $ignored==0) {
1.28      albertel  186: 	  $ad='EXACT_ANS';
                    187: 	} elsif ($wrong==0 && $right==0) {
                    188: 	  #nothing submitted
1.22      albertel  189: 	} else {
1.28      albertel  190: 	  $ad='INCORRECT';
1.1       albertel  191: 	}
1.28      albertel  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);
1.22      albertel  197:       }
1.1       albertel  198:     }
1.25      albertel  199:   } elsif ($target eq 'edit') {
1.22      albertel  200:     $result.=&Apache::edit::end_table();
                    201:   }
                    202:   return $result;
1.1       albertel  203: }
                    204: 
                    205: sub getfoilcounts {
1.22      albertel  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);
1.1       albertel  211: }
                    212: 
                    213: sub whichfoils {
1.22      albertel  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;
1.1       albertel  226: }
                    227: 
1.25      albertel  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: 
1.1       albertel  241: sub displayfoils {
1.22      albertel  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);
1.28      albertel  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')) {
1.22      albertel  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;
1.28      albertel  258:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
1.22      albertel  259:     foreach $name (@whichopt) {
1.28      albertel  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:       }
1.22      albertel  270:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
                    271: 	.$optionlist
                    272: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
                    273:       $temp++;
1.1       albertel  274:     }
1.22      albertel  275:   }
                    276:   return $result."<br />";
1.1       albertel  277: }
                    278: 
1.22      albertel  279: 
1.2       albertel  280: sub start_conceptgroup {
1.22      albertel  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");
1.30    ! matthew   287:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
        !           288:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22      albertel  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;
1.2       albertel  295: }
                    296: 
                    297: sub end_conceptgroup {
1.22      albertel  298:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    299:   $Apache::optionresponse::conceptgroup=0;
                    300:   my $result='';
1.25      albertel  301:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.22      albertel  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;
1.2       albertel  319: }
                    320: 
1.16      albertel  321: sub insert_conceptgroup {
1.22      albertel  322:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    323:   return $result;
1.16      albertel  324: }
                    325: 
1.1       albertel  326: sub start_foil {
1.22      albertel  327:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    328:   my $result='';
1.25      albertel  329:   if ($target eq 'web') {
                    330:     &Apache::lonxml::startredirection;
                    331:   } elsif ($target eq 'edit') {
1.22      albertel  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');
1.30    ! matthew   339:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25      albertel  340:   } elsif ($target eq 'modified') {
1.22      albertel  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;
1.1       albertel  345: }
                    346: 
                    347: sub end_foil {
1.22      albertel  348:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    349:   my $text ='';
                    350:   my $result = '';
                    351:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.25      albertel  352:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
1.22      albertel  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;
1.1       albertel  374: }
                    375: 
1.7       albertel  376: sub insert_foil {
1.22      albertel  377:   return '
1.15      albertel  378: <foil name="" value="unused">
1.14      albertel  379: <startouttext />
                    380: <endouttext />
1.7       albertel  381: </foil>';
                    382: }
1.1       albertel  383: 1;
                    384: __END__
                    385:  

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