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

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

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