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

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

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