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

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

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