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

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.31      albertel    3: #
1.136   ! bisitz      4: # $Id: radiobuttonresponse.pm,v 1.135 2008/09/05 12:50:45 onken 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.136   ! bisitz     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',
                     79: 				    $token,'4').
1.136   ! bisitz     80: 	    &Apache::edit::select_arg('Randomize Foil Order:','randomize',
1.83      albertel   81: 				      ['yes','no'],$token).
1.103     albertel   82: 	    &Apache::edit::select_arg('Display Direction','direction',
                     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: 	}
1.135     onken     105:         if($env{'form.pdfFormFields'} eq 'yes') {
                    106:             $result .= &Apache::lonxml::print_pdf_hiddenfield('meta', $env{'user.name'}, $env{'user.domain'});
                    107:             $result .= "\n\\\\\n\\\\\n";
                    108:         } else {
                    109:             $result .= '\begin{enumerate}';
                    110:         }
1.83      albertel  111:     } elsif ($target eq 'analyze') {
                    112: 	my $part_id="$Apache::inputtags::part.$id";
1.131     raeburn   113:         $Apache::lonhomework::analyze{"$part_id.type"} = 'radiobuttonresponse';
1.83      albertel  114: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
                    115:     }
                    116:     return $result;
1.1       albertel  117: }
                    118: 
                    119: sub end_radiobuttonresponse {
1.83      albertel  120:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    121:     my $result;
                    122:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.135     onken     123:     if ($target eq 'tex' and $env{'form.pdfFormFields'} ne 'yes') { 
                    124:         $result .= '\end{enumerate}'; 
                    125:     }
1.83      albertel  126:     &Apache::response::end_response;
                    127:     pop @Apache::lonxml::namespace;
                    128:     &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.85      albertel  129:     undef(%Apache::response::foilnames);
1.83      albertel  130:     return $result;
1.1       albertel  131: }
                    132: 
1.43      albertel  133: %Apache::response::foilgroup=();
1.1       albertel  134: sub start_foilgroup {
1.83      albertel  135:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    136:     my $result;
                    137:     %Apache::response::foilgroup=();
                    138:     $Apache::radiobuttonresponse::conceptgroup=0;
1.89      albertel  139:     &Apache::response::pushrandomnumber();
1.83      albertel  140:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                    141: 	$result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
                    142:     }
                    143:     return $result;
1.5       albertel  144: }
                    145: 
1.15      albertel  146: sub storesurvey {
1.99      albertel  147:     if ( !&Apache::response::submitted() ) { return ''; }
1.100     albertel  148:     my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83      albertel  149:     &Apache::lonxml::debug("Here I am!:$response:");
                    150:     if ( $response !~ /[0-9]+/) { return ''; }
1.96      albertel  151:     my $part = $Apache::inputtags::part;
1.83      albertel  152:     my $id = $Apache::inputtags::response['-1'];
                    153:     my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
                    154:     my %responsehash;
                    155:     $responsehash{$whichfoils[$response]}=$response;
1.96      albertel  156:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    157:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    158: 	$responsestr;
                    159:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
                    160:     my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
                    161:     &Apache::response::handle_previous(\%previous,$ad);
1.83      albertel  162:     &Apache::lonxml::debug("submitted a $response<br />\n");
                    163:     return '';
1.15      albertel  164: }
                    165: 
