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

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

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