File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.108: download - view: text, annotated - select for diffs
Fri Apr 23 21:37:41 2004 UTC (20 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Make TeXlayout Editable
- move TeXlayout to <optionreponse> from <foilgroup>
- remove some debug messages

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

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