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

1.46      sakharuk    1: # LearningOnline Network with CAPA
1.22      albertel    2: # option list style responses
1.27      albertel    3: #
1.187   ! raeburn     4: # $Id: optionresponse.pm,v 1.186 2012/06/25 10:36:00 foxr Exp $
1.27      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.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.173     raeburn   106:   &Apache::response::pushrandomnumber(undef,$target);
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>
1.162     www       126:         </td>
1.13      albertel  127: ENDTABLE
1.162     www       128:     $result.= '<td>'.&Apache::edit::checked_arg('Print options:','texoptions',
1.132     albertel  129: 					 [ ['nochoice','Don\'t show option list'] ]
1.162     www       130: 					 ,$token).'</td>';
                    131:     $result.= '<td><label>'.&mt('Two-option checkboxes for:').
                    132:               '<select name="checkboxvalue_'.$Apache::lonxml::curdepth.'">';
                    133:     foreach $option (('',@opt)) {
                    134:        $result.='<option value="'.$option.'"';
                    135:        if ($option eq &Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval)) {
                    136:           $result.=' selected="selected"';
                    137:        }
                    138:        $result.='>'.$option.'</option>';
                    139:     }
1.172     www       140:     $result.='</select></label>';
                    141:     $result.=&Apache::edit::checked_arg('Checkbox options:','checkboxoptions',
                    142:                                          [ ['nochoice','Don\'t show option list'] ]
                    143:                                          ,$token).'</td>';
1.30      matthew   144:     $result.= &Apache::edit::end_row();
                    145:     $result.= &Apache::edit::start_spanning_row();
                    146:     $result.= $insertlist.'<br />';
1.22      albertel  147:   }
                    148:   if ($target eq 'modified') {
                    149:     my @options;
                    150:     my $optchanged=0;
                    151:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.125     albertel  152:     if ($env{"form.$Apache::lonxml::curdepth.deleteopt"}) {
                    153:       my $delopt=$env{"form.$Apache::lonxml::curdepth.deleteopt"};
1.22      albertel  154:       &Apache::lonxml::debug("Deleting :$delopt:");
                    155:       splice(@options,$delopt-1,1);
                    156:       $optchanged=1;
                    157:     }
1.180     raeburn   158:     if ($env{"form.$Apache::lonxml::curdepth.options"} ne '') {
1.125     albertel  159:       my $newopt = $env{"form.$Apache::lonxml::curdepth.options"};
1.180     raeburn   160:       push(@options,$newopt);
1.22      albertel  161:       $optchanged=1;
                    162:     }
1.132     albertel  163:     my $rebuildtag = &Apache::edit::get_new_args($token,$parstack,$safeeval,
1.172     www       164: 						 'texoptions','checkboxvalue','checkboxoptions');
1.132     albertel  165:     if ($optchanged || $rebuildtag ) {
1.22      albertel  166:       $result = "<foilgroup options=\"(";
                    167:       foreach my $option (@options) {
1.41      albertel  168: 	$option=~s/\'/\\\'/g;
1.22      albertel  169: 	&Apache::lonxml::debug("adding option :$option:");
                    170: 	$result .="'".$option."',";
                    171:       }
                    172:       chop $result;
1.132     albertel  173:       $result.=')" ';
                    174:       $result .= 'texoptions="'.$token->[2]{'texoptions'}.'" ';
1.172     www       175:       $result .= 'checkboxoptions="'.$token->[2]{'checkboxoptions'}.'" ';
1.162     www       176:       $result .= 'checkboxvalue="'.$token->[2]{'checkboxvalue'}.'"';
1.132     albertel  177:       $result .= '>';
1.22      albertel  178:     } # else nothing changed so just use the default mechanism
                    179:   }
1.68      sakharuk  180:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.152     onken     181:     if($env{'form.pdfFormFields'} ne 'yes') {
1.167     raeburn   182:       $result .= ' \begin{itemize} ';
1.152     onken     183:     } else {
                    184:       $result .= "\\\\";
                    185:     }
1.42      sakharuk  186:   }
1.22      albertel  187:   return $result;
1.1       albertel  188: }
                    189: 
                    190: sub end_foilgroup {
1.22      albertel  191:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    192:   
                    193:   my $result;
1.94      albertel  194:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    195:       $target eq 'tex' || $target eq 'analyze') {
1.81      sakharuk  196:     my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
1.22      albertel  197:     my $name;
                    198:     my @opt;
                    199:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
1.126     albertel  200:     &Apache::lonxml::debug("options:".join(':',@opt));
                    201: 
1.108     albertel  202:     my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
                    203: 					     -2,0);
1.161     www       204:     my $checkboxvalue=&Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval);
1.172     www       205:     my $checkboxchoices=(&Apache::lonxml::get_param('checkboxoptions',$parstack,$safeeval) ne 'nochoice');
1.186     foxr      206:     my $noprompt       =&Apache::lonxml::get_param('noprompt', $parstack, $safeeval);
                    207: 
