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

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

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