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

1.46      sakharuk    1: # LearningOnline Network with CAPA
1.22      albertel    2: # option list style responses
1.27      albertel    3: #
1.137   ! albertel    4: # $Id: optionresponse.pm,v 1.136 2007/01/17 10:46:13 foxr 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.137   ! albertel  186:       $Apache::lonxml::post_evaluate=0;
1.25      albertel  187:     } elsif ( $target eq 'answer') {
1.48      albertel  188:       $result.=&displayanswers($max,$randomize,@opt);
1.136     foxr      189:       
1.34      albertel  190:     } elsif ( $target eq 'analyze') {
1.93      albertel  191: 	my @shown = &whichfoils($max,$randomize);
1.94      albertel  192: 	&Apache::response::analyze_store_foilgroup(\@shown,
                    193: 						  ['text','value','location']);
1.93      albertel  194: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    195: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
1.22      albertel  196:     } elsif ( $target eq 'grade') {
1.124     albertel  197:       if ( &Apache::response::submitted()) {
1.48      albertel  198: 	my @whichopt = &whichfoils($max,$randomize);
1.22      albertel  199: 	my $temp=1;my $name;
1.26      albertel  200: 	my %responsehash;
1.57      albertel  201: 	my %grade;
1.22      albertel  202: 	my $right=0;
                    203: 	my $wrong=0;
                    204: 	my $ignored=0;
                    205: 	foreach $name (@whichopt) {
1.74      albertel  206: 	  my $response=&Apache::response::getresponse($temp);
1.125     albertel  207: 	  if ($env{'form.submitted'} eq 'scantron' && $response=~/\S/) {
1.74      albertel  208: 	      $response = $opt[$response];
                    209: 	  }
1.22      albertel  210: 	  if ( $response =~ /[^\s]/) {
1.96      albertel  211: 	    $responsehash{$name}=$response;
1.22      albertel  212: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
1.34      albertel  213: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
1.57      albertel  214: 	    if ($value eq $response) {
                    215: 		$grade{$name}='1'; $right++;
                    216: 	    } else {
                    217: 		$grade{$name}='0'; $wrong++;
                    218: 	    }
1.22      albertel  219: 	  } else {
                    220: 	    $ignored++;
                    221: 	  }
                    222: 	  $temp++;
                    223: 	}
1.28      albertel  224: 	my $part=$Apache::inputtags::part;
1.22      albertel  225: 	my $id = $Apache::inputtags::response['-1'];
1.28      albertel  226: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.57      albertel  227: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
1.28      albertel  228: 	my %previous=&Apache::response::check_for_previous($responsestr,
                    229: 							   $part,$id);
1.22      albertel  230: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.75      albertel  231: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    232: 	    $responsestr;
                    233: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
                    234: 	
1.121     albertel  235: 	if ($Apache::lonhomework::type eq 'survey') {
                    236: 	    if ($ignored == 0) {
                    237: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
                    238: 		&Apache::response::handle_previous(\%previous,$ad);
                    239: 	    } elsif ($wrong==0 && $right==0) {
                    240: 	    } else {
                    241: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
                    242: 		&Apache::response::handle_previous(\%previous,$ad);
                    243: 	    }
                    244: 	} elsif (!$Apache::lonhomework::scantronmode) {
1.75      albertel  245: 	    my $ad;
                    246: 	    if ($wrong==0 && $ignored==0) {
                    247: 		$ad='EXACT_ANS';
                    248: 	    } elsif ($wrong==0 && $right==0) {
                    249: 		#nothing submitted
                    250: 	    } else {
                    251: 		if ($ignored==0) {
                    252: 		    $ad='INCORRECT';
                    253: 		} else {
                    254: 		    $ad='MISSING_ANSWER';
                    255: 		}
                    256: 	    }
                    257: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    258: 	    &Apache::response::handle_previous(\%previous,$ad);
1.22      albertel  259: 	} else {
1.75      albertel  260: 	    my $ad;
                    261: 	    if ($wrong==0 && $right==0) {
1.120     albertel  262: 		#nothing submitted only assign a score if we 
                    263: 		#need to override a previous grade
                    264: 		if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
                    265: 		    $ad='ASSIGNED_SCORE';
                    266: 		}
1.75      albertel  267: 	    } else {
                    268: 		$ad='ASSIGNED_SCORE';
                    269: 	    }
                    270: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    271: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
                    272: 		$right/(scalar(@whichopt));
1.76      albertel  273: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
                    274: 		scalar(@whichopt);
1.1       albertel  275: 	}
1.22      albertel  276:       }
1.1       albertel  277:     }
1.92      sakharuk  278:     &Apache::lonxml::increment_counter(&getfoilcounts($max));
1.25      albertel  279:   } elsif ($target eq 'edit') {
1.22      albertel  280:     $result.=&Apache::edit::end_table();
1.74      albertel  281:   }
                    282:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
                    283:       $result .= '\end{enumerate}';
                    284:   }
