File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.154: download - view: text, annotated - select for diffs
Wed Nov 12 20:32:29 2008 UTC (15 years, 6 months ago) by jms
Branches: MAIN
CVS tags: HEAD
Added POD documentation

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

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