1.120     foxr      166: 
1.32      albertel  167: sub grade_response {
1.123     albertel  168:     my ($answer, $whichfoils, $bubbles_per_line)=@_;
                    169: 
1.99      albertel  170:     if ( !&Apache::response::submitted() ) { return; }
1.83      albertel  171:     my $response;
1.118     foxr      172:     
1.100     albertel  173:     if ($env{'form.submitted'} eq 'scantron') {
1.121     foxr      174: 	$response = &Apache::response::getresponse(1,undef,
1.123     albertel  175: 						   &bubble_line_count(scalar(@{ $whichfoils}),
1.121     foxr      176: 								      $bubbles_per_line),
                    177: 						   $bubbles_per_line);
1.116     foxr      178: 
1.83      albertel  179:     } else {
1.100     albertel  180: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83      albertel  181:     }
1.120     foxr      182: 
1.118     foxr      183: 
1.83      albertel  184:     if ( $response !~ /[0-9]+/) { return; }
                    185:     my $part=$Apache::inputtags::part;
                    186:     my $id = $Apache::inputtags::response['-1'];
                    187:     my %responsehash;
1.123     albertel  188:     $responsehash{$whichfoils->[$response]}=$response;
1.83      albertel  189:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    190:     my %previous=&Apache::response::check_for_previous($responsestr,
                    191: 						       $part,$id);
                    192:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    193: 	$responsestr;
                    194:     &Apache::lonxml::debug("submitted a $response<br />\n");
                    195:     my $ad;
                    196:     if ($response == $answer) {
                    197: 	$ad='EXACT_ANS';
                    198:     } else {
                    199: 	$ad='INCORRECT';
                    200:     }
                    201:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    202:     &Apache::response::handle_previous(\%previous,$ad);
1.32      albertel  203: }
                    204: 
1.1       albertel  205: sub end_foilgroup {
1.83      albertel  206:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29      albertel  207: 
1.83      albertel  208:     my $result;
1.121     foxr      209:     my $bubble_lines;
                    210:     my $bubbles_per_line;
                    211:     my $answer_count;
1.129     foxr      212:     my $id   = $Apache::inputtags::response['-1'];
                    213:     my $part = $Apache::inputtags::part;
1.121     foxr      214:     $bubbles_per_line = 
                    215: 	&Apache::response::get_response_param($Apache::inputtags::part."_$id",
                    216: 					      'numbubbles',
                    217: 					      $default_bubbles_per_line);
                    218: 
                    219: 
1.83      albertel  220:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    221: 	$target eq 'tex' || $target eq 'analyze') {
                    222: 	my $style = $Apache::lonhomework::type;
1.93      albertel  223: 	my $direction = &Apache::lonxml::get_param('direction',$parstack,
                    224: 						   $safeeval,'-2');
1.83      albertel  225: 	if ( $style eq 'survey'  && $target ne 'analyze') {
                    226: 	    if ($target eq 'web' || $target eq 'tex') {
1.110     foxr      227: 		$result=&displayallfoils($direction, $target);
1.83      albertel  228: 	    } elsif ( $target eq 'answer' ) {
                    229: 		$result=&displayallanswers();
                    230: 	    } elsif ( $target eq 'grade' ) {
                    231: 		$result=&storesurvey();
                    232: 	    }
1.121     foxr      233: 	    $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
                    234: 
1.83      albertel  235: 	} else {
1.121     foxr      236: 
1.83      albertel  237: 	    my $name;
                    238: 	    my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
                    239: 						 '-2');
                    240: 	    my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    241: 						       $safeeval,'-2');
1.123     albertel  242: 	    my ($answer, @shown) = &whichfoils($max, $randomize);
1.121     foxr      243: 	    $answer_count = scalar(@shown);
                    244: 
1.83      albertel  245: 	    if ($target eq 'web' || $target eq 'tex') {
1.121     foxr      246: 		$result=&displayfoils($target,
1.123     albertel  247: 				      $answer, \@shown,
1.121     foxr      248: 				      $direction,
                    249: 				      $bubbles_per_line);
1.83      albertel  250: 	    } elsif ($target eq 'answer' ) {
1.124     albertel  251: 		$result=&displayanswers($answer, \@shown, $bubbles_per_line);
1.83      albertel  252: 	    } elsif ( $target eq 'grade') {
1.123     albertel  253: 		&grade_response($answer, \@shown, $bubbles_per_line);
1.83      albertel  254: 	    }  elsif ( $target eq 'analyze') {
1.126     foxr      255: 		my $bubble_lines = &bubble_line_count($answer_count, 
                    256: 						      $bubbles_per_line);
1.83      albertel  257: 		&Apache::response::analyze_store_foilgroup(\@shown,
                    258: 							   ['text','value','location']);
1.129     foxr      259: 		my $part_id="$part.$id";
1.83      albertel  260: 		push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
                    261: 		      ('true','false'));
