File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.117: download - view: text, annotated - select for diffs
Wed Jun 20 21:43:59 2007 UTC (16 years, 10 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Remove bogus /misleading comments

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

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