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

1.46      sakharuk    1: # LearningOnline Network with CAPA
1.22      albertel    2: # option list style responses
1.27      albertel    3: #
1.61    ! sakharuk    4: # $Id: optionresponse.pm,v 1.60 2002/11/11 15:58:51 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);
1.57      albertel   45:   %Apache::hint::option=();
1.22      albertel   46:   if ($target eq 'edit') {
1.23      albertel   47:     $result.=&Apache::edit::start_table($token).
1.30      matthew    48: 	"<tr><td>Multiple Option Response Question</td><td>Delete:".
1.23      albertel   49: 	&Apache::edit::deletelist($target,$token)
1.30      matthew    50: 	."</td><td>&nbsp;".
                     51:         &Apache::edit::end_row().
                     52:         &Apache::edit::start_spanning_row().
                     53:         "\n";
1.48      albertel   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();
1.34      albertel   59:   } elsif ($target eq 'modified') {
1.23      albertel   60:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.48      albertel   61: 						 $safeeval,'max','randomize');
1.22      albertel   62:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.34      albertel   63:   } elsif ($target eq 'meta') {
1.23      albertel   64:     $result=&Apache::response::meta_package_write('optionresponse');
1.34      albertel   65:   } elsif ($target eq 'analyze') {
                     66:     my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                     67:     push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.22      albertel   68:   }
                     69:   return $result;
1.1       albertel   70: }
                     71: 
                     72: sub end_optionresponse {
1.22      albertel   73:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     74:   &Apache::response::end_response;
                     75:   pop @Apache::lonxml::namespace;
1.29      albertel   76:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
1.22      albertel   77:   my $result;
                     78:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                     79:   return $result;
1.1       albertel   80: }
                     81: 
1.44      albertel   82: %Apache::response::foilgroup=();
1.1       albertel   83: sub start_foilgroup {
1.22      albertel   84:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     85: 
                     86:   my $result='';
1.44      albertel   87:   %Apache::response::foilgroup=();
1.22      albertel   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>
1.13      albertel  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
1.30      matthew   110:     $result.= &Apache::edit::end_row();
                    111:     $result.= &Apache::edit::start_spanning_row();
                    112:     $result.= $insertlist.'<br />';
1.22      albertel  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) {
1.41      albertel  136: 	$option=~s/\'/\\\'/g;
1.22      albertel  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:   }
1.42      sakharuk  144:   if ($target eq 'tex') {
                    145:       $result .= ' \begin{enumerate} ';
                    146:   }
