File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.121: download - view: text, annotated - select for diffs
Fri Jun 29 12:49:10 2007 UTC (16 years, 10 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
BZ4704 - Factor all the bubble line computations out into
end_foilgroup ... eliminating globals for
bubbles_per_line and bubble_lines.

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

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