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

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

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