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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # option list style responses
1.4       albertel    3: # 2/21 Guy
1.1       albertel    4: package Apache::optionresponse;
                      5: use strict;
1.6       albertel    6: use Apache::response;
1.1       albertel    7: 
                      8: sub BEGIN {
                      9:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
                     10: }
                     11: 
                     12: sub start_optionresponse {
1.11      albertel   13:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.9       albertel   14:   my $result='';
1.1       albertel   15:   #when in a radiobutton response use these
1.2       albertel   16:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
1.7       albertel   17:   push (@Apache::lonxml::namespace,'optionresponse');
1.1       albertel   18:   my $id = &Apache::response::start_response($parstack,$safeeval);
1.9       albertel   19:   if ($target eq 'edit') {
1.14    ! albertel   20:     $result.=&Apache::edit::start_table($token)."<tr><td>Multiple Option Response Question</td>
1.9       albertel   21: <td>Delete:".
                     22:   &Apache::edit::deletelist($target,$token)
                     23: ."</td></tr><tr><td colspan=\"3\">\n";
                     24:   }
                     25: 
                     26:   return $result;
1.1       albertel   27: }
                     28: 
                     29: sub end_optionresponse {
                     30:   &Apache::response::end_response;
1.7       albertel   31:   pop @Apache::lonxml::namespace;
1.1       albertel   32:   return '';
                     33: }
                     34: 
1.7       albertel   35: sub insert_optionresponse {
                     36:   return '
                     37: <optionresponse max="10">
                     38:     <foilgroup options=\"\">
                     39:     </foilgroup>
                     40: </optionresponse>';
                     41: }
                     42: 
1.1       albertel   43: %Apache::response::foilgroup={};
                     44: sub start_foilgroup {
1.13      albertel   45:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     46: 
                     47:   my $result='';
1.1       albertel   48:   %Apache::response::foilgroup={};
1.9       albertel   49:   $Apache::optionresponse::conceptgroup=0;
1.6       albertel   50:   &Apache::response::setrandomnumber();
1.13      albertel   51:   if ($target eq 'edit') {
                     52:     my $optionlist="<option></option>\n";
                     53:     my $option;
                     54:     my @opt;
                     55:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
1.14    ! albertel   56:     my $count=0;
1.13      albertel   57:     foreach $option (@opt) {
1.14    ! albertel   58:       $optionlist.="<option value=\"$count\">$option</option>\n";
1.13      albertel   59:     }
                     60:     my $insertlist=&Apache::edit::insertlist($target,$token);
1.14    ! albertel   61:     $result.=&Apache::edit::start_table($token);
1.13      albertel   62:     $result.= (<<ENDTABLE);
                     63:       <tr><td>Select Options</td>
                     64:         <td>
                     65: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
                     66:         </td>
                     67:         <td>Delete an Option:
                     68: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
                     69:         </td>
                     70:      </tr>
                     71:      <tr><td colspan="3">$insertlist<br />
                     72: ENDTABLE
                     73:   }
                     74:   if ($target eq 'modified') {
                     75:     my @options;
1.14    ! albertel   76:     my $optchanged=0;
1.13      albertel   77:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.14    ! albertel   78:     if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
        !            79:       my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
        !            80:       splice(@options,$delopt,1);
        !            81:       $optchanged=1;
        !            82:     }
1.13      albertel   83:     if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
                     84:       my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
                     85:       if ($options[0]) {
1.14    ! albertel   86: 	push(@options,$newopt);
1.13      albertel   87:       } else {
1.14    ! albertel   88: 	$options[0]=$newopt;
1.13      albertel   89:       }
1.14    ! albertel   90:       $optchanged=1;
        !            91:     }
        !            92:     if ($optchanged) {
1.13      albertel   93:       $result = "<foilgroup options=\"(";
                     94:       foreach my $option (@options) {
1.14    ! albertel   95: 	$option=~s/\'/\\\'/;
1.13      albertel   96: 	&Apache::lonxml::debug("adding option :$option:");
                     97: 	$result .="'".$option."',";
                     98:       }
                     99:       chop $result;
                    100:       $result.=')">';
1.14    ! albertel  101:     } # else nothing changed so just use the default mechanism
1.13      albertel  102:   }
                    103:   return $result;
