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

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

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