File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.135: download - view: text, annotated - select for diffs
Fri Sep 5 12:50:45 2008 UTC (15 years, 8 months ago) by onken
Branches: MAIN
CVS tags: HEAD
Adds PDF-formfields function to radiobutton-problems (not to exams)

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

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