1.81      sakharuk  208:     if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
1.22      albertel  209:     &Apache::lonxml::debug("Options are $#opt");
1.48      albertel  210:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    211:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    212: 					       $safeeval,'-2');
1.32      sakharuk  213:     if ($target eq 'web' || $target eq 'tex') {
1.186     foxr      214:       $result.=&displayfoils($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,$tex_option_switch, $noprompt, @opt);
1.137     albertel  215:       $Apache::lonxml::post_evaluate=0;
1.25      albertel  216:     } elsif ( $target eq 'answer') {
1.48      albertel  217:       $result.=&displayanswers($max,$randomize,@opt);
1.136     foxr      218:       
1.34      albertel  219:     } elsif ( $target eq 'analyze') {
1.93      albertel  220: 	my @shown = &whichfoils($max,$randomize);
1.94      albertel  221: 	&Apache::response::analyze_store_foilgroup(\@shown,
                    222: 						  ['text','value','location']);
1.93      albertel  223: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    224: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
1.22      albertel  225:     } elsif ( $target eq 'grade') {
1.175     www       226: 
1.124     albertel  227:       if ( &Apache::response::submitted()) {
1.175     www       228: 
1.48      albertel  229: 	my @whichopt = &whichfoils($max,$randomize);
1.22      albertel  230: 	my $temp=1;my $name;
1.26      albertel  231: 	my %responsehash;
1.57      albertel  232: 	my %grade;
1.22      albertel  233: 	my $right=0;
                    234: 	my $wrong=0;
                    235: 	my $ignored=0;
1.177     raeburn   236:         my ($numrows,$bubbles_per_row);
                    237:         if ($Apache::lonhomework::scantronmode) {
                    238:             my $numitems = scalar(@opt);
                    239:             ($numrows,$bubbles_per_row) =
                    240:                 &Apache::optionresponse::getnumrows($numitems);
                    241:         }
                    242:         if ($numrows < 1) {
                    243:             $numrows = 1;
                    244:         }
1.22      albertel  245: 	foreach $name (@whichopt) {
1.177     raeburn   246: 	  my $response;
                    247:           if ($env{'form.submitted'} eq 'scantron') {
                    248:               if ($numrows > 1) {
                    249:                   my $num = $temp;
                    250:                   my $totalnum;
                    251:                   for (my $i=0; $i<$numrows; $i++) {
                    252:                       my $item = &Apache::response::getresponse($num);
                    253:                       if ($item =~ /^\d+$/) {
                    254:                           $totalnum = $i*$bubbles_per_row + $item;
                    255:                       }
                    256:                       $num ++;
                    257:                   }
                    258:                   if ($totalnum =~ /^\d+$/) {
                    259:                       $response = $opt[$totalnum];
                    260:                   }
                    261:                   $temp += $numrows;
                    262:               } else {
1.178     raeburn   263:                   $response=&Apache::response::getresponse($temp);
1.177     raeburn   264:                   if ($response=~/\S/) {
                    265:                       $response = $opt[$response];
                    266:                   }
                    267:                   $temp ++;
                    268:               }
                    269: 	  } else {
1.178     raeburn   270:               $response=&Apache::response::getresponse($temp);
1.177     raeburn   271:               $temp ++;
                    272:           }
1.22      albertel  273: 	  if ( $response =~ /[^\s]/) {
1.96      albertel  274: 	    $responsehash{$name}=$response;
1.22      albertel  275: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
1.34      albertel  276: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
1.57      albertel  277: 	    if ($value eq $response) {
                    278: 		$grade{$name}='1'; $right++;
                    279: 	    } else {
                    280: 		$grade{$name}='0'; $wrong++;
                    281: 	    }
1.22      albertel  282: 	  } else {
                    283: 	    $ignored++;
                    284: 	  }
                    285: 	}
1.28      albertel  286: 	my $part=$Apache::inputtags::part;
1.184     raeburn   287:         my $nonlenient=&grading_is_nonlenient($part);
1.22      albertel  288: 	my $id = $Apache::inputtags::response['-1'];
1.28      albertel  289: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.57      albertel  290: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
1.28      albertel  291: 	my %previous=&Apache::response::check_for_previous($responsestr,
                    292: 							   $part,$id);
1.22      albertel  293: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
1.75      albertel  294: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    295: 	    $responsestr;
                    296: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
1.173     raeburn   297:         if ($Apache::lonhomework::type eq 'randomizetry') {
                    298:             $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
                    299:         }
1.163     raeburn   300: 	if (($Apache::lonhomework::type eq 'survey') ||
                    301:             ($Apache::lonhomework::type eq 'surveycred') ||
                    302:             ($Apache::lonhomework::type eq 'anonsurvey') ||
                    303:             ($Apache::lonhomework::type eq 'anonsurveycred')) {
1.121     albertel  304: 	    if ($ignored == 0) {
1.163     raeburn   305:                 my $ad;
                    306:                 if ($Apache::lonhomework::type eq 'anonsurveycred') {
                    307:                     $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
                    308:                 } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
                    309:                     $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
                    310:                 } elsif ($Apache::lonhomework::type eq 'surveycred') {
                    311:                     $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
                    312:                 } else {
                    313: 		    $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
                    314:                 }
1.121     albertel  315: 		&Apache::response::handle_previous(\%previous,$ad);
                    316: 	    } elsif ($wrong==0 && $right==0) {
                    317: 	    } else {
                    318: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
                    319: 		&Apache::response::handle_previous(\%previous,$ad);
                    320: 	    }
1.174     www       321: 	} elsif ($nonlenient) {
                    322: #
                    323: # Non-lenient mode. All right or all wrong
                    324: #
1.75      albertel  325: 	    my $ad;
                    326: 	    if ($wrong==0 && $ignored==0) {
                    327: 		$ad='EXACT_ANS';
                    328: 	    } elsif ($wrong==0 && $right==0) {
                    329: 		#nothing submitted
                    330: 	    } else {
                    331: 		if ($ignored==0) {
                    332: 		    $ad='INCORRECT';
                    333: 		} else {
                    334: 		    $ad='MISSING_ANSWER';
                    335: 		}
                    336: 	    }
                    337: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    338: 	    &Apache::response::handle_previous(\%previous,$ad);
1.22      albertel  339: 	} else {
1.174     www       340: #
                    341: # This is lenient mode
                    342: #
1.75      albertel  343: 	    my $ad;
                    344: 	    if ($wrong==0 && $right==0) {
1.120     albertel  345: 		#nothing submitted only assign a score if we 
                    346: 		#need to override a previous grade
                    347: 		if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
                    348: 		    $ad='ASSIGNED_SCORE';
                    349: 		}
1.75      albertel  350: 	    } else {
                    351: 		$ad='ASSIGNED_SCORE';
                    352: 	    }
                    353: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    354: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
                    355: 		$right/(scalar(@whichopt));
1.76      albertel  356: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
                    357: 		scalar(@whichopt);
1.1       albertel  358: 	}
1.22      albertel  359:       }
1.1       albertel  360:     }
1.147     foxr      361:     my $part_id     = $Apache::inputtags::part;
                    362:     my $response_id = $Apache::inputtags::response[-1];
