File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.115: download - view: text, annotated - select for diffs
Thu May 17 10:01:44 2007 UTC (17 years ago) by foxr
Branches: MAIN
CVS tags: HEAD
:q!

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

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