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

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.31      albertel    3: #
1.114   ! albertel    4: # $Id: radiobuttonresponse.pm,v 1.113 2007/03/27 19:20:49 albertel Exp $
1.31      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   28: 
                     29: package Apache::radiobuttonresponse;
                     30: use strict;
1.42      albertel   31: use HTML::Entities();
1.85      albertel   32: use Apache::lonlocal;
1.100     albertel   33: use Apache::lonnet;
1.1       albertel   34: 
1.36      harris41   35: BEGIN {
1.83      albertel   36:     &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1       albertel   37: }
                     38: 
                     39: sub start_radiobuttonresponse {
1.83      albertel   40:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     41:     my $result;
                     42:     #when in a radiobutton response use these
                     43:     &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
                     44:     push (@Apache::lonxml::namespace,'radiobuttonresponse');
                     45:     my $id = &Apache::response::start_response($parstack,$safeeval);
                     46:     %Apache::hint::radiobutton=();
1.85      albertel   47:     undef(%Apache::response::foilnames);
1.83      albertel   48:     if ($target eq 'meta') {
                     49: 	$result=&Apache::response::meta_package_write('radiobuttonresponse');
                     50:     } elsif ($target eq 'edit' ) {
                     51: 	$result.=&Apache::edit::start_table($token).
                     52: 	    '<tr><td>'.&Apache::lonxml::description($token).
                     53: 	    &Apache::loncommon::help_open_topic('Radio_Response_Problems').
                     54: 	    "</td><td>Delete:".
                     55: 	    &Apache::edit::deletelist($target,$token)
                     56: 	    ."</td><td>&nbsp".&Apache::edit::end_row()
                     57: 	    .&Apache::edit::start_spanning_row();
                     58: 	$result.=
                     59: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',
                     60: 				    $token,'4').
                     61: 	    &Apache::edit::select_arg('Randomize Foil Order','randomize',
                     62: 				      ['yes','no'],$token).
1.103     albertel   63: 	    &Apache::edit::select_arg('Display Direction','direction',
                     64: 				      ['vertical','horizontal'],$token).
1.83      albertel   65: 				      &Apache::edit::end_row().
                     66: 				      &Apache::edit::start_spanning_row()."\n";
                     67:     } elsif ($target eq 'modified') {
                     68: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                     69: 						     $safeeval,'max',
1.103     albertel   70: 						     'randomize','direction');
1.83      albertel   71: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                     72:     } elsif ($target eq 'tex') {
                     73: 	my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,
                     74: 					    undef,0);
                     75: 	if ($type eq '1') {
                     76: 	    $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
                     77: 	} elsif ($type eq 'A') {
                     78: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
                     79: 	} elsif ($type eq 'a') {
                     80: 	    $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
                     81: 	} elsif ($type eq 'i') {
                     82: 	    $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
1.88      albertel   83: 	} else {
                     84: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
1.83      albertel   85: 	}
                     86: 	$result .= '\begin{enumerate}';
                     87:     } elsif ($target eq 'analyze') {
                     88: 	my $part_id="$Apache::inputtags::part.$id";
                     89: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
                     90:     }
                     91:     return $result;
1.1       albertel   92: }
                     93: 
                     94: sub end_radiobuttonresponse {
1.83      albertel   95:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     96:     my $result;
                     97:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
                     98:     if ($target eq 'tex') { $result .= '\end{enumerate}'; }
                     99:     &Apache::response::end_response;
                    100:     pop @Apache::lonxml::namespace;
                    101:     &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.85      albertel  102:     undef(%Apache::response::foilnames);
1.83      albertel  103:     return $result;
1.1       albertel  104: }
                    105: 
1.43      albertel  106: %Apache::response::foilgroup=();
1.1       albertel  107: sub start_foilgroup {
1.83      albertel  108:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    109:     my $result;
                    110:     %Apache::response::foilgroup=();
                    111:     $Apache::radiobuttonresponse::conceptgroup=0;
1.89      albertel  112:     &Apache::response::pushrandomnumber();
1.83      albertel  113:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                    114: 	$result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
                    115:     }
                    116:     return $result;
