File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.152: download - view: text, annotated - select for diffs
Thu Sep 11 17:38:14 2008 UTC (15 years, 8 months ago) by onken
Branches: MAIN
CVS tags: HEAD
Add optionresponse problems to PDF-Forms

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

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