File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.166: download - view: text, annotated - select for diffs
Mon Aug 23 03:57:48 2010 UTC (13 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6329.

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

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