1.1       albertel  104: }
                    105: 
                    106: sub end_foilgroup {
1.11      albertel  107:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel  108:   
                    109:   my $result;
                    110:   if ($target ne 'meta') {
                    111:     my $name;
                    112:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    113:     if ($count>$max) { $count=$max } 
                    114:     &Apache::lonxml::debug("Count is $count from $max");
                    115:     my @opt;
1.13      albertel  116:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
                    117:     &Apache::lonxml::debug("Options are $#opt");
1.1       albertel  118:     if ($target eq 'web') {
                    119:       $result=&displayfoils($count,@opt);
                    120:     } elsif ( $target eq 'grade') {
                    121:       if ( defined $ENV{'form.submitted'}) {
                    122: 	my @whichopt = &whichfoils($count);
                    123: 	my $temp=1;my $name;
                    124: 	my $allresponse;
                    125: 	my $right=0;
                    126: 	my $wrong=0;
1.8       albertel  127: 	my $ignored=0;
1.1       albertel  128: 	foreach $name (@whichopt) {
                    129: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
                    130: 	  $allresponse.="$response:";
                    131: 	  if ( $response =~ /[^\s]/) {
1.3       albertel  132: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
1.1       albertel  133: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    134: 	    if ($value eq $response) {$right++;} else {$wrong++;}
1.8       albertel  135: 	  } else {
                    136: 	    $ignored++;
1.1       albertel  137: 	  }
                    138: 	  $temp++;
                    139: 	}
                    140: 	my $id = $Apache::inputtags::response['-1'];
                    141: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
1.8       albertel  142: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
                    143: 	if ($wrong==0 && $ignored==0) {
1.1       albertel  144: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
                    145: 	} else {
                    146: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
                    147: 	}
                    148:       }
                    149:     }
                    150:   }
                    151:   return $result;
                    152: }
                    153: 
                    154: sub getfoilcounts {
                    155:   my ($parstack,$safeeval)=@_;
1.10      albertel  156:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
1.9       albertel  157:   # +1 since instructors will count from 1
                    158:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.1       albertel  159:   return ($count,$max);
                    160: }
                    161: 
                    162: sub whichfoils {
                    163:   my ($max)=@_;
                    164:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    165:   my @whichopt =();
1.9       albertel  166:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
                    167:     &Apache::lonxml::debug("Have $#whichopt max is $max");
                    168:     my $aopt=int(rand($#names+1));
1.6       albertel  169:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
1.1       albertel  170:     $aopt=splice(@names,$aopt,1);
                    171:     &Apache::lonxml::debug("Picked $aopt");
                    172:     push (@whichopt,$aopt);
                    173:   }
                    174:   return @whichopt;
                    175: }
                    176: 
                    177: sub displayfoils {
                    178:   my ($max,@opt)=@_;
                    179:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    180:   my @truelist;
                    181:   my @falselist;
                    182:   my $result;
                    183:   my $name;
                    184:   my @whichopt = &whichfoils($max);
                    185:   my $optionlist="<option></option>\n";
                    186:   my $option;
                    187:   foreach $option (@opt) {
                    188:     $optionlist.="<option>$option</option>\n";
                    189:   }
                    190:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
                    191:     foreach $name (@whichopt) {
                    192:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
                    193: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
                    194:     }
                    195:   } else {
                    196:     my $temp=1;
                    197:     foreach $name (@whichopt) {
                    198:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
                    199: 	.$optionlist
                    200: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
                    201:       $temp++;
                    202:     }
                    203:   }
                    204:   return $result."<br />";
                    205: }
                    206: 
1.5       albertel  207: 
1.2       albertel  208: sub start_conceptgroup {
1.5       albertel  209:   $Apache::optionresponse::conceptgroup=1;  
                    210:   %Apache::response::conceptgroup={};
                    211:   return '';
1.2       albertel  212: }
                    213: 
                    214: sub end_conceptgroup {
1.11      albertel  215:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.5       albertel  216:   $Apache::optionresponse::conceptgroup=0;  
                    217:   if ($target eq 'web' || $target eq 'grade') {
                    218:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    219:     my $pick=int rand $#names+1;
                    220:     my $name=$names[$pick];
                    221:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    222:     $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
                    223:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
1.10      albertel  224:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
1.5       albertel  225:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    226:     &Apache::lonxml::debug("Selecting $name in $concept");
                    227:   }
                    228:   return '';
1.2       albertel  229: }
                    230: 
1.1       albertel  231: sub start_foil {
1.11      albertel  232:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      albertel  233:   my $result='';
1.4       albertel  234:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
1.13      albertel  235:   if ($target eq 'edit') {
                    236:     $result=&Apache::edit::tag_start($target,$token,"Foil");
                    237:     my @opt;
1.14    ! albertel  238:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,'-2');
        !           239:     my $optionlist="<option></option>\n";
        !           240:     foreach my $option (@opt) {
        !           241:       $optionlist.="<option>$option</option>\n";
        !           242:     }
        !           243:     $result.="Name: <input name=\"$Apache::lonxml::curdepth.name\" type=\"text\" />".
        !           244:       "Correct Option:<select>$optionlist</select></td></tr><tr><td colspan=\"3\">";
1.13      albertel  245:   }
                    246:   return $result;
1.1       albertel  247: }
                    248: 
                    249: sub end_foil {
1.11      albertel  250:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.4       albertel  251:   my $text ='';
1.13      albertel  252:   my $result = '';
1.4       albertel  253:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.1       albertel  254:   if ($target eq 'web' || $target eq 'grade') {
1.10      albertel  255:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.1       albertel  256:     if ($value ne 'unused') {
1.10      albertel  257:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.13      albertel  258:       if (!$name) { $name=$Apache::lonxml::curdepth; }
1.5       albertel  259:       if ( $Apache::optionresponse::conceptgroup ) {
                    260: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    261: 	$Apache::response::conceptgroup{"$name.value"} = $value;
                    262: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
                    263:       } else {
                    264: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    265: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    266: 	$Apache::response::foilgroup{"$name.text"} = $text;
                    267:       }
1.2       albertel  268:     }
1.6       albertel  269:   }
                    270:   if ($target eq 'edit') {
1.13      albertel  271:     $result.= &Apache::edit::tag_end($target,$token,'');
1.1       albertel  272:   }
1.13      albertel  273:   return $result;
1.1       albertel  274: }
                    275: 
1.7       albertel  276: sub insert_foil {
                    277:   return '
                    278: <foil name="" value="">
1.14    ! albertel  279: <startouttext />
        !           280: <endouttext />
1.7       albertel  281: </foil>';
                    282: }
1.1       albertel  283: 1;
                    284: __END__
                    285:  

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