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

1.46      sakharuk    1: # LearningOnline Network with CAPA
1.22      albertel    2: # option list style responses
1.27      albertel    3: #
1.134   ! foxr        4: # $Id: optionresponse.pm,v 1.133 2006/03/09 01:04:43 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.132     albertel  117:     $result.= &Apache::edit::checked_arg('Print options:','texoptions',
                    118: 					 [ ['nochoice','Don\'t show option list'] ]
                    119: 					 ,$token);
1.30      matthew   120:     $result.= &Apache::edit::end_row();
                    121:     $result.= &Apache::edit::start_spanning_row();
                    122:     $result.= $insertlist.'<br />';
1.22      albertel  123:   }
                    124:   if ($target eq 'modified') {
                    125:     my @options;
                    126:     my $optchanged=0;
                    127:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.125     albertel  128:     if ($env{"form.$Apache::lonxml::curdepth.deleteopt"}) {
                    129:       my $delopt=$env{"form.$Apache::lonxml::curdepth.deleteopt"};
1.22      albertel  130:       &Apache::lonxml::debug("Deleting :$delopt:");
                    131:       splice(@options,$delopt-1,1);
                    132:       $optchanged=1;
                    133:     }
1.125     albertel  134:     if ($env{"form.$Apache::lonxml::curdepth.options"}) {
                    135:       my $newopt = $env{"form.$Apache::lonxml::curdepth.options"};
1.22      albertel  136:       if ($options[0]) {
                    137: 	push(@options,$newopt);
                    138:       } else {
                    139: 	$options[0]=$newopt;
                    140:       }
                    141:       $optchanged=1;
                    142:     }
1.132     albertel  143:     my $rebuildtag = &Apache::edit::get_new_args($token,$parstack,$safeeval,
                    144: 						 ('texoptions'));
                    145:     if ($optchanged || $rebuildtag ) {
1.22      albertel  146:       $result = "<foilgroup options=\"(";
                    147:       foreach my $option (@options) {
1.41      albertel  148: 	$option=~s/\'/\\\'/g;
1.22      albertel  149: 	&Apache::lonxml::debug("adding option :$option:");
                    150: 	$result .="'".$option."',";
                    151:       }
                    152:       chop $result;
1.132     albertel  153:       $result.=')" ';
                    154:       $result .= 'texoptions="'.$token->[2]{'texoptions'}.'" ';
                    155:       $result .= '>';
1.22      albertel  156:     } # else nothing changed so just use the default mechanism
                    157:   }
1.68      sakharuk  158:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.109     albertel  159:       $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}
                    160:                    \begin{enumerate} ';
1.42      sakharuk  161:   }
1.22      albertel  162:   return $result;
1.1       albertel  163: }
                    164: 
                    165: sub end_foilgroup {
1.22      albertel  166:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    167:   
                    168:   my $result;
1.94      albertel  169:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    170:       $target eq 'tex' || $target eq 'analyze') {
1.81      sakharuk  171:     my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
1.22      albertel  172:     my $name;
                    173:     my @opt;
                    174:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.126     albertel  175:     &Apache::lonxml::debug("options:".join(':',@opt));
                    176: 
1.108     albertel  177:     my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
                    178: 					     -2,0);
