File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.150: download - view: text, annotated - select for diffs
Mon Jun 23 11:27:44 2008 UTC (15 years, 10 months ago) by bisitz
Branches: MAIN
CVS tags: version_2_6_99_1, version_2_6_99_0, HEAD
Localisation:
Added missing &mt() call (-> Choices)

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

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