1.177     raeburn   363:     my ($numrows,$bubbles_per_row);
                    364:     if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
                    365:         ($numrows,$bubbles_per_row) =
                    366:             &Apache::optionresponse::getnumrows(scalar(@opt));
                    367:     }
                    368:     if ($numrows < 1) {
                    369:         $numrows = 1;
                    370:     }
                    371:     my $increment = &getfoilcounts($max) * $numrows;
                    372:     &Apache::lonxml::increment_counter($increment,"$part_id.$response_id");
1.146     foxr      373:     if ($target eq 'analyze') {
                    374: 	&Apache::lonhomework::set_bubble_lines();
                    375:     }
                    376: 
1.25      albertel  377:   } elsif ($target eq 'edit') {
1.22      albertel  378:     $result.=&Apache::edit::end_table();
1.74      albertel  379:   }
                    380:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.152     onken     381:     if($env{'form.pdfFormFields'} ne 'yes') {
1.167     raeburn   382:       $result .= '\end{itemize}'; 
1.152     onken     383:     } else {
                    384:       $result .= "\\\\";
                    385:     }
1.74      albertel  386:   }
1.110     albertel  387:   &Apache::response::poprandomnumber();
1.22      albertel  388:   return $result;
1.1       albertel  389: }
                    390: 
1.184     raeburn   391: sub grading_is_nonlenient {
                    392:     my ($part) = @_;
                    393: # Web mode: we are non-lenient unless told otherwise
                    394:     my $defaultparm = 'off';
                    395:     my $nonlenient = 0;
                    396: # Grading a bubblesheet exam: we are grading lenient unless told otherwise
                    397:     if ($Apache::lonhomework::scantronmode) {
                    398:         $defaultparm = 'on';
                    399:         $nonlenient = 1;
                    400:     }
                    401:     my $lenientparm =
                    402:         &Apache::response::get_response_param($part,'lenient',$defaultparm);
                    403:     if ($lenientparm=~/^0|off|no$/i) {
                    404:         $nonlenient = 1;
                    405:     } elsif ($lenientparm=~/^1|on|yes$/i) {
                    406:         $nonlenient = 0;
                    407:     }
                    408:     return $nonlenient;
                    409: }
                    410: 
1.1       albertel  411: sub getfoilcounts {
1.48      albertel  412:   my ($max)=@_;
1.22      albertel  413:   # +1 since instructors will count from 1
                    414:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.39      albertel  415:   if (&Apache::response::showallfoils()) { $max=$count; }
1.48      albertel  416:   if ($count>$max) { $count=$max } 
                    417:   &Apache::lonxml::debug("Count is $count from $max");
                    418:   return $count;
1.1       albertel  419: }
                    420: 
                    421: sub whichfoils {
1.77      albertel  422:     my ($max,$randomize)=@_;
                    423:     return &Apache::response::whichorder($max,$randomize,
                    424: 					 &Apache::response::showallfoils(),
                    425: 					 \%Apache::response::foilgroup);
1.1       albertel  426: }
                    427: 
1.25      albertel  428: sub displayanswers {
1.130     albertel  429:     my ($max,$randomize,@opt)=@_;
1.187   ! raeburn   430:     my @names;
        !           431:     if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
        !           432:         @names = @{ $Apache::response::foilgroup{'names'} };
        !           433:     }
        !           434:     return if (!@names);
1.130     albertel  435:     my @whichopt = &whichfoils($max,$randomize);
                    436:     my $result;
                    437:     if ($Apache::lonhomework::type eq 'exam') {
                    438: 	my $i = 0;
                    439: 	my %opt = map { ($_,$i++) } @opt;
                    440: 	
                    441: 	$i = 0;
                    442: 	my @alphabet = ('A'..'Z');
                    443: 	foreach my $name (@whichopt) {
                    444: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
                    445: 	    $result.=&Apache::response::answer_part('optionresponse',
                    446: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
                    447: 	    $result.=&Apache::response::answer_part('optionresponse',
                    448: 						    $Apache::response::foilgroup{$name.'.value'});
                    449: 	    $result.=&Apache::response::answer_footer('optionresponse');
                    450: 	}
                    451:     } else {
                    452: 	$result=&Apache::response::answer_header('optionresponse');
                    453: 	foreach my $name (@whichopt) {
                    454: 	    $result.=&Apache::response::answer_part('optionresponse',
                    455: 						    $Apache::response::foilgroup{$name.'.value'});
                    456: 	}
                    457: 	$result.=&Apache::response::answer_footer('optionresponse');
                    458:     }
                    459:     return $result;
1.25      albertel  460: }
                    461: 