1.5       albertel  117: }
                    118: 
1.15      albertel  119: sub storesurvey {
1.99      albertel  120:     if ( !&Apache::response::submitted() ) { return ''; }
1.100     albertel  121:     my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83      albertel  122:     &Apache::lonxml::debug("Here I am!:$response:");
                    123:     if ( $response !~ /[0-9]+/) { return ''; }
1.96      albertel  124:     my $part = $Apache::inputtags::part;
1.83      albertel  125:     my $id = $Apache::inputtags::response['-1'];
                    126:     my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
                    127:     my %responsehash;
                    128:     $responsehash{$whichfoils[$response]}=$response;
1.96      albertel  129:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    130:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    131: 	$responsestr;
                    132:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
                    133:     my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
                    134:     &Apache::response::handle_previous(\%previous,$ad);
1.83      albertel  135:     &Apache::lonxml::debug("submitted a $response<br />\n");
                    136:     return '';
1.15      albertel  137: }
                    138: 
1.32      albertel  139: sub grade_response {
1.83      albertel  140:     my ($max,$randomize)=@_;
                    141:     #keep the random numbers the same must always call this
                    142:     my ($answer,@whichfoils)=&whichfoils($max,$randomize);
1.99      albertel  143:     if ( !&Apache::response::submitted() ) { return; }
1.83      albertel  144:     my $response;
1.100     albertel  145:     if ($env{'form.submitted'} eq 'scantron') {
1.83      albertel  146: 	$response=&Apache::response::getresponse();
                    147:     } else {
1.100     albertel  148: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83      albertel  149:     }
                    150:     if ( $response !~ /[0-9]+/) { return; }
                    151:     my $part=$Apache::inputtags::part;
                    152:     my $id = $Apache::inputtags::response['-1'];
                    153:     my %responsehash;
                    154:     $responsehash{$whichfoils[$response]}=$response;
                    155:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    156:     my %previous=&Apache::response::check_for_previous($responsestr,
                    157: 						       $part,$id);
                    158:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    159: 	$responsestr;
                    160:     &Apache::lonxml::debug("submitted a $response<br />\n");
                    161:     my $ad;
                    162:     if ($response == $answer) {
                    163: 	$ad='EXACT_ANS';
                    164:     } else {
                    165: 	$ad='INCORRECT';
                    166:     }
                    167:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    168:     &Apache::response::handle_previous(\%previous,$ad);
1.32      albertel  169: }
                    170: 
1.1       albertel  171: sub end_foilgroup {
1.83      albertel  172:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29      albertel  173: 
1.83      albertel  174:     my $result;
                    175:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    176: 	$target eq 'tex' || $target eq 'analyze') {
                    177: 	my $style = $Apache::lonhomework::type;
1.93      albertel  178: 	my $direction = &Apache::lonxml::get_param('direction',$parstack,
                    179: 						   $safeeval,'-2');
1.83      albertel  180: 	if ( $style eq 'survey'  && $target ne 'analyze') {
                    181: 	    if ($target eq 'web' || $target eq 'tex') {
1.110     foxr      182: 		$result=&displayallfoils($direction, $target);
1.83      albertel  183: 	    } elsif ( $target eq 'answer' ) {
                    184: 		$result=&displayallanswers();
                    185: 	    } elsif ( $target eq 'grade' ) {
                    186: 		$result=&storesurvey();
                    187: 	    }
                    188: 	} else {
                    189: 	    my $name;
                    190: 	    my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
                    191: 						 '-2');
                    192: 	    my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    193: 						       $safeeval,'-2');
                    194: 	    if ($target eq 'web' || $target eq 'tex') {
1.90      albertel  195: 		$result=&displayfoils($target,$max,$randomize,$direction);
1.83      albertel  196: 	    } elsif ($target eq 'answer' ) {
                    197: 		$result=&displayanswers($max,$randomize);
                    198: 	    } elsif ( $target eq 'grade') {
                    199: 		&grade_response($max,$randomize);
                    200: 	    }  elsif ( $target eq 'analyze') {
                    201: 		my @shown = &whichfoils($max,$randomize);
                    202: 		&Apache::response::analyze_store_foilgroup(\@shown,
                    203: 							   ['text','value','location']);
                    204: 		my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    205: 		push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
                    206: 		      ('true','false'));
                    207: 	    }
