Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.93

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

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