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

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)
1.17      albertel   23:     ."</td><td>&nbsp;</td></tr><tr><td colspan=\"3\">\n";
                     24:     $result.=&Apache::edit::text_arg('Max Number Of Foils:','max',$token,'4')."</td></tr>";
                     25:     $result.="<tr><td colspan=\"3\">\n";
                     26:   }
                     27:   if ($target eq 'modified') {
                     28:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'max');
                     29:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.9       albertel   30:   }
                     31:   return $result;
1.1       albertel   32: }
                     33: 
                     34: sub end_optionresponse {
1.15      albertel   35:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel   36:   &Apache::response::end_response;
1.7       albertel   37:   pop @Apache::lonxml::namespace;
1.15      albertel   38:   my $result;
                     39:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                     40:   return $result;
1.1       albertel   41: }
                     42: 
                     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.17      albertel   56:     my $count=1;
1.13      albertel   57:     foreach $option (@opt) {
1.14      albertel   58:       $optionlist.="<option value=\"$count\">$option</option>\n";
1.16      albertel   59:       $count++;
1.13      albertel   60:     }
                     61:     my $insertlist=&Apache::edit::insertlist($target,$token);
1.14      albertel   62:     $result.=&Apache::edit::start_table($token);
1.13      albertel   63:     $result.= (<<ENDTABLE);
                     64:       <tr><td>Select Options</td>
                     65:         <td>
                     66: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
                     67:         </td>
                     68:         <td>Delete an Option:
                     69: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
                     70:         </td>
                     71:      </tr>
                     72:      <tr><td colspan="3">$insertlist<br />
                     73: ENDTABLE
                     74:   }
                     75:   if ($target eq 'modified') {
                     76:     my @options;
1.14      albertel   77:     my $optchanged=0;
1.13      albertel   78:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.14      albertel   79:     if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
                     80:       my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
1.16      albertel   81:       &Apache::lonxml::debug("Deleting :$delopt:");
1.17      albertel   82:       splice(@options,$delopt-1,1);
1.14      albertel   83:       $optchanged=1;
                     84:     }
1.13      albertel   85:     if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
                     86:       my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
                     87:       if ($options[0]) {
1.14      albertel   88: 	push(@options,$newopt);
1.13      albertel   89:       } else {
1.14      albertel   90: 	$options[0]=$newopt;
1.13      albertel   91:       }
1.14      albertel   92:       $optchanged=1;
                     93:     }
                     94:     if ($optchanged) {
1.13      albertel   95:       $result = "<foilgroup options=\"(";
                     96:       foreach my $option (@options) {
1.14      albertel   97: 	$option=~s/\'/\\\'/;
1.13      albertel   98: 	&Apache::lonxml::debug("adding option :$option:");
                     99: 	$result .="'".$option."',";
                    100:       }
                    101:       chop $result;
                    102:       $result.=')">';
1.14      albertel  103:     } # else nothing changed so just use the default mechanism
1.13      albertel  104:   }
                    105:   return $result;
1.1       albertel  106: }
                    107: 
                    108: sub end_foilgroup {
1.11      albertel  109:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.1       albertel  110:   
                    111:   my $result;
1.15      albertel  112:   if ($target eq 'grade' || $target eq 'web') {
1.1       albertel  113:     my $name;
                    114:     my ($count,$max) = &getfoilcounts($parstack,$safeeval);
                    115:     if ($count>$max) { $count=$max } 
                    116:     &Apache::lonxml::debug("Count is $count from $max");
                    117:     my @opt;
1.13      albertel  118:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
                    119:     &Apache::lonxml::debug("Options are $#opt");
1.1       albertel  120:     if ($target eq 'web') {
                    121:       $result=&displayfoils($count,@opt);
                    122:     } elsif ( $target eq 'grade') {
                    123:       if ( defined $ENV{'form.submitted'}) {
                    124: 	my @whichopt = &whichfoils($count);
                    125: 	my $temp=1;my $name;
                    126: 	my $allresponse;
                    127: 	my $right=0;
                    128: 	my $wrong=0;
1.8       albertel  129: 	my $ignored=0;
1.1       albertel  130: 	foreach $name (@whichopt) {
                    131: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
                    132: 	  $allresponse.="$response:";
                    133: 	  if ( $response =~ /[^\s]/) {
1.3       albertel  134: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
1.1       albertel  135: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    136: 	    if ($value eq $response) {$right++;} else {$wrong++;}
1.8       albertel  137: 	  } else {
                    138: 	    $ignored++;
1.1       albertel  139: 	  }
                    140: 	  $temp++;
                    141: 	}
                    142: 	my $id = $Apache::inputtags::response['-1'];
                    143: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
1.8       albertel  144: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
                    145: 	if ($wrong==0 && $ignored==0) {
1.1       albertel  146: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
                    147: 	} else {
                    148: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
                    149: 	}
                    150:       }
                    151:     }
                    152:   }
1.15      albertel  153:   if ($target eq 'edit') {
                    154:     $result.=&Apache::edit::end_table();
                    155:   }
1.1       albertel  156:   return $result;
                    157: }
                    158: 
                    159: sub getfoilcounts {
                    160:   my ($parstack,$safeeval)=@_;
1.10      albertel  161:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
1.9       albertel  162:   # +1 since instructors will count from 1
                    163:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.1       albertel  164:   return ($count,$max);
                    165: }
                    166: 
                    167: sub whichfoils {
                    168:   my ($max)=@_;
                    169:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    170:   my @whichopt =();
1.9       albertel  171:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
                    172:     &Apache::lonxml::debug("Have $#whichopt max is $max");
                    173:     my $aopt=int(rand($#names+1));
1.6       albertel  174:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
1.1       albertel  175:     $aopt=splice(@names,$aopt,1);
                    176:     &Apache::lonxml::debug("Picked $aopt");
                    177:     push (@whichopt,$aopt);
                    178:   }
                    179:   return @whichopt;
                    180: }
                    181: 
                    182: sub displayfoils {
                    183:   my ($max,@opt)=@_;
                    184:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    185:   my @truelist;
                    186:   my @falselist;
                    187:   my $result;
                    188:   my $name;
                    189:   my @whichopt = &whichfoils($max);
                    190:   my $optionlist="<option></option>\n";
                    191:   my $option;
                    192:   foreach $option (@opt) {
                    193:     $optionlist.="<option>$option</option>\n";
                    194:   }
                    195:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
                    196:     foreach $name (@whichopt) {
                    197:       $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
                    198: 	":".$Apache::response::foilgroup{$name.'.text'}."\n";
                    199:     }
                    200:   } else {
                    201:     my $temp=1;
                    202:     foreach $name (@whichopt) {
                    203:       $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
                    204: 	.$optionlist
                    205: 	  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
                    206:       $temp++;
                    207:     }
                    208:   }
                    209:   return $result."<br />";
                    210: }
                    211: 
