File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.134.2.1: download - view: text, annotated - select for diffs
Thu Dec 11 03:29:30 2008 UTC (15 years, 4 months ago) by raeburn
Branches: version_2_8_X
CVS tags: version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_99_1, version_2_7_99_0, GCI_1
Diff to branchpoint 1.134: preferred, unified
- Backport 1.136, 1.140, 1.141.

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

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