Annotation of loncom/homework/optionresponse.pm, revision 1.55

1.46      sakharuk    1: # LearningOnline Network with CAPA
1.22      albertel    2: # option list style responses
1.27      albertel    3: #
1.55    ! sakharuk    4: # $Id: optionresponse.pm,v 1.54 2002/10/24 19:51:49 sakharuk Exp $
1.27      albertel    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: #
1.22      albertel   28: # 2/21 Guy
1.32      sakharuk   29: # 01/18 Alex
1.1       albertel   30: package Apache::optionresponse;
                     31: use strict;
1.6       albertel   32: use Apache::response;
1.1       albertel   33: 
1.31      harris41   34: BEGIN {
1.1       albertel   35:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
                     36: }
                     37: 
                     38: sub start_optionresponse {
1.22      albertel   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     40:   my $result='';
1.29      albertel   41:   #when in a option response use these
1.22      albertel   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:   if ($target eq 'edit') {
1.23      albertel   46:     $result.=&Apache::edit::start_table($token).
1.30      matthew    47: 	"<tr><td>Multiple Option Response Question</td><td>Delete:".
1.23      albertel   48: 	&Apache::edit::deletelist($target,$token)
1.30      matthew    49: 	."</td><td>&nbsp;".
                     50:         &Apache::edit::end_row().
                     51:         &Apache::edit::start_spanning_row().
                     52:         "\n";
1.48      albertel   53:     $result.=&Apache::edit::text_arg('Max Number Of Shown Foils:','max',
                     54: 				     $token,'4').
                     55:         &Apache::edit::select_arg('Randomize Foil Order','randomize',
                     56: 				  ['yes','no'],$token).
                     57:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.34      albertel   58:   } elsif ($target eq 'modified') {
1.23      albertel   59:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.48      albertel   60: 						 $safeeval,'max','randomize');
1.22      albertel   61:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.34      albertel   62:   } elsif ($target eq 'meta') {
1.23      albertel   63:     $result=&Apache::response::meta_package_write('optionresponse');
1.34      albertel   64:   } elsif ($target eq 'analyze') {
                     65:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                     66:     push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.22      albertel   67:   }
                     68:   return $result;
1.1       albertel   69: }
                     70: 
                     71: sub end_optionresponse {
1.22      albertel   72:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     73:   &Apache::response::end_response;
                     74:   pop @Apache::lonxml::namespace;
1.29      albertel   75:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
1.22      albertel   76:   my $result;
                     77:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                     78:   return $result;
1.1       albertel   79: }
                     80: 