1.110     albertel  285:   &Apache::response::poprandomnumber();
1.22      albertel  286:   return $result;
1.1       albertel  287: }
                    288: 
                    289: sub getfoilcounts {
1.48      albertel  290:   my ($max)=@_;
1.22      albertel  291:   # +1 since instructors will count from 1
                    292:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.39      albertel  293:   if (&Apache::response::showallfoils()) { $max=$count; }
1.48      albertel  294:   if ($count>$max) { $count=$max } 
                    295:   &Apache::lonxml::debug("Count is $count from $max");
                    296:   return $count;
1.1       albertel  297: }
                    298: 
                    299: sub whichfoils {
1.77      albertel  300:     my ($max,$randomize)=@_;
                    301:     return &Apache::response::whichorder($max,$randomize,
                    302: 					 &Apache::response::showallfoils(),
                    303: 					 \%Apache::response::foilgroup);
1.1       albertel  304: }
                    305: 
1.25      albertel  306: sub displayanswers {
1.130     albertel  307:     my ($max,$randomize,@opt)=@_;
                    308:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
                    309:     my @names = @{ $Apache::response::foilgroup{'names'} };
                    310:     my @whichopt = &whichfoils($max,$randomize);
                    311:     my $result;
                    312:     if ($Apache::lonhomework::type eq 'exam') {
                    313: 	my $i = 0;
                    314: 	my %opt = map { ($_,$i++) } @opt;
                    315: 	
                    316: 	$i = 0;
                    317: 	my @alphabet = ('A'..'Z');
                    318: 	foreach my $name (@whichopt) {
                    319: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
                    320: 	    $result.=&Apache::response::answer_part('optionresponse',
                    321: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
                    322: 	    $result.=&Apache::response::answer_part('optionresponse',
                    323: 						    $Apache::response::foilgroup{$name.'.value'});
                    324: 	    $result.=&Apache::response::answer_footer('optionresponse');
                    325: 	}
                    326:     } else {
                    327: 	$result=&Apache::response::answer_header('optionresponse');
                    328: 	foreach my $name (@whichopt) {
                    329: 	    $result.=&Apache::response::answer_part('optionresponse',
                    330: 						    $Apache::response::foilgroup{$name.'.value'});
                    331: 	}
                    332: 	$result.=&Apache::response::answer_footer('optionresponse');
                    333:     }
                    334:     return $result;
1.25      albertel  335: }
                    336: 
1.106     albertel  337: sub check_for_invalid {
                    338:     my ($names,$options) = @_;
                    339:     my %bad_names;
                    340:     foreach my $name (@{ $names }) {
                    341: 	my $value=$Apache::response::foilgroup{$name.'.value'};
                    342: 	my $found=0;
                    343: 	foreach my $option (@{ $options }) {
                    344: 	    if ($value eq $option) { $found=1; }
                    345: 	}
                    346: 	if (!$found) { $bad_names{$name}=$value; }
                    347:     }
                    348:     if (%bad_names) {
                    349: 	my $error=&mt('The question can not be gotten correct, '.
                    350: 	    'the following foils in the &lt;optionresponse&gt; '.
                    351: 	    'have invalid correct options').' <br /><tt>'.
                    352: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
                    353: 	    "</tt>";
                    354: 	&Apache::lonxml::error($error);
                    355:     }
                    356: }
                    357: 
1.1       albertel  358: sub displayfoils {
1.107     sakharuk  359:   my ($target,$max,$randomize,$TeXlayout,@opt)=@_;
1.67      albertel  360:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
1.22      albertel  361:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    362:   my @truelist;
                    363:   my @falselist;
1.79      sakharuk  364:   my $result;  
1.22      albertel  365:   my $name;
1.101     albertel  366:   my $displayoptionintex=1;
1.72      sakharuk  367:   my @alphabet = ('A'..'Z');
1.48      albertel  368:   my @whichopt = &whichfoils($max,$randomize);
1.106     albertel  369:   &check_for_invalid(\@whichopt,\@opt);
1.28      albertel  370:   my $part=$Apache::inputtags::part;
                    371:   my $id=$Apache::inputtags::response[-1];
1.62      albertel  372:   my $break;
1.65      albertel  373:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
1.86      albertel  374:   if ( ($target ne 'tex') &&
                    375:        &Apache::response::show_answer() ) {
1.90      albertel  376:     my $temp=1;
1.22      albertel  377:     foreach $name (@whichopt) {
1.62      albertel  378: 	my $text=$Apache::response::foilgroup{$name.'.text'};
1.90      albertel  379: 	my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
                    380: 	my $lastopt=$lastresponse{$name};
1.101     albertel  381: 	if ($text!~/^\s*$/) { $break='<br />'; }
1.62      albertel  382: 	$result.=$break;
1.113     sakharuk  383: 	if ($target eq 'web') {
1.62      albertel  384: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    385: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
                    386: 		if ($text=~/^\s*$/) {
                    387: 		    $text=$value.$text;
                    388: 		} else {
1.127     albertel  389: 		    $text='<b>'.$value.':</b> '.$text;
1.62      albertel  390: 		}
1.95      albertel  391: 	    } else {
                    392: 		$text='&#149;'.$text;
1.62      albertel  393: 	    }
                    394: 	    $result.=$text."\n";
1.112     sakharuk  395: 	}
1.58      sakharuk  396:       if ($Apache::lonhomework::type eq 'exam') {
1.90      albertel  397: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.58      sakharuk  398:       }
1.90      albertel  399:       $temp++;
1.22      albertel  400:     }
                    401:   } else {
                    402:     my $temp=1;
1.28      albertel  403:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
1.79      sakharuk  404:     my $internal_counter=$Apache::lonxml::counter;
1.22      albertel  405:     foreach $name (@whichopt) {
1.62      albertel  406:       my $text=$Apache::response::foilgroup{$name.'.text'};
                    407:       if ($text!~/^\s*$/) {
                    408: 	  if ($target eq 'tex') {
                    409: 	      $break='\vskip 0 mm ';
                    410: 	  } elsif ($target eq 'web') {
                    411: 	      $break='<br />';
                    412: 	  }
                    413:       }
1.28      albertel  414:       my $lastopt=$lastresponse{$name};
                    415:       my $optionlist="<option></option>\n";
1.126     albertel  416:       foreach my $option (@opt) {
                    417: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
                    418: 	  if ($option eq $lastopt) {
1.133     albertel  419: 	      $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
1.126     albertel  420: 	  } else {
                    421: 	      $optionlist.="<option value='".$escopt."'>$option</option>\n";
                    422: 	  }
1.28      albertel  423:       }
1.32      sakharuk  424:       if ($target ne 'tex') {
1.72      sakharuk  425: 	  if ($Apache::lonhomework::type ne 'exam') {
1.101     albertel  426: 	      $optionlist='<select name="HWVAL_'.
                    427: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
1.59      albertel  428: 		  $optionlist."</select>\n";
1.101     albertel  429: 	  } else {
                    430: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
1.72      sakharuk  431: 	  }
1.101     albertel  432: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
                    433: 	      if ($Apache::lonhomework::type ne 'exam') {
                    434: 		  $text='&#149;'.$text;
                    435: 	      }
1.95      albertel  436: 	  } else {
1.101     albertel  437: 	      if ($Apache::lonhomework::type ne 'exam') {
                    438: 		  $text=$optionlist.$text;
                    439: 	      }
1.59      albertel  440: 	  }
1.62      albertel  441: 	  $result.=$break.$text."\n";
1.49      sakharuk  442: 	  if ($Apache::lonhomework::type eq 'exam') {
1.101     albertel  443: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.49      sakharuk  444: 	  }
1.32      sakharuk  445: 	  $temp++;
                    446:       } else {
1.101     albertel  447: 	  my $texoptionlist='';
                    448: 	  if ($displayoptionintex &&
                    449: 	      $Apache::lonhomework::type ne 'exam') {
1.107     sakharuk  450: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
1.101     albertel  451: 	  }
                    452: 	  if ($text=~/<drawoptionlist\s*\/>/) {
1.136     foxr      453: 	      $text=~s|<drawoptionlist\s*\/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |g;
1.101     albertel  454: 	  }
                    455: 
                    456: 	  if ($text=~m/\\item /) {
1.50      sakharuk  457: 	      if ($Apache::lonhomework::type eq 'exam') {
1.101     albertel  458: 	          $text=~s/\\item/\\vskip 2 mm/;
1.50      sakharuk  459: 	      }
1.101     albertel  460: 	      $result.= $texoptionlist.$text;
1.32      sakharuk  461: 	  } else {
1.101     albertel  462: 	      if ($Apache::lonhomework::type eq 'exam') {
                    463: 		  $result.= $texoptionlist.'  '.$text;
1.46      sakharuk  464: 	      } else {
1.107     sakharuk  465: 		  if ($text=~/\S/) {$result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;} else {$result.= $texoptionlist;}
1.50      sakharuk  466: 	      }
1.32      sakharuk  467: 	  }
1.101     albertel  468: 	  if ($Apache::lonhomework::type eq 'exam') {
                    469: 	      $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.
                    470: 		  $internal_counter.'}.]'.&bubbles(\@alphabet,\@opt).
                    471: 		  '\end{enumerate} \vskip -8 mm \strut ';
                    472: 	      $internal_counter++;
                    473: 	  }
                    474: 	  $displayoptionintex=0;
1.62      albertel  475:       }
1.1       albertel  476:     }
1.62      albertel  477:   }
1.32      sakharuk  478:   if ($target ne 'tex') {
1.62      albertel  479:       return $result.$break;
                    480:   } else {
1.32      sakharuk  481:       return $result;
1.22      albertel  482:   }
1.50      sakharuk  483: }
                    484: 
                    485: 
                    486: sub optionlist_correction {
1.107     sakharuk  487:     my ($TeXlayout,@options) = @_;
1.101     albertel  488:     my $texoptionlist='\\item [] Choices: ';
1.107     sakharuk  489:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
1.101     albertel  490:     if (scalar(@options) > 0) {
                    491: 	foreach my $option (@options) {
                    492: 	    $texoptionlist.='{\bf '.
1.135     albertel  493: 		&Apache::lonxml::latex_special_symbols($option).
1.107     sakharuk  494: 		'}';
                    495: 	    if ($TeXlayout eq 'vertical') {
                    496: 		$texoptionlist.=' \vskip 0 mm ',
                    497: 	    } else {
1.115     sakharuk  498: 		$texoptionlist.=', ';
1.107     sakharuk  499: 	    }
1.101     albertel  500: 	}
1.116     albertel  501: 	$texoptionlist=~s/, $//;
1.107     sakharuk  502: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
1.80      sakharuk  503:     } else {
1.107     sakharuk  504: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
1.80      sakharuk  505:     }
1.50      sakharuk  506:     return $texoptionlist;
1.58      sakharuk  507: }
                    508: 
                    509: 
                    510: sub webbubbles {
1.90      albertel  511:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
1.58      sakharuk  512:     my @opt=@$ropt; 
                    513:     my @alphabet=@$ralphabet;
                    514:     my $result='';
1.90      albertel  515:     my $number_of_bubbles = $#opt + 1;
                    516:     $result.= '<table border="1"><tr>';
                    517:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
                    518: 	my $checked='';
                    519: 	if ($lastopt eq $opt[$ind]) {
                    520: 	    $checked=' checked="on" ';
1.58      sakharuk  521: 	}
1.90      albertel  522: 	$result.='<td><input type="radio" name="HWVAL_'.
                    523: 	    $Apache::inputtags::response['-1'].':'.$temp.
1.111     albertel  524: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
                    525: 	if ($alphabet[$ind]) {
                    526: 	    $result.=$alphabet[$ind].': ';
                    527: 	}
                    528: 	$result.=$opt[$ind].'</td>';
1.90      albertel  529:     }
                    530:     $result.='</tr></table>';
                    531:     return $result;