1.81      sakharuk  179:     if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
1.22      albertel  180:     &Apache::lonxml::debug("Options are $#opt");
1.48      albertel  181:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    182:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    183: 					       $safeeval,'-2');
1.32      sakharuk  184:     if ($target eq 'web' || $target eq 'tex') {
1.107     sakharuk  185:       $result.=&displayfoils($target,$max,$randomize,$TeXlayout,@opt);
1.25      albertel  186:     } elsif ( $target eq 'answer') {
1.48      albertel  187:       $result.=&displayanswers($max,$randomize,@opt);
1.34      albertel  188:     } elsif ( $target eq 'analyze') {
1.93      albertel  189: 	my @shown = &whichfoils($max,$randomize);
1.94      albertel  190: 	&Apache::response::analyze_store_foilgroup(\@shown,
                    191: 						  ['text','value','location']);
1.93      albertel  192: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    193: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
1.22      albertel  194:     } elsif ( $target eq 'grade') {
1.124     albertel  195:       if ( &Apache::response::submitted()) {
1.48      albertel  196: 	my @whichopt = &whichfoils($max,$randomize);
1.22      albertel  197: 	my $temp=1;my $name;
1.26      albertel  198: 	my %responsehash;
1.57      albertel  199: 	my %grade;
1.22      albertel  200: 	my $right=0;
                    201: 	my $wrong=0;
                    202: 	my $ignored=0;
                    203: 	foreach $name (@whichopt) {
1.74      albertel  204: 	  my $response=&Apache::response::getresponse($temp);
1.125     albertel  205: 	  if ($env{'form.submitted'} eq 'scantron' && $response=~/\S/) {
1.74      albertel  206: 	      $response = $opt[$response];
                    207: 	  }
1.22      albertel  208: 	  if ( $response =~ /[^\s]/) {
1.96      albertel  209: 	    $responsehash{$name}=$response;
1.22      albertel  210: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
1.34      albertel  211: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
1.57      albertel  212: 	    if ($value eq $response) {
                    213: 		$grade{$name}='1'; $right++;
                    214: 	    } else {
                    215: 		$grade{$name}='0'; $wrong++;
                    216: 	    }
1.22      albertel  217: 	  } else {
                    218: 	    $ignored++;
                    219: 	  }
                    220: 	  $temp++;
                    221: 	}
1.28      albertel  222: 	my $part=$Apache::inputtags::part;
1.22      albertel  223: 	my $id = $Apache::inputtags::response['-1'];
1.28      albertel  224: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.57      albertel  225: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
1.28      albertel  226: 	my %previous=&Apache::response::check_for_previous($responsestr,
                    227: 							   $part,$id);
1.22      albertel  228: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.75      albertel  229: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    230: 	    $responsestr;
                    231: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
                    232: 	
1.121     albertel  233: 	if ($Apache::lonhomework::type eq 'survey') {
                    234: 	    if ($ignored == 0) {
                    235: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
                    236: 		&Apache::response::handle_previous(\%previous,$ad);
                    237: 	    } elsif ($wrong==0 && $right==0) {
                    238: 	    } else {
                    239: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
                    240: 		&Apache::response::handle_previous(\%previous,$ad);
                    241: 	    }
                    242: 	} elsif (!$Apache::lonhomework::scantronmode) {
1.75      albertel  243: 	    my $ad;
                    244: 	    if ($wrong==0 && $ignored==0) {
                    245: 		$ad='EXACT_ANS';
                    246: 	    } elsif ($wrong==0 && $right==0) {
                    247: 		#nothing submitted
                    248: 	    } else {
                    249: 		if ($ignored==0) {
                    250: 		    $ad='INCORRECT';
                    251: 		} else {
                    252: 		    $ad='MISSING_ANSWER';
                    253: 		}
                    254: 	    }
                    255: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    256: 	    &Apache::response::handle_previous(\%previous,$ad);
1.22      albertel  257: 	} else {
1.75      albertel  258: 	    my $ad;
                    259: 	    if ($wrong==0 && $right==0) {
1.120     albertel  260: 		#nothing submitted only assign a score if we 
                    261: 		#need to override a previous grade
                    262: 		if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
                    263: 		    $ad='ASSIGNED_SCORE';
                    264: 		}
1.75      albertel  265: 	    } else {
                    266: 		$ad='ASSIGNED_SCORE';
                    267: 	    }
                    268: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    269: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
                    270: 		$right/(scalar(@whichopt));
1.76      albertel  271: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
                    272: 		scalar(@whichopt);
1.1       albertel  273: 	}
1.22      albertel  274:       }
1.1       albertel  275:     }
1.92      sakharuk  276:     &Apache::lonxml::increment_counter(&getfoilcounts($max));
1.25      albertel  277:   } elsif ($target eq 'edit') {
1.22      albertel  278:     $result.=&Apache::edit::end_table();
1.74      albertel  279:   }
                    280:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
                    281:       $result .= '\end{enumerate}';
                    282:   }
1.110     albertel  283:   &Apache::response::poprandomnumber();
1.22      albertel  284:   return $result;
1.1       albertel  285: }
                    286: 
                    287: sub getfoilcounts {
1.48      albertel  288:   my ($max)=@_;
1.22      albertel  289:   # +1 since instructors will count from 1
                    290:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.39      albertel  291:   if (&Apache::response::showallfoils()) { $max=$count; }
1.48      albertel  292:   if ($count>$max) { $count=$max } 
                    293:   &Apache::lonxml::debug("Count is $count from $max");
                    294:   return $count;
1.1       albertel  295: }
                    296: 
                    297: sub whichfoils {
1.77      albertel  298:     my ($max,$randomize)=@_;
                    299:     return &Apache::response::whichorder($max,$randomize,
                    300: 					 &Apache::response::showallfoils(),
                    301: 					 \%Apache::response::foilgroup);
1.1       albertel  302: }
                    303: 
