File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.135: download - view: text, annotated - select for diffs
Wed Mar 15 17:10:49 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_2_3_X, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
- Ron has convinced me that this is a bad idea

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

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