File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.160: download - view: text, annotated - select for diffs
Fri Feb 5 20:54:33 2010 UTC (14 years, 3 months ago) by www
Branches: MAIN
CVS tags: HEAD
More work on 2294: provide list of choices

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

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