1.25      albertel  304: sub displayanswers {
1.130     albertel  305:     my ($max,$randomize,@opt)=@_;
                    306:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
                    307:     my @names = @{ $Apache::response::foilgroup{'names'} };
                    308:     my @whichopt = &whichfoils($max,$randomize);
                    309:     my $result;
                    310:     if ($Apache::lonhomework::type eq 'exam') {
                    311: 	my $i = 0;
                    312: 	my %opt = map { ($_,$i++) } @opt;
                    313: 	
                    314: 	$i = 0;
                    315: 	my @alphabet = ('A'..'Z');
                    316: 	foreach my $name (@whichopt) {
                    317: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
                    318: 	    $result.=&Apache::response::answer_part('optionresponse',
                    319: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
                    320: 	    $result.=&Apache::response::answer_part('optionresponse',
                    321: 						    $Apache::response::foilgroup{$name.'.value'});
                    322: 	    $result.=&Apache::response::answer_footer('optionresponse');
                    323: 	}
                    324:     } else {
                    325: 	$result=&Apache::response::answer_header('optionresponse');
                    326: 	foreach my $name (@whichopt) {
                    327: 	    $result.=&Apache::response::answer_part('optionresponse',
                    328: 						    $Apache::response::foilgroup{$name.'.value'});
                    329: 	}
                    330: 	$result.=&Apache::response::answer_footer('optionresponse');
                    331:     }
                    332:     return $result;
1.25      albertel  333: }
                    334: 
1.106     albertel  335: sub check_for_invalid {
                    336:     my ($names,$options) = @_;
                    337:     my %bad_names;
                    338:     foreach my $name (@{ $names }) {
                    339: 	my $value=$Apache::response::foilgroup{$name.'.value'};
                    340: 	my $found=0;
                    341: 	foreach my $option (@{ $options }) {
                    342: 	    if ($value eq $option) { $found=1; }
                    343: 	}
                    344: 	if (!$found) { $bad_names{$name}=$value; }
                    345:     }
                    346:     if (%bad_names) {
                    347: 	my $error=&mt('The question can not be gotten correct, '.
                    348: 	    'the following foils in the &lt;optionresponse&gt; '.
                    349: 	    'have invalid correct options').' <br /><tt>'.
                    350: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
                    351: 	    "</tt>";
                    352: 	&Apache::lonxml::error($error);
                    353:     }
                    354: }
                    355: 
1.1       albertel  356: sub displayfoils {
1.107     sakharuk  357:   my ($target,$max,$randomize,$TeXlayout,@opt)=@_;
1.67      albertel  358:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
1.22      albertel  359:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    360:   my @truelist;
                    361:   my @falselist;
1.79      sakharuk  362:   my $result;  
1.22      albertel  363:   my $name;
1.101     albertel  364:   my $displayoptionintex=1;
1.72      sakharuk  365:   my @alphabet = ('A'..'Z');
1.48      albertel  366:   my @whichopt = &whichfoils($max,$randomize);
1.106     albertel  367:   &check_for_invalid(\@whichopt,\@opt);
1.28      albertel  368:   my $part=$Apache::inputtags::part;
                    369:   my $id=$Apache::inputtags::response[-1];
1.62      albertel  370:   my $break;
1.65      albertel  371:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
1.86      albertel  372:   if ( ($target ne 'tex') &&
                    373:        &Apache::response::show_answer() ) {
1.90      albertel  374:     my $temp=1;
1.22      albertel  375:     foreach $name (@whichopt) {
1.62      albertel  376: 	my $text=$Apache::response::foilgroup{$name.'.text'};
1.90      albertel  377: 	my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
                    378: 	my $lastopt=$lastresponse{$name};
1.101     albertel  379: 	if ($text!~/^\s*$/) { $break='<br />'; }
1.62      albertel  380: 	$result.=$break;
1.113     sakharuk  381: 	if ($target eq 'web') {
1.62      albertel  382: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    383: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
                    384: 		if ($text=~/^\s*$/) {
                    385: 		    $text=$value.$text;
                    386: 		} else {
1.127     albertel  387: 		    $text='<b>'.$value.':</b> '.$text;
1.62      albertel  388: 		}
1.95      albertel  389: 	    } else {
                    390: 		$text='&#149;'.$text;
1.62      albertel  391: 	    }
                    392: 	    $result.=$text."\n";
1.112     sakharuk  393: 	}
1.58      sakharuk  394:       if ($Apache::lonhomework::type eq 'exam') {
1.90      albertel  395: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.58      sakharuk  396:       }
1.90      albertel  397:       $temp++;
1.22      albertel  398:     }
                    399:   } else {
                    400:     my $temp=1;
1.28      albertel  401:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
1.79      sakharuk  402:     my $internal_counter=$Apache::lonxml::counter;
1.22      albertel  403:     foreach $name (@whichopt) {
1.62      albertel  404:       my $text=$Apache::response::foilgroup{$name.'.text'};
                    405:       if ($text!~/^\s*$/) {
                    406: 	  if ($target eq 'tex') {
                    407: 	      $break='\vskip 0 mm ';
                    408: 	  } elsif ($target eq 'web') {
                    409: 	      $break='<br />';
                    410: 	  }
                    411:       }
1.28      albertel  412:       my $lastopt=$lastresponse{$name};
                    413:       my $optionlist="<option></option>\n";
1.126     albertel  414:       foreach my $option (@opt) {
                    415: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
                    416: 	  if ($option eq $lastopt) {
1.133     albertel  417: 	      $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
1.126     albertel  418: 	  } else {
                    419: 	      $optionlist.="<option value='".$escopt."'>$option</option>\n";
                    420: 	  }
1.28      albertel  421:       }
1.32      sakharuk  422:       if ($target ne 'tex') {
1.72      sakharuk  423: 	  if ($Apache::lonhomework::type ne 'exam') {
1.101     albertel  424: 	      $optionlist='<select name="HWVAL_'.
                    425: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
1.59      albertel  426: 		  $optionlist."</select>\n";
1.101     albertel  427: 	  } else {
                    428: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
1.72      sakharuk  429: 	  }
1.101     albertel  430: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
                    431: 	      if ($Apache::lonhomework::type ne 'exam') {
                    432: 		  $text='&#149;'.$text;
                    433: 	      }
1.95      albertel  434: 	  } else {
1.101     albertel  435: 	      if ($Apache::lonhomework::type ne 'exam') {
                    436: 		  $text=$optionlist.$text;
                    437: 	      }
1.59      albertel  438: 	  }
1.62      albertel  439: 	  $result.=$break.$text."\n";
1.49      sakharuk  440: 	  if ($Apache::lonhomework::type eq 'exam') {
1.101     albertel  441: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.49      sakharuk  442: 	  }
1.32      sakharuk  443: 	  $temp++;
                    444:       } else {
1.101     albertel  445: 	  my $texoptionlist='';
                    446: 	  if ($displayoptionintex &&
                    447: 	      $Apache::lonhomework::type ne 'exam') {
1.107     sakharuk  448: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
1.101     albertel  449: 	  }
                    450: 	  if ($text=~/<drawoptionlist\s*\/>/) {
                    451: 	      $text=~s|<drawoptionlist\s*/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |;
                    452: 	  }
                    453: 
                    454: 	  if ($text=~m/\\item /) {
1.50      sakharuk  455: 	      if ($Apache::lonhomework::type eq 'exam') {
1.101     albertel  456: 	          $text=~s/\\item/\\vskip 2 mm/;
1.50      sakharuk  457: 	      }
1.101     albertel  458: 	      $result.= $texoptionlist.$text;
1.32      sakharuk  459: 	  } else {
1.101     albertel  460: 	      if ($Apache::lonhomework::type eq 'exam') {
                    461: 		  $result.= $texoptionlist.'  '.$text;
1.46      sakharuk  462: 	      } else {
1.107     sakharuk  463: 		  if ($text=~/\S/) {$result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;} else {$result.= $texoptionlist;}
1.50      sakharuk  464: 	      }
1.32      sakharuk  465: 	  }
1.101     albertel  466: 	  if ($Apache::lonhomework::type eq 'exam') {
                    467: 	      $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.
                    468: 		  $internal_counter.'}.]'.&bubbles(\@alphabet,\@opt).
                    469: 		  '\end{enumerate} \vskip -8 mm \strut ';
                    470: 	      $internal_counter++;
                    471: 	  }
                    472: 	  $displayoptionintex=0;
1.62      albertel  473:       }
1.1       albertel  474:     }
1.62      albertel  475:   }
1.32      sakharuk  476:   if ($target ne 'tex') {
1.62      albertel  477:       return $result.$break;
                    478:   } else {
1.32      sakharuk  479:       return $result;
1.22      albertel  480:   }
1.50      sakharuk  481: }
                    482: 
                    483: 