1.160     www       462: sub check_box_opt {
1.161     www       463:     my ($target,$checkboxvalue,@opt)=@_;
                    464: # Check if we are in checkbox mode: checkboxvalue specified, on web, only two options.
                    465: # If so, return "checked" value
                    466:     if ($#opt!=1) { return ''; }
                    467:     if ($target ne 'web') { return ''; }
                    468:     return $checkboxvalue;
1.160     www       469: }
                    470: 
1.106     albertel  471: sub check_for_invalid {
                    472:     my ($names,$options) = @_;
                    473:     my %bad_names;
                    474:     foreach my $name (@{ $names }) {
                    475: 	my $value=$Apache::response::foilgroup{$name.'.value'};
                    476: 	my $found=0;
                    477: 	foreach my $option (@{ $options }) {
                    478: 	    if ($value eq $option) { $found=1; }
                    479: 	}
                    480: 	if (!$found) { $bad_names{$name}=$value; }
                    481:     }
                    482:     if (%bad_names) {
                    483: 	my $error=&mt('The question can not be gotten correct, '.
                    484: 	    'the following foils in the &lt;optionresponse&gt; '.
                    485: 	    'have invalid correct options').' <br /><tt>'.
                    486: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
                    487: 	    "</tt>";
                    488: 	&Apache::lonxml::error($error);
                    489:     }
                    490: }
                    491: 
1.1       albertel  492: sub displayfoils {
1.182     raeburn   493:   my ($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,
1.186     foxr      494:       $tex_option_switch, $no_tfprompt, @opt)=@_;
1.67      albertel  495:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
1.22      albertel  496:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    497:   my @truelist;
                    498:   my @falselist;
1.79      sakharuk  499:   my $result;  
1.22      albertel  500:   my $name;
1.101     albertel  501:   my $displayoptionintex=1;
1.72      sakharuk  502:   my @alphabet = ('A'..'Z');
1.48      albertel  503:   my @whichopt = &whichfoils($max,$randomize);
1.182     raeburn   504:   unless (($target eq 'tex') && ($tex_option_switch eq 'nochoice')) {
                    505:       &check_for_invalid(\@whichopt,\@opt);
                    506:   }
1.28      albertel  507:   my $part=$Apache::inputtags::part;
                    508:   my $id=$Apache::inputtags::response[-1];
1.62      albertel  509:   my $break;
1.86      albertel  510:   if ( ($target ne 'tex') &&
                    511:        &Apache::response::show_answer() ) {
1.90      albertel  512:     my $temp=1;
1.22      albertel  513:     foreach $name (@whichopt) {
1.62      albertel  514: 	my $text=$Apache::response::foilgroup{$name.'.text'};
1.163     raeburn   515:         my $lastresp;
                    516:         unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) && (defined($env{'form.grade_symb'}))) {
                    517:             $lastresp = $Apache::lonhomework::history{"resource.$part.$id.submission"};
                    518:         }
                    519: 	my %lastresponse=&Apache::lonnet::str2hash($lastresp);
1.90      albertel  520: 	my $lastopt=$lastresponse{$name};
1.101     albertel  521: 	if ($text!~/^\s*$/) { $break='<br />'; }
1.62      albertel  522: 	$result.=$break;
1.113     sakharuk  523: 	if ($target eq 'web') {
1.62      albertel  524: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
                    525: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
                    526: 		if ($text=~/^\s*$/) {
                    527: 		    $text=$value.$text;
                    528: 		} else {
1.127     albertel  529: 		    $text='<b>'.$value.':</b> '.$text;
1.62      albertel  530: 		}
1.95      albertel  531: 	    } else {
1.170     raeburn   532:                 if (@whichopt > 1) {
                    533:                     $text='&#149;'.$text;
                    534:                 }
1.62      albertel  535: 	    }
                    536: 	    $result.=$text."\n";
1.112     sakharuk  537: 	}
1.58      sakharuk  538:       if ($Apache::lonhomework::type eq 'exam') {
1.90      albertel  539: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.58      sakharuk  540:       }
1.90      albertel  541:       $temp++;
1.22      albertel  542:     }
                    543:   } else {
                    544:     my $temp=1;
1.173     raeburn   545:     my %lastresponse;
                    546:     my $newvariation;
                    547:     if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
                    548:         ($Apache::lonhomework::type eq 'randomizetry')) &&
                    549:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
                    550:         if ($env{'form.'.$part.'.rndseed'} ne
                    551:             $Apache::lonhomework::history{"resource.$part.rndseed"}) {
                    552:             $newvariation = 1;
                    553:         }
                    554:     }
                    555:     unless ($newvariation) {
                    556:         %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
                    557:     }
