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

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.31      albertel    3: #
1.111   ! albertel    4: # $Id: radiobuttonresponse.pm,v 1.110 2006/12/19 11:05:21 foxr 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.89      albertel  211:     &Apache::response::poprandomnumber();
1.83      albertel  212:     &Apache::lonxml::increment_counter();
                    213:     return $result;
1.6       albertel  214: }
                    215: 
                    216: sub getfoilcounts {
1.83      albertel  217:     my @names;
                    218:     my $truecnt=0;
                    219:     my $falsecnt=0;
                    220:     my $name;
                    221:     if ( $Apache::response::foilgroup{'names'} ) {
                    222: 	@names= @{ $Apache::response::foilgroup{'names'} };
1.6       albertel  223:     }
1.83      albertel  224:     foreach $name (@names) {
                    225: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    226: 	    $truecnt++;
                    227: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    228: 	    $falsecnt++;
                    229: 	}
                    230:     }
                    231:     return ($truecnt,$falsecnt);
1.5       albertel  232: }
                    233: 
1.15      albertel  234: sub displayallfoils {
1.110     foxr      235:     my ($direction, $target)=@_;
1.83      albertel  236:     my $result;
                    237:     &Apache::lonxml::debug("survey style display");
1.106     albertel  238:     my @names;
                    239:     if ( $Apache::response::foilgroup{'names'} ) {
                    240: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    241:     }
1.83      albertel  242:     my $temp=0;
1.110     foxr      243:     my $i   =0;
1.83      albertel  244:     my $id=$Apache::inputtags::response['-1'];
                    245:     my $part=$Apache::inputtags::part;
                    246:     my $lastresponse=
                    247: 	$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.93      albertel  248:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
1.83      albertel  249:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
                    250:     if (&Apache::response::show_answer() ) {
                    251: 	foreach my $name (@names) {
                    252: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.110     foxr      253: 		if (($direction eq 'horizontal') && ($target ne 'tex')) {
1.93      albertel  254: 		    $result.="<td>";
                    255: 		} else {
1.110     foxr      256: 		    if ($target eq 'tex') {
                    257: 			$result .= '\item \vskip -2mm ';
                    258: 		    } else {
                    259: 			$result.="<br />";
                    260: 		    }
1.93      albertel  261: 		}
1.84      albertel  262: 		if (defined($lastresponse{$name})) {
1.110     foxr      263: 		    if ($target eq 'tex') {
                    264: 			$result .= '}';
                    265: 		    } else {
                    266: 			$result.='<b>';
                    267: 		    }
1.83      albertel  268: 		}
                    269: 		$result .= $Apache::response::foilgroup{$name.'.text'};
1.110     foxr      270: 		if (defined($lastresponse{$name}) && ($target ne 'tex')) {
1.83      albertel  271: 		    $result.='</b>';
                    272: 		}
1.110     foxr      273: 		if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
1.83      albertel  274: 	    }
1.45      albertel  275: 	}
1.83      albertel  276:     } else {
                    277: 	foreach my $name (@names) {
                    278: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.93      albertel  279: 		if ($direction eq 'horizontal') {
                    280: 		    $result.="<td>";
                    281: 		} else {
1.110     foxr      282: 		    if ($target eq 'tex') {
                    283: 			$result .= '\item \vskip -2mm ';
                    284: 		    } else {
                    285: 			$result.="<br />";
                    286: 		    }
                    287: 		}
                    288: 		if ($target eq 'tex') {
                    289: 		    $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    290: 		    $i++;
                    291: 		} else {
                    292: 		    $result .= '<label>';
                    293: 		    $result.="<input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";
                    294: 		    if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
                    295: 		    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
                    296: 			'</label>';
1.93      albertel  297: 		}
1.83      albertel  298: 		$temp++;
1.110     foxr      299: 		if ($target ne 'tex') {
                    300: 		    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
                    301: 		} else {
                    302: 		    $result.='\vskip 0 mm ';
                    303: 		}
1.83      albertel  304: 	    }
1.45      albertel  305: 	}
                    306:     }
1.110     foxr      307:     if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
1.83      albertel  308:     return $result;
1.15      albertel  309: }
                    310: 
