File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.111: download - view: text, annotated - select for diffs
Thu Feb 22 01:25:31 2007 UTC (17 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#5206, <foil>s were posibly getting 2 eauation passes

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

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