1.79      sakharuk  558:     my $internal_counter=$Apache::lonxml::counter;
1.161     www       559:     my $checkboxopt=&check_box_opt($target,$checkboxvalue,@opt);
1.186     foxr      560:     if ($checkboxopt && (!$no_tfprompt)) {
1.172     www       561:        $result.='<br />'.
                    562:                 ($checkboxchoices?&mt('Choices: ').'<b>'.$opt[0].','.$opt[1].'</b>. ':'').
1.168     www       563:                  &mt('Select all that are <b>[_1]</b>.',$checkboxopt);
1.160     www       564:     }
1.22      albertel  565:     foreach $name (@whichopt) {
1.62      albertel  566:       my $text=$Apache::response::foilgroup{$name.'.text'};
                    567:       if ($text!~/^\s*$/) {
                    568: 	  if ($target eq 'tex') {
                    569: 	      $break='\vskip 0 mm ';
                    570: 	  } elsif ($target eq 'web') {
                    571: 	      $break='<br />';
                    572: 	  }
                    573:       }
1.28      albertel  574:       my $lastopt=$lastresponse{$name};
                    575:       my $optionlist="<option></option>\n";
1.152     onken     576: 
1.158     onken     577:       if($target eq 'tex' and $env{'form.pdfFormFields'} eq 'yes'
                    578:          && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.152     onken     579:           my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&optionresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
                    580:           $optionlist =  &Apache::lonxml::print_pdf_start_combobox($fieldname);
                    581:       }
                    582: 
1.126     albertel  583:       foreach my $option (@opt) {
                    584: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
1.152     onken     585:           if ($option eq $lastopt) {
1.158     onken     586:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.164     onken     587:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
                    588:                   && $Apache::lonhomework::type ne 'exam') {
1.152     onken     589:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
                    590:               } else {
                    591:                   $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
                    592:               }
                    593:           } else {
1.158     onken     594:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.164     onken     595:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
                    596:                   && $Apache::lonhomework::type ne 'exam') {
1.152     onken     597:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
                    598:               } else {
                    599:                   $optionlist.="<option value='".$escopt."'>$option</option>\n";
                    600:               }
                    601:           }
1.28      albertel  602:       }
1.32      sakharuk  603:       if ($target ne 'tex') {
1.72      sakharuk  604: 	  if ($Apache::lonhomework::type ne 'exam') {
1.159     www       605: # we are on the web, this is not an exam, and the problem can be answered
                    606:               if ($checkboxopt) {
                    607: # generate checkboxes
                    608:                   my $fieldname=$Apache::inputtags::response['-1'].':'.$temp;
                    609:                   my $altopt=$opt[0];
                    610:                   if ($opt[0] eq $checkboxopt) {
                    611:                      $altopt=$opt[1];
                    612:                   }
                    613:                   my $defopt=$lastopt;
                    614:                   unless ($defopt) { $defopt=$altopt; }
                    615:                   my $escdefopt=&HTML::Entities::encode($defopt,'\'"&<>');
                    616:                   my $esccheckboxopt=&HTML::Entities::encode($checkboxopt,'\'"&<>');
                    617:                   my $escaltopt=&HTML::Entities::encode($altopt,'\'"&<>');
                    618: # checkboxopt is how the box is labelled
                    619: # altopt is the alternative option
                    620: # lastopt is what the user submitted before
                    621: # defopt is what the field is going to start out with: either previous choice or altopt
                    622: # fieldname is this input field's name after HWVAL_
                    623:                   $optionlist='<input type="hidden" name="HWVAL_'.$fieldname.'" value="'.$escdefopt.'" />'.
1.160     www       624:                   '<input type="checkbox" name="HWCHK_'.$fieldname.'" onclick="javascript:if (this.form.elements[\'HWCHK_'.
                    625:                   $fieldname.'\'].checked) { this.form.elements[\'HWVAL_'.$fieldname.'\'].value=\''.$esccheckboxopt.'\'; } else { this.form.elements[\'HWVAL_'.$fieldname.'\'].value=\''.$escaltopt.'\'; };setSubmittedPart(\''.$part.'\');"'.($defopt eq $checkboxopt?' checked="checked"':'')." />\n";
1.159     www       626:               } else {
                    627: # classic selection list
                    628: 	          $optionlist='<select onchange="javascript:setSubmittedPart(\''.
1.139     albertel  629: 		  $part.'\');" name="HWVAL_'.
1.101     albertel  630: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
1.59      albertel  631: 		  $optionlist."</select>\n";
1.159     www       632:               }
1.101     albertel  633: 	  } else {
                    634: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
1.72      sakharuk  635: 	  }
1.101     albertel  636: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
                    637: 	      if ($Apache::lonhomework::type ne 'exam') {
1.170     raeburn   638:                   if (@whichopt > 1) {  
                    639:                       $text='&#149;'.$text;
                    640:                   }
1.101     albertel  641: 	      }
1.95      albertel  642: 	  } else {
1.101     albertel  643: 	      if ($Apache::lonhomework::type ne 'exam') {
                    644: 		  $text=$optionlist.$text;
                    645: 	      }
1.59      albertel  646: 	  }
1.62      albertel  647: 	  $result.=$break.$text."\n";
1.49      sakharuk  648: 	  if ($Apache::lonhomework::type eq 'exam') {
1.101     albertel  649: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
1.49      sakharuk  650: 	  }
1.32      sakharuk  651: 	  $temp++;
                    652:       } else {
1.101     albertel  653: 	  my $texoptionlist='';
                    654: 	  if ($displayoptionintex &&
                    655: 	      $Apache::lonhomework::type ne 'exam') {
1.107     sakharuk  656: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
1.101     albertel  657: 	  }
                    658: 	  if ($text=~/<drawoptionlist\s*\/>/) {
1.136     foxr      659: 	      $text=~s|<drawoptionlist\s*\/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |g;
1.101     albertel  660: 	  }
                    661: 
                    662: 	  if ($text=~m/\\item /) {
1.50      sakharuk  663: 	      if ($Apache::lonhomework::type eq 'exam') {
1.101     albertel  664: 	          $text=~s/\\item/\\vskip 2 mm/;
1.152     onken     665: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
                    666:                   $result.= $texoptionlist.$text;
                    667:               }
                    668:           } else {
1.101     albertel  669: 	      if ($Apache::lonhomework::type eq 'exam') {
                    670: 		  $result.= $texoptionlist.'  '.$text;
1.152     onken     671: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
                    672: 		  if ($text=~/\S/) {
                    673:                       $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;
                    674:                   } else {
                    675:                       $result.= $texoptionlist;
                    676:                   }
1.50      sakharuk  677: 	      }
1.32      sakharuk  678: 	  }
1.101     albertel  679: 	  if ($Apache::lonhomework::type eq 'exam') {
1.165     foxr      680: 	      $result.='\vskip -1 mm\noindent';
1.177     raeburn   681:               my ($numrows,$bubbles_per_row) = &getnumrows(scalar(@opt)); 
                    682:               if ($numrows == 1) {  
                    683:                   $result .= '\textbf{'.$internal_counter.'}. \vskip -3mm';
                    684:               } else {
                    685:                   my $linetext;
                    686:                   for (my $i=0; $i<$numrows; $i++) {
                    687:                       $linetext .= $internal_counter+$i.', ';
                    688:                   }
                    689:                   $linetext =~ s/,\s$//;
                    690:                   $result .= '\small {\textbf{'.$linetext.'}} '.
                    691:                              '\hskip 2 mm {\footnotesize '.
                    692:                              &mt('(Bubble once in [_1] lines)',$numrows).
                    693:                              '} \vskip 1 mm';
                    694:               }
                    695:               $result.= &bubbles(\@alphabet,\@opt,undef,undef,$numrows,
                    696:                                  $bubbles_per_row,$internal_counter).
                    697:                         ' \strut ';
                    698: 	      $internal_counter += $numrows;
1.101     albertel  699: 	  }
1.158     onken     700:           if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.164     onken     701:               && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
                    702:               && $Apache::lonhomework::type ne 'exam') {
1.158     onken     703:               $text =~ s/\\item//m;
1.152     onken     704:               $result .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
                    705:               $temp++;
                    706:           }
