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

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

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