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

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

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