File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.116: download - view: text, annotated - select for diffs
Tue Jun 19 10:00:23 2007 UTC (16 years, 10 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Support more than 10 radio responses in exam mode. Note that
grades.pm must still be taught to deal with more than one line
of bubbled responses as a multibubble.

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

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