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

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.31      albertel    3: #
1.134.2.1! raeburn     4: # $Id: radiobuttonresponse.pm,v 1.134 2008/08/08 16:37:13 bisitz 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
1.130     foxr       21: # along with LON-CAPA; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1.31      albertel   22: #
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
1.1       albertel   27: 
                     28: package Apache::radiobuttonresponse;
                     29: use strict;
1.42      albertel   30: use HTML::Entities();
1.85      albertel   31: use Apache::lonlocal;
1.100     albertel   32: use Apache::lonnet;
1.115     foxr       33: use Apache::response;
1.1       albertel   34: 
1.120     foxr       35: my $default_bubbles_per_line = 10;
1.121     foxr       36: 
1.116     foxr       37: 
1.36      harris41   38: BEGIN {
1.83      albertel   39:     &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1       albertel   40: }
                     41: 
1.121     foxr       42: sub bubble_line_count {
                     43:     my ($numfoils, $bubbles_per_line) = @_;
                     44:     my $bubble_lines;
                     45:     $bubble_lines = int($numfoils / $bubbles_per_line);
                     46:     if (($numfoils % $bubbles_per_line) != 0) {
                     47: 	$bubble_lines++;
                     48:     }
                     49:     return $bubble_lines;
                     50:     
                     51: }
                     52: 
                     53: 
1.1       albertel   54: sub start_radiobuttonresponse {
1.83      albertel   55:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     56:     my $result;
1.115     foxr       57: 
1.83      albertel   58:     #when in a radiobutton response use these
                     59:     &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
                     60:     push (@Apache::lonxml::namespace,'radiobuttonresponse');
                     61:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.120     foxr       62: 
1.83      albertel   63:     %Apache::hint::radiobutton=();
1.85      albertel   64:     undef(%Apache::response::foilnames);
1.83      albertel   65:     if ($target eq 'meta') {
                     66: 	$result=&Apache::response::meta_package_write('radiobuttonresponse');
                     67:     } elsif ($target eq 'edit' ) {
1.134.2.1! raeburn    68:         $result.=&Apache::edit::start_table($token)
        !            69:            .'<tr><td>'.&Apache::lonxml::description($token)
        !            70:            .&Apache::loncommon::help_open_topic('Radio_Response_Problems')
        !            71:            .'</td>'
        !            72:            .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
        !            73:            .&Apache::edit::deletelist($target,$token)
        !            74:            .'</span></td>'
        !            75:            .'<td>&nbsp;'.&Apache::edit::end_row()
        !            76:            .&Apache::edit::start_spanning_row();
1.83      albertel   77: 	$result.=
                     78: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',
1.134.2.1! raeburn    79: 				    $token,'4').'&nbsp;'x 3 .
        !            80: 	    &Apache::edit::select_arg('Randomize Foil Order:','randomize',
        !            81: 				      ['yes','no'],$token).'&nbsp;'x 3 .
        !            82: 	    &Apache::edit::select_arg('Display Direction:','direction',
1.103     albertel   83: 				      ['vertical','horizontal'],$token).
1.83      albertel   84: 				      &Apache::edit::end_row().
                     85: 				      &Apache::edit::start_spanning_row()."\n";
                     86:     } elsif ($target eq 'modified') {
                     87: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                     88: 						     $safeeval,'max',
1.103     albertel   89: 						     'randomize','direction');
1.83      albertel   90: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                     91:     } elsif ($target eq 'tex') {
                     92: 	my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,
                     93: 					    undef,0);
                     94: 	if ($type eq '1') {
                     95: 	    $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
                     96: 	} elsif ($type eq 'A') {
                     97: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
                     98: 	} elsif ($type eq 'a') {
                     99: 	    $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
                    100: 	} elsif ($type eq 'i') {
                    101: 	    $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
1.88      albertel  102: 	} else {
                    103: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
1.83      albertel  104: 	}
                    105: 	$result .= '\begin{enumerate}';
                    106:     } elsif ($target eq 'analyze') {
                    107: 	my $part_id="$Apache::inputtags::part.$id";
1.131     raeburn   108:         $Apache::lonhomework::analyze{"$part_id.type"} = 'radiobuttonresponse';
1.83      albertel  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.123     albertel  161:     my ($answer, $whichfoils, $bubbles_per_line)=@_;
                    162: 
1.99      albertel  163:     if ( !&Apache::response::submitted() ) { return; }
1.83      albertel  164:     my $response;
1.118     foxr      165:     
1.100     albertel  166:     if ($env{'form.submitted'} eq 'scantron') {
1.121     foxr      167: 	$response = &Apache::response::getresponse(1,undef,
1.123     albertel  168: 						   &bubble_line_count(scalar(@{ $whichfoils}),
1.121     foxr      169: 								      $bubbles_per_line),
                    170: 						   $bubbles_per_line);
1.116     foxr      171: 
1.83      albertel  172:     } else {
1.100     albertel  173: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83      albertel  174:     }
1.120     foxr      175: 
1.118     foxr      176: 
1.83      albertel  177:     if ( $response !~ /[0-9]+/) { return; }
                    178:     my $part=$Apache::inputtags::part;
                    179:     my $id = $Apache::inputtags::response['-1'];
                    180:     my %responsehash;
1.123     albertel  181:     $responsehash{$whichfoils->[$response]}=$response;
1.83      albertel  182:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    183:     my %previous=&Apache::response::check_for_previous($responsestr,
                    184: 						       $part,$id);
                    185:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    186: 	$responsestr;
                    187:     &Apache::lonxml::debug("submitted a $response<br />\n");
                    188:     my $ad;
                    189:     if ($response == $answer) {
                    190: 	$ad='EXACT_ANS';
                    191:     } else {
                    192: 	$ad='INCORRECT';
                    193:     }
                    194:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    195:     &Apache::response::handle_previous(\%previous,$ad);
1.32      albertel  196: }
                    197: 
