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

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

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