File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.78: download - view: text, annotated - select for diffs
Tue May 27 13:56:21 2003 UTC (20 years, 11 months ago) by sakharuk
Branches: MAIN
CVS tags: version_0_99_3, version_0_99_2, version_0_99_1, conference_2003, HEAD
Bug 1466 (optionresponse in .exam shows tex in web mode) is fixed.

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

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