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

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

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