File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.107: download - view: text, annotated - select for diffs
Mon Nov 27 11:42:40 2006 UTC (17 years, 5 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
BUG 4074 - If there are more than 10 foils displayed,
 in exam mode, break this up into two problems so that
 bubble sheets can be created.  Note that 10 is tunable
using exam_max_bubbles at the top of the module.

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: #
    4: # $Id: radiobuttonresponse.pm,v 1.107 2006/11/27 11:42:40 foxr Exp $
    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: #
   28: 
   29: package Apache::radiobuttonresponse;
   30: use strict;
   31: use HTML::Entities();
   32: use Apache::lonlocal;
   33: use Apache::lonnet;
   34: 
   35: my $exam_max_bubbles = 10;
   36: 
   37: BEGIN {
   38:     &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   39: }
   40: 
   41: sub start_radiobuttonresponse {
   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=();
   49:     undef(%Apache::response::foilnames);
   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).
   65: 	    &Apache::edit::select_arg('Display Direction','direction',
   66: 				      ['vertical','horizontal'],$token).
   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',
   72: 						     'randomize','direction');
   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}.}';
   85: 	} else {
   86: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
   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;
   94: }
   95: 
   96: sub end_radiobuttonresponse {
   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'));
  104:     undef(%Apache::response::foilnames);
  105:     return $result;
  106: }
  107: 
  108: %Apache::response::foilgroup=();
  109: sub start_foilgroup {
  110:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  111:     my $result;
  112:     %Apache::response::foilgroup=();
  113:     $Apache::radiobuttonresponse::conceptgroup=0;
  114:     &Apache::response::pushrandomnumber();
  115:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  116: 	$result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
  117:     }
  118:     return $result;
  119: }
  120: 
  121: sub storesurvey {
  122:     if ( !&Apache::response::submitted() ) { return ''; }
  123:     my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  124:     &Apache::lonxml::debug("Here I am!:$response:");
  125:     if ( $response !~ /[0-9]+/) { return ''; }
  126:     my $part = $Apache::inputtags::part;
  127:     my $id = $Apache::inputtags::response['-1'];
  128:     my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
  129:     my %responsehash;
  130:     $responsehash{$whichfoils[$response]}=$response;
  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);
  137:     &Apache::lonxml::debug("submitted a $response<br />\n");
  138:     return '';
  139: }
  140: 
  141: sub grade_response {
  142:     my ($max,$randomize)=@_;
  143:     #keep the random numbers the same must always call this
  144:     my ($answer,@whichfoils)=&whichfoils($max,$randomize);
  145:     if ( !&Apache::response::submitted() ) { return; }
  146:     my $response;
  147:     if ($env{'form.submitted'} eq 'scantron') {
  148: 	$response=&Apache::response::getresponse();
  149:     } else {
  150: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  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);
  171: }
  172: 
  173: sub end_foilgroup {
  174:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  175: 
  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;
  180: 	my $direction = &Apache::lonxml::get_param('direction',$parstack,
  181: 						   $safeeval,'-2');
  182: 	if ( $style eq 'survey'  && $target ne 'analyze') {
  183: 	    if ($target eq 'web' || $target eq 'tex') {
  184: 		$result=&displayallfoils($direction);
  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') {
  197: 		$result=&displayfoils($target,$max,$randomize,$direction);
  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: 	    }
  210: 	}
  211:     }
  212:     &Apache::response::poprandomnumber();
  213:     &Apache::lonxml::increment_counter();
  214:     return $result;
  215: }
  216: 
  217: sub getfoilcounts {
  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'} };
  224:     }
  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);
  233: }
  234: 
  235: sub displayallfoils {
  236:     my ($direction)=@_;
  237:     my $result;
  238:     &Apache::lonxml::debug("survey style display");
  239:     my @names;
  240:     if ( $Apache::response::foilgroup{'names'} ) {
  241: 	@names= @{ $Apache::response::foilgroup{'names'} };
  242:     }
  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"};
  248:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
  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') {
  253: 		if ($direction eq 'horizontal') {
  254: 		    $result.="<td>";
  255: 		} else {
  256: 		    $result.="<br />";
  257: 		}
  258: 		if (defined($lastresponse{$name})) {
  259: 		    $result.='<b>';
  260: 		}
  261: 		$result .= $Apache::response::foilgroup{$name.'.text'};
  262: 		if (defined($lastresponse{$name})) {
  263: 		    $result.='</b>';
  264: 		}
  265: 		if ($direction eq 'horizontal') { $result.="</td>"; }
  266: 	    }
  267: 	}
  268:     } else {
  269: 	foreach my $name (@names) {
  270: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  271: 		if ($direction eq 'horizontal') {
  272: 		    $result.="<td>";
  273: 		} else {
  274: 		    $result.="<br />";
  275: 		}
  276:                 $result .= '<label>';
  277: 		$result.="<input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  278: 		if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  279: 		$result .= '>'.$Apache::response::foilgroup{$name.'.text'}.
  280:                     '</label>';
  281: 		$temp++;
  282: 		if ($direction eq 'horizontal') { $result.="</td>"; }
  283: 	    }
  284: 	}
  285:     }
  286:     if ($direction eq 'horizontal') { $result.='</tr></table>'; }
  287:     return $result;
  288: }
  289: 
  290: sub whichfoils {
  291:     my ($max,$randomize)=@_;
  292: 
  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
  299:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
  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 {
  320: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  321: 	    }
  322: 	}
  323: 	if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
  324: 	    &Apache::lonxml::error("There are no true statements available.<br />");
  325: 	}
  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 {
  349: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  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; }
  394: 	    }
  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; }
  402: 	    }
  403: 	    $answer+=$topcount+$#whichfalse+1;
  404: 	    $dosplice=0;
  405: 	} else {
  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]) }
  415: 	}
  416: 	for (my $i=0;$i<=$#bottomlist;$i++) {
  417: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
  418: 	}
  419: 	#if the true statement is randomized insert it into the list
  420: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
  421:     }
  422:     &Apache::lonxml::debug("Answer is $answer");
  423:     return ($answer,@whichfalse);
  424: }
  425: 
  426: sub displayfoils {
  427:     my ($target,$max,$randomize,$direction)=@_;
  428:     my $result;
  429: 
  430:     my ($answer,@whichfoils)=&whichfoils($max,$randomize);
  431:     my $part=$Apache::inputtags::part;
  432:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  433:     if ( ($target ne 'tex') &&
  434: 	 &Apache::response::show_answer() ) {
  435: 	if ($direction eq 'horizontal') {
  436: 	    if ($target ne 'tex') {
  437: 		$result.='<table><tr>';
  438: 	    }
  439: 	}
  440: 	foreach my $name (@whichfoils) {
  441: 	    if ($direction eq 'horizontal') {
  442: 		if ($target ne 'tex') { $result.='<td>'; }
  443: 	    }
  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: 	    }
  454: 	    if ($target eq 'web') { $result.="<label>"; }
  455: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
  456: 	    if ($target eq 'web') { $result.="</label>"; }
  457: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  458: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
  459: 	    }
  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: 	    }
  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);
  477: 	if ($target ne 'tex' && $direction eq 'horizontal') {
  478: 	    $result.="<table><tr>";
  479: 	}
  480: 	foreach my $name (@whichfoils) {
  481: 	    if ($target ne 'tex') {
  482: 		if ($direction eq 'horizontal') {
  483: 		    $result.="<td>"; 
  484: 		} else { 
  485: 		    $result.="<br />";
  486: 		} 
  487: 	    }
  488: 	    if ($target ne 'tex') { 
  489:                 $result.= '<label>';
  490: 		$result.="<input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  491: 		if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  492: 		$result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</label>";
  493: 	    } else {
  494: 		if ($Apache::lonhomework::type eq 'exam') {
  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: 
  505: 		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
  506: 		    $i++;
  507: 
  508: 		} else {
  509: 		    $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
  510: 		}
  511: 	    }
  512: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
  513: 		$result.="</td>"; 
  514: 	    }
  515: 	    $temp++;
  516: 	}
  517: 	if ($target ne 'tex' && $direction eq 'horizontal') {
  518: 	    $result.="</tr></table>";
  519: 	}
  520:     }
  521:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
  522:     return $result;
  523: }
  524: 
  525: sub displayallanswers {
  526:     my @names;
  527:     if ( $Apache::response::foilgroup{'names'} ) {
  528: 	@names= @{ $Apache::response::foilgroup{'names'} };
  529:     }
  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;
  538: }
  539: 
  540: sub displayanswers {
  541:     my ($max,$randomize)=@_;
  542:     my ($answer,@whichopt) = &whichfoils($max,$randomize);
  543:     my $result=&Apache::response::answer_header('radiobuttonresponse');
  544:     if ($Apache::lonhomework::type eq 'exam') {
  545: 	my $correct = ('A'..'Z')[$answer];
  546: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  547: 						$correct);
  548:     }
  549:     foreach my $name (@whichopt) {
  550: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  551: 						$Apache::response::foilgroup{$name.'.value'});
  552:     }
  553:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
  554:     return $result;
  555: }
  556: 
  557: sub start_conceptgroup {
  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;
  572: }
  573: 
  574: sub end_conceptgroup {
  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;
  588: }
  589: 
  590: sub insert_conceptgroup {
  591:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  592:     return $result;
  593: }
  594: 
  595: sub start_foil {
  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;
  600: 	if ($target eq 'analyze') {
  601: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  602: 	}
  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;
  623: }
  624: 
  625: sub end_foil {
  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:     }
  631:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
  632: 	|| $target eq 'tex' || $target eq 'analyze') {
  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);
  636: 	    if ($name eq "") {
  637: 		&Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
  638: 		$name=$Apache::lonxml::curdepth;
  639: 	    }
  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: 	    }
  643: 	    $Apache::response::foilnames{$name}++;
  644: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  645: 						     $safeeval);
  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: 	}
  659:     }
  660:     return '';
  661: }
  662: 
  663: sub insert_foil {
  664:     return '
  665: <foil name="" value="unused">
  666: <startouttext />
  667: <endouttext />
  668: </foil>';
  669: }
  670: 1;
  671: __END__
  672:  

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