1.50      sakharuk  532: }
                    533: 
                    534: 
                    535: sub bubbles {
1.119     albertel  536:     my ($ralphabet,$ropt,$response) = @_;
                    537:     my @alphabet = @$ralphabet;
1.50      sakharuk  538:     my @opt = @$ropt;
1.51      sakharuk  539:     my ($result,$head,$line) =('','','');
1.50      sakharuk  540:     my $number_of_bubbles = $#opt + 1;
1.51      sakharuk  541:     my $current_length = 0;
1.100     sakharuk  542:     my $textwidth;
1.125     albertel  543:     if ($env{'form.textwidth'} ne '') {
                    544: 	$env{'form.textwidth'}=~/(\d+)/;
1.100     sakharuk  545: 	$textwidth=$1;
                    546:     } else {
1.125     albertel  547: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.102     sakharuk  548: 	$textwidth=$1.'.'.$2;
1.100     sakharuk  549:     }
1.51      sakharuk  550:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
1.99      sakharuk  551: 	my $leftmargin;
1.101     albertel  552: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
1.119     albertel  553: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
1.99      sakharuk  554: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
1.102     sakharuk  555: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+4)*2;
1.99      sakharuk  556: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
1.51      sakharuk  557: 	    $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
                    558: 	    $head.='lr';
                    559: 	} else {
                    560: 	    $line=~s/\&\s*$//;
1.131     albertel  561: 	    $result.='\vskip -2 mm\noindent\setlength{\tabcolsep}{2 mm}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
1.51      sakharuk  562: 	    $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
                    563: 	    $head ='lr';
                    564: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
                    565: 	}
                    566: 