1.134   ! foxr      484: #  Correct a single option list element.
        !           485: #  - For embedded <display></display> tags,
        !           486: #    we pass literally the stuff between them.
        !           487: #  - For everything else, we run through latex_special_symbols
        !           488: #    so that any symbols that have meaning to LaTeX will be
        !           489: #    correctly escaped.
        !           490: #
        !           491: sub correct_option {
        !           492:     my $option = shift;
        !           493: 
        !           494: 
        !           495:     # There's probably a beter way with perl if I was
        !           496:     # more of a regexp wiz..(RF).
        !           497: 
        !           498:     my $strlen = length($option);
        !           499:     my $here   = 0;		# Where to start searching for <display></display>
        !           500:     my $result ="";		# The return string is built here.
        !           501:     
        !           502:     while ($here < $strlen) {
        !           503: 	# look for an opening <display> tag:
        !           504: 
        !           505: 	my $openloc = index($option, "<display>", $here);
        !           506: 	if ($openloc == -1) {
        !           507: 	    # No more... operate with latex_special_symbols on
        !           508: 	    # the rest of the string.
        !           509: 
        !           510: 	    $result .= &Apache::lonxml::latex_special_symbols(substr($option, $here));
        !           511: 	    return $result;
        !           512: 	} else {
        !           513: 	    # Need to pass the string up to the <display> tag 
        !           514: 	    # through l_s_s ...
        !           515: 	    $result .= &Apache::lonxml::latex_special_symbols(substr($option, $here, $openloc-$here));
        !           516: 	}
        !           517: 	# look for the closing </display> tag...
        !           518: 	# We're a bit stupid..or tolerant...
        !           519: 	# in that if the user forgets the </display> tag
        !           520: 	# we infer they intended one at the end of the option.
        !           521: 	$openloc += 9;		# 9 chars in <display>
        !           522: 
        !           523: 	my $closeloc = index($option, "</display>", $openloc); # 9 chars in <display>
        !           524: 	if ($closeloc == -1) {
        !           525: 	    $closeloc = $strlen + 9;                             # 10chars in </display> faked off end. 
        !           526: 	}
        !           527: 	#  Pass from openloc through closeloc without any interpretation:
        !           528: 
        !           529: 	&Apache::lonnet::logthis("Segment inside display: $openloc through $closeloc");
        !           530: 	$result .= substr($option, $openloc, $closeloc - $openloc);
        !           531: 	$here    = $closeloc + 10;                               # Next search is after the </display> 
        !           532:     }
        !           533: 
        !           534:     return $result;
        !           535: 
        !           536:     # return &Apache::lonxml::latex_special_symbols($option);
        !           537: }
        !           538: 
        !           539: #  Correct the option list elements so that if there
        !           540: #  are special symbosl that have meaning to LaTeX
        !           541: #  they get fixed up to display correctly.
        !           542: 
