File:  [LON-CAPA] / loncom / homework / optionresponse.pm
Revision 1.161: download - view: text, annotated - select for diffs
Fri Feb 5 21:38:41 2010 UTC (14 years, 3 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #2294: now only needs editing interface

    1: # LearningOnline Network with CAPA
    2: # option list style responses
    3: #
    4: # $Id: optionresponse.pm,v 1.161 2010/02/05 21:38:41 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:     my $checkboxvalue=&Apache::lonxml::get_param('checkboxvalue',$parstack,$safeeval);
  194:     if ($target eq 'tex' && $tex_option_switch eq 'nochoice') {@opt=();}
  195:     &Apache::lonxml::debug("Options are $#opt");
  196:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  197:     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  198: 					       $safeeval,'-2');
  199:     if ($target eq 'web' || $target eq 'tex') {
  200:       $result.=&displayfoils($target,$max,$randomize,$TeXlayout,$checkboxvalue,@opt);
  201:       $Apache::lonxml::post_evaluate=0;
  202:     } elsif ( $target eq 'answer') {
  203:       $result.=&displayanswers($max,$randomize,@opt);
  204:       
  205:     } elsif ( $target eq 'analyze') {
  206: 	my @shown = &whichfoils($max,$randomize);
  207: 	&Apache::response::analyze_store_foilgroup(\@shown,
  208: 						  ['text','value','location']);
  209: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  210: 	push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },@opt);
  211:     } elsif ( $target eq 'grade') {
  212:       if ( &Apache::response::submitted()) {
  213: 	my @whichopt = &whichfoils($max,$randomize);
  214: 	my $temp=1;my $name;
  215: 	my %responsehash;
  216: 	my %grade;
  217: 	my $right=0;
  218: 	my $wrong=0;
  219: 	my $ignored=0;
  220: 	foreach $name (@whichopt) {
  221: 	  my $response=&Apache::response::getresponse($temp);
  222: 	  if ($env{'form.submitted'} eq 'scantron' && $response=~/\S/) {
  223: 	      $response = $opt[$response];
  224: 	  }
  225: 	  if ( $response =~ /[^\s]/) {
  226: 	    $responsehash{$name}=$response;
  227: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  228: 	    &Apache::lonxml::debug("submitted a $response for $value<br />\n");
  229: 	    if ($value eq $response) {
  230: 		$grade{$name}='1'; $right++;
  231: 	    } else {
  232: 		$grade{$name}='0'; $wrong++;
  233: 	    }
  234: 	  } else {
  235: 	    $ignored++;
  236: 	  }
  237: 	  $temp++;
  238: 	}
  239: 	my $part=$Apache::inputtags::part;
  240: 	my $id = $Apache::inputtags::response['-1'];
  241: 	my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  242: 	my $gradestr   =&Apache::lonnet::hash2str(%grade);
  243: 	my %previous=&Apache::response::check_for_previous($responsestr,
  244: 							   $part,$id);
  245: 	&Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
  246: 	$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  247: 	    $responsestr;
  248: 	$Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=$gradestr;
  249: 	
  250: 	if ($Apache::lonhomework::type eq 'survey') {
  251: 	    if ($ignored == 0) {
  252: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  253: 		&Apache::response::handle_previous(\%previous,$ad);
  254: 	    } elsif ($wrong==0 && $right==0) {
  255: 	    } else {
  256: 		my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
  257: 		&Apache::response::handle_previous(\%previous,$ad);
  258: 	    }
  259: 	} elsif (!$Apache::lonhomework::scantronmode) {
  260: 	    my $ad;
  261: 	    if ($wrong==0 && $ignored==0) {
  262: 		$ad='EXACT_ANS';
  263: 	    } elsif ($wrong==0 && $right==0) {
  264: 		#nothing submitted
  265: 	    } else {
  266: 		if ($ignored==0) {
  267: 		    $ad='INCORRECT';
  268: 		} else {
  269: 		    $ad='MISSING_ANSWER';
  270: 		}
  271: 	    }
  272: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  273: 	    &Apache::response::handle_previous(\%previous,$ad);
  274: 	} else {
  275: 	    my $ad;
  276: 	    if ($wrong==0 && $right==0) {
  277: 		#nothing submitted only assign a score if we 
  278: 		#need to override a previous grade
  279: 		if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
  280: 		    $ad='ASSIGNED_SCORE';
  281: 		}
  282: 	    } else {
  283: 		$ad='ASSIGNED_SCORE';
  284: 	    }
  285: 	    $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  286: 	    $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
  287: 		$right/(scalar(@whichopt));
  288: 	    $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
  289: 		scalar(@whichopt);
  290: 	}
  291:       }
  292:     }
  293:     my $part_id     = $Apache::inputtags::part;
  294:     my $response_id = $Apache::inputtags::response[-1];
  295:     &Apache::lonxml::increment_counter(&getfoilcounts($max),
  296: 				       "$part_id.$response_id");
  297:     if ($target eq 'analyze') {
  298: 	&Apache::lonhomework::set_bubble_lines();
  299:     }
  300: 
  301:   } elsif ($target eq 'edit') {
  302:     $result.=&Apache::edit::end_table();
  303:   }
  304:   if ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  305:     if($env{'form.pdfFormFields'} ne 'yes') {
  306:       $result .= '\end{enumerate}';
  307:     } else {
  308:       $result .= "\\\\";
  309:     }
  310:   }
  311:   &Apache::response::poprandomnumber();
  312:   return $result;
  313: }
  314: 
  315: sub getfoilcounts {
  316:   my ($max)=@_;
  317:   # +1 since instructors will count from 1
  318:   my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
  319:   if (&Apache::response::showallfoils()) { $max=$count; }
  320:   if ($count>$max) { $count=$max } 
  321:   &Apache::lonxml::debug("Count is $count from $max");
  322:   return $count;
  323: }
  324: 
  325: sub whichfoils {
  326:     my ($max,$randomize)=@_;
  327:     return &Apache::response::whichorder($max,$randomize,
  328: 					 &Apache::response::showallfoils(),
  329: 					 \%Apache::response::foilgroup);
  330: }
  331: 
  332: sub displayanswers {
  333:     my ($max,$randomize,@opt)=@_;
  334:     if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  335:     my @names = @{ $Apache::response::foilgroup{'names'} };
  336:     my @whichopt = &whichfoils($max,$randomize);
  337:     my $result;
  338:     if ($Apache::lonhomework::type eq 'exam') {
  339: 	my $i = 0;
  340: 	my %opt = map { ($_,$i++) } @opt;
  341: 	
  342: 	$i = 0;
  343: 	my @alphabet = ('A'..'Z');
  344: 	foreach my $name (@whichopt) {
  345: 	    $result.=&Apache::response::answer_header('optionresponse',$i++);
  346: 	    $result.=&Apache::response::answer_part('optionresponse',
  347: 						    $alphabet[$opt{$Apache::response::foilgroup{$name.'.value'}}]);
  348: 	    $result.=&Apache::response::answer_part('optionresponse',
  349: 						    $Apache::response::foilgroup{$name.'.value'});
  350: 	    $result.=&Apache::response::answer_footer('optionresponse');
  351: 	}
  352:     } else {
  353: 	$result=&Apache::response::answer_header('optionresponse');
  354: 	foreach my $name (@whichopt) {
  355: 	    $result.=&Apache::response::answer_part('optionresponse',
  356: 						    $Apache::response::foilgroup{$name.'.value'});
  357: 	}
  358: 	$result.=&Apache::response::answer_footer('optionresponse');
  359:     }
  360:     return $result;
  361: }
  362: 
  363: sub check_box_opt {
  364:     my ($target,$checkboxvalue,@opt)=@_;
  365: # Check if we are in checkbox mode: checkboxvalue specified, on web, only two options.
  366: # If so, return "checked" value
  367:     if ($#opt!=1) { return ''; }
  368:     if ($target ne 'web') { return ''; }
  369:     return $checkboxvalue;
  370: }
  371: 
  372: sub check_for_invalid {
  373:     my ($names,$options) = @_;
  374:     my %bad_names;
  375:     foreach my $name (@{ $names }) {
  376: 	my $value=$Apache::response::foilgroup{$name.'.value'};
  377: 	my $found=0;
  378: 	foreach my $option (@{ $options }) {
  379: 	    if ($value eq $option) { $found=1; }
  380: 	}
  381: 	if (!$found) { $bad_names{$name}=$value; }
  382:     }
  383:     if (%bad_names) {
  384: 	my $error=&mt('The question can not be gotten correct, '.
  385: 	    'the following foils in the &lt;optionresponse&gt; '.
  386: 	    'have invalid correct options').' <br /><tt>'.
  387: 	    join('<br />',(map { $_=&mt("[_1] with value [_2]",$_,$bad_names{$_}) } (keys(%bad_names)))).
  388: 	    "</tt>";
  389: 	&Apache::lonxml::error($error);
  390:     }
  391: }
  392: 
  393: sub displayfoils {
  394:   my ($target,$max,$randomize,$TeXlayout,$checkboxvalue,@opt)=@_;
  395:   if (!defined(@{ $Apache::response::foilgroup{'names'} })) {return;}
  396:   my @names = @{ $Apache::response::foilgroup{'names'} };
  397:   my @truelist;
  398:   my @falselist;
  399:   my $result;  
  400:   my $name;
  401:   my $displayoptionintex=1;
  402:   my @alphabet = ('A'..'Z');
  403:   my @whichopt = &whichfoils($max,$randomize);
  404:   &check_for_invalid(\@whichopt,\@opt);
  405:   my $part=$Apache::inputtags::part;
  406:   my $id=$Apache::inputtags::response[-1];
  407:   my $break;
  408:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  409:   if ( ($target ne 'tex') &&
  410:        &Apache::response::show_answer() ) {
  411:     my $temp=1;
  412:     foreach $name (@whichopt) {
  413: 	my $text=$Apache::response::foilgroup{$name.'.text'};
  414: 	my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  415: 	my $lastopt=$lastresponse{$name};
  416: 	if ($text!~/^\s*$/) { $break='<br />'; }
  417: 	$result.=$break;
  418: 	if ($target eq 'web') {
  419: 	    my $value=$Apache::response::foilgroup{$name.'.value'};
  420: 	    if (!($text=~s|<drawoptionlist\s*/>|$value|)) {
  421: 		if ($text=~/^\s*$/) {
  422: 		    $text=$value.$text;
  423: 		} else {
  424: 		    $text='<b>'.$value.':</b> '.$text;
  425: 		}
  426: 	    } else {
  427: 		$text='&#149;'.$text;
  428: 	    }
  429: 	    $result.=$text."\n";
  430: 	}
  431:       if ($Apache::lonhomework::type eq 'exam') {
  432: 	  $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  433:       }
  434:       $temp++;
  435:     }
  436:   } else {
  437:     my $temp=1;
  438:     my %lastresponse=&Apache::lonnet::str2hash($Apache::lonhomework::history{"resource.$part.$id.submission"});
  439:     my $internal_counter=$Apache::lonxml::counter;
  440:     my $checkboxopt=&check_box_opt($target,$checkboxvalue,@opt);
  441:     if ($checkboxopt) {
  442:        $result.='<br />'.&mt('Choices: ').'<b>'.$opt[0].','.$opt[1].'</b>. '.
  443:                 &mt('Select all that are <b>[_1]</b>.',$checkboxopt);
  444:     }
  445:     foreach $name (@whichopt) {
  446:       my $text=$Apache::response::foilgroup{$name.'.text'};
  447:       if ($text!~/^\s*$/) {
  448: 	  if ($target eq 'tex') {
  449: 	      $break='\vskip 0 mm ';
  450: 	  } elsif ($target eq 'web') {
  451: 	      $break='<br />';
  452: 	  }
  453:       }
  454:       my $lastopt=$lastresponse{$name};
  455:       my $optionlist="<option></option>\n";
  456: 
  457:       if($target eq 'tex' and $env{'form.pdfFormFields'} eq 'yes'
  458:          && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  459:           my $fieldname = $env{'request.symb'}.'&part_'.$Apache::inputtags::part.'&optionresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'].':'.$temp;
  460:           $optionlist =  &Apache::lonxml::print_pdf_start_combobox($fieldname);
  461:       }
  462: 
  463:       foreach my $option (@opt) {
  464: 	  my $escopt=&HTML::Entities::encode($option,'\'"&<>');
  465:           if ($option eq $lastopt) {
  466:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  467:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  468:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  469:               } else {
  470:                   $optionlist.="<option value='".$escopt."' selected=\"selected\">$option</option>\n";
  471:               }
  472:           } else {
  473:               if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  474:                   && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  475:                   $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
  476:               } else {
  477:                   $optionlist.="<option value='".$escopt."'>$option</option>\n";
  478:               }
  479:           }
  480:       }
  481:       if ($target ne 'tex') {
  482: 	  if ($Apache::lonhomework::type ne 'exam') {
  483: # we are on the web, this is not an exam, and the problem can be answered
  484:               if ($checkboxopt) {
  485: # generate checkboxes
  486:                   my $fieldname=$Apache::inputtags::response['-1'].':'.$temp;
  487:                   my $altopt=$opt[0];
  488:                   if ($opt[0] eq $checkboxopt) {
  489:                      $altopt=$opt[1];
  490:                   }
  491:                   my $defopt=$lastopt;
  492:                   unless ($defopt) { $defopt=$altopt; }
  493:                   my $escdefopt=&HTML::Entities::encode($defopt,'\'"&<>');
  494:                   my $esccheckboxopt=&HTML::Entities::encode($checkboxopt,'\'"&<>');
  495:                   my $escaltopt=&HTML::Entities::encode($altopt,'\'"&<>');
  496: # checkboxopt is how the box is labelled
  497: # altopt is the alternative option
  498: # lastopt is what the user submitted before
  499: # defopt is what the field is going to start out with: either previous choice or altopt
  500: # fieldname is this input field's name after HWVAL_
  501:                   $optionlist='<input type="hidden" name="HWVAL_'.$fieldname.'" value="'.$escdefopt.'" />'.
  502:                   '<input type="checkbox" name="HWCHK_'.$fieldname.'" onclick="javascript:if (this.form.elements[\'HWCHK_'.
  503:                   $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";
  504:               } else {
  505: # classic selection list
  506: 	          $optionlist='<select onchange="javascript:setSubmittedPart(\''.
  507: 		  $part.'\');" name="HWVAL_'.
  508: 		  $Apache::inputtags::response['-1'].':'.$temp.'">'.
  509: 		  $optionlist."</select>\n";
  510:               }
  511: 	  } else {
  512: 	      $optionlist='<u>'.('&nbsp;'x10).'</u>';
  513: 	  }
  514: 	  if ($text=~s|<drawoptionlist\s*/>|$optionlist|) {
  515: 	      if ($Apache::lonhomework::type ne 'exam') {
  516: 		  $text='&#149;'.$text;
  517: 	      }
  518: 	  } else {
  519: 	      if ($Apache::lonhomework::type ne 'exam') {
  520: 		  $text=$optionlist.$text;
  521: 	      }
  522: 	  }
  523: 	  $result.=$break.$text."\n";
  524: 	  if ($Apache::lonhomework::type eq 'exam') {
  525: 	      $result.=&webbubbles(\@opt,\@alphabet,$temp,$lastopt);
  526: 	  }
  527: 	  $temp++;
  528:       } else {
  529: 	  my $texoptionlist='';
  530: 	  if ($displayoptionintex &&
  531: 	      $Apache::lonhomework::type ne 'exam') {
  532: 	      $texoptionlist = &optionlist_correction($TeXlayout,@opt);
  533: 	  }
  534: 	  if ($text=~/<drawoptionlist\s*\/>/) {
  535: 	      $text=~s|<drawoptionlist\s*\/>| \\makebox\[0\.3in\]\[b\]\{\\hrulefill\} |g;
  536: 	  }
  537: 
  538: 	  if ($text=~m/\\item /) {
  539: 	      if ($Apache::lonhomework::type eq 'exam') {
  540: 	          $text=~s/\\item/\\vskip 2 mm/;
  541: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
  542:                   $result.= $texoptionlist.$text;
  543:               }
  544:           } else {
  545: 	      if ($Apache::lonhomework::type eq 'exam') {
  546: 		  $result.= $texoptionlist.'  '.$text;
  547: 	      } elsif ($env{'form.pdfFormFields'} ne 'yes') {
  548: 		  if ($text=~/\S/) {
  549:                       $result.= $texoptionlist.'\vspace*{-2 mm}\item '.$text;
  550:                   } else {
  551:                       $result.= $texoptionlist;
  552:                   }
  553: 	      }
  554: 	  }
  555: 	  if ($Apache::lonhomework::type eq 'exam') {
  556: 	      $result.='\vskip -1 mm\noindent\begin{enumerate}\item[\textbf{'.
  557: 		  $internal_counter.'}.]'.&bubbles(\@alphabet,\@opt).
  558: 		  '\end{enumerate} \vskip -8 mm \strut ';
  559: 	      $internal_counter++;
  560: 	  }
  561:           if ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
  562:               && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  563:               $text =~ s/\\item//m;
  564:               $result .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut \\\\';
  565:               $temp++;
  566:           }
  567: 	  $displayoptionintex=0;
  568:       }
  569:     }
  570:   }
  571: 
  572:   if ($target eq 'web') {
  573:       &Apache::response::setup_prior_tries_hash(\&Apache::rankresponse::format_prior_answer,
  574: 						[\@whichopt,
  575: 						 'submissiongrading']);
  576:   }
  577: 
  578:   if ($target ne 'tex') {
  579:       return $result.$break;
  580:   } else {
  581:       return $result;
  582:   }
  583: }
  584: 
  585: 
  586: sub optionlist_correction {
  587:     my ($TeXlayout,@options) = @_;
  588:     my $texoptionlist='\\item [] '.&mt('Choices:').' ';
  589:     if ($TeXlayout eq 'vertical') {$texoptionlist='\\item []';}
  590:     if (scalar(@options) > 0) {
  591: 	foreach my $option (@options) {
  592: 	    $texoptionlist.='{\bf '.
  593: 		&Apache::lonxml::latex_special_symbols($option).
  594: 		'}';
  595: 	    if ($TeXlayout eq 'vertical') {
  596: 		$texoptionlist.=' \vskip 0 mm ',
  597: 	    } else {
  598: 		$texoptionlist.=', ';
  599: 	    }
  600: 	}
  601: 	$texoptionlist=~s/, $//;
  602: 	if ($TeXlayout ne 'vertical') {$texoptionlist.='.';}
  603:     } else {
  604: 	if ($TeXlayout ne 'vertical') {$texoptionlist='\\item [] \\vskip -5 mm';}
  605:     }
  606:     return $texoptionlist;
  607: }
  608: 
  609: 
  610: sub webbubbles {
  611:     my ($ropt,$ralphabet,$temp,$lastopt)=@_;
  612:     my @opt=@$ropt; 
  613:     my @alphabet=@$ralphabet;
  614:     my $result='';
  615:     my $number_of_bubbles = $#opt + 1;
  616:     $result.= '<table border="1"><tr>';
  617:     for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  618: 	my $checked='';
  619: 	if ($lastopt eq $opt[$ind]) {
  620: 	    $checked=' checked="on" ';
  621: 	}
  622: 	$result.='<td><input type="radio" name="HWVAL_'.
  623: 	    $Apache::inputtags::response['-1'].':'.$temp.
  624: 	    '" value="'.$opt[$ind].'" '.$checked.' />';
  625: 	if ($alphabet[$ind]) {
  626: 	    $result.=$alphabet[$ind].': ';
  627: 	}
  628: 	$result.=$opt[$ind].'</td>';
  629:     }
  630:     $result.='</tr></table>';
  631:     return $result;
  632: }
  633: 
  634: 
  635: sub bubbles {
  636:     my ($ralphabet,$ropt,$response, $max_width) = @_;
  637:     my @alphabet = @$ralphabet;
  638:     my @opt = @$ropt;
  639:     my ($result,$head,$line) =('','','');
  640:     my $number_of_bubbles = $#opt + 1;
  641:     my $current_length = 0;
  642:     my $textwidth;
  643:     if (defined($max_width)) {
  644: 	$textwidth=$max_width;
  645: 	&Apache::lonxml::debug("Max width passed in: $max_width");
  646:     } elsif ($env{'form.textwidth'} ne '') {
  647: 	$env{'form.textwidth'}=~/(\d+)/;
  648: 	$textwidth=$1;
  649: 	&Apache::lonxml::debug("Max width from form: $textwidth");
  650:     } else {
  651: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
  652: 	$textwidth=$1.'.'.$2;
  653: 	&Apache::lonxml::debug("Max width defaults? $textwidth");
  654:     }
  655:     &Apache::lonxml::debug("Final maxwidth: $textwidth");
  656:     for (my $ind=0;$ind<=$number_of_bubbles;$ind++) {
  657: 	my $leftmargin;
  658: 	$opt[$ind]=&Apache::lonxml::latex_special_symbols($opt[$ind]);
  659: 	if ($response eq 'rankresponse') {$opt[$ind]='Rank '.$opt[$ind];}
  660: 	if ($ind==0) {$leftmargin=6;} else {$leftmargin=10;}
  661: 
  662: 	$current_length += (length($opt[$ind])+length($alphabet[$ind])+4)*2;
  663: 	if ($current_length<($textwidth-$leftmargin) and $ind!=$number_of_bubbles) {
  664: 	    $line.='\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';
  665: 	    $head.='lr';
  666: 	} else {
  667: 	    $line=~s/\&\s*$//;
  668: 	    $result.='\vskip -2 mm\noindent\setlength{\tabcolsep}{2 mm}\begin{tabular}{'.$head.'}'.$line.'\\\\\end{tabular}\vskip 0 mm';
  669: 	    $line = '\hskip -1 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -1 mm & \hskip -3 mm {\small '.$opt[$ind].'} & ';;
  670: 	    $head ='lr';
  671: 	    $current_length = (length($opt[$ind])+length($alphabet[$ind]))*2;
  672: 	}
  673: 
  674:     }
  675:     return $result;
  676: }
  677: 
  678: 
  679: sub start_conceptgroup {
  680:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  681:   $Apache::optionresponse::conceptgroup=1;
  682:   %Apache::response::conceptgroup=();
  683:   my $result;
  684:   if ($target eq 'edit') {
  685:     $result.=&Apache::edit::tag_start($target,$token,"Concept Grouped Foils");
  686:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  687:         &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  688:   }
  689:   if ($target eq 'modified') {
  690:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'concept');
  691:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  692:   }
  693:   return $result;
  694: }
  695: 
  696: sub end_conceptgroup {
  697:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  698:   $Apache::optionresponse::conceptgroup=0;
  699:   my $result='';
  700:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  701:       $target eq 'tex' || $target eq 'analyze') {
  702:     #if not there aren't any foils to display and thus no question
  703:       &Apache::response::pick_foil_for_concept($target,
  704: 					       ['value','text','location'],
  705: 					       \%Apache::hint::option,
  706: 					       $parstack,$safeeval);
  707:   } elsif ($target eq 'edit') {
  708:     $result=&Apache::edit::end_table();
  709:   }
  710:   return $result;
  711: }
  712: 
  713: sub insert_conceptgroup {
  714:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  715:   return $result;
  716: }
  717: 
  718: sub start_foil {
  719:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  720:   my $result='';
  721:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
  722:       &Apache::lonxml::startredirection;
  723:       if ($target eq 'analyze') {
  724: 	  &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  725:       }
  726:   } elsif ($target eq 'edit') {
  727:     $result=&Apache::edit::tag_start($target,$token,"Foil");
  728:     my $level='-2';
  729:     if ($$tagstack['-2'] eq 'conceptgroup') { $level = '-3'; }
  730:     my @opt;
  731:     eval '@opt ='.&Apache::lonxml::get_param('options',$parstack,$safeeval,$level);
  732:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  733:     $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',
  734: 					       ['unused',(@opt)],$token,'15');
  735:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  736: 					     $safeeval,'-3');
  737:     if ($randomize ne 'no') {
  738: 	$result.=&Apache::edit::select_arg('Location:','location',
  739: 					   ['random','top','bottom'],$token);
  740:     }
  741:     $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  742:   } elsif ($target eq 'modified') {
  743:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  744: 						 'value','name','location');
  745:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  746:   } 
  747:   return $result;
  748: }
  749: 
  750: sub end_foil {
  751:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  752:   my $text ='';
  753:   my $result = '';
  754:   if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') { 
  755:       $text=&Apache::lonxml::endredirection;
  756:       if ($target eq 'tex') {$text=~s/\\strut\s*\\\\\s*\\strut/\\vskip 0 mm/;}
  757:       if (($target eq 'tex') and ($Apache::lonhomework::type ne 'exam') and ($text=~/\S/)) {$text = '\vspace*{-2 mm}\item '.$text;} 
  758:   }
  759:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' 
  760:       || $target eq 'tex' || $target eq 'analyze') {
  761:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  762:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  763: 	$text='\vskip 5mm $\triangleright$ '.$text;
  764:     }
  765:     if ($value ne 'unused') {
  766:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  767:       &Apache::lonxml::debug("Got a name of :$name:");
  768:       if ($name eq "") {
  769: 	  &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  770: 	  $name=$Apache::lonxml::curdepth;
  771:       }
  772:       &Apache::lonxml::debug("Using a name of :$name:");
  773:       if (defined($Apache::response::foilnames{$name})) {
  774: 	  &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
  775:       }
  776:       $Apache::response::foilnames{$name}++;
  777:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
  778:       if ( $Apache::optionresponse::conceptgroup
  779: 	   && !&Apache::response::showallfoils() ) {
  780: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  781: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  782: 	$Apache::response::conceptgroup{"$name.text"} = $text;
  783: 	$Apache::response::conceptgroup{"$name.location"} = $location;
  784:       } else {
  785: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  786: 	$Apache::response::foilgroup{"$name.value"} = $value;
  787: 	$Apache::response::foilgroup{"$name.text"} = $text;
  788: 	$Apache::response::foilgroup{"$name.location"} = $location;
  789:       }
  790:     }
  791:   }
  792:   if ($target eq 'edit') {
  793:     $result.= &Apache::edit::tag_end($target,$token,'');
  794:   }
  795:   return $result;
  796: }
  797: 
  798: sub start_drawoptionlist {
  799:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  800:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
  801: 	return $token->[4];
  802:     }
  803: }
  804: 
  805: sub end_drawoptionlist {
  806:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  807:     if ($target !~ /^(?:meta|answer|modified|edit)$/) {
  808: 	return $token->[2];
  809:     }
  810: }
  811: 
  812: sub insert_foil {
  813:     return '
  814: <foil name="" value="unused">
  815: <startouttext />
  816: <endouttext />
  817: </foil>';
  818: }
  819: 
  820: sub insert_drawoptionlist {
  821:     return '<drawoptionlist />';
  822: }
  823: 1;
  824: __END__
  825:  
  826: =head1 NAME
  827: 
  828: Apache::optionresponse.pm;
  829: 
  830: =head1 SYNOPSIS
  831: 
  832: Handles tags associated with showing a list of
  833: options.
  834: 
  835: This is part of the LearningOnline Network with CAPA project
  836: described at http://www.lon-capa.org.
  837: 
  838: =head1 HANDLER SUBROUTINE
  839: 
  840: start_optionresponse()
  841: 
  842: =head1 OTHER SUBROUTINES
  843: 
  844: =over
  845: 
  846: =item end_optionresponse()
  847: 
  848: =item start_foilgroup()
  849: 
  850: =item end_foilgroup()
  851: 
  852: =item getfoilcounts()
  853: 
  854: =item displayanswers()
  855: 
  856: =item check_for_invalid()
  857: 
  858: =item displayfoils()
  859: 
  860: =item optionlist_correction()
  861: 
  862: =item webbubbles()
  863: 
  864: =item bubbles()
  865: 
  866: =item start_conceptgroup()
  867: 
  868: =item end_conceptgroup()
  869: 
  870: =item insert_conceptgroup()
  871: 
  872: =item start_foil()
  873: 
  874: =item end_foil()
  875: 
  876: =item start_drawoptionlist()
  877: 
  878: =item end_drawoptionlist()
  879: 
  880: =item insert_foil()
  881: 
  882: =item insert_drawoptionlist()
  883: 
  884: =back
  885: 
  886: =cut

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