File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.174: download - view: text, annotated - select for diffs
Thu Mar 3 21:05:35 2011 UTC (13 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Work in progress on lenient option response grading

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

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