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

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

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