Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.143

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

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