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

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

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