1.130     foxr      262: 
1.83      albertel  263: 	    }
1.81      albertel  264: 	}
1.111     albertel  265: 	$Apache::lonxml::post_evaluate=0;
1.83      albertel  266:     }
1.114     albertel  267:     if ($target eq 'web') {
                    268: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
                    269: 						  [\%Apache::response::foilgroup]);
                    270:     }
1.128     foxr      271:     &Apache::response::poprandomnumber();
1.121     foxr      272:     $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
1.128     foxr      273:     &Apache::lonxml::increment_counter($bubble_lines,
1.129     foxr      274: 				       "$part.$id");
1.128     foxr      275:     if ($target eq 'analyze') {
                    276: 	&Apache::lonhomework::set_bubble_lines();
                    277:     }
1.83      albertel  278:     return $result;
1.6       albertel  279: }
                    280: 
                    281: sub getfoilcounts {
1.83      albertel  282:     my @names;
                    283:     my $truecnt=0;
                    284:     my $falsecnt=0;
                    285:     my $name;
                    286:     if ( $Apache::response::foilgroup{'names'} ) {
                    287: 	@names= @{ $Apache::response::foilgroup{'names'} };
1.6       albertel  288:     }
1.83      albertel  289:     foreach $name (@names) {
                    290: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    291: 	    $truecnt++;
                    292: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    293: 	    $falsecnt++;
                    294: 	}
                    295:     }
                    296:     return ($truecnt,$falsecnt);
1.5       albertel  297: }
                    298: 
1.114     albertel  299: sub format_prior_answer {
                    300:     my ($mode,$answer,$other_data) = @_;
                    301:     my $foil_data = $other_data->[0];
                    302:     my %response = &Apache::lonnet::str2hash($answer);
                    303:     my ($name)   = keys(%response);
                    304:     return '<span class="LC_prior_radiobutton">'.
                    305: 	$foil_data->{$name.'.text'}.'</span>';
                    306: 
1.112     albertel  307: }
                    308: 
