File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.173: download - view: text, annotated - select for diffs
Sun Dec 19 00:52:00 2010 UTC (13 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_0_RC2, version_2_10_0, HEAD
- New Question Type - randomizetry
  - Only set dropdown lists to selections last try when displaying options
    for new try, if rndseed for last try is the same as for current try.

    1: # LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.173 2010/12/19 00:52:00 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(&mt('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:     eval '@opt ='. &Apache::lonxml::get_param('options',$parstack,$safeeval);
  112:     my $count=1;
  113:     foreach $option (@opt) {
  114:       $optionlist.="<option value=\"$count\">$option</option>\n";
  115:       $count++;
  116:     }
  117:     my $insertlist=&Apache::edit::insertlist($target,$token);
  118:     $result.=&Apache::edit::start_table($token);
  119:     $result.= (<<ENDTABLE);
  120:       <tr><td>Select Options</td>
  121:         <td>
  122: 	  Add new Option: <input type="text" name="$Apache::lonxml::curdepth.options" />
  123:         </td>
  124:         <td>Delete an Option:
  125: 	  <select name="$Apache::lonxml::curdepth.deleteopt">$optionlist</select>
  126:         </td>
  127: ENDTABLE
  128:     $result.= '<td>'.&Apache::edit::checked_arg('Print options:','texoptions',
  129: 					 [ ['nochoice','Don\'t show option list'] ]
  130: 					 ,$token).'</td>';
  131:     $result.= '<td><label>'.&mt('Two-option checkboxes for:').
  132:               '<select name="checkboxvalue_'.$Apache::lonxml::curdepth.'">';
  133:     foreach $option (('',@opt)) {
  134:        $result.='<option value="'.$option.'"';
  135:        if ($option eq &Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval)) {
  136:           $result.=' selected="selected"';
  137:        }
  138:        $result.='>'.$option.'</option>';
  139:     }
  140:     $result.='</select></label>';
  141:     $result.=&Apache::edit::checked_arg('Checkbox options:','checkboxoptions',
  142:                                          [ ['nochoice','Don\'t show option list'] ]
  143:                                          ,$token).'</td>';
  144:     $result.= &Apache::edit::end_row();
  145:     $result.= &Apache::edit::start_spanning_row();
  146:     $result.= $insertlist.'<br />';
  147:   }
  148:   if ($target eq 'modified') {
  149:     my @options;
  150:     my $optchanged=0;
  151:     eval '@options ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  152:     if ($env{"form.$Apache::lonxml::curdepth.deleteopt"}) {
  153:       my $delopt=$env{"form.$Apache::lonxml::curdepth.deleteopt"};
  154:       &Apache::lonxml::debug("Deleting :$delopt:");
  155:       splice(@options,$delopt-1,1);
  156:       $optchanged=1;
  157:     }
  158:     if ($env{"form.$Apache::lonxml::curdepth.options"}) {
  159:       my $newopt = $env{"form.$Apache::lonxml::curdepth.options"};
  160:       if ($options[0]) {
  161: 	push(@options,$newopt);
  162:       } else {
  163: 	$options[0]=$newopt;
  164:       }
  165:       $optchanged=1;
  166:     }
  167:     my $rebuildtag = &Apache::edit::get_new_args($token,$parstack,$safeeval,
  168: 						 'texoptions','checkboxvalue','checkboxoptions');
  169:     if ($optchanged || $rebuildtag ) {
  170:       $result = "<foilgroup options=\"(";
  171:       foreach my $option (@options) {
  172: 	$option=~s/\'/\\\'/g;
  173: 	&Apache::lonxml::debug("adding option :$option:");
  174: 	$result .="'".$option."',";
  175:       }
  176:       chop $result;
  177:       $result.=')" ';
  178:       $result .= 'texoptions="'.$token->[2]{'texoptions'}.'" ';
  179:       $result .= 'checkboxoptions="'.$token->[2]{'checkboxoptions'}.'" ';
  180:       $result .= 'checkboxvalue="'.$token->[2]{'checkboxvalue'}.'"';
  181:       $result .= '>';
  182:     } # else nothing changed so just use the default mechanism
  183:   }
  184:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  185:     if($env{'form.pdfFormFields'} ne 'yes') {
  186:       $result .= ' \begin{itemize} ';
  187:     } else {
  188:       $result .= "\\\\";
  189:     }
  190:   }
  191:   return $result;
  192: }
  193: 
  194: sub end_foilgroup {
  195:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  196:   
  197:   my $result;
  198:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  199:       $target eq 'tex' || $target eq 'analyze') {
  200:     my $tex_option_switch=&Apache::lonxml::get_param('texoptions',$parstack,$safeeval);
  201:     my $name;
  202:     my @opt;
  203:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval);
  204:     &Apache::lonxml::debug("options:".join(':',@opt));
  205: 
  206:     my $TeXlayout=&Apache::lonxml::get_param('TeXlayout',$parstack,$safeeval,
  207: 					     -2,0);
  208:     my $checkboxvalue=&Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval);
  209:     my $checkboxchoices=(&Apache::lonxml::get_param('checkboxoptions',$parstack,$safeeval) ne 'nochoice');
  210:     if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
  211:     &Apache::lonxml::debug("Options are $#opt");
  212:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  213:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  214: 					       $safeeval,'-2');
  215:     if ($target eq 'web' || $target eq 'tex') {
  216:       $result.=&displayfoils($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,@opt);
  217:       $Apache::lonxml::post_evaluate=0;
  218:     } elsif ( $target eq 'answer') {
  219:       $result.=&displayanswers($max,$randomize,@opt);
  220:       
  221:     } elsif ( $target eq 'analyze') {
  222: 	my @shown = &whichfoils($max,$randomize);
  223: 	&Apache::response::analyze_store_foilgroup(\@shown,
  224: 						  ['text','value','location']);
  225: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  226: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
  227:     } elsif ( $target eq 'grade') {
  228:       if ( &Apache::response::submitted()) {
  229: 	my @whichopt = &whichfoils($max,$randomize);
  230: 	my $temp=1;my $name;
  231: 	my %responsehash;
  232: 	my %grade;
  233: 	my $right=0;
  234: 	my $wrong=0;
  235: 	my $ignored=0;
  236: 	foreach $name (@whichopt) {
  237: 	  my $response=&Apache::response::getresponse($temp);
  238: 	  if ($env{'form.submitted'} eq 'scantron' && $response=~/\S/) {
  239: 	      $response = $opt[$response];
  240: 	  }
  241: 	  if ( $response =~ /[^\s]/) {
  242: 	    $responsehash{$name}=$response;
  243: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  244: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  245: 	    if ($value eq $response) {
  246: 		$grade{$name}='1'; $right++;
  247: 	    } else {
  248: 		$grade{$name}='0'; $wrong++;
  249: 	    }
  250: 	  } else {
  251: 	    $ignored++;
  252: 	  }
  253: 	  $temp++;
  254: 	}
  255: 	my $part=$Apache::inputtags::part;
  256: 	my $id = $Apache::inputtags::response['-1'];
  257: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  258: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
  259: 	my %previous=&Apache::response::check_for_previous($responsestr,
  260: 							   $part,$id);
  261: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  262: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  263: 	    $responsestr;
  264: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  265:         if ($Apache::lonhomework::type eq 'randomizetry') {
  266:             $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichopt);
  267:         }
  268: 	if (($Apache::lonhomework::type eq 'survey') ||
  269:             ($Apache::lonhomework::type eq 'surveycred') ||
  270:             ($Apache::lonhomework::type eq 'anonsurvey') ||
  271:             ($Apache::lonhomework::type eq 'anonsurveycred')) {
  272: 	    if ($ignored == 0) {
  273:                 my $ad;
  274:                 if ($Apache::lonhomework::type eq 'anonsurveycred') {
  275:                     $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
  276:                 } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
  277:                     $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
  278:                 } elsif ($Apache::lonhomework::type eq 'surveycred') {
  279:                     $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
  280:                 } else {
  281: 		    $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  282:                 }
  283: 		&Apache::response::handle_previous(\%previous,$ad);
  284: 	    } elsif ($wrong==0 && $right==0) {
  285: 	    } else {
  286: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  287: 		&Apache::response::handle_previous(\%previous,$ad);
  288: 	    }
  289: 	} elsif (!$Apache::lonhomework::scantronmode) {
  290: 	    my $ad;
  291: 	    if ($wrong==0 && $ignored==0) {
  292: 		$ad='EXACT_ANS';
  293: 	    } elsif ($wrong==0 && $right==0) {
  294: 		#nothing submitted
  295: 	    } else {
  296: 		if ($ignored==0) {
  297: 		    $ad='INCORRECT';
  298: 		} else {
  299: 		    $ad='MISSING_ANSWER';
  300: 		}
  301: 	    }
  302: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  303: 	    &Apache::response::handle_previous(\%previous,$ad);
  304: 	} else {
  305: 	    my $ad;
  306: 	    if ($wrong==0 && $right==0) {
  307: 		#nothing submitted only assign a score if we 
  308: 		#need to override a previous grade
  309: 		if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
  310: 		    $ad='ASSIGNED_SCORE';
  311: 		}
  312: 	    } else {
  313: 		$ad='ASSIGNED_SCORE';
  314: 	    }
  315: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  316: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  317: 		$right/(scalar(@whichopt));
  318: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  319: 		scalar(@whichopt);
  320: 	}
  321:       }
  322:     }
  323:     my $part_id     = $Apache::inputtags::part;
  324:     my $response_id = $Apache::inputtags::response[-1];
  325:     &Apache::lonxml::increment_counter(&getfoilcounts($max),
  326: 				       "$part_id.$response_id");
  327:     if ($target eq 'analyze') {
  328: 	&Apache::lonhomework::set_bubble_lines();
  329:     }
  330: 
  331:   } elsif ($target eq 'edit') {
  332:     $result.=&Apache::edit::end_table();
  333:   }
  334:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  335:     if($env{'form.pdfFormFields'} ne 'yes') {
  336:       $result .= '\end{itemize}'; 
  337:     } else {
  338:       $result .= "\\\\";
  339:     }
  340:   }
  341:   &Apache::response::poprandomnumber();
  342:   return $result;
  343: }
  344: 
  345: sub getfoilcounts {
  346:   my ($max)=@_;
  347:   # +1 since instructors will count from 1
  348:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  349:   if (&Apache::response::showallfoils()) { $max=$count; }
  350:   if ($count>$max) { $count=$max } 
  351:   &Apache::lonxml::debug("Count is $count from $max");
  352:   return $count;
  353: }
  354: 
  355: sub whichfoils {
  356:     my ($max,$randomize)=@_;
  357:     return &Apache::response::whichorder($max,$randomize,
  358: 					 &Apache::response::showallfoils(),
  359: 					 \%Apache::response::foilgroup);
  360: }
  361: 
  362: sub displayanswers {
  363:     my ($max,$randomize,@opt)=@_;
  364:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  365:     my @names = @{ $Apache::response::foilgroup{'names'} };
  366:     my @whichopt = &whichfoils($max,$randomize);
  367:     my $result;
  368:     if ($Apache::lonhomework::type eq 'exam') {
  369: 	my $i = 0;
  370: 	my %opt = map { ($_,$i++) } @opt;
  371: 	
  372: 	$i = 0;
  373: 	my @alphabet = ('A'..'Z');
  374: 	foreach my $name (@whichopt) {
  375: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
  376: 	    $result.=&Apache::response::answer_part('optionresponse',
  377: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
  378: 	    $result.=&Apache::response::answer_part('optionresponse',
  379: 						    $Apache::response::foilgroup{$name.'.value'});
  380: 	    $result.=&Apache::response::answer_footer('optionresponse');
  381: 	}
  382:     } else {
  383: 	$result=&Apache::response::answer_header('optionresponse');
  384: 	foreach my $name (@whichopt) {
  385: 	    $result.=&Apache::response::answer_part('optionresponse',
  386: 						    $Apache::response::foilgroup{$name.'.value'});
  387: 	}
  388: 	$result.=&Apache::response::answer_footer('optionresponse');
  389:     }
  390:     return $result;
  391: }
  392: 
  393: sub check_box_opt {
  394:     my ($target,$checkboxvalue,@opt)=@_;
  395: # Check if we are in checkbox mode: checkboxvalue specified, on web, only two options.
  396: # If so, return "checked" value
  397:     if ($#opt!=1) { return ''; }
  398:     if ($target ne 'web') { return ''; }
  399:     return $checkboxvalue;
  400: }
  401: 
  402: sub check_for_invalid {
  403:     my ($names,$options) = @_;
  404:     my %bad_names;
  405:     foreach my $name (@{ $names }) {
  406: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  407: 	my $found=0;
  408: 	foreach my $option (@{ $options }) {
  409: 	    if ($value eq $option) { $found=1; }
  410: 	}
  411: 	if (!$found) { $bad_names{$name}=$value; }
  412:     }
  413:     if (%bad_names) {
  414: 	my $error=&mt('The question can not be gotten correct, '.
  415: 	    'the following foils in the &lt;optionresponse&gt; '.
  416: 	    'have invalid correct options').' <br /><tt>'.
  417: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
  418: 	    "</tt>";
  419: 	&Apache::lonxml::error($error);
  420:     }
  421: }
  422: 
  423: sub displayfoils {
  424:   my ($target,$max,$randomize,$TeXlayout,$checkboxvalue,$checkboxchoices,@opt)=@_;
  425:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  426:   my @names = @{ $Apache::response::foilgroup{'names'} };
  427:   my @truelist;
  428:   my @falselist;
  429:   my $result;  
  430:   my $name;
  431:   my $displayoptionintex=1;
  432:   my @alphabet = ('A'..'Z');
  433:   my @whichopt = &whichfoils($max,$randomize);
  434:   &check_for_invalid(\@whichopt,\@opt);
  435:   my $part=$Apache::inputtags::part;
  436:   my $id=$Apache::inputtags::response[-1];
  437:   my $break;
  438:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  439:   if ( ($target ne 'tex') &&
  440:        &Apache::response::show_answer() ) {
  441:     my $temp=1;
  442:     foreach $name (@whichopt) {
  443: 	my $text=$Apache::response::foilgroup{$name.'.text'};
  444:         my $lastresp;
  445:         unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) && (defined($env{'form.grade_symb'}))) {
  446:             $lastresp = $Apache::lonhomework::history{"resource.$part.$id.submission"};
  447:         }
  448: 	my %lastresponse=&Apache::lonnet::str2hash($lastresp);
  449: 	my $lastopt=$lastresponse{$name};
  450: 	if ($text!~/^\s*$/) { $break='<br />'; }
  451: 	$result.=$break;
  452: 	if ($target eq 'web') {
  453: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  454: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
  455: 		if ($text=~/^\s*$/) {
  456: 		    $text=$value.$text;
  457: 		} else {
  458: 		    $text='<b>'.$value.':</b> '.$text;
  459: 		}
  460: 	    } else {
  461:                 if (@whichopt > 1) {
  462:                     $text='&#149;'.$text;
  463:                 }
  464: 	    }
  465: 	    $result.=$text."\n";
  466: 	}
  467:       if ($Apache::lonhomework::type eq 'exam') {
  468: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  469:       }
  470:       $temp++;
  471:     }
  472:   } else {
  473:     my $temp=1;
  474:     my %lastresponse;
  475:     my $newvariation;
  476:     if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  477:         ($Apache::lonhomework::type eq 'randomizetry')) &&
  478:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  479:         if ($env{'form.'.$part.'.rndseed'} ne
  480:             $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  481:             $newvariation = 1;
  482:         }
  483:     }
  484:     unless ($newvariation) {
  485:         %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  486:     }
  487:     my $internal_counter=$Apache::lonxml::counter;
  488:     my $checkboxopt=&check_box_opt($target,$checkboxvalue,@opt);
  489:     if ($checkboxopt) {
  490:        $result.='<br />'.
  491:                 ($checkboxchoices?&mt('Choices: ').'<b>'.$opt[0].','.$opt[1].'</b>. ':'').
  492:                  &mt('Select all that are <b>[_1]</b>.',$checkboxopt);
  493:     }
  494:     foreach $name (@whichopt) {
  495:       my $text=$Apache::response::foilgroup{$name.'.text'};
  496:       if ($text!~/^\s*$/) {
  497: 	  if ($target eq 'tex') {
  498: 	      $break='\vskip 0 mm ';
  499: 	  } elsif ($target eq 'web') {
  500: 	      $break='<br />';
  501: 	  }
  502:       }
  503:       my $lastopt=$lastresponse{$name};
  504:       my $optionlist="<option></option>\n";
  505: 
  506:       if($target eq 'tex' and $env{'form.pdfFormFields'} eq 'yes'
  507:          && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  508:           my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&optionresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
  509:           $optionlist =  &Apache::lonxml::print_pdf_start_combobox($fieldname);
  510:       }
  511: 
  512:       foreach my $option (@opt) {
  513: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
  514:           if ($option eq $lastopt) {
  515:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  516:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  517:                   && $Apache::lonhomework::type ne 'exam') {
  518:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  519:               } else {
  520:                   $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
  521:               }
  522:           } else {
  523:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  524:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  525:                   && $Apache::lonhomework::type ne 'exam') {
  526:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  527:               } else {
  528:                   $optionlist.="<option value='".$escopt."'>$option</option>\n";
  529:               }
  530:           }
  531:       }
  532:       if ($target ne 'tex') {
  533: 	  if ($Apache::lonhomework::type ne 'exam') {
  534: # we are on the web, this is not an exam, and the problem can be answered
  535:               if ($checkboxopt) {
  536: # generate checkboxes
  537:                   my $fieldname=$Apache::inputtags::response['-1'].':'.$temp;
  538:                   my $altopt=$opt[0];
  539:                   if ($opt[0] eq $checkboxopt) {
  540:                      $altopt=$opt[1];
  541:                   }
  542:                   my $defopt=$lastopt;
  543:                   unless ($defopt) { $defopt=$altopt; }
  544:                   my $escdefopt=&HTML::Entities::encode($defopt,'\'"&<>');
  545:                   my $esccheckboxopt=&HTML::Entities::encode($checkboxopt,'\'"&<>');
  546:                   my $escaltopt=&HTML::Entities::encode($altopt,'\'"&<>');
  547: # checkboxopt is how the box is labelled
  548: # altopt is the alternative option
  549: # lastopt is what the user submitted before
  550: # defopt is what the field is going to start out with: either previous choice or altopt
  551: # fieldname is this input field's name after HWVAL_
  552:                   $optionlist='<input type="hidden" name="HWVAL_'.$fieldname.'" value="'.$escdefopt.'" />'.
  553:                   '<input type="checkbox" name="HWCHK_'.$fieldname.'" onclick="javascript:if (this.form.elements[\'HWCHK_'.
  554:                   $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";
  555:               } else {
  556: # classic selection list
  557: 	          $optionlist='<select onchange="javascript:setSubmittedPart(\''.
  558: 		  $part.'\');" name="HWVAL_'.
  559: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
  560: 		  $optionlist."</select>\n";
  561:               }
  562: 	  } else {
  563: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
  564: 	  }
  565: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
  566: 	      if ($Apache::lonhomework::type ne 'exam') {
  567:                   if (@whichopt > 1) {  
  568:                       $text='&#149;'.$text;
  569:                   }
  570: 	      }
  571: 	  } else {
  572: 	      if ($Apache::lonhomework::type ne 'exam') {
  573: 		  $text=$optionlist.$text;
  574: 	      }
  575: 	  }
  576: 	  $result.=$break.$text."\n";
  577: 	  if ($Apache::lonhomework::type eq 'exam') {
  578: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  579: 	  }
  580: 	  $temp++;
  581:       } else {
  582: 	  my $texoptionlist='';
  583: 	  if ($displayoptionintex &&
  584: 	      $Apache::lonhomework::type ne 'exam') {
  585: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
  586: 	  }
  587: 	  if ($text=~/<drawoptionlist\s*\/>/) {
  588: 	      $text=~s|<drawoptionlist\s*\/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |g;
  589: 	  }
  590: 
  591: 	  if ($text=~m/\\item /) {
  592: 	      if ($Apache::lonhomework::type eq 'exam') {
  593: 	          $text=~s/\\item/\\vskip 2 mm/;
  594: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
  595:                   $result.= $texoptionlist.$text;
  596:               }
  597:           } else {
  598: 	      if ($Apache::lonhomework::type eq 'exam') {
  599: 		  $result.= $texoptionlist.'  '.$text;
  600: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
  601: 		  if ($text=~/\S/) {
  602:                       $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;
  603:                   } else {
  604:                       $result.= $texoptionlist;
  605:                   }
  606: 	      }
  607: 	  }
  608: 	  if ($Apache::lonhomework::type eq 'exam') {
  609: 	      $result.='\vskip -1 mm\noindent';
  610:               $result.= '\textbf{'. $internal_counter.'}. \vskip -3mm'.&bubbles(\@alphabet,\@opt).
  611: 		                 ' \strut ';
  612: 	      $internal_counter++;
  613: 	  }
  614:           if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  615:               && $Apache::inputtags::status[-1] eq 'CAN_ANSWER'
  616:               && $Apache::lonhomework::type ne 'exam') {
  617:               $text =~ s/\\item//m;
  618:               $result .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
  619:               $temp++;
  620:           }
  621: 	  $displayoptionintex=0;
  622:       }
  623:     }
  624:   }
  625: 
  626:   if ($target eq 'web') {
  627:       my $data = [\@whichopt,'submissiongrading'];
  628:       my $questiontype;
  629:       if ($Apache::lonhomework::type eq 'randomizetry') {
  630:           $questiontype = $Apache::lonhomework::type,
  631:       }
  632:       &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,$data,$questiontype);
  633:   }
  634: 
  635:   if ($target ne 'tex') {
  636:       return $result.$break;
  637:   } else {
  638:       return $result;
  639:   }
  640: }
  641: 
  642: 
  643: sub optionlist_correction {
  644:     my ($TeXlayout,@options) = @_;
  645:     my $texoptionlist='\\item [] '.&mt('Choices:').' ';
  646:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
  647:     if (scalar(@options) > 0) {
  648: 	foreach my $option (@options) {
  649: 	    $texoptionlist.='{\bf '.
  650: 		&Apache::lonxml::latex_special_symbols($option).
  651: 		'}';
  652: 	    if ($TeXlayout eq 'vertical') {
  653: 		$texoptionlist.=' \vskip 0 mm ',
  654: 	    } else {
  655: 		$texoptionlist.=', ';
  656: 	    }
  657: 	}
  658: 	$texoptionlist=~s/, $//;
  659: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
  660:     } else {
  661: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
  662:     }
  663:     return $texoptionlist;
  664: }
  665: 
  666: 
  667: sub webbubbles {
  668:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
  669:     my @opt=@$ropt; 
  670:     my @alphabet=@$ralphabet;
  671:     my $result='';
  672:     my $number_of_bubbles = $#opt + 1;
  673:     $result.= '<table border="1"><tr>';
  674:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  675: 	my $checked='';
  676: 	if ($lastopt eq $opt[$ind]) {
  677: 	    $checked=' checked="on" ';
  678: 	}
  679: 	$result.='<td><input type="radio" name="HWVAL_'.
  680: 	    $Apache::inputtags::response['-1'].':'.$temp.
  681: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
  682: 	if ($alphabet[$ind]) {
  683: 	    $result.=$alphabet[$ind].': ';
  684: 	}
  685: 	$result.=$opt[$ind].'</td>';
  686:     }
  687:     $result.='</tr></table>';
  688:     return $result;
  689: }
  690: 
  691: 
  692: sub bubbles {
  693:     my ($ralphabet,$ropt,$response, $max_width) = @_;
  694:     my @alphabet = @$ralphabet;
  695:     my @opt = @$ropt;
  696:     my ($result,$head,$line) =('','','');
  697:     my $number_of_bubbles = $#opt + 1;
  698:     my $current_length = 0;
  699:     my $textwidth;
  700:     if (defined($max_width)) {
  701: 	$textwidth=$max_width;
  702: 	&Apache::lonxml::debug("Max width passed in: $max_width");
  703:     } elsif ($env{'form.textwidth'} ne '') {
  704: 	$env{'form.textwidth'}=~/(\d+)/;
  705: 	$textwidth=$1;
  706: 	&Apache::lonxml::debug("Max width from form: $textwidth");
  707:     } else {
  708: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
  709: 	$textwidth=$1.'.'.$2;
  710: 	&Apache::lonxml::debug("Max width defaults? $textwidth");
  711:     }
  712:     &Apache::lonxml::debug("Final maxwidth: $textwidth");
  713:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
  714: 	my $leftmargin;
  715: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
  716: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
  717: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
  718: 
  719: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+4)*2;
  720: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
  721: 	    $line.='\hskip 4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
  722: 	    $head.='lr';
  723: 	} else {
  724: 	    $line=~s/\&\s*$//;
  725: 	    $result.='\vskip -1 mm\noindent\setlength{\tabcolsep}{2 mm}\renewcommand{\arraystretch}{1.25}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
  726: 	    $line = '\hskip 4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
  727: 	    $head ='lr';
  728: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
  729: 	}
  730: 
  731:     }
  732:     return $result;
  733: }
  734: 
  735: 
  736: sub start_conceptgroup {
  737:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  738:   $Apache::optionresponse::conceptgroup=1;
  739:   %Apache::response::conceptgroup=();
  740:   my $result;
  741:   if ($target eq 'edit') {
  742:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  743:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  744:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  745:   }
  746:   if ($target eq 'modified') {
  747:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  748:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  749:   }
  750:   return $result;
  751: }
  752: 
  753: sub end_conceptgroup {
  754:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  755:   $Apache::optionresponse::conceptgroup=0;
  756:   my $result='';
  757:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  758:       $target eq 'tex' || $target eq 'analyze') {
  759:     #if not there aren't any foils to display and thus no question
  760:       &Apache::response::pick_foil_for_concept($target,
  761: 					       ['value','text','location'],
  762: 					       \%Apache::hint::option,
  763: 					       $parstack,$safeeval);
  764:   } elsif ($target eq 'edit') {
  765:     $result=&Apache::edit::end_table();
  766:   }
  767:   return $result;
  768: }
  769: 
  770: sub insert_conceptgroup {
  771:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  772:   return $result;
  773: }
  774: 
  775: sub start_foil {
  776:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  777:   my $result='';
  778:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
  779:       &Apache::lonxml::startredirection;
  780:       if ($target eq 'analyze') {
  781: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  782:       }
  783:   } elsif ($target eq 'edit') {
  784:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  785:     my $level='-2';
  786:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  787:     my @opt;
  788:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
  789:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  790:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
  791: 					       ['unused',(@opt)],$token,'15');
  792:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  793: 					     $safeeval,'-3');
  794:     if ($randomize ne 'no') {
  795: 	$result.=&Apache::edit::select_arg('Location:','location',
  796: 					   ['random','top','bottom'],$token);
  797:     }
  798:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  799:   } elsif ($target eq 'modified') {
  800:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  801: 						 'value','name','location');
  802:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  803:   } 
  804:   return $result;
  805: }
  806: 
  807: sub end_foil {
  808:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  809:   my $text ='';
  810:   my $result = '';
  811:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  812:       $text=&Apache::lonxml::endredirection;
  813:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
  814:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
  815:   }
  816:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
  817:       || $target eq 'tex' || $target eq 'analyze') {
  818:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  819:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  820: 	$text='\vskip 5mm $\triangleright$ '.$text;
  821:     }
  822:     if ($value ne 'unused') {
  823:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  824:       &Apache::lonxml::debug("Got a name of :$name:");
  825:       if ($name eq "") {
  826: 	  &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  827: 	  $name=$Apache::lonxml::curdepth;
  828:       }
  829:       &Apache::lonxml::debug("Using a name of :$name:");
  830:       if (defined($Apache::response::foilnames{$name})) {
  831: 	  &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
  832:       }
  833:       $Apache::response::foilnames{$name}++;
  834:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
  835:       if ( $Apache::optionresponse::conceptgroup
  836: 	   && !&Apache::response::showallfoils() ) {
  837: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  838: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  839: 	$Apache::response::conceptgroup{"$name.text"} = $text;
  840: 	$Apache::response::conceptgroup{"$name.location"} = $location;
  841:       } else {
  842: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  843: 	$Apache::response::foilgroup{"$name.value"} = $value;
  844: 	$Apache::response::foilgroup{"$name.text"} = $text;
  845: 	$Apache::response::foilgroup{"$name.location"} = $location;
  846:       }
  847:     }
  848:   }
  849:   if ($target eq 'edit') {
  850:     $result.= &Apache::edit::tag_end($target,$token,'');
  851:   }
  852:   return $result;
  853: }
  854: 
  855: sub start_drawoptionlist {
  856:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  857:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
  858: 	return $token->[4];
  859:     }
  860: }
  861: 
  862: sub end_drawoptionlist {
  863:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  864:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
  865: 	return $token->[2];
  866:     }
  867: }
  868: 
  869: sub insert_foil {
  870:     return '
  871: <foil name="" value="unused">
  872: <startouttext />
  873: <endouttext />
  874: </foil>';
  875: }
  876: 
  877: sub insert_drawoptionlist {
  878:     return '<drawoptionlist />';
  879: }
  880: 1;
  881: __END__
  882:  
  883: =head1 NAME
  884: 
  885: Apache::optionresponse.pm;
  886: 
  887: =head1 SYNOPSIS
  888: 
  889: Handles tags associated with showing a list of
  890: options.
  891: 
  892: This is part of the LearningOnline Network with CAPA project
  893: described at http://www.lon-capa.org.
  894: 
  895: =head1 HANDLER SUBROUTINE
  896: 
  897: start_optionresponse()
  898: 
  899: =head1 OTHER SUBROUTINES
  900: 
  901: =over
  902: 
  903: =item end_optionresponse()
  904: 
  905: =item start_foilgroup()
  906: 
  907: =item end_foilgroup()
  908: 
  909: =item getfoilcounts()
  910: 
  911: =item displayanswers()
  912: 
  913: =item check_for_invalid()
  914: 
  915: =item displayfoils()
  916: 
  917: =item optionlist_correction()
  918: 
  919: =item webbubbles()
  920: 
  921: =item bubbles()
  922: 
  923: =item start_conceptgroup()
  924: 
  925: =item end_conceptgroup()
  926: 
  927: =item insert_conceptgroup()
  928: 
  929: =item start_foil()
  930: 
  931: =item end_foil()
  932: 
  933: =item start_drawoptionlist()
  934: 
  935: =item end_drawoptionlist()
  936: 
  937: =item insert_foil()
  938: 
  939: =item insert_drawoptionlist()
  940: 
  941: =back
  942: 
  943: =cut

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