File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.14: download - view: text, annotated - select for diffs
Tue Jun 12 22:42:55 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added color to edit mode <table>
- added start_table function to coordinate the color assignment and other attributes for <table>.

    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></tr><tr><td colspan=\"3\">\n";
   24:   }
   25: 
   26:   return $result;
   27: }
   28: 
   29: sub end_optionresponse {
   30:   &Apache::response::end_response;
   31:   pop @Apache::lonxml::namespace;
   32:   return '';
   33: }
   34: 
   35: sub insert_optionresponse {
   36:   return '
   37: <optionresponse max="10">
   38:     <foilgroup options=\"\">
   39:     </foilgroup>
   40: </optionresponse>';
   41: }
   42: 
   43: %Apache::response::foilgroup={};
   44: sub start_foilgroup {
   45:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   46: 
   47:   my $result='';
   48:   %Apache::response::foilgroup={};
   49:   $Apache::optionresponse::conceptgroup=0;
   50:   &Apache::response::setrandomnumber();
   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);
   56:     my $count=0;
   57:     foreach $option (@opt) {
   58:       $optionlist.="<option value=\"$count\">$option</option>\n";
   59:     }
   60:     my $insertlist=&Apache::edit::insertlist($target,$token);
   61:     $result.=&Apache::edit::start_table($token);
   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;
   76:     my $optchanged=0;
   77:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
   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:     }
   83:     if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
   84:       my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
   85:       if ($options[0]) {
   86: 	push(@options,$newopt);
   87:       } else {
   88: 	$options[0]=$newopt;
   89:       }
   90:       $optchanged=1;
   91:     }
   92:     if ($optchanged) {
   93:       $result = "<foilgroup options=\"(";
   94:       foreach my $option (@options) {
   95: 	$option=~s/\'/\\\'/;
   96: 	&Apache::lonxml::debug("adding option :$option:");
   97: 	$result .="'".$option."',";
   98:       }
   99:       chop $result;
  100:       $result.=')">';
  101:     } # else nothing changed so just use the default mechanism
  102:   }
  103:   return $result;
  104: }
  105: 
  106: sub end_foilgroup {
  107:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  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;
  116:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  117:     &Apache::lonxml::debug("Options are $#opt");
  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;
  127: 	my $ignored=0;
  128: 	foreach $name (@whichopt) {
  129: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
  130: 	  $allresponse.="$response:";
  131: 	  if ( $response =~ /[^\s]/) {
  132: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
  133: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  134: 	    if ($value eq $response) {$right++;} else {$wrong++;}
  135: 	  } else {
  136: 	    $ignored++;
  137: 	  }
  138: 	  $temp++;
  139: 	}
  140: 	my $id = $Apache::inputtags::response['-1'];
  141: 	$Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
  142: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  143: 	if ($wrong==0 && $ignored==0) {
  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)=@_;
  156:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  157:   # +1 since instructors will count from 1
  158:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  159:   return ($count,$max);
  160: }
  161: 
  162: sub whichfoils {
  163:   my ($max)=@_;
  164:   my @names = @{ $Apache::response::foilgroup{'names'} };
  165:   my @whichopt =();
  166:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
  167:     &Apache::lonxml::debug("Have $#whichopt max is $max");
  168:     my $aopt=int(rand($#names+1));
  169:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  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: 
  207: 
  208: sub start_conceptgroup {
  209:   $Apache::optionresponse::conceptgroup=1;  
  210:   %Apache::response::conceptgroup={};
  211:   return '';
  212: }
  213: 
  214: sub end_conceptgroup {
  215:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  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"};
  224:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  225:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  226:     &Apache::lonxml::debug("Selecting $name in $concept");
  227:   }
  228:   return '';
  229: }
  230: 
  231: sub start_foil {
  232:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  233:   my $result='';
  234:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  235:   if ($target eq 'edit') {
  236:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  237:     my @opt;
  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\">";
  245:   }
  246:   return $result;
  247: }
  248: 
  249: sub end_foil {
  250:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  251:   my $text ='';
  252:   my $result = '';
  253:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  254:   if ($target eq 'web' || $target eq 'grade') {
  255:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  256:     if ($value ne 'unused') {
  257:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  258:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  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:       }
  268:     }
  269:   }
  270:   if ($target eq 'edit') {
  271:     $result.= &Apache::edit::tag_end($target,$token,'');
  272:   }
  273:   return $result;
  274: }
  275: 
  276: sub insert_foil {
  277:   return '
  278: <foil name="" value="">
  279: <startouttext />
  280: <endouttext />
  281: </foil>';
  282: }
  283: 1;
  284: __END__
  285:  

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