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

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

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