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

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

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