File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.67: download - view: text, annotated - select for diffs
Sun Jan 19 08:10:22 2003 UTC (21 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- preventing more possible bombs on incomplete problems

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

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