1.44      albertel   81: %Apache::response::foilgroup=();
1.1       albertel   82: sub start_foilgroup {
1.22      albertel   83:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     84: 
                     85:   my $result='';
1.44      albertel   86:   %Apache::response::foilgroup=();
1.22      albertel   87:   $Apache::optionresponse::conceptgroup=0;
                     88:   &Apache::response::setrandomnumber();
                     89:   if ($target eq 'edit') {
                     90:     my $optionlist="<option></option>\n";
                     91:     my $option;
                     92:     my @opt;
                     93:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
                     94:     my $count=1;
                     95:     foreach $option (@opt) {
                     96:       $optionlist.="<option value=\"$count\">$option</option>\n";
                     97:       $count++;
                     98:     }
                     99:     my $insertlist=&Apache::edit::insertlist($target,$token);
                    100:     $result.=&Apache::edit::start_table($token);
                    101:     $result.= (<<ENDTABLE);
                    102:       <tr><td>Select Options</td>
1.13      albertel  103:         <td>
                    104: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
                    105:         </td>
                    106:         <td>Delete an Option:
                    107: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
                    108: ENDTABLE
1.30      matthew   109:     $result.= &Apache::edit::end_row();
                    110:     $result.= &Apache::edit::start_spanning_row();
                    111:     $result.= $insertlist.'<br />';
1.22      albertel  112:   }
                    113:   if ($target eq 'modified') {
                    114:     my @options;
                    115:     my $optchanged=0;
                    116:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
                    117:     if ($ENV{"form.$Apache::lonxml::curdepth.deleteopt"}) {
                    118:       my $delopt=$ENV{"form.$Apache::lonxml::curdepth.deleteopt"};
                    119:       &Apache::lonxml::debug("Deleting :$delopt:");
                    120:       splice(@options,$delopt-1,1);
                    121:       $optchanged=1;
                    122:     }
                    123:     if ($ENV{"form.$Apache::lonxml::curdepth.options"}) {
                    124:       my $newopt = $ENV{"form.$Apache::lonxml::curdepth.options"};
                    125:       if ($options[0]) {
                    126: 	push(@options,$newopt);
                    127:       } else {
                    128: 	$options[0]=$newopt;
                    129:       }
                    130:       $optchanged=1;
                    131:     }
                    132:     if ($optchanged) {
                    133:       $result = "<foilgroup options=\"(";
                    134:       foreach my $option (@options) {
1.41      albertel  135: 	$option=~s/\'/\\\'/g;
1.22      albertel  136: 	&Apache::lonxml::debug("adding option :$option:");
                    137: 	$result .="'".$option."',";
                    138:       }
                    139:       chop $result;
                    140:       $result.=')">';
                    141:     } # else nothing changed so just use the default mechanism
                    142:   }
1.42      sakharuk  143:   if ($target eq 'tex') {
                    144:       $result .= ' \begin{enumerate} ';
                    145:   }
1.22      albertel  146:   return $result;
1.1       albertel  147: }
                    148: 
                    149: sub end_foilgroup {
1.22      albertel  150:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    151:   
                    152:   my $result;
1.35      albertel  153:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  154:     my $name;
                    155:     my @opt;
                    156:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
                    157:     &Apache::lonxml::debug("Options are $#opt");
1.48      albertel  158:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    159:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    160: 					       $safeeval,'-2');
1.32      sakharuk  161:     if ($target eq 'web' || $target eq 'tex') {
1.48      albertel  162:       $result.=&displayfoils($target,$max,$randomize,@opt);
1.25      albertel  163:     } elsif ( $target eq 'answer') {
1.48      albertel  164:       $result.=&displayanswers($max,$randomize,@opt);
1.34      albertel  165:     } elsif ( $target eq 'analyze') {
                    166:       my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    167:       foreach my $name (@{ $Apache::response::foilgroup{'names'} }) {
                    168: 	if (defined($Apache::lonhomework::analyze{"$part_id.foil.value.$name"})) { next; }
                    169: 	push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },$name);
                    170: 	$Apache::lonhomework::analyze{"$part_id.foil.value.$name"} =
