File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.145: download - view: text, annotated - select for diffs
Wed Aug 29 10:07:42 2007 UTC (16 years, 8 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Make anaylze target produce a bubble_lines entry that describes how many
lines on an answer bubble sheet the problem requires for its answer.

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

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