File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.84: download - view: text, annotated - select for diffs
Fri Aug 1 19:04:06 2003 UTC (20 years, 9 months ago) by sakharuk
Branches: MAIN
CVS tags: version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, HEAD
Bug 756 ("possible answers" in option resp printouts) is fixed.

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

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