1.35      albertel  171: 	  $Apache::response::foilgroup{"$name.value"};
1.34      albertel  172: 	$Apache::lonhomework::analyze{"$part_id.foil.text.$name"} =
1.35      albertel  173: 	  $Apache::response::foilgroup{"$name.text"};
1.34      albertel  174:       }
1.35      albertel  175:       push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
1.48      albertel  176:       push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} },&whichfoils($max,$randomize));
1.22      albertel  177:     } elsif ( $target eq 'grade') {
                    178:       if ( defined $ENV{'form.submitted'}) {
1.48      albertel  179: 	my @whichopt = &whichfoils($max,$randomize);
1.22      albertel  180: 	my $temp=1;my $name;
1.26      albertel  181: 	my %responsehash;
1.22      albertel  182: 	my $right=0;
                    183: 	my $wrong=0;
                    184: 	my $ignored=0;
                    185: 	foreach $name (@whichopt) {
                    186: 	  my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
1.26      albertel  187: 	  $responsehash{$name}=$response;
1.22      albertel  188: 	  if ( $response =~ /[^\s]/) {
                    189: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
1.34      albertel  190: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
1.22      albertel  191: 	    if ($value eq $response) {$right++;} else {$wrong++;}
                    192: 	  } else {
                    193: 	    $ignored++;
                    194: 	  }
                    195: 	  $temp++;
                    196: 	}
1.28      albertel  197: 	my $part=$Apache::inputtags::part;
1.22      albertel  198: 	my $id = $Apache::inputtags::response['-1'];
1.28      albertel  199: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    200: 	my %previous=&Apache::response::check_for_previous($responsestr,
                    201: 							   $part,$id);
1.22      albertel  202: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.28      albertel  203: 	my $ad;
1.22      albertel  204: 	if ($wrong==0 && $ignored==0) {
1.28      albertel  205: 	  $ad='EXACT_ANS';
                    206: 	} elsif ($wrong==0 && $right==0) {
                    207: 	  #nothing submitted
1.22      albertel  208: 	} else {
1.36      albertel  209: 	  if ($ignored==0) {
                    210: 	    $ad='INCORRECT';
                    211: 	  } else {
                    212: 	    $ad='MISSING_ANSWER';
                    213: 	  }
1.1       albertel  214: 	}
1.28      albertel  215: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    216: 	  $responsestr;
                    217: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    218: 	  $ad;
                    219: 	&Apache::response::handle_previous(\%previous,$ad);
1.22      albertel  220:       }
1.1       albertel  221:     }
1.25      albertel  222:   } elsif ($target eq 'edit') {
1.22      albertel  223:     $result.=&Apache::edit::end_table();
1.32      sakharuk  224:   }  
1.38      sakharuk  225:   if ($target eq 'tex') {$result .= '\end{enumerate}';}
1.22      albertel  226:   return $result;
1.1       albertel  227: }
                    228: 
                    229: sub getfoilcounts {
1.48      albertel  230:   my ($max)=@_;
1.22      albertel  231:   # +1 since instructors will count from 1
                    232:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.39      albertel  233:   if (&Apache::response::showallfoils()) { $max=$count; }
1.48      albertel  234:   if ($count>$max) { $count=$max } 
                    235:   &Apache::lonxml::debug("Count is $count from $max");
                    236:   return $count;
1.1       albertel  237: }
                    238: 
                    239: sub whichfoils {
1.48      albertel  240:   my ($max,$randomize)=@_;
                    241:   $max = &getfoilcounts($max);
                    242:   &Apache::lonxml::debug("randomize $randomize");
1.22      albertel  243:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    244:   my @whichopt =();
1.48      albertel  245:   my (%top,@toplist,%bottom,@bottomlist);
                    246:   if (!(&Apache::response::showallfoils() || ($randomize eq 'no'))) {
                    247:       my $current=0;
                    248:       foreach my $name (@names) {
                    249: 	  $current++;
                    250: 	  if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    251: 	      $top{$name}=$current;
                    252: 	  } elsif ($Apache::response::foilgroup{$name.'.location'} eq
                    253: 		   'bottom') {
                    254: 	      $bottom{$name}=$current;
                    255: 	  }
                    256:       }
                    257:   }
1.22      albertel  258:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
                    259:     &Apache::lonxml::debug("Have $#whichopt max is $max");
1.39      albertel  260:     my $aopt;
1.48      albertel  261:     if (&Apache::response::showallfoils() || ($randomize eq 'no')) {
1.39      albertel  262:       $aopt=0;
                    263:     } else {
1.47      albertel  264:       $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1.39      albertel  265:     }
1.22      albertel  266:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    267:     $aopt=splice(@names,$aopt,1);
                    268:     &Apache::lonxml::debug("Picked $aopt");
1.48      albertel  269:     if ($top{$aopt}) {
                    270: 	$toplist[$top{$aopt}]=$aopt;
                    271:     } elsif ($bottom{$aopt}) {
                    272: 	$bottomlist[$bottom{$aopt}]=$aopt;
                    273:     } else {
                    274: 	push (@whichopt,$aopt);
                    275:     }
                    276:   }
                    277:   for (my $i=0;$i<=$#toplist;$i++) {
                    278:       if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
1.22      albertel  279:   }
1.48      albertel  280:   for (my $i=0;$i<=$#bottomlist;$i++) {
                    281:       if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    282:   }
                    283: 