1.1       albertel  198: sub end_foilgroup {
1.83      albertel  199:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29      albertel  200: 
1.83      albertel  201:     my $result;
1.121     foxr      202:     my $bubble_lines;
                    203:     my $bubbles_per_line;
                    204:     my $answer_count;
1.129     foxr      205:     my $id   = $Apache::inputtags::response['-1'];
                    206:     my $part = $Apache::inputtags::part;
1.121     foxr      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.123     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,
1.123     albertel  240: 				      $answer, \@shown,
1.121     foxr      241: 				      $direction,
                    242: 				      $bubbles_per_line);
1.83      albertel  243: 	    } elsif ($target eq 'answer' ) {
1.124     albertel  244: 		$result=&displayanswers($answer, \@shown, $bubbles_per_line);
1.83      albertel  245: 	    } elsif ( $target eq 'grade') {
1.123     albertel  246: 		&grade_response($answer, \@shown, $bubbles_per_line);
1.83      albertel  247: 	    }  elsif ( $target eq 'analyze') {
1.126     foxr      248: 		my $bubble_lines = &bubble_line_count($answer_count, 
                    249: 						      $bubbles_per_line);
1.83      albertel  250: 		&Apache::response::analyze_store_foilgroup(\@shown,
                    251: 							   ['text','value','location']);
1.129     foxr      252: 		my $part_id="$part.$id";
1.83      albertel  253: 		push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
                    254: 		      ('true','false'));
1.130     foxr      255: 
1.83      albertel  256: 	    }
1.81      albertel  257: 	}
1.111     albertel  258: 	$Apache::lonxml::post_evaluate=0;
1.83      albertel  259:     }
1.114     albertel  260:     if ($target eq 'web') {
                    261: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
                    262: 						  [\%Apache::response::foilgroup]);
                    263:     }
1.128     foxr      264:     &Apache::response::poprandomnumber();
1.121     foxr      265:     $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
1.128     foxr      266:     &Apache::lonxml::increment_counter($bubble_lines,
1.129     foxr      267: 				       "$part.$id");
1.128     foxr      268:     if ($target eq 'analyze') {
                    269: 	&Apache::lonhomework::set_bubble_lines();
                    270:     }