1.28      albertel  311: sub whichfoils {
1.83      albertel  312:     my ($max,$randomize)=@_;
1.28      albertel  313: 
1.83      albertel  314:     my @truelist;
                    315:     my @falselist;
                    316:     my @whichfalse =();
                    317:     my ($truecnt,$falsecnt) = &getfoilcounts();
                    318:     my $count=0;
                    319:     # we will add in 1 of the true statements
1.104     albertel  320:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
1.83      albertel  321:     my $answer=int(&Math::Random::random_uniform() * ($count));
                    322:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
                    323:     my @names;
                    324:     if ( $Apache::response::foilgroup{'names'} ) {
                    325: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    326:     }
                    327:     if (&Apache::response::showallfoils()) {
                    328: 	@whichfalse=@names;
                    329:     } elsif ($randomize eq 'no') {
                    330: 	&Apache::lonxml::debug("No randomization");
                    331: 	my $havetrue=0;
                    332: 	foreach my $name (@names) {
                    333: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    334: 		if (!$havetrue ) {
                    335: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
                    336: 		}
                    337: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    338: 		push (@whichfalse,$name);
                    339: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    340: 	    } else {
1.87      albertel  341: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  342: 	    }
                    343: 	}
1.97      albertel  344: 	if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
                    345: 	    &Apache::lonxml::error("There are no true statements available.<br />");
                    346: 	}
1.83      albertel  347:     } else {
                    348: 	my $current=0;
                    349: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
                    350: 	my (%top,%bottom);
                    351: 	#first find out where everyone wants to be
                    352: 	foreach my $name (@names) {
                    353: 	    $current++;
                    354: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    355: 		push (@truelist,$name);
                    356: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    357: 		    $top{$name}=$current;
                    358: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    359: 		    $bottom{$name}=$current;
                    360: 		}
                    361: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    362: 		push (@falselist,$name);
                    363: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    364: 		    $top{$name}=$current;
                    365: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    366: 		    $bottom{$name}=$current;
                    367: 		}
                    368: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    369: 	    } else {
1.87      albertel  370: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83      albertel  371: 	    }
                    372: 	}
                    373: 	#pick a true statement
                    374: 	my $notrue=0;
                    375: 	if (scalar(@truelist) == 0) { $notrue=1; }
                    376: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
                    377: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    378: 	my (@toplist, @bottomlist);
                    379: 	my $topcount=0;
                    380: 	my $bottomcount=0;
                    381: 	# assign everyone to either toplist/bottomlist or whichfalse
                    382: 	# which false is randomized, toplist bottomlist are in order
                    383: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
                    384: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    385: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
                    386: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    387: 	    $afalse=splice(@falselist,$afalse,1);
                    388: 	    &Apache::lonxml::debug("Picked $afalse");
                    389: 	    &Apache::lonhomework::showhash(('names'=>\@names));
                    390: 	    &Apache::lonhomework::showhash(%top);
                    391: 	    if ($top{$afalse}) {
                    392: 		$toplist[$top{$afalse}]=$afalse;
                    393: 		$topcount++;
                    394: 	    } elsif ($bottom{$afalse}) {
                    395: 		$bottomlist[$bottom{$afalse}]=$afalse;
                    396: 		$bottomcount++;
                    397: 	    } else {
                    398: 		push (@whichfalse,$afalse);
                    399: 	    }
                    400: 	}
                    401: 	&Apache::lonxml::debug("Answer wants $answer");
                    402: 	my $truename=$truelist[$whichtrue];
                    403: 	my $dosplice=1;
                    404: 	if ($notrue && $Apache::lonhomework::type ne 'survey') {
                    405: 	    $dosplice=0;
                    406: 	    &Apache::lonxml::error("There are no true statements available.<br />");
                    407: 	}
                    408: 	#insert the true statement, keeping track of where it wants to be
                    409: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
                    410: 	    $toplist[$top{$truename}]=$truename;
                    411: 	    $answer=-1;
                    412: 	    foreach my $top (reverse(@toplist)) {
                    413: 		if ($top) { $answer++;}
                    414: 		if ($top eq $truename) { last; }
1.49      albertel  415: 	    }
1.83      albertel  416: 	    $dosplice=0;
                    417: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
                    418: 	    $bottomlist[$bottom{$truename}]=$truename;
                    419: 	    $answer=-1;
                    420: 	    foreach my $bot (@bottomlist) {
                    421: 		if ($bot) { $answer++;}
                    422: 		if ($bot eq $truename) { last; }
1.49      albertel  423: 	    }
1.83      albertel  424: 	    $answer+=$topcount+$#whichfalse+1;
                    425: 	    $dosplice=0;
1.49      albertel  426: 	} else {
1.83      albertel  427: 	    if ($topcount>0 || $bottomcount>0) {
                    428: 		$answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
                    429: 		    + $topcount;
                    430: 	    }
                    431: 	}
                    432: 	&Apache::lonxml::debug("Answer now wants $answer");
                    433: 	#add the top items to the top, bottom items to the bottom
                    434: 	for (my $i=0;$i<=$#toplist;$i++) {
                    435: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
1.49      albertel  436: 	}
1.83      albertel  437: 	for (my $i=0;$i<=$#bottomlist;$i++) {
                    438: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
1.49      albertel  439: 	}
1.83      albertel  440: 	#if the true statement is randomized insert it into the list
                    441: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.49      albertel  442:     }