1.22      albertel  284:   return @whichopt;
1.1       albertel  285: }
                    286: 
1.25      albertel  287: sub displayanswers {
1.48      albertel  288:   my ($max,$randomize,@opt)=@_;
1.25      albertel  289:   my @names = @{ $Apache::response::foilgroup{'names'} };
1.48      albertel  290:   my @whichopt = &whichfoils($max,$randomize);
1.25      albertel  291:   my $result=&Apache::response::answer_header('optionresponse');
                    292:   foreach my $name (@whichopt) {
                    293:     $result.=&Apache::response::answer_part('optionresponse',
                    294: 		     $Apache::response::foilgroup{$name.'.value'})
                    295:   }
                    296:   $result.=&Apache::response::answer_footer('optionresponse');
                    297:   return $result;
                    298: }
                    299: 
1.1       albertel  300: sub displayfoils {
1.48      albertel  301:   my ($target,$max,$randomize,@opt)=@_;
1.22      albertel  302:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    303:   my @truelist;
                    304:   my @falselist;
                    305:   my $result;
                    306:   my $name;
1.45      albertel  307:   my $displayoptionintex=0;
1.49      sakharuk  308:   my @alphabet = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
                    309: 		  'Q','R','S','T','U','V','W','X','Y','Z');
1.48      albertel  310:   my @whichopt = &whichfoils($max,$randomize);
1.28      albertel  311:   my $part=$Apache::inputtags::part;
                    312:   my $id=$Apache::inputtags::response[-1];
                    313:   if (($Apache::lonhomework::history{"resource.$part.solved"} =~ /^correct/)  || ($Apache::inputtags::status[-1] eq  'SHOW_ANSWER')) {
1.22      albertel  314:     foreach $name (@whichopt) {
1.32      sakharuk  315:       if ($target eq 'web') {
                    316: 	  $result.="<br />";
1.46      sakharuk  317:       } elsif ($target eq 'tex') {
                    318: 	  $result.='\vskip 0 mm ';
                    319:       }
                    320:       if ($target ne 'tex') {
                    321: 	  $result .=$Apache::response::foilgroup{$name.'.value'}.
                    322: 	      ":".$Apache::response::foilgroup{$name.'.text'}."\n";
                    323:       } else {
1.54      sakharuk  324: 	  $Apache::response::foilgroup{$name.'.text'}=~s/\\item//;
1.55    ! sakharuk  325:  	  $result .='\item \textit{'.$Apache::response::foilgroup{$name.'.value'}.'}'.
1.46      sakharuk  326: 	      ":".$Apache::response::foilgroup{$name.'.text'}."\n";
1.32      sakharuk  327:       }
1.22      albertel  328:     }
                    329:   } else {
                    330:     my $temp=1;
1.28      albertel  331:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
1.22      albertel  332:     foreach $name (@whichopt) {
1.28      albertel  333:       my $lastopt=$lastresponse{$name};
                    334:       my $optionlist="<option></option>\n";
                    335:       my $option;
                    336:       foreach $option (@opt) {
                    337: 	if ($option eq $lastopt) {
                    338: 	  $optionlist.="<option selected=\"on\">$option</option>\n";
                    339: 	} else {
                    340: 	  $optionlist.="<option>$option</option>\n";
                    341: 	}
                    342:       }
1.32      sakharuk  343:       if ($target ne 'tex') {
                    344: 	  $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
                    345: 	      .$optionlist
                    346: 		  ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
1.49      sakharuk  347: 	  if ($Apache::lonhomework::type eq 'exam') {
                    348: 		  my $number_of_bubbles = $#opt + 1;
                    349: 		  $result.= '<table border="1"><tr>';
                    350: 		  for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
                    351: 		      $result.='<td>'.$alphabet[$ind].': '.$opt[$ind].'</td>';
                    352: 		  }
                    353: 		  $result.='</tr></table>';
                    354: 	  }
1.32      sakharuk  355: 	  $temp++;
                    356:       } else {
1.45      albertel  357: 	  if ($displayoptionintex == 0) {
1.50      sakharuk  358:               my $texoptionlist = &optionlist_correction($optionlist);
1.51      sakharuk  359: 	      if ($Apache::lonhomework::type eq 'exam') {$texoptionlist='';}
1.46      sakharuk  360: 	      if ($Apache::response::foilgroup{$name.'.text'}=~m/\\item /) {
1.51      sakharuk  361: 		  if ($Apache::lonhomework::type eq 'exam') {
                    362: 		      $Apache::response::foilgroup{$name.'.text'}=~s/\\item/\\item[\\textbf{$Apache::lonxml::counter}\.]/;
                    363: 		  }
1.46      sakharuk  364: 		  $result.= $texoptionlist.$Apache::response::foilgroup{$name.'.text'};
                    365: 	      } else {
1.51      sakharuk  366: 		  if ($Apache::lonhomework::type eq 'exam') {
                    367: 		      $result.= $texoptionlist.'\item[\textbf{'.$Apache::lonxml::counter.'}.]'.$Apache::response::foilgroup{$name.'.text'};
                    368: 		  } else {
                    369: 		      $result.= $texoptionlist.'\item '.$Apache::response::foilgroup{$name.'.text'};
                    370: 		  }
1.46      sakharuk  371: 	      }
1.50      sakharuk  372: 	      if ($Apache::lonhomework::type eq 'exam') {
                    373: 		  $result.=&bubbles(\@alphabet,\@opt);
                    374: 	      }
1.45      albertel  375: 	      $displayoptionintex=1;
1.32      sakharuk  376: 	  } else {
1.46      sakharuk  377: 	      if ($Apache::response::foilgroup{$name.'.text'}=~m/\\item /) {
1.51      sakharuk  378: 		  if ($Apache::lonhomework::type eq 'exam') {
                    379: 		      $Apache::response::foilgroup{$name.'.text'}=~s/\\item/\\item[\\textbf{$Apache::lonxml::counter}\.]/;
                    380: 		  }
1.46      sakharuk  381: 		  $result.= $Apache::response::foilgroup{$name.'.text'};
                    382: 	      } else {
1.51      sakharuk  383: 		  if ($Apache::lonhomework::type eq 'exam') {
                    384: 		      $result.= '\item[\textbf{'.$Apache::lonxml::counter.'}.]'.$Apache::response::foilgroup{$name.'.text'};
                    385: 		  } else {
                    386: 		      $result.= '\item '.$Apache::response::foilgroup{$name.'.text'};
                    387: 		  }
1.46      sakharuk  388: 	      }
1.50      sakharuk  389: 	      if ($Apache::lonhomework::type eq 'exam') {
                    390: 		  $result.=&bubbles(\@alphabet,\@opt);
                    391: 	      }
1.32      sakharuk  392: 	  }
                    393:       } 
1.1       albertel  394:     }
1.32      sakharuk  395:   } 
                    396:   if ($target ne 'tex') {
                    397:       return $result."<br />";
                    398:   }
                    399:   else {
                    400:       return $result;
1.22      albertel  401:   }
1.50      sakharuk  402: }
                    403: 
                    404: 
                    405: sub optionlist_correction {
                    406: 
                    407:     my $texoptionlist = shift;
1.51      sakharuk  408:     $texoptionlist =~ s/<option><\/option>/\\item \[\] Possible answers are:/;
1.50      sakharuk  409:     $texoptionlist =~ s/<option>/\{\\bf /g;
                    410:     $texoptionlist =~ s/<option selected="on">/\{\\bf /g;
                    411:     $texoptionlist =~ s/<\/option>/\},/g;
                    412:     $texoptionlist =~ s/,$/\./g;
                    413:     $texoptionlist =~ s/>/\$>\$/g;
                    414:     $texoptionlist =~ s/</\$<\$/g;
                    415:     $texoptionlist =~ s/=/\$=\$/g;
                    416:     return $texoptionlist;
                    417: }
                    418: 
                    419: 
                    420: sub bubbles {
                    421: 
                    422:     my ($ralphabit,$ropt) = @_;
                    423:     my @alphabet = @$ralphabit;
                    424:     my @opt = @$ropt;
1.51      sakharuk  425:     my ($result,$head,$line) =('','','');
1.50      sakharuk  426:     my $number_of_bubbles = $#opt + 1;
1.51      sakharuk  427:     my $current_length = 0;
                    428:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
                    429: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+2)*2;
1.52      sakharuk  430: 	if ($current_length<0.9*$ENV{'form.textwidth'} and $ind!=$number_of_bubbles) {
1.51      sakharuk  431: 	    $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
                    432: 	    $head.='lr';
                    433: 	} else {
                    434: 	    $line=~s/\&\s*$//;
                    435: 	    $result.='\vskip -5 mm\begin{tabular}{'.$head.'}\\\\'.$line.'\\\\\end{tabular}';
                    436: 	    $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
                    437: 	    $head ='lr';
                    438: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
                    439: 	}
                    440: 
1.50      sakharuk  441:     }
1.53      albertel  442:     &Apache::lonxml::increment_counter();
1.50      sakharuk  443:     return $result;
1.1       albertel  444: }
                    445: 
