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

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.31      albertel    3: #
1.122   ! albertel    4: # $Id: radiobuttonresponse.pm,v 1.121 2007/06/29 12:49:10 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.122   ! albertel  235: 	    my ($answer,@shown) = &whichfoils($max,$randomize);
1.121     foxr      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.122   ! albertel  381: =pod
        !           382: 
        !           383: =item &whichfoils($max,$randomize)
        !           384: 
        !           385: Randomizes the list of foils.
        !           386: Respects
        !           387:   - each foils desire to be randomized
        !           388:   - the existance of Concept groups of foils (select 1 foil from each)
        !           389:   - and selects a single correct statement from all possilble true statments
        !           390:   - and limits it to a toal of $max foils
        !           391: 
        !           392: Arguments
        !           393:   $max - maximum number of foils to select (including the true one)
        !           394:          (so a max of 5 is: 1 true, 4 false)
        !           395: 
        !           396:   $randomize - whether to randomize the listing of foils, by default
        !           397:                will randomize, only if randomize is 'no' will it not
        !           398: 
        !           399: Returns
        !           400:   $answer - location in the array of the correct answer
        !           401:   @foils  - array of foil names in to display order
        !           402: 
        !           403: =cut
        !           404: 
1.28      albertel  405: sub whichfoils {
1.83      albertel  406:     my ($max,$randomize)=@_;
1.28      albertel  407: 
1.83      albertel  408:     my @truelist;
                    409:     my @falselist;
                    410:     my @whichfalse =();
                    411:     my ($truecnt,$falsecnt) = &getfoilcounts();
                    412:     my $count=0;
                    413:     # we will add in 1 of the true statements
1.104     albertel  414:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
1.83      albertel  415:     my $answer=int(&Math::Random::random_uniform() * ($count));
                    416:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
                    417:     my @names;
                    418:     if ( $Apache::response::foilgroup{'names'} ) {
                    419: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    420:     }
                    421:     if (&Apache::response::showallfoils()) {
                    422: 	@whichfalse=@names;
                    423:     } elsif ($randomize eq 'no') {
                    424: 	&Apache::lonxml::debug("No randomization");
                    425: 	my $havetrue=0;
                    426: 	foreach my $name (@names) {
                    427: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    428: 		if (!$havetrue ) {
                    429: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
                    430: 		}
                    431: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    432: 		push (@whichfalse,$name);
                    433: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    434: 	    } else {
1.87      albertel  435: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  436: 	    }
                    437: 	}
1.97      albertel  438: 	if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
                    439: 	    &Apache::lonxml::error("There are no true statements available.<br />");
                    440: 	}
