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

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

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