1.22      albertel  446: 
1.2       albertel  447: sub start_conceptgroup {
1.22      albertel  448:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    449:   $Apache::optionresponse::conceptgroup=1;
1.44      albertel  450:   %Apache::response::conceptgroup=();
1.22      albertel  451:   my $result;
                    452:   if ($target eq 'edit') {
                    453:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.30      matthew   454:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    455:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22      albertel  456:   }
                    457:   if ($target eq 'modified') {
                    458:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
                    459:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    460:   }
                    461:   return $result;
1.2       albertel  462: }
                    463: 
                    464: sub end_conceptgroup {
1.22      albertel  465:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    466:   $Apache::optionresponse::conceptgroup=0;
                    467:   my $result='';
1.32      sakharuk  468:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
1.35      albertel  469:       || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  470:     #if not there aren't any foils to display and thus no question
                    471:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    472:       my @names = @{ $Apache::response::conceptgroup{'names'} };
1.47      albertel  473:       my $pick=int(&Math::Random::random_uniform() * ($#names+1));
1.22      albertel  474:       my $name=$names[$pick];
                    475:       push @{ $Apache::response::foilgroup{'names'} }, $name;
1.35      albertel  476:       $Apache::response::foilgroup{"$name.value"} =
                    477: 	$Apache::response::conceptgroup{"$name.value"};
                    478:       $Apache::response::foilgroup{"$name.text"} =
                    479: 	$Apache::response::conceptgroup{"$name.text"};
1.48      albertel  480:       $Apache::response::foilgroup{"$name.location"} =
                    481: 	$Apache::response::conceptgroup{"$name.location"};
1.22      albertel  482:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    483:       $Apache::response::foilgroup{"$name.concept"} = $concept;
                    484:       &Apache::lonxml::debug("Selecting $name in $concept");
1.35      albertel  485:       if ($target eq 'analyze') {
                    486: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    487:         push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    488: 	      $concept);
                    489: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    490: 	  $Apache::response::conceptgroup{'names'};
                    491: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    492: 	  push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    493: 		$name);
                    494: 	  $Apache::lonhomework::analyze{"$part_id.foil.value.$name"} =
                    495: 	    $Apache::response::conceptgroup{"$name.value"};
                    496: 	  $Apache::lonhomework::analyze{"$part_id.foil.text.$name"} =
                    497: 	    $Apache::response::conceptgroup{"$name.text"};
1.48      albertel  498: 	  $Apache::lonhomework::analyze{"$part_id.foil.location.$name"} =
                    499: 	    $Apache::response::conceptgroup{"$name.location"};
1.35      albertel  500: 	}
                    501:       }