1.81      albertel  208: 	}
1.111     albertel  209: 	$Apache::lonxml::post_evaluate=0;
1.83      albertel  210:     }
1.114   ! albertel  211:     if ($target eq 'web') {
        !           212: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
        !           213: 						  [\%Apache::response::foilgroup]);
        !           214:     }
        !           215:     
1.89      albertel  216:     &Apache::response::poprandomnumber();
1.83      albertel  217:     &Apache::lonxml::increment_counter();
                    218:     return $result;
1.6       albertel  219: }
                    220: 
                    221: sub getfoilcounts {
1.83      albertel  222:     my @names;
                    223:     my $truecnt=0;
                    224:     my $falsecnt=0;
                    225:     my $name;
                    226:     if ( $Apache::response::foilgroup{'names'} ) {
                    227: 	@names= @{ $Apache::response::foilgroup{'names'} };
1.6       albertel  228:     }
1.83      albertel  229:     foreach $name (@names) {
                    230: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    231: 	    $truecnt++;
                    232: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    233: 	    $falsecnt++;
                    234: 	}
                    235:     }
                    236:     return ($truecnt,$falsecnt);
1.5       albertel  237: }
                    238: 
1.114   ! albertel  239: sub format_prior_answer {
        !           240:     my ($mode,$answer,$other_data) = @_;
        !           241:     my $foil_data = $other_data->[0];
        !           242:     my %response = &Apache::lonnet::str2hash($answer);
        !           243:     my ($name)   = keys(%response);
        !           244:     return '<span class="LC_prior_radiobutton">'.
        !           245: 	$foil_data->{$name.'.text'}.'</span>';
        !           246: 
1.112     albertel  247: }
                    248: 
1.15      albertel  249: sub displayallfoils {
1.110     foxr      250:     my ($direction, $target)=@_;
1.83      albertel  251:     my $result;
                    252:     &Apache::lonxml::debug("survey style display");
1.106     albertel  253:     my @names;
                    254:     if ( $Apache::response::foilgroup{'names'} ) {
                    255: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    256:     }
1.83      albertel  257:     my $temp=0;
1.110     foxr      258:     my $i   =0;
1.83      albertel  259:     my $id=$Apache::inputtags::response['-1'];
                    260:     my $part=$Apache::inputtags::part;
                    261:     my $lastresponse=
                    262: 	$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.93      albertel  263:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
1.83      albertel  264:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
                    265:     if (&Apache::response::show_answer() ) {
                    266: 	foreach my $name (@names) {
                    267: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.110     foxr      268: 		if (($direction eq 'horizontal') && ($target ne 'tex')) {
1.93      albertel  269: 		    $result.="<td>";
                    270: 		} else {
1.110     foxr      271: 		    if ($target eq 'tex') {
                    272: 			$result .= '\item \vskip -2mm ';
                    273: 		    } else {
                    274: 			$result.="<br />";
                    275: 		    }
1.93      albertel  276: 		}
1.84      albertel  277: 		if (defined($lastresponse{$name})) {
1.110     foxr      278: 		    if ($target eq 'tex') {
                    279: 			$result .= '}';
                    280: 		    } else {
                    281: 			$result.='<b>';
                    282: 		    }
1.83      albertel  283: 		}
                    284: 		$result .= $Apache::response::foilgroup{$name.'.text'};
1.110     foxr      285: 		if (defined($lastresponse{$name}) && ($target ne 'tex')) {
1.83      albertel  286: 		    $result.='</b>';
                    287: 		}
1.110     foxr      288: 		if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
1.83      albertel  289: 	    }
1.45      albertel  290: 	}
1.83      albertel  291:     } else {
                    292: 	foreach my $name (@names) {
                    293: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.93      albertel  294: 		if ($direction eq 'horizontal') {
                    295: 		    $result.="<td>";
                    296: 		} else {
1.110     foxr      297: 		    if ($target eq 'tex') {
                    298: 			$result .= '\item \vskip -2mm ';
                    299: 		    } else {
                    300: 			$result.="<br />";
                    301: 		    }
                    302: 		}
                    303: 		if ($target eq 'tex') {
                    304: 		    $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    305: 		    $i++;
                    306: 		} else {
                    307: 		    $result .= '<label>';
1.113     albertel  308: 		    $result.="<input
                    309:                        onchange=\"javascript:setSubmittedPart('$part');\"
                    310:                        type=\"radio\"
                    311:                        name=\"HWVAL_$Apache::inputtags::response['-1']\"
                    312:                        value=\"$temp\" ";
1.110     foxr      313: 		    if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
                    314: 		    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
                    315: 			'</label>';
1.93      albertel  316: 		}
1.83      albertel  317: 		$temp++;
1.110     foxr      318: 		if ($target ne 'tex') {
                    319: 		    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
                    320: 		} else {
                    321: 		    $result.='\vskip 0 mm ';
                    322: 		}
1.83      albertel  323: 	    }
1.45      albertel  324: 	}
                    325:     }
