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

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

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