File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.176: download - view: text, annotated - select for diffs
Tue Jun 7 17:27:37 2011 UTC (12 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #5771: foil name 0 does not work

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

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