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

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

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