File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.201: download - view: text, annotated - select for diffs
Wed Feb 28 13:20:50 2018 UTC (6 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Eliminate javascript error in composite page conatining optionresponse.
  - Regular expression in lonpage.pm which is used to append prefix argument
    in call to setSubmittedPart() expects string javascript:setSubmittedPart

    1: # LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.201 2018/02/28 13:20:50 raeburn Exp $
    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: #
   28: 
   29: 
   30: 
   31: 
   32: 
   33: package Apache::optionresponse;
   34: use strict;
   35: use Apache::response();
   36: use Apache::lonlocal;
   37: use Apache::lonnet;
   38: 
   39: BEGIN {
   40:   &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
   41: }
   42: 
   43: sub start_optionresponse {
   44:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   45:   my $result='';
   46:   #when in a option response use these
   47:   &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup','drawoptionlist'));
   48:   push (@Apache::lonxml::namespace,'optionresponse');
   49:   my $id = &Apache::response::start_response($parstack,$safeeval);
   50:   %Apache::hint::option=();
   51:   undef(%Apache::response::foilnames);
   52:   if ($target eq 'edit') {
   53:     $result.=&Apache::edit::start_table($token)
   54:        .'<tr><td><span class="LC_nobreak">'.&Apache::loncommon::insert_folding_button().' '
   55:        .&mt('Multiple Option Response Question').'</span> '
   56:        .&Apache::loncommon::help_open_topic('Option_Response_Problems')."</td>"
   57:        .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
   58:        .&Apache::edit::deletelist($target,$token)
   59:        .'</span></td>'
   60:        ."<td>&nbsp;"
   61:        .&Apache::edit::end_row()
   62:        .&Apache::edit::start_spanning_row()
   63:        ."\n";
   64:     $result.=&Apache::edit::text_arg('Max Number Of Shown Foils:','max',
   65: 				     $token,'4').
   66:         &Apache::edit::select_arg('Randomize Foil Order:','randomize',
   67: 				  ['yes','no'],$token).
   68:         &Apache::edit::select_arg('Display of options when printed','TeXlayout',
   69: 				  [['horizontal','Normal list'],
   70: 				   ['vertical','Listed in a vertical column']],$token).
   71:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
   72:   } elsif ($target eq 'modified') {
   73:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   74: 						 $safeeval,'max','randomize',
   75: 						 'TeXlayout');
   76:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   77:   } elsif ($target eq 'meta') {
   78:     $result=&Apache::response::meta_package_write('optionresponse');
   79:   } elsif ($target eq 'analyze') {
   80:     my $part_id="$Apache::inputtags::part.$id";
   81:     $Apache::lonhomework::analyze{"$part_id.type"} = 'optionresponse';
   82:     push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
   83:     push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} }, 1);
   84: 
   85:   }
   86:   return $result;
   87: }
   88: 
   89: sub end_optionresponse {
   90:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   91:   &Apache::response::end_response;
   92:   pop @Apache::lonxml::namespace;
   93:   &Apache::lonxml::deregister('Apache::optionresponse',('foilgroup','foil','conceptgroup','drawoptionlist'));
   94:   my $result;
   95:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   96:   undef(%Apache::response::foilnames);
   97:   return $result;
   98: }
   99: 
  100: %Apache::response::foilgroup=();
  101: sub start_foilgroup {
  102:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  103: 
  104:   my $result='';
  105:   %Apache::response::foilgroup=();
  106:   $Apache::optionresponse::conceptgroup=0;
  107:   &Apache::response::pushrandomnumber(undef,$target);
  108:   if ($target eq 'edit') {
  109:     my $optionlist="<option></option>\n";
  110:     my $option;
  111:     my @opt;
  112:     my @raw_options = &Apache::lonxml::get_param('options', $parstack, $safeeval, 0,  0, 1);
  113: 
  114: 
  115:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval, 0, 0, 1);
  116: 
  117:     my $count=1;
  118:     foreach $option (@opt) {
  119:       $optionlist.="<option value=\"$count\">$option</option>\n";
  120:       $count++;
  121:     }
  122:     my $insertlist=&Apache::edit::insertlist($target,$token);
  123:     $result.=&Apache::edit::start_table($token);
  124:     my %lt = &Apache::lonlocal::texthash(
  125:         sel => 'Select Options',
  126:         add => 'Add new Option:',
  127:         del => 'Delete an Option:',
  128:         maxcheck => 'Enter maximum number students may check (e.g., 3)',
  129:         mincheck => 'Enter minimum number students need to check (e.g., 1)',
  130:     );
  131:     $result.= sprintf(<<ENDTABLE, &Apache::loncommon::insert_folding_button());
  132:       <tr><td>%s $lt{'sel'}</td>
  133:         <td>
  134: 	  $lt{'add'} <input type="text" name="$Apache::lonxml::curdepth.options" />
  135:         </td>
  136:         <td>$lt{'del'}
  137: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
  138:         </td>
  139: ENDTABLE
  140:     $result.= '<td>'.&Apache::edit::checked_arg('Print options:','texoptions',
  141: 					 [ ['nochoice',"Don't show option list"] ]
  142: 					 ,$token).'</td>';
  143:     $result.= '<td><label>'.&mt('Two-option checkboxes for:').
  144:               '<select name="checkboxvalue_'.$Apache::lonxml::curdepth.'">';
  145:     foreach $option (('',@opt)) {
  146:        $result.='<option value="'.$option.'"';
  147:        if ($option eq &Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval)) {
  148:           $result.=' selected="selected"';
  149:        }
  150:        $result.='>'.$option.'</option>';
  151:     }
  152:     $result.='</select></label> ';
  153:     my %checknumtxt = (
  154:                         maxcheck => 'Max. boxes permitted to check',
  155:                         mincheck => 'Min. boxes required to check',
  156:     );
  157:     my (%onclicknum,%checknum);
  158:     foreach my $item ('maxcheck','mincheck') {
  159:         $onclicknum{$item} = 'onclick="'."updateNumber('$item','$Apache::lonxml::curdepth','check','$lt{$item}');".'"';
  160:         $checknum{$item} = &Apache::lonxml::get_param($item,$parstack,$safeeval);
  161:     }
  162:     $result.=&Apache::edit::checked_arg('Checkbox options:','checkboxoptions',
  163:                                          [ ['nochoice',"Don't show option list"] ]
  164:                                          ,$token).' '.
  165:              &Apache::edit::checked_arg('','noprompt',
  166:                                          [ ['nochoice','Omit "Select all that are ... "' ] ]
  167:                                          ,$token);
  168:     foreach my $item ('maxcheck','mincheck') {
  169:         my $currnum = $checknum{$item};
  170:         unless ($currnum =~ /^\d+$/) {
  171:             $currnum = 'nochoice';
  172:         } 
  173:         $result.= ' <span class="LC_nobreak">'.
  174:                   &Apache::edit::checked_arg('',$item,[ [$currnum,$checknumtxt{$item}] ],
  175:                                         $token,$onclicknum{$item},1).' '.
  176:                   '<span id="'.$item.'_numtext_'.$Apache::lonxml::curdepth.'">';
  177:         if ($checknum{$item} ne '') {
  178:             $result .= ':&nbsp;<a href="javascript:updateNumber('."'$item','$Apache::lonxml::curdepth','link','$lt{$item}'".')">'.
  179:                        $checknum{$item}.'</a>';
  180:         }
  181:         $result .= '</span></span>';
  182:     } 
  183:     $result.= '</td>';
  184:     $result.= &Apache::edit::end_row();
  185:     $result.= &Apache::edit::start_spanning_row();
  186:     $result.= $insertlist.'<br />';
  187:   }
  188:   if ($target eq 'modified') {
  189:     my @options;
  190:     my $optchanged=0;
  191: 
  192: 
  193:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval, 0, 0, 1);
  194: 
  195:     if ($env{"form.$Apache::lonxml::curdepth.deleteopt"}) {
  196:       my $delopt=$env{"form.$Apache::lonxml::curdepth.deleteopt"};
  197:       &Apache::lonxml::debug("Deleting :$delopt:");
  198:       splice(@options,$delopt-1,1);
  199:       $optchanged=1;
  200:     }
  201:     if ($env{"form.$Apache::lonxml::curdepth.options"} ne '') {
  202:       my $newopt = $env{"form.$Apache::lonxml::curdepth.options"};
  203:       push(@options,$newopt);
  204:       $optchanged=1;
  205:     }
  206:     my $rebuildtag = &Apache::edit::get_new_args($token,$parstack,$safeeval,
  207: 						 'texoptions','checkboxvalue','checkboxoptions',
  208:                                                  'noprompt','maxcheck','mincheck');
  209:     if ($rebuildtag) {
  210:         my $maxcheck = $token->[2]->{'maxcheck'};
  211:         my $mincheck = $token->[2]->{'mincheck'};
  212:         if ($mincheck =~ /D/) {
  213:             $token->[2]->{'mincheck'}='';
  214:         }
  215:         if ($maxcheck =~ /D/) {
  216:             $token->[2]->{'maxcheck'}='';
  217:         }
  218:         if (($mincheck ne '') && ($maxcheck ne '')) {
  219:             if ($mincheck > $maxcheck) {
  220:                 $token->[2]->{'mincheck'}=$maxcheck; 
  221:             }
  222:         }
  223:     }
  224:     if ($optchanged || $rebuildtag ) {
  225:       $result = "<foilgroup options=\"(";
  226:       foreach my $option (@options) {
  227: 	$option=~s/\'/\\\'/g;
  228: 	&Apache::lonxml::debug("adding option :$option:");
  229: 	$result .="'".$option."',";
  230:       }
  231:       chop $result;
  232:       $result.=')" ';
  233:       $result .= 'texoptions="'.$token->[2]{'texoptions'}.'" ';
  234:       $result .= 'checkboxoptions="'.$token->[2]{'checkboxoptions'}.'" ';
  235:       $result .= 'checkboxvalue="'.$token->[2]{'checkboxvalue'}.'" ';
  236:       $result .= 'noprompt="'.$token->[2]{'noprompt'}.'" ';
  237:       $result .= 'maxcheck="'.$token->[2]{'maxcheck'}.'" ';
  238:       $result .= 'mincheck="'.$token->[2]{'mincheck'}.'"';
  239:       $result .= '>';
  240:     } # else nothing changed so just use the default mechanism
  241:   }
  242:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  243:     if($env{'form.pdfFormFields'} ne 'yes') {
  244:       $result .= ' \begin{itemize} ';
  245:     } else {
  246:       $result .= "\\\\";
  247:     }
  248:   }
  249:   return $result;
  250: }
  251: 
  252: sub end_foilgroup {
  253:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  254:   
  255:   my $result;
  256:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  257:       $target eq 'tex' || $target eq 'analyze') {
  258:     my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
  259:     my $name;
  260:     my @opt;
  261:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  262:     &Apache::lonxml::debug("options:".join(':',@opt));
  263: 
  264:     my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
  265: 					     -2,0);
  266:     my $checkboxvalue=&Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval);
  267:     my $checkboxchoices=(&Apache::lonxml::get_param('checkboxoptions',$parstack,$safeeval) ne 'nochoice');
  268:     my $maxcheck=&Apache::lonxml::get_param('maxcheck', $parstack, $safeeval);
  269:     $maxcheck =~ s/\D+//g;
  270:     my $mincheck=&Apache::lonxml::get_param('mincheck', $parstack, $safeeval);
  271:     $mincheck =~ s/\D+//g;
  272:     my $noprompt=&Apache::lonxml::get_param('noprompt', $parstack, $safeeval);
  273: 
  274:     if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
  275:     &Apache::lonxml::debug("Options are $#opt");
  276:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  277:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  278: 					       $safeeval,'-2');
  279:     if ($target eq 'web' || $target eq 'tex') {
  280:       $result.=&displayfoils($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,$tex_option_switch, $noprompt,$maxcheck,$mincheck, @opt);
  281:       $Apache::lonxml::post_evaluate=0;
  282:     } elsif ( $target eq 'answer') {
  283:       $result.=&displayanswers($max,$randomize,@opt);
  284:       
  285:     } elsif ( $target eq 'analyze') {
  286: 	my @shown = &whichfoils($max,$randomize);
  287: 	&Apache::response::analyze_store_foilgroup(\@shown,
  288: 						  ['text','value','location']);
  289: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  290: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
  291:     } elsif ( $target eq 'grade') {
  292: 
  293:       if ( &Apache::response::submitted()) {
  294: 
  295: 	my @whichopt = &whichfoils($max,$randomize);
  296: 	my $temp=1;my $name;
  297: 	my %responsehash;
  298: 	my %grade;
  299: 	my $right=0;
  300: 	my $wrong=0;
  301: 	my $ignored=0;
  302:         my $checked=0;
  303:         my $corrchkd=0;
  304:         my $corrunchkd=0;
  305:         my $incorrchkd=0;
  306:         my $incorrunchkd=0;
  307:         my ($numrows,$bubbles_per_row);
  308:         if ($Apache::lonhomework::scantronmode) {
  309:             my $numitems = scalar(@opt);
  310:             ($numrows,$bubbles_per_row) =
  311:                 &Apache::optionresponse::getnumrows($numitems);
  312:         }
  313:         if ($numrows < 1) {
  314:             $numrows = 1;
  315:         }
  316:         my $checkboxopt;
  317:         if ($checkboxvalue) {
  318:             $checkboxopt = &check_box_opt($target,$checkboxvalue,@opt);
  319:             if (($checkboxopt) && ($Apache::lonhomework::type !~ /survey/)) {
  320:                 my $corrnumcheck = 0;
  321:                 if (($maxcheck ne '') || ($mincheck ne '')) {
  322:                     foreach $name (@whichopt) {
  323:                         if ($Apache::response::foilgroup{$name.'.value'} eq $checkboxopt) {
  324:                             $corrnumcheck ++; 
  325:                         }
  326:                     }
  327:                     if (($maxcheck ne '') && ($corrnumcheck > $maxcheck)) {
  328:                         $maxcheck = $corrnumcheck;
  329:                     }
  330:                     if (($mincheck ne '') && ($corrnumcheck < $mincheck)) {
  331:                         $mincheck = $corrnumcheck;
  332:                     }
  333:                 }
  334:             }
  335:         }
  336:         my $part=$Apache::inputtags::part;
  337:         my $id = $Apache::inputtags::response['-1'];
  338:         my @relweights;
  339:         my $nonlenient=&grading_is_nonlenient($part,$id,\@relweights);
  340: 	foreach $name (@whichopt) {
  341: 	  my $response;
  342:           if ($env{'form.submitted'} eq 'scantron') {
  343:               if ($numrows > 1) {
  344:                   my $num = $temp;
  345:                   my $totalnum;
  346:                   for (my $i=0; $i<$numrows; $i++) {
  347:                       my $item = &Apache::response::getresponse($num);
  348:                       if ($item =~ /^\d+$/) {
  349:                           $totalnum = $i*$bubbles_per_row + $item;
  350:                       }
  351:                       $num ++;
  352:                   }
  353:                   if ($totalnum =~ /^\d+$/) {
  354:                       $response = $opt[$totalnum];
  355:                   }
  356:                   $temp += $numrows;
  357:               } else {
  358:                   $response=&Apache::response::getresponse($temp);
  359:                   if ($response=~/\S/) {
  360:                       $response = $opt[$response];
  361:                   }
  362:                   $temp ++;
  363:               }
  364: 	  } else {
  365:               $response=&Apache::response::getresponse($temp);
  366:               $temp ++;
  367:           }
  368: 	  if ( $response =~ /[^\s]/) {
  369: 	    $responsehash{$name}=$response;
  370: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  371: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  372: 	    if ($value eq $response) {
  373: 		$grade{$name}='1'; $right++;
  374: 	    } else {
  375: 		$grade{$name}='0'; $wrong++;
  376: 	    }
  377:             if ($checkboxopt) {
  378:                 if ($response eq $checkboxopt) {
  379:                     $checked++;
  380:                     if ($grade{$name}) {
  381:                         $corrchkd ++;
  382:                     } else {
  383:                         $incorrchkd ++;
  384:                     }
  385:                 } else {
  386:                     if ($grade{$name}) {
  387:                         $corrunchkd ++;
  388:                     } else {
  389:                         $incorrunchkd ++;
  390:                     }
  391:                 }
  392:             }
  393: 	  } else {
  394: 	    $ignored++;
  395: 	  }
  396: 	}
  397: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  398: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
  399: 	my %previous=&Apache::response::check_for_previous($responsestr,
  400: 							   $part,$id);
  401: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  402: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  403: 	    $responsestr;
  404: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  405:         if ($Apache::lonhomework::type eq 'randomizetry') {
  406:             $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
  407:         }
  408:         my $skipaward;
  409:         if ($checkboxvalue) {
  410:             if ((($maxcheck ne '') && ($checked > $maxcheck)) ||
  411:                 (($mincheck ne '') && ($checked < $mincheck))) {
  412:                 my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='WRONG_NUMBOXESCHECKED';
  413:                 &Apache::response::handle_previous(\%previous,$ad);
  414:                 $skipaward = 1;
  415:             }
  416:         }
  417:         unless ($skipaward) {
  418: 	    if (($Apache::lonhomework::type eq 'survey') ||
  419:                 ($Apache::lonhomework::type eq 'surveycred') ||
  420:                 ($Apache::lonhomework::type eq 'anonsurvey') ||
  421:                 ($Apache::lonhomework::type eq 'anonsurveycred')) {
  422: 	        if ($ignored == 0) {
  423:                     my $ad;
  424:                     if ($Apache::lonhomework::type eq 'anonsurveycred') {
  425:                         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
  426:                     } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
  427:                         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
  428:                     } elsif ($Apache::lonhomework::type eq 'surveycred') {
  429:                         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
  430:                     } else {
  431: 		        $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  432:                     }
  433: 		    &Apache::response::handle_previous(\%previous,$ad);
  434: 	        } elsif ($wrong==0 && $right==0) {
  435: 	        } else {
  436: 		    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  437: 		    &Apache::response::handle_previous(\%previous,$ad);
  438: 	        }
  439: 	    } elsif ($nonlenient) {
  440: #
  441: # Non-lenient mode. All right or all wrong
  442: #
  443: 	        my $ad;
  444: 	        if ($wrong==0 && $ignored==0) {
  445: 		    $ad='EXACT_ANS';
  446: 	        } elsif ($wrong==0 && $right==0) {
  447: 		    #nothing submitted
  448: 	        } else {
  449: 		    if ($ignored==0) {
  450: 		        $ad='INCORRECT';
  451: 		    } else {
  452: 		        $ad='MISSING_ANSWER';
  453: 		    }
  454: 	        }
  455: 	        $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  456: 	        &Apache::response::handle_previous(\%previous,$ad);
  457: 	     } else {
  458: #
  459: # This is lenient mode
  460: #
  461: 	        my $ad;
  462: 	        if ($wrong==0 && $right==0) {
  463: 		    #nothing submitted only assign a score if we 
  464: 		    #need to override a previous grade
  465: 		    if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
  466: 		        $ad='ASSIGNED_SCORE';
  467: 		    }
  468: 	        } else {
  469: 		    $ad='ASSIGNED_SCORE';
  470: 	        }
  471: 	        $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  472:                 my $awarded;
  473:                 my $totalfoils = $#{ $Apache::response::foilgroup{'names'} }+1;
  474:                 if (($checkboxopt) && ($max >= $totalfoils) && (@relweights > 0)) {
  475:                     if ($right == scalar(@whichopt)) {
  476:                         $awarded = 1;
  477:                     } else {
  478:                         $awarded = ($corrchkd*$relweights[0] + $corrunchkd*$relweights[1] +
  479:                                     $incorrchkd*$relweights[2] + $incorrunchkd*$relweights[3])/(scalar(@whichopt));
  480:                         $awarded = 1 if ($awarded > 1);
  481:                         $awarded = 0 if ($awarded < 0);
  482:                     }
  483:                 } else {
  484:                     $awarded = $right/(scalar(@whichopt));
  485:                 }
  486: 	        $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  487: 		    $awarded;
  488: 	        $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  489: 		    scalar(@whichopt);
  490: 	     }
  491:        }
  492:       }
  493:     }
  494:     my $part_id     = $Apache::inputtags::part;
  495:     my $response_id = $Apache::inputtags::response[-1];
  496:     my ($numrows,$bubbles_per_row);
  497:     if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
  498:         ($numrows,$bubbles_per_row) =
  499:             &Apache::optionresponse::getnumrows(scalar(@opt));
  500:     }
  501:     if ($numrows < 1) {
  502:         $numrows = 1;
  503:     }
  504:     my $increment = &getfoilcounts($max) * $numrows;
  505:     &Apache::lonxml::increment_counter($increment,"$part_id.$response_id");
  506:     if ($target eq 'analyze') {
  507: 	&Apache::lonhomework::set_bubble_lines();
  508:     }
  509: 
  510:   } elsif ($target eq 'edit') {
  511:     $result.=&Apache::edit::end_table();
  512:   }
  513:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  514:     if($env{'form.pdfFormFields'} ne 'yes') {
  515:       $result .= '\end{itemize}'; 
  516:     } else {
  517:       $result .= "\\\\";
  518:     }
  519:   }
  520:   &Apache::response::poprandomnumber();
  521:   return $result;
  522: }
  523: 
  524: sub grading_is_nonlenient {
  525:     my ($part,$id,$relweights) = @_;
  526: # Web mode: we are non-lenient unless told otherwise
  527:     my $defaultparm = 'off';
  528:     my $nonlenient = 1;
  529: # Grading a bubblesheet exam: we are grading lenient unless told otherwise
  530:     if ($Apache::lonhomework::scantronmode) {
  531:         $defaultparm = 'on';
  532:         $nonlenient = 0;
  533:     }
  534:     my $lenientparm =
  535:         &Apache::response::get_response_param($part,'lenient',$defaultparm);
  536:     if ($lenientparm eq 'default') {
  537:         $lenientparm = $defaultparm;
  538:     }
  539:     $lenientparm =
  540:         &Apache::response::get_response_param($part.'_'.$id,'lenient',$lenientparm);
  541:     if ($lenientparm eq 'default') {
  542:         $lenientparm = $defaultparm;
  543:     }
  544:     if ($lenientparm=~/^(?:0|off|no)$/i) {
  545:         $nonlenient = 1;
  546:     } elsif ($lenientparm=~/^(?:1|on|yes)$/i) {
  547:         $nonlenient = 0;
  548:     } elsif ($lenientparm =~ /^\-?[.\d]+,\-?[.\d]+,\-?[.\d]+,\-?[.\d]+$/) {
  549:         if (ref($relweights) eq 'ARRAY') {
  550:             @{$relweights} = split(/,/,$lenientparm);
  551:         }
  552:         $nonlenient = 0;
  553:     }
  554:     if (!$nonlenient) {
  555:         $Apache::inputtags::leniency=1;
  556:     }
  557:     return $nonlenient;
  558: }
  559: 
  560: sub getfoilcounts {
  561:   my ($max)=@_;
  562:   # +1 since instructors will count from 1
  563:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  564:   if (&Apache::response::showallfoils()) { $max=$count; }
  565:   if ($count>$max) { $count=$max } 
  566:   &Apache::lonxml::debug("Count is $count from $max");
  567:   return $count;
  568: }
  569: 
  570: sub whichfoils {
  571:     my ($max,$randomize)=@_;
  572:     return &Apache::response::whichorder($max,$randomize,
  573: 					 &Apache::response::showallfoils(),
  574: 					 \%Apache::response::foilgroup);
  575: }
  576: 
  577: sub displayanswers {
  578:     my ($max,$randomize,@opt)=@_;
  579:     my @names;
  580:     if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
  581:         @names = @{ $Apache::response::foilgroup{'names'} };
  582:     }
  583:     return if (!@names);
  584:     my @whichopt = &whichfoils($max,$randomize);
  585:     my $result;
  586:     if ($Apache::lonhomework::type eq 'exam') {
  587: 	my $i = 0;
  588: 	my %opt = map { ($_,$i++) } @opt;
  589: 	
  590: 	$i = 0;
  591: 	my @alphabet = ('A'..'Z');
  592: 	foreach my $name (@whichopt) {
  593: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
  594: 	    $result.=&Apache::response::answer_part('optionresponse',
  595: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
  596: 	    $result.=&Apache::response::answer_part('optionresponse',
  597: 						    $Apache::response::foilgroup{$name.'.value'});
  598: 	    $result.=&Apache::response::answer_footer('optionresponse');
  599: 	}
  600:     } else {
  601: 	$result=&Apache::response::answer_header('optionresponse');
  602: 	foreach my $name (@whichopt) {
  603: 	    $result.=&Apache::response::answer_part('optionresponse',
  604: 						    $Apache::response::foilgroup{$name.'.value'});
  605: 	}
  606: 	$result.=&Apache::response::answer_footer('optionresponse');
  607:     }
  608:     return $result;
  609: }
  610: 
  611: sub check_box_opt {
  612:     my ($target,$checkboxvalue,@opt)=@_;
  613: # Check if we are in checkbox mode: checkboxvalue specified, on web, only two options.
  614: # If so, return "checked" value
  615:     if ($#opt!=1) { return ''; }
  616:     unless (($target eq 'web') || ($target eq 'grade')) { return ''; }
  617:     return $checkboxvalue;
  618: }
  619: 
  620: sub check_for_invalid {
  621:     my ($names,$options) = @_;
  622:     my %bad_names;
  623:     foreach my $name (@{ $names }) {
  624: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  625: 	my $found=0;
  626: 	foreach my $option (@{ $options }) {
  627: 	    if ($value eq $option) { $found=1; }
  628: 	}
  629: 	if (!$found) { $bad_names{$name}=$value; }
  630:     }
  631:     if (%bad_names) {
  632: 	my $error=&mt('The question can not be gotten correct, '.
  633: 	    'the following foils in the &lt;optionresponse&gt; '.
  634: 	    'have invalid correct options').' <br /><tt>'.
  635: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
  636: 	    "</tt>";
  637: 	&Apache::lonxml::error($error);
  638:     }
  639: }
  640: 
  641: sub displayfoils {
  642:   my ($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,
  643:       $tex_option_switch, $no_tfprompt, $maxcheck, $mincheck, @opt)=@_;
  644:   my @names; 
  645:   if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
  646:       @names = @{ $Apache::response::foilgroup{'names'} };
  647:   }
  648:   unless (@names > 0) { return;}
  649:   my @truelist;
  650:   my @falselist;
  651:   my $result;  
  652:   my $name;
  653:   my $displayoptionintex=1;
  654:   my @alphabet = ('A'..'Z');
  655:   my @whichopt = &whichfoils($max,$randomize);
  656:   unless (($target eq 'tex') && ($tex_option_switch eq 'nochoice')) {
  657:       &check_for_invalid(\@whichopt,\@opt);
  658:   }
  659:   my $part=$Apache::inputtags::part;
  660:   my $id=$Apache::inputtags::response[-1];
  661:   my $break;
  662:   if ( ($target ne 'tex') &&
  663:        &Apache::response::show_answer() ) {
  664:     my $temp=1;
  665:     foreach $name (@whichopt) {
  666: 	my $text=$Apache::response::foilgroup{$name.'.text'};
  667:         my $lastresp;
  668:         if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
  669:             (($env{'form.grade_username'} eq $env{'user.name'}) &&
  670:              ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
  671:             $lastresp = $Apache::lonhomework::history{"resource.$part.$id.submission"};
  672:         } else {
  673:             unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
  674:                     ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
  675:                 $lastresp = $Apache::lonhomework::history{"resource.$part.$id.submission"};
  676:             }
  677:         }
  678: 	my %lastresponse=&Apache::lonnet::str2hash($lastresp);
  679: 	my $lastopt=$lastresponse{$name};
  680: 	if ($text!~/^\s*$/) { $break='<br />'; }
  681: 	$result.=$break;
  682: 	if ($target eq 'web') {
  683: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  684: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
  685: 		if ($text=~/^\s*$/) {
  686: 		    $text=$value.$text;
  687: 		} else {
  688: 		    $text='<b>'.$value.':</b> '.$text;
  689: 		}
  690: 	    } else {
  691:                 if (@whichopt > 1) {
  692:                     $text='&#149;'.$text;
  693:                 }
  694: 	    }
  695: 	    $result.=$text."\n";
  696: 	}
  697:       if ($Apache::lonhomework::type eq 'exam') {
  698: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  699:       }
  700:       $temp++;
  701:     }
  702:   } else {
  703:     my $temp=1;
  704:     my %lastresponse;
  705:     my $newvariation;
  706:     if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  707:         ($Apache::lonhomework::type eq 'randomizetry')) &&
  708:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  709:         if ($env{'form.'.$part.'.rndseed'} ne
  710:             $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  711:             $newvariation = 1;
  712:         }
  713:     }
  714:     unless ($newvariation) {
  715:         if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
  716:             (($env{'form.grade_username'} eq $env{'user.name'}) &&
  717:              ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
  718:                  %lastresponse =
  719:                      &Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  720:         } else {
  721:             unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
  722:                     ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
  723:                 %lastresponse =
  724:                     &Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  725:             }
  726:         }
  727:     }
  728:     my $internal_counter=$Apache::lonxml::counter;
  729:     my $checkboxopt;
  730:     if ($target eq 'web') {
  731:         $checkboxopt=&check_box_opt($target,$checkboxvalue,@opt);
  732:     }
  733:     if ($checkboxopt && (!$no_tfprompt)) {
  734:        $result.='<br />'.
  735:                 ($checkboxchoices?&mt('Choices: ').'<b>'.$opt[0].','.$opt[1].'</b>. ':'').
  736:                  &mt('Select all that are [_1].','<b>'.$checkboxopt.'</b>');
  737:     }
  738:     foreach $name (@whichopt) {
  739:       my $text=$Apache::response::foilgroup{$name.'.text'};
  740:       if ($text!~/^\s*$/) {
  741: 	  if ($target eq 'tex') {
  742: 	      $break='\vskip 0 mm ';
  743: 	  } elsif ($target eq 'web') {
  744: 	      $break='<br />';
  745: 	  }
  746:       }
  747:       my $lastopt=$lastresponse{$name};
  748:       my $optionlist="<option></option>\n";
  749: 
  750:       if($target eq 'tex' and $env{'form.pdfFormFields'} eq 'yes'
  751:          && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  752:           my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&optionresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
  753:           $optionlist =  &Apache::lonxml::print_pdf_start_combobox($fieldname);
  754:       }
  755: 
  756:       foreach my $option (@opt) {
  757: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
  758:           if ($option eq $lastopt) {
  759:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  760:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  761:                   && $Apache::lonhomework::type ne 'exam') {
  762:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  763:               } else {
  764:                   $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
  765:               }
  766:           } else {
  767:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  768:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  769:                   && $Apache::lonhomework::type ne 'exam') {
  770:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  771:               } else {
  772:                   $optionlist.="<option value='".$escopt."'>$option</option>\n";
  773:               }
  774:           }
  775:       }
  776:       if ($target ne 'tex') {
  777: 	  if ($Apache::lonhomework::type ne 'exam') {
  778: # we are on the web, this is not an exam, and the problem can be answered
  779:               if ($checkboxopt) {
  780: # generate checkboxes
  781:                   my $fieldname=$Apache::inputtags::response['-1'].':'.$temp;
  782:                   my $altopt=$opt[0];
  783:                   if ($opt[0] eq $checkboxopt) {
  784:                      $altopt=$opt[1];
  785:                   }
  786:                   my $defopt=$lastopt;
  787:                   unless ($defopt) { $defopt=$altopt; }
  788:                   my $escdefopt=&HTML::Entities::encode($defopt,'\'"&<>');
  789:                   my $esccheckboxopt=&HTML::Entities::encode($checkboxopt,'\'"&<>');
  790:                   my $escaltopt=&HTML::Entities::encode($altopt,'\'"&<>');
  791: # checkboxopt is how the box is labelled
  792: # altopt is the alternative option
  793: # lastopt is what the user submitted before
  794: # defopt is what the field is going to start out with: either previous choice or altopt
  795: # fieldname is this input field's name after HWVAL_
  796:                   $optionlist='<input type="hidden" name="HWVAL_'.$fieldname.'" value="'.$escdefopt.'" />'.
  797:                   '<input type="checkbox" name="HWCHK_'.$fieldname.'" onclick="javascript:if (this.form.elements[\'HWCHK_'.
  798:                   $fieldname.'\'].checked) { this.form.elements[\'HWVAL_'.$fieldname.'\'].value=\''.$esccheckboxopt.'\'; } else { this.form.elements[\'HWVAL_'.$fieldname.'\'].value=\''.$escaltopt.'\'; };javascript:setSubmittedPart(\''.$part.'\');"'.($defopt eq $checkboxopt?' checked="checked"':'')." />\n";
  799:               } else {
  800: # classic selection list
  801: 	          $optionlist='<select onchange="javascript:setSubmittedPart(\''.
  802: 		  $part.'\');" name="HWVAL_'.
  803: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
  804: 		  $optionlist."</select>\n";
  805:               }
  806: 	  } else {
  807: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
  808: 	  }
  809: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
  810: 	      if ($Apache::lonhomework::type ne 'exam') {
  811:                   if (@whichopt > 1) {  
  812:                       $text='&#149;'.$text;
  813:                   }
  814: 	      }
  815: 	  } else {
  816: 	      if ($Apache::lonhomework::type ne 'exam') {
  817: 		  $text=$optionlist.$text;
  818: 	      }
  819: 	  }
  820: 	  $result.=$break.$text."\n";
  821: 	  if ($Apache::lonhomework::type eq 'exam') {
  822: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  823: 	  }
  824: 	  $temp++;
  825:       } else {
  826: 	  my $texoptionlist='';
  827: 	  if ($displayoptionintex &&
  828: 	      $Apache::lonhomework::type ne 'exam') {
  829: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
  830: 	  }
  831: 	  if ($text=~/<drawoptionlist\s*\/>/) {
  832: 	      $text=~s|<drawoptionlist\s*\/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |g;
  833: 	  }
  834: 
  835: 	  if ($text=~m/\\item /) {
  836: 	      if ($Apache::lonhomework::type eq 'exam') {
  837: 	          $text=~s/\\item/\\vskip 2 mm/;
  838: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
  839:                   $result.= $texoptionlist.$text;
  840:               }
  841:           } else {
  842: 	      if ($Apache::lonhomework::type eq 'exam') {
  843: 		  $result.= $texoptionlist.'  '.$text;
  844: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
  845: 		  if ($text=~/\S/) {
  846:                       $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;
  847:                   } else {
  848:                       $result.= $texoptionlist;
  849:                   }
  850: 	      }
  851: 	  }
  852: 	  if ($Apache::lonhomework::type eq 'exam') {
  853: 	      $result.='\vskip -1 mm\noindent';
  854:               my ($numrows,$bubbles_per_row) = &getnumrows(scalar(@opt)); 
  855:               if ($numrows == 1) {  
  856:                   $result .= '\textbf{'.$internal_counter.'}. \vskip -3mm';
  857:               } else {
  858:                   my $linetext;
  859:                   for (my $i=0; $i<$numrows; $i++) {
  860:                       $linetext .= $internal_counter+$i.', ';
  861:                   }
  862:                   $linetext =~ s/,\s$//;
  863:                   $result .= '\small {\textbf{'.$linetext.'}} '.
  864:                              '\hskip 2 mm {\footnotesize '.
  865:                              &mt('(Bubble once in [_1] lines)',$numrows).
  866:                              '} \vskip 1 mm';
  867:               }
  868:               $result.= &bubbles(\@alphabet,\@opt,undef,undef,$numrows,
  869:                                  $bubbles_per_row,$internal_counter).
  870:                         ' \strut ';
  871: 	      $internal_counter += $numrows;
  872: 	  }
  873:           if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  874:               && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  875:               && $Apache::lonhomework::type ne 'exam') {
  876:               $text =~ s/\\item//m;
  877:               $result .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
  878:               $temp++;
  879:           }
  880: 	  $displayoptionintex=0;
  881:       }
  882:     }
  883:   }
  884: 
  885:   if ($target eq 'web') {
  886:       my $data = [\@whichopt,'submissiongrading'];
  887:       my $questiontype;
  888:       if ($Apache::lonhomework::type eq 'randomizetry') {
  889:           $questiontype = $Apache::lonhomework::type,
  890:       }
  891:       &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,$data,$questiontype);
  892:   }
  893: 
  894:   if ($target ne 'tex') {
  895:       return $result.$break;
  896:   } else {
  897:       return $result;
  898:   }
  899: }
  900: 
  901: 
  902: sub optionlist_correction {
  903:     my ($TeXlayout,@options) = @_;
  904:     my $texoptionlist='\\item [] '.&mt('Choices:').' ';
  905:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
  906:     if (scalar(@options) > 0) {
  907: 	foreach my $option (@options) {
  908: 	    $texoptionlist.='{\bf '.
  909: 		&Apache::lonxml::latex_special_symbols($option).
  910: 		'}';
  911: 	    if ($TeXlayout eq 'vertical') {
  912: 		$texoptionlist.=' \vskip 0 mm ',
  913: 	    } else {
  914: 		$texoptionlist.=', ';
  915: 	    }
  916: 	}
  917: 	$texoptionlist=~s/, $//;
  918: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
  919:     } else {
  920: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
  921:     }
  922:     return $texoptionlist;
  923: }
  924: 
  925: 
  926: sub webbubbles {
  927:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
  928:     my @opt=@$ropt; 
  929:     my @alphabet=@$ralphabet;
  930:     my $result='';
  931:     my $number_of_bubbles = $#opt + 1;
  932:     $result.= '<table border="1"><tr>';
  933:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  934: 	my $checked='';
  935: 	if ($lastopt eq $opt[$ind]) {
  936: 	    $checked=' checked="on" ';
  937: 	}
  938: 	$result.='<td><input type="radio" name="HWVAL_'.
  939: 	    $Apache::inputtags::response['-1'].':'.$temp.
  940: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
  941: 	if ($alphabet[$ind]) {
  942: 	    $result.=$alphabet[$ind].': ';
  943: 	}
  944: 	$result.=$opt[$ind].'</td>';
  945:     }
  946:     $result.='</tr></table>';
  947:     return $result;
  948: }
  949: 
  950: 
  951: sub bubbles {
  952:     my ($ralphabet,$ropt,$response,$max_width,$numrows,$bubbles_per_row,
  953:         $internal_counter) = @_;
  954:     my @alphabet = @$ralphabet;
  955:     my @opt = @$ropt;
  956:     my ($result,$head,$line) =('','','');
  957:     my $number_of_bubbles = $#opt + 1;
  958:     my $current_length = 0;
  959:     my $textwidth;
  960:     if (defined($max_width)) {
  961: 	$textwidth=$max_width;
  962: 	&Apache::lonxml::debug("Max width passed in: $max_width");
  963:     } elsif ($env{'form.textwidth'} ne '') {
  964: 	$env{'form.textwidth'}=~/(\d+)/;
  965: 	$textwidth=$1;
  966: 	&Apache::lonxml::debug("Max width from form: $textwidth");
  967:     } else {
  968: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
  969: 	$textwidth=$1.'.'.$2;
  970: 	&Apache::lonxml::debug("Max width defaults? $textwidth");
  971:     }
  972:     &Apache::lonxml::debug("Final maxwidth: $textwidth");
  973:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
  974:         my $item;
  975:         if ($numrows > 1) {
  976:             my $num = $internal_counter+int($ind/$bubbles_per_row);
  977:             my $idx = int($ind % $bubbles_per_row);
  978:             $item = $num.$alphabet[$idx];
  979:         } else {
  980:             $item = $alphabet[$ind];
  981:         }
  982: 	my $leftmargin;
  983: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
  984: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
  985: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
  986: 
  987: 	$current_length += (length($opt[$ind])+length($item)+5)*2;
  988:         
  989: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
  990:             
  991: 	    $line.='\hskip 4 mm {\small \textbf{'.$item.'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
  992: 	    $head.='lr';
  993: 	} else {
  994: 	    $line=~s/\&\s*$//;
  995: 	    $result.='\vskip -1 mm\noindent\setlength{\tabcolsep}{2 mm}\renewcommand{\arraystretch}{1.25}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
  996: 	    $line = '\hskip 4 mm {\small \textbf{'.$item.'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
  997: 	    $head ='lr';
  998: 	    $current_length = (length($opt[$ind])+length($item))*2;
  999: 	}
 1000: 
 1001:     }
 1002:     return $result;
 1003: }
 1004: 
 1005: sub start_conceptgroup {
 1006:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1007:   $Apache::optionresponse::conceptgroup=1;
 1008:   %Apache::response::conceptgroup=();
 1009:   my $result;
 1010:   if ($target eq 'edit') {
 1011:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
 1012:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
 1013:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
 1014:   }
 1015:   if ($target eq 'modified') {
 1016:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
 1017:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 1018:   }
 1019:   return $result;
 1020: }
 1021: 
 1022: sub end_conceptgroup {
 1023:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1024:   $Apache::optionresponse::conceptgroup=0;
 1025:   my $result='';
 1026:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
 1027:       $target eq 'tex' || $target eq 'analyze') {
 1028:     #if not there aren't any foils to display and thus no question
 1029:       &Apache::response::pick_foil_for_concept($target,
 1030: 					       ['value','text','location'],
 1031: 					       \%Apache::hint::option,
 1032: 					       $parstack,$safeeval);
 1033:   } elsif ($target eq 'edit') {
 1034:     $result=&Apache::edit::end_table();
 1035:   }
 1036:   return $result;
 1037: }
 1038: 
 1039: sub insert_conceptgroup {
 1040:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
 1041:   return $result;
 1042: }
 1043: 
 1044: sub start_foil {
 1045:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1046:   my $result='';
 1047:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
 1048:       &Apache::lonxml::startredirection;
 1049:       if ($target eq 'analyze') {
 1050: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
 1051:       }
 1052:   } elsif ($target eq 'edit') {
 1053:     $result=&Apache::edit::tag_start($target,$token,"Foil");
 1054:     my $level='-2';
 1055:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
 1056:     my @opt;
 1057:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
 1058:     $result.=&Apache::edit::text_arg('Name:','name',$token);
 1059:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
 1060: 					       ['unused',(@opt)],$token,'15');
 1061:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
 1062: 					     $safeeval,'-3');
 1063:     if ($randomize ne 'no') {
 1064: 	$result.=&Apache::edit::select_arg('Location:','location',
 1065: 					   ['random','top','bottom'],$token);
 1066:     }
 1067:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 1068:   } elsif ($target eq 'modified') {
 1069:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
 1070: 						 'value','name','location');
 1071:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 1072:   } 
 1073:   return $result;
 1074: }
 1075: 
 1076: sub end_foil {
 1077:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1078:   my $text ='';
 1079:   my $result = '';
 1080:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
 1081:       $text=&Apache::lonxml::endredirection;
 1082:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
 1083:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
 1084:   }
 1085:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
 1086:       || $target eq 'tex' || $target eq 'analyze') {
 1087:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
 1088:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
 1089: 	$text='\vskip 5mm $\triangleright$ '.$text;
 1090:     }
 1091:     if ($value ne 'unused') {
 1092:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
 1093:       &Apache::lonxml::debug("Got a name of :$name:");
 1094:       if ($name eq "") {
 1095: 	  &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
 1096: 	  $name=$Apache::lonxml::curdepth;
 1097:       }
 1098:       &Apache::lonxml::debug("Using a name of :$name:");
 1099:       if (defined($Apache::response::foilnames{$name})) {
 1100: 	  &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
 1101:       }
 1102:       $Apache::response::foilnames{$name}++;
 1103:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
 1104:       if ( $Apache::optionresponse::conceptgroup
 1105: 	   && !&Apache::response::showallfoils() ) {
 1106: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
 1107: 	$Apache::response::conceptgroup{"$name.value"} = $value;
 1108: 	$Apache::response::conceptgroup{"$name.text"} = $text;
 1109: 	$Apache::response::conceptgroup{"$name.location"} = $location;
 1110:       } else {
 1111: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
 1112: 	$Apache::response::foilgroup{"$name.value"} = $value;
 1113: 	$Apache::response::foilgroup{"$name.text"} = $text;
 1114: 	$Apache::response::foilgroup{"$name.location"} = $location;
 1115:       }
 1116:     }
 1117:   }
 1118:   if ($target eq 'edit') {
 1119:     $result.= &Apache::edit::tag_end($target,$token,'');
 1120:   }
 1121:   return $result;
 1122: }
 1123: 
 1124: sub start_drawoptionlist {
 1125:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1126:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
 1127: 	return $token->[4];
 1128:     }
 1129: }
 1130: 
 1131: sub end_drawoptionlist {
 1132:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1133:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
 1134: 	return $token->[2];
 1135:     }
 1136: }
 1137: 
 1138: sub insert_foil {
 1139:     return '
 1140: <foil name="" value="unused">
 1141: <startouttext />
 1142: <endouttext />
 1143: </foil>';
 1144: }
 1145: 
 1146: sub insert_drawoptionlist {
 1147:     return '<drawoptionlist />';
 1148: }
 1149: 
 1150: sub getnumrows {
 1151:     my ($numitems) = @_;
 1152:     my $bubbles_per_row;
 1153:     my $default_numbubbles = 10;
 1154:     if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
 1155:         ($env{'form.bubbles_per_row'} > 0)) {
 1156:         $bubbles_per_row = $env{'form.bubbles_per_row'};
 1157:     } else {
 1158:         $bubbles_per_row = $default_numbubbles;
 1159:     }
 1160:     my $numrows = int ($numitems/$bubbles_per_row);
 1161:     if (($numitems % $bubbles_per_row) != 0) {
 1162:         $numrows ++;
 1163:     }
 1164:     return ($numrows,$bubbles_per_row);
 1165: }
 1166: 
 1167: 1;
 1168: __END__
 1169:  
 1170: =head1 NAME
 1171: 
 1172: Apache::optionresponse.pm;
 1173: 
 1174: =head1 SYNOPSIS
 1175: 
 1176: Handles tags associated with showing a list of
 1177: options.
 1178: 
 1179: This is part of the LearningOnline Network with CAPA project
 1180: described at http://www.lon-capa.org.
 1181: 
 1182: =head1 HANDLER SUBROUTINE
 1183: 
 1184: start_optionresponse()
 1185: 
 1186: =head1 OTHER SUBROUTINES
 1187: 
 1188: =over
 1189: 
 1190: =item end_optionresponse()
 1191: 
 1192: =item start_foilgroup()
 1193: 
 1194: =item end_foilgroup()
 1195: 
 1196: =item getfoilcounts()
 1197: 
 1198: =item displayanswers()
 1199: 
 1200: =item check_for_invalid()
 1201: 
 1202: =item displayfoils()
 1203: 
 1204: =item optionlist_correction()
 1205: 
 1206: =item webbubbles()
 1207: 
 1208: =item bubbles()
 1209: 
 1210: =item start_conceptgroup()
 1211: 
 1212: =item end_conceptgroup()
 1213: 
 1214: =item insert_conceptgroup()
 1215: 
 1216: =item start_foil()
 1217: 
 1218: =item end_foil()
 1219: 
 1220: =item start_drawoptionlist()
 1221: 
 1222: =item end_drawoptionlist()
 1223: 
 1224: =item insert_foil()
 1225: 
 1226: =item insert_drawoptionlist()
 1227: 
 1228: =back
 1229: 
 1230: =cut

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