File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.144: download - view: text, annotated - select for diffs
Sun Feb 28 23:42:24 2010 UTC (14 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- New awards for surveys:
 ANONYMOUS ANONYMOUS_CREDIT SUBMITTED_CREDIT
  - given for question types: anonsurvey, anonsurvey_cred, survey_cred.
    - anonsurvey -- course personnel can not view both identity of submitter
                    and submission details.
    - anonsurveycred -- same anonymity, but submitter receives 'awarded' for
                        submission
    - surveycred - submitter receives 'awarded' for submission to survey.
- Do not display submission details on grading screens for anonymous surveys.

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: #
    4: # $Id: radiobuttonresponse.pm,v 1.144 2010/02/28 23:42:24 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: 
   28: 
   29: 
   30: package Apache::radiobuttonresponse;
   31: use strict;
   32: use HTML::Entities();
   33: use Apache::lonlocal;
   34: use Apache::lonnet;
   35: use Apache::response;
   36: 
   37: my $default_bubbles_per_line = 10;
   38: 
   39: 
   40: BEGIN {
   41:     &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   42: }
   43: 
   44: sub bubble_line_count {
   45:     my ($numfoils, $bubbles_per_line) = @_;
   46:     my $bubble_lines;
   47:     $bubble_lines = int($numfoils / $bubbles_per_line);
   48:     if (($numfoils % $bubbles_per_line) != 0) {
   49: 	$bubble_lines++;
   50:     }
   51:     return $bubble_lines;
   52:     
   53: }
   54: 
   55: 
   56: sub start_radiobuttonresponse {
   57:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   58:     my $result;
   59: 
   60:     #when in a radiobutton response use these
   61:     &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
   62:     push (@Apache::lonxml::namespace,'radiobuttonresponse');
   63:     my $id = &Apache::response::start_response($parstack,$safeeval);
   64: 
   65:     %Apache::hint::radiobutton=();
   66:     undef(%Apache::response::foilnames);
   67:     if ($target eq 'meta') {
   68: 	$result=&Apache::response::meta_package_write('radiobuttonresponse');
   69:     } elsif ($target eq 'edit' ) {
   70: 	$result.=&Apache::edit::start_table($token)
   71:            .'<tr><td>'.&Apache::lonxml::description($token)
   72:            .&Apache::loncommon::help_open_topic('Radio_Response_Problems')
   73:            .'</td>'
   74:            .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
   75:            .&Apache::edit::deletelist($target,$token)
   76:            .'</span></td>'
   77:            .'<td>&nbsp;'.&Apache::edit::end_row()
   78:            .&Apache::edit::start_spanning_row();
   79: 	$result.=
   80: 	    &Apache::edit::text_arg('Max Number Of Shown Foils:','max',
   81: 				    $token,'4').'&nbsp;'x 3 .
   82: 	    &Apache::edit::select_arg('Randomize Foil Order:','randomize',
   83: 				      ['yes','no'],$token).'&nbsp;'x 3 .
   84: 	    &Apache::edit::select_arg('Display Direction:','direction',
   85: 				      ['vertical','horizontal'],$token).
   86: 				      &Apache::edit::end_row().
   87: 				      &Apache::edit::start_spanning_row()."\n";
   88:     } elsif ($target eq 'modified') {
   89: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   90: 						     $safeeval,'max',
   91: 						     'randomize','direction');
   92: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   93:     } elsif ($target eq 'tex') {
   94: 	my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,
   95: 					    undef,0);
   96: 	if ($type eq '1') {
   97: 	    $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
   98: 	} elsif ($type eq 'A') {
   99: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
  100: 	} elsif ($type eq 'a') {
  101: 	    $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
  102: 	} elsif ($type eq 'i') {
  103: 	    $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
  104: 	} else {
  105: 	    $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
  106: 	}
  107:         if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  108:             $result .= &Apache::lonxml::print_pdf_hiddenfield('meta', $env{'user.name'}, $env{'user.domain'});
  109:             $result .= "\n\\\\\n\\\\\n";
  110:         } else {
  111:             $result .= '\begin{enumerate}';
  112:         }
  113:     } elsif ($target eq 'analyze') {
  114: 	my $part_id="$Apache::inputtags::part.$id";
  115:         $Apache::lonhomework::analyze{"$part_id.type"} = 'radiobuttonresponse';
  116: 	push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
  117:     }
  118:     return $result;
  119: }
  120: 
  121: sub end_radiobuttonresponse {
  122:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  123:     my $result;
  124:     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
  125:     if ($target eq 'tex' and ($env{'form.pdfFormFields'} ne 'yes' or $Apache::inputtags::status[-1] ne 'CAN_ANSWER')) {
  126:         $result .= '\end{enumerate}'; 
  127:     }
  128:     &Apache::response::end_response;
  129:     pop @Apache::lonxml::namespace;
  130:     &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
  131:     undef(%Apache::response::foilnames);
  132:     return $result;
  133: }
  134: 
  135: %Apache::response::foilgroup=();
  136: sub start_foilgroup {
  137:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  138:     my $result;
  139:     %Apache::response::foilgroup=();
  140:     $Apache::radiobuttonresponse::conceptgroup=0;
  141:     &Apache::response::pushrandomnumber();
  142:     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  143: 	$result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
  144:     }
  145:     return $result;
  146: }
  147: 
  148: sub storesurvey {
  149:     my ($style) = @_;
  150:     if ( !&Apache::response::submitted() ) { return ''; }
  151:     my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  152:     &Apache::lonxml::debug("Here I am!:$response:");
  153:     if ( $response !~ /[0-9]+/) { return ''; }
  154:     my $part = $Apache::inputtags::part;
  155:     my $id = $Apache::inputtags::response['-1'];
  156:     my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
  157:     my %responsehash;
  158:     $responsehash{$whichfoils[$response]}=$response;
  159:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  160:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  161: 	$responsestr;
  162:     my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
  163:     my $ad;
  164:     if ($style eq 'anonsurvey') {
  165:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
  166:     } elsif ($style eq 'anonsurveycred') {
  167:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CRED';
  168:     } elsif ($style eq 'surveycred') {
  169:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
  170:     } else {
  171:         $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
  172:     }
  173:     &Apache::response::handle_previous(\%previous,$ad);
  174:     &Apache::lonxml::debug("submitted a $response<br />\n");
  175:     return '';
  176: }
  177: 
  178: 
  179: sub grade_response {
  180:     my ($answer, $whichfoils, $bubbles_per_line)=@_;
  181: 
  182:     if ( !&Apache::response::submitted() ) { return; }
  183:     my $response;
  184:     
  185:     if ($env{'form.submitted'} eq 'scantron') {
  186: 	$response = &Apache::response::getresponse(1,undef,
  187: 						   &bubble_line_count(scalar(@{ $whichfoils}),
  188: 								      $bubbles_per_line),
  189: 						   $bubbles_per_line);
  190: 
  191:     } else {
  192: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  193:     }
  194: 
  195: 
  196:     if ( $response !~ /[0-9]+/) { return; }
  197:     my $part=$Apache::inputtags::part;
  198:     my $id = $Apache::inputtags::response['-1'];
  199:     my %responsehash;
  200:     $responsehash{$whichfoils->[$response]}=$response;
  201:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  202:     my %previous=&Apache::response::check_for_previous($responsestr,
  203: 						       $part,$id);
  204:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  205: 	$responsestr;
  206:     &Apache::lonxml::debug("submitted a $response<br />\n");
  207:     my $ad;
  208:     if ($response == $answer) {
  209: 	$ad='EXACT_ANS';
  210:     } else {
  211: 	$ad='INCORRECT';
  212:     }
  213:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  214:     &Apache::response::handle_previous(\%previous,$ad);
  215: }
  216: 
  217: sub end_foilgroup {
  218:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  219: 
  220:     my $result;
  221:     my $bubble_lines;
  222:     my $bubbles_per_line;
  223:     my $answer_count;
  224:     my $id   = $Apache::inputtags::response['-1'];
  225:     my $part = $Apache::inputtags::part;
  226:     $bubbles_per_line = 
  227: 	&Apache::response::get_response_param($Apache::inputtags::part."_$id",
  228: 					      'numbubbles',
  229: 					      $default_bubbles_per_line);
  230: 
  231: 
  232:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  233: 	$target eq 'tex' || $target eq 'analyze') {
  234: 	my $style = $Apache::lonhomework::type;
  235: 	my $direction = &Apache::lonxml::get_param('direction',$parstack,
  236: 						   $safeeval,'-2');
  237: 	if ( (($style eq 'survey') || ($style eq 'surveycred') || 
  238:               ($style eq 'anonsurvey') || ($style eq 'anonsurveycred')) 
  239:              && ($target ne 'analyze')) {
  240: 	    if ($target eq 'web' || $target eq 'tex') {
  241: 		$result=&displayallfoils($direction, $target);
  242: 	    } elsif ( $target eq 'answer' ) {
  243: 		$result=&displayallanswers();
  244: 	    } elsif ( $target eq 'grade' ) {
  245: 		$result=&storesurvey($style);
  246: 	    }
  247: 	    $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
  248: 
  249: 	} else {
  250: 
  251: 	    my $name;
  252: 	    my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
  253: 						 '-2');
  254: 	    my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  255: 						       $safeeval,'-2');
  256: 	    my ($answer, @shown) = &whichfoils($max, $randomize);
  257: 	    $answer_count = scalar(@shown);
  258: 
  259: 	    if ($target eq 'web' || $target eq 'tex') {
  260: 		$result=&displayfoils($target,
  261: 				      $answer, \@shown,
  262: 				      $direction,
  263: 				      $bubbles_per_line);
  264: 	    } elsif ($target eq 'answer' ) {
  265: 		$result=&displayanswers($answer, \@shown, $bubbles_per_line);
  266: 	    } elsif ( $target eq 'grade') {
  267: 		&grade_response($answer, \@shown, $bubbles_per_line);
  268: 	    }  elsif ( $target eq 'analyze') {
  269: 		my $bubble_lines = &bubble_line_count($answer_count, 
  270: 						      $bubbles_per_line);
  271: 		&Apache::response::analyze_store_foilgroup(\@shown,
  272: 							   ['text','value','location']);
  273: 		my $part_id="$part.$id";
  274: 		push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
  275: 		      ('true','false'));
  276: 
  277: 	    }
  278: 	}
  279: 	$Apache::lonxml::post_evaluate=0;
  280:     }
  281:     if ($target eq 'web') {
  282: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  283: 						  [\%Apache::response::foilgroup]);
  284:     }
  285:     &Apache::response::poprandomnumber();
  286:     $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
  287:     &Apache::lonxml::increment_counter($bubble_lines,
  288: 				       "$part.$id");
  289:     if ($target eq 'analyze') {
  290: 	&Apache::lonhomework::set_bubble_lines();
  291:     }
  292:     return $result;
  293: }
  294: 
  295: sub getfoilcounts {
  296:     my @names;
  297:     my $truecnt=0;
  298:     my $falsecnt=0;
  299:     my $name;
  300:     if ( $Apache::response::foilgroup{'names'} ) {
  301: 	@names= @{ $Apache::response::foilgroup{'names'} };
  302:     }
  303:     foreach $name (@names) {
  304: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  305: 	    $truecnt++;
  306: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  307: 	    $falsecnt++;
  308: 	}
  309:     }
  310:     return ($truecnt,$falsecnt);
  311: }
  312: 
  313: sub format_prior_answer {
  314:     my ($mode,$answer,$other_data) = @_;
  315:     my $foil_data = $other_data->[0];
  316:     my %response = &Apache::lonnet::str2hash($answer);
  317:     my ($name)   = keys(%response);
  318:     return '<span class="LC_prior_radiobutton">'.
  319: 	$foil_data->{$name.'.text'}.'</span>';
  320: 
  321: }
  322: 
  323: sub displayallfoils {
  324:     my ($direction, $target)=@_;
  325:     my $result;
  326:     &Apache::lonxml::debug("survey style display");
  327:     my @names;
  328:     if ( $Apache::response::foilgroup{'names'} ) {
  329: 	@names= @{ $Apache::response::foilgroup{'names'} };
  330:     }
  331: 
  332:     my $temp=0;
  333:     my $i   =0;
  334:     my $id=$Apache::inputtags::response['-1'];
  335:     my $part=$Apache::inputtags::part;
  336:     my $lastresponse;
  337:     unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) && (defined($env{'form.grade_symb'}))) {
  338:         $lastresponse = 
  339: 	    $Apache::lonhomework::history{"resource.$part.$id.submission"};
  340:     }
  341:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
  342:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  343:     if (&Apache::response::show_answer() ) {
  344: 	foreach my $name (@names) {
  345: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  346: 		if (($direction eq 'horizontal') && ($target ne 'tex')) {
  347: 		    $result.="<td>";
  348: 		} else {
  349: 		    if ($target eq 'tex') {
  350: 			$result .= '\item \vskip -2mm ';
  351: 		    } else {
  352: 			$result.="<br />";
  353: 		    }
  354: 		}
  355: 		if (defined($lastresponse{$name})) {
  356: 		    if ($target eq 'tex') {
  357: 			$result .= '}';
  358: 		    } else {
  359: 			$result.='<b>';
  360: 		    }
  361: 		}
  362: 		$result .= $Apache::response::foilgroup{$name.'.text'};
  363: 		if (defined($lastresponse{$name}) && ($target ne 'tex')) {
  364: 		    $result.='</b>';
  365: 		}
  366: 		if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
  367: 	    }
  368: 	}
  369:     } else {
  370: 	foreach my $name (@names) {
  371: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  372: 		if ($direction eq 'horizontal') {
  373: 		    $result.="<td>";
  374: 		} else {
  375: 		    if ($target eq 'tex') {
  376: 		        if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  377:                             my $fieldname = $env{'request.symb'}.
  378:                                            '&part_'. $Apache::inputtags::part.
  379:                                            '&radiobuttonresponse'.
  380:                                            '&HWVAL_' . $Apache::inputtags::response['-1'];
  381:                             my $value = $temp;
  382:                             my $text = $Apache::response::foilgroup{$name.'.text'};
  383:                             $result .= &Apache::lonxml::print_pdf_radiobutton($fieldname,
  384:                                                                              $value,
  385:                                                                              $text)."\n";
  386:                         } else {
  387:                             $result .= '\item \vskip -2mm ';
  388:                         }
  389:                     } else {
  390: 			$result.="<br />";
  391: 		    }
  392: 		}
  393: 		if ($target eq 'tex') {
  394: 		    if($env{'form.pdfFormFields'} ne 'yes' or $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
  395:                         $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
  396:                     }
  397: 		    $i++;
  398: 		} else {
  399: 		    $result .= '<label>';
  400: 		    $result.="<input
  401:                        onchange=\"javascript:setSubmittedPart('$part');\"
  402:                        type=\"radio\"
  403:                        name=\"HWVAL_$Apache::inputtags::response['-1']\"
  404:                        value=\"$temp\"";
  405: 		    if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
  406: 		    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
  407: 			'</label>';
  408: 		}
  409: 		$temp++;
  410: 		if ($target ne 'tex') {
  411: 		    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
  412: 		} else {
  413: 		    $result.='\vskip 0 mm ';
  414: 		}
  415: 	    }
  416: 	}
  417:     }
  418:     
  419:     if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
  420:     return $result;
  421: }
  422: 
  423: 
  424: sub whichfoils {
  425:     my ($max,$randomize)=@_;
  426: 
  427:     my @truelist;
  428:     my @falselist;
  429:     my @whichfalse =();
  430:     my ($truecnt,$falsecnt) = &getfoilcounts();
  431:     my $count=0;
  432:     # we will add in 1 of the true statements
  433:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
  434:     my $answer=int(&Math::Random::random_uniform() * ($count));
  435:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
  436:     my @names;
  437:     if ( $Apache::response::foilgroup{'names'} ) {
  438: 	@names= @{ $Apache::response::foilgroup{'names'} };
  439:     }
  440:     if (&Apache::response::showallfoils()) {
  441: 	@whichfalse=@names;
  442:     } elsif ($randomize eq 'no') {
  443: 	&Apache::lonxml::debug("No randomization");
  444: 	my $havetrue=0;
  445: 	foreach my $name (@names) {
  446: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  447: 		if (!$havetrue ) {
  448: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
  449: 		}
  450: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  451: 		push (@whichfalse,$name);
  452: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  453: 	    } else {
  454: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  455: 	    }
  456: 	}
  457: 	if ((!$havetrue) && 
  458:             ($Apache::lonhomework::type ne 'survey') && 
  459:             ($Apache::lonhomework::type ne 'surveycred') &&
  460:             ($Apache::lonhomework::type ne 'anonsurvey') &&
  461:             ($Apache::lonhomework::type ne 'anonsurveycred')) {
  462: 	    &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
  463: 	}
  464:     } else {
  465: 	my $current=0;
  466: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
  467: 	my (%top,%bottom);
  468: 	#first find out where everyone wants to be
  469: 	foreach my $name (@names) {
  470: 	    $current++;
  471: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  472: 		push (@truelist,$name);
  473: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  474: 		    $top{$name}=$current;
  475: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
  476: 		    $bottom{$name}=$current;
  477: 		}
  478: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  479: 		push (@falselist,$name);
  480: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  481: 		    $top{$name}=$current;
  482: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
  483: 		    $bottom{$name}=$current;
  484: 		}
  485: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  486: 	    } else {
  487: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  488: 	    }
  489: 	}
  490: 	#pick a true statement
  491: 	my $notrue=0;
  492: 	if (scalar(@truelist) == 0) { $notrue=1; }
  493: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
  494: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  495: 	my (@toplist, @bottomlist);
  496: 	my $topcount=0;
  497: 	my $bottomcount=0;
  498: 	# assign everyone to either toplist/bottomlist or whichfalse
  499: 	# which false is randomized, toplist bottomlist are in order
  500: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
  501: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
  502: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
  503: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  504: 	    $afalse=splice(@falselist,$afalse,1);
  505: 	    &Apache::lonxml::debug("Picked $afalse");
  506: 	    &Apache::lonhomework::showhash(('names'=>\@names));
  507: 	    &Apache::lonhomework::showhash(%top);
  508: 	    if ($top{$afalse}) {
  509: 		$toplist[$top{$afalse}]=$afalse;
  510: 		$topcount++;
  511: 	    } elsif ($bottom{$afalse}) {
  512: 		$bottomlist[$bottom{$afalse}]=$afalse;
  513: 		$bottomcount++;
  514: 	    } else {
  515: 		push (@whichfalse,$afalse);
  516: 	    }
  517: 	}
  518: 	&Apache::lonxml::debug("Answer wants $answer");
  519: 	my $truename=$truelist[$whichtrue];
  520: 	my $dosplice=1;
  521: 	if (($notrue) && 
  522:             ($Apache::lonhomework::type ne 'survey') &&
  523:             ($Apache::lonhomework::type ne 'surveycred') &&
  524:             ($Apache::lonhomework::type ne 'anonsurvey') &&
  525:             ($Apache::lonhomework::type ne 'anonsurveycred')) {
  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="checked"'; }
  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' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  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: 
  835: 
  836: 
  837: =head1 NAME
  838: 
  839: Apache::radiobuttonresponse
  840: 
  841: =head1 SYNOPSIS
  842: 
  843: Handles multiple-choice style responses.
  844: 
  845: This is part of the LearningOnline Network with CAPA project
  846: described at http://www.lon-capa.org.
  847: 
  848: =head1 SUBROUTINES
  849: 
  850: =over
  851: 
  852: =item start_radiobuttonresponse()
  853: 
  854: =item bubble_line_count()
  855: 
  856: =item end_radiobuttonresponse()
  857: 
  858: =item start_foilgroup()
  859: 
  860: =item storesurvey()
  861: 
  862: =item grade_response()
  863: 
  864: =item end_foilgroup()
  865: 
  866: =item getfoilcounts()
  867: 
  868: =item format_prior_answer()
  869: 
  870: =item displayallfoils()
  871: 
  872: =item &whichfoils($max,$randomize)
  873: 
  874: Randomizes the list of foils.
  875: Respects
  876:   - each foils desire to be randomized
  877:   - the existance of Concept groups of foils (select 1 foil from each)
  878:   - and selects a single correct statement from all possilble true statments
  879:   - and limits it to a toal of $max foils
  880: 
  881: WARNING: this routine uses the random number generator, it should only
  882: be called once per target, otherwise it can cause randomness changes in
  883: homework problems.
  884: 
  885: Arguments
  886:   $max - maximum number of foils to select (including the true one)
  887:          (so a max of 5 is: 1 true, 4 false)
  888: 
  889:   $randomize - whether to randomize the listing of foils, by default
  890:                will randomize, only if randomize is 'no' will it not
  891: 
  892: Returns
  893:   $answer - location in the array of the correct answer
  894:   @foils  - array of foil names in to display order
  895: 
  896: =item displayfoils()
  897: 
  898: =item displayallanswers()
  899: 
  900: =item displayanswers()
  901: 
  902: =item start_conceptgroup()
  903: 
  904: =item end_conceptgroup()
  905: 
  906: =item insert_conceptgroup()
  907: 
  908: =item start_foil()
  909: 
  910: =item end_foil()
  911: 
  912: =item insert_foil()
  913: 
  914: =back
  915: 
  916: =cut
  917:  

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