File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.129: download - view: text, annotated - select for diffs
Mon Oct 15 09:47:29 2007 UTC (16 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
BZ 4074  Ensure the bubble lines chunk of the analysis hash gets
keyed by the concatenation of partid.resopnseid.

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

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