File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.65: download - view: text, annotated - select for diffs
Mon Dec 2 17:06:08 2002 UTC (21 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
- correct answers are no longer shown for target tex

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

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