File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.125: download - view: text, annotated - select for diffs
Thu Apr 7 06:56:22 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_1_99_1_tmcc, version_1_99_0_tmcc, HEAD
- ENV -> env

    1: # LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.125 2005/04/07 06:56:22 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:     my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
  169: 					     -2,0);
  170:     if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
  171:     &Apache::lonxml::debug("Options are $#opt");
  172:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  173:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  174: 					       $safeeval,'-2');
  175:     if ($target eq 'web' || $target eq 'tex') {
  176:       $result.=&displayfoils($target,$max,$randomize,$TeXlayout,@opt);
  177:     } elsif ( $target eq 'answer') {
  178:       $result.=&displayanswers($max,$randomize,@opt);
  179:     } elsif ( $target eq 'analyze') {
  180: 	my @shown = &whichfoils($max,$randomize);
  181: 	&Apache::response::analyze_store_foilgroup(\@shown,
  182: 						  ['text','value','location']);
  183: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  184: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
  185:     } elsif ( $target eq 'grade') {
  186:       if ( &Apache::response::submitted()) {
  187: 	my @whichopt = &whichfoils($max,$randomize);
  188: 	my $temp=1;my $name;
  189: 	my %responsehash;
  190: 	my %grade;
  191: 	my $right=0;
  192: 	my $wrong=0;
  193: 	my $ignored=0;
  194: 	foreach $name (@whichopt) {
  195: 	  my $response=&Apache::response::getresponse($temp);
  196: 	  if ($env{'form.submitted'} eq 'scantron' && $response=~/\S/) {
  197: 	      $response = $opt[$response];
  198: 	  }
  199: 	  if ( $response =~ /[^\s]/) {
  200: 	    $responsehash{$name}=$response;
  201: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  202: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  203: 	    if ($value eq $response) {
  204: 		$grade{$name}='1'; $right++;
  205: 	    } else {
  206: 		$grade{$name}='0'; $wrong++;
  207: 	    }
  208: 	  } else {
  209: 	    $ignored++;
  210: 	  }
  211: 	  $temp++;
  212: 	}
  213: 	my $part=$Apache::inputtags::part;
  214: 	my $id = $Apache::inputtags::response['-1'];
  215: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  216: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
  217: 	my %previous=&Apache::response::check_for_previous($responsestr,
  218: 							   $part,$id);
  219: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  220: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  221: 	    $responsestr;
  222: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  223: 	
  224: 	if ($Apache::lonhomework::type eq 'survey') {
  225: 	    if ($ignored == 0) {
  226: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  227: 		&Apache::response::handle_previous(\%previous,$ad);
  228: 	    } elsif ($wrong==0 && $right==0) {
  229: 	    } else {
  230: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  231: 		&Apache::response::handle_previous(\%previous,$ad);
  232: 	    }
  233: 	} elsif (!$Apache::lonhomework::scantronmode) {
  234: 	    my $ad;
  235: 	    if ($wrong==0 && $ignored==0) {
  236: 		$ad='EXACT_ANS';
  237: 	    } elsif ($wrong==0 && $right==0) {
  238: 		#nothing submitted
  239: 	    } else {
  240: 		if ($ignored==0) {
  241: 		    $ad='INCORRECT';
  242: 		} else {
  243: 		    $ad='MISSING_ANSWER';
  244: 		}
  245: 	    }
  246: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  247: 	    &Apache::response::handle_previous(\%previous,$ad);
  248: 	} else {
  249: 	    my $ad;
  250: 	    if ($wrong==0 && $right==0) {
  251: 		#nothing submitted only assign a score if we 
  252: 		#need to override a previous grade
  253: 		if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
  254: 		    $ad='ASSIGNED_SCORE';
  255: 		}
  256: 	    } else {
  257: 		$ad='ASSIGNED_SCORE';
  258: 	    }
  259: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  260: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  261: 		$right/(scalar(@whichopt));
  262: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  263: 		scalar(@whichopt);
  264: 	}
  265:       }
  266:     }
  267:     &Apache::lonxml::increment_counter(&getfoilcounts($max));
  268:   } elsif ($target eq 'edit') {
  269:     $result.=&Apache::edit::end_table();
  270:   }
  271:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  272:       $result .= '\end{enumerate}';
  273:   }
  274:   &Apache::response::poprandomnumber();
  275:   return $result;
  276: }
  277: 
  278: sub getfoilcounts {
  279:   my ($max)=@_;
  280:   # +1 since instructors will count from 1
  281:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  282:   if (&Apache::response::showallfoils()) { $max=$count; }
  283:   if ($count>$max) { $count=$max } 
  284:   &Apache::lonxml::debug("Count is $count from $max");
  285:   return $count;
  286: }
  287: 
  288: sub whichfoils {
  289:     my ($max,$randomize)=@_;
  290:     return &Apache::response::whichorder($max,$randomize,
  291: 					 &Apache::response::showallfoils(),
  292: 					 \%Apache::response::foilgroup);
  293: }
  294: 
  295: sub displayanswers {
  296:   my ($max,$randomize,@opt)=@_;
  297:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  298:   my @names = @{ $Apache::response::foilgroup{'names'} };
  299:   my @whichopt = &whichfoils($max,$randomize);
  300:   my $result=&Apache::response::answer_header('optionresponse');
  301:   foreach my $name (@whichopt) {
  302:     $result.=&Apache::response::answer_part('optionresponse',
  303: 		     $Apache::response::foilgroup{$name.'.value'})
  304:   }
  305:   $result.=&Apache::response::answer_footer('optionresponse');
  306:   return $result;
  307: }
  308: 
  309: sub check_for_invalid {
  310:     my ($names,$options) = @_;
  311:     my %bad_names;
  312:     foreach my $name (@{ $names }) {
  313: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  314: 	my $found=0;
  315: 	foreach my $option (@{ $options }) {
  316: 	    if ($value eq $option) { $found=1; }
  317: 	}
  318: 	if (!$found) { $bad_names{$name}=$value; }
  319:     }
  320:     if (%bad_names) {
  321: 	my $error=&mt('The question can not be gotten correct, '.
  322: 	    'the following foils in the &lt;optionresponse&gt; '.
  323: 	    'have invalid correct options').' <br /><tt>'.
  324: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
  325: 	    "</tt>";
  326: 	&Apache::lonxml::error($error);
  327:     }
  328: }
  329: 
  330: sub displayfoils {
  331:   my ($target,$max,$randomize,$TeXlayout,@opt)=@_;
  332:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  333:   my @names = @{ $Apache::response::foilgroup{'names'} };
  334:   my @truelist;
  335:   my @falselist;
  336:   my $result;  
  337:   my $name;
  338:   my $displayoptionintex=1;
  339:   my @alphabet = ('A'..'Z');
  340:   my @whichopt = &whichfoils($max,$randomize);
  341:   &check_for_invalid(\@whichopt,\@opt);
  342:   my $part=$Apache::inputtags::part;
  343:   my $id=$Apache::inputtags::response[-1];
  344:   my $break;
  345:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  346:   if ( ($target ne 'tex') &&
  347:        &Apache::response::show_answer() ) {
  348:     my $temp=1;
  349:     foreach $name (@whichopt) {
  350: 	my $text=$Apache::response::foilgroup{$name.'.text'};
  351: 	my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  352: 	my $lastopt=$lastresponse{$name};
  353: 	if ($text!~/^\s*$/) { $break='<br />'; }
  354: 	$result.=$break;
  355: 	if ($target eq 'web') {
  356: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  357: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
  358: 		if ($text=~/^\s*$/) {
  359: 		    $text=$value.$text;
  360: 		} else {
  361: 		    $text=$value.': '.$text;
  362: 		}
  363: 	    } else {
  364: 		$text='&#149;'.$text;
  365: 	    }
  366: 	    $result.=$text."\n";
  367: 	}
  368:       if ($Apache::lonhomework::type eq 'exam') {
  369: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  370:       }
  371:       $temp++;
  372:     }
  373:   } else {
  374:     my $temp=1;
  375:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  376:     my $internal_counter=$Apache::lonxml::counter;
  377:     foreach $name (@whichopt) {
  378:       my $text=$Apache::response::foilgroup{$name.'.text'};
  379:       if ($text!~/^\s*$/) {
  380: 	  if ($target eq 'tex') {
  381: 	      $break='\vskip 0 mm ';
  382: 	  } elsif ($target eq 'web') {
  383: 	      $break='<br />';
  384: 	  }
  385:       }
  386:       my $lastopt=$lastresponse{$name};
  387:       my $optionlist="<option></option>\n";
  388:       my $option;
  389:       foreach $option (@opt) {
  390: 	if ($option eq $lastopt) {
  391: 	  $optionlist.="<option selected=\"on\">$option</option>\n";
  392: 	} else {
  393: 	  $optionlist.="<option>$option</option>\n";
  394: 	}
  395:       }
  396:       if ($target ne 'tex') {
  397: 	  if ($Apache::lonhomework::type ne 'exam') {
  398: 	      $optionlist='<select name="HWVAL_'.
  399: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
  400: 		  $optionlist."</select>\n";
  401: 	  } else {
  402: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
  403: 	  }
  404: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
  405: 	      if ($Apache::lonhomework::type ne 'exam') {
  406: 		  $text='&#149;'.$text;
  407: 	      }
  408: 	  } else {
  409: 	      if ($Apache::lonhomework::type ne 'exam') {
  410: 		  $text=$optionlist.$text;
  411: 	      }
  412: 	  }
  413: 	  $result.=$break.$text."\n";
  414: 	  if ($Apache::lonhomework::type eq 'exam') {
  415: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  416: 	  }
  417: 	  $temp++;
  418:       } else {
  419: 	  my $texoptionlist='';
  420: 	  if ($displayoptionintex &&
  421: 	      $Apache::lonhomework::type ne 'exam') {
  422: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
  423: 	  }
  424: 	  if ($text=~/<drawoptionlist\s*\/>/) {
  425: 	      $text=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
  426: 	  }
  427: 
  428: 	  if ($text=~m/\\item /) {
  429: 	      if ($Apache::lonhomework::type eq 'exam') {
  430: 	          $text=~s/\\item/\\vskip 2 mm/;
  431: 	      }
  432: 	      $result.= $texoptionlist.$text;
  433: 	  } else {
  434: 	      if ($Apache::lonhomework::type eq 'exam') {
  435: 		  $result.= $texoptionlist.'  '.$text;
  436: 	      } else {
  437: 		  if ($text=~/\S/) {$result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;} else {$result.= $texoptionlist;}
  438: 	      }
  439: 	  }
  440: 	  if ($Apache::lonhomework::type eq 'exam') {
  441: 	      $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.
  442: 		  $internal_counter.'}.]'.&bubbles(\@alphabet,\@opt).
  443: 		  '\end{enumerate} \vskip -8 mm \strut ';
  444: 	      $internal_counter++;
  445: 	  }
  446: 	  $displayoptionintex=0;
  447:       }
  448:     }
  449:   }
  450:   if ($target ne 'tex') {
  451:       return $result.$break;
  452:   } else {
  453:       return $result;
  454:   }
  455: }
  456: 
  457: 
  458: sub optionlist_correction {
  459:     my ($TeXlayout,@options) = @_;
  460:     my $texoptionlist='\\item [] Choices: ';
  461:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
  462:     if (scalar(@options) > 0) {
  463: 	foreach my $option (@options) {
  464: 	    $texoptionlist.='{\bf '.
  465: 		&Apache::lonxml::latex_special_symbols($option).
  466: 		'}';
  467: 	    if ($TeXlayout eq 'vertical') {
  468: 		$texoptionlist.=' \vskip 0 mm ',
  469: 	    } else {
  470: 		$texoptionlist.=', ';
  471: 	    }
  472: 	}
  473: 	$texoptionlist=~s/, $//;
  474: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
  475:     } else {
  476: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
  477:     }
  478:     return $texoptionlist;
  479: }
  480: 
  481: 
  482: sub webbubbles {
  483:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
  484:     my @opt=@$ropt; 
  485:     my @alphabet=@$ralphabet;
  486:     my $result='';
  487:     my $number_of_bubbles = $#opt + 1;
  488:     $result.= '<table border="1"><tr>';
  489:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  490: 	my $checked='';
  491: 	if ($lastopt eq $opt[$ind]) {
  492: 	    $checked=' checked="on" ';
  493: 	}
  494: 	$result.='<td><input type="radio" name="HWVAL_'.
  495: 	    $Apache::inputtags::response['-1'].':'.$temp.
  496: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
  497: 	if ($alphabet[$ind]) {
  498: 	    $result.=$alphabet[$ind].': ';
  499: 	}
  500: 	$result.=$opt[$ind].'</td>';
  501:     }
  502:     $result.='</tr></table>';
  503:     return $result;
  504: }
  505: 
  506: 
  507: sub bubbles {
  508:     my ($ralphabet,$ropt,$response) = @_;
  509:     my @alphabet = @$ralphabet;
  510:     my @opt = @$ropt;
  511:     my ($result,$head,$line) =('','','');
  512:     my $number_of_bubbles = $#opt + 1;
  513:     my $current_length = 0;
  514:     my $textwidth;
  515:     if ($env{'form.textwidth'} ne '') {
  516: 	$env{'form.textwidth'}=~/(\d+)/;
  517: 	$textwidth=$1;
  518:     } else {
  519: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
  520: 	$textwidth=$1.'.'.$2;
  521:     }
  522:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
  523: 	my $leftmargin;
  524: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
  525: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
  526: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
  527: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+4)*2;
  528: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
  529: 	    $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
  530: 	    $head.='lr';
  531: 	} else {
  532: 	    $line=~s/\&\s*$//;
  533: 	    $result.='\vskip -2 mm\noindent\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
  534: 	    $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
  535: 	    $head ='lr';
  536: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
  537: 	}
  538: 
  539:     }
  540:     return $result;
  541: }
  542: 
  543: 
  544: sub start_conceptgroup {
  545:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  546:   $Apache::optionresponse::conceptgroup=1;
  547:   %Apache::response::conceptgroup=();
  548:   my $result;
  549:   if ($target eq 'edit') {
  550:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  551:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  552:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  553:   }
  554:   if ($target eq 'modified') {
  555:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  556:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  557:   }
  558:   return $result;
  559: }
  560: 
  561: sub end_conceptgroup {
  562:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  563:   $Apache::optionresponse::conceptgroup=0;
  564:   my $result='';
  565:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  566:       $target eq 'tex' || $target eq 'analyze') {
  567:     #if not there aren't any foils to display and thus no question
  568:       &Apache::response::pick_foil_for_concept($target,
  569: 					       ['value','text','location'],
  570: 					       \%Apache::hint::option,
  571: 					       $parstack,$safeeval);
  572:   } elsif ($target eq 'edit') {
  573:     $result=&Apache::edit::end_table();
  574:   }
  575:   return $result;
  576: }
  577: 
  578: sub insert_conceptgroup {
  579:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  580:   return $result;
  581: }
  582: 
  583: sub start_foil {
  584:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  585:   my $result='';
  586:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
  587:       &Apache::lonxml::startredirection;
  588:       if ($target eq 'analyze') {
  589: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  590:       }
  591:   } elsif ($target eq 'edit') {
  592:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  593:     my $level='-2';
  594:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  595:     my @opt;
  596:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
  597:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  598:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
  599: 					       ['unused',(@opt)],$token,'15');
  600:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  601: 					     $safeeval,'-3');
  602:     if ($randomize ne 'no') {
  603: 	$result.=&Apache::edit::select_arg('Location:','location',
  604: 					   ['random','top','bottom'],$token);
  605:     }
  606:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  607:   } elsif ($target eq 'modified') {
  608:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  609: 						 'value','name','location');
  610:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  611:   } 
  612:   return $result;
  613: }
  614: 
  615: sub end_foil {
  616:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  617:   my $text ='';
  618:   my $result = '';
  619:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  620:       $text=&Apache::lonxml::endredirection;
  621:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
  622:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
  623:   }
  624:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
  625:       || $target eq 'tex' || $target eq 'analyze') {
  626:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  627:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  628: 	$text='\vskip 5mm $\triangleright$ '.$text;
  629:     }
  630:     if ($value ne 'unused') {
  631:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  632:       &Apache::lonxml::debug("Got a name of :$name:");
  633:       if (!$name) {
  634: 	  &Apache::lonxml::error("Foils without names exist. This can cause problems to malfunction.");
  635: 	  $name=$Apache::lonxml::curdepth;
  636:       }
  637:       &Apache::lonxml::debug("Using a name of :$name:");
  638:       if (defined($Apache::response::foilnames{$name})) {
  639: 	  &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
  640:       }
  641:       $Apache::response::foilnames{$name}++;
  642:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
  643:       if ( $Apache::optionresponse::conceptgroup
  644: 	   && !&Apache::response::showallfoils() ) {
  645: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  646: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  647: 	$Apache::response::conceptgroup{"$name.text"} = $text;
  648: 	$Apache::response::conceptgroup{"$name.location"} = $location;
  649:       } else {
  650: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  651: 	$Apache::response::foilgroup{"$name.value"} = $value;
  652: 	$Apache::response::foilgroup{"$name.text"} = $text;
  653: 	$Apache::response::foilgroup{"$name.location"} = $location;
  654:       }
  655:     }
  656:   }
  657:   if ($target eq 'edit') {
  658:     $result.= &Apache::edit::tag_end($target,$token,'');
  659:   }
  660:   return $result;
  661: }
  662: 
  663: sub start_drawoptionlist {
  664:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  665:     if ($target ne 'meta') {
  666: 	return $token->[4];
  667:     }
  668: }
  669: 
  670: sub end_drawoptionlist {
  671:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  672:     if ($target ne 'meta') {
  673: 	return $token->[2];
  674:     }
  675: }
  676: 
  677: sub insert_foil {
  678:   return '
  679: <foil name="" value="unused">
  680: <startouttext />
  681: <endouttext />
  682: </foil>';
  683: }
  684: 1;
  685: __END__
  686:  

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