File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.125: download - view: text, annotated - select for diffs
Mon Aug 6 20:52:54 2007 UTC (16 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_99_0, HEAD
- remove debug spew

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

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