File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.96: download - view: text, annotated - select for diffs
Thu Oct 21 03:34:06 2004 UTC (19 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_1_2_99_1, version_1_2_99_0, HEAD
- propely do previous cheiking or rbr survey responses

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

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