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

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

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