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

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

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