1.22      albertel  502:     }
1.34      albertel  503:   } elsif ($target eq 'edit') {
1.22      albertel  504:     $result=&Apache::edit::end_table();
                    505:   }
                    506:   return $result;
1.2       albertel  507: }
                    508: 
1.16      albertel  509: sub insert_conceptgroup {
1.22      albertel  510:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    511:   return $result;
1.16      albertel  512: }
                    513: 
1.1       albertel  514: sub start_foil {
1.22      albertel  515:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    516:   my $result='';
1.34      albertel  517:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1.25      albertel  518:     &Apache::lonxml::startredirection;
                    519:   } elsif ($target eq 'edit') {
1.22      albertel  520:     $result=&Apache::edit::tag_start($target,$token,"Foil");
                    521:     my $level='-2';
                    522:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
                    523:     my @opt;
                    524:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
                    525:     $result.=&Apache::edit::text_arg('Name:','name',$token);
1.48      albertel  526:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
                    527: 					       ['unused',(@opt)],$token,'15');
                    528:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    529: 					     $safeeval,'-3');
                    530:     if ($randomize ne 'no') {
                    531: 	$result.=&Apache::edit::select_arg('Location:','location',
                    532: 					   ['random','top','bottom'],$token);
                    533:     }
1.30      matthew   534:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25      albertel  535:   } elsif ($target eq 'modified') {
1.48      albertel  536:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    537: 						 'value','name','location');
1.22      albertel  538:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32      sakharuk  539:   } 
1.22      albertel  540:   return $result;
1.1       albertel  541: }
                    542: 
                    543: sub end_foil {
1.22      albertel  544:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    545:   my $text ='';
                    546:   my $result = '';
1.32      sakharuk  547:   if ($target eq 'web' || $target eq 'tex') { 
1.48      albertel  548:       $text=&Apache::lonxml::endredirection;
1.55    ! sakharuk  549:       if ($target eq 'tex') { $text = '\item '.$text; }
1.32      sakharuk  550:   }
                    551:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
1.34      albertel  552:       || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  553:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    554:     if ($value ne 'unused') {
                    555:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.48      albertel  556:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.22      albertel  557:       &Apache::lonxml::debug("Got a name of :$name:");
                    558:       if (!$name) { $name=$Apache::lonxml::curdepth; }
                    559:       &Apache::lonxml::debug("Using a name of :$name:");
1.39      albertel  560:       if ( $Apache::optionresponse::conceptgroup
                    561: 	   && !&Apache::response::showallfoils() ) {
1.22      albertel  562: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    563: 	$Apache::response::conceptgroup{"$name.value"} = $value;
1.46      sakharuk  564: 	$Apache::response::conceptgroup{"$name.text"} = $text;
1.48      albertel  565: 	$Apache::response::conceptgroup{"$name.location"} = $location;
1.22      albertel  566:       } else {
                    567: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
1.48      albertel  568: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    569: 	$Apache::response::foilgroup{"$name.text"} = $text;
                    570: 	$Apache::response::foilgroup{"$name.location"} = $location;
1.22      albertel  571:       }
                    572:     }
                    573:   }
                    574:   if ($target eq 'edit') {
                    575:     $result.= &Apache::edit::tag_end($target,$token,'');
                    576:   }
                    577:   return $result;
1.1       albertel  578: }
                    579: 
1.7       albertel  580: sub insert_foil {
1.22      albertel  581:   return '
1.15      albertel  582: <foil name="" value="unused">
1.14      albertel  583: <startouttext />
                    584: <endouttext />
1.7       albertel  585: </foil>';
                    586: }
1.1       albertel  587: 1;
                    588: __END__
                    589:  

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