1.5       albertel  212: 
1.2       albertel  213: sub start_conceptgroup {
1.15      albertel  214:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    215:   $Apache::optionresponse::conceptgroup=1;
1.5       albertel  216:   %Apache::response::conceptgroup={};
1.15      albertel  217:   my $result;
                    218:   if ($target eq 'edit') {
                    219:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.17      albertel  220:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50')."</td></tr>";
                    221:     $result.="<tr><td colspan=\"3\">\n";
1.15      albertel  222:   }
1.16      albertel  223:   if ($target eq 'modified') {
                    224:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
                    225:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    226:   }
1.15      albertel  227:   return $result;
1.2       albertel  228: }
                    229: 
                    230: sub end_conceptgroup {
1.11      albertel  231:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.15      albertel  232:   $Apache::optionresponse::conceptgroup=0;
                    233:   my $result='';
1.5       albertel  234:   if ($target eq 'web' || $target eq 'grade') {
1.18      albertel  235:     #if not there aren't any foils to display and thus no question
                    236:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    237:       my @names = @{ $Apache::response::conceptgroup{'names'} };
                    238:       my $pick=int rand $#names+1;
                    239:       my $name=$names[$pick];
                    240:       push @{ $Apache::response::foilgroup{'names'} }, $name;
                    241:       $Apache::response::foilgroup{"$name.value"} =  $Apache::response::conceptgroup{"$name.value"};
                    242:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
                    243:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    244:       $Apache::response::foilgroup{"$name.concept"} = $concept;
                    245:       &Apache::lonxml::debug("Selecting $name in $concept");
                    246:     }
1.5       albertel  247:   }
1.15      albertel  248:   if ($target eq 'edit') {
                    249:     $result=&Apache::edit::end_table();
                    250:   }
                    251:   return $result;
1.2       albertel  252: }
                    253: 
1.16      albertel  254: sub insert_conceptgroup {
1.18      albertel  255:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
1.16      albertel  256:   return $result;
                    257: }
                    258: 
1.1       albertel  259: sub start_foil {
1.11      albertel  260:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      albertel  261:   my $result='';
1.4       albertel  262:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
1.13      albertel  263:   if ($target eq 'edit') {
                    264:     $result=&Apache::edit::tag_start($target,$token,"Foil");
1.15      albertel  265:     my $level='-2';
                    266:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
1.13      albertel  267:     my @opt;
1.15      albertel  268:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
1.17      albertel  269:     $result.=&Apache::edit::text_arg('Name:','name',$token).
                    270:       &Apache::edit::select_arg('Correct Option:','value',['unused',(@opt)],$token).
                    271:       '</td></tr><tr><td colspan="3">';
1.15      albertel  272:   }
                    273:   if ($target eq 'modified') {
1.16      albertel  274:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'value','name');
                    275:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.13      albertel  276:   }
                    277:   return $result;
1.1       albertel  278: }
                    279: 
                    280: sub end_foil {
1.11      albertel  281:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.4       albertel  282:   my $text ='';
1.13      albertel  283:   my $result = '';
1.4       albertel  284:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.1       albertel  285:   if ($target eq 'web' || $target eq 'grade') {
1.10      albertel  286:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.1       albertel  287:     if ($value ne 'unused') {
1.10      albertel  288:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.18      albertel  289:       &Apache::lonxml::debug("Got a name of :$name:");
1.13      albertel  290:       if (!$name) { $name=$Apache::lonxml::curdepth; }
1.18      albertel  291:       &Apache::lonxml::debug("Using a name of :$name:");
1.5       albertel  292:       if ( $Apache::optionresponse::conceptgroup ) {
                    293: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    294: 	$Apache::response::conceptgroup{"$name.value"} = $value;
                    295: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
                    296:       } else {
                    297: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    298: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    299: 	$Apache::response::foilgroup{"$name.text"} = $text;
                    300:       }
1.2       albertel  301:     }
1.6       albertel  302:   }
                    303:   if ($target eq 'edit') {
1.13      albertel  304:     $result.= &Apache::edit::tag_end($target,$token,'');
1.1       albertel  305:   }
1.13      albertel  306:   return $result;
1.1       albertel  307: }
                    308: 
1.7       albertel  309: sub insert_foil {
                    310:   return '
1.15      albertel  311: <foil name="" value="unused">
1.14      albertel  312: <startouttext />
                    313: <endouttext />
1.7       albertel  314: </foil>';
                    315: }
1.1       albertel  316: 1;
                    317: __END__
                    318:  

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