File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.69: download - view: text, annotated - select for diffs
Wed Mar 26 18:42:29 2003 UTC (21 years, 1 month ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Changes were done according to Ed's desires: 1. Added righttriangle symbol yo any foil in the exam LaTeX mode (lines before bubbles). 2. Spacing adjustment of the text and bubbles is done. 3. Separation of problems with respect to foils (questions) is more clear now.

    1: # LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.69 2003/03/26 18:42:29 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: my $flag_for_exam_printing = 1;
   35: 
   36: BEGIN {
   37:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
   38: }
   39: 
   40: sub start_optionresponse {
   41:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   42:   my $result='';
   43:   #when in a option response use these
   44:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
   45:   push (@Apache::lonxml::namespace,'optionresponse');
   46:   my $id = &Apache::response::start_response($parstack,$safeeval);
   47:   %Apache::hint::option=();
   48:   if ($target eq 'edit') {
   49:     $result.=&Apache::edit::start_table($token).
   50: 	"<tr><td>Multiple Option Response Question</td><td>Delete:".
   51: 	&Apache::edit::deletelist($target,$token)
   52: 	."</td><td>&nbsp;".
   53:         &Apache::edit::end_row().
   54:         &Apache::edit::start_spanning_row().
   55:         "\n";
   56:     $result.=&Apache::edit::text_arg('Max Number Of Shown Foils:','max',
   57: 				     $token,'4').
   58:         &Apache::edit::select_arg('Randomize Foil Order','randomize',
   59: 				  ['yes','no'],$token).
   60:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
   61:   } elsif ($target eq 'modified') {
   62:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   63: 						 $safeeval,'max','randomize');
   64:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   65:   } elsif ($target eq 'meta') {
   66:     $result=&Apache::response::meta_package_write('optionresponse');
   67:   } elsif ($target eq 'analyze') {
   68:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
   69:     push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   70:   }
   71:   return $result;
   72: }
   73: 
   74: sub end_optionresponse {
   75:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   76:   &Apache::response::end_response;
   77:   pop @Apache::lonxml::namespace;
   78:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
   79:   my $result;
   80:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   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' || $target eq 'tex' || $target eq 'analyze') {
  157:     my $name;
  158:     my @opt;
  159:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  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 = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
  191: 	  $responsehash{$name}=$response;
  192: 	  if ( $response =~ /[^\s]/) {
  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: 	my $ad;
  213: 	if ($wrong==0 && $ignored==0) {
  214: 	  $ad='EXACT_ANS';
  215: 	} elsif ($wrong==0 && $right==0) {
  216: 	  #nothing submitted
  217: 	} else {
  218: 	  if ($ignored==0) {
  219: 	    $ad='INCORRECT';
  220: 	  } else {
  221: 	    $ad='MISSING_ANSWER';
  222: 	  }
  223: 	}
  224: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  225: 	  $responsestr;
  226: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  227: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
  228: 	  $ad;
  229: 	&Apache::response::handle_previous(\%previous,$ad);
  230:       }
  231:     }
  232:   } elsif ($target eq 'edit') {
  233:     $result.=&Apache::edit::end_table();
  234:   }  
  235:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {$result .= '\end{enumerate}';}
  236:   return $result;
  237: }
  238: 
  239: sub getfoilcounts {
  240:   my ($max)=@_;
  241:   # +1 since instructors will count from 1
  242:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  243:   if (&Apache::response::showallfoils()) { $max=$count; }
  244:   if ($count>$max) { $count=$max } 
  245:   &Apache::lonxml::debug("Count is $count from $max");
  246:   return $count;
  247: }
  248: 
  249: sub whichfoils {
  250:   my ($max,$randomize)=@_;
  251:   $max = &getfoilcounts($max);
  252:   &Apache::lonxml::debug("randomize $randomize");
  253:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  254:   my @names = @{ $Apache::response::foilgroup{'names'} };
  255:   my @whichopt =();
  256:   my (%top,@toplist,%bottom,@bottomlist);
  257:   if (!(&Apache::response::showallfoils() || ($randomize eq 'no'))) {
  258:       my $current=0;
  259:       foreach my $name (@names) {
  260: 	  $current++;
  261: 	  if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  262: 	      $top{$name}=$current;
  263: 	  } elsif ($Apache::response::foilgroup{$name.'.location'} eq
  264: 		   'bottom') {
  265: 	      $bottom{$name}=$current;
  266: 	  }
  267:       }
  268:   }
  269:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
  270:     &Apache::lonxml::debug("Have $#whichopt max is $max");
  271:     my $aopt;
  272:     if (&Apache::response::showallfoils() || ($randomize eq 'no')) {
  273:       $aopt=0;
  274:     } else {
  275:       $aopt=int(&Math::Random::random_uniform() * ($#names+1));
  276:     }
  277:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
  278:     $aopt=splice(@names,$aopt,1);
  279:     &Apache::lonxml::debug("Picked $aopt");
  280:     if ($top{$aopt}) {
  281: 	$toplist[$top{$aopt}]=$aopt;
  282:     } elsif ($bottom{$aopt}) {
  283: 	$bottomlist[$bottom{$aopt}]=$aopt;
  284:     } else {
  285: 	push (@whichopt,$aopt);
  286:     }
  287:   }
  288:   for (my $i=0;$i<=$#toplist;$i++) {
  289:       if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
  290:   }
  291:   for (my $i=0;$i<=$#bottomlist;$i++) {
  292:       if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
  293:   }
  294: 
  295:   return @whichopt;
  296: }
  297: 
  298: sub displayanswers {
  299:   my ($max,$randomize,@opt)=@_;
  300:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  301:   my @names = @{ $Apache::response::foilgroup{'names'} };
  302:   my @whichopt = &whichfoils($max,$randomize);
  303:   my $result=&Apache::response::answer_header('optionresponse');
  304:   foreach my $name (@whichopt) {
  305:     $result.=&Apache::response::answer_part('optionresponse',
  306: 		     $Apache::response::foilgroup{$name.'.value'})
  307:   }
  308:   $result.=&Apache::response::answer_footer('optionresponse');
  309:   return $result;
  310: }
  311: 
  312: sub displayfoils {
  313:   my ($target,$max,$randomize,@opt)=@_;
  314:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  315:   my @names = @{ $Apache::response::foilgroup{'names'} };
  316:   my @truelist;
  317:   my @falselist;
  318:   my $result;
  319:   my $name;
  320:   my $displayoptionintex=0;
  321:   my @alphabet = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  322: 		  'Q','R','S','T','U','V','W','X','Y','Z');
  323:   my @whichopt = &whichfoils($max,$randomize);
  324:   my $part=$Apache::inputtags::part;
  325:   my $id=$Apache::inputtags::response[-1];
  326:   my $break;
  327:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  328:   my $status=$Apache::inputtags::status[-1];
  329:   if (
  330:       ($target ne 'tex') &&
  331:       (($solved =~ /^correct/) || ($status eq 'SHOW_ANSWER')) ) {
  332:     foreach $name (@whichopt) {
  333: 	my $text=$Apache::response::foilgroup{$name.'.text'};
  334: 	if ($text!~/^\s*$/) {
  335: 	    if ($target eq 'tex') {
  336: 		$break='\vskip 0 mm ';
  337: 	    } elsif ($target eq 'web') {
  338: 		$break='<br />';
  339: 	    }
  340: 	}
  341: 	$result.=$break;
  342: 	if ($target eq 'web') {
  343: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  344: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
  345: 		if ($text=~/^\s*$/) {
  346: 		    $text=$value.$text;
  347: 		} else {
  348: 		    $text=$value.': '.$text;
  349: 		}
  350: 	    }
  351: 	    $result.=$text."\n";
  352:       } elsif ($target eq 'tex') {
  353: 	  $Apache::response::foilgroup{$name.'.text'}=~s/\\item//;
  354:  	  $result .='\item \textit{'.$Apache::response::foilgroup{$name.'.value'}.'}'.
  355: 	      ":".$Apache::response::foilgroup{$name.'.text'}."\n";
  356:       }
  357:       if ($Apache::lonhomework::type eq 'exam') {
  358: 	if ($target ne 'tex') {
  359: 	  $result.=&webbubbles(\@opt,\@alphabet);
  360:         } else {
  361: 	  $result.=&bubbles(\@alphabet,\@opt);
  362:         }
  363:       }
  364:     }
  365:   } else {
  366:     my $temp=1;
  367:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  368:     foreach $name (@whichopt) {
  369:       my $text=$Apache::response::foilgroup{$name.'.text'};
  370:       if ($text!~/^\s*$/) {
  371: 	  if ($target eq 'tex') {
  372: 	      $break='\vskip 0 mm ';
  373: 	  } elsif ($target eq 'web') {
  374: 	      $break='<br />';
  375: 	  }
  376:       }
  377:       my $lastopt=$lastresponse{$name};
  378:       my $optionlist="<option></option>\n";
  379:       my $option;
  380:       foreach $option (@opt) {
  381: 	if ($option eq $lastopt) {
  382: 	  $optionlist.="<option selected=\"on\">$option</option>\n";
  383: 	} else {
  384: 	  $optionlist.="<option>$option</option>\n";
  385: 	}
  386:       }
  387:       if ($target ne 'tex') {
  388: 	  $optionlist='<select name="HWVAL_'.
  389: 	      $Apache::inputtags::response['-1'].':'.$temp.'">'.
  390: 		  $optionlist."</select>\n";
  391: 	  my $text=$Apache::response::foilgroup{$name.'.text'};
  392: 	  if (!($text=~s|<drawoptionlist\s*/>|$optionlist|)) {
  393: 	      $text=$optionlist.$text;
  394: 	  }
  395: 	  $result.=$break.$text."\n";
  396: 	  if ($Apache::lonhomework::type eq 'exam') {
  397: 	    $result.=&webbubbles(\@opt,\@alphabet);
  398: 	  }
  399: 	  $temp++;
  400:       } else {
  401:           my $texoptionlist = &optionlist_correction($optionlist);
  402: 	  if ($displayoptionintex == 0) {
  403: 	      if ($Apache::lonhomework::type eq 'exam') {$texoptionlist='';} #if exam we do not need to show optionlist
  404: 	      if ($Apache::response::foilgroup{$name.'.text'}=~m/\\item /) {
  405: 		if ($Apache::lonhomework::type eq 'exam') {
  406: 	          $Apache::response::foilgroup{$name.'.text'}=~s/\\item/\\vskip 2 mm/;
  407: 	        }
  408: 	        if ($Apache::response::foilgroup{$name.'.text'}=~/<drawoptionlist\s*\/>/) {
  409: 	          $Apache::response::foilgroup{$name.'.text'}=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
  410: 	        }
  411: 		$result.= $texoptionlist.$Apache::response::foilgroup{$name.'.text'};
  412: 	      } else {
  413: 		  if ($Apache::lonhomework::type eq 'exam') {
  414: 		      $result.= $texoptionlist.'  '.$Apache::response::foilgroup{$name.'.text'};
  415: 		  } else {
  416: 		      $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
  417: 		  }
  418: 	      }
  419: 	      if ($Apache::lonhomework::type eq 'exam') {
  420: 		  $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 ';
  421: 	      }
  422: 	      $displayoptionintex=1;
  423: 	  } else {
  424: 	      if ($Apache::response::foilgroup{$name.'.text'}=~m/\\item /) {
  425: 		  if ($Apache::lonhomework::type eq 'exam') {
  426: 		      $Apache::response::foilgroup{$name.'.text'}=~s/\\item//;
  427: 		  }
  428: 	        if ($Apache::response::foilgroup{$name.'.text'}=~/<drawoptionlist\s*\/>/) {
  429: 	          $Apache::response::foilgroup{$name.'.text'}=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
  430: 	        }
  431: 		$result.= $Apache::response::foilgroup{$name.'.text'};
  432: 	      } else {
  433: 		  if ($Apache::lonhomework::type eq 'exam') {
  434: 		      $result.= '  '.$Apache::response::foilgroup{$name.'.text'};
  435: 		  } else {
  436: 		      $result.= '\item '.$Apache::response::foilgroup{$name.'.text'};
  437: 		  }
  438: 	      }
  439: 	      if ($Apache::lonhomework::type eq 'exam') {
  440: 		  $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 ';
  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' and $Apache::lonhomework::type ne 'exam') { $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: 	if ($target eq 'tex' and $Apache::lonhomework::type eq 'exam' and $flag_for_exam_printing) {
  637: 	    $Apache::response::conceptgroup{"$name.text"} = '\vskip 4 mm $\triangleright$ '.$text;
  638: 	} else {
  639: 	    if ($target eq 'tex') {
  640: 		$Apache::response::conceptgroup{"$name.text"} = ' $\triangleright$ '.$text;
  641: 	    } else {
  642: 		$Apache::response::conceptgroup{"$name.text"} = $text;
  643: 	    }
  644: 	}
  645: 	$Apache::response::conceptgroup{"$name.location"} = $location;
  646:       } else {
  647: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  648: 	$Apache::response::foilgroup{"$name.value"} = $value;
  649: 	if ($Apache::lonhomework::type eq 'exam' and $flag_for_exam_printing) {
  650: 	    $Apache::response::foilgroup{"$name.text"} = '\vskip 5 mm $\triangleright$ '.$text;
  651: 	} else {
  652: 	    if ($target eq 'tex') {
  653: 		$Apache::response::foilgroup{"$name.text"} = ' $\triangleright$ '.$text;
  654: 	    } else {
  655: 		$Apache::response::foilgroup{"$name.text"} = $text;
  656: 	    }
  657: 	}
  658: 	$Apache::response::foilgroup{"$name.location"} = $location;
  659:       }
  660:     }
  661:   }
  662:   if ($target eq 'edit') {
  663:     $result.= &Apache::edit::tag_end($target,$token,'');
  664:   }
  665:   return $result;
  666: }
  667: 
  668: sub insert_foil {
  669:   return '
  670: <foil name="" value="unused">
  671: <startouttext />
  672: <endouttext />
  673: </foil>';
  674: }
  675: 1;
  676: __END__
  677:  

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