1.83      albertel  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.120     foxr      310: 
1.83      albertel  311:     my $temp=0;
1.110     foxr      312:     my $i   =0;
1.83      albertel  313:     my $id=$Apache::inputtags::response['-1'];
                    314:     my $part=$Apache::inputtags::part;
                    315:     my $lastresponse=
                    316: 	$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.93      albertel  317:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
1.83      albertel  318:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
                    319:     if (&Apache::response::show_answer() ) {
                    320: 	foreach my $name (@names) {
                    321: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.110     foxr      322: 		if (($direction eq 'horizontal') && ($target ne 'tex')) {
1.93      albertel  323: 		    $result.="<td>";
                    324: 		} else {
1.110     foxr      325: 		    if ($target eq 'tex') {
                    326: 			$result .= '\item \vskip -2mm ';
                    327: 		    } else {
                    328: 			$result.="<br />";
                    329: 		    }
1.93      albertel  330: 		}
1.84      albertel  331: 		if (defined($lastresponse{$name})) {
1.110     foxr      332: 		    if ($target eq 'tex') {
                    333: 			$result .= '}';
                    334: 		    } else {
                    335: 			$result.='<b>';
                    336: 		    }
1.83      albertel  337: 		}
                    338: 		$result .= $Apache::response::foilgroup{$name.'.text'};
1.110     foxr      339: 		if (defined($lastresponse{$name}) && ($target ne 'tex')) {
1.83      albertel  340: 		    $result.='</b>';
                    341: 		}
1.110     foxr      342: 		if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
1.83      albertel  343: 	    }
1.45      albertel  344: 	}
1.83      albertel  345:     } else {
                    346: 	foreach my $name (@names) {
                    347: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.93      albertel  348: 		if ($direction eq 'horizontal') {
                    349: 		    $result.="<td>";
                    350: 		} else {
1.110     foxr      351: 		    if ($target eq 'tex') {
                    352: 			$result .= '\item \vskip -2mm ';
                    353: 		    } else {
                    354: 			$result.="<br />";
                    355: 		    }
                    356: 		}
                    357: 		if ($target eq 'tex') {
                    358: 		    $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    359: 		    $i++;
                    360: 		} else {
                    361: 		    $result .= '<label>';
1.113     albertel  362: 		    $result.="<input
                    363:                        onchange=\"javascript:setSubmittedPart('$part');\"
                    364:                        type=\"radio\"
                    365:                        name=\"HWVAL_$Apache::inputtags::response['-1']\"
                    366:                        value=\"$temp\" ";
1.110     foxr      367: 		    if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
                    368: 		    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
                    369: 			'</label>';
1.93      albertel  370: 		}
1.83      albertel  371: 		$temp++;
1.110     foxr      372: 		if ($target ne 'tex') {
                    373: 		    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
                    374: 		} else {
                    375: 		    $result.='\vskip 0 mm ';
                    376: 		}
1.83      albertel  377: 	    }
1.45      albertel  378: 	}
                    379:     }
1.120     foxr      380:     
1.110     foxr      381:     if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
1.83      albertel  382:     return $result;
1.15      albertel  383: }
                    384: 
1.122     albertel  385: =pod
                    386: 
                    387: =item &whichfoils($max,$randomize)
                    388: 
                    389: Randomizes the list of foils.
                    390: Respects
                    391:   - each foils desire to be randomized
                    392:   - the existance of Concept groups of foils (select 1 foil from each)
                    393:   - and selects a single correct statement from all possilble true statments
                    394:   - and limits it to a toal of $max foils
                    395: 
1.123     albertel  396: WARNING: this routine uses the random number generator, it should only
                    397: be called once per target, otherwise it can cause randomness changes in
                    398: homework problems.
                    399: 