1.110     foxr      326:     if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
1.83      albertel  327:     return $result;
1.15      albertel  328: }
                    329: 
1.28      albertel  330: sub whichfoils {
1.83      albertel  331:     my ($max,$randomize)=@_;
1.28      albertel  332: 
1.83      albertel  333:     my @truelist;
                    334:     my @falselist;
                    335:     my @whichfalse =();
                    336:     my ($truecnt,$falsecnt) = &getfoilcounts();
                    337:     my $count=0;
                    338:     # we will add in 1 of the true statements
1.104     albertel  339:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
1.83      albertel  340:     my $answer=int(&Math::Random::random_uniform() * ($count));
                    341:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
                    342:     my @names;
                    343:     if ( $Apache::response::foilgroup{'names'} ) {
                    344: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    345:     }
                    346:     if (&Apache::response::showallfoils()) {
                    347: 	@whichfalse=@names;
                    348:     } elsif ($randomize eq 'no') {
                    349: 	&Apache::lonxml::debug("No randomization");
                    350: 	my $havetrue=0;
                    351: 	foreach my $name (@names) {
                    352: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    353: 		if (!$havetrue ) {
                    354: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
                    355: 		}
                    356: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    357: 		push (@whichfalse,$name);
                    358: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    359: 	    } else {
1.87      albertel  360: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  361: 	    }
                    362: 	}
1.97      albertel  363: 	if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
                    364: 	    &Apache::lonxml::error("There are no true statements available.<br />");
                    365: 	}