1.50      sakharuk  567:     }
                    568:     return $result;
1.1       albertel  569: }
                    570: 
1.22      albertel  571: 
1.2       albertel  572: sub start_conceptgroup {
1.22      albertel  573:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    574:   $Apache::optionresponse::conceptgroup=1;
1.44      albertel  575:   %Apache::response::conceptgroup=();
1.22      albertel  576:   my $result;
                    577:   if ($target eq 'edit') {
                    578:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.30      matthew   579:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    580:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22      albertel  581:   }
                    582:   if ($target eq 'modified') {
                    583:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
                    584:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    585:   }
                    586:   return $result;
1.2       albertel  587: }
                    588: 
                    589: sub end_conceptgroup {
1.22      albertel  590:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    591:   $Apache::optionresponse::conceptgroup=0;
                    592:   my $result='';
1.93      albertel  593:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    594:       $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  595:     #if not there aren't any foils to display and thus no question
1.93      albertel  596:       &Apache::response::pick_foil_for_concept($target,
                    597: 					       ['value','text','location'],
                    598: 					       \%Apache::hint::option,
                    599: 					       $parstack,$safeeval);
1.34      albertel  600:   } elsif ($target eq 'edit') {
1.22      albertel  601:     $result=&Apache::edit::end_table();
                    602:   }
                    603:   return $result;