1.122     albertel  400: Arguments
                    401:   $max - maximum number of foils to select (including the true one)
                    402:          (so a max of 5 is: 1 true, 4 false)
                    403: 
                    404:   $randomize - whether to randomize the listing of foils, by default
                    405:                will randomize, only if randomize is 'no' will it not
                    406: 
                    407: Returns
                    408:   $answer - location in the array of the correct answer
                    409:   @foils  - array of foil names in to display order
                    410: 
                    411: =cut
                    412: 
1.28      albertel  413: sub whichfoils {
1.83      albertel  414:     my ($max,$randomize)=@_;
1.28      albertel  415: 
1.83      albertel  416:     my @truelist;
                    417:     my @falselist;
                    418:     my @whichfalse =();
                    419:     my ($truecnt,$falsecnt) = &getfoilcounts();
                    420:     my $count=0;
                    421:     # we will add in 1 of the true statements
1.104     albertel  422:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
1.83      albertel  423:     my $answer=int(&Math::Random::random_uniform() * ($count));
                    424:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
                    425:     my @names;
                    426:     if ( $Apache::response::foilgroup{'names'} ) {
                    427: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    428:     }
                    429:     if (&Apache::response::showallfoils()) {
                    430: 	@whichfalse=@names;
                    431:     } elsif ($randomize eq 'no') {
                    432: 	&Apache::lonxml::debug("No randomization");
                    433: 	my $havetrue=0;
                    434: 	foreach my $name (@names) {
                    435: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    436: 		if (!$havetrue ) {
                    437: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
                    438: 		}
                    439: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    440: 		push (@whichfalse,$name);
                    441: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    442: 	    } else {
1.87      albertel  443: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  444: 	    }
                    445: 	}
1.97      albertel  446: 	if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
1.134     bisitz    447: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
1.97      albertel  448: 	}
1.83      albertel  449:     } else {
                    450: 	my $current=0;
                    451: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
                    452: 	my (%top,%bottom);
                    453: 	#first find out where everyone wants to be
                    454: 	foreach my $name (@names) {
                    455: 	    $current++;
                    456: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    457: 		push (@truelist,$name);
                    458: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    459: 		    $top{$name}=$current;
                    460: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    461: 		    $bottom{$name}=$current;
                    462: 		}
                    463: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    464: 		push (@falselist,$name);
                    465: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    466: 		    $top{$name}=$current;
                    467: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    468: 		    $bottom{$name}=$current;
                    469: 		}
                    470: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    471: 	    } else {
1.87      albertel  472: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  473: 	    }
                    474: 	}
                    475: 	#pick a true statement
                    476: 	my $notrue=0;
                    477: 	if (scalar(@truelist) == 0) { $notrue=1; }
                    478: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
                    479: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    480: 	my (@toplist, @bottomlist);
                    481: 	my $topcount=0;
                    482: 	my $bottomcount=0;
                    483: 	# assign everyone to either toplist/bottomlist or whichfalse
                    484: 	# which false is randomized, toplist bottomlist are in order
                    485: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
                    486: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    487: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
                    488: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    489: 	    $afalse=splice(@falselist,$afalse,1);
                    490: 	    &Apache::lonxml::debug("Picked $afalse");
                    491: 	    &Apache::lonhomework::showhash(('names'=>\@names));
                    492: 	    &Apache::lonhomework::showhash(%top);
                    493: 	    if ($top{$afalse}) {
                    494: 		$toplist[$top{$afalse}]=$afalse;
                    495: 		$topcount++;
                    496: 	    } elsif ($bottom{$afalse}) {
                    497: 		$bottomlist[$bottom{$afalse}]=$afalse;
                    498: 		$bottomcount++;
                    499: 	    } else {
                    500: 		push (@whichfalse,$afalse);
                    501: 	    }
                    502: 	}
                    503: 	&Apache::lonxml::debug("Answer wants $answer");
                    504: 	my $truename=$truelist[$whichtrue];
                    505: 	my $dosplice=1;
                    506: 	if ($notrue && $Apache::lonhomework::type ne 'survey') {
                    507: 	    $dosplice=0;
1.134     bisitz    508: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
1.83      albertel  509: 	}
                    510: 	#insert the true statement, keeping track of where it wants to be
                    511: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
                    512: 	    $toplist[$top{$truename}]=$truename;
                    513: 	    $answer=-1;
                    514: 	    foreach my $top (reverse(@toplist)) {
                    515: 		if ($top) { $answer++;}
                    516: 		if ($top eq $truename) { last; }
1.49      albertel  517: 	    }
1.83      albertel  518: 	    $dosplice=0;
                    519: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
                    520: 	    $bottomlist[$bottom{$truename}]=$truename;
                    521: 	    $answer=-1;
                    522: 	    foreach my $bot (@bottomlist) {
                    523: 		if ($bot) { $answer++;}
                    524: 		if ($bot eq $truename) { last; }
1.49      albertel  525: 	    }
1.83      albertel  526: 	    $answer+=$topcount+$#whichfalse+1;
                    527: 	    $dosplice=0;
