File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.152: download - view: text, annotated - select for diffs
Fri Sep 16 22:23:54 2011 UTC (12 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
- Bug 5771
  - Fix underlying cause in lonnet::hashref2str().
  - Preferred to earlier fix (now reverted).
    - Revert earlier changes in *response.pm to report foil name of 0
      as an error.

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: #
    4: # $Id: radiobuttonresponse.pm,v 1.152 2011/09/16 22:23:54 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: 
   29: 
   30: package Apache::radiobuttonresponse;
   31: use strict;
   32: use HTML::Entities();
   33: use Apache::lonlocal;
   34: use Apache::lonnet;
   35: use Apache::response;
   36: 
   37: my $default_bubbles_per_line = 10;
   38: 
   39: 
   40: BEGIN {
   41:     &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   42: }
   43: 
   44: sub bubble_line_count {
   45:     my ($numfoils, $bubbles_per_line) = @_;
   46:     my $bubble_lines;
   47:     $bubble_lines = int($numfoils / $bubbles_per_line);
   48:     if (($numfoils % $bubbles_per_line) != 0) {
   49: 	$bubble_lines++;
   50:     }
   51:     return $bubble_lines;
   52:     
   53: }
   54: 
   55: 
   56: sub start_radiobuttonresponse {
   57:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   58:     my $result;
   59: 
   60:     #when in a radiobutton response use these
   61:     &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
   62:     push (@Apache::lonxml::namespace,'radiobuttonresponse');
   63:     my $id = &Apache::response::start_response($parstack,$safeeval);
   64: 
   65:     %Apache::hint::radiobutton=();
   66:     undef(%Apache::response::foilnames);
   67:     if ($target eq 'meta') {
   68: 	$result=&Apache::response::meta_package_write('radiobuttonresponse');
   69:     } elsif ($target eq 'edit' ) {
   70: 	$result.=&Apache::edit::start_table($token)
   71:            .'<tr><td>'.&Apache::lonxml::description($token)
   72:            .&Apache::loncommon::help_open_topic('Radio_Response_Problems')
   73:            .'</td>'
   74:            .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
   75:            .&Apache::edit::deletelist($target,$token)
   76:            .'</span></td>'
   77:            .'<td>&nbsp;'.&Apache::edit::end_row()
   78:            .&Apache::edit::start_spanning_row();
   79: 	$result.=
   80: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',
   81: 				    $token,'4').'&nbsp;'x 3 .
   82: 	    &Apache::edit::select_arg('Randomize Foil Order:','randomize',
   83: 				      ['yes','no'],$token).'&nbsp;'x 3 .
   84: 	    &Apache::edit::select_arg('Display Direction:','direction',
   85: 				      ['vertical','horizontal'],$token).
   86: 				      &Apache::edit::end_row().
   87: 				      &Apache::edit::start_spanning_row()."\n";
   88:     } elsif ($target eq 'modified') {
   89: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   90: 						     $safeeval,'max',
   91: 						     'randomize','direction');
   92: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   93:     } elsif ($target eq 'tex') {
   94: 	my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,
   95: 					    undef,0);
   96: 	if ($type eq '1') {
   97: 	    $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
   98: 	} elsif ($type eq 'A') {
   99: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
  100: 	} elsif ($type eq 'a') {
  101: 	    $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
  102: 	} elsif ($type eq 'i') {
  103: 	    $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
  104: 	} else {
  105: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
  106: 	}
  107:         if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  108:             $result .= '\begin{itemize}';
  109:         } else {
  110:             $result .= '\begin{enumerate}';
  111:         }
  112:     } elsif ($target eq 'analyze') {
  113: 	my $part_id="$Apache::inputtags::part.$id";
  114:         $Apache::lonhomework::analyze{"$part_id.type"} = 'radiobuttonresponse';
  115: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
  116:     }
  117:     return $result;
  118: }
  119: 
  120: sub end_radiobuttonresponse {
  121:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  122:     my $result;
  123:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
  124:     if ($target eq 'tex' ) {
  125:         if($env{'form.pdfFormFields'} eq 'yes' and $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  126:             $result .= '\end{itemize}';
  127:         } else {
  128:             $result .= '\end{enumerate}';
  129:         }
  130:     }
  131:     &Apache::response::end_response;
  132:     pop @Apache::lonxml::namespace;
  133:     &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
  134:     undef(%Apache::response::foilnames);
  135:     return $result;
  136: }
  137: 
  138: %Apache::response::foilgroup=();
  139: sub start_foilgroup {
  140:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  141:     %Apache::response::foilgroup=();
  142:     $Apache::radiobuttonresponse::conceptgroup=0;
  143:     &Apache::response::pushrandomnumber(undef,$target);
  144:     return;
  145: }
  146: 
  147: sub storesurvey {
  148:     my ($style) = @_;
  149:     if ( !&Apache::response::submitted() ) { return ''; }
  150:     my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  151:     &Apache::lonxml::debug("Here I am!:$response:");
  152:     if ( $response !~ /[0-9]+/) { return ''; }
  153:     my $part = $Apache::inputtags::part;
  154:     my $id = $Apache::inputtags::response['-1'];
  155:     my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
  156:     my %responsehash;
  157:     $responsehash{$whichfoils[$response]}=$response;
  158:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  159:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  160: 	$responsestr;
  161:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
  162:     my $ad;
  163:     if ($style eq 'anonsurvey') {
  164:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
  165:     } elsif ($style eq 'anonsurveycred') {
  166:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
  167:     } elsif ($style eq 'surveycred') {
  168:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
  169:     } else {
  170:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  171:     }
  172:     &Apache::response::handle_previous(\%previous,$ad);
  173:     &Apache::lonxml::debug("submitted a $response<br />\n");
  174:     return '';
  175: }
  176: 
  177: 
  178: sub grade_response {
  179:     my ($answer, $whichfoils, $bubbles_per_line)=@_;
  180: 
  181:     if ( !&Apache::response::submitted() ) { return; }
  182:     my $response;
  183:     
  184:     if ($env{'form.submitted'} eq 'scantron') {
  185: 	$response = &Apache::response::getresponse(1,undef,
  186: 						   &bubble_line_count(scalar(@{ $whichfoils}),
  187: 								      $bubbles_per_line),
  188: 						   $bubbles_per_line);
  189: 
  190:     } else {
  191: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  192:     }
  193: 
  194: 
  195:     if ( $response !~ /[0-9]+/) { return; }
  196:     my $part=$Apache::inputtags::part;
  197:     my $id = $Apache::inputtags::response['-1'];
  198:     my %responsehash;
  199:     $responsehash{$whichfoils->[$response]}=$response;
  200:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  201:     my %previous=&Apache::response::check_for_previous($responsestr,
  202: 						       $part,$id);
  203:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  204: 	$responsestr;
  205:     &Apache::lonxml::debug("submitted a $response<br />\n");
  206:     my $ad;
  207:     if ($response == $answer) {
  208: 	$ad='EXACT_ANS';
  209:     } else {
  210: 	$ad='INCORRECT';
  211:     }
  212:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  213:     &Apache::response::handle_previous(\%previous,$ad);
  214: }
  215: 
  216: sub end_foilgroup {
  217:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  218: 
  219:     my $result;
  220:     my $bubble_lines;
  221:     my $answer_count;
  222:     my $id   = $Apache::inputtags::response['-1'];
  223:     my $part = $Apache::inputtags::part;
  224:     my $bubbles_per_line = &getbubblesnum($part,$id);
  225: 
  226:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  227: 	$target eq 'tex' || $target eq 'analyze') {
  228: 	my $style = $Apache::lonhomework::type;
  229: 	my $direction = &Apache::lonxml::get_param('direction',$parstack,
  230: 						   $safeeval,'-2');
  231: 	if ( (($style eq 'survey') || ($style eq 'surveycred') || 
  232:               ($style eq 'anonsurvey') || ($style eq 'anonsurveycred')) 
  233:              && ($target ne 'analyze')) {
  234: 	    if ($target eq 'web' || $target eq 'tex') {
  235: 		$result=&displayallfoils($direction, $target);
  236: 	    } elsif ( $target eq 'answer' ) {
  237: 		$result=&displayallanswers();
  238: 	    } elsif ( $target eq 'grade' ) {
  239: 		$result=&storesurvey($style);
  240: 	    }
  241: 	    $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
  242: 
  243: 	} else {
  244: 
  245: 	    my $name;
  246: 	    my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
  247: 						 '-2');
  248: 	    my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  249: 						       $safeeval,'-2');
  250: 	    my ($answer, @shown) = &whichfoils($max, $randomize);
  251: 	    $answer_count = scalar(@shown);
  252: 
  253: 	    if ($target eq 'web' || $target eq 'tex') {
  254:                 $result=&displayfoils($target,
  255: 				      $answer, \@shown,
  256: 				      $direction,
  257: 				      $bubbles_per_line);
  258: 	    } elsif ($target eq 'answer' ) {
  259: 		$result=&displayanswers($answer, \@shown, $bubbles_per_line);
  260: 	    } elsif ( $target eq 'grade') {
  261: 		&grade_response($answer, \@shown, $bubbles_per_line);
  262: 	    }  elsif ( $target eq 'analyze') {
  263: 		my $bubble_lines = &bubble_line_count($answer_count, 
  264: 						      $bubbles_per_line);
  265: 		&Apache::response::analyze_store_foilgroup(\@shown,
  266: 							   ['text','value','location']);
  267: 		my $part_id="$part.$id";
  268: 		push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
  269: 		      ('true','false'));
  270: 
  271: 	    }
  272: 	}
  273: 	$Apache::lonxml::post_evaluate=0;
  274:     }
  275:     if ($target eq 'web') {
  276: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  277: 						  [\%Apache::response::foilgroup]);
  278:     }
  279:     &Apache::response::poprandomnumber();
  280:     $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
  281:     &Apache::lonxml::increment_counter($bubble_lines,
  282: 				       "$part.$id");
  283:     if ($target eq 'analyze') {
  284: 	&Apache::lonhomework::set_bubble_lines();
  285:     }
  286:     return $result;
  287: }
  288: 
  289: sub getbubblesnum {
  290:     my ($part,$id) = @_;
  291:     my $bubbles_per_line;
  292:     my $default_numbubbles = $default_bubbles_per_line;
  293:     if (($env{'form.bubbles_per_row'} =~ /^\d+$/) &&
  294:         ($env{'form.bubbles_per_row'} > 0)) {
  295:         $default_numbubbles = $env{'form.bubbles_per_row'};
  296:     }
  297:     $bubbles_per_line =
  298:         &Apache::response::get_response_param($part."_$id",'numbubbles',
  299:                                               $default_numbubbles);
  300:     return $bubbles_per_line;
  301: }
  302: 
  303: sub getfoilcounts {
  304:     my @names;
  305:     my $truecnt=0;
  306:     my $falsecnt=0;
  307:     my $name;
  308:     if ( $Apache::response::foilgroup{'names'} ) {
  309: 	@names= @{ $Apache::response::foilgroup{'names'} };
  310:     }
  311:     foreach $name (@names) {
  312: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  313: 	    $truecnt++;
  314: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  315: 	    $falsecnt++;
  316: 	}
  317:     }
  318:     return ($truecnt,$falsecnt);
  319: }
  320: 
  321: sub format_prior_answer {
  322:     my ($mode,$answer,$other_data) = @_;
  323:     my $foil_data = $other_data->[0];
  324:     my %response = &Apache::lonnet::str2hash($answer);
  325:     my ($name)   = keys(%response);
  326:     return '<span class="LC_prior_radiobutton">'.
  327: 	$foil_data->{$name.'.text'}.'</span>';
  328: 
  329: }
  330: 
  331: sub displayallfoils {
  332:     my ($direction, $target)=@_;
  333:     my $result;
  334:     &Apache::lonxml::debug("survey style display");
  335:     my @names;
  336:     if ( $Apache::response::foilgroup{'names'} ) {
  337: 	@names= @{ $Apache::response::foilgroup{'names'} };
  338:     }
  339: 
  340:     my $temp=0;
  341:     my $i   =0;
  342:     my $id=$Apache::inputtags::response['-1'];
  343:     my $part=$Apache::inputtags::part;
  344:     my ($lastresponse,$newvariation,$showanswer);
  345:     if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  346:         ($Apache::lonhomework::type eq 'randomizetry')) &&
  347:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  348:         if ($env{'form.'.$part.'.rndseed'} ne
  349:             $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  350:             $newvariation = 1;
  351:         }
  352:     }
  353:     $showanswer = &Apache::response::show_answer();
  354:     unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) && (defined($env{'form.grade_symb'})) ||
  355:            ($newvariation && !$showanswer)) {
  356:         $lastresponse = 
  357: 	    $Apache::lonhomework::history{"resource.$part.$id.submission"};
  358:     }
  359:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
  360:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  361:     if ($showanswer) {
  362: 	foreach my $name (@names) {
  363: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  364: 		if (($direction eq 'horizontal') && ($target ne 'tex')) {
  365: 		    $result.="<td>";
  366: 		} else {
  367: 		    if ($target eq 'tex') {
  368: 			$result .= '\item \vskip -2mm ';
  369: 		    } else {
  370: 			$result.="<br />";
  371: 		    }
  372: 		}
  373: 		if (defined($lastresponse{$name})) {
  374: 		    if ($target eq 'tex') {
  375: 			$result .= '}';
  376: 		    } else {
  377: 			$result.='<b>';
  378: 		    }
  379: 		}
  380: 		$result .= $Apache::response::foilgroup{$name.'.text'};
  381: 		if (defined($lastresponse{$name}) && ($target ne 'tex')) {
  382: 		    $result.='</b>';
  383: 		}
  384: 		if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
  385: 	    }
  386: 	}
  387:     } else {
  388: 	foreach my $name (@names) {
  389: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  390: 		if ($direction eq 'horizontal') {
  391: 		    $result.="<td>";
  392: 		} else {
  393: 		    if ($target eq 'tex') {
  394: 		        if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  395:                             my $fieldname = $env{'request.symb'}.'&part_'. $Apache::inputtags::part
  396:                                             .'&radiobuttonresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'];
  397:                             $result .= '\item[{'.&Apache::lonxml::print_pdf_radiobutton($fieldname,$temp).'}]'
  398:                                        .$Apache::response::foilgroup{$name.'.text'}."\n";
  399:                         } else {
  400:                             $result .= '\item \vskip -2mm ';
  401:                         }
  402:                     } else {
  403: 			$result.="<br />";
  404: 		    }
  405: 		}
  406: 		if ($target eq 'tex') {
  407: 		    if($env{'form.pdfFormFields'} ne 'yes' or $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
  408:                         $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
  409:                     }
  410: 		    $i++;
  411: 		} else {
  412: 		    $result .= '<label>';
  413: 		    $result.="<input
  414:                        onchange=\"javascript:setSubmittedPart('$part');\"
  415:                        type=\"radio\"
  416:                        name=\"HWVAL_$Apache::inputtags::response['-1']\"
  417:                        value=\"$temp\"";
  418: 
  419: 		    if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
  420: 		    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
  421: 			'</label>';
  422: 		}
  423: 		$temp++;
  424: 		if ($target ne 'tex') {
  425: 		    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
  426: 		} else {
  427: 		    $result.='\vskip 0 mm ';
  428: 		}
  429: 	    }
  430: 	}
  431:     }
  432:     
  433:     if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
  434:     return $result;
  435: }
  436: 
  437: 
  438: sub whichfoils {
  439:     my ($max,$randomize)=@_;
  440: 
  441:     my @truelist;
  442:     my @falselist;
  443:     my @whichfalse =();
  444:     my ($truecnt,$falsecnt) = &getfoilcounts();
  445:     my $count=0;
  446:     # we will add in 1 of the true statements
  447:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
  448:     my $answer=int(&Math::Random::random_uniform() * ($count));
  449:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
  450:     my @names;
  451:     if ( $Apache::response::foilgroup{'names'} ) {
  452: 	@names= @{ $Apache::response::foilgroup{'names'} };
  453:     }
  454:     if (&Apache::response::showallfoils()) {
  455: 	@whichfalse=@names;
  456:     } elsif ($randomize eq 'no') {
  457: 	&Apache::lonxml::debug("No randomization");
  458: 	my $havetrue=0;
  459: 	foreach my $name (@names) {
  460: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  461: 		if (!$havetrue ) {
  462: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
  463: 		}
  464: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  465: 		push (@whichfalse,$name);
  466: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  467: 	    } else {
  468: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  469: 	    }
  470: 	}
  471: 	if ((!$havetrue) && 
  472:             ($Apache::lonhomework::type ne 'survey') && 
  473:             ($Apache::lonhomework::type ne 'surveycred') &&
  474:             ($Apache::lonhomework::type ne 'anonsurvey') &&
  475:             ($Apache::lonhomework::type ne 'anonsurveycred')) {
  476: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
  477: 	}
  478:     } else {
  479: 	my $current=0;
  480: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
  481: 	my (%top,%bottom);
  482: 	#first find out where everyone wants to be
  483: 	foreach my $name (@names) {
  484: 	    $current++;
  485: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  486: 		push (@truelist,$name);
  487: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  488: 		    $top{$name}=$current;
  489: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
  490: 		    $bottom{$name}=$current;
  491: 		}
  492: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  493: 		push (@falselist,$name);
  494: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  495: 		    $top{$name}=$current;
  496: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
  497: 		    $bottom{$name}=$current;
  498: 		}
  499: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  500: 	    } else {
  501: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  502: 	    }
  503: 	}
  504: 	#pick a true statement
  505: 	my $notrue=0;
  506: 	if (scalar(@truelist) == 0) { $notrue=1; }
  507: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
  508: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  509: 	my (@toplist, @bottomlist);
  510: 	my $topcount=0;
  511: 	my $bottomcount=0;
  512: 	# assign everyone to either toplist/bottomlist or whichfalse
  513: 	# which false is randomized, toplist bottomlist are in order
  514: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
  515: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
  516: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
  517: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  518: 	    $afalse=splice(@falselist,$afalse,1);
  519: 	    &Apache::lonxml::debug("Picked $afalse");
  520: 	    &Apache::lonhomework::showhash(('names'=>\@names));
  521: 	    &Apache::lonhomework::showhash(%top);
  522: 	    if ($top{$afalse}) {
  523: 		$toplist[$top{$afalse}]=$afalse;
  524: 		$topcount++;
  525: 	    } elsif ($bottom{$afalse}) {
  526: 		$bottomlist[$bottom{$afalse}]=$afalse;
  527: 		$bottomcount++;
  528: 	    } else {
  529: 		push (@whichfalse,$afalse);
  530: 	    }
  531: 	}
  532: 	&Apache::lonxml::debug("Answer wants $answer");
  533: 	my $truename=$truelist[$whichtrue];
  534: 	my $dosplice=1;
  535: 	if (($notrue) && 
  536:             ($Apache::lonhomework::type ne 'survey') &&
  537:             ($Apache::lonhomework::type ne 'surveycred') &&
  538:             ($Apache::lonhomework::type ne 'anonsurvey') &&
  539:             ($Apache::lonhomework::type ne 'anonsurveycred')) {
  540: 	    $dosplice=0;
  541: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
  542: 	}
  543: 	#insert the true statement, keeping track of where it wants to be
  544: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
  545: 	    $toplist[$top{$truename}]=$truename;
  546: 	    $answer=-1;
  547: 	    foreach my $top (reverse(@toplist)) {
  548: 		if ($top) { $answer++;}
  549: 		if ($top eq $truename) { last; }
  550: 	    }
  551: 	    $dosplice=0;
  552: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
  553: 	    $bottomlist[$bottom{$truename}]=$truename;
  554: 	    $answer=-1;
  555: 	    foreach my $bot (@bottomlist) {
  556: 		if ($bot) { $answer++;}
  557: 		if ($bot eq $truename) { last; }
  558: 	    }
  559: 	    $answer+=$topcount+$#whichfalse+1;
  560: 	    $dosplice=0;
  561: 	} else {
  562: 	    if ($topcount>0 || $bottomcount>0) {
  563:                 my $inc = 1;
  564:                 if (($bottomcount > 0) && ($Apache::lonhomework::type ne 'exam')) {
  565:                     $inc = 2;
  566:                 }
  567:                 $answer=int(&Math::Random::random_uniform() * ($#whichfalse+$inc))
  568:                         + $topcount;
  569: 	    }
  570: 	}
  571: 	&Apache::lonxml::debug("Answer now wants $answer");
  572: 	#add the top items to the top, bottom items to the bottom
  573: 	for (my $i=0;$i<=$#toplist;$i++) {
  574: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
  575: 	}
  576: 	for (my $i=0;$i<=$#bottomlist;$i++) {
  577: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
  578: 	}
  579: 	#if the true statement is randomized insert it into the list
  580: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
  581:     }
  582:     &Apache::lonxml::debug("Answer is $answer");
  583:     return ($answer,@whichfalse);
  584: }
  585: 
  586: sub displayfoils {
  587:     my ($target,$answer,$whichfoils,$direction, $bubbles_per_line)=@_;
  588:     my $result;
  589: 
  590:     my $part=$Apache::inputtags::part;
  591:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  592:     if ( ($target ne 'tex') &&
  593: 	 &Apache::response::show_answer() ) {
  594: 	if ($direction eq 'horizontal') {
  595: 	    if ($target ne 'tex') {
  596: 		$result.='<table><tr>';
  597: 	    }
  598: 	}
  599: 	foreach my $name (@{ $whichfoils }) {
  600: 	    if ($direction eq 'horizontal') {
  601: 		if ($target ne 'tex') { $result.='<td>'; }
  602: 	    }
  603: 	    if ($target ne 'tex') {
  604: 		$result.="<br />";
  605: 	    } else {
  606: 		$result.='\item \vskip -2 mm  ';
  607: 	    }
  608: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  609: 		if ($target ne 'tex') {
  610:                     $result.=&mt('Correct:').'<b>';
  611:                 } else {
  612:                     $result.=&mt('Correct:').' \textbf{';
  613:                 }
  614: 	    } else {
  615: 		$result.=&mt('Incorrect:');
  616: 	    }
  617: 	    if ($target eq 'web') { $result.="<label>"; }
  618: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
  619: 	    if ($target eq 'web') { $result.="</label>"; }
  620: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  621: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
  622: 	    }
  623: 	    if ($direction eq 'horizontal') {
  624: 		if ($target ne 'tex') { $result.='</td>'; }
  625: 	    }
  626: 	}
  627: 	if ($direction eq 'horizontal') {
  628: 	    if ($target ne 'tex') {
  629: 		$result.='</tr></table>';
  630: 	    }
  631: 	}
  632:     } else {
  633: 	my @alphabet = ('A'..'Z');
  634: 	my $i = 0;
  635: 	my $bubble_number = 0;
  636: 	my $line = 0;
  637: 	my $temp=0;  
  638: 	my $id=$Apache::inputtags::response['-1'];
  639: 	my $part=$Apache::inputtags::part;
  640:         my ($lastresponse,$newvariation);
  641:         if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  642:              ($Apache::lonhomework::type eq 'randomizetry')) && 
  643:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  644:             if ($env{'form.'.$part.'.rndseed'} ne
  645:                 $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  646:                 $newvariation = 1;
  647:             }
  648:         }
  649:         unless ($newvariation) {
  650: 	    $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  651:         }
  652: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  653: 	if ($target ne 'tex' && $direction eq 'horizontal') {
  654: 	    $result.="<table><tr>";
  655: 	}
  656:         my $numlines;
  657:         if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
  658:             my $numitems = scalar(@{ $whichfoils });
  659:             $numlines = int($numitems/$bubbles_per_line);
  660:             if (($numitems % $bubbles_per_line) != 0) {
  661:                 $numlines ++;
  662:             }
  663:             if ($numlines < 1) {
  664:                 $numlines = 1;
  665:             }
  666:             if ($numlines > 1) {
  667:                 my $linetext;
  668:                 for (my $i=0; $i<$numlines; $i++) {
  669:                     $linetext .= $Apache::lonxml::counter+$i.', ';
  670:                 }
  671:                 $linetext =~ s/,\s$//;
  672:                 $result .= '\item[\small {\textbf{'.$linetext.'}}]'.
  673:                            ' {\footnotesize '.
  674:                            &mt('(Bubble once in [_1] lines)',$numlines).
  675:                            '} \hspace*{\fill} \\\\';
  676:             } else {
  677:                 $result .= '\item[\textbf{'.$Apache::lonxml::counter.'}.]';
  678:             }
  679:         }
  680: 	foreach my $name (@{ $whichfoils }) {
  681: 	    if ($target ne 'tex') {
  682: 		if ($direction eq 'horizontal') {
  683: 		    $result.="<td>"; 
  684: 		} else { 
  685: 		    $result.="<br />";
  686: 		} 
  687: 	    }
  688: 	    if ($target ne 'tex') { 
  689:                 $result.= '<label>';
  690: 		$result.=
  691: 		    "<input type=\"radio\"
  692:                             onchange=\"javascript:setSubmittedPart('$part');\"
  693:                             name=\"HWVAL_$Apache::inputtags::response['-1']\"
  694:                             value=\"$temp\"";
  695: 		if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
  696: 		$result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
  697: 	    } else {
  698: 		if ($Apache::lonhomework::type eq 'exam') {
  699: 		    if($bubble_number >= $bubbles_per_line) {
  700: 			$line++;
  701: 			$i = 0;
  702: 			$bubble_number = 0;
  703: 		    }
  704:                     my $identifier;
  705:                     if ($numlines > 1) {
  706:                         $identifier = $Apache::lonxml::counter+$line;
  707:                     }
  708:                     $result .= '{\small \textbf{'.$identifier.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
  709: 		    $i++;
  710: 		    $bubble_number++;
  711: 		} else {
  712:                     if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  713:                         my $fieldname = $env{'request.symb'}.'&part_'. $Apache::inputtags::part
  714:                                         .'&radiobuttonresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'];
  715:                         $result .= '\item[{'.&Apache::lonxml::print_pdf_radiobutton($fieldname,$temp).'}]'
  716:                                    .$Apache::response::foilgroup{$name.'.text'}."\n";
  717:                     } else { 
  718:                         $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
  719:                     }
  720:                 }
  721: 	    }
  722: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
  723: 		$result.="</td>"; 
  724: 	    }
  725: 	    $temp++;
  726: 	}
  727: 	if ($target ne 'tex' && $direction eq 'horizontal') {
  728: 	    $result.="</tr></table>";
  729: 	}
  730:     }
  731:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
  732:     return $result;
  733: }
  734: 
  735: sub displayallanswers {
  736:     my @names;
  737:     if ( $Apache::response::foilgroup{'names'} ) {
  738: 	@names= @{ $Apache::response::foilgroup{'names'} };
  739:     }
  740:     my $result=&Apache::response::answer_header('radiobuttonresponse');
  741:     foreach my $name (@names) {
  742: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  743: 				$Apache::response::foilgroup{$name.'.value'});
  744:     }
  745:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
  746:     return $result;
  747: }
  748: 
  749: sub displayanswers {
  750:     my ($answer, $whichopt, $bubbles_per_line)=@_;
  751:     my $result;
  752: 
  753:     if ($Apache::lonhomework::type eq 'exam') {
  754: 	my $line = int($answer/$bubbles_per_line);
  755: 	my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
  756: 	$result .= &Apache::response::answer_header('radiobuttonresponse',
  757: 						    $line);
  758: 	$result .= &Apache::response::answer_part('radiobuttonresponse',
  759: 						  $correct);
  760:     } else {
  761: 	$result .= &Apache::response::answer_header('radiobuttonresponse');
  762:     }
  763:     foreach my $name (@{ $whichopt }) {
  764: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  765: 						$Apache::response::foilgroup{$name.'.value'});
  766:     }
  767:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
  768:     return $result;
  769: }
  770: 
  771: sub start_conceptgroup {
  772:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  773:     $Apache::radiobuttonresponse::conceptgroup=1;
  774:     %Apache::response::conceptgroup=();
  775:     my $result;
  776:     if ($target eq 'edit') {
  777: 	$result.=&Apache::edit::tag_start($target,$token);
  778: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  779: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  780:     } elsif ($target eq 'modified') {
  781: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  782: 						     $safeeval,'concept');
  783: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  784:     }
  785:     return $result;
  786: }
  787: 
  788: sub end_conceptgroup {
  789:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  790:     $Apache::radiobuttonresponse::conceptgroup=0;
  791:     my $result;
  792:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
  793: 	$target eq 'tex' || $target eq 'analyze') {
  794: 	&Apache::response::pick_foil_for_concept($target,
  795: 						 ['value','text','location'],
  796: 						 \%Apache::hint::radiobutton,
  797: 						 $parstack,$safeeval);
  798:     } elsif ($target eq 'edit') {
  799: 	$result=&Apache::edit::end_table();
  800:     }
  801:     return $result;
  802: }
  803: 
  804: sub insert_conceptgroup {
  805:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  806:     return $result;
  807: }
  808: 
  809: sub start_foil {
  810:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  811:     my $result='';
  812:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  813: 	&Apache::lonxml::startredirection;
  814: 	if ($target eq 'analyze') {
  815: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  816: 	}
  817:     } elsif ($target eq 'edit') {
  818: 	$result=&Apache::edit::tag_start($target,$token);
  819: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  820: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
  821: 						   ['unused','true','false'],
  822: 						   $token);
  823: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  824: 						 $safeeval,'-3');
  825: 	if ($randomize ne 'no') {
  826: 	    $result.=&Apache::edit::select_arg('Location:','location',
  827: 					       ['random','top','bottom'],$token);
  828: 	}
  829: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  830:     } elsif ($target eq 'modified') {
  831: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  832: 						     $safeeval,'value','name',
  833: 						     'location');
  834: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  835:     } 
  836:     return $result;
  837: }
  838: 
  839: sub end_foil {
  840:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  841:     my $text='';
  842:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  843: 	$text=&Apache::lonxml::endredirection;
  844:     }
  845:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
  846: 	|| $target eq 'tex' || $target eq 'analyze') {
  847: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  848: 	if ($value ne 'unused') {
  849: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  850: 	    if ($name eq "") {
  851: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  852: 		$name=$Apache::lonxml::curdepth;
  853: 	    }
  854: 	    if (defined($Apache::response::foilnames{$name})) {
  855: 		&Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
  856: 	    }
  857: 	    $Apache::response::foilnames{$name}++;
  858: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  859: 						     $safeeval);
  860: 	    if ( $Apache::radiobuttonresponse::conceptgroup
  861: 		 && !&Apache::response::showallfoils() ) {
  862: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  863: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  864: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
  865: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  866: 	    } else {
  867: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  868: 		$Apache::response::foilgroup{"$name.value"} = $value;
  869: 		$Apache::response::foilgroup{"$name.text"} = $text;
  870: 		$Apache::response::foilgroup{"$name.location"} = $location;
  871: 	    }
  872: 	}
  873:     }
  874:     return '';
  875: }
  876: 
  877: sub insert_foil {
  878:     return '
  879: <foil name="" value="unused">
  880: <startouttext />
  881: <endouttext />
  882: </foil>';
  883: }
  884: 
  885: 1;
  886: __END__
  887: 
  888: 
  889: 
  890: =head1 NAME
  891: 
  892: Apache::radiobuttonresponse
  893: 
  894: =head1 SYNOPSIS
  895: 
  896: Handles multiple-choice style responses.
  897: 
  898: This is part of the LearningOnline Network with CAPA project
  899: described at http://www.lon-capa.org.
  900: 
  901: =head1 SUBROUTINES
  902: 
  903: =over
  904: 
  905: =item start_radiobuttonresponse()
  906: 
  907: =item bubble_line_count()
  908: 
  909: =item end_radiobuttonresponse()
  910: 
  911: =item start_foilgroup()
  912: 
  913: =item storesurvey()
  914: 
  915: =item grade_response()
  916: 
  917: =item end_foilgroup()
  918: 
  919: =item getfoilcounts()
  920: 
  921: =item format_prior_answer()
  922: 
  923: =item displayallfoils()
  924: 
  925: =item &whichfoils($max,$randomize)
  926: 
  927: Randomizes the list of foils.
  928: Respects
  929:   - each foils desire to be randomized
  930:   - the existance of Concept groups of foils (select 1 foil from each)
  931:   - and selects a single correct statement from all possilble true statments
  932:   - and limits it to a toal of $max foils
  933: 
  934: WARNING: this routine uses the random number generator, it should only
  935: be called once per target, otherwise it can cause randomness changes in
  936: homework problems.
  937: 
  938: Arguments
  939:   $max - maximum number of foils to select (including the true one)
  940:          (so a max of 5 is: 1 true, 4 false)
  941: 
  942:   $randomize - whether to randomize the listing of foils, by default
  943:                will randomize, only if randomize is 'no' will it not
  944: 
  945: Returns
  946:   $answer - location in the array of the correct answer
  947:   @foils  - array of foil names in to display order
  948: 
  949: =item displayfoils()
  950: 
  951: =item displayallanswers()
  952: 
  953: =item displayanswers()
  954: 
  955: =item start_conceptgroup()
  956: 
  957: =item end_conceptgroup()
  958: 
  959: =item insert_conceptgroup()
  960: 
  961: =item start_foil()
  962: 
  963: =item end_foil()
  964: 
  965: =item insert_foil()
  966: 
  967: =back
  968: 
  969: =cut
  970:  

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