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

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

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