1.15      albertel  309: sub displayallfoils {
1.110     foxr      310:     my ($direction, $target)=@_;
1.83      albertel  311:     my $result;
                    312:     &Apache::lonxml::debug("survey style display");
1.106     albertel  313:     my @names;
                    314:     if ( $Apache::response::foilgroup{'names'} ) {
                    315: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    316:     }
1.120     foxr      317: 
1.83      albertel  318:     my $temp=0;
1.110     foxr      319:     my $i   =0;
1.83      albertel  320:     my $id=$Apache::inputtags::response['-1'];
                    321:     my $part=$Apache::inputtags::part;
                    322:     my $lastresponse=
                    323: 	$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.93      albertel  324:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
1.83      albertel  325:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
                    326:     if (&Apache::response::show_answer() ) {
                    327: 	foreach my $name (@names) {
                    328: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.110     foxr      329: 		if (($direction eq 'horizontal') && ($target ne 'tex')) {
1.93      albertel  330: 		    $result.="<td>";
                    331: 		} else {
1.110     foxr      332: 		    if ($target eq 'tex') {
                    333: 			$result .= '\item \vskip -2mm ';
                    334: 		    } else {
                    335: 			$result.="<br />";
                    336: 		    }
1.93      albertel  337: 		}
1.84      albertel  338: 		if (defined($lastresponse{$name})) {
1.110     foxr      339: 		    if ($target eq 'tex') {
                    340: 			$result .= '}';
                    341: 		    } else {
                    342: 			$result.='<b>';
                    343: 		    }
1.83      albertel  344: 		}
                    345: 		$result .= $Apache::response::foilgroup{$name.'.text'};
1.110     foxr      346: 		if (defined($lastresponse{$name}) && ($target ne 'tex')) {
1.83      albertel  347: 		    $result.='</b>';
                    348: 		}
1.110     foxr      349: 		if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
1.83      albertel  350: 	    }
1.45      albertel  351: 	}
1.83      albertel  352:     } else {
                    353: 	foreach my $name (@names) {
                    354: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.93      albertel  355: 		if ($direction eq 'horizontal') {
                    356: 		    $result.="<td>";
                    357: 		} else {
1.110     foxr      358: 		    if ($target eq 'tex') {
1.135     onken     359: 		        if($env{'form.pdfFormFields'} eq 'yes') {
                    360:                             my $fieldname = $env{'request.symb'}.
                    361:                                            '&part_'. $Apache::inputtags::part.
                    362:                                            '&radiobuttonresponse'.
                    363:                                            '&HWVAL_' . $Apache::inputtags::response['-1'];
                    364:                             my $value = $temp;
                    365:                             my $text = $Apache::response::foilgroup{$name.'.text'};
                    366:                             $result .= &Apache::lonxml::print_pdf_radiobutton($fieldname,
                    367:                                                                              $value,
                    368:                                                                              $text)."\n";
                    369:                         } else {
                    370:                             $result .= '\item \vskip -2mm ';
                    371:                         }
                    372:                     } else {
1.110     foxr      373: 			$result.="<br />";
                    374: 		    }
                    375: 		}
                    376: 		if ($target eq 'tex') {
1.135     onken     377: 		    if($env{'form.pdfFormFields'} ne 'yes') {
                    378:                         $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    379:                     }
1.110     foxr      380: 		    $i++;
                    381: 		} else {
                    382: 		    $result .= '<label>';
1.113     albertel  383: 		    $result.="<input
                    384:                        onchange=\"javascript:setSubmittedPart('$part');\"
                    385:                        type=\"radio\"
                    386:                        name=\"HWVAL_$Apache::inputtags::response['-1']\"
                    387:                        value=\"$temp\" ";
1.110     foxr      388: 		    if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
                    389: 		    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
                    390: 			'</label>';
1.93      albertel  391: 		}
1.83      albertel  392: 		$temp++;
1.110     foxr      393: 		if ($target ne 'tex') {
                    394: 		    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
                    395: 		} else {
                    396: 		    $result.='\vskip 0 mm ';
                    397: 		}
1.83      albertel  398: 	    }
1.45      albertel  399: 	}
                    400:     }
1.120     foxr      401:     
1.110     foxr      402:     if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
1.83      albertel  403:     return $result;
1.15      albertel  404: }
                    405: 
1.122     albertel  406: =pod
                    407: 
                    408: =item &whichfoils($max,$randomize)
                    409: 
                    410: Randomizes the list of foils.
                    411: Respects
                    412:   - each foils desire to be randomized
                    413:   - the existance of Concept groups of foils (select 1 foil from each)
                    414:   - and selects a single correct statement from all possilble true statments
                    415:   - and limits it to a toal of $max foils
                    416: 
1.123     albertel  417: WARNING: this routine uses the random number generator, it should only
                    418: be called once per target, otherwise it can cause randomness changes in
                    419: homework problems.
                    420: 
1.122     albertel  421: Arguments
                    422:   $max - maximum number of foils to select (including the true one)
                    423:          (so a max of 5 is: 1 true, 4 false)
                    424: 
                    425:   $randomize - whether to randomize the listing of foils, by default
                    426:                will randomize, only if randomize is 'no' will it not
                    427: 
                    428: Returns
                    429:   $answer - location in the array of the correct answer
                    430:   @foils  - array of foil names in to display order
                    431: 
                    432: =cut
                    433: 
