File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.120: download - view: text, annotated - select for diffs
Tue Jun 26 10:42:09 2007 UTC (16 years, 10 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
BZ 4074:
  - Centralize the increment of the line counter.
  - Honor the bubbles per line parameter.
  - Centralize the line increment counter.

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

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