File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.147: download - view: text, annotated - select for diffs
Sun Dec 19 04:24:29 2010 UTC (13 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_0_RC2, HEAD
- New Question Type - randomizetry
  - Only check radiobutton checked in last try when displaying new try
    if rndseed for last try is the same as for current try.

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: #
    4: # $Id: radiobuttonresponse.pm,v 1.147 2010/12/19 04:24:29 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:     my $result;
  142:     %Apache::response::foilgroup=();
  143:     $Apache::radiobuttonresponse::conceptgroup=0;
  144:     &Apache::response::pushrandomnumber(undef,$target);
  145:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  146: 	$result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
  147:     }
  148:     return $result;
  149: }
  150: 
  151: sub storesurvey {
  152:     my ($style) = @_;
  153:     if ( !&Apache::response::submitted() ) { return ''; }
  154:     my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  155:     &Apache::lonxml::debug("Here I am!:$response:");
  156:     if ( $response !~ /[0-9]+/) { return ''; }
  157:     my $part = $Apache::inputtags::part;
  158:     my $id = $Apache::inputtags::response['-1'];
  159:     my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
  160:     my %responsehash;
  161:     $responsehash{$whichfoils[$response]}=$response;
  162:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  163:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  164: 	$responsestr;
  165:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
  166:     my $ad;
  167:     if ($style eq 'anonsurvey') {
  168:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
  169:     } elsif ($style eq 'anonsurveycred') {
  170:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
  171:     } elsif ($style eq 'surveycred') {
  172:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
  173:     } else {
  174:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  175:     }
  176:     &Apache::response::handle_previous(\%previous,$ad);
  177:     &Apache::lonxml::debug("submitted a $response<br />\n");
  178:     return '';
  179: }
  180: 
  181: 
  182: sub grade_response {
  183:     my ($answer, $whichfoils, $bubbles_per_line)=@_;
  184: 
  185:     if ( !&Apache::response::submitted() ) { return; }
  186:     my $response;
  187:     
  188:     if ($env{'form.submitted'} eq 'scantron') {
  189: 	$response = &Apache::response::getresponse(1,undef,
  190: 						   &bubble_line_count(scalar(@{ $whichfoils}),
  191: 								      $bubbles_per_line),
  192: 						   $bubbles_per_line);
  193: 
  194:     } else {
  195: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  196:     }
  197: 
  198: 
  199:     if ( $response !~ /[0-9]+/) { return; }
  200:     my $part=$Apache::inputtags::part;
  201:     my $id = $Apache::inputtags::response['-1'];
  202:     my %responsehash;
  203:     $responsehash{$whichfoils->[$response]}=$response;
  204:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  205:     my %previous=&Apache::response::check_for_previous($responsestr,
  206: 						       $part,$id);
  207:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  208: 	$responsestr;
  209:     &Apache::lonxml::debug("submitted a $response<br />\n");
  210:     my $ad;
  211:     if ($response == $answer) {
  212: 	$ad='EXACT_ANS';
  213:     } else {
  214: 	$ad='INCORRECT';
  215:     }
  216:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  217:     &Apache::response::handle_previous(\%previous,$ad);
  218: }
  219: 
  220: sub end_foilgroup {
  221:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  222: 
  223:     my $result;
  224:     my $bubble_lines;
  225:     my $bubbles_per_line;
  226:     my $answer_count;
  227:     my $id   = $Apache::inputtags::response['-1'];
  228:     my $part = $Apache::inputtags::part;
  229:     $bubbles_per_line = 
  230: 	&Apache::response::get_response_param($Apache::inputtags::part."_$id",
  231: 					      'numbubbles',
  232: 					      $default_bubbles_per_line);
  233: 
  234: 
  235:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  236: 	$target eq 'tex' || $target eq 'analyze') {
  237: 	my $style = $Apache::lonhomework::type;
  238: 	my $direction = &Apache::lonxml::get_param('direction',$parstack,
  239: 						   $safeeval,'-2');
  240: 	if ( (($style eq 'survey') || ($style eq 'surveycred') || 
  241:               ($style eq 'anonsurvey') || ($style eq 'anonsurveycred')) 
  242:              && ($target ne 'analyze')) {
  243: 	    if ($target eq 'web' || $target eq 'tex') {
  244: 		$result=&displayallfoils($direction, $target);
  245: 	    } elsif ( $target eq 'answer' ) {
  246: 		$result=&displayallanswers();
  247: 	    } elsif ( $target eq 'grade' ) {
  248: 		$result=&storesurvey($style);
  249: 	    }
  250: 	    $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
  251: 
  252: 	} else {
  253: 
  254: 	    my $name;
  255: 	    my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
  256: 						 '-2');
  257: 	    my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  258: 						       $safeeval,'-2');
  259: 	    my ($answer, @shown) = &whichfoils($max, $randomize);
  260: 	    $answer_count = scalar(@shown);
  261: 
  262: 	    if ($target eq 'web' || $target eq 'tex') {
  263: 		$result=&displayfoils($target,
  264: 				      $answer, \@shown,
  265: 				      $direction,
  266: 				      $bubbles_per_line);
  267: 	    } elsif ($target eq 'answer' ) {
  268: 		$result=&displayanswers($answer, \@shown, $bubbles_per_line);
  269: 	    } elsif ( $target eq 'grade') {
  270: 		&grade_response($answer, \@shown, $bubbles_per_line);
  271: 	    }  elsif ( $target eq 'analyze') {
  272: 		my $bubble_lines = &bubble_line_count($answer_count, 
  273: 						      $bubbles_per_line);
  274: 		&Apache::response::analyze_store_foilgroup(\@shown,
  275: 							   ['text','value','location']);
  276: 		my $part_id="$part.$id";
  277: 		push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
  278: 		      ('true','false'));
  279: 
  280: 	    }
  281: 	}
  282: 	$Apache::lonxml::post_evaluate=0;
  283:     }
  284:     if ($target eq 'web') {
  285: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  286: 						  [\%Apache::response::foilgroup]);
  287:     }
  288:     &Apache::response::poprandomnumber();
  289:     $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
  290:     &Apache::lonxml::increment_counter($bubble_lines,
  291: 				       "$part.$id");
  292:     if ($target eq 'analyze') {
  293: 	&Apache::lonhomework::set_bubble_lines();
  294:     }
  295:     return $result;
  296: }
  297: 
  298: sub getfoilcounts {
  299:     my @names;
  300:     my $truecnt=0;
  301:     my $falsecnt=0;
  302:     my $name;
  303:     if ( $Apache::response::foilgroup{'names'} ) {
  304: 	@names= @{ $Apache::response::foilgroup{'names'} };
  305:     }
  306:     foreach $name (@names) {
  307: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  308: 	    $truecnt++;
  309: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  310: 	    $falsecnt++;
  311: 	}
  312:     }
  313:     return ($truecnt,$falsecnt);
  314: }
  315: 
  316: sub format_prior_answer {
  317:     my ($mode,$answer,$other_data) = @_;
  318:     my $foil_data = $other_data->[0];
  319:     my %response = &Apache::lonnet::str2hash($answer);
  320:     my ($name)   = keys(%response);
  321:     return '<span class="LC_prior_radiobutton">'.
  322: 	$foil_data->{$name.'.text'}.'</span>';
  323: 
  324: }
  325: 
  326: sub displayallfoils {
  327:     my ($direction, $target)=@_;
  328:     my $result;
  329:     &Apache::lonxml::debug("survey style display");
  330:     my @names;
  331:     if ( $Apache::response::foilgroup{'names'} ) {
  332: 	@names= @{ $Apache::response::foilgroup{'names'} };
  333:     }
  334: 
  335:     my $temp=0;
  336:     my $i   =0;
  337:     my $id=$Apache::inputtags::response['-1'];
  338:     my $part=$Apache::inputtags::part;
  339:     my $lastresponse;
  340:     my ($lastresponse,$newvariation,$showanswer);
  341:     if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  342:         ($Apache::lonhomework::type eq 'randomizetry')) &&
  343:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  344:         if ($env{'form.'.$part.'.rndseed'} ne
  345:             $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  346:             $newvariation = 1;
  347:         }
  348:     }
  349:     $showanswer = &Apache::response::show_answer();
  350:     unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) && (defined($env{'form.grade_symb'})) ||
  351:            ($newvariation && !$showanswer)) {
  352:         $lastresponse = 
  353: 	    $Apache::lonhomework::history{"resource.$part.$id.submission"};
  354:     }
  355:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
  356:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  357:     if ($showanswer) {
  358: 	foreach my $name (@names) {
  359: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  360: 		if (($direction eq 'horizontal') && ($target ne 'tex')) {
  361: 		    $result.="<td>";
  362: 		} else {
  363: 		    if ($target eq 'tex') {
  364: 			$result .= '\item \vskip -2mm ';
  365: 		    } else {
  366: 			$result.="<br />";
  367: 		    }
  368: 		}
  369: 		if (defined($lastresponse{$name})) {
  370: 		    if ($target eq 'tex') {
  371: 			$result .= '}';
  372: 		    } else {
  373: 			$result.='<b>';
  374: 		    }
  375: 		}
  376: 		$result .= $Apache::response::foilgroup{$name.'.text'};
  377: 		if (defined($lastresponse{$name}) && ($target ne 'tex')) {
  378: 		    $result.='</b>';
  379: 		}
  380: 		if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
  381: 	    }
  382: 	}
  383:     } else {
  384: 	foreach my $name (@names) {
  385: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  386: 		if ($direction eq 'horizontal') {
  387: 		    $result.="<td>";
  388: 		} else {
  389: 		    if ($target eq 'tex') {
  390: 		        if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  391:                             my $fieldname = $env{'request.symb'}.'&part_'. $Apache::inputtags::part
  392:                                             .'&radiobuttonresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'];
  393:                             $result .= '\item[{'.&Apache::lonxml::print_pdf_radiobutton($fieldname,$temp).'}]'
  394:                                        .$Apache::response::foilgroup{$name.'.text'}."\n";
  395:                         } else {
  396:                             $result .= '\item \vskip -2mm ';
  397:                         }
  398:                     } else {
  399: 			$result.="<br />";
  400: 		    }
  401: 		}
  402: 		if ($target eq 'tex') {
  403: 		    if($env{'form.pdfFormFields'} ne 'yes' or $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
  404:                         $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
  405:                     }
  406: 		    $i++;
  407: 		} else {
  408: 		    $result .= '<label>';
  409: 		    $result.="<input
  410:                        onchange=\"javascript:setSubmittedPart('$part');\"
  411:                        type=\"radio\"
  412:                        name=\"HWVAL_$Apache::inputtags::response['-1']\"
  413:                        value=\"$temp\"";
  414: 
  415: 		    if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
  416: 		    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
  417: 			'</label>';
  418: 		}
  419: 		$temp++;
  420: 		if ($target ne 'tex') {
  421: 		    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
  422: 		} else {
  423: 		    $result.='\vskip 0 mm ';
  424: 		}
  425: 	    }
  426: 	}
  427:     }
  428:     
  429:     if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
  430:     return $result;
  431: }
  432: 
  433: 
  434: sub whichfoils {
  435:     my ($max,$randomize)=@_;
  436: 
  437:     my @truelist;
  438:     my @falselist;
  439:     my @whichfalse =();
  440:     my ($truecnt,$falsecnt) = &getfoilcounts();
  441:     my $count=0;
  442:     # we will add in 1 of the true statements
  443:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
  444:     my $answer=int(&Math::Random::random_uniform() * ($count));
  445:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
  446:     my @names;
  447:     if ( $Apache::response::foilgroup{'names'} ) {
  448: 	@names= @{ $Apache::response::foilgroup{'names'} };
  449:     }
  450:     if (&Apache::response::showallfoils()) {
  451: 	@whichfalse=@names;
  452:     } elsif ($randomize eq 'no') {
  453: 	&Apache::lonxml::debug("No randomization");
  454: 	my $havetrue=0;
  455: 	foreach my $name (@names) {
  456: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  457: 		if (!$havetrue ) {
  458: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
  459: 		}
  460: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  461: 		push (@whichfalse,$name);
  462: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  463: 	    } else {
  464: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  465: 	    }
  466: 	}
  467: 	if ((!$havetrue) && 
  468:             ($Apache::lonhomework::type ne 'survey') && 
  469:             ($Apache::lonhomework::type ne 'surveycred') &&
  470:             ($Apache::lonhomework::type ne 'anonsurvey') &&
  471:             ($Apache::lonhomework::type ne 'anonsurveycred')) {
  472: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
  473: 	}
  474:     } else {
  475: 	my $current=0;
  476: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
  477: 	my (%top,%bottom);
  478: 	#first find out where everyone wants to be
  479: 	foreach my $name (@names) {
  480: 	    $current++;
  481: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  482: 		push (@truelist,$name);
  483: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  484: 		    $top{$name}=$current;
  485: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
  486: 		    $bottom{$name}=$current;
  487: 		}
  488: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  489: 		push (@falselist,$name);
  490: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  491: 		    $top{$name}=$current;
  492: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
  493: 		    $bottom{$name}=$current;
  494: 		}
  495: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  496: 	    } else {
  497: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  498: 	    }
  499: 	}
  500: 	#pick a true statement
  501: 	my $notrue=0;
  502: 	if (scalar(@truelist) == 0) { $notrue=1; }
  503: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
  504: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  505: 	my (@toplist, @bottomlist);
  506: 	my $topcount=0;
  507: 	my $bottomcount=0;
  508: 	# assign everyone to either toplist/bottomlist or whichfalse
  509: 	# which false is randomized, toplist bottomlist are in order
  510: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
  511: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
  512: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
  513: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  514: 	    $afalse=splice(@falselist,$afalse,1);
  515: 	    &Apache::lonxml::debug("Picked $afalse");
  516: 	    &Apache::lonhomework::showhash(('names'=>\@names));
  517: 	    &Apache::lonhomework::showhash(%top);
  518: 	    if ($top{$afalse}) {
  519: 		$toplist[$top{$afalse}]=$afalse;
  520: 		$topcount++;
  521: 	    } elsif ($bottom{$afalse}) {
  522: 		$bottomlist[$bottom{$afalse}]=$afalse;
  523: 		$bottomcount++;
  524: 	    } else {
  525: 		push (@whichfalse,$afalse);
  526: 	    }
  527: 	}
  528: 	&Apache::lonxml::debug("Answer wants $answer");
  529: 	my $truename=$truelist[$whichtrue];
  530: 	my $dosplice=1;
  531: 	if (($notrue) && 
  532:             ($Apache::lonhomework::type ne 'survey') &&
  533:             ($Apache::lonhomework::type ne 'surveycred') &&
  534:             ($Apache::lonhomework::type ne 'anonsurvey') &&
  535:             ($Apache::lonhomework::type ne 'anonsurveycred')) {
  536: 	    $dosplice=0;
  537: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
  538: 	}
  539: 	#insert the true statement, keeping track of where it wants to be
  540: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
  541: 	    $toplist[$top{$truename}]=$truename;
  542: 	    $answer=-1;
  543: 	    foreach my $top (reverse(@toplist)) {
  544: 		if ($top) { $answer++;}
  545: 		if ($top eq $truename) { last; }
  546: 	    }
  547: 	    $dosplice=0;
  548: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
  549: 	    $bottomlist[$bottom{$truename}]=$truename;
  550: 	    $answer=-1;
  551: 	    foreach my $bot (@bottomlist) {
  552: 		if ($bot) { $answer++;}
  553: 		if ($bot eq $truename) { last; }
  554: 	    }
  555: 	    $answer+=$topcount+$#whichfalse+1;
  556: 	    $dosplice=0;
  557: 	} else {
  558: 	    if ($topcount>0 || $bottomcount>0) {
  559: 		$answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
  560: 		    + $topcount;
  561: 	    }
  562: 	}
  563: 	&Apache::lonxml::debug("Answer now wants $answer");
  564: 	#add the top items to the top, bottom items to the bottom
  565: 	for (my $i=0;$i<=$#toplist;$i++) {
  566: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
  567: 	}
  568: 	for (my $i=0;$i<=$#bottomlist;$i++) {
  569: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
  570: 	}
  571: 	#if the true statement is randomized insert it into the list
  572: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
  573:     }
  574:     &Apache::lonxml::debug("Answer is $answer");
  575:     return ($answer,@whichfalse);
  576: }
  577: 
  578: sub displayfoils {
  579:     my ($target,$answer,$whichfoils,$direction, $bubbles_per_line)=@_;
  580:     my $result;
  581: 
  582:     my $part=$Apache::inputtags::part;
  583:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  584:     if ( ($target ne 'tex') &&
  585: 	 &Apache::response::show_answer() ) {
  586: 	if ($direction eq 'horizontal') {
  587: 	    if ($target ne 'tex') {
  588: 		$result.='<table><tr>';
  589: 	    }
  590: 	}
  591: 	foreach my $name (@{ $whichfoils }) {
  592: 	    if ($direction eq 'horizontal') {
  593: 		if ($target ne 'tex') { $result.='<td>'; }
  594: 	    }
  595: 	    if ($target ne 'tex') {
  596: 		$result.="<br />";
  597: 	    } else {
  598: 		$result.='\item \vskip -2 mm  ';
  599: 	    }
  600: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  601: 		if ($target ne 'tex') {
  602:                     $result.=&mt('Correct:').'<b>';
  603:                 } else {
  604:                     $result.=&mt('Correct:').' \textbf{';
  605:                 }
  606: 	    } else {
  607: 		$result.=&mt('Incorrect:');
  608: 	    }
  609: 	    if ($target eq 'web') { $result.="<label>"; }
  610: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
  611: 	    if ($target eq 'web') { $result.="</label>"; }
  612: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  613: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
  614: 	    }
  615: 	    if ($direction eq 'horizontal') {
  616: 		if ($target ne 'tex') { $result.='</td>'; }
  617: 	    }
  618: 	}
  619: 	if ($direction eq 'horizontal') {
  620: 	    if ($target ne 'tex') {
  621: 		$result.='</tr></table>';
  622: 	    }
  623: 	}
  624:     } else {
  625: 	my @alphabet = ('A'..'Z');
  626: 	my $i = 0;
  627: 	my $bubble_number = 0;
  628: 	my $line = 0;
  629: 	my $temp=0;  
  630: 	my $id=$Apache::inputtags::response['-1'];
  631: 	my $part=$Apache::inputtags::part;
  632:         my ($lastresponse,$newvariation);
  633:         if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
  634:              ($Apache::lonhomework::type eq 'randomizetry')) && 
  635:         ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
  636:             if ($env{'form.'.$part.'.rndseed'} ne
  637:                 $Apache::lonhomework::history{"resource.$part.rndseed"}) {
  638:                 $newvariation = 1;
  639:             }
  640:         }
  641:         unless ($newvariation) {
  642: 	    $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  643:         }
  644: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  645: 	if ($target ne 'tex' && $direction eq 'horizontal') {
  646: 	    $result.="<table><tr>";
  647: 	}
  648: 	foreach my $name (@{ $whichfoils }) {
  649: 	    if ($target ne 'tex') {
  650: 		if ($direction eq 'horizontal') {
  651: 		    $result.="<td>"; 
  652: 		} else { 
  653: 		    $result.="<br />";
  654: 		} 
  655: 	    }
  656: 	    if ($target ne 'tex') { 
  657:                 $result.= '<label>';
  658: 		$result.=
  659: 		    "<input type=\"radio\"
  660:                             onchange=\"javascript:setSubmittedPart('$part');\"
  661:                             name=\"HWVAL_$Apache::inputtags::response['-1']\"
  662:                             value=\"$temp\"";
  663: 		if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
  664: 		$result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
  665: 	    } else {
  666: 		if ($Apache::lonhomework::type eq 'exam') {
  667: 		    if($bubble_number >= $bubbles_per_line) {
  668: 			$line++;
  669: 			$i = 0;
  670: 			$bubble_number = 0;
  671: 			$result.='\item[\textbf{'.($Apache::lonxml::counter+$line).'}.]';
  672: 		    }
  673: 		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
  674: 		    $i++;
  675: 		    $bubble_number++;
  676: 		} else {
  677:                     if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  678:                         my $fieldname = $env{'request.symb'}.'&part_'. $Apache::inputtags::part
  679:                                         .'&radiobuttonresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'];
  680:                         $result .= '\item[{'.&Apache::lonxml::print_pdf_radiobutton($fieldname,$temp).'}]'
  681:                                    .$Apache::response::foilgroup{$name.'.text'}."\n";
  682:                     } else { 
  683:                         $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
  684:                     }
  685:                 }
  686: 	    }
  687: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
  688: 		$result.="</td>"; 
  689: 	    }
  690: 	    $temp++;
  691: 	}
  692: 	if ($target ne 'tex' && $direction eq 'horizontal') {
  693: 	    $result.="</tr></table>";
  694: 	}
  695:     }
  696:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
  697:     return $result;
  698: }
  699: 
  700: sub displayallanswers {
  701:     my @names;
  702:     if ( $Apache::response::foilgroup{'names'} ) {
  703: 	@names= @{ $Apache::response::foilgroup{'names'} };
  704:     }
  705:     my $result=&Apache::response::answer_header('radiobuttonresponse');
  706:     foreach my $name (@names) {
  707: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  708: 				$Apache::response::foilgroup{$name.'.value'});
  709:     }
  710:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
  711:     return $result;
  712: }
  713: 
  714: sub displayanswers {
  715:     my ($answer, $whichopt, $bubbles_per_line)=@_;
  716:     my $result;
  717: 
  718:     if ($Apache::lonhomework::type eq 'exam') {
  719: 	my $line = int($answer/$bubbles_per_line);
  720: 	my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
  721: 	$result .= &Apache::response::answer_header('radiobuttonresponse',
  722: 						    $line);
  723: 	$result .= &Apache::response::answer_part('radiobuttonresponse',
  724: 						  $correct);
  725:     } else {
  726: 	$result .= &Apache::response::answer_header('radiobuttonresponse');
  727:     }
  728:     foreach my $name (@{ $whichopt }) {
  729: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  730: 						$Apache::response::foilgroup{$name.'.value'});
  731:     }
  732:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
  733:     return $result;
  734: }
  735: 
  736: sub start_conceptgroup {
  737:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  738:     $Apache::radiobuttonresponse::conceptgroup=1;
  739:     %Apache::response::conceptgroup=();
  740:     my $result;
  741:     if ($target eq 'edit') {
  742: 	$result.=&Apache::edit::tag_start($target,$token);
  743: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  744: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  745:     } elsif ($target eq 'modified') {
  746: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  747: 						     $safeeval,'concept');
  748: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  749:     }
  750:     return $result;
  751: }
  752: 
  753: sub end_conceptgroup {
  754:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  755:     $Apache::radiobuttonresponse::conceptgroup=0;
  756:     my $result;
  757:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
  758: 	$target eq 'tex' || $target eq 'analyze') {
  759: 	&Apache::response::pick_foil_for_concept($target,
  760: 						 ['value','text','location'],
  761: 						 \%Apache::hint::radiobutton,
  762: 						 $parstack,$safeeval);
  763:     } elsif ($target eq 'edit') {
  764: 	$result=&Apache::edit::end_table();
  765:     }
  766:     return $result;
  767: }
  768: 
  769: sub insert_conceptgroup {
  770:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  771:     return $result;
  772: }
  773: 
  774: sub start_foil {
  775:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  776:     my $result='';
  777:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  778: 	&Apache::lonxml::startredirection;
  779: 	if ($target eq 'analyze') {
  780: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  781: 	}
  782:     } elsif ($target eq 'edit') {
  783: 	$result=&Apache::edit::tag_start($target,$token);
  784: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  785: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
  786: 						   ['unused','true','false'],
  787: 						   $token);
  788: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  789: 						 $safeeval,'-3');
  790: 	if ($randomize ne 'no') {
  791: 	    $result.=&Apache::edit::select_arg('Location:','location',
  792: 					       ['random','top','bottom'],$token);
  793: 	}
  794: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  795:     } elsif ($target eq 'modified') {
  796: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  797: 						     $safeeval,'value','name',
  798: 						     'location');
  799: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  800:     } 
  801:     return $result;
  802: }
  803: 
  804: sub end_foil {
  805:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  806:     my $text='';
  807:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  808: 	$text=&Apache::lonxml::endredirection;
  809:     }
  810:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
  811: 	|| $target eq 'tex' || $target eq 'analyze') {
  812: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  813: 	if ($value ne 'unused') {
  814: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  815: 	    if ($name eq "") {
  816: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
  817: 		$name=$Apache::lonxml::curdepth;
  818: 	    }
  819: 	    if (defined($Apache::response::foilnames{$name})) {
  820: 		&Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
  821: 	    }
  822: 	    $Apache::response::foilnames{$name}++;
  823: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  824: 						     $safeeval);
  825: 	    if ( $Apache::radiobuttonresponse::conceptgroup
  826: 		 && !&Apache::response::showallfoils() ) {
  827: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  828: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  829: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
  830: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  831: 	    } else {
  832: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  833: 		$Apache::response::foilgroup{"$name.value"} = $value;
  834: 		$Apache::response::foilgroup{"$name.text"} = $text;
  835: 		$Apache::response::foilgroup{"$name.location"} = $location;
  836: 	    }
  837: 	}
  838:     }
  839:     return '';
  840: }
  841: 
  842: sub insert_foil {
  843:     return '
  844: <foil name="" value="unused">
  845: <startouttext />
  846: <endouttext />
  847: </foil>';
  848: }
  849: 1;
  850: __END__
  851: 
  852: 
  853: 
  854: =head1 NAME
  855: 
  856: Apache::radiobuttonresponse
  857: 
  858: =head1 SYNOPSIS
  859: 
  860: Handles multiple-choice style responses.
  861: 
  862: This is part of the LearningOnline Network with CAPA project
  863: described at http://www.lon-capa.org.
  864: 
  865: =head1 SUBROUTINES
  866: 
  867: =over
  868: 
  869: =item start_radiobuttonresponse()
  870: 
  871: =item bubble_line_count()
  872: 
  873: =item end_radiobuttonresponse()
  874: 
  875: =item start_foilgroup()
  876: 
  877: =item storesurvey()
  878: 
  879: =item grade_response()
  880: 
  881: =item end_foilgroup()
  882: 
  883: =item getfoilcounts()
  884: 
  885: =item format_prior_answer()
  886: 
  887: =item displayallfoils()
  888: 
  889: =item &whichfoils($max,$randomize)
  890: 
  891: Randomizes the list of foils.
  892: Respects
  893:   - each foils desire to be randomized
  894:   - the existance of Concept groups of foils (select 1 foil from each)
  895:   - and selects a single correct statement from all possilble true statments
  896:   - and limits it to a toal of $max foils
  897: 
  898: WARNING: this routine uses the random number generator, it should only
  899: be called once per target, otherwise it can cause randomness changes in
  900: homework problems.
  901: 
  902: Arguments
  903:   $max - maximum number of foils to select (including the true one)
  904:          (so a max of 5 is: 1 true, 4 false)
  905: 
  906:   $randomize - whether to randomize the listing of foils, by default
  907:                will randomize, only if randomize is 'no' will it not
  908: 
  909: Returns
  910:   $answer - location in the array of the correct answer
  911:   @foils  - array of foil names in to display order
  912: 
  913: =item displayfoils()
  914: 
  915: =item displayallanswers()
  916: 
  917: =item displayanswers()
  918: 
  919: =item start_conceptgroup()
  920: 
  921: =item end_conceptgroup()
  922: 
  923: =item insert_conceptgroup()
  924: 
  925: =item start_foil()
  926: 
  927: =item end_foil()
  928: 
  929: =item insert_foil()
  930: 
  931: =back
  932: 
  933: =cut
  934:  

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