1.83      albertel  366:     } else {
                    367: 	my $current=0;
                    368: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
                    369: 	my (%top,%bottom);
                    370: 	#first find out where everyone wants to be
                    371: 	foreach my $name (@names) {
                    372: 	    $current++;
                    373: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    374: 		push (@truelist,$name);
                    375: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    376: 		    $top{$name}=$current;
                    377: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    378: 		    $bottom{$name}=$current;
                    379: 		}
                    380: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    381: 		push (@falselist,$name);
                    382: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    383: 		    $top{$name}=$current;
                    384: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    385: 		    $bottom{$name}=$current;
                    386: 		}
                    387: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    388: 	    } else {
1.87      albertel  389: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  390: 	    }
                    391: 	}
                    392: 	#pick a true statement
                    393: 	my $notrue=0;
                    394: 	if (scalar(@truelist) == 0) { $notrue=1; }
                    395: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
                    396: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    397: 	my (@toplist, @bottomlist);
                    398: 	my $topcount=0;
                    399: 	my $bottomcount=0;
                    400: 	# assign everyone to either toplist/bottomlist or whichfalse
                    401: 	# which false is randomized, toplist bottomlist are in order
                    402: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
                    403: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    404: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
                    405: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    406: 	    $afalse=splice(@falselist,$afalse,1);
                    407: 	    &Apache::lonxml::debug("Picked $afalse");
                    408: 	    &Apache::lonhomework::showhash(('names'=>\@names));
                    409: 	    &Apache::lonhomework::showhash(%top);
                    410: 	    if ($top{$afalse}) {
                    411: 		$toplist[$top{$afalse}]=$afalse;
                    412: 		$topcount++;
                    413: 	    } elsif ($bottom{$afalse}) {
                    414: 		$bottomlist[$bottom{$afalse}]=$afalse;
                    415: 		$bottomcount++;
                    416: 	    } else {
                    417: 		push (@whichfalse,$afalse);
                    418: 	    }
                    419: 	}
                    420: 	&Apache::lonxml::debug("Answer wants $answer");
                    421: 	my $truename=$truelist[$whichtrue];
                    422: 	my $dosplice=1;
                    423: 	if ($notrue && $Apache::lonhomework::type ne 'survey') {
                    424: 	    $dosplice=0;
                    425: 	    &Apache::lonxml::error("There are no true statements available.<br />");
                    426: 	}
                    427: 	#insert the true statement, keeping track of where it wants to be
                    428: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
                    429: 	    $toplist[$top{$truename}]=$truename;
                    430: 	    $answer=-1;
                    431: 	    foreach my $top (reverse(@toplist)) {
                    432: 		if ($top) { $answer++;}
                    433: 		if ($top eq $truename) { last; }
1.49      albertel  434: 	    }
1.83      albertel  435: 	    $dosplice=0;
                    436: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
                    437: 	    $bottomlist[$bottom{$truename}]=$truename;
                    438: 	    $answer=-1;
                    439: 	    foreach my $bot (@bottomlist) {
                    440: 		if ($bot) { $answer++;}
                    441: 		if ($bot eq $truename) { last; }
1.49      albertel  442: 	    }
1.83      albertel  443: 	    $answer+=$topcount+$#whichfalse+1;
                    444: 	    $dosplice=0;
1.49      albertel  445: 	} else {
1.83      albertel  446: 	    if ($topcount>0 || $bottomcount>0) {
                    447: 		$answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
                    448: 		    + $topcount;
                    449: 	    }
                    450: 	}
                    451: 	&Apache::lonxml::debug("Answer now wants $answer");
                    452: 	#add the top items to the top, bottom items to the bottom
                    453: 	for (my $i=0;$i<=$#toplist;$i++) {
                    454: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
1.49      albertel  455: 	}
1.83      albertel  456: 	for (my $i=0;$i<=$#bottomlist;$i++) {
                    457: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
1.49      albertel  458: 	}
1.83      albertel  459: 	#if the true statement is randomized insert it into the list
                    460: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.49      albertel  461:     }
1.83      albertel  462:     &Apache::lonxml::debug("Answer is $answer");
                    463:     return ($answer,@whichfalse);
1.28      albertel  464: }
                    465: 
                    466: sub displayfoils {
1.90      albertel  467:     my ($target,$max,$randomize,$direction)=@_;
1.83      albertel  468:     my $result;
1.28      albertel  469: 
1.83      albertel  470:     my ($answer,@whichfoils)=&whichfoils($max,$randomize);
1.22      albertel  471:     my $part=$Apache::inputtags::part;
1.83      albertel  472:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    473:     if ( ($target ne 'tex') &&
                    474: 	 &Apache::response::show_answer() ) {
1.90      albertel  475: 	if ($direction eq 'horizontal') {
                    476: 	    if ($target ne 'tex') {
                    477: 		$result.='<table><tr>';
                    478: 	    }
                    479: 	}
1.83      albertel  480: 	foreach my $name (@whichfoils) {
1.90      albertel  481: 	    if ($direction eq 'horizontal') {
                    482: 		if ($target ne 'tex') { $result.='<td>'; }
                    483: 	    }
1.83      albertel  484: 	    if ($target ne 'tex') {
                    485: 		$result.="<br />";
                    486: 	    } else {
                    487: 		$result.='\item \vskip -2 mm  ';
                    488: 	    }
                    489: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
                    490: 		if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}
                    491: 	    } else {
                    492: 		$result.='Incorrect:';
                    493: 	    }
1.94      matthew   494: 	    if ($target eq 'web') { $result.="<label>"; }
1.90      albertel  495: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
1.94      matthew   496: 	    if ($target eq 'web') { $result.="</label>"; }
1.83      albertel  497: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    498: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
                    499: 	    }
