File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.118: download - view: text, annotated - select for diffs
Wed Jun 20 22:36:52 2007 UTC (16 years, 10 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Factor the multi-line bubble stuff out of radiobuttonresponse.pm and put
it into response.pm

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: #
    4: # $Id: radiobuttonresponse.pm,v 1.118 2007/06/20 22:36:52 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:     
  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:     
  159:     
  160:     if ($env{'form.submitted'} eq 'scantron') {
  161: 	$response = &Apache::response::getresponse(1,undef,$bubble_lines,
  162: 						 $bubbles_per_line);
  163: 
  164:     } else {
  165: 	$response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
  166:     }
  167:     &Apache::lonxml::increment_counter($bubble_lines-1);
  168: 
  169:     if ( $response !~ /[0-9]+/) { return; }
  170:     my $part=$Apache::inputtags::part;
  171:     my $id = $Apache::inputtags::response['-1'];
  172:     my %responsehash;
  173:     $responsehash{$whichfoils[$response]}=$response;
  174:     my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  175:     my %previous=&Apache::response::check_for_previous($responsestr,
  176: 						       $part,$id);
  177:     $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  178: 	$responsestr;
  179:     &Apache::lonxml::debug("submitted a $response<br />\n");
  180:     my $ad;
  181:     if ($response == $answer) {
  182: 	$ad='EXACT_ANS';
  183:     } else {
  184: 	$ad='INCORRECT';
  185:     }
  186:     $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  187:     &Apache::response::handle_previous(\%previous,$ad);
  188: }
  189: 
  190: sub end_foilgroup {
  191:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  192: 
  193:     my $result;
  194:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  195: 	$target eq 'tex' || $target eq 'analyze') {
  196: 	my $style = $Apache::lonhomework::type;
  197: 	my $direction = &Apache::lonxml::get_param('direction',$parstack,
  198: 						   $safeeval,'-2');
  199: 	if ( $style eq 'survey'  && $target ne 'analyze') {
  200: 	    if ($target eq 'web' || $target eq 'tex') {
  201: 		$result=&displayallfoils($direction, $target);
  202: 	    } elsif ( $target eq 'answer' ) {
  203: 		$result=&displayallanswers();
  204: 	    } elsif ( $target eq 'grade' ) {
  205: 		$result=&storesurvey();
  206: 	    }
  207: 	} else {
  208: 	    my $name;
  209: 	    my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
  210: 						 '-2');
  211: 	    my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
  212: 						       $safeeval,'-2');
  213: 	    if ($target eq 'web' || $target eq 'tex') {
  214: 		$result=&displayfoils($target,$max,$randomize,$direction);
  215: 	    } elsif ($target eq 'answer' ) {
  216: 		$result=&displayanswers($max,$randomize);
  217: 	    } elsif ( $target eq 'grade') {
  218: 		&grade_response($max,$randomize);
  219: 	    }  elsif ( $target eq 'analyze') {
  220: 		my @shown = &whichfoils($max,$randomize);
  221: 		&Apache::response::analyze_store_foilgroup(\@shown,
  222: 							   ['text','value','location']);
  223: 		my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  224: 		push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
  225: 		      ('true','false'));
  226: 	    }
  227: 	}
  228: 	$Apache::lonxml::post_evaluate=0;
  229:     }
  230:     if ($target eq 'web') {
  231: 	&Apache::response::setup_prior_tries_hash(\&format_prior_answer,
  232: 						  [\%Apache::response::foilgroup]);
  233:     }
  234:     
  235:     &Apache::response::poprandomnumber();
  236:     &Apache::lonxml::increment_counter();
  237:     return $result;
  238: }
  239: 
  240: sub getfoilcounts {
  241:     my @names;
  242:     my $truecnt=0;
  243:     my $falsecnt=0;
  244:     my $name;
  245:     if ( $Apache::response::foilgroup{'names'} ) {
  246: 	@names= @{ $Apache::response::foilgroup{'names'} };
  247:     }
  248:     foreach $name (@names) {
  249: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  250: 	    $truecnt++;
  251: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  252: 	    $falsecnt++;
  253: 	}
  254:     }
  255:     return ($truecnt,$falsecnt);
  256: }
  257: 
  258: sub format_prior_answer {
  259:     my ($mode,$answer,$other_data) = @_;
  260:     my $foil_data = $other_data->[0];
  261:     my %response = &Apache::lonnet::str2hash($answer);
  262:     my ($name)   = keys(%response);
  263:     return '<span class="LC_prior_radiobutton">'.
  264: 	$foil_data->{$name.'.text'}.'</span>';
  265: 
  266: }
  267: 
  268: sub displayallfoils {
  269:     my ($direction, $target)=@_;
  270:     my $result;
  271:     &Apache::lonxml::debug("survey style display");
  272:     my @names;
  273:     if ( $Apache::response::foilgroup{'names'} ) {
  274: 	@names= @{ $Apache::response::foilgroup{'names'} };
  275:     }
  276:     my $temp=0;
  277:     my $i   =0;
  278:     my $id=$Apache::inputtags::response['-1'];
  279:     my $part=$Apache::inputtags::part;
  280:     my $lastresponse=
  281: 	$Apache::lonhomework::history{"resource.$part.$id.submission"};
  282:     if ($direction eq 'horizontal') { $result.='<table><tr>'; }
  283:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  284:     if (&Apache::response::show_answer() ) {
  285: 	foreach my $name (@names) {
  286: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  287: 		if (($direction eq 'horizontal') && ($target ne 'tex')) {
  288: 		    $result.="<td>";
  289: 		} else {
  290: 		    if ($target eq 'tex') {
  291: 			$result .= '\item \vskip -2mm ';
  292: 		    } else {
  293: 			$result.="<br />";
  294: 		    }
  295: 		}
  296: 		if (defined($lastresponse{$name})) {
  297: 		    if ($target eq 'tex') {
  298: 			$result .= '}';
  299: 		    } else {
  300: 			$result.='<b>';
  301: 		    }
  302: 		}
  303: 		$result .= $Apache::response::foilgroup{$name.'.text'};
  304: 		if (defined($lastresponse{$name}) && ($target ne 'tex')) {
  305: 		    $result.='</b>';
  306: 		}
  307: 		if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
  308: 	    }
  309: 	}
  310:     } else {
  311: 	foreach my $name (@names) {
  312: 	    if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  313: 		if ($direction eq 'horizontal') {
  314: 		    $result.="<td>";
  315: 		} else {
  316: 		    if ($target eq 'tex') {
  317: 			$result .= '\item \vskip -2mm ';
  318: 		    } else {
  319: 			$result.="<br />";
  320: 		    }
  321: 		}
  322: 		if ($target eq 'tex') {
  323: 		    $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
  324: 		    $i++;
  325: 		} else {
  326: 		    $result .= '<label>';
  327: 		    $result.="<input
  328:                        onchange=\"javascript:setSubmittedPart('$part');\"
  329:                        type=\"radio\"
  330:                        name=\"HWVAL_$Apache::inputtags::response['-1']\"
  331:                        value=\"$temp\" ";
  332: 		    if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  333: 		    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
  334: 			'</label>';
  335: 		}
  336: 		$temp++;
  337: 		if ($target ne 'tex') {
  338: 		    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
  339: 		} else {
  340: 		    $result.='\vskip 0 mm ';
  341: 		}
  342: 	    }
  343: 	}
  344:     }
  345:     if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
  346:     return $result;
  347: }
  348: 
  349: sub whichfoils {
  350:     my ($max,$randomize)=@_;
  351: 
  352:     my @truelist;
  353:     my @falselist;
  354:     my @whichfalse =();
  355:     my ($truecnt,$falsecnt) = &getfoilcounts();
  356:     my $count=0;
  357:     # we will add in 1 of the true statements
  358:     if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
  359:     my $answer=int(&Math::Random::random_uniform() * ($count));
  360:     &Apache::lonxml::debug("Count is $count, $answer is $answer");
  361:     my @names;
  362:     if ( $Apache::response::foilgroup{'names'} ) {
  363: 	@names= @{ $Apache::response::foilgroup{'names'} };
  364:     }
  365:     if (&Apache::response::showallfoils()) {
  366: 	@whichfalse=@names;
  367:     } elsif ($randomize eq 'no') {
  368: 	&Apache::lonxml::debug("No randomization");
  369: 	my $havetrue=0;
  370: 	foreach my $name (@names) {
  371: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  372: 		if (!$havetrue ) {
  373: 		    push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
  374: 		}
  375: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  376: 		push (@whichfalse,$name);
  377: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  378: 	    } else {
  379: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  380: 	    }
  381: 	}
  382: 	if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
  383: 	    &Apache::lonxml::error("There are no true statements available.<br />");
  384: 	}
  385:     } else {
  386: 	my $current=0;
  387: 	&Apache::lonhomework::showhash(%Apache::response::foilgroup);
  388: 	my (%top,%bottom);
  389: 	#first find out where everyone wants to be
  390: 	foreach my $name (@names) {
  391: 	    $current++;
  392: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  393: 		push (@truelist,$name);
  394: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  395: 		    $top{$name}=$current;
  396: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
  397: 		    $bottom{$name}=$current;
  398: 		}
  399: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  400: 		push (@falselist,$name);
  401: 		if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
  402: 		    $top{$name}=$current;
  403: 		} elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
  404: 		    $bottom{$name}=$current;
  405: 		}
  406: 	    } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  407: 	    } else {
  408: 		&Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
  409: 	    }
  410: 	}
  411: 	#pick a true statement
  412: 	my $notrue=0;
  413: 	if (scalar(@truelist) == 0) { $notrue=1; }
  414: 	my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
  415: 	&Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  416: 	my (@toplist, @bottomlist);
  417: 	my $topcount=0;
  418: 	my $bottomcount=0;
  419: 	# assign everyone to either toplist/bottomlist or whichfalse
  420: 	# which false is randomized, toplist bottomlist are in order
  421: 	while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
  422: 	    &Apache::lonxml::debug("Have $#whichfalse max is $max");
  423: 	    my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
  424: 	    &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  425: 	    $afalse=splice(@falselist,$afalse,1);
  426: 	    &Apache::lonxml::debug("Picked $afalse");
  427: 	    &Apache::lonhomework::showhash(('names'=>\@names));
  428: 	    &Apache::lonhomework::showhash(%top);
  429: 	    if ($top{$afalse}) {
  430: 		$toplist[$top{$afalse}]=$afalse;
  431: 		$topcount++;
  432: 	    } elsif ($bottom{$afalse}) {
  433: 		$bottomlist[$bottom{$afalse}]=$afalse;
  434: 		$bottomcount++;
  435: 	    } else {
  436: 		push (@whichfalse,$afalse);
  437: 	    }
  438: 	}
  439: 	&Apache::lonxml::debug("Answer wants $answer");
  440: 	my $truename=$truelist[$whichtrue];
  441: 	my $dosplice=1;
  442: 	if ($notrue && $Apache::lonhomework::type ne 'survey') {
  443: 	    $dosplice=0;
  444: 	    &Apache::lonxml::error("There are no true statements available.<br />");
  445: 	}
  446: 	#insert the true statement, keeping track of where it wants to be
  447: 	if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
  448: 	    $toplist[$top{$truename}]=$truename;
  449: 	    $answer=-1;
  450: 	    foreach my $top (reverse(@toplist)) {
  451: 		if ($top) { $answer++;}
  452: 		if ($top eq $truename) { last; }
  453: 	    }
  454: 	    $dosplice=0;
  455: 	} elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
  456: 	    $bottomlist[$bottom{$truename}]=$truename;
  457: 	    $answer=-1;
  458: 	    foreach my $bot (@bottomlist) {
  459: 		if ($bot) { $answer++;}
  460: 		if ($bot eq $truename) { last; }
  461: 	    }
  462: 	    $answer+=$topcount+$#whichfalse+1;
  463: 	    $dosplice=0;
  464: 	} else {
  465: 	    if ($topcount>0 || $bottomcount>0) {
  466: 		$answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
  467: 		    + $topcount;
  468: 	    }
  469: 	}
  470: 	&Apache::lonxml::debug("Answer now wants $answer");
  471: 	#add the top items to the top, bottom items to the bottom
  472: 	for (my $i=0;$i<=$#toplist;$i++) {
  473: 	    if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
  474: 	}
  475: 	for (my $i=0;$i<=$#bottomlist;$i++) {
  476: 	    if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
  477: 	}
  478: 	#if the true statement is randomized insert it into the list
  479: 	if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
  480:     }
  481:     &Apache::lonxml::debug("Answer is $answer");
  482:     return ($answer,@whichfalse);
  483: }
  484: 
  485: sub displayfoils {
  486:     my ($target,$max,$randomize,$direction)=@_;
  487:     my $result;
  488: 
  489:     my ($answer,@whichfoils)=&whichfoils($max,$randomize);
  490:     my $part=$Apache::inputtags::part;
  491:     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
  492:     if ( ($target ne 'tex') &&
  493: 	 &Apache::response::show_answer() ) {
  494: 	if ($direction eq 'horizontal') {
  495: 	    if ($target ne 'tex') {
  496: 		$result.='<table><tr>';
  497: 	    }
  498: 	}
  499: 	foreach my $name (@whichfoils) {
  500: 	    if ($direction eq 'horizontal') {
  501: 		if ($target ne 'tex') { $result.='<td>'; }
  502: 	    }
  503: 	    if ($target ne 'tex') {
  504: 		$result.="<br />";
  505: 	    } else {
  506: 		$result.='\item \vskip -2 mm  ';
  507: 	    }
  508: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  509: 		if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}
  510: 	    } else {
  511: 		$result.='Incorrect:';
  512: 	    }
  513: 	    if ($target eq 'web') { $result.="<label>"; }
  514: 	    $result.=$Apache::response::foilgroup{$name.'.text'};
  515: 	    if ($target eq 'web') { $result.="</label>"; }
  516: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  517: 		if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
  518: 	    }
  519: 	    if ($direction eq 'horizontal') {
  520: 		if ($target ne 'tex') { $result.='</td>'; }
  521: 	    }
  522: 	}
  523: 	if ($direction eq 'horizontal') {
  524: 	    if ($target ne 'tex') {
  525: 		$result.='</tr></table>';
  526: 	    }
  527: 	}
  528:     } else {
  529: 	my @alphabet = ('A'..'Z');
  530: 	my $i = 0;
  531: 	my $bubble_number = 0;
  532: 	my $temp=0;  
  533: 	my $id=$Apache::inputtags::response['-1'];
  534: 	my $part=$Apache::inputtags::part;
  535: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  536: 	my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  537: 	if ($target ne 'tex' && $direction eq 'horizontal') {
  538: 	    $result.="<table><tr>";
  539: 	}
  540: 	foreach my $name (@whichfoils) {
  541: 	    if ($target ne 'tex') {
  542: 		if ($direction eq 'horizontal') {
  543: 		    $result.="<td>"; 
  544: 		} else { 
  545: 		    $result.="<br />";
  546: 		} 
  547: 	    }
  548: 	    if ($target ne 'tex') { 
  549:                 $result.= '<label>';
  550: 		$result.=
  551: 		    "<input type=\"radio\"
  552:                             onchange=\"javascript:setSubmittedPart('$part');\"
  553:                             name=\"HWVAL_$Apache::inputtags::response['-1']\"
  554:                             value=\"$temp\" ";
  555: 		if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  556: 		$result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
  557: 	    } else {
  558: 		if ($Apache::lonhomework::type eq 'exam') {
  559: 		    $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
  560: 		    $i++;
  561: 		    $bubble_number++;
  562: 		    if($bubble_number >= $bubbles_per_line) {
  563: 			$i = 0;
  564: 			$bubble_number = 0;
  565: 			&Apache::lonxml::increment_counter(1);
  566: 			$result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
  567: 		    }
  568: 		} else {
  569: 		    $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
  570: 		}
  571: 	    }
  572: 	    if ($target ne 'tex' && $direction eq 'horizontal') {
  573: 		$result.="</td>"; 
  574: 	    }
  575: 	    $temp++;
  576: 	}
  577: 	if ($target ne 'tex' && $direction eq 'horizontal') {
  578: 	    $result.="</tr></table>";
  579: 	}
  580:     }
  581:     if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
  582:     return $result;
  583: }
  584: 
  585: sub displayallanswers {
  586:     my @names;
  587:     if ( $Apache::response::foilgroup{'names'} ) {
  588: 	@names= @{ $Apache::response::foilgroup{'names'} };
  589:     }
  590:   
  591:     my $result=&Apache::response::answer_header('radiobuttonresponse');
  592:     foreach my $name (@names) {
  593: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  594: 				$Apache::response::foilgroup{$name.'.value'});
  595:     }
  596:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
  597:     return $result;
  598: }
  599: 
  600: sub displayanswers {
  601:     my ($max,$randomize)=@_;
  602:     my ($answer,@whichopt) = &whichfoils($max,$randomize);
  603:     my $result=&Apache::response::answer_header('radiobuttonresponse');
  604:     if ($Apache::lonhomework::type eq 'exam') {
  605: 	my $correct = ('A'..'Z')[$answer];
  606: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  607: 						$correct);
  608:     }
  609:     foreach my $name (@whichopt) {
  610: 	$result.=&Apache::response::answer_part('radiobuttonresponse',
  611: 						$Apache::response::foilgroup{$name.'.value'});
  612:     }
  613:     $result.=&Apache::response::answer_footer('radiobuttonresponse');
  614:     return $result;
  615: }
  616: 
  617: sub start_conceptgroup {
  618:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  619:     $Apache::radiobuttonresponse::conceptgroup=1;
  620:     %Apache::response::conceptgroup=();
  621:     my $result;
  622:     if ($target eq 'edit') {
  623: 	$result.=&Apache::edit::tag_start($target,$token);
  624: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  625: 	    &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  626:     } elsif ($target eq 'modified') {
  627: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  628: 						     $safeeval,'concept');
  629: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  630:     }
  631:     return $result;
  632: }
  633: 
  634: sub end_conceptgroup {
  635:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  636:     $Apache::radiobuttonresponse::conceptgroup=0;
  637:     my $result;
  638:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  ||
  639: 	$target eq 'tex' || $target eq 'analyze') {
  640: 	&Apache::response::pick_foil_for_concept($target,
  641: 						 ['value','text','location'],
  642: 						 \%Apache::hint::radiobutton,
  643: 						 $parstack,$safeeval);
  644:     } elsif ($target eq 'edit') {
  645: 	$result=&Apache::edit::end_table();
  646:     }
  647:     return $result;
  648: }
  649: 
  650: sub insert_conceptgroup {
  651:     my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  652:     return $result;
  653: }
  654: 
  655: sub start_foil {
  656:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  657:     my $result='';
  658:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  659: 	&Apache::lonxml::startredirection;
  660: 	if ($target eq 'analyze') {
  661: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
  662: 	}
  663:     } elsif ($target eq 'edit') {
  664: 	$result=&Apache::edit::tag_start($target,$token);
  665: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  666: 	$result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
  667: 						   ['unused','true','false'],
  668: 						   $token);
  669: 	my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
  670: 						 $safeeval,'-3');
  671: 	if ($randomize ne 'no') {
  672: 	    $result.=&Apache::edit::select_arg('Location:','location',
  673: 					       ['random','top','bottom'],$token);
  674: 	}
  675: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  676:     } elsif ($target eq 'modified') {
  677: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  678: 						     $safeeval,'value','name',
  679: 						     'location');
  680: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  681:     } 
  682:     return $result;
  683: }
  684: 
  685: sub end_foil {
  686:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  687:     my $text='';
  688:     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  689: 	$text=&Apache::lonxml::endredirection;
  690:     }
  691:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
  692: 	|| $target eq 'tex' || $target eq 'analyze') {
  693: 	my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  694: 	if ($value ne 'unused') {
  695: 	    my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  696: 	    if ($name eq "") {
  697: 		&Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
  698: 		$name=$Apache::lonxml::curdepth;
  699: 	    }
  700: 	    if (defined($Apache::response::foilnames{$name})) {
  701: 		&Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
  702: 	    }
  703: 	    $Apache::response::foilnames{$name}++;
  704: 	    my $location =&Apache::lonxml::get_param('location',$parstack,
  705: 						     $safeeval);
  706: 	    if ( $Apache::radiobuttonresponse::conceptgroup
  707: 		 && !&Apache::response::showallfoils() ) {
  708: 		push @{ $Apache::response::conceptgroup{'names'} }, $name;
  709: 		$Apache::response::conceptgroup{"$name.value"} = $value;
  710: 		$Apache::response::conceptgroup{"$name.text"} = $text;	
  711: 		$Apache::response::conceptgroup{"$name.location"} = $location;
  712: 	    } else {
  713: 		push @{ $Apache::response::foilgroup{'names'} }, $name;
  714: 		$Apache::response::foilgroup{"$name.value"} = $value;
  715: 		$Apache::response::foilgroup{"$name.text"} = $text;
  716: 		$Apache::response::foilgroup{"$name.location"} = $location;
  717: 	    }
  718: 	}
  719:     }
  720:     return '';
  721: }
  722: 
  723: sub insert_foil {
  724:     return '
  725: <foil name="" value="unused">
  726: <startouttext />
  727: <endouttext />
  728: </foil>';
  729: }
  730: 1;
  731: __END__
  732:  

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