1.22      albertel  147:   return $result;
1.1       albertel  148: }
                    149: 
                    150: sub end_foilgroup {
1.22      albertel  151:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    152:   
                    153:   my $result;
1.35      albertel  154:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  155:     my $name;
                    156:     my @opt;
                    157:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
                    158:     &Apache::lonxml::debug("Options are $#opt");
1.48      albertel  159:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    160:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    161: 					       $safeeval,'-2');
1.32      sakharuk  162:     if ($target eq 'web' || $target eq 'tex') {
1.48      albertel  163:       $result.=&displayfoils($target,$max,$randomize,@opt);
1.25      albertel  164:     } elsif ( $target eq 'answer') {
1.48      albertel  165:       $result.=&displayanswers($max,$randomize,@opt);
1.34      albertel  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"} =
1.35      albertel  172: 	  $Apache::response::foilgroup{"$name.value"};
1.34      albertel  173: 	$Apache::lonhomework::analyze{"$part_id.foil.text.$name"} =
1.35      albertel  174: 	  $Apache::response::foilgroup{"$name.text"};
1.34      albertel  175:       }
1.35      albertel  176:       push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
1.48      albertel  177:       push (@{ $Apache::lonhomework::analyze{"$part_id.shown"} },&whichfoils($max,$randomize));
1.22      albertel  178:     } elsif ( $target eq 'grade') {
                    179:       if ( defined $ENV{'form.submitted'}) {
1.48      albertel  180: 	my @whichopt = &whichfoils($max,$randomize);
1.22      albertel  181: 	my $temp=1;my $name;
1.26      albertel  182: 	my %responsehash;
1.57      albertel  183: 	my %grade;
1.22      albertel  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"};
1.26      albertel  189: 	  $responsehash{$name}=$response;
1.22      albertel  190: 	  if ( $response =~ /[^\s]/) {
                    191: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
1.34      albertel  192: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
1.57      albertel  193: 	    if ($value eq $response) {
                    194: 		$grade{$name}='1'; $right++;
                    195: 	    } else {
                    196: 		$grade{$name}='0'; $wrong++;
                    197: 	    }
1.22      albertel  198: 	  } else {
                    199: 	    $ignored++;
                    200: 	  }
                    201: 	  $temp++;
                    202: 	}
1.28      albertel  203: 	my $part=$Apache::inputtags::part;
1.22      albertel  204: 	my $id = $Apache::inputtags::response['-1'];
1.28      albertel  205: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.57      albertel  206: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
1.28      albertel  207: 	my %previous=&Apache::response::check_for_previous($responsestr,
                    208: 							   $part,$id);
1.22      albertel  209: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.28      albertel  210: 	my $ad;
1.22      albertel  211: 	if ($wrong==0 && $ignored==0) {
1.28      albertel  212: 	  $ad='EXACT_ANS';
                    213: 	} elsif ($wrong==0 && $right==0) {
                    214: 	  #nothing submitted
1.22      albertel  215: 	} else {
1.36      albertel  216: 	  if ($ignored==0) {
                    217: 	    $ad='INCORRECT';
                    218: 	  } else {
                    219: 	    $ad='MISSING_ANSWER';
                    220: 	  }
1.1       albertel  221: 	}
1.28      albertel  222: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    223: 	  $responsestr;
1.57      albertel  224: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
1.28      albertel  225: 	$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
                    226: 	  $ad;
                    227: 	&Apache::response::handle_previous(\%previous,$ad);
1.22      albertel  228:       }
1.1       albertel  229:     }
1.25      albertel  230:   } elsif ($target eq 'edit') {
1.22      albertel  231:     $result.=&Apache::edit::end_table();
1.32      sakharuk  232:   }  
1.38      sakharuk  233:   if ($target eq 'tex') {$result .= '\end{enumerate}';}
1.22      albertel  234:   return $result;
1.1       albertel  235: }
                    236: 
                    237: sub getfoilcounts {
1.48      albertel  238:   my ($max)=@_;
1.22      albertel  239:   # +1 since instructors will count from 1
                    240:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.39      albertel  241:   if (&Apache::response::showallfoils()) { $max=$count; }
1.48      albertel  242:   if ($count>$max) { $count=$max } 
                    243:   &Apache::lonxml::debug("Count is $count from $max");
                    244:   return $count;
1.1       albertel  245: }
                    246: 
                    247: sub whichfoils {
1.48      albertel  248:   my ($max,$randomize)=@_;
                    249:   $max = &getfoilcounts($max);
                    250:   &Apache::lonxml::debug("randomize $randomize");
1.22      albertel  251:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    252:   my @whichopt =();
1.48      albertel  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:   }
1.22      albertel  266:   while ((($#whichopt+1) < $max) && ($#names > -1)) {
                    267:     &Apache::lonxml::debug("Have $#whichopt max is $max");
1.39      albertel  268:     my $aopt;
1.48      albertel  269:     if (&Apache::response::showallfoils() || ($randomize eq 'no')) {
1.39      albertel  270:       $aopt=0;
                    271:     } else {
1.47      albertel  272:       $aopt=int(&Math::Random::random_uniform() * ($#names+1));
1.39      albertel  273:     }
1.22      albertel  274:     &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
                    275:     $aopt=splice(@names,$aopt,1);
                    276:     &Apache::lonxml::debug("Picked $aopt");
1.48      albertel  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]) }
1.22      albertel  287:   }
1.48      albertel  288:   for (my $i=0;$i<=$#bottomlist;$i++) {
                    289:       if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
                    290:   }
                    291: 
1.22      albertel  292:   return @whichopt;
1.1       albertel  293: }
                    294: 