1.90      albertel  500: 	    if ($direction eq 'horizontal') {
                    501: 		if ($target ne 'tex') { $result.='</td>'; }
                    502: 	    }
                    503: 	}
                    504: 	if ($direction eq 'horizontal') {
                    505: 	    if ($target ne 'tex') {
                    506: 		$result.='</tr></table>';
                    507: 	    }
1.83      albertel  508: 	}
                    509:     } else {
                    510: 	my @alphabet = ('A'..'Z');
                    511: 	my $i = 0;
                    512: 	my $temp=0;  
                    513: 	my $id=$Apache::inputtags::response['-1'];
                    514: 	my $part=$Apache::inputtags::part;
                    515: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    516: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.90      albertel  517: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    518: 	    $result.="<table><tr>";
                    519: 	}
1.83      albertel  520: 	foreach my $name (@whichfoils) {
                    521: 	    if ($target ne 'tex') {
1.90      albertel  522: 		if ($direction eq 'horizontal') {
                    523: 		    $result.="<td>"; 
                    524: 		} else { 
                    525: 		    $result.="<br />";
                    526: 		} 
                    527: 	    }
                    528: 	    if ($target ne 'tex') { 
1.94      matthew   529:                 $result.= '<label>';
1.113     albertel  530: 		$result.=
                    531: 		    "<input type=\"radio\"
                    532:                             onchange=\"javascript:setSubmittedPart('$part');\"
                    533:                             name=\"HWVAL_$Apache::inputtags::response['-1']\"
                    534:                             value=\"$temp\" ";
1.83      albertel  535: 		if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.108     albertel  536: 		$result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
1.83      albertel  537: 	    } else {
                    538: 		if ($Apache::lonhomework::type eq 'exam') {
                    539: 		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    540: 		    $i++;
                    541: 		} else {
                    542: 		    $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
                    543: 		}
                    544: 	    }
1.90      albertel  545: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
                    546: 		$result.="</td>"; 
                    547: 	    }
1.83      albertel  548: 	    $temp++;
                    549: 	}
1.90      albertel  550: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    551: 	    $result.="</tr></table>";
                    552: 	}
1.83      albertel  553:     }
1.92      albertel  554:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
1.83      albertel  555:     return $result;
1.81      albertel  556: }
                    557: 
                    558: sub displayallanswers {
1.106     albertel  559:     my @names;
                    560:     if ( $Apache::response::foilgroup{'names'} ) {
                    561: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    562:     }
1.81      albertel  563:   
                    564:     my $result=&Apache::response::answer_header('radiobuttonresponse');
                    565:     foreach my $name (@names) {
                    566: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                    567: 				$Apache::response::foilgroup{$name.'.value'});
                    568:     }
                    569:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    570:     return $result;
1.14      albertel  571: }
                    572: 
1.28      albertel  573: sub displayanswers {
1.83      albertel  574:     my ($max,$randomize)=@_;
                    575:     my ($answer,@whichopt) = &whichfoils($max,$randomize);
                    576:     my $result=&Apache::response::answer_header('radiobuttonresponse');
1.105     albertel  577:     if ($Apache::lonhomework::type eq 'exam') {
                    578: 	my $correct = ('A'..'Z')[$answer];
                    579: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                    580: 						$correct);
                    581:     }
1.83      albertel  582:     foreach my $name (@whichopt) {
                    583: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
1.105     albertel  584: 						$Apache::response::foilgroup{$name.'.value'});
                    585:     }
1.83      albertel  586:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    587:     return $result;
1.28      albertel  588: }
                    589: 