1.50      sakharuk  543: sub optionlist_correction {
1.107     sakharuk  544:     my ($TeXlayout,@options) = @_;
1.101     albertel  545:     my $texoptionlist='\\item [] Choices: ';
1.107     sakharuk  546:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
1.101     albertel  547:     if (scalar(@options) > 0) {
                    548: 	foreach my $option (@options) {
                    549: 	    $texoptionlist.='{\bf '.
1.134   ! foxr      550: 		&correct_option($option).
1.107     sakharuk  551: 		'}';
                    552: 	    if ($TeXlayout eq 'vertical') {
                    553: 		$texoptionlist.=' \vskip 0 mm ',
                    554: 	    } else {
1.115     sakharuk  555: 		$texoptionlist.=', ';
1.107     sakharuk  556: 	    }
1.101     albertel  557: 	}
1.116     albertel  558: 	$texoptionlist=~s/, $//;
1.107     sakharuk  559: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
1.80      sakharuk  560:     } else {
1.107     sakharuk  561: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
1.80      sakharuk  562:     }
1.50      sakharuk  563:     return $texoptionlist;
1.58      sakharuk  564: }
                    565: 
                    566: 
                    567: sub webbubbles {
1.90      albertel  568:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
1.58      sakharuk  569:     my @opt=@$ropt; 
                    570:     my @alphabet=@$ralphabet;
                    571:     my $result='';
1.90      albertel  572:     my $number_of_bubbles = $#opt + 1;
                    573:     $result.= '<table border="1"><tr>';
                    574:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
                    575: 	my $checked='';
                    576: 	if ($lastopt eq $opt[$ind]) {
                    577: 	    $checked=' checked="on" ';
1.58      sakharuk  578: 	}
1.90      albertel  579: 	$result.='<td><input type="radio" name="HWVAL_'.
                    580: 	    $Apache::inputtags::response['-1'].':'.$temp.
1.111     albertel  581: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
                    582: 	if ($alphabet[$ind]) {
                    583: 	    $result.=$alphabet[$ind].': ';
                    584: 	}
                    585: 	$result.=$opt[$ind].'</td>';
1.90      albertel  586:     }
                    587:     $result.='</tr></table>';
                    588:     return $result;