1.2       albertel  604: }
                    605: 
1.16      albertel  606: sub insert_conceptgroup {
1.22      albertel  607:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    608:   return $result;
1.16      albertel  609: }
                    610: 
1.1       albertel  611: sub start_foil {
1.22      albertel  612:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    613:   my $result='';
1.34      albertel  614:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1.105     albertel  615:       &Apache::lonxml::startredirection;
                    616:       if ($target eq 'analyze') {
                    617: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    618:       }
1.25      albertel  619:   } elsif ($target eq 'edit') {
1.22      albertel  620:     $result=&Apache::edit::tag_start($target,$token,"Foil");
                    621:     my $level='-2';
                    622:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
                    623:     my @opt;
                    624:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
                    625:     $result.=&Apache::edit::text_arg('Name:','name',$token);
1.48      albertel  626:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
                    627: 					       ['unused',(@opt)],$token,'15');
                    628:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    629: 					     $safeeval,'-3');
                    630:     if ($randomize ne 'no') {
                    631: 	$result.=&Apache::edit::select_arg('Location:','location',
                    632: 					   ['random','top','bottom'],$token);
                    633:     }
1.30      matthew   634:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25      albertel  635:   } elsif ($target eq 'modified') {
1.48      albertel  636:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    637: 						 'value','name','location');
1.22      albertel  638:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32      sakharuk  639:   } 
1.22      albertel  640:   return $result;
1.1       albertel  641: }
                    642: 
                    643: sub end_foil {
1.22      albertel  644:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    645:   my $text ='';
                    646:   my $result = '';
1.88      albertel  647:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
1.48      albertel  648:       $text=&Apache::lonxml::endredirection;
1.114     sakharuk  649:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
1.107     sakharuk  650:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
1.32      sakharuk  651:   }
                    652:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