1.14      albertel  590: sub start_conceptgroup {
1.83      albertel  591:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    592:     $Apache::radiobuttonresponse::conceptgroup=1;
                    593:     %Apache::response::conceptgroup=();
                    594:     my $result;
                    595:     if ($target eq 'edit') {
                    596: 	$result.=&Apache::edit::tag_start($target,$token);
                    597: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    598: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    599:     } elsif ($target eq 'modified') {
                    600: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    601: 						     $safeeval,'concept');
                    602: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    603:     }
                    604:     return $result;
1.14      albertel  605: }
                    606: 
                    607: sub end_conceptgroup {
1.83      albertel  608:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    609:     $Apache::radiobuttonresponse::conceptgroup=0;
                    610:     my $result;
                    611:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
                    612: 	$target eq 'tex' || $target eq 'analyze') {
                    613: 	&Apache::response::pick_foil_for_concept($target,
                    614: 						 ['value','text','location'],
                    615: 						 \%Apache::hint::radiobutton,
                    616: 						 $parstack,$safeeval);
                    617:     } elsif ($target eq 'edit') {
                    618: 	$result=&Apache::edit::end_table();
                    619:     }
                    620:     return $result;
1.26      albertel  621: }
                    622: 
                    623: sub insert_conceptgroup {
1.83      albertel  624:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    625:     return $result;
1.1       albertel  626: }
                    627: 
                    628: sub start_foil {
1.83      albertel  629:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    630:     my $result='';
                    631:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    632: 	&Apache::lonxml::startredirection;
1.95      albertel  633: 	if ($target eq 'analyze') {
                    634: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    635: 	}
1.83      albertel  636:     } elsif ($target eq 'edit') {
                    637: 	$result=&Apache::edit::tag_start($target,$token);
                    638: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    639: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
                    640: 						   ['unused','true','false'],
                    641: 						   $token);
                    642: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    643: 						 $safeeval,'-3');
                    644: 	if ($randomize ne 'no') {
                    645: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    646: 					       ['random','top','bottom'],$token);
                    647: 	}
                    648: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    649:     } elsif ($target eq 'modified') {
                    650: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    651: 						     $safeeval,'value','name',
                    652: 						     'location');
                    653: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    654:     } 
                    655:     return $result;
1.1       albertel  656: }
                    657: 
                    658: sub end_foil {
1.83      albertel  659:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    660:     my $text='';
                    661:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    662: 	$text=&Apache::lonxml::endredirection;
                    663:     }
1.85      albertel  664:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
                    665: 	|| $target eq 'tex' || $target eq 'analyze') {
1.83      albertel  666: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    667: 	if ($value ne 'unused') {
                    668: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.102     albertel  669: 	    if ($name eq "") {
1.101     albertel  670: 		&Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.98      albertel  671: 		$name=$Apache::lonxml::curdepth;
                    672: 	    }
1.85      albertel  673: 	    if (defined($Apache::response::foilnames{$name})) {
                    674: 		&Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
                    675: 	    }
1.86      albertel  676: 	    $Apache::response::foilnames{$name}++;
1.85      albertel  677: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
                    678: 						     $safeeval);
1.83      albertel  679: 	    if ( $Apache::radiobuttonresponse::conceptgroup
                    680: 		 && !&Apache::response::showallfoils() ) {
                    681: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    682: 		$Apache::response::conceptgroup{"$name.value"} = $value;
                    683: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
                    684: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    685: 	    } else {
                    686: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    687: 		$Apache::response::foilgroup{"$name.value"} = $value;
                    688: 		$Apache::response::foilgroup{"$name.text"} = $text;
                    689: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    690: 	    }
                    691: 	}
1.18      albertel  692:     }
1.83      albertel  693:     return '';
1.1       albertel  694: }
                    695: 
1.27      albertel  696: sub insert_foil {
1.83      albertel  697:     return '
1.27      albertel  698: <foil name="" value="unused">
                    699: <startouttext />
                    700: <endouttext />
                    701: </foil>';
                    702: }
1.1       albertel  703: 1;
                    704: __END__
                    705:  

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