File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.139: download - view: text, annotated - select for diffs
Tue Mar 27 19:20:49 2007 UTC (17 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#4244 enter works correctly now also with ption/radio/rank/match response

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

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