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

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

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