1.83      albertel  443:     &Apache::lonxml::debug("Answer is $answer");
                    444:     return ($answer,@whichfalse);
1.28      albertel  445: }
                    446: 
                    447: sub displayfoils {
1.90      albertel  448:     my ($target,$max,$randomize,$direction)=@_;
1.83      albertel  449:     my $result;
1.28      albertel  450: 
1.83      albertel  451:     my ($answer,@whichfoils)=&whichfoils($max,$randomize);
1.22      albertel  452:     my $part=$Apache::inputtags::part;
1.83      albertel  453:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    454:     if ( ($target ne 'tex') &&
                    455: 	 &Apache::response::show_answer() ) {
1.90      albertel  456: 	if ($direction eq 'horizontal') {
                    457: 	    if ($target ne 'tex') {
                    458: 		$result.='<table><tr>';
                    459: 	    }
                    460: 	}
1.83      albertel  461: 	foreach my $name (@whichfoils) {
1.90      albertel  462: 	    if ($direction eq 'horizontal') {
                    463: 		if ($target ne 'tex') { $result.='<td>'; }
                    464: 	    }
1.83      albertel  465: 	    if ($target ne 'tex') {
                    466: 		$result.="<br />";
                    467: 	    } else {
                    468: 		$result.='\item \vskip -2 mm  ';
                    469: 	    }
                    470: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
                    471: 		if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}
                    472: 	    } else {
                    473: 		$result.='Incorrect:';
                    474: 	    }
1.94      matthew   475: 	    if ($target eq 'web') { $result.="<label>"; }
1.90      albertel  476: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
1.94      matthew   477: 	    if ($target eq 'web') { $result.="</label>"; }
1.83      albertel  478: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    479: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
                    480: 	    }
1.90      albertel  481: 	    if ($direction eq 'horizontal') {
                    482: 		if ($target ne 'tex') { $result.='</td>'; }
                    483: 	    }
                    484: 	}
                    485: 	if ($direction eq 'horizontal') {
                    486: 	    if ($target ne 'tex') {
                    487: 		$result.='</tr></table>';
                    488: 	    }
1.83      albertel  489: 	}
                    490:     } else {
                    491: 	my @alphabet = ('A'..'Z');
                    492: 	my $i = 0;
                    493: 	my $temp=0;  
                    494: 	my $id=$Apache::inputtags::response['-1'];
                    495: 	my $part=$Apache::inputtags::part;
                    496: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    497: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.90      albertel  498: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    499: 	    $result.="<table><tr>";
                    500: 	}
1.83      albertel  501: 	foreach my $name (@whichfoils) {
                    502: 	    if ($target ne 'tex') {
1.90      albertel  503: 		if ($direction eq 'horizontal') {
                    504: 		    $result.="<td>"; 
                    505: 		} else { 
                    506: 		    $result.="<br />";
                    507: 		} 
                    508: 	    }
                    509: 	    if ($target ne 'tex') { 
1.94      matthew   510:                 $result.= '<label>';
1.90      albertel  511: 		$result.="<input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.83      albertel  512: 		if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.108     albertel  513: 		$result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
1.83      albertel  514: 	    } else {
                    515: 		if ($Apache::lonhomework::type eq 'exam') {
                    516: 		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                    517: 		    $i++;
                    518: 		} else {
                    519: 		    $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
                    520: 		}
                    521: 	    }
1.90      albertel  522: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
                    523: 		$result.="</td>"; 
                    524: 	    }
1.83      albertel  525: 	    $temp++;
                    526: 	}
1.90      albertel  527: 	if ($target ne 'tex' && $direction eq 'horizontal') {
                    528: 	    $result.="</tr></table>";
                    529: 	}
1.83      albertel  530:     }
1.92      albertel  531:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
1.83      albertel  532:     return $result;
1.81      albertel  533: }
                    534: 
                    535: sub displayallanswers {
1.106     albertel  536:     my @names;
                    537:     if ( $Apache::response::foilgroup{'names'} ) {
                    538: 	@names= @{ $Apache::response::foilgroup{'names'} };
                    539:     }
1.81      albertel  540:   
                    541:     my $result=&Apache::response::answer_header('radiobuttonresponse');
                    542:     foreach my $name (@names) {
                    543: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                    544: 				$Apache::response::foilgroup{$name.'.value'});
                    545:     }
                    546:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    547:     return $result;
1.14      albertel  548: }
                    549: 
1.28      albertel  550: sub displayanswers {
1.83      albertel  551:     my ($max,$randomize)=@_;
                    552:     my ($answer,@whichopt) = &whichfoils($max,$randomize);
                    553:     my $result=&Apache::response::answer_header('radiobuttonresponse');
1.105     albertel  554:     if ($Apache::lonhomework::type eq 'exam') {
                    555: 	my $correct = ('A'..'Z')[$answer];
                    556: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
                    557: 						$correct);
                    558:     }
