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

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

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