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

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

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