1.83      albertel  559:     foreach my $name (@whichopt) {
                    560: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
1.105     albertel  561: 						$Apache::response::foilgroup{$name.'.value'});
                    562:     }
1.83      albertel  563:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    564:     return $result;
1.28      albertel  565: }
                    566: 
1.14      albertel  567: sub start_conceptgroup {
1.83      albertel  568:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    569:     $Apache::radiobuttonresponse::conceptgroup=1;
                    570:     %Apache::response::conceptgroup=();
                    571:     my $result;
                    572:     if ($target eq 'edit') {
                    573: 	$result.=&Apache::edit::tag_start($target,$token);
                    574: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    575: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    576:     } elsif ($target eq 'modified') {
                    577: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    578: 						     $safeeval,'concept');
                    579: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    580:     }
                    581:     return $result;
1.14      albertel  582: }
                    583: 
                    584: sub end_conceptgroup {
1.83      albertel  585:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    586:     $Apache::radiobuttonresponse::conceptgroup=0;
                    587:     my $result;
                    588:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
                    589: 	$target eq 'tex' || $target eq 'analyze') {
                    590: 	&Apache::response::pick_foil_for_concept($target,
                    591: 						 ['value','text','location'],
                    592: 						 \%Apache::hint::radiobutton,
                    593: 						 $parstack,$safeeval);
                    594:     } elsif ($target eq 'edit') {
                    595: 	$result=&Apache::edit::end_table();
                    596:     }
                    597:     return $result;
1.26      albertel  598: }
                    599: 
                    600: sub insert_conceptgroup {
1.83      albertel  601:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    602:     return $result;
1.1       albertel  603: }
                    604: 
                    605: sub start_foil {
1.83      albertel  606:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    607:     my $result='';
                    608:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    609: 	&Apache::lonxml::startredirection;
1.95      albertel  610: 	if ($target eq 'analyze') {
                    611: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
                    612: 	}
1.83      albertel  613:     } elsif ($target eq 'edit') {
                    614: 	$result=&Apache::edit::tag_start($target,$token);
                    615: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                    616: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
                    617: 						   ['unused','true','false'],
                    618: 						   $token);
                    619: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    620: 						 $safeeval,'-3');
                    621: 	if ($randomize ne 'no') {
                    622: 	    $result.=&Apache::edit::select_arg('Location:','location',
                    623: 					       ['random','top','bottom'],$token);
                    624: 	}
                    625: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    626:     } elsif ($target eq 'modified') {
                    627: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    628: 						     $safeeval,'value','name',
                    629: 						     'location');
                    630: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    631:     } 
                    632:     return $result;
1.1       albertel  633: }
                    634: 
                    635: sub end_foil {
1.83      albertel  636:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    637:     my $text='';
                    638:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
                    639: 	$text=&Apache::lonxml::endredirection;
                    640:     }
1.85      albertel  641:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
                    642: 	|| $target eq 'tex' || $target eq 'analyze') {
1.83      albertel  643: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    644: 	if ($value ne 'unused') {
                    645: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.102     albertel  646: 	    if ($name eq "") {
1.101     albertel  647: 		&Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.98      albertel  648: 		$name=$Apache::lonxml::curdepth;
                    649: 	    }
1.85      albertel  650: 	    if (defined($Apache::response::foilnames{$name})) {
                    651: 		&Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
                    652: 	    }
1.86      albertel  653: 	    $Apache::response::foilnames{$name}++;
1.85      albertel  654: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
                    655: 						     $safeeval);
1.83      albertel  656: 	    if ( $Apache::radiobuttonresponse::conceptgroup
                    657: 		 && !&Apache::response::showallfoils() ) {
                    658: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    659: 		$Apache::response::conceptgroup{"$name.value"} = $value;
                    660: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
                    661: 		$Apache::response::conceptgroup{"$name.location"} = $location;
                    662: 	    } else {
                    663: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
                    664: 		$Apache::response::foilgroup{"$name.value"} = $value;
                    665: 		$Apache::response::foilgroup{"$name.text"} = $text;
                    666: 		$Apache::response::foilgroup{"$name.location"} = $location;
                    667: 	    }
                    668: 	}
1.18      albertel  669:     }
1.83      albertel  670:     return '';
1.1       albertel  671: }
                    672: 
1.27      albertel  673: sub insert_foil {
1.83      albertel  674:     return '
1.27      albertel  675: <foil name="" value="unused">
                    676: <startouttext />
                    677: <endouttext />
                    678: </foil>';
                    679: }
1.1       albertel  680: 1;
                    681: __END__
                    682:  

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