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

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

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