1.49      albertel  528: 	} else {
1.83      albertel  529: 	    if ($topcount>0 || $bottomcount>0) {
                    530: 		$answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
                    531: 		    + $topcount;
                    532: 	    }
                    533: 	}
                    534: 	&Apache::lonxml::debug("Answer now wants $answer");
                    535: 	#add the top items to the top, bottom items to the bottom
                    536: 	for (my $i=0;$i<=$#toplist;$i++) {
                    537: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
1.49      albertel  538: 	}
1.83      albertel  539: 	for (my $i=0;$i<=$#bottomlist;$i++) {
                    540: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
1.49      albertel  541: 	}
1.83      albertel  542: 	#if the true statement is randomized insert it into the list
                    543: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.49      albertel  544:     }
1.83      albertel  545:     &Apache::lonxml::debug("Answer is $answer");
                    546:     return ($answer,@whichfalse);
1.28      albertel  547: }
                    548: 
                    549: sub displayfoils {
1.123     albertel  550:     my ($target,$answer,$whichfoils,$direction, $bubbles_per_line)=@_;
1.83      albertel  551:     my $result;
1.28      albertel  552: 
1.22      albertel  553:     my $part=$Apache::inputtags::part;
1.83      albertel  554:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    555:     if ( ($target ne 'tex') &&
                    556: 	 &Apache::response::show_answer() ) {
1.90      albertel  557: 	if ($direction eq 'horizontal') {
                    558: 	    if ($target ne 'tex') {
                    559: 		$result.='<table><tr>';
                    560: 	    }
                    561: 	}
1.123     albertel  562: 	foreach my $name (@{ $whichfoils }) {
1.90      albertel  563: 	    if ($direction eq 'horizontal') {
                    564: 		if ($target ne 'tex') { $result.='<td>'; }
                    565: 	    }
1.83      albertel  566: 	    if ($target ne 'tex') {
                    567: 		$result.="<br />";
                    568: 	    } else {
                    569: 		$result.='\item \vskip -2 mm  ';
                    570: 	    }
                    571: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
1.132     bisitz    572: 		if ($target ne 'tex') {
                    573:                     $result.=&mt('Correct:').'<b>';
                    574:                 } else {
                    575:                     $result.=&mt('Correct:').' \textbf{';
                    576:                 }
1.83      albertel  577: 	    } else {
1.132     bisitz    578: 		$result.=&mt('Incorrect:');
1.83      albertel  579: 	    }
1.94      matthew   580: 	    if ($target eq 'web') { $result.="<label>"; }
1.90      albertel  581: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
1.94      matthew   582: 	    if ($target eq 'web') { $result.="</label>"; }
1.83      albertel  583: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    584: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
                    585: 	    }
1.90      albertel  586: 	    if ($direction eq 'horizontal') {
                    587: 		if ($target ne 'tex') { $result.='</td>'; }
                    588: 	    }
                    589: 	}
                    590: 	if ($direction eq 'horizontal') {
                    591: 	    if ($target ne 'tex') {
                    592: 		$result.='</tr></table>';
                    593: 	    }
1.83      albertel  594: 	}
                    595:     } else {
                    596: 	my @alphabet = ('A'..'Z');
                    597: 	my $i = 0;
1.116     foxr      598: 	my $bubble_number = 0;
1.124     albertel  599: 	my $line = 0;
1.83      albertel  600: 	my $temp=0;  
                    601: 	my $id=$Apache::inputtags::response['-1'];
                    602: 	my $part=$Apache::inputtags::part;
                    603: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    604: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.90      albertel  605: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    606: 	    $result.="<table><tr>";
                    607: 	}