1.25      albertel  295: sub displayanswers {
1.48      albertel  296:   my ($max,$randomize,@opt)=@_;
1.25      albertel  297:   my @names = @{ $Apache::response::foilgroup{'names'} };
1.48      albertel  298:   my @whichopt = &whichfoils($max,$randomize);
1.25      albertel  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: 
1.1       albertel  308: sub displayfoils {
1.48      albertel  309:   my ($target,$max,$randomize,@opt)=@_;
1.22      albertel  310:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    311:   my @truelist;
                    312:   my @falselist;
                    313:   my $result;
                    314:   my $name;
1.45      albertel  315:   my $displayoptionintex=0;
1.49      sakharuk  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');
1.48      albertel  318:   my @whichopt = &whichfoils($max,$randomize);
1.28      albertel  319:   my $part=$Apache::inputtags::part;
                    320:   my $id=$Apache::inputtags::response[-1];
1.58      sakharuk  321:   if (($Apache::lonhomework::history{"resource.$part.solved"} =~ /^correct/)  || ($Apache::inputtags::status[-1] eq  'SHOW_ANSWER')) {    
                    322:     my $temp=1; ####
1.22      albertel  323:     foreach $name (@whichopt) {
1.32      sakharuk  324:       if ($target eq 'web') {
                    325: 	  $result.="<br />";
1.46      sakharuk  326:       } elsif ($target eq 'tex') {
                    327: 	  $result.='\vskip 0 mm ';
                    328:       }
                    329:       if ($target ne 'tex') {
1.59      albertel  330: 	  my $text=$Apache::response::foilgroup{$name.'.text'};
                    331: 	  my $value=$Apache::response::foilgroup{$name.'.value'};
                    332: 	  if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
                    333: 	      $text=$value.': '.$text;
                    334: 	  }
                    335: 	  $result.=$text."\n";
1.46      sakharuk  336:       } else {
1.54      sakharuk  337: 	  $Apache::response::foilgroup{$name.'.text'}=~s/\\item//;
1.55      sakharuk  338:  	  $result .='\item \textit{'.$Apache::response::foilgroup{$name.'.value'}.'}'.
1.46      sakharuk  339: 	      ":".$Apache::response::foilgroup{$name.'.text'}."\n";
1.32      sakharuk  340:       }
1.58      sakharuk  341:       if ($Apache::lonhomework::type eq 'exam') {
                    342: 	if ($target ne 'tex') {
                    343: 	  $result.=&webbubbles(\@opt,\@alphabet);
                    344:         } else {
                    345: 	  $result.=&bubbles(\@alphabet,\@opt);
                    346:         }
                    347:       }
                    348:     $temp++;
1.22      albertel  349:     }
                    350:   } else {
                    351:     my $temp=1;
1.28      albertel  352:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
1.22      albertel  353:     foreach $name (@whichopt) {
1.28      albertel  354:       my $lastopt=$lastresponse{$name};
                    355:       my $optionlist="<option></option>\n";
                    356:       my $option;
                    357:       foreach $option (@opt) {
                    358: 	if ($option eq $lastopt) {
                    359: 	  $optionlist.="<option selected=\"on\">$option</option>\n";
                    360: 	} else {
                    361: 	  $optionlist.="<option>$option</option>\n";
                    362: 	}
                    363:       }
1.32      sakharuk  364:       if ($target ne 'tex') {
1.59      albertel  365: 	  $optionlist='<select name="HWVAL_'.
                    366: 	      $Apache::inputtags::response['-1'].':'.$temp.'">'.
                    367: 		  $optionlist."</select>\n";
                    368: 	  my $text=$Apache::response::foilgroup{$name.'.text'};
                    369: 	  if (!($text=~s|<drawoptionlist\s*/>|$optionlist|)) {
                    370: 	      $text=$optionlist.$text;
                    371: 	  }
                    372: 	  $result.="<br />".$text."\n";
1.49      sakharuk  373: 	  if ($Apache::lonhomework::type eq 'exam') {
1.58      sakharuk  374: 	    $result.=&webbubbles(\@opt,\@alphabet);
1.49      sakharuk  375: 	  }
1.32      sakharuk  376: 	  $temp++;
                    377:       } else {
1.60      sakharuk  378:           my $texoptionlist = &optionlist_correction($optionlist);
1.45      albertel  379: 	  if ($displayoptionintex == 0) {
1.60      sakharuk  380: 	      if ($Apache::lonhomework::type eq 'exam') {$texoptionlist='';} #if exam we do not need to show optionlist
1.46      sakharuk  381: 	      if ($Apache::response::foilgroup{$name.'.text'}=~m/\\item /) {
1.60      sakharuk  382: 		if ($Apache::lonhomework::type eq 'exam') {
                    383: 	          $Apache::response::foilgroup{$name.'.text'}=~s/\\item/\\item[\\textbf{$Apache::lonxml::counter}\.]/;
                    384: 	        }
                    385: 	        if ($Apache::response::foilgroup{$name.'.text'}=~/<drawoptionlist\s*\/>/) {
1.61    ! sakharuk  386: 	          $Apache::response::foilgroup{$name.'.text'}=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
1.60      sakharuk  387: 	          $result.= $texoptionlist.$Apache::response::foilgroup{$name.'.text'};
                    388: 	        }
1.46      sakharuk  389: 	      } else {
1.51      sakharuk  390: 		  if ($Apache::lonhomework::type eq 'exam') {
1.56      sakharuk  391: 		      $result.= $texoptionlist.'\vspace*{-2 mm}\item[\textbf{'.$Apache::lonxml::counter.'}.]'.$Apache::response::foilgroup{$name.'.text'};
1.51      sakharuk  392: 		  } else {
1.56      sakharuk  393: 		      $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
1.51      sakharuk  394: 		  }
1.46      sakharuk  395: 	      }
1.50      sakharuk  396: 	      if ($Apache::lonhomework::type eq 'exam') {
                    397: 		  $result.=&bubbles(\@alphabet,\@opt);
                    398: 	      }
1.45      albertel  399: 	      $displayoptionintex=1;
1.32      sakharuk  400: 	  } else {
1.46      sakharuk  401: 	      if ($Apache::response::foilgroup{$name.'.text'}=~m/\\item /) {
1.51      sakharuk  402: 		  if ($Apache::lonhomework::type eq 'exam') {
                    403: 		      $Apache::response::foilgroup{$name.'.text'}=~s/\\item/\\item[\\textbf{$Apache::lonxml::counter}\.]/;
                    404: 		  }
1.60      sakharuk  405: 	        if ($Apache::response::foilgroup{$name.'.text'}=~/<drawoptionlist\s*\/>/) {
1.61    ! sakharuk  406: 	          $Apache::response::foilgroup{$name.'.text'}=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
1.60      sakharuk  407: 	        }
                    408: 		$result.= $Apache::response::foilgroup{$name.'.text'};
1.46      sakharuk  409: 	      } else {
1.51      sakharuk  410: 		  if ($Apache::lonhomework::type eq 'exam') {
                    411: 		      $result.= '\item[\textbf{'.$Apache::lonxml::counter.'}.]'.$Apache::response::foilgroup{$name.'.text'};
                    412: 		  } else {
                    413: 		      $result.= '\item '.$Apache::response::foilgroup{$name.'.text'};
                    414: 		  }
1.46      sakharuk  415: 	      }
1.50      sakharuk  416: 	      if ($Apache::lonhomework::type eq 'exam') {
                    417: 		  $result.=&bubbles(\@alphabet,\@opt);
                    418: 	      }
1.32      sakharuk  419: 	  }
                    420:       } 
1.1       albertel  421:     }
1.32      sakharuk  422:   } 
                    423:   if ($target ne 'tex') {
                    424:       return $result."<br />";
                    425:   }
                    426:   else {
                    427:       return $result;
1.22      albertel  428:   }
1.50      sakharuk  429: }
                    430: 
                    431: 
                    432: sub optionlist_correction {
                    433: 
                    434:     my $texoptionlist = shift;
1.51      sakharuk  435:     $texoptionlist =~ s/<option><\/option>/\\item \[\] Possible answers are:/;
1.50      sakharuk  436:     $texoptionlist =~ s/<option>/\{\\bf /g;
                    437:     $texoptionlist =~ s/<option selected="on">/\{\\bf /g;
                    438:     $texoptionlist =~ s/<\/option>/\},/g;
                    439:     $texoptionlist =~ s/,$/\./g;
                    440:     $texoptionlist =~ s/>/\$>\$/g;
                    441:     $texoptionlist =~ s/</\$<\$/g;
                    442:     $texoptionlist =~ s/=/\$=\$/g;
                    443:     return $texoptionlist;
1.58      sakharuk  444: }
                    445: 
                    446: 
                    447: sub webbubbles {
                    448: 
                    449:     my ($ropt,$ralphabet)=@_;
                    450:     my @opt=@$ropt; 
                    451:     my @alphabet=@$ralphabet;
                    452:     my $result='';
                    453: 	my $number_of_bubbles = $#opt + 1;
                    454: 	$result.= '<table border="1"><tr>';
                    455: 	for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
                    456: 	   $result.='<td>'.$alphabet[$ind].': '.$opt[$ind].'</td>';
                    457: 	}
                    458: 	$result.='</tr></table>';
                    459: 	return $result;
1.50      sakharuk  460: }
                    461: 
                    462: 
                    463: sub bubbles {
                    464: 
                    465:     my ($ralphabit,$ropt) = @_;
                    466:     my @alphabet = @$ralphabit;
                    467:     my @opt = @$ropt;
1.51      sakharuk  468:     my ($result,$head,$line) =('','','');
1.50      sakharuk  469:     my $number_of_bubbles = $#opt + 1;
1.51      sakharuk  470:     my $current_length = 0;
                    471:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
                    472: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+2)*2;
