File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.167: download - view: text, annotated - select for diffs
Mon Aug 23 04:43:04 2010 UTC (13 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6324.
 - reversing change in rev 1.109 (bug 2966), and replacing enumeration with bullets.

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

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