1.123     albertel  608: 	foreach my $name (@{ $whichfoils }) {
1.83      albertel  609: 	    if ($target ne 'tex') {
1.90      albertel  610: 		if ($direction eq 'horizontal') {
                    611: 		    $result.="<td>"; 
                    612: 		} else { 
                    613: 		    $result.="<br />";
                    614: 		} 
                    615: 	    }
                    616: 	    if ($target ne 'tex') { 
1.94      matthew   617:                 $result.= '<label>';
1.113     albertel  618: 		$result.=
                    619: 		    "<input type=\"radio\"
                    620:                             onchange=\"javascript:setSubmittedPart('$part');\"
                    621:                             name=\"HWVAL_$Apache::inputtags::response['-1']\"
                    622:                             value=\"$temp\" ";
1.83      albertel  623: 		if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.108     albertel  624: 		$result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
1.83      albertel  625: 	    } else {
                    626: 		if ($Apache::lonhomework::type eq 'exam') {
1.116     foxr      627: 		    if($bubble_number >= $bubbles_per_line) {
1.124     albertel  628: 			$line++;
1.116     foxr      629: 			$i = 0;
                    630: 			$bubble_number = 0;
1.124     albertel  631: 			$result.='\item[\textbf{'.($Apache::lonxml::counter+$line).'}.]';
1.116     foxr      632: 		    }
1.124     albertel  633: 		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    634: 		    $i++;
                    635: 		    $bubble_number++;
1.83      albertel  636: 		} else {
                    637: 		    $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
                    638: 		}
                    639: 	    }
1.90      albertel  640: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
                    641: 		$result.="</td>"; 
                    642: 	    }
1.83      albertel  643: 	    $temp++;
                    644: 	}
1.90      albertel  645: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    646: 	    $result.="</tr></table>";
                    647: 	}
1.83      albertel  648:     }
1.92      albertel  649:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
1.83      albertel  650:     return $result;
1.81      albertel  651: }
                    652: 
                    653: sub displayallanswers {
1.106     albertel  654:     my @names;
                    655:     if ( $Apache::response::foilgroup{'names'} ) {
                    656: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    657:     }
1.81      albertel  658:     my $result=&Apache::response::answer_header('radiobuttonresponse');
                    659:     foreach my $name (@names) {
                    660: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                    661: 				$Apache::response::foilgroup{$name.'.value'});
                    662:     }
                    663:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    664:     return $result;
1.14      albertel  665: }
                    666: 
1.28      albertel  667: sub displayanswers {
1.124     albertel  668:     my ($answer, $whichopt, $bubbles_per_line)=@_;
                    669:     my $result;
                    670: 
1.105     albertel  671:     if ($Apache::lonhomework::type eq 'exam') {
1.124     albertel  672: 	my $line = int($answer/$bubbles_per_line);
                    673: 	my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
                    674: 	$result .= &Apache::response::answer_header('radiobuttonresponse',
                    675: 						    $line);
                    676: 	$result .= &Apache::response::answer_part('radiobuttonresponse',
                    677: 						  $correct);
                    678:     } else {
                    679: 	$result .= &Apache::response::answer_header('radiobuttonresponse');
1.105     albertel  680:     }
1.123     albertel  681:     foreach my $name (@{ $whichopt }) {
1.83      albertel  682: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
1.105     albertel  683: 						$Apache::response::foilgroup{$name.'.value'});
                    684:     }
1.83      albertel  685:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    686:     return $result;
1.28      albertel  687: }
                    688: 
