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

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

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