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

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

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