1.28      albertel  434: sub whichfoils {
1.83      albertel  435:     my ($max,$randomize)=@_;
1.28      albertel  436: 
1.83      albertel  437:     my @truelist;
                    438:     my @falselist;
                    439:     my @whichfalse =();
                    440:     my ($truecnt,$falsecnt) = &getfoilcounts();
                    441:     my $count=0;
                    442:     # we will add in 1 of the true statements
1.104     albertel  443:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
1.83      albertel  444:     my $answer=int(&Math::Random::random_uniform() * ($count));
                    445:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
                    446:     my @names;
                    447:     if ( $Apache::response::foilgroup{'names'} ) {
                    448: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    449:     }
                    450:     if (&Apache::response::showallfoils()) {
                    451: 	@whichfalse=@names;
                    452:     } elsif ($randomize eq 'no') {
                    453: 	&Apache::lonxml::debug("No randomization");
                    454: 	my $havetrue=0;
                    455: 	foreach my $name (@names) {
                    456: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    457: 		if (!$havetrue ) {
                    458: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
                    459: 		}
                    460: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    461: 		push (@whichfalse,$name);
                    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: 	}
1.97      albertel  467: 	if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
1.134     bisitz    468: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
1.97      albertel  469: 	}
1.83      albertel  470:     } else {
                    471: 	my $current=0;
                    472: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
                    473: 	my (%top,%bottom);
                    474: 	#first find out where everyone wants to be
                    475: 	foreach my $name (@names) {
                    476: 	    $current++;
                    477: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    478: 		push (@truelist,$name);
                    479: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    480: 		    $top{$name}=$current;
                    481: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    482: 		    $bottom{$name}=$current;
                    483: 		}
                    484: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    485: 		push (@falselist,$name);
                    486: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    487: 		    $top{$name}=$current;
                    488: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    489: 		    $bottom{$name}=$current;
                    490: 		}
                    491: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    492: 	    } else {
1.87      albertel  493: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  494: 	    }
                    495: 	}
                    496: 	#pick a true statement
                    497: 	my $notrue=0;
                    498: 	if (scalar(@truelist) == 0) { $notrue=1; }
                    499: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
                    500: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    501: 	my (@toplist, @bottomlist);
                    502: 	my $topcount=0;
                    503: 	my $bottomcount=0;
                    504: 	# assign everyone to either toplist/bottomlist or whichfalse
                    505: 	# which false is randomized, toplist bottomlist are in order
                    506: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
                    507: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    508: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
                    509: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    510: 	    $afalse=splice(@falselist,$afalse,1);
                    511: 	    &Apache::lonxml::debug("Picked $afalse");
                    512: 	    &Apache::lonhomework::showhash(('names'=>\@names));
                    513: 	    &Apache::lonhomework::showhash(%top);
                    514: 	    if ($top{$afalse}) {
                    515: 		$toplist[$top{$afalse}]=$afalse;
                    516: 		$topcount++;
                    517: 	    } elsif ($bottom{$afalse}) {
                    518: 		$bottomlist[$bottom{$afalse}]=$afalse;
                    519: 		$bottomcount++;
                    520: 	    } else {
                    521: 		push (@whichfalse,$afalse);
                    522: 	    }
                    523: 	}
                    524: 	&Apache::lonxml::debug("Answer wants $answer");
                    525: 	my $truename=$truelist[$whichtrue];
                    526: 	my $dosplice=1;
                    527: 	if ($notrue && $Apache::lonhomework::type ne 'survey') {
                    528: 	    $dosplice=0;
1.134     bisitz    529: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
1.83      albertel  530: 	}
                    531: 	#insert the true statement, keeping track of where it wants to be
                    532: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
                    533: 	    $toplist[$top{$truename}]=$truename;
                    534: 	    $answer=-1;
                    535: 	    foreach my $top (reverse(@toplist)) {
                    536: 		if ($top) { $answer++;}
                    537: 		if ($top eq $truename) { last; }
1.49      albertel  538: 	    }
1.83      albertel  539: 	    $dosplice=0;
                    540: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
                    541: 	    $bottomlist[$bottom{$truename}]=$truename;
                    542: 	    $answer=-1;
                    543: 	    foreach my $bot (@bottomlist) {
                    544: 		if ($bot) { $answer++;}
                    545: 		if ($bot eq $truename) { last; }
1.49      albertel  546: 	    }
1.83      albertel  547: 	    $answer+=$topcount+$#whichfalse+1;
                    548: 	    $dosplice=0;