1.101     albertel  707: 	  $displayoptionintex=0;
1.62      albertel  708:       }
1.1       albertel  709:     }
1.62      albertel  710:   }
1.138     albertel  711: 
                    712:   if ($target eq 'web') {
1.173     raeburn   713:       my $data = [\@whichopt,'submissiongrading'];
                    714:       my $questiontype;
                    715:       if ($Apache::lonhomework::type eq 'randomizetry') {
                    716:           $questiontype = $Apache::lonhomework::type,
                    717:       }
                    718:       &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,$data,$questiontype);
1.138     albertel  719:   }
                    720: 
1.32      sakharuk  721:   if ($target ne 'tex') {
1.62      albertel  722:       return $result.$break;
                    723:   } else {
1.32      sakharuk  724:       return $result;
1.22      albertel  725:   }
1.50      sakharuk  726: }
                    727: 
                    728: 
                    729: sub optionlist_correction {
1.107     sakharuk  730:     my ($TeXlayout,@options) = @_;
1.150     bisitz    731:     my $texoptionlist='\\item [] '.&mt('Choices:').' ';
1.107     sakharuk  732:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
1.101     albertel  733:     if (scalar(@options) > 0) {
                    734: 	foreach my $option (@options) {
                    735: 	    $texoptionlist.='{\bf '.
1.135     albertel  736: 		&Apache::lonxml::latex_special_symbols($option).
1.107     sakharuk  737: 		'}';
                    738: 	    if ($TeXlayout eq 'vertical') {
                    739: 		$texoptionlist.=' \vskip 0 mm ',
                    740: 	    } else {
1.115     sakharuk  741: 		$texoptionlist.=', ';
1.107     sakharuk  742: 	    }
1.101     albertel  743: 	}
1.116     albertel  744: 	$texoptionlist=~s/, $//;
1.107     sakharuk  745: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
1.80      sakharuk  746:     } else {
1.107     sakharuk  747: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
1.80      sakharuk  748:     }
1.50      sakharuk  749:     return $texoptionlist;
1.58      sakharuk  750: }
                    751: 
                    752: 
                    753: sub webbubbles {
1.90      albertel  754:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
1.58      sakharuk  755:     my @opt=@$ropt; 
                    756:     my @alphabet=@$ralphabet;
                    757:     my $result='';
1.90      albertel  758:     my $number_of_bubbles = $#opt + 1;
                    759:     $result.= '<table border="1"><tr>';
                    760:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
                    761: 	my $checked='';
                    762: 	if ($lastopt eq $opt[$ind]) {
                    763: 	    $checked=' checked="on" ';
1.58      sakharuk  764: 	}
1.90      albertel  765: 	$result.='<td><input type="radio" name="HWVAL_'.
                    766: 	    $Apache::inputtags::response['-1'].':'.$temp.
1.111     albertel  767: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
                    768: 	if ($alphabet[$ind]) {
                    769: 	    $result.=$alphabet[$ind].': ';
                    770: 	}
                    771: 	$result.=$opt[$ind].'</td>';
1.90      albertel  772:     }
                    773:     $result.='</tr></table>';
                    774:     return $result;
