File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.130: download - view: text, annotated - select for diffs
Tue Dec 6 10:03:57 2005 UTC (18 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- add correct bubble to exam printout
- optionrepsonse prints multiple answer lines for exam printout

    1: # LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.130 2005/12/06 10:03:57 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: package Apache::optionresponse;
   29: use strict;
   30: use Apache::response();
   31: use Apache::lonlocal;
   32: use Apache::lonnet;
   33: 
   34: BEGIN {
   35:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
   36: }
   37: 
   38: sub start_optionresponse {
   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   40:   my $result='';
   41:   #when in a option response use these
   42:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup','drawoptionlist'));
   43:   push (@Apache::lonxml::namespace,'optionresponse');
   44:   my $id = &Apache::response::start_response($parstack,$safeeval);
   45:   %Apache::hint::option=();
   46:   undef(%Apache::response::foilnames);
   47:   if ($target eq 'edit') {
   48:     $result.=&Apache::edit::start_table($token).
   49: 	"<tr><td>Multiple Option Response Question ".
   50: 	&Apache::loncommon::help_open_topic('Option_Response_Problems')."</td><td>Delete:".
   51: 	&Apache::edit::deletelist($target,$token)
   52: 	."</td><td>&nbsp;".
   53:         &Apache::edit::end_row().
   54:         &Apache::edit::start_spanning_row().
   55:         "\n";
   56:     $result.=&Apache::edit::text_arg('Max Number Of Shown Foils:','max',
   57: 				     $token,'4').
   58:         &Apache::edit::select_arg('Randomize Foil Order','randomize',
   59: 				  ['yes','no'],$token).
   60:         &Apache::edit::select_arg(&mt('Display of options when printed'),'TeXlayout',
   61: 				  [['horizontal',&mt('Normal list')],
   62: 				   ['vertical',&mt('Listed in a vertical column')]],$token).
   63:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
   64:   } elsif ($target eq 'modified') {
   65:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   66: 						 $safeeval,'max','randomize',
   67: 						 'TeXlayout');
   68:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   69:   } elsif ($target eq 'meta') {
   70:     $result=&Apache::response::meta_package_write('optionresponse');
   71:   } elsif ($target eq 'analyze') {
   72:     my $part_id="$Apache::inputtags::part.$id";
   73:     push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   74:   }
   75:   return $result;
   76: }
   77: 
   78: sub end_optionresponse {
   79:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   80:   &Apache::response::end_response;
   81:   pop @Apache::lonxml::namespace;
   82:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup','drawoptionlist'));
   83:   my $result;
   84:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   85:   undef(%Apache::response::foilnames);
   86:   return $result;
   87: }
   88: 
   89: %Apache::response::foilgroup=();
   90: sub start_foilgroup {
   91:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   92: 
   93:   my $result='';
   94:   %Apache::response::foilgroup=();
   95:   $Apache::optionresponse::conceptgroup=0;
   96:   &Apache::response::pushrandomnumber();
   97:   if ($target eq 'edit') {
   98:     my $optionlist="<option></option>\n";
   99:     my $option;
  100:     my @opt;
  101:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
  102:     my $count=1;
  103:     foreach $option (@opt) {
  104:       $optionlist.="<option value=\"$count\">$option</option>\n";
  105:       $count++;
  106:     }
  107:     my $insertlist=&Apache::edit::insertlist($target,$token);
  108:     $result.=&Apache::edit::start_table($token);
  109:     $result.= (<<ENDTABLE);
  110:       <tr><td>Select Options</td>
  111:         <td>
  112: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
  113:         </td>
  114:         <td>Delete an Option:
  115: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
  116: ENDTABLE
  117:     $result.= &Apache::edit::end_row();
  118:     $result.= &Apache::edit::start_spanning_row();
  119:     $result.= $insertlist.'<br />';
  120:   }
  121:   if ($target eq 'modified') {
  122:     my @options;
  123:     my $optchanged=0;
  124:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  125:     if ($env{"form.$Apache::lonxml::curdepth.deleteopt"}) {
  126:       my $delopt=$env{"form.$Apache::lonxml::curdepth.deleteopt"};
  127:       &Apache::lonxml::debug("Deleting :$delopt:");
  128:       splice(@options,$delopt-1,1);
  129:       $optchanged=1;
  130:     }
  131:     if ($env{"form.$Apache::lonxml::curdepth.options"}) {
  132:       my $newopt = $env{"form.$Apache::lonxml::curdepth.options"};
  133:       if ($options[0]) {
  134: 	push(@options,$newopt);
  135:       } else {
  136: 	$options[0]=$newopt;
  137:       }
  138:       $optchanged=1;
  139:     }
  140:     if ($optchanged) {
  141:       $result = "<foilgroup options=\"(";
  142:       foreach my $option (@options) {
  143: 	$option=~s/\'/\\\'/g;
  144: 	&Apache::lonxml::debug("adding option :$option:");
  145: 	$result .="'".$option."',";
  146:       }
  147:       chop $result;
  148:       $result.=')">';
  149:     } # else nothing changed so just use the default mechanism
  150:   }
  151:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  152:       $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}
  153:                    \begin{enumerate} ';
  154:   }
  155:   return $result;
  156: }
  157: 
  158: sub end_foilgroup {
  159:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  160:   
  161:   my $result;
  162:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  163:       $target eq 'tex' || $target eq 'analyze') {
  164:     my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
  165:     my $name;
  166:     my @opt;
  167:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  168:     &Apache::lonxml::debug("options:".join(':',@opt));
  169: 
  170:     my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
  171: 					     -2,0);
  172:     if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
  173:     &Apache::lonxml::debug("Options are $#opt");
  174:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  175:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  176: 					       $safeeval,'-2');
  177:     if ($target eq 'web' || $target eq 'tex') {
  178:       $result.=&displayfoils($target,$max,$randomize,$TeXlayout,@opt);
  179:     } elsif ( $target eq 'answer') {
  180:       $result.=&displayanswers($max,$randomize,@opt);
  181:     } elsif ( $target eq 'analyze') {
  182: 	my @shown = &whichfoils($max,$randomize);
  183: 	&Apache::response::analyze_store_foilgroup(\@shown,
  184: 						  ['text','value','location']);
  185: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  186: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
  187:     } elsif ( $target eq 'grade') {
  188:       if ( &Apache::response::submitted()) {
  189: 	my @whichopt = &whichfoils($max,$randomize);
  190: 	my $temp=1;my $name;
  191: 	my %responsehash;
  192: 	my %grade;
  193: 	my $right=0;
  194: 	my $wrong=0;
  195: 	my $ignored=0;
  196: 	foreach $name (@whichopt) {
  197: 	  my $response=&Apache::response::getresponse($temp);
  198: 	  if ($env{'form.submitted'} eq 'scantron' && $response=~/\S/) {
  199: 	      $response = $opt[$response];
  200: 	  }
  201: 	  if ( $response =~ /[^\s]/) {
  202: 	    $responsehash{$name}=$response;
  203: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  204: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  205: 	    if ($value eq $response) {
  206: 		$grade{$name}='1'; $right++;
  207: 	    } else {
  208: 		$grade{$name}='0'; $wrong++;
  209: 	    }
  210: 	  } else {
  211: 	    $ignored++;
  212: 	  }
  213: 	  $temp++;
  214: 	}
  215: 	my $part=$Apache::inputtags::part;
  216: 	my $id = $Apache::inputtags::response['-1'];
  217: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  218: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
  219: 	my %previous=&Apache::response::check_for_previous($responsestr,
  220: 							   $part,$id);
  221: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  222: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  223: 	    $responsestr;
  224: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  225: 	
  226: 	if ($Apache::lonhomework::type eq 'survey') {
  227: 	    if ($ignored == 0) {
  228: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  229: 		&Apache::response::handle_previous(\%previous,$ad);
  230: 	    } elsif ($wrong==0 && $right==0) {
  231: 	    } else {
  232: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  233: 		&Apache::response::handle_previous(\%previous,$ad);
  234: 	    }
  235: 	} elsif (!$Apache::lonhomework::scantronmode) {
  236: 	    my $ad;
  237: 	    if ($wrong==0 && $ignored==0) {
  238: 		$ad='EXACT_ANS';
  239: 	    } elsif ($wrong==0 && $right==0) {
  240: 		#nothing submitted
  241: 	    } else {
  242: 		if ($ignored==0) {
  243: 		    $ad='INCORRECT';
  244: 		} else {
  245: 		    $ad='MISSING_ANSWER';
  246: 		}
  247: 	    }
  248: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  249: 	    &Apache::response::handle_previous(\%previous,$ad);
  250: 	} else {
  251: 	    my $ad;
  252: 	    if ($wrong==0 && $right==0) {
  253: 		#nothing submitted only assign a score if we 
  254: 		#need to override a previous grade
  255: 		if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
  256: 		    $ad='ASSIGNED_SCORE';
  257: 		}
  258: 	    } else {
  259: 		$ad='ASSIGNED_SCORE';
  260: 	    }
  261: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  262: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  263: 		$right/(scalar(@whichopt));
  264: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  265: 		scalar(@whichopt);
  266: 	}
  267:       }
  268:     }
  269:     &Apache::lonxml::increment_counter(&getfoilcounts($max));
  270:   } elsif ($target eq 'edit') {
  271:     $result.=&Apache::edit::end_table();
  272:   }
  273:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  274:       $result .= '\end{enumerate}';
  275:   }
  276:   &Apache::response::poprandomnumber();
  277:   return $result;
  278: }
  279: 
  280: sub getfoilcounts {
  281:   my ($max)=@_;
  282:   # +1 since instructors will count from 1
  283:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  284:   if (&Apache::response::showallfoils()) { $max=$count; }
  285:   if ($count>$max) { $count=$max } 
  286:   &Apache::lonxml::debug("Count is $count from $max");
  287:   return $count;
  288: }
  289: 
  290: sub whichfoils {
  291:     my ($max,$randomize)=@_;
  292:     return &Apache::response::whichorder($max,$randomize,
  293: 					 &Apache::response::showallfoils(),
  294: 					 \%Apache::response::foilgroup);
  295: }
  296: 
  297: sub displayanswers {
  298:     my ($max,$randomize,@opt)=@_;
  299:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  300:     my @names = @{ $Apache::response::foilgroup{'names'} };
  301:     my @whichopt = &whichfoils($max,$randomize);
  302:     my $result;
  303:     if ($Apache::lonhomework::type eq 'exam') {
  304: 	my $i = 0;
  305: 	my %opt = map { ($_,$i++) } @opt;
  306: 	
  307: 	$i = 0;
  308: 	my @alphabet = ('A'..'Z');
  309: 	foreach my $name (@whichopt) {
  310: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
  311: 	    $result.=&Apache::response::answer_part('optionresponse',
  312: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
  313: 	    $result.=&Apache::response::answer_part('optionresponse',
  314: 						    $Apache::response::foilgroup{$name.'.value'});
  315: 	    $result.=&Apache::response::answer_footer('optionresponse');
  316: 	}
  317:     } else {
  318: 	$result=&Apache::response::answer_header('optionresponse');
  319: 	foreach my $name (@whichopt) {
  320: 	    $result.=&Apache::response::answer_part('optionresponse',
  321: 						    $Apache::response::foilgroup{$name.'.value'});
  322: 	}
  323: 	$result.=&Apache::response::answer_footer('optionresponse');
  324:     }
  325:     return $result;
  326: }
  327: 
  328: sub check_for_invalid {
  329:     my ($names,$options) = @_;
  330:     my %bad_names;
  331:     foreach my $name (@{ $names }) {
  332: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  333: 	my $found=0;
  334: 	foreach my $option (@{ $options }) {
  335: 	    if ($value eq $option) { $found=1; }
  336: 	}
  337: 	if (!$found) { $bad_names{$name}=$value; }
  338:     }
  339:     if (%bad_names) {
  340: 	my $error=&mt('The question can not be gotten correct, '.
  341: 	    'the following foils in the &lt;optionresponse&gt; '.
  342: 	    'have invalid correct options').' <br /><tt>'.
  343: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
  344: 	    "</tt>";
  345: 	&Apache::lonxml::error($error);
  346:     }
  347: }
  348: 
  349: sub displayfoils {
  350:   my ($target,$max,$randomize,$TeXlayout,@opt)=@_;
  351:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  352:   my @names = @{ $Apache::response::foilgroup{'names'} };
  353:   my @truelist;
  354:   my @falselist;
  355:   my $result;  
  356:   my $name;
  357:   my $displayoptionintex=1;
  358:   my @alphabet = ('A'..'Z');
  359:   my @whichopt = &whichfoils($max,$randomize);
  360:   &check_for_invalid(\@whichopt,\@opt);
  361:   my $part=$Apache::inputtags::part;
  362:   my $id=$Apache::inputtags::response[-1];
  363:   my $break;
  364:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  365:   if ( ($target ne 'tex') &&
  366:        &Apache::response::show_answer() ) {
  367:     my $temp=1;
  368:     foreach $name (@whichopt) {
  369: 	my $text=$Apache::response::foilgroup{$name.'.text'};
  370: 	my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  371: 	my $lastopt=$lastresponse{$name};
  372: 	if ($text!~/^\s*$/) { $break='<br />'; }
  373: 	$result.=$break;
  374: 	if ($target eq 'web') {
  375: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  376: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
  377: 		if ($text=~/^\s*$/) {
  378: 		    $text=$value.$text;
  379: 		} else {
  380: 		    $text='<b>'.$value.':</b> '.$text;
  381: 		}
  382: 	    } else {
  383: 		$text='&#149;'.$text;
  384: 	    }
  385: 	    $result.=$text."\n";
  386: 	}
  387:       if ($Apache::lonhomework::type eq 'exam') {
  388: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  389:       }
  390:       $temp++;
  391:     }
  392:   } else {
  393:     my $temp=1;
  394:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  395:     my $internal_counter=$Apache::lonxml::counter;
  396:     foreach $name (@whichopt) {
  397:       my $text=$Apache::response::foilgroup{$name.'.text'};
  398:       if ($text!~/^\s*$/) {
  399: 	  if ($target eq 'tex') {
  400: 	      $break='\vskip 0 mm ';
  401: 	  } elsif ($target eq 'web') {
  402: 	      $break='<br />';
  403: 	  }
  404:       }
  405:       my $lastopt=$lastresponse{$name};
  406:       my $optionlist="<option></option>\n";
  407:       foreach my $option (@opt) {
  408: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
  409: 	  if ($option eq $lastopt) {
  410: 	      $optionlist.="<option value='".$escopt."' selected=\"on\">$option</option>\n";
  411: 	  } else {
  412: 	      $optionlist.="<option value='".$escopt."'>$option</option>\n";
  413: 	  }
  414:       }
  415:       if ($target ne 'tex') {
  416: 	  if ($Apache::lonhomework::type ne 'exam') {
  417: 	      $optionlist='<select name="HWVAL_'.
  418: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
  419: 		  $optionlist."</select>\n";
  420: 	  } else {
  421: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
  422: 	  }
  423: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
  424: 	      if ($Apache::lonhomework::type ne 'exam') {
  425: 		  $text='&#149;'.$text;
  426: 	      }
  427: 	  } else {
  428: 	      if ($Apache::lonhomework::type ne 'exam') {
  429: 		  $text=$optionlist.$text;
  430: 	      }
  431: 	  }
  432: 	  $result.=$break.$text."\n";
  433: 	  if ($Apache::lonhomework::type eq 'exam') {
  434: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  435: 	  }
  436: 	  $temp++;
  437:       } else {
  438: 	  my $texoptionlist='';
  439: 	  if ($displayoptionintex &&
  440: 	      $Apache::lonhomework::type ne 'exam') {
  441: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
  442: 	  }
  443: 	  if ($text=~/<drawoptionlist\s*\/>/) {
  444: 	      $text=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
  445: 	  }
  446: 
  447: 	  if ($text=~m/\\item /) {
  448: 	      if ($Apache::lonhomework::type eq 'exam') {
  449: 	          $text=~s/\\item/\\vskip 2 mm/;
  450: 	      }
  451: 	      $result.= $texoptionlist.$text;
  452: 	  } else {
  453: 	      if ($Apache::lonhomework::type eq 'exam') {
  454: 		  $result.= $texoptionlist.'  '.$text;
  455: 	      } else {
  456: 		  if ($text=~/\S/) {$result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;} else {$result.= $texoptionlist;}
  457: 	      }
  458: 	  }
  459: 	  if ($Apache::lonhomework::type eq 'exam') {
  460: 	      $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.
  461: 		  $internal_counter.'}.]'.&bubbles(\@alphabet,\@opt).
  462: 		  '\end{enumerate} \vskip -8 mm \strut ';
  463: 	      $internal_counter++;
  464: 	  }
  465: 	  $displayoptionintex=0;
  466:       }
  467:     }
  468:   }
  469:   if ($target ne 'tex') {
  470:       return $result.$break;
  471:   } else {
  472:       return $result;
  473:   }
  474: }
  475: 
  476: 
  477: sub optionlist_correction {
  478:     my ($TeXlayout,@options) = @_;
  479:     my $texoptionlist='\\item [] Choices: ';
  480:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
  481:     if (scalar(@options) > 0) {
  482: 	foreach my $option (@options) {
  483: 	    $texoptionlist.='{\bf '.
  484: 		&Apache::lonxml::latex_special_symbols($option).
  485: 		'}';
  486: 	    if ($TeXlayout eq 'vertical') {
  487: 		$texoptionlist.=' \vskip 0 mm ',
  488: 	    } else {
  489: 		$texoptionlist.=', ';
  490: 	    }
  491: 	}
  492: 	$texoptionlist=~s/, $//;
  493: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
  494:     } else {
  495: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
  496:     }
  497:     return $texoptionlist;
  498: }
  499: 
  500: 
  501: sub webbubbles {
  502:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
  503:     my @opt=@$ropt; 
  504:     my @alphabet=@$ralphabet;
  505:     my $result='';
  506:     my $number_of_bubbles = $#opt + 1;
  507:     $result.= '<table border="1"><tr>';
  508:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  509: 	my $checked='';
  510: 	if ($lastopt eq $opt[$ind]) {
  511: 	    $checked=' checked="on" ';
  512: 	}
  513: 	$result.='<td><input type="radio" name="HWVAL_'.
  514: 	    $Apache::inputtags::response['-1'].':'.$temp.
  515: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
  516: 	if ($alphabet[$ind]) {
  517: 	    $result.=$alphabet[$ind].': ';
  518: 	}
  519: 	$result.=$opt[$ind].'</td>';
  520:     }
  521:     $result.='</tr></table>';
  522:     return $result;
  523: }
  524: 
  525: 
  526: sub bubbles {
  527:     my ($ralphabet,$ropt,$response) = @_;
  528:     my @alphabet = @$ralphabet;
  529:     my @opt = @$ropt;
  530:     my ($result,$head,$line) =('','','');
  531:     my $number_of_bubbles = $#opt + 1;
  532:     my $current_length = 0;
  533:     my $textwidth;
  534:     if ($env{'form.textwidth'} ne '') {
  535: 	$env{'form.textwidth'}=~/(\d+)/;
  536: 	$textwidth=$1;
  537:     } else {
  538: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
  539: 	$textwidth=$1.'.'.$2;
  540:     }
  541:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
  542: 	my $leftmargin;
  543: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
  544: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
  545: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
  546: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+4)*2;
  547: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
  548: 	    $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
  549: 	    $head.='lr';
  550: 	} else {
  551: 	    $line=~s/\&\s*$//;
  552: 	    $result.='\vskip -2 mm\noindent\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
  553: 	    $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
  554: 	    $head ='lr';
  555: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
  556: 	}
  557: 
  558:     }
  559:     return $result;
  560: }
  561: 
  562: 
  563: sub start_conceptgroup {
  564:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  565:   $Apache::optionresponse::conceptgroup=1;
  566:   %Apache::response::conceptgroup=();
  567:   my $result;
  568:   if ($target eq 'edit') {
  569:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  570:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  571:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  572:   }
  573:   if ($target eq 'modified') {
  574:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  575:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  576:   }
  577:   return $result;
  578: }
  579: 
  580: sub end_conceptgroup {
  581:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  582:   $Apache::optionresponse::conceptgroup=0;
  583:   my $result='';
  584:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  585:       $target eq 'tex' || $target eq 'analyze') {
  586:     #if not there aren't any foils to display and thus no question
  587:       &Apache::response::pick_foil_for_concept($target,
  588: 					       ['value','text','location'],
  589: 					       \%Apache::hint::option,
  590: 					       $parstack,$safeeval);
  591:   } elsif ($target eq 'edit') {
  592:     $result=&Apache::edit::end_table();
  593:   }
  594:   return $result;
  595: }
  596: 
  597: sub insert_conceptgroup {
  598:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  599:   return $result;
  600: }
  601: 
  602: sub start_foil {
  603:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  604:   my $result='';
  605:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
  606:       &Apache::lonxml::startredirection;
  607:       if ($target eq 'analyze') {
  608: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  609:       }
  610:   } elsif ($target eq 'edit') {
  611:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  612:     my $level='-2';
  613:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  614:     my @opt;
  615:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
  616:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  617:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
  618: 					       ['unused',(@opt)],$token,'15');
  619:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  620: 					     $safeeval,'-3');
  621:     if ($randomize ne 'no') {
  622: 	$result.=&Apache::edit::select_arg('Location:','location',
  623: 					   ['random','top','bottom'],$token);
  624:     }
  625:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  626:   } elsif ($target eq 'modified') {
  627:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  628: 						 'value','name','location');
  629:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  630:   } 
  631:   return $result;
  632: }
  633: 
  634: sub end_foil {
  635:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  636:   my $text ='';
  637:   my $result = '';
  638:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  639:       $text=&Apache::lonxml::endredirection;
  640:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
  641:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
  642:   }
  643:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
  644:       || $target eq 'tex' || $target eq 'analyze') {
  645:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  646:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  647: 	$text='\vskip 5mm $\triangleright$ '.$text;
  648:     }
  649:     if ($value ne 'unused') {
  650:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  651:       &Apache::lonxml::debug("Got a name of :$name:");
  652:       if ($name eq "") {
  653: 	  &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
  654: 	  $name=$Apache::lonxml::curdepth;
  655:       }
  656:       &Apache::lonxml::debug("Using a name of :$name:");
  657:       if (defined($Apache::response::foilnames{$name})) {
  658: 	  &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
  659:       }
  660:       $Apache::response::foilnames{$name}++;
  661:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
  662:       if ( $Apache::optionresponse::conceptgroup
  663: 	   && !&Apache::response::showallfoils() ) {
  664: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  665: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  666: 	$Apache::response::conceptgroup{"$name.text"} = $text;
  667: 	$Apache::response::conceptgroup{"$name.location"} = $location;
  668:       } else {
  669: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  670: 	$Apache::response::foilgroup{"$name.value"} = $value;
  671: 	$Apache::response::foilgroup{"$name.text"} = $text;
  672: 	$Apache::response::foilgroup{"$name.location"} = $location;
  673:       }
  674:     }
  675:   }
  676:   if ($target eq 'edit') {
  677:     $result.= &Apache::edit::tag_end($target,$token,'');
  678:   }
  679:   return $result;
  680: }
  681: 
  682: sub start_drawoptionlist {
  683:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  684:     if ($target ne 'meta') {
  685: 	return $token->[4];
  686:     }
  687: }
  688: 
  689: sub end_drawoptionlist {
  690:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  691:     if ($target ne 'meta') {
  692: 	return $token->[2];
  693:     }
  694: }
  695: 
  696: sub insert_foil {
  697:   return '
  698: <foil name="" value="unused">
  699: <startouttext />
  700: <endouttext />
  701: </foil>';
  702: }
  703: 1;
  704: __END__
  705:  

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