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

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

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