1.50      sakharuk  775: }
                    776: 
                    777: 
                    778: sub bubbles {
1.177     raeburn   779:     my ($ralphabet,$ropt,$response,$max_width,$numrows,$bubbles_per_row,
                    780:         $internal_counter) = @_;
1.119     albertel  781:     my @alphabet = @$ralphabet;
1.50      sakharuk  782:     my @opt = @$ropt;
1.51      sakharuk  783:     my ($result,$head,$line) =('','','');
1.50      sakharuk  784:     my $number_of_bubbles = $#opt + 1;
1.51      sakharuk  785:     my $current_length = 0;
1.100     sakharuk  786:     my $textwidth;
1.141     albertel  787:     if (defined($max_width)) {
1.140     foxr      788: 	$textwidth=$max_width;
1.141     albertel  789: 	&Apache::lonxml::debug("Max width passed in: $max_width");
                    790:     } elsif ($env{'form.textwidth'} ne '') {
1.125     albertel  791: 	$env{'form.textwidth'}=~/(\d+)/;
1.100     sakharuk  792: 	$textwidth=$1;
1.141     albertel  793: 	&Apache::lonxml::debug("Max width from form: $textwidth");
1.100     sakharuk  794:     } else {
1.125     albertel  795: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.102     sakharuk  796: 	$textwidth=$1.'.'.$2;
1.141     albertel  797: 	&Apache::lonxml::debug("Max width defaults? $textwidth");
1.100     sakharuk  798:     }
1.141     albertel  799:     &Apache::lonxml::debug("Final maxwidth: $textwidth");
1.51      sakharuk  800:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
1.177     raeburn   801:         my $item;
                    802:         if ($numrows > 1) {
                    803:             my $num = $internal_counter+int($ind/$bubbles_per_row);
                    804:             my $idx = int($ind % $bubbles_per_row);
                    805:             $item = $num.$alphabet[$idx];
                    806:         } else {
                    807:             $item = $alphabet[$ind];
                    808:         }
1.99      sakharuk  809: 	my $leftmargin;
1.101     albertel  810: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
1.119     albertel  811: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
1.99      sakharuk  812: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
1.140     foxr      813: 
1.185     raeburn   814: 	$current_length += (length($opt[$ind])+length($item)+5)*2;
1.177     raeburn   815:         
1.99      sakharuk  816: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
1.177     raeburn   817:             
                    818: 	    $line.='\hskip 4 mm {\small \textbf{'.$item.'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
1.51      sakharuk  819: 	    $head.='lr';
                    820: 	} else {
                    821: 	    $line=~s/\&\s*$//;
1.171     raeburn   822: 	    $result.='\vskip -1 mm\noindent\setlength{\tabcolsep}{2 mm}\renewcommand{\arraystretch}{1.25}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
1.177     raeburn   823: 	    $line = '\hskip 4 mm {\small \textbf{'.$item.'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
1.51      sakharuk  824: 	    $head ='lr';
1.177     raeburn   825: 	    $current_length = (length($opt[$ind])+length($item))*2;
1.51      sakharuk  826: 	}
                    827: 
1.50      sakharuk  828:     }
                    829:     return $result;
1.1       albertel  830: }
                    831: 
1.2       albertel  832: sub start_conceptgroup {
1.22      albertel  833:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    834:   $Apache::optionresponse::conceptgroup=1;
1.44      albertel  835:   %Apache::response::conceptgroup=();
1.22      albertel  836:   my $result;
                    837:   if ($target eq 'edit') {
                    838:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
1.30      matthew   839:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    840:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.22      albertel  841:   }
                    842:   if ($target eq 'modified') {
                    843:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
                    844:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    845:   }
                    846:   return $result;
1.2       albertel  847: }
                    848: 
                    849: sub end_conceptgroup {
1.22      albertel  850:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    851:   $Apache::optionresponse::conceptgroup=0;
                    852:   my $result='';
1.93      albertel  853:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                    854:       $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  855:     #if not there aren't any foils to display and thus no question
1.93      albertel  856:       &Apache::response::pick_foil_for_concept($target,
                    857: 					       ['value','text','location'],
                    858: 					       \%Apache::hint::option,
                    859: 					       $parstack,$safeeval);
1.34      albertel  860:   } elsif ($target eq 'edit') {
1.22      albertel  861:     $result=&Apache::edit::end_table();
                    862:   }
                    863:   return $result;
1.2       albertel  864: }
                    865: 
1.16      albertel  866: sub insert_conceptgroup {
1.22      albertel  867:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    868:   return $result;
1.16      albertel  869: }
                    870: 
1.1       albertel  871: sub start_foil {
1.22      albertel  872:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    873:   my $result='';
1.34      albertel  874:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1.105     albertel  875:       &Apache::lonxml::startredirection;
                    876:       if ($target eq 'analyze') {
                    877: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    878:       }
1.25      albertel  879:   } elsif ($target eq 'edit') {
1.22      albertel  880:     $result=&Apache::edit::tag_start($target,$token,"Foil");
                    881:     my $level='-2';
                    882:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
                    883:     my @opt;
                    884:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
                    885:     $result.=&Apache::edit::text_arg('Name:','name',$token);
1.48      albertel  886:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
                    887: 					       ['unused',(@opt)],$token,'15');
                    888:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    889: 					     $safeeval,'-3');
                    890:     if ($randomize ne 'no') {
                    891: 	$result.=&Apache::edit::select_arg('Location:','location',
                    892: 					   ['random','top','bottom'],$token);
                    893:     }
1.30      matthew   894:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.25      albertel  895:   } elsif ($target eq 'modified') {
1.48      albertel  896:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    897: 						 'value','name','location');
1.22      albertel  898:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.32      sakharuk  899:   } 
1.22      albertel  900:   return $result;
1.1       albertel  901: }
                    902: 
                    903: sub end_foil {
1.22      albertel  904:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    905:   my $text ='';
                    906:   my $result = '';
1.88      albertel  907:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
1.48      albertel  908:       $text=&Apache::lonxml::endredirection;
1.114     sakharuk  909:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
1.107     sakharuk  910:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
1.32      sakharuk  911:   }
                    912:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
