File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.106: download - view: text, annotated - select for diffs
Fri Apr 16 22:25:13 2004 UTC (20 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#2284, optionresponse now complains if the correct option for a question isn't available for selection for the student to choose

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

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