1.52      sakharuk  473: 	if ($current_length<0.9*$ENV{'form.textwidth'} and $ind!=$number_of_bubbles) {
1.51      sakharuk  474: 	    $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
                    475: 	    $head.='lr';
                    476: 	} else {
                    477: 	    $line=~s/\&\s*$//;
                    478: 	    $result.='\vskip -5 mm\begin{tabular}{'.$head.'}\\\\'.$line.'\\\\\end{tabular}';
                    479: 	    $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
                    480: 	    $head ='lr';
                    481: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
                    482: 	}
                    483: 
1.50      sakharuk  484:     }
1.53      albertel  485:     &Apache::lonxml::increment_counter();
1.50      sakharuk  486:     return $result;
1.1       albertel  487: }
                    488: 
1.22      albertel  489: 
1.2       albertel  490: sub start_conceptgroup {
1.22      albertel  491:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    492:   $Apache::optionresponse::conceptgroup=1;
1.44      albertel  493:   %Apache::response::conceptgroup=();
1.22      albertel  494:   my $result;
                    495:   if ($target eq 'edit') {
                    496:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.30      matthew   497:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    498:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22      albertel  499:   }
                    500:   if ($target eq 'modified') {
                    501:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
                    502:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    503:   }
                    504:   return $result;
1.2       albertel  505: }
                    506: 
                    507: sub end_conceptgroup {
1.22      albertel  508:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    509:   $Apache::optionresponse::conceptgroup=0;
                    510:   my $result='';
1.32      sakharuk  511:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
1.35      albertel  512:       || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  513:     #if not there aren't any foils to display and thus no question
                    514:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    515:       my @names = @{ $Apache::response::conceptgroup{'names'} };
1.47      albertel  516:       my $pick=int(&Math::Random::random_uniform() * ($#names+1));
1.22      albertel  517:       my $name=$names[$pick];
                    518:       push @{ $Apache::response::foilgroup{'names'} }, $name;
1.35      albertel  519:       $Apache::response::foilgroup{"$name.value"} =
                    520: 	$Apache::response::conceptgroup{"$name.value"};
                    521:       $Apache::response::foilgroup{"$name.text"} =
                    522: 	$Apache::response::conceptgroup{"$name.text"};
1.48      albertel  523:       $Apache::response::foilgroup{"$name.location"} =
                    524: 	$Apache::response::conceptgroup{"$name.location"};
1.22      albertel  525:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    526:       $Apache::response::foilgroup{"$name.concept"} = $concept;
                    527:       &Apache::lonxml::debug("Selecting $name in $concept");
1.35      albertel  528:       if ($target eq 'analyze') {
                    529: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    530:         push (@{ $Apache::lonhomework::analyze{"$part_id.concepts"} },
                    531: 	      $concept);
                    532: 	$Apache::lonhomework::analyze{"$part_id.concept.$concept"}=
                    533: 	  $Apache::response::conceptgroup{'names'};
                    534: 	foreach my $name (@{ $Apache::response::conceptgroup{'names'} }) {
                    535: 	  push (@{ $Apache::lonhomework::analyze{"$part_id.foils"} },
                    536: 		$name);
                    537: 	  $Apache::lonhomework::analyze{"$part_id.foil.value.$name"} =
                    538: 	    $Apache::response::conceptgroup{"$name.value"};
                    539: 	  $Apache::lonhomework::analyze{"$part_id.foil.text.$name"} =
                    540: 	    $Apache::response::conceptgroup{"$name.text"};
1.48      albertel  541: 	  $Apache::lonhomework::analyze{"$part_id.foil.location.$name"} =
                    542: 	    $Apache::response::conceptgroup{"$name.location"};
1.35      albertel  543: 	}
1.57      albertel  544:       } elsif ($target eq 'web') {
                    545: 	  my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    546: 	  push(@{ $Apache::hint::option{"$part_id.concepts"} },
                    547: 	       $concept);
                    548: 	  $Apache::hint::option{"$part_id.concept.$concept"}=
                    549: 	      $Apache::response::conceptgroup{'names'};
1.35      albertel  550:       }
1.22      albertel  551:     }
1.34      albertel  552:   } elsif ($target eq 'edit') {
1.22      albertel  553:     $result=&Apache::edit::end_table();
                    554:   }
                    555:   return $result;
1.2       albertel  556: }
                    557: 