1.34      albertel  913:       || $target eq 'tex' || $target eq 'analyze') {
1.22      albertel  914:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.122     albertel  915:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                    916: 	$text='\vskip 5mm $\triangleright$ '.$text;
                    917:     }
1.22      albertel  918:     if ($value ne 'unused') {
                    919:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    920:       &Apache::lonxml::debug("Got a name of :$name:");
1.129     albertel  921:       if ($name eq "") {
1.151     bisitz    922: 	  &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.123     albertel  923: 	  $name=$Apache::lonxml::curdepth;
                    924:       }
1.22      albertel  925:       &Apache::lonxml::debug("Using a name of :$name:");
1.103     albertel  926:       if (defined($Apache::response::foilnames{$name})) {
1.151     bisitz    927: 	  &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  928:       }
1.104     albertel  929:       $Apache::response::foilnames{$name}++;
1.103     albertel  930:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.39      albertel  931:       if ( $Apache::optionresponse::conceptgroup
                    932: 	   && !&Apache::response::showallfoils() ) {
1.22      albertel  933: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    934: 	$Apache::response::conceptgroup{"$name.value"} = $value;
1.122     albertel  935: 	$Apache::response::conceptgroup{"$name.text"} = $text;
1.48      albertel  936: 	$Apache::response::conceptgroup{"$name.location"} = $location;
1.22      albertel  937:       } else {
                    938: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
1.48      albertel  939: 	$Apache::response::foilgroup{"$name.value"} = $value;
1.122     albertel  940: 	$Apache::response::foilgroup{"$name.text"} = $text;
1.48      albertel  941: 	$Apache::response::foilgroup{"$name.location"} = $location;
1.22      albertel  942:       }
                    943:     }
                    944:   }
                    945:   if ($target eq 'edit') {
                    946:     $result.= &Apache::edit::tag_end($target,$token,'');
                    947:   }
                    948:   return $result;
1.1       albertel  949: }
                    950: 
1.112     sakharuk  951: sub start_drawoptionlist {
                    952:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.144     albertel  953:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
1.117     albertel  954: 	return $token->[4];
                    955:     }
1.112     sakharuk  956: }
                    957: 
                    958: sub end_drawoptionlist {
                    959:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.144     albertel  960:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
1.117     albertel  961: 	return $token->[2];
                    962:     }
1.112     sakharuk  963: }
                    964: 
1.7       albertel  965: sub insert_foil {
1.144     albertel  966:     return '
1.15      albertel  967: <foil name="" value="unused">
1.14      albertel  968: <startouttext />
                    969: <endouttext />
1.7       albertel  970: </foil>';
                    971: }
1.144     albertel  972: 
                    973: sub insert_drawoptionlist {
                    974:     return '<drawoptionlist />';
                    975: }
1.177     raeburn   976: 
                    977: sub getnumrows {
                    978:     my ($numitems) = @_;
                    979:     my $bubbles_per_row;
                    980:     my $default_numbubbles = 10;
                    981:     if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
                    982:         ($env{'form.bubbles_per_row'} > 0)) {
                    983:         $bubbles_per_row = $env{'form.bubbles_per_row'};
                    984:     } else {
                    985:         $bubbles_per_row = $default_numbubbles;
                    986:     }
                    987:     my $numrows = int ($numitems/$bubbles_per_row);
                    988:     if (($numitems % $bubbles_per_row) != 0) {
                    989:         $numrows ++;
                    990:     }
                    991:     return ($numrows,$bubbles_per_row);
                    992: }
                    993: 
1.1       albertel  994: 1;
                    995: __END__
                    996:  
1.156     jms       997: =head1 NAME
                    998: 
                    999: Apache::optionresponse.pm;
                   1000: 
                   1001: =head1 SYNOPSIS
                   1002: 
                   1003: Handles tags associated with showing a list of
                   1004: options.
                   1005: 
                   1006: This is part of the LearningOnline Network with CAPA project
                   1007: described at http://www.lon-capa.org.
                   1008: 
                   1009: =head1 HANDLER SUBROUTINE
                   1010: 
                   1011: start_optionresponse()
                   1012: 
                   1013: =head1 OTHER SUBROUTINES
                   1014: 
                   1015: =over
                   1016: 
                   1017: =item end_optionresponse()
                   1018: 
                   1019: =item start_foilgroup()
                   1020: 
                   1021: =item end_foilgroup()
                   1022: 
                   1023: =item getfoilcounts()
                   1024: 
                   1025: =item displayanswers()
                   1026: 
                   1027: =item check_for_invalid()
                   1028: 
                   1029: =item displayfoils()
                   1030: 
                   1031: =item optionlist_correction()
                   1032: 
                   1033: =item webbubbles()
                   1034: 
                   1035: =item bubbles()
                   1036: 
                   1037: =item start_conceptgroup()
                   1038: 
                   1039: =item end_conceptgroup()
                   1040: 
                   1041: =item insert_conceptgroup()
                   1042: 
                   1043: =item start_foil()
                   1044: 
                   1045: =item end_foil()
                   1046: 
                   1047: =item start_drawoptionlist()
                   1048: 
                   1049: =item end_drawoptionlist()
                   1050: 
                   1051: =item insert_foil()
                   1052: 
                   1053: =item insert_drawoptionlist()
                   1054: 
                   1055: =back
                   1056: 
1.157     raeburn  1057: =cut

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