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

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

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