File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.138: download - view: text, annotated - select for diffs
Tue Nov 18 19:14:28 2008 UTC (15 years, 5 months ago) by jms
Branches: MAIN
CVS tags: HEAD
Added/updated/converted POD. Many files had their POD redone to new standards.

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

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