1.83      albertel  441:     } else {
                    442: 	my $current=0;
                    443: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
                    444: 	my (%top,%bottom);
                    445: 	#first find out where everyone wants to be
                    446: 	foreach my $name (@names) {
                    447: 	    $current++;
                    448: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    449: 		push (@truelist,$name);
                    450: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    451: 		    $top{$name}=$current;
                    452: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    453: 		    $bottom{$name}=$current;
                    454: 		}
                    455: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    456: 		push (@falselist,$name);
                    457: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    458: 		    $top{$name}=$current;
                    459: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    460: 		    $bottom{$name}=$current;
                    461: 		}
                    462: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    463: 	    } else {
1.87      albertel  464: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  465: 	    }
                    466: 	}
                    467: 	#pick a true statement
                    468: 	my $notrue=0;
                    469: 	if (scalar(@truelist) == 0) { $notrue=1; }
                    470: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
                    471: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    472: 	my (@toplist, @bottomlist);
                    473: 	my $topcount=0;
                    474: 	my $bottomcount=0;
                    475: 	# assign everyone to either toplist/bottomlist or whichfalse
                    476: 	# which false is randomized, toplist bottomlist are in order
                    477: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
                    478: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    479: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
                    480: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    481: 	    $afalse=splice(@falselist,$afalse,1);
                    482: 	    &Apache::lonxml::debug("Picked $afalse");
                    483: 	    &Apache::lonhomework::showhash(('names'=>\@names));
                    484: 	    &Apache::lonhomework::showhash(%top);
                    485: 	    if ($top{$afalse}) {
                    486: 		$toplist[$top{$afalse}]=$afalse;
                    487: 		$topcount++;
                    488: 	    } elsif ($bottom{$afalse}) {
                    489: 		$bottomlist[$bottom{$afalse}]=$afalse;
                    490: 		$bottomcount++;
                    491: 	    } else {
                    492: 		push (@whichfalse,$afalse);
                    493: 	    }
                    494: 	}
                    495: 	&Apache::lonxml::debug("Answer wants $answer");
                    496: 	my $truename=$truelist[$whichtrue];
                    497: 	my $dosplice=1;
                    498: 	if ($notrue && $Apache::lonhomework::type ne 'survey') {
                    499: 	    $dosplice=0;
                    500: 	    &Apache::lonxml::error("There are no true statements available.<br />");
                    501: 	}
                    502: 	#insert the true statement, keeping track of where it wants to be
                    503: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
                    504: 	    $toplist[$top{$truename}]=$truename;
                    505: 	    $answer=-1;
                    506: 	    foreach my $top (reverse(@toplist)) {
                    507: 		if ($top) { $answer++;}
                    508: 		if ($top eq $truename) { last; }
1.49      albertel  509: 	    }
1.83      albertel  510: 	    $dosplice=0;
                    511: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
                    512: 	    $bottomlist[$bottom{$truename}]=$truename;
                    513: 	    $answer=-1;
                    514: 	    foreach my $bot (@bottomlist) {
                    515: 		if ($bot) { $answer++;}
                    516: 		if ($bot eq $truename) { last; }
1.49      albertel  517: 	    }
1.83      albertel  518: 	    $answer+=$topcount+$#whichfalse+1;
                    519: 	    $dosplice=0;
1.49      albertel  520: 	} else {
1.83      albertel  521: 	    if ($topcount>0 || $bottomcount>0) {
                    522: 		$answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
                    523: 		    + $topcount;
                    524: 	    }
                    525: 	}
                    526: 	&Apache::lonxml::debug("Answer now wants $answer");
                    527: 	#add the top items to the top, bottom items to the bottom
                    528: 	for (my $i=0;$i<=$#toplist;$i++) {
                    529: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
1.49      albertel  530: 	}
1.83      albertel  531: 	for (my $i=0;$i<=$#bottomlist;$i++) {
                    532: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
1.49      albertel  533: 	}
1.83      albertel  534: 	#if the true statement is randomized insert it into the list
                    535: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.49      albertel  536:     }
1.83      albertel  537:     &Apache::lonxml::debug("Answer is $answer");
                    538:     return ($answer,@whichfalse);
1.28      albertel  539: }
                    540: 
                    541: sub displayfoils {
1.121     foxr      542:     my ($target,$max,$randomize,$direction, $bubbles_per_line)=@_;
1.83      albertel  543:     my $result;
1.28      albertel  544: 
1.83      albertel  545:     my ($answer,@whichfoils)=&whichfoils($max,$randomize);
1.22      albertel  546:     my $part=$Apache::inputtags::part;
1.83      albertel  547:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    548:     if ( ($target ne 'tex') &&
                    549: 	 &Apache::response::show_answer() ) {
1.90      albertel  550: 	if ($direction eq 'horizontal') {
                    551: 	    if ($target ne 'tex') {
                    552: 		$result.='<table><tr>';
                    553: 	    }
                    554: 	}
1.83      albertel  555: 	foreach my $name (@whichfoils) {
1.90      albertel  556: 	    if ($direction eq 'horizontal') {
                    557: 		if ($target ne 'tex') { $result.='<td>'; }
                    558: 	    }
1.83      albertel  559: 	    if ($target ne 'tex') {
                    560: 		$result.="<br />";
                    561: 	    } else {
                    562: 		$result.='\item \vskip -2 mm  ';
                    563: 	    }
                    564: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
                    565: 		if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}
                    566: 	    } else {
                    567: 		$result.='Incorrect:';
                    568: 	    }
1.94      matthew   569: 	    if ($target eq 'web') { $result.="<label>"; }
1.90      albertel  570: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
1.94      matthew   571: 	    if ($target eq 'web') { $result.="</label>"; }
1.83      albertel  572: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    573: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
                    574: 	    }
