File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.172: download - view: text, annotated - select for diffs
Sun Sep 5 20:46:58 2010 UTC (13 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #6329: can switch off checkbox choices

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

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