1.50      sakharuk  589: }
                    590: 
                    591: 
                    592: sub bubbles {
1.119     albertel  593:     my ($ralphabet,$ropt,$response) = @_;
                    594:     my @alphabet = @$ralphabet;
1.50      sakharuk  595:     my @opt = @$ropt;
1.51      sakharuk  596:     my ($result,$head,$line) =('','','');
1.50      sakharuk  597:     my $number_of_bubbles = $#opt + 1;
1.51      sakharuk  598:     my $current_length = 0;
1.100     sakharuk  599:     my $textwidth;
1.125     albertel  600:     if ($env{'form.textwidth'} ne '') {
                    601: 	$env{'form.textwidth'}=~/(\d+)/;
1.100     sakharuk  602: 	$textwidth=$1;
                    603:     } else {
1.125     albertel  604: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.102     sakharuk  605: 	$textwidth=$1.'.'.$2;
1.100     sakharuk  606:     }
1.51      sakharuk  607:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
1.99      sakharuk  608: 	my $leftmargin;
1.101     albertel  609: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
1.119     albertel  610: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
1.99      sakharuk  611: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
1.102     sakharuk  612: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+4)*2;
1.99      sakharuk  613: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
1.51      sakharuk  614: 	    $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
                    615: 	    $head.='lr';
                    616: 	} else {
                    617: 	    $line=~s/\&\s*$//;
1.131     albertel  618: 	    $result.='\vskip -2 mm\noindent\setlength{\tabcolsep}{2 mm}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
1.51      sakharuk  619: 	    $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
                    620: 	    $head ='lr';
                    621: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
                    622: 	}
                    623: 
1.50      sakharuk  624:     }
                    625:     return $result;
1.1       albertel  626: }
                    627: 
1.22      albertel  628: 
1.2       albertel  629: sub start_conceptgroup {
1.22      albertel  630:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    631:   $Apache::optionresponse::conceptgroup=1;
1.44      albertel  632:   %Apache::response::conceptgroup=();
1.22      albertel  633:   my $result;
                    634:   if ($target eq 'edit') {
                    635:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.30      matthew   636:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    637:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22      albertel  638:   }
                    639:   if ($target eq 'modified') {
                    640:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
                    641:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    642:   }
                    643:   return $result;
1.2       albertel  644: }
                    645: 
                    646: sub end_conceptgroup {
1.22      albertel  647:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    648:   $Apache::optionresponse::conceptgroup=0;
                    649:   my $result='';
1.93      albertel  650:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    651:       $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  652:     #if not there aren't any foils to display and thus no question
1.93      albertel  653:       &Apache::response::pick_foil_for_concept($target,
                    654: 					       ['value','text','location'],
                    655: 					       \%Apache::hint::option,
                    656: 					       $parstack,$safeeval);
1.34      albertel  657:   } elsif ($target eq 'edit') {
1.22      albertel  658:     $result=&Apache::edit::end_table();
                    659:   }
                    660:   return $result;
1.2       albertel  661: }
                    662: 
