File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.173.2.1: download - view: text, annotated - select for diffs
Sat Oct 8 23:03:42 2011 UTC (12 years, 7 months ago) by raeburn
Branches: version_2_10_X
Diff to branchpoint 1.173: preferred, unified
- Backport 1.180.

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

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