1.49      albertel  549: 	} else {
1.83      albertel  550: 	    if ($topcount>0 || $bottomcount>0) {
                    551: 		$answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
                    552: 		    + $topcount;
                    553: 	    }
                    554: 	}
                    555: 	&Apache::lonxml::debug("Answer now wants $answer");
                    556: 	#add the top items to the top, bottom items to the bottom
                    557: 	for (my $i=0;$i<=$#toplist;$i++) {
                    558: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
1.49      albertel  559: 	}
1.83      albertel  560: 	for (my $i=0;$i<=$#bottomlist;$i++) {
                    561: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
1.49      albertel  562: 	}
1.83      albertel  563: 	#if the true statement is randomized insert it into the list
                    564: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.49      albertel  565:     }
1.83      albertel  566:     &Apache::lonxml::debug("Answer is $answer");
                    567:     return ($answer,@whichfalse);
1.28      albertel  568: }
                    569: 
                    570: sub displayfoils {
1.123     albertel  571:     my ($target,$answer,$whichfoils,$direction, $bubbles_per_line)=@_;
1.83      albertel  572:     my $result;
1.28      albertel  573: 
1.22      albertel  574:     my $part=$Apache::inputtags::part;
1.83      albertel  575:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    576:     if ( ($target ne 'tex') &&
                    577: 	 &Apache::response::show_answer() ) {
1.90      albertel  578: 	if ($direction eq 'horizontal') {
                    579: 	    if ($target ne 'tex') {
                    580: 		$result.='<table><tr>';
                    581: 	    }
                    582: 	}
1.123     albertel  583: 	foreach my $name (@{ $whichfoils }) {
1.90      albertel  584: 	    if ($direction eq 'horizontal') {
                    585: 		if ($target ne 'tex') { $result.='<td>'; }
                    586: 	    }
1.83      albertel  587: 	    if ($target ne 'tex') {
                    588: 		$result.="<br />";
                    589: 	    } else {
                    590: 		$result.='\item \vskip -2 mm  ';
                    591: 	    }
                    592: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
1.132     bisitz    593: 		if ($target ne 'tex') {
                    594:                     $result.=&mt('Correct:').'<b>';
                    595:                 } else {
                    596:                     $result.=&mt('Correct:').' \textbf{';
                    597:                 }
1.83      albertel  598: 	    } else {
1.132     bisitz    599: 		$result.=&mt('Incorrect:');
1.83      albertel  600: 	    }
1.94      matthew   601: 	    if ($target eq 'web') { $result.="<label>"; }
1.90      albertel  602: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
1.94      matthew   603: 	    if ($target eq 'web') { $result.="</label>"; }
1.83      albertel  604: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    605: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
                    606: 	    }
1.90      albertel  607: 	    if ($direction eq 'horizontal') {
                    608: 		if ($target ne 'tex') { $result.='</td>'; }
                    609: 	    }
                    610: 	}
                    611: 	if ($direction eq 'horizontal') {
                    612: 	    if ($target ne 'tex') {
                    613: 		$result.='</tr></table>';
                    614: 	    }
1.83      albertel  615: 	}
                    616:     } else {
                    617: 	my @alphabet = ('A'..'Z');
                    618: 	my $i = 0;
1.116     foxr      619: 	my $bubble_number = 0;
1.124     albertel  620: 	my $line = 0;
1.83      albertel  621: 	my $temp=0;  
                    622: 	my $id=$Apache::inputtags::response['-1'];
                    623: 	my $part=$Apache::inputtags::part;
                    624: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    625: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.90      albertel  626: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    627: 	    $result.="<table><tr>";
                    628: 	}