1.34      albertel  653:       || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  654:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.122     albertel  655:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                    656: 	$text='\vskip 5mm $\triangleright$ '.$text;
                    657:     }
1.22      albertel  658:     if ($value ne 'unused') {
                    659:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    660:       &Apache::lonxml::debug("Got a name of :$name:");
1.129     albertel  661:       if ($name eq "") {
1.128     albertel  662: 	  &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.123     albertel  663: 	  $name=$Apache::lonxml::curdepth;
                    664:       }
1.22      albertel  665:       &Apache::lonxml::debug("Using a name of :$name:");
1.103     albertel  666:       if (defined($Apache::response::foilnames{$name})) {
                    667: 	  &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
                    668:       }
1.104     albertel  669:       $Apache::response::foilnames{$name}++;
1.103     albertel  670:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.39      albertel  671:       if ( $Apache::optionresponse::conceptgroup
                    672: 	   && !&Apache::response::showallfoils() ) {
1.22      albertel  673: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    674: 	$Apache::response::conceptgroup{"$name.value"} = $value;
1.122     albertel  675: 	$Apache::response::conceptgroup{"$name.text"} = $text;
1.48      albertel  676: 	$Apache::response::conceptgroup{"$name.location"} = $location;
1.22      albertel  677:       } else {
                    678: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
1.48      albertel  679: 	$Apache::response::foilgroup{"$name.value"} = $value;
1.122     albertel  680: 	$Apache::response::foilgroup{"$name.text"} = $text;
1.48      albertel  681: 	$Apache::response::foilgroup{"$name.location"} = $location;
1.22      albertel  682:       }
                    683:     }
                    684:   }
                    685:   if ($target eq 'edit') {
                    686:     $result.= &Apache::edit::tag_end($target,$token,'');
                    687:   }
                    688:   return $result;
1.1       albertel  689: }
                    690: 
1.112     sakharuk  691: sub start_drawoptionlist {
                    692:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.136     foxr      693:     if (($target ne 'meta')  && ($target ne 'answer')) {
1.117     albertel  694: 	return $token->[4];
                    695:     }
1.112     sakharuk  696: }
                    697: 
                    698: sub end_drawoptionlist {
                    699:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.136     foxr      700:     if (($target ne 'meta') && ($target ne 'answer')) {
1.117     albertel  701: 	return $token->[2];
                    702:     }
1.112     sakharuk  703: }
                    704: 
1.7       albertel  705: sub insert_foil {
1.22      albertel  706:   return '
1.15      albertel  707: <foil name="" value="unused">
1.14      albertel  708: <startouttext />
                    709: <endouttext />
1.7       albertel  710: </foil>';
                    711: }
1.1       albertel  712: 1;
                    713: __END__
                    714:  

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