1.90      albertel  575: 	    if ($direction eq 'horizontal') {
                    576: 		if ($target ne 'tex') { $result.='</td>'; }
                    577: 	    }
                    578: 	}
                    579: 	if ($direction eq 'horizontal') {
                    580: 	    if ($target ne 'tex') {
                    581: 		$result.='</tr></table>';
                    582: 	    }
1.83      albertel  583: 	}
                    584:     } else {
                    585: 	my @alphabet = ('A'..'Z');
                    586: 	my $i = 0;
1.116     foxr      587: 	my $bubble_number = 0;
1.83      albertel  588: 	my $temp=0;  
                    589: 	my $id=$Apache::inputtags::response['-1'];
                    590: 	my $part=$Apache::inputtags::part;
                    591: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    592: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.90      albertel  593: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    594: 	    $result.="<table><tr>";
                    595: 	}
1.83      albertel  596: 	foreach my $name (@whichfoils) {
                    597: 	    if ($target ne 'tex') {
1.90      albertel  598: 		if ($direction eq 'horizontal') {
                    599: 		    $result.="<td>"; 
                    600: 		} else { 
                    601: 		    $result.="<br />";
                    602: 		} 
                    603: 	    }
                    604: 	    if ($target ne 'tex') { 
1.94      matthew   605:                 $result.= '<label>';
1.113     albertel  606: 		$result.=
                    607: 		    "<input type=\"radio\"
                    608:                             onchange=\"javascript:setSubmittedPart('$part');\"
                    609:                             name=\"HWVAL_$Apache::inputtags::response['-1']\"
                    610:                             value=\"$temp\" ";
1.83      albertel  611: 		if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.108     albertel  612: 		$result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
1.83      albertel  613: 	    } else {
                    614: 		if ($Apache::lonhomework::type eq 'exam') {
                    615: 		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    616: 		    $i++;
1.116     foxr      617: 		    $bubble_number++;
                    618: 		    if($bubble_number >= $bubbles_per_line) {
                    619: 			$i = 0;
                    620: 			$bubble_number = 0;
                    621: 			$result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
                    622: 		    }
1.83      albertel  623: 		} else {
                    624: 		    $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
                    625: 		}
                    626: 	    }
1.90      albertel  627: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
                    628: 		$result.="</td>"; 
                    629: 	    }
1.83      albertel  630: 	    $temp++;
                    631: 	}
1.90      albertel  632: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    633: 	    $result.="</tr></table>";
                    634: 	}
1.83      albertel  635:     }
1.92      albertel  636:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
1.83      albertel  637:     return $result;
1.81      albertel  638: }
                    639: 
                    640: sub displayallanswers {
1.106     albertel  641:     my @names;
                    642:     if ( $Apache::response::foilgroup{'names'} ) {
                    643: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    644:     }
1.81      albertel  645:     my $result=&Apache::response::answer_header('radiobuttonresponse');
                    646:     foreach my $name (@names) {
                    647: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                    648: 				$Apache::response::foilgroup{$name.'.value'});
                    649:     }
                    650:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    651:     return $result;
1.14      albertel  652: }
                    653: 
1.28      albertel  654: sub displayanswers {
1.83      albertel  655:     my ($max,$randomize)=@_;
                    656:     my ($answer,@whichopt) = &whichfoils($max,$randomize);
                    657:     my $result=&Apache::response::answer_header('radiobuttonresponse');
1.105     albertel  658:     if ($Apache::lonhomework::type eq 'exam') {
                    659: 	my $correct = ('A'..'Z')[$answer];
                    660: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                    661: 						$correct);
                    662:     }
1.83      albertel  663:     foreach my $name (@whichopt) {
                    664: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
1.105     albertel  665: 						$Apache::response::foilgroup{$name.'.value'});
                    666:     }
1.83      albertel  667:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    668:     return $result;
1.28      albertel  669: }
                    670: 
