File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.110: download - view: text, annotated - select for diffs
Tue Dec 19 11:05:21 2006 UTC (17 years, 4 months ago) by foxr
Branches: MAIN
CVS tags: version_2_3_X, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, HEAD
BUG 4795 - Survey printing fails 'cause displayallfoils has no
conditional branches for $target eq 'tex'.  Put in a first crack at this.
NOTE: are the opportunities for a refactoriziation of the display of a single
foil between displayallfoils and display foil?

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

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