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

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

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