File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.195.2.1: download - view: text, annotated - select for diffs
Tue Apr 21 23:36:43 2015 UTC (9 years, 1 month ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_1
- For 2.11
  Backport 1.198,  1.199.

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

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