File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.113: download - view: text, annotated - select for diffs
Tue Mar 27 19:20:49 2007 UTC (17 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#4244 enter works correctly now also with ption/radio/rank/match response

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

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