File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.137: download - view: text, annotated - select for diffs
Fri Nov 14 21:27:17 2008 UTC (15 years, 5 months ago) by jms
Branches: MAIN
CVS tags: HEAD
Added/converted comments to POD style

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

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