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

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

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