1.16      albertel  558: sub insert_conceptgroup {
1.22      albertel  559:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    560:   return $result;
1.16      albertel  561: }
                    562: 
1.1       albertel  563: sub start_foil {
1.22      albertel  564:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    565:   my $result='';
1.34      albertel  566:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1.25      albertel  567:     &Apache::lonxml::startredirection;
                    568:   } elsif ($target eq 'edit') {
1.22      albertel  569:     $result=&Apache::edit::tag_start($target,$token,"Foil");
                    570:     my $level='-2';
                    571:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
                    572:     my @opt;
                    573:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
                    574:     $result.=&Apache::edit::text_arg('Name:','name',$token);
1.48      albertel  575:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
                    576: 					       ['unused',(@opt)],$token,'15');
                    577:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    578: 					     $safeeval,'-3');
                    579:     if ($randomize ne 'no') {
                    580: 	$result.=&Apache::edit::select_arg('Location:','location',
                    581: 					   ['random','top','bottom'],$token);
                    582:     }
1.30      matthew   583:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25      albertel  584:   } elsif ($target eq 'modified') {
1.48      albertel  585:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    586: 						 'value','name','location');
1.22      albertel  587:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32      sakharuk  588:   } 
1.22      albertel  589:   return $result;
1.1       albertel  590: }
                    591: 
                    592: sub end_foil {
1.22      albertel  593:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    594:   my $text ='';
                    595:   my $result = '';
1.32      sakharuk  596:   if ($target eq 'web' || $target eq 'tex') { 
1.48      albertel  597:       $text=&Apache::lonxml::endredirection;
1.56      sakharuk  598:       if ($target eq 'tex') { $text = '\vspace*{-2 mm}\item '.$text; }
1.32      sakharuk  599:   }
                    600:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
1.34      albertel  601:       || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  602:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    603:     if ($value ne 'unused') {
                    604:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.48      albertel  605:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.22      albertel  606:       &Apache::lonxml::debug("Got a name of :$name:");
                    607:       if (!$name) { $name=$Apache::lonxml::curdepth; }
                    608:       &Apache::lonxml::debug("Using a name of :$name:");
1.39      albertel  609:       if ( $Apache::optionresponse::conceptgroup
                    610: 	   && !&Apache::response::showallfoils() ) {
1.22      albertel  611: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    612: 	$Apache::response::conceptgroup{"$name.value"} = $value;
1.46      sakharuk  613: 	$Apache::response::conceptgroup{"$name.text"} = $text;
1.48      albertel  614: 	$Apache::response::conceptgroup{"$name.location"} = $location;
1.22      albertel  615:       } else {
                    616: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
1.48      albertel  617: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    618: 	$Apache::response::foilgroup{"$name.text"} = $text;
                    619: 	$Apache::response::foilgroup{"$name.location"} = $location;
1.22      albertel  620:       }
                    621:     }
                    622:   }
                    623:   if ($target eq 'edit') {
                    624:     $result.= &Apache::edit::tag_end($target,$token,'');
                    625:   }
                    626:   return $result;
1.1       albertel  627: }
                    628: 
1.7       albertel  629: sub insert_foil {
1.22      albertel  630:   return '
1.15      albertel  631: <foil name="" value="unused">
1.14      albertel  632: <startouttext />
                    633: <endouttext />
1.7       albertel  634: </foil>';
                    635: }
1.1       albertel  636: 1;
                    637: __END__
                    638:  

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