File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.195.2.3: download - view: text, annotated - select for diffs
Thu Mar 1 13:11:47 2018 UTC (6 years, 2 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu
- For 2.11
  - Backport 1.201

    1: # LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.195.2.3 2018/03/01 13:11:47 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 ($numrows,$bubbles_per_row);
  304:         if ($Apache::lonhomework::scantronmode) {
  305:             my $numitems = scalar(@opt);
  306:             ($numrows,$bubbles_per_row) =
  307:                 &Apache::optionresponse::getnumrows($numitems);
  308:         }
  309:         if ($numrows < 1) {
  310:             $numrows = 1;
  311:         }
  312:         my $checkboxopt;
  313:         if ($checkboxvalue) {
  314:             $checkboxopt = &check_box_opt($target,$checkboxvalue,@opt);
  315:             if (($checkboxopt) && ($Apache::lonhomework::type !~ /survey/)) {
  316:                 my $corrnumcheck = 0;
  317:                 if (($maxcheck ne '') || ($mincheck ne '')) {
  318:                     foreach $name (@whichopt) {
  319:                         if ($Apache::response::foilgroup{$name.'.value'} eq $checkboxopt) {
  320:                             $corrnumcheck ++; 
  321:                         }
  322:                     }
  323:                     if (($maxcheck ne '') && ($corrnumcheck > $maxcheck)) {
  324:                         $maxcheck = $corrnumcheck;
  325:                     }
  326:                     if (($mincheck ne '') && ($corrnumcheck < $mincheck)) {
  327:                         $mincheck = $corrnumcheck;
  328:                     }
  329:                 }
  330:             }
  331:         }
  332: 	foreach $name (@whichopt) {
  333: 	  my $response;
  334:           if ($env{'form.submitted'} eq 'scantron') {
  335:               if ($numrows > 1) {
  336:                   my $num = $temp;
  337:                   my $totalnum;
  338:                   for (my $i=0; $i<$numrows; $i++) {
  339:                       my $item = &Apache::response::getresponse($num);
  340:                       if ($item =~ /^\d+$/) {
  341:                           $totalnum = $i*$bubbles_per_row + $item;
  342:                       }
  343:                       $num ++;
  344:                   }
  345:                   if ($totalnum =~ /^\d+$/) {
  346:                       $response = $opt[$totalnum];
  347:                   }
  348:                   $temp += $numrows;
  349:               } else {
  350:                   $response=&Apache::response::getresponse($temp);
  351:                   if ($response=~/\S/) {
  352:                       $response = $opt[$response];
  353:                   }
  354:                   $temp ++;
  355:               }
  356: 	  } else {
  357:               $response=&Apache::response::getresponse($temp);
  358:               $temp ++;
  359:           }
  360: 	  if ( $response =~ /[^\s]/) {
  361: 	    $responsehash{$name}=$response;
  362: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  363: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  364: 	    if ($value eq $response) {
  365: 		$grade{$name}='1'; $right++;
  366: 	    } else {
  367: 		$grade{$name}='0'; $wrong++;
  368: 	    }
  369:             if ($checkboxopt) {
  370:                 if ($response eq $checkboxopt) {
  371:                     $checked++;
  372:                 }
  373:             } 
  374: 	  } else {
  375: 	    $ignored++;
  376: 	  }
  377: 	}
  378: 	my $part=$Apache::inputtags::part;
  379:         my $id = $Apache::inputtags::response['-1'];
  380:         my $nonlenient=&grading_is_nonlenient($part,$id);
  381: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  382: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
  383: 	my %previous=&Apache::response::check_for_previous($responsestr,
  384: 							   $part,$id);
  385: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  386: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  387: 	    $responsestr;
  388: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  389:         if ($Apache::lonhomework::type eq 'randomizetry') {
  390:             $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
  391:         }
  392:         my $skipaward;
  393:         if ($checkboxvalue) {
  394:             if ((($maxcheck ne '') && ($checked > $maxcheck)) ||
  395:                 (($mincheck ne '') && ($checked < $mincheck))) {
  396:                 my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='WRONG_NUMBOXESCHECKED';
  397:                 &Apache::response::handle_previous(\%previous,$ad);
  398:                 $skipaward = 1;
  399:             }
  400:         }
  401:         unless ($skipaward) {
  402: 	    if (($Apache::lonhomework::type eq 'survey') ||
  403:                 ($Apache::lonhomework::type eq 'surveycred') ||
  404:                 ($Apache::lonhomework::type eq 'anonsurvey') ||
  405:                 ($Apache::lonhomework::type eq 'anonsurveycred')) {
  406: 	        if ($ignored == 0) {
  407:                     my $ad;
  408:                     if ($Apache::lonhomework::type eq 'anonsurveycred') {
  409:                         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
  410:                     } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
  411:                         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
  412:                     } elsif ($Apache::lonhomework::type eq 'surveycred') {
  413:                         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
  414:                     } else {
  415: 		        $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  416:                     }
  417: 		    &Apache::response::handle_previous(\%previous,$ad);
  418: 	        } elsif ($wrong==0 && $right==0) {
  419: 	        } else {
  420: 		    my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  421: 		    &Apache::response::handle_previous(\%previous,$ad);
  422: 	        }
  423: 	    } elsif ($nonlenient) {
  424: #
  425: # Non-lenient mode. All right or all wrong
  426: #
  427: 	        my $ad;
  428: 	        if ($wrong==0 && $ignored==0) {
  429: 		    $ad='EXACT_ANS';
  430: 	        } elsif ($wrong==0 && $right==0) {
  431: 		    #nothing submitted
  432: 	        } else {
  433: 		    if ($ignored==0) {
  434: 		        $ad='INCORRECT';
  435: 		    } else {
  436: 		        $ad='MISSING_ANSWER';
  437: 		    }
  438: 	        }
  439: 	        $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  440: 	        &Apache::response::handle_previous(\%previous,$ad);
  441: 	     } else {
  442: #
  443: # This is lenient mode
  444: #
  445: 	        my $ad;
  446: 	        if ($wrong==0 && $right==0) {
  447: 		    #nothing submitted only assign a score if we 
  448: 		    #need to override a previous grade
  449: 		    if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
  450: 		        $ad='ASSIGNED_SCORE';
  451: 		    }
  452: 	        } else {
  453: 		    $ad='ASSIGNED_SCORE';
  454: 	        }
  455: 	        $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  456: 	        $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  457: 		    $right/(scalar(@whichopt));
  458: 	        $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  459: 		    scalar(@whichopt);
  460: 	     }
  461:        }
  462:       }
  463:     }
  464:     my $part_id     = $Apache::inputtags::part;
  465:     my $response_id = $Apache::inputtags::response[-1];
  466:     my ($numrows,$bubbles_per_row);
  467:     if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
  468:         ($numrows,$bubbles_per_row) =
  469:             &Apache::optionresponse::getnumrows(scalar(@opt));
  470:     }
  471:     if ($numrows < 1) {
  472:         $numrows = 1;
  473:     }
  474:     my $increment = &getfoilcounts($max) * $numrows;
  475:     &Apache::lonxml::increment_counter($increment,"$part_id.$response_id");
  476:     if ($target eq 'analyze') {
  477: 	&Apache::lonhomework::set_bubble_lines();
  478:     }
  479: 
  480:   } elsif ($target eq 'edit') {
  481:     $result.=&Apache::edit::end_table();
  482:   }
  483:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  484:     if($env{'form.pdfFormFields'} ne 'yes') {
  485:       $result .= '\end{itemize}'; 
  486:     } else {
  487:       $result .= "\\\\";
  488:     }
  489:   }
  490:   &Apache::response::poprandomnumber();
  491:   return $result;
  492: }
  493: 
  494: sub grading_is_nonlenient {
  495:     my ($part,$id) = @_;
  496: # Web mode: we are non-lenient unless told otherwise
  497:     my $defaultparm = 'off';
  498:     my $nonlenient = 1;
  499: # Grading a bubblesheet exam: we are grading lenient unless told otherwise
  500:     if ($Apache::lonhomework::scantronmode) {
  501:         $defaultparm = 'on';
  502:         $nonlenient = 0;
  503:     }
  504:     my $lenientparm =
  505:         &Apache::response::get_response_param($part,'lenient',$defaultparm);
  506:     if ($lenientparm eq 'default') {
  507:         $lenientparm = $defaultparm;
  508:     }
  509:     $lenientparm =
  510:         &Apache::response::get_response_param($part.'_'.$id,'lenient',$lenientparm);
  511:     if ($lenientparm eq 'default') {
  512:         $lenientparm = $defaultparm;
  513:     }
  514:     if ($lenientparm=~/^0|off|no$/i) {
  515:         $nonlenient = 1;
  516:     } elsif ($lenientparm=~/^1|on|yes$/i) {
  517:         $nonlenient = 0;
  518:     }
  519:     if (!$nonlenient) {
  520:         $Apache::inputtags::leniency=1;
  521:     }
  522:     return $nonlenient;
  523: }
  524: 
  525: sub getfoilcounts {
  526:   my ($max)=@_;
  527:   # +1 since instructors will count from 1
  528:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  529:   if (&Apache::response::showallfoils()) { $max=$count; }
  530:   if ($count>$max) { $count=$max } 
  531:   &Apache::lonxml::debug("Count is $count from $max");
  532:   return $count;
  533: }
  534: 
  535: sub whichfoils {
  536:     my ($max,$randomize)=@_;
  537:     return &Apache::response::whichorder($max,$randomize,
  538: 					 &Apache::response::showallfoils(),
  539: 					 \%Apache::response::foilgroup);
  540: }
  541: 
  542: sub displayanswers {
  543:     my ($max,$randomize,@opt)=@_;
  544:     my @names;
  545:     if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
  546:         @names = @{ $Apache::response::foilgroup{'names'} };
  547:     }
  548:     return if (!@names);
  549:     my @whichopt = &whichfoils($max,$randomize);
  550:     my $result;
  551:     if ($Apache::lonhomework::type eq 'exam') {
  552: 	my $i = 0;
  553: 	my %opt = map { ($_,$i++) } @opt;
  554: 	
  555: 	$i = 0;
  556: 	my @alphabet = ('A'..'Z');
  557: 	foreach my $name (@whichopt) {
  558: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
  559: 	    $result.=&Apache::response::answer_part('optionresponse',
  560: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
  561: 	    $result.=&Apache::response::answer_part('optionresponse',
  562: 						    $Apache::response::foilgroup{$name.'.value'});
  563: 	    $result.=&Apache::response::answer_footer('optionresponse');
  564: 	}
  565:     } else {
  566: 	$result=&Apache::response::answer_header('optionresponse');
  567: 	foreach my $name (@whichopt) {
  568: 	    $result.=&Apache::response::answer_part('optionresponse',
  569: 						    $Apache::response::foilgroup{$name.'.value'});
  570: 	}
  571: 	$result.=&Apache::response::answer_footer('optionresponse');
  572:     }
  573:     return $result;
  574: }
  575: 
  576: sub check_box_opt {
  577:     my ($target,$checkboxvalue,@opt)=@_;
  578: # Check if we are in checkbox mode: checkboxvalue specified, on web, only two options.
  579: # If so, return "checked" value
  580:     if ($#opt!=1) { return ''; }
  581:     unless (($target eq 'web') || ($target eq 'grade')) { return ''; }
  582:     return $checkboxvalue;
  583: }
  584: 
  585: sub check_for_invalid {
  586:     my ($names,$options) = @_;
  587:     my %bad_names;
  588:     foreach my $name (@{ $names }) {
  589: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  590: 	my $found=0;
  591: 	foreach my $option (@{ $options }) {
  592: 	    if ($value eq $option) { $found=1; }
  593: 	}
  594: 	if (!$found) { $bad_names{$name}=$value; }
  595:     }
  596:     if (%bad_names) {
  597: 	my $error=&mt('The question can not be gotten correct, '.
  598: 	    'the following foils in the &lt;optionresponse&gt; '.
  599: 	    'have invalid correct options').' <br /><tt>'.
  600: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
  601: 	    "</tt>";
  602: 	&Apache::lonxml::error($error);
  603:     }
  604: }
  605: 
  606: sub displayfoils {
  607:   my ($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,
  608:       $tex_option_switch, $no_tfprompt, $maxcheck, $mincheck, @opt)=@_;
  609:   my @names; 
  610:   if (ref($Apache::response::foilgroup{'names'}) eq 'ARRAY') {
  611:       @names = @{ $Apache::response::foilgroup{'names'} };
  612:   }
  613:   unless (@names > 0) { return;}
  614:   my @truelist;
  615:   my @falselist;
  616:   my $result;  
  617:   my $name;
  618:   my $displayoptionintex=1;
  619:   my @alphabet = ('A'..'Z');
  620:   my @whichopt = &whichfoils($max,$randomize);
  621:   unless (($target eq 'tex') && ($tex_option_switch eq 'nochoice')) {
  622:       &check_for_invalid(\@whichopt,\@opt);
  623:   }
  624:   my $part=$Apache::inputtags::part;
  625:   my $id=$Apache::inputtags::response[-1];
  626:   my $break;
  627:   if ( ($target ne 'tex') &&
  628:        &Apache::response::show_answer() ) {
  629:     my $temp=1;
  630:     foreach $name (@whichopt) {
  631: 	my $text=$Apache::response::foilgroup{$name.'.text'};
  632:         my $lastresp;
  633:         if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
  634:             (($env{'form.grade_username'} eq $env{'user.name'}) &&
  635:              ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
  636:             $lastresp = $Apache::lonhomework::history{"resource.$part.$id.submission"};
  637:         } else {
  638:             unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
  639:                     ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
  640:                 $lastresp = $Apache::lonhomework::history{"resource.$part.$id.submission"};
  641:             }
  642:         }
  643: 	my %lastresponse=&Apache::lonnet::str2hash($lastresp);
  644: 	my $lastopt=$lastresponse{$name};
  645: 	if ($text!~/^\s*$/) { $break='<br />'; }
  646: 	$result.=$break;
  647: 	if ($target eq 'web') {
  648: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  649: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
  650: 		if ($text=~/^\s*$/) {
  651: 		    $text=$value.$text;
  652: 		} else {
  653: 		    $text='<b>'.$value.':</b> '.$text;
  654: 		}
  655: 	    } else {
  656:                 if (@whichopt > 1) {
  657:                     $text='&#149;'.$text;
  658:                 }
  659: 	    }
  660: 	    $result.=$text."\n";
  661: 	}
  662:       if ($Apache::lonhomework::type eq 'exam') {
  663: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  664:       }
  665:       $temp++;
  666:     }
  667:   } else {
  668:     my $temp=1;
  669:     my %lastresponse;
  670:     my $newvariation;
  671:     if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  672:         ($Apache::lonhomework::type eq 'randomizetry')) &&
  673:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  674:         if ($env{'form.'.$part.'.rndseed'} ne
  675:             $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  676:             $newvariation = 1;
  677:         }
  678:     }
  679:     unless ($newvariation) {
  680:         if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
  681:             (($env{'form.grade_username'} eq $env{'user.name'}) &&
  682:              ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
  683:                  %lastresponse =
  684:                      &Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  685:         } else {
  686:             unless (($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') ||
  687:                     ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) {
  688:                 %lastresponse =
  689:                     &Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  690:             }
  691:         }
  692:     }
  693:     my $internal_counter=$Apache::lonxml::counter;
  694:     my $checkboxopt;
  695:     if ($target eq 'web') {
  696:         $checkboxopt=&check_box_opt($target,$checkboxvalue,@opt);
  697:     }
  698:     if ($checkboxopt && (!$no_tfprompt)) {
  699:        $result.='<br />'.
  700:                 ($checkboxchoices?&mt('Choices: ').'<b>'.$opt[0].','.$opt[1].'</b>. ':'').
  701:                  &mt('Select all that are [_1].','<b>'.$checkboxopt.'</b>');
  702:     }
  703:     foreach $name (@whichopt) {
  704:       my $text=$Apache::response::foilgroup{$name.'.text'};
  705:       if ($text!~/^\s*$/) {
  706: 	  if ($target eq 'tex') {
  707: 	      $break='\vskip 0 mm ';
  708: 	  } elsif ($target eq 'web') {
  709: 	      $break='<br />';
  710: 	  }
  711:       }
  712:       my $lastopt=$lastresponse{$name};
  713:       my $optionlist="<option></option>\n";
  714: 
  715:       if($target eq 'tex' and $env{'form.pdfFormFields'} eq 'yes'
  716:          && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  717:           my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&optionresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
  718:           $optionlist =  &Apache::lonxml::print_pdf_start_combobox($fieldname);
  719:       }
  720: 
  721:       foreach my $option (@opt) {
  722: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
  723:           if ($option eq $lastopt) {
  724:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  725:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  726:                   && $Apache::lonhomework::type ne 'exam') {
  727:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  728:               } else {
  729:                   $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
  730:               }
  731:           } else {
  732:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  733:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  734:                   && $Apache::lonhomework::type ne 'exam') {
  735:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  736:               } else {
  737:                   $optionlist.="<option value='".$escopt."'>$option</option>\n";
  738:               }
  739:           }
  740:       }
  741:       if ($target ne 'tex') {
  742: 	  if ($Apache::lonhomework::type ne 'exam') {
  743: # we are on the web, this is not an exam, and the problem can be answered
  744:               if ($checkboxopt) {
  745: # generate checkboxes
  746:                   my $fieldname=$Apache::inputtags::response['-1'].':'.$temp;
  747:                   my $altopt=$opt[0];
  748:                   if ($opt[0] eq $checkboxopt) {
  749:                      $altopt=$opt[1];
  750:                   }
  751:                   my $defopt=$lastopt;
  752:                   unless ($defopt) { $defopt=$altopt; }
  753:                   my $escdefopt=&HTML::Entities::encode($defopt,'\'"&<>');
  754:                   my $esccheckboxopt=&HTML::Entities::encode($checkboxopt,'\'"&<>');
  755:                   my $escaltopt=&HTML::Entities::encode($altopt,'\'"&<>');
  756: # checkboxopt is how the box is labelled
  757: # altopt is the alternative option
  758: # lastopt is what the user submitted before
  759: # defopt is what the field is going to start out with: either previous choice or altopt
  760: # fieldname is this input field's name after HWVAL_
  761:                   $optionlist='<input type="hidden" name="HWVAL_'.$fieldname.'" value="'.$escdefopt.'" />'.
  762:                   '<input type="checkbox" name="HWCHK_'.$fieldname.'" onclick="javascript:if (this.form.elements[\'HWCHK_'.
  763:                   $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";
  764:               } else {
  765: # classic selection list
  766: 	          $optionlist='<select onchange="javascript:setSubmittedPart(\''.
  767: 		  $part.'\');" name="HWVAL_'.
  768: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
  769: 		  $optionlist."</select>\n";
  770:               }
  771: 	  } else {
  772: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
  773: 	  }
  774: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
  775: 	      if ($Apache::lonhomework::type ne 'exam') {
  776:                   if (@whichopt > 1) {  
  777:                       $text='&#149;'.$text;
  778:                   }
  779: 	      }
  780: 	  } else {
  781: 	      if ($Apache::lonhomework::type ne 'exam') {
  782: 		  $text=$optionlist.$text;
  783: 	      }
  784: 	  }
  785: 	  $result.=$break.$text."\n";
  786: 	  if ($Apache::lonhomework::type eq 'exam') {
  787: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  788: 	  }
  789: 	  $temp++;
  790:       } else {
  791: 	  my $texoptionlist='';
  792: 	  if ($displayoptionintex &&
  793: 	      $Apache::lonhomework::type ne 'exam') {
  794: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
  795: 	  }
  796: 	  if ($text=~/<drawoptionlist\s*\/>/) {
  797: 	      $text=~s|<drawoptionlist\s*\/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |g;
  798: 	  }
  799: 
  800: 	  if ($text=~m/\\item /) {
  801: 	      if ($Apache::lonhomework::type eq 'exam') {
  802: 	          $text=~s/\\item/\\vskip 2 mm/;
  803: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
  804:                   $result.= $texoptionlist.$text;
  805:               }
  806:           } else {
  807: 	      if ($Apache::lonhomework::type eq 'exam') {
  808: 		  $result.= $texoptionlist.'  '.$text;
  809: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
  810: 		  if ($text=~/\S/) {
  811:                       $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;
  812:                   } else {
  813:                       $result.= $texoptionlist;
  814:                   }
  815: 	      }
  816: 	  }
  817: 	  if ($Apache::lonhomework::type eq 'exam') {
  818: 	      $result.='\vskip -1 mm\noindent';
  819:               my ($numrows,$bubbles_per_row) = &getnumrows(scalar(@opt)); 
  820:               if ($numrows == 1) {  
  821:                   $result .= '\textbf{'.$internal_counter.'}. \vskip -3mm';
  822:               } else {
  823:                   my $linetext;
  824:                   for (my $i=0; $i<$numrows; $i++) {
  825:                       $linetext .= $internal_counter+$i.', ';
  826:                   }
  827:                   $linetext =~ s/,\s$//;
  828:                   $result .= '\small {\textbf{'.$linetext.'}} '.
  829:                              '\hskip 2 mm {\footnotesize '.
  830:                              &mt('(Bubble once in [_1] lines)',$numrows).
  831:                              '} \vskip 1 mm';
  832:               }
  833:               $result.= &bubbles(\@alphabet,\@opt,undef,undef,$numrows,
  834:                                  $bubbles_per_row,$internal_counter).
  835:                         ' \strut ';
  836: 	      $internal_counter += $numrows;
  837: 	  }
  838:           if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  839:               && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  840:               && $Apache::lonhomework::type ne 'exam') {
  841:               $text =~ s/\\item//m;
  842:               $result .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
  843:               $temp++;
  844:           }
  845: 	  $displayoptionintex=0;
  846:       }
  847:     }
  848:   }
  849: 
  850:   if ($target eq 'web') {
  851:       my $data = [\@whichopt,'submissiongrading'];
  852:       my $questiontype;
  853:       if ($Apache::lonhomework::type eq 'randomizetry') {
  854:           $questiontype = $Apache::lonhomework::type,
  855:       }
  856:       &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,$data,$questiontype);
  857:   }
  858: 
  859:   if ($target ne 'tex') {
  860:       return $result.$break;
  861:   } else {
  862:       return $result;
  863:   }
  864: }
  865: 
  866: 
  867: sub optionlist_correction {
  868:     my ($TeXlayout,@options) = @_;
  869:     my $texoptionlist='\\item [] '.&mt('Choices:').' ';
  870:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
  871:     if (scalar(@options) > 0) {
  872: 	foreach my $option (@options) {
  873: 	    $texoptionlist.='{\bf '.
  874: 		&Apache::lonxml::latex_special_symbols($option).
  875: 		'}';
  876: 	    if ($TeXlayout eq 'vertical') {
  877: 		$texoptionlist.=' \vskip 0 mm ',
  878: 	    } else {
  879: 		$texoptionlist.=', ';
  880: 	    }
  881: 	}
  882: 	$texoptionlist=~s/, $//;
  883: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
  884:     } else {
  885: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
  886:     }
  887:     return $texoptionlist;
  888: }
  889: 
  890: 
  891: sub webbubbles {
  892:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
  893:     my @opt=@$ropt; 
  894:     my @alphabet=@$ralphabet;
  895:     my $result='';
  896:     my $number_of_bubbles = $#opt + 1;
  897:     $result.= '<table border="1"><tr>';
  898:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  899: 	my $checked='';
  900: 	if ($lastopt eq $opt[$ind]) {
  901: 	    $checked=' checked="on" ';
  902: 	}
  903: 	$result.='<td><input type="radio" name="HWVAL_'.
  904: 	    $Apache::inputtags::response['-1'].':'.$temp.
  905: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
  906: 	if ($alphabet[$ind]) {
  907: 	    $result.=$alphabet[$ind].': ';
  908: 	}
  909: 	$result.=$opt[$ind].'</td>';
  910:     }
  911:     $result.='</tr></table>';
  912:     return $result;
  913: }
  914: 
  915: 
  916: sub bubbles {
  917:     my ($ralphabet,$ropt,$response,$max_width,$numrows,$bubbles_per_row,
  918:         $internal_counter) = @_;
  919:     my @alphabet = @$ralphabet;
  920:     my @opt = @$ropt;
  921:     my ($result,$head,$line) =('','','');
  922:     my $number_of_bubbles = $#opt + 1;
  923:     my $current_length = 0;
  924:     my $textwidth;
  925:     if (defined($max_width)) {
  926: 	$textwidth=$max_width;
  927: 	&Apache::lonxml::debug("Max width passed in: $max_width");
  928:     } elsif ($env{'form.textwidth'} ne '') {
  929: 	$env{'form.textwidth'}=~/(\d+)/;
  930: 	$textwidth=$1;
  931: 	&Apache::lonxml::debug("Max width from form: $textwidth");
  932:     } else {
  933: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
  934: 	$textwidth=$1.'.'.$2;
  935: 	&Apache::lonxml::debug("Max width defaults? $textwidth");
  936:     }
  937:     &Apache::lonxml::debug("Final maxwidth: $textwidth");
  938:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
  939:         my $item;
  940:         if ($numrows > 1) {
  941:             my $num = $internal_counter+int($ind/$bubbles_per_row);
  942:             my $idx = int($ind % $bubbles_per_row);
  943:             $item = $num.$alphabet[$idx];
  944:         } else {
  945:             $item = $alphabet[$ind];
  946:         }
  947: 	my $leftmargin;
  948: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
  949: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
  950: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
  951: 
  952: 	$current_length += (length($opt[$ind])+length($item)+5)*2;
  953:         
  954: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
  955:             
  956: 	    $line.='\hskip 4 mm {\small \textbf{'.$item.'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
  957: 	    $head.='lr';
  958: 	} else {
  959: 	    $line=~s/\&\s*$//;
  960: 	    $result.='\vskip -1 mm\noindent\setlength{\tabcolsep}{2 mm}\renewcommand{\arraystretch}{1.25}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
  961: 	    $line = '\hskip 4 mm {\small \textbf{'.$item.'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
  962: 	    $head ='lr';
  963: 	    $current_length = (length($opt[$ind])+length($item))*2;
  964: 	}
  965: 
  966:     }
  967:     return $result;
  968: }
  969: 
  970: sub start_conceptgroup {
  971:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  972:   $Apache::optionresponse::conceptgroup=1;
  973:   %Apache::response::conceptgroup=();
  974:   my $result;
  975:   if ($target eq 'edit') {
  976:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  977:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  978:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  979:   }
  980:   if ($target eq 'modified') {
  981:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  982:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  983:   }
  984:   return $result;
  985: }
  986: 
  987: sub end_conceptgroup {
  988:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  989:   $Apache::optionresponse::conceptgroup=0;
  990:   my $result='';
  991:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  992:       $target eq 'tex' || $target eq 'analyze') {
  993:     #if not there aren't any foils to display and thus no question
  994:       &Apache::response::pick_foil_for_concept($target,
  995: 					       ['value','text','location'],
  996: 					       \%Apache::hint::option,
  997: 					       $parstack,$safeeval);
  998:   } elsif ($target eq 'edit') {
  999:     $result=&Apache::edit::end_table();
 1000:   }
 1001:   return $result;
 1002: }
 1003: 
 1004: sub insert_conceptgroup {
 1005:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
 1006:   return $result;
 1007: }
 1008: 
 1009: sub start_foil {
 1010:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1011:   my $result='';
 1012:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
 1013:       &Apache::lonxml::startredirection;
 1014:       if ($target eq 'analyze') {
 1015: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
 1016:       }
 1017:   } elsif ($target eq 'edit') {
 1018:     $result=&Apache::edit::tag_start($target,$token,"Foil");
 1019:     my $level='-2';
 1020:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
 1021:     my @opt;
 1022:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
 1023:     $result.=&Apache::edit::text_arg('Name:','name',$token);
 1024:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
 1025: 					       ['unused',(@opt)],$token,'15');
 1026:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
 1027: 					     $safeeval,'-3');
 1028:     if ($randomize ne 'no') {
 1029: 	$result.=&Apache::edit::select_arg('Location:','location',
 1030: 					   ['random','top','bottom'],$token);
 1031:     }
 1032:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 1033:   } elsif ($target eq 'modified') {
 1034:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
 1035: 						 'value','name','location');
 1036:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 1037:   } 
 1038:   return $result;
 1039: }
 1040: 
 1041: sub end_foil {
 1042:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1043:   my $text ='';
 1044:   my $result = '';
 1045:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
 1046:       $text=&Apache::lonxml::endredirection;
 1047:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
 1048:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
 1049:   }
 1050:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
 1051:       || $target eq 'tex' || $target eq 'analyze') {
 1052:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
 1053:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
 1054: 	$text='\vskip 5mm $\triangleright$ '.$text;
 1055:     }
 1056:     if ($value ne 'unused') {
 1057:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
 1058:       &Apache::lonxml::debug("Got a name of :$name:");
 1059:       if ($name eq "") {
 1060: 	  &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
 1061: 	  $name=$Apache::lonxml::curdepth;
 1062:       }
 1063:       &Apache::lonxml::debug("Using a name of :$name:");
 1064:       if (defined($Apache::response::foilnames{$name})) {
 1065: 	  &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
 1066:       }
 1067:       $Apache::response::foilnames{$name}++;
 1068:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
 1069:       if ( $Apache::optionresponse::conceptgroup
 1070: 	   && !&Apache::response::showallfoils() ) {
 1071: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
 1072: 	$Apache::response::conceptgroup{"$name.value"} = $value;
 1073: 	$Apache::response::conceptgroup{"$name.text"} = $text;
 1074: 	$Apache::response::conceptgroup{"$name.location"} = $location;
 1075:       } else {
 1076: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
 1077: 	$Apache::response::foilgroup{"$name.value"} = $value;
 1078: 	$Apache::response::foilgroup{"$name.text"} = $text;
 1079: 	$Apache::response::foilgroup{"$name.location"} = $location;
 1080:       }
 1081:     }
 1082:   }
 1083:   if ($target eq 'edit') {
 1084:     $result.= &Apache::edit::tag_end($target,$token,'');
 1085:   }
 1086:   return $result;
 1087: }
 1088: 
 1089: sub start_drawoptionlist {
 1090:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1091:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
 1092: 	return $token->[4];
 1093:     }
 1094: }
 1095: 
 1096: sub end_drawoptionlist {
 1097:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1098:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
 1099: 	return $token->[2];
 1100:     }
 1101: }
 1102: 
 1103: sub insert_foil {
 1104:     return '
 1105: <foil name="" value="unused">
 1106: <startouttext />
 1107: <endouttext />
 1108: </foil>';
 1109: }
 1110: 
 1111: sub insert_drawoptionlist {
 1112:     return '<drawoptionlist />';
 1113: }
 1114: 
 1115: sub getnumrows {
 1116:     my ($numitems) = @_;
 1117:     my $bubbles_per_row;
 1118:     my $default_numbubbles = 10;
 1119:     if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
 1120:         ($env{'form.bubbles_per_row'} > 0)) {
 1121:         $bubbles_per_row = $env{'form.bubbles_per_row'};
 1122:     } else {
 1123:         $bubbles_per_row = $default_numbubbles;
 1124:     }
 1125:     my $numrows = int ($numitems/$bubbles_per_row);
 1126:     if (($numitems % $bubbles_per_row) != 0) {
 1127:         $numrows ++;
 1128:     }
 1129:     return ($numrows,$bubbles_per_row);
 1130: }
 1131: 
 1132: 1;
 1133: __END__
 1134:  
 1135: =head1 NAME
 1136: 
 1137: Apache::optionresponse.pm;
 1138: 
 1139: =head1 SYNOPSIS
 1140: 
 1141: Handles tags associated with showing a list of
 1142: options.
 1143: 
 1144: This is part of the LearningOnline Network with CAPA project
 1145: described at http://www.lon-capa.org.
 1146: 
 1147: =head1 HANDLER SUBROUTINE
 1148: 
 1149: start_optionresponse()
 1150: 
 1151: =head1 OTHER SUBROUTINES
 1152: 
 1153: =over
 1154: 
 1155: =item end_optionresponse()
 1156: 
 1157: =item start_foilgroup()
 1158: 
 1159: =item end_foilgroup()
 1160: 
 1161: =item getfoilcounts()
 1162: 
 1163: =item displayanswers()
 1164: 
 1165: =item check_for_invalid()
 1166: 
 1167: =item displayfoils()
 1168: 
 1169: =item optionlist_correction()
 1170: 
 1171: =item webbubbles()
 1172: 
 1173: =item bubbles()
 1174: 
 1175: =item start_conceptgroup()
 1176: 
 1177: =item end_conceptgroup()
 1178: 
 1179: =item insert_conceptgroup()
 1180: 
 1181: =item start_foil()
 1182: 
 1183: =item end_foil()
 1184: 
 1185: =item start_drawoptionlist()
 1186: 
 1187: =item end_drawoptionlist()
 1188: 
 1189: =item insert_foil()
 1190: 
 1191: =item insert_drawoptionlist()
 1192: 
 1193: =back
 1194: 
 1195: =cut

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