File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.16: download - view: text, annotated - select for diffs
Sat Jun 16 20:33:33 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added easy way to get and set options for a tag

    1: # The LearningOnline Network with CAPA
    2: # option list style responses
    3: # 2/21 Guy
    4: package Apache::optionresponse;
    5: use strict;
    6: use Apache::response;
    7: 
    8: sub BEGIN {
    9:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
   10: }
   11: 
   12: sub start_optionresponse {
   13:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   14:   my $result='';
   15:   #when in a radiobutton response use these
   16:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
   17:   push (@Apache::lonxml::namespace,'optionresponse');
   18:   my $id = &Apache::response::start_response($parstack,$safeeval);
   19:   if ($target eq 'edit') {
   20:     $result.=&Apache::edit::start_table($token)."<tr><td>Multiple Option Response Question</td>
   21: <td>Delete:".
   22:   &Apache::edit::deletelist($target,$token)
   23: ."</td><td>&nbsp;</td></tr><tr><td colspan=\"3\">\n";
   24:   }
   25: 
   26:   return $result;
   27: }
   28: 
   29: sub end_optionresponse {
   30:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   31:   &Apache::response::end_response;
   32:   pop @Apache::lonxml::namespace;
   33:   my $result;
   34:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   35:   return $result;
   36: }
   37: 
   38: sub insert_optionresponse {
   39:   return '
   40: <optionresponse max="10">
   41:     <foilgroup options=\"\">
   42:     </foilgroup>
   43: </optionresponse>';
   44: }
   45: 
   46: %Apache::response::foilgroup={};
   47: sub start_foilgroup {
   48:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   49: 
   50:   my $result='';
   51:   %Apache::response::foilgroup={};
   52:   $Apache::optionresponse::conceptgroup=0;
   53:   &Apache::response::setrandomnumber();
   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);
   59:     my $count=0;
   60:     foreach $option (@opt) {
   61:       $optionlist.="<option value=\"$count\">$option</option>\n";
   62:       $count++;
   63:     }
   64:     my $insertlist=&Apache::edit::insertlist($target,$token);
   65:     $result.=&Apache::edit::start_table($token);
   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;
   80:     my $optchanged=0;
   81:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
   82:     if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
   83:       my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
   84:       &Apache::lonxml::debug("Deleting :$delopt:");
   85:       splice(@options,$delopt,1);
   86:       $optchanged=1;
   87:     }
   88:     if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
   89:       my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
   90:       if ($options[0]) {
   91: 	push(@options,$newopt);
   92:       } else {
   93: 	$options[0]=$newopt;
   94:       }
   95:       $optchanged=1;
   96:     }
   97:     if ($optchanged) {
   98:       $result = "<foilgroup options=\"(";
   99:       foreach my $option (@options) {
  100: 	$option=~s/\'/\\\'/;
  101: 	&Apache::lonxml::debug("adding option :$option:");
  102: 	$result .="'".$option."',";
  103:       }
  104:       chop $result;
  105:       $result.=')">';
  106:     } # else nothing changed so just use the default mechanism
  107:   }
  108:   return $result;
  109: }
  110: 
  111: sub end_foilgroup {
  112:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  113:   
  114:   my $result;
  115:   if ($target eq 'grade' || $target eq 'web') {
  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;
  121:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  122:     &Apache::lonxml::debug("Options are $#opt");
  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;
  132: 	my $ignored=0;
  133: 	foreach $name (@whichopt) {
  134: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
  135: 	  $allresponse.="$response:";
  136: 	  if ( $response =~ /[^\s]/) {
  137: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
  138: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  139: 	    if ($value eq $response) {$right++;} else {$wrong++;}
  140: 	  } else {
  141: 	    $ignored++;
  142: 	  }
  143: 	  $temp++;
  144: 	}
  145: 	my $id = $Apache::inputtags::response['-1'];
  146: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
  147: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  148: 	if ($wrong==0 && $ignored==0) {
  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:   }
  156:   if ($target eq 'edit') {
  157:     $result.=&Apache::edit::end_table();
  158:   }
  159:   return $result;
  160: }
  161: 
  162: sub getfoilcounts {
  163:   my ($parstack,$safeeval)=@_;
  164:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  165:   # +1 since instructors will count from 1
  166:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  167:   return ($count,$max);
  168: }
  169: 
  170: sub whichfoils {
  171:   my ($max)=@_;
  172:   my @names = @{ $Apache::response::foilgroup{'names'} };
  173:   my @whichopt =();
  174:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
  175:     &Apache::lonxml::debug("Have $#whichopt max is $max");
  176:     my $aopt=int(rand($#names+1));
  177:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  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: 
  215: 
  216: sub start_conceptgroup {
  217:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  218:   $Apache::optionresponse::conceptgroup=1;
  219:   %Apache::response::conceptgroup={};
  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:   }
  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:   }
  231:   return $result;
  232: }
  233: 
  234: sub end_conceptgroup {
  235:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  236:   $Apache::optionresponse::conceptgroup=0;
  237:   my $result='';
  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"};
  245:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  246:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  247:     &Apache::lonxml::debug("Selecting $name in $concept");
  248:   }
  249:   if ($target eq 'edit') {
  250:     $result=&Apache::edit::end_table();
  251:   }
  252:   return $result;
  253: }
  254: 
  255: sub insert_conceptgroup {
  256:   my $result="\t\t<conceptgroup concept=\"\">\n".&insert_foil()."\t\t</conceptgroup>\n";
  257:   return $result;
  258: }
  259: 
  260: sub start_foil {
  261:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  262:   my $result='';
  263:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  264:   if ($target eq 'edit') {
  265:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  266:     my $level='-2';
  267:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  268:     my @opt;
  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";
  272:     foreach my $option (@opt) {
  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') {
  287:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'value','name');
  288:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  289:   }
  290:   return $result;
  291: }
  292: 
  293: sub end_foil {
  294:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  295:   my $text ='';
  296:   my $result = '';
  297:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  298:   if ($target eq 'web' || $target eq 'grade') {
  299:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  300:     if ($value ne 'unused') {
  301:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  302:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  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:       }
  312:     }
  313:   }
  314:   if ($target eq 'edit') {
  315:     $result.= &Apache::edit::tag_end($target,$token,'');
  316:   }
  317:   return $result;
  318: }
  319: 
  320: sub insert_foil {
  321:   return '
  322: <foil name="" value="unused">
  323: <startouttext />
  324: <endouttext />
  325: </foil>';
  326: }
  327: 1;
  328: __END__
  329:  

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