1.14      albertel  689: sub start_conceptgroup {
1.83      albertel  690:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    691:     $Apache::radiobuttonresponse::conceptgroup=1;
                    692:     %Apache::response::conceptgroup=();
                    693:     my $result;
                    694:     if ($target eq 'edit') {
                    695: 	$result.=&Apache::edit::tag_start($target,$token);
                    696: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    697: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    698:     } elsif ($target eq 'modified') {
                    699: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    700: 						     $safeeval,'concept');
                    701: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    702:     }
                    703:     return $result;
1.14      albertel  704: }
                    705: 
                    706: sub end_conceptgroup {
1.83      albertel  707:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    708:     $Apache::radiobuttonresponse::conceptgroup=0;
                    709:     my $result;
                    710:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
                    711: 	$target eq 'tex' || $target eq 'analyze') {
                    712: 	&Apache::response::pick_foil_for_concept($target,
                    713: 						 ['value','text','location'],
                    714: 						 \%Apache::hint::radiobutton,
                    715: 						 $parstack,$safeeval);
                    716:     } elsif ($target eq 'edit') {
                    717: 	$result=&Apache::edit::end_table();
                    718:     }
                    719:     return $result;
1.26      albertel  720: }
                    721: 
                    722: sub insert_conceptgroup {
1.83      albertel  723:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    724:     return $result;
1.1       albertel  725: }
                    726: 
                    727: sub start_foil {
1.83      albertel  728:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    729:     my $result='';
                    730:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    731: 	&Apache::lonxml::startredirection;
1.95      albertel  732: 	if ($target eq 'analyze') {
                    733: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    734: 	}
1.83      albertel  735:     } elsif ($target eq 'edit') {
                    736: 	$result=&Apache::edit::tag_start($target,$token);
                    737: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    738: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
                    739: 						   ['unused','true','false'],
                    740: 						   $token);
                    741: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    742: 						 $safeeval,'-3');
                    743: 	if ($randomize ne 'no') {
                    744: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    745: 					       ['random','top','bottom'],$token);
                    746: 	}
                    747: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    748:     } elsif ($target eq 'modified') {
                    749: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    750: 						     $safeeval,'value','name',
                    751: 						     'location');
                    752: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    753:     } 
                    754:     return $result;
1.1       albertel  755: }
                    756: 
                    757: sub end_foil {
1.83      albertel  758:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    759:     my $text='';
                    760:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    761: 	$text=&Apache::lonxml::endredirection;
                    762:     }
1.85      albertel  763:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
                    764: 	|| $target eq 'tex' || $target eq 'analyze') {
1.83      albertel  765: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    766: 	if ($value ne 'unused') {
                    767: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.102     albertel  768: 	    if ($name eq "") {
1.134     bisitz    769: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.98      albertel  770: 		$name=$Apache::lonxml::curdepth;
                    771: 	    }
1.85      albertel  772: 	    if (defined($Apache::response::foilnames{$name})) {
1.134     bisitz    773: 		&Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
1.85      albertel  774: 	    }
1.86      albertel  775: 	    $Apache::response::foilnames{$name}++;
1.85      albertel  776: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
                    777: 						     $safeeval);
1.83      albertel  778: 	    if ( $Apache::radiobuttonresponse::conceptgroup
                    779: 		 && !&Apache::response::showallfoils() ) {
                    780: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    781: 		$Apache::response::conceptgroup{"$name.value"} = $value;
                    782: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
                    783: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    784: 	    } else {
                    785: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    786: 		$Apache::response::foilgroup{"$name.value"} = $value;
                    787: 		$Apache::response::foilgroup{"$name.text"} = $text;
                    788: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    789: 	    }
                    790: 	}
1.18      albertel  791:     }
1.83      albertel  792:     return '';
1.1       albertel  793: }
                    794: 
1.27      albertel  795: sub insert_foil {
1.83      albertel  796:     return '
1.27      albertel  797: <foil name="" value="unused">
                    798: <startouttext />
                    799: <endouttext />
                    800: </foil>';
                    801: }
1.1       albertel  802: 1;
                    803: __END__
                    804:  

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