1.123     albertel  629: 	foreach my $name (@{ $whichfoils }) {
1.83      albertel  630: 	    if ($target ne 'tex') {
1.90      albertel  631: 		if ($direction eq 'horizontal') {
                    632: 		    $result.="<td>"; 
                    633: 		} else { 
                    634: 		    $result.="<br />";
                    635: 		} 
                    636: 	    }
                    637: 	    if ($target ne 'tex') { 
1.94      matthew   638:                 $result.= '<label>';
1.113     albertel  639: 		$result.=
                    640: 		    "<input type=\"radio\"
                    641:                             onchange=\"javascript:setSubmittedPart('$part');\"
                    642:                             name=\"HWVAL_$Apache::inputtags::response['-1']\"
                    643:                             value=\"$temp\" ";
1.83      albertel  644: 		if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.108     albertel  645: 		$result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
1.83      albertel  646: 	    } else {
                    647: 		if ($Apache::lonhomework::type eq 'exam') {
1.116     foxr      648: 		    if($bubble_number >= $bubbles_per_line) {
1.124     albertel  649: 			$line++;
1.116     foxr      650: 			$i = 0;
                    651: 			$bubble_number = 0;
1.124     albertel  652: 			$result.='\item[\textbf{'.($Apache::lonxml::counter+$line).'}.]';
1.116     foxr      653: 		    }
1.124     albertel  654: 		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    655: 		    $i++;
                    656: 		    $bubble_number++;
1.83      albertel  657: 		} else {
1.135     onken     658: 		      if($env{'form.pdfFormFields'} eq 'yes') {
                    659: 
                    660:                          my $fieldname = $env{'request.symb'}.
                    661:                                          '&part_'. $Apache::inputtags::part.
                    662:                                          '&radiobuttonresponse'.
                    663:                                          '&HWVAL_' . $Apache::inputtags::response['-1'];
                    664:                          my $value = $temp;
                    665:                          my $text = $Apache::response::foilgroup{$name.'.text'};
                    666:                          $result .= &Apache::lonxml::print_pdf_radiobutton($fieldname, $value, $text).'\newline'."\n";
                    667:                      } else { 
                    668:                          $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
                    669:                      }
                    670:                 }
1.83      albertel  671: 	    }
1.90      albertel  672: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
                    673: 		$result.="</td>"; 
                    674: 	    }
1.83      albertel  675: 	    $temp++;
                    676: 	}
1.90      albertel  677: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    678: 	    $result.="</tr></table>";
                    679: 	}
1.83      albertel  680:     }
1.92      albertel  681:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
1.83      albertel  682:     return $result;
1.81      albertel  683: }
                    684: 
                    685: sub displayallanswers {
1.106     albertel  686:     my @names;
                    687:     if ( $Apache::response::foilgroup{'names'} ) {
                    688: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    689:     }
1.81      albertel  690:     my $result=&Apache::response::answer_header('radiobuttonresponse');
                    691:     foreach my $name (@names) {
                    692: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                    693: 				$Apache::response::foilgroup{$name.'.value'});
                    694:     }
                    695:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    696:     return $result;
1.14      albertel  697: }
                    698: 
1.28      albertel  699: sub displayanswers {
1.124     albertel  700:     my ($answer, $whichopt, $bubbles_per_line)=@_;
                    701:     my $result;
                    702: 
1.105     albertel  703:     if ($Apache::lonhomework::type eq 'exam') {
1.124     albertel  704: 	my $line = int($answer/$bubbles_per_line);
                    705: 	my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
                    706: 	$result .= &Apache::response::answer_header('radiobuttonresponse',
                    707: 						    $line);
                    708: 	$result .= &Apache::response::answer_part('radiobuttonresponse',
                    709: 						  $correct);
                    710:     } else {
                    711: 	$result .= &Apache::response::answer_header('radiobuttonresponse');
1.105     albertel  712:     }
1.123     albertel  713:     foreach my $name (@{ $whichopt }) {
1.83      albertel  714: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
1.105     albertel  715: 						$Apache::response::foilgroup{$name.'.value'});
                    716:     }
1.83      albertel  717:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    718:     return $result;
1.28      albertel  719: }
                    720: 