1.14      albertel  671: sub start_conceptgroup {
1.83      albertel  672:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    673:     $Apache::radiobuttonresponse::conceptgroup=1;
                    674:     %Apache::response::conceptgroup=();
                    675:     my $result;
                    676:     if ($target eq 'edit') {
                    677: 	$result.=&Apache::edit::tag_start($target,$token);
                    678: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    679: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    680:     } elsif ($target eq 'modified') {
                    681: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    682: 						     $safeeval,'concept');
                    683: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    684:     }
                    685:     return $result;
1.14      albertel  686: }
                    687: 
                    688: sub end_conceptgroup {
1.83      albertel  689:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    690:     $Apache::radiobuttonresponse::conceptgroup=0;
                    691:     my $result;
                    692:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
                    693: 	$target eq 'tex' || $target eq 'analyze') {
                    694: 	&Apache::response::pick_foil_for_concept($target,
                    695: 						 ['value','text','location'],
                    696: 						 \%Apache::hint::radiobutton,
                    697: 						 $parstack,$safeeval);
                    698:     } elsif ($target eq 'edit') {
                    699: 	$result=&Apache::edit::end_table();
                    700:     }
                    701:     return $result;
1.26      albertel  702: }
                    703: 
                    704: sub insert_conceptgroup {
1.83      albertel  705:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    706:     return $result;
1.1       albertel  707: }
                    708: 
                    709: sub start_foil {
1.83      albertel  710:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    711:     my $result='';
                    712:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    713: 	&Apache::lonxml::startredirection;
1.95      albertel  714: 	if ($target eq 'analyze') {
                    715: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    716: 	}
1.83      albertel  717:     } elsif ($target eq 'edit') {
                    718: 	$result=&Apache::edit::tag_start($target,$token);
                    719: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    720: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
                    721: 						   ['unused','true','false'],
                    722: 						   $token);
                    723: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    724: 						 $safeeval,'-3');
                    725: 	if ($randomize ne 'no') {
                    726: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    727: 					       ['random','top','bottom'],$token);
                    728: 	}
                    729: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    730:     } elsif ($target eq 'modified') {
                    731: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    732: 						     $safeeval,'value','name',
                    733: 						     'location');
                    734: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    735:     } 
                    736:     return $result;
1.1       albertel  737: }
                    738: 
                    739: sub end_foil {
1.83      albertel  740:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    741:     my $text='';
                    742:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    743: 	$text=&Apache::lonxml::endredirection;
                    744:     }
1.85      albertel  745:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
                    746: 	|| $target eq 'tex' || $target eq 'analyze') {
1.83      albertel  747: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    748: 	if ($value ne 'unused') {
                    749: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.102     albertel  750: 	    if ($name eq "") {
1.101     albertel  751: 		&Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.98      albertel  752: 		$name=$Apache::lonxml::curdepth;
                    753: 	    }
1.85      albertel  754: 	    if (defined($Apache::response::foilnames{$name})) {
                    755: 		&Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
                    756: 	    }
1.86      albertel  757: 	    $Apache::response::foilnames{$name}++;
1.85      albertel  758: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
                    759: 						     $safeeval);
1.83      albertel  760: 	    if ( $Apache::radiobuttonresponse::conceptgroup
                    761: 		 && !&Apache::response::showallfoils() ) {
                    762: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    763: 		$Apache::response::conceptgroup{"$name.value"} = $value;
                    764: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
                    765: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    766: 	    } else {
                    767: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    768: 		$Apache::response::foilgroup{"$name.value"} = $value;
                    769: 		$Apache::response::foilgroup{"$name.text"} = $text;
                    770: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    771: 	    }
                    772: 	}
1.18      albertel  773:     }
1.83      albertel  774:     return '';
1.1       albertel  775: }
                    776: 
1.27      albertel  777: sub insert_foil {
1.83      albertel  778:     return '
1.27      albertel  779: <foil name="" value="unused">
                    780: <startouttext />
                    781: <endouttext />
                    782: </foil>';
                    783: }
1.1       albertel  784: 1;
                    785: __END__
                    786:  

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