1.16      albertel  663: sub insert_conceptgroup {
1.22      albertel  664:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    665:   return $result;
1.16      albertel  666: }
                    667: 
1.1       albertel  668: sub start_foil {
1.22      albertel  669:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    670:   my $result='';
1.34      albertel  671:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1.105     albertel  672:       &Apache::lonxml::startredirection;
                    673:       if ($target eq 'analyze') {
                    674: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    675:       }
1.25      albertel  676:   } elsif ($target eq 'edit') {
1.22      albertel  677:     $result=&Apache::edit::tag_start($target,$token,"Foil");
                    678:     my $level='-2';
                    679:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
                    680:     my @opt;
                    681:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
                    682:     $result.=&Apache::edit::text_arg('Name:','name',$token);
1.48      albertel  683:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
                    684: 					       ['unused',(@opt)],$token,'15');
                    685:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    686: 					     $safeeval,'-3');
                    687:     if ($randomize ne 'no') {
                    688: 	$result.=&Apache::edit::select_arg('Location:','location',
                    689: 					   ['random','top','bottom'],$token);
                    690:     }
1.30      matthew   691:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25      albertel  692:   } elsif ($target eq 'modified') {
1.48      albertel  693:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    694: 						 'value','name','location');
1.22      albertel  695:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32      sakharuk  696:   } 
1.22      albertel  697:   return $result;
1.1       albertel  698: }
                    699: 
                    700: sub end_foil {
1.22      albertel  701:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    702:   my $text ='';
                    703:   my $result = '';
1.88      albertel  704:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
1.48      albertel  705:       $text=&Apache::lonxml::endredirection;
1.114     sakharuk  706:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
1.107     sakharuk  707:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
1.32      sakharuk  708:   }
                    709:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
1.34      albertel  710:       || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  711:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.122     albertel  712:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                    713: 	$text='\vskip 5mm $\triangleright$ '.$text;
                    714:     }
1.22      albertel  715:     if ($value ne 'unused') {
                    716:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    717:       &Apache::lonxml::debug("Got a name of :$name:");
1.129     albertel  718:       if ($name eq "") {
1.128     albertel  719: 	  &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.123     albertel  720: 	  $name=$Apache::lonxml::curdepth;
                    721:       }
1.22      albertel  722:       &Apache::lonxml::debug("Using a name of :$name:");
1.103     albertel  723:       if (defined($Apache::response::foilnames{$name})) {
                    724: 	  &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
                    725:       }
1.104     albertel  726:       $Apache::response::foilnames{$name}++;
1.103     albertel  727:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.39      albertel  728:       if ( $Apache::optionresponse::conceptgroup
                    729: 	   && !&Apache::response::showallfoils() ) {
1.22      albertel  730: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    731: 	$Apache::response::conceptgroup{"$name.value"} = $value;
1.122     albertel  732: 	$Apache::response::conceptgroup{"$name.text"} = $text;
1.48      albertel  733: 	$Apache::response::conceptgroup{"$name.location"} = $location;
1.22      albertel  734:       } else {
                    735: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
1.48      albertel  736: 	$Apache::response::foilgroup{"$name.value"} = $value;
1.122     albertel  737: 	$Apache::response::foilgroup{"$name.text"} = $text;
1.48      albertel  738: 	$Apache::response::foilgroup{"$name.location"} = $location;
1.22      albertel  739:       }
                    740:     }
                    741:   }
                    742:   if ($target eq 'edit') {
                    743:     $result.= &Apache::edit::tag_end($target,$token,'');
                    744:   }
                    745:   return $result;
1.1       albertel  746: }
                    747: 
1.112     sakharuk  748: sub start_drawoptionlist {
                    749:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.117     albertel  750:     if ($target ne 'meta') {
                    751: 	return $token->[4];
                    752:     }
1.112     sakharuk  753: }
                    754: 
                    755: sub end_drawoptionlist {
                    756:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.117     albertel  757:     if ($target ne 'meta') {
                    758: 	return $token->[2];
                    759:     }
1.112     sakharuk  760: }
                    761: 
1.7       albertel  762: sub insert_foil {
1.22      albertel  763:   return '
1.15      albertel  764: <foil name="" value="unused">
1.14      albertel  765: <startouttext />
                    766: <endouttext />
1.7       albertel  767: </foil>';
                    768: }
1.1       albertel  769: 1;
                    770: __END__
                    771:  

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