1.14      albertel  721: sub start_conceptgroup {
1.83      albertel  722:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    723:     $Apache::radiobuttonresponse::conceptgroup=1;
                    724:     %Apache::response::conceptgroup=();
                    725:     my $result;
                    726:     if ($target eq 'edit') {
                    727: 	$result.=&Apache::edit::tag_start($target,$token);
                    728: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    729: 	    &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,'concept');
                    733: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    734:     }
                    735:     return $result;
1.14      albertel  736: }
                    737: 
                    738: sub end_conceptgroup {
1.83      albertel  739:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    740:     $Apache::radiobuttonresponse::conceptgroup=0;
                    741:     my $result;
                    742:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
                    743: 	$target eq 'tex' || $target eq 'analyze') {
                    744: 	&Apache::response::pick_foil_for_concept($target,
                    745: 						 ['value','text','location'],
                    746: 						 \%Apache::hint::radiobutton,
                    747: 						 $parstack,$safeeval);
                    748:     } elsif ($target eq 'edit') {
                    749: 	$result=&Apache::edit::end_table();
                    750:     }
                    751:     return $result;
1.26      albertel  752: }
                    753: 
                    754: sub insert_conceptgroup {
1.83      albertel  755:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    756:     return $result;
1.1       albertel  757: }
                    758: 
                    759: sub start_foil {
1.83      albertel  760:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    761:     my $result='';
                    762:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    763: 	&Apache::lonxml::startredirection;
1.95      albertel  764: 	if ($target eq 'analyze') {
                    765: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    766: 	}
1.83      albertel  767:     } elsif ($target eq 'edit') {
                    768: 	$result=&Apache::edit::tag_start($target,$token);
                    769: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    770: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
                    771: 						   ['unused','true','false'],
                    772: 						   $token);
                    773: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    774: 						 $safeeval,'-3');
                    775: 	if ($randomize ne 'no') {
                    776: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    777: 					       ['random','top','bottom'],$token);
                    778: 	}
                    779: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    780:     } elsif ($target eq 'modified') {
                    781: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    782: 						     $safeeval,'value','name',
                    783: 						     'location');
                    784: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    785:     } 
                    786:     return $result;
1.1       albertel  787: }
                    788: 
                    789: sub end_foil {
1.83      albertel  790:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    791:     my $text='';
                    792:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    793: 	$text=&Apache::lonxml::endredirection;
                    794:     }
1.85      albertel  795:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
                    796: 	|| $target eq 'tex' || $target eq 'analyze') {
1.83      albertel  797: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    798: 	if ($value ne 'unused') {
                    799: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.102     albertel  800: 	    if ($name eq "") {
1.134     bisitz    801: 		&Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.98      albertel  802: 		$name=$Apache::lonxml::curdepth;
                    803: 	    }
1.85      albertel  804: 	    if (defined($Apache::response::foilnames{$name})) {
1.134     bisitz    805: 		&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  806: 	    }
1.86      albertel  807: 	    $Apache::response::foilnames{$name}++;
1.85      albertel  808: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
                    809: 						     $safeeval);
1.83      albertel  810: 	    if ( $Apache::radiobuttonresponse::conceptgroup
                    811: 		 && !&Apache::response::showallfoils() ) {
                    812: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    813: 		$Apache::response::conceptgroup{"$name.value"} = $value;
                    814: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
                    815: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    816: 	    } else {
                    817: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    818: 		$Apache::response::foilgroup{"$name.value"} = $value;
                    819: 		$Apache::response::foilgroup{"$name.text"} = $text;
                    820: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    821: 	    }
                    822: 	}
1.18      albertel  823:     }
1.83      albertel  824:     return '';
1.1       albertel  825: }
                    826: 
1.27      albertel  827: sub insert_foil {
1.83      albertel  828:     return '
1.27      albertel  829: <foil name="" value="unused">
                    830: <startouttext />
                    831: <endouttext />
                    832: </foil>';
                    833: }
1.1       albertel  834: 1;
                    835: __END__
                    836:  

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