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

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.31      albertel    3: #
1.71    ! albertel    4: # $Id: radiobuttonresponse.pm,v 1.70 2003/04/18 17:10:08 sakharuk 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.22      albertel   28: # 2/21 Guy
1.1       albertel   29: 
                     30: package Apache::radiobuttonresponse;
                     31: use strict;
1.42      albertel   32: use HTML::Entities();
1.1       albertel   33: 
1.36      harris41   34: BEGIN {
1.22      albertel   35:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1       albertel   36: }
                     37: 
                     38: sub start_radiobuttonresponse {
1.22      albertel   39:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26      albertel   40:   my $result;
1.3       albertel   41:   #when in a radiobutton response use these
1.22      albertel   42:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26      albertel   43:   push (@Apache::lonxml::namespace,'radiobuttonresponse');
1.4       albertel   44:   my $id = &Apache::response::start_response($parstack,$safeeval);
1.58      albertel   45:   %Apache::hint::radiobutton=();
1.25      albertel   46:   if ($target eq 'meta') {
                     47:     $result=&Apache::response::meta_package_write('radiobuttonresponse');
1.26      albertel   48:   } elsif ($target eq 'edit' ) {
                     49:     $result.=&Apache::edit::start_table($token).
1.35      matthew    50: 	'<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
1.26      albertel   51: 	&Apache::edit::deletelist($target,$token)
1.35      matthew    52: 	."</td><td>&nbsp".&Apache::edit::end_row()
                     53:         .&Apache::edit::start_spanning_row();
                     54: 
1.47      albertel   55:     $result.=
                     56: 	&Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
                     57: 	&Apache::edit::select_arg('Randomize Foil Order','randomize',
                     58: 				  ['yes','no'],$token).
                     59:         &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
1.26      albertel   60:   } elsif ($target eq 'modified') {
                     61:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.47      albertel   62: 						 $safeeval,'max','randomize');
1.26      albertel   63:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38      sakharuk   64:   } elsif ($target eq 'tex') {
1.70      sakharuk   65:       my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,undef,0);
                     66:       if ($type eq '1') {
                     67: 	  $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
                     68:       } elsif ($type eq 'A') {
                     69: 	  $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
                     70:       } elsif ($type eq 'a') {
                     71: 	  $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
                     72:       } elsif ($type eq 'i') {
                     73: 	  $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
                     74:       }
1.38      sakharuk   75:       $result .= '\begin{enumerate}';
1.25      albertel   76:   }
                     77:   return $result;
1.1       albertel   78: }
                     79: 
                     80: sub end_radiobuttonresponse {
1.26      albertel   81:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     82:   my $result;
                     83:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.38      sakharuk   84:   if ($target eq 'tex') { $result .= '\end{enumerate}'; }
1.22      albertel   85:   &Apache::response::end_response;
1.26      albertel   86:   pop @Apache::lonxml::namespace;
1.33      albertel   87:   &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26      albertel   88:   return $result;
1.1       albertel   89: }
                     90: 
1.43      albertel   91: %Apache::response::foilgroup=();
1.1       albertel   92: sub start_foilgroup {
1.56      sakharuk   93:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     94:   my $result;
1.43      albertel   95:   %Apache::response::foilgroup=();
1.22      albertel   96:   $Apache::radiobuttonresponse::conceptgroup=0;
                     97:   &Apache::response::setrandomnumber();
1.56      sakharuk   98:   if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
                     99:       $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
                    100:   }
                    101:   return $result;
1.5       albertel  102: }
                    103: 
1.15      albertel  104: sub storesurvey {
1.33      albertel  105:   if ( !defined($ENV{'form.submitted'})) { return ''; }
1.68      albertel  106:   my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.33      albertel  107:   &Apache::lonxml::debug("Here I am!:$response:");
                    108:   if ( $response !~ /[0-9]+/) { return ''; }
                    109:   my $id = $Apache::inputtags::response['-1'];
                    110:   my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
                    111:   my %responsehash;
                    112:   $responsehash{$whichfoils[$response]}=$response;
                    113:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
                    114:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
                    115:   &Apache::lonxml::debug("submitted a $response<br />\n");
1.22      albertel  116:   return '';
1.15      albertel  117: }
                    118: 
1.32      albertel  119: sub grade_response {
1.49      albertel  120:   my ($max,$randomize)=@_;
1.55      albertel  121:   #keep the random numbers the same must always call this
                    122:   my ($answer,@whichfoils)=&whichfoils($max,$randomize);
1.32      albertel  123:   if (!defined($ENV{'form.submitted'})) { return; }
1.65      albertel  124:   my $response;
                    125:   if ($ENV{'form.submitted'} eq 'scantron') {
1.69      albertel  126:       $response=&Apache::response::getresponse();
1.65      albertel  127:   } else {
1.68      albertel  128:       $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.65      albertel  129:   }
1.32      albertel  130:   if ( $response !~ /[0-9]+/) { return; }
                    131:   my $part=$Apache::inputtags::part;
                    132:   my $id = $Apache::inputtags::response['-1'];
                    133:   my %responsehash;
                    134:   $responsehash{$whichfoils[$response]}=$response;
                    135:   my $responsestr=&Apache::lonnet::hash2str(%responsehash);
                    136:   my %previous=&Apache::response::check_for_previous($responsestr,
                    137: 						     $part,$id);
                    138:   $Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    139:     $responsestr;
                    140:   &Apache::lonxml::debug("submitted a $response<br />\n");
                    141:   my $ad;
                    142:   if ($response == $answer) {
                    143:     $ad='EXACT_ANS';
                    144:   } else {
                    145:     $ad='INCORRECT';
                    146:   }
                    147:   $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
                    148:   &Apache::response::handle_previous(\%previous,$ad);
                    149: }
                    150: 
1.1       albertel  151: sub end_foilgroup {
1.22      albertel  152:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29      albertel  153: 
1.22      albertel  154:   my $result;
1.38      sakharuk  155:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.29      albertel  156:     my $style = $Apache::lonhomework::type;
1.22      albertel  157:     if ( $style eq 'survey' ) {
1.38      sakharuk  158:       if ($target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.22      albertel  159: 	$result=&displayallfoils();
                    160:       } elsif ( $target eq 'grade' ) {
                    161: 	$result=&storesurvey();
                    162:       }
                    163:     } else {
                    164:       my $name;
1.49      albertel  165:       my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
1.47      albertel  166:       my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
                    167: 						 $safeeval,'-2');
1.38      sakharuk  168:       if ($target eq 'web' || $target eq 'tex') {
1.49      albertel  169: 	$result=&displayfoils($target,$max,$randomize);
1.28      albertel  170:       } elsif ($target eq 'answer' ) {
1.49      albertel  171: 	$result=&displayanswers($max,$randomize);
1.22      albertel  172:       } elsif ( $target eq 'grade') {
1.49      albertel  173: 	&grade_response($max,$randomize);
1.22      albertel  174:       }
1.5       albertel  175:     }
1.65      albertel  176:   }
1.67      albertel  177:   &Apache::lonxml::increment_counter();
1.22      albertel  178:   return $result;
1.6       albertel  179: }
                    180: 
                    181: sub getfoilcounts {
1.34      albertel  182:   my @names;
1.22      albertel  183:   my $truecnt=0;
                    184:   my $falsecnt=0;
                    185:   my $name;
1.34      albertel  186:   if ( $Apache::response::foilgroup{'names'} ) {
                    187:     @names= @{ $Apache::response::foilgroup{'names'} };
                    188:   }
1.22      albertel  189:   foreach $name (@names) {
                    190:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    191:       $truecnt++;
                    192:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    193:       $falsecnt++;
1.6       albertel  194:     }
1.22      albertel  195:   }
1.50      albertel  196:   return ($truecnt,$falsecnt);
1.5       albertel  197: }
                    198: 
1.15      albertel  199: sub displayallfoils {
1.22      albertel  200:   my $result;
                    201:   &Apache::lonxml::debug("survey style display");
                    202:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    203:   my $temp=0;
                    204:   my $id=$Apache::inputtags::response['-1'];
                    205:   my $part=$Apache::inputtags::part;
                    206:   my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.32      albertel  207:   my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.45      albertel  208:   if (($Apache::lonhomework::history{"resource.$part.solved"} =~ /^correct/)  || ($Apache::inputtags::status[-1] eq  'SHOW_ANSWER')) {
                    209:     foreach my $name (@names) {
                    210:       if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
                    211: 	$result.="<br />".$Apache::response::foilgroup{$name.'.value'};
                    212: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    213: 	  $result.='<b>';
                    214: 	}
                    215: 	$result .= $Apache::response::foilgroup{$name.'.text'};
                    216: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    217: 	  $result.='</b>';
                    218: 	}
                    219:       }
                    220:     }
                    221:   } else {
                    222:     foreach my $name (@names) {
                    223:       if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.68      albertel  224: 	$result.="<br /><input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.45      albertel  225: 	if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
                    226: 	$result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    227: 	$temp++;
                    228:       }
1.15      albertel  229:     }
1.22      albertel  230:   }
                    231:   return $result;
1.15      albertel  232: }
                    233: 
1.28      albertel  234: sub whichfoils {
1.49      albertel  235:   my ($max,$randomize)=@_;
1.28      albertel  236: 
1.22      albertel  237:   my @truelist;
                    238:   my @falselist;
1.41      albertel  239:   my @whichfalse =();
1.50      albertel  240:   my ($truecnt,$falsecnt) = &getfoilcounts();
1.49      albertel  241:   my $count=0;
                    242:   # we will add in 1 of the true statements
                    243:   if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
1.51      albertel  244:   my $answer=int(&Math::Random::random_uniform() * ($count));
1.49      albertel  245:   &Apache::lonxml::debug("Count is $count, $answer is $answer");
1.34      albertel  246:   my @names;
                    247:   if ( $Apache::response::foilgroup{'names'} ) {
1.49      albertel  248:       @names= @{ $Apache::response::foilgroup{'names'} };
1.34      albertel  249:   }
1.41      albertel  250:   if (&Apache::response::showallfoils()) {
1.49      albertel  251:       @whichfalse=@names;
1.48      albertel  252:   } elsif ($randomize eq 'no') {
1.47      albertel  253:       &Apache::lonxml::debug("No randomization");
                    254:       my $havetrue=0;
                    255:       foreach my $name (@names) {
                    256: 	  if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
1.49      albertel  257: 	      if (!$havetrue ) {
                    258: 		  push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
                    259: 	      }
1.47      albertel  260: 	  } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    261: 	      push (@whichfalse,$name);
                    262: 	  } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    263: 	  } else {
                    264: 	      &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
                    265: 	  }
                    266:       }
1.41      albertel  267:   } else {
1.49      albertel  268:     my $current=0;
                    269:     &Apache::lonhomework::showhash(%Apache::response::foilgroup);
                    270:     my (%top,%bottom);
                    271:     #first find out where everyone wants to be
1.41      albertel  272:     foreach my $name (@names) {
1.49      albertel  273: 	$current++;
                    274: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    275: 	    push (@truelist,$name);
                    276: 	    if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    277: 		$top{$name}=$current;
                    278: 	    } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    279: 		$bottom{$name}=$current;
                    280: 	    }
                    281: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    282: 	    push (@falselist,$name);
                    283: 	    if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
                    284: 		$top{$name}=$current;
                    285: 	    } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
                    286: 		$bottom{$name}=$current;
                    287: 	    }
                    288: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    289: 	} else {
                    290: 	    &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
                    291: 	}
1.41      albertel  292:     }
1.49      albertel  293:     #pick a true statement
1.51      albertel  294:     my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
1.41      albertel  295:     &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
1.49      albertel  296:     my (@toplist, @bottomlist);
                    297:     my $topcount=0;
1.71    ! albertel  298:     my $bottomcount=0;
1.49      albertel  299:     # assign everyone to either toplist/bottomlist or whichfalse
                    300:     # which false is randomized, toplist bottomlist are in order
1.71    ! albertel  301:     while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
1.49      albertel  302: 	&Apache::lonxml::debug("Have $#whichfalse max is $max");
1.51      albertel  303: 	my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
1.49      albertel  304: 	&Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    305: 	$afalse=splice(@falselist,$afalse,1);
                    306: 	&Apache::lonxml::debug("Picked $afalse");
                    307: 	&Apache::lonhomework::showhash(('names'=>\@names));
                    308: 	&Apache::lonhomework::showhash(%top);
                    309: 	if ($top{$afalse}) {
                    310: 	    $toplist[$top{$afalse}]=$afalse;
                    311: 	    $topcount++;
                    312: 	} elsif ($bottom{$afalse}) {
                    313: 	    $bottomlist[$bottom{$afalse}]=$afalse;
1.71    ! albertel  314: 	    $bottomcount++;
1.49      albertel  315: 	} else {
                    316: 	    push (@whichfalse,$afalse);
                    317: 	}
                    318:     }
1.71    ! albertel  319:     &Apache::lonxml::debug("Answer wants $answer");
1.49      albertel  320:     my $truename=$truelist[$whichtrue];
                    321:     my $dosplice=1;
                    322:     #insert the true statement, keeping track of where it wants to be
                    323:     if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' ) {
                    324: 	$toplist[$top{$truename}]=$truename;
                    325: 	$answer=-1;
                    326: 	foreach my $top (reverse(@toplist)) {
                    327: 	    if ($top) { $answer++;}
                    328: 	    if ($top eq $truename) { last; }
                    329: 	}
                    330: 	$dosplice=0;
                    331:     } elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom') {
                    332: 	$bottomlist[$bottom{$truename}]=$truename;
                    333: 	$answer=-1;
                    334: 	foreach my $bot (@bottomlist) {
                    335: 	    if ($bot) { $answer++;}
                    336: 	    if ($bot eq $truename) { last; }
                    337: 	}
                    338: 	$answer+=$topcount+$#whichfalse+1;
                    339: 	$dosplice=0;
                    340:     } else {
1.71    ! albertel  341: 	if ($topcount>0 || $bottomcount>0) {
        !           342: 	    $answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
        !           343: 		+ $topcount;
1.51      albertel  344: 	}
1.49      albertel  345:     }
1.71    ! albertel  346:     &Apache::lonxml::debug("Answer now wants $answer");
1.49      albertel  347:     #add the top items to the top, bottom items to the bottom
                    348:     for (my $i=0;$i<=$#toplist;$i++) {
                    349: 	if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
                    350:     }
                    351:     for (my $i=0;$i<=$#bottomlist;$i++) {
                    352: 	if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
1.3       albertel  353:     }
1.49      albertel  354:     #if the true statement is randomized insert it into the list
                    355:     if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.22      albertel  356:   }
1.49      albertel  357:   &Apache::lonxml::debug("Answer is $answer");
                    358:   return ($answer,@whichfalse);
1.28      albertel  359: }
                    360: 
                    361: sub displayfoils {
1.49      albertel  362:   my ($target,$max,$randomize)=@_;
1.28      albertel  363:   my $result;
                    364: 
1.49      albertel  365:   my ($answer,@whichfoils)=&whichfoils($max,$randomize);
1.62      albertel  366:   my $part=$Apache::inputtags::part;
                    367:   my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
                    368:   my $status=$Apache::inputtags::status[-1];
                    369:   if ( ($target ne 'tex') &&
                    370:        (($solved =~ /^correct/) || ($status eq  'SHOW_ANSWER')) ) {
1.28      albertel  371:     foreach my $name (@whichfoils) {
1.38      sakharuk  372:       if ($target ne 'tex') {
                    373: 	  $result.="<br />";
1.44      sakharuk  374:       } else {
1.61      sakharuk  375: 	  $result.='\item \vskip -2 mm  ';
1.38      sakharuk  376:       }
1.22      albertel  377:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
1.60      sakharuk  378: 	  if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}
1.22      albertel  379:       } else {
1.45      albertel  380: 	$result.='Incorrect:';
                    381:       }
1.46      sakharuk  382:       if ($target ne 'tex') {
                    383: 	  $result.=$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    384:       } else {
                    385: 	  $result.=$Apache::response::foilgroup{$name.'.text'};
                    386:       }
1.45      albertel  387:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
1.60      sakharuk  388: 	  if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
1.22      albertel  389:       }
1.10      albertel  390:     }
1.22      albertel  391:   } else {
1.64      sakharuk  392:     my @alphabet = ('A'..'Z');
1.56      sakharuk  393:     my $i = 0;
1.54      sakharuk  394:     my $temp=0;  
1.22      albertel  395:     my $id=$Apache::inputtags::response['-1'];
                    396:     my $part=$Apache::inputtags::part;
                    397:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.32      albertel  398:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.28      albertel  399:     foreach my $name (@whichfoils) {
1.38      sakharuk  400:       if ($target ne 'tex') {
1.68      albertel  401: 	  $result.="<br /><input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.38      sakharuk  402: 	  if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
                    403: 	  $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    404:       } else {
1.54      sakharuk  405: 	  if ($Apache::lonhomework::type eq 'exam') {
1.58      albertel  406: 	      $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
1.56      sakharuk  407: 	      $i++;
                    408: 	  } else {
1.59      sakharuk  409: 	      $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
1.54      sakharuk  410: 	  }
1.38      sakharuk  411:       }
1.22      albertel  412:       $temp++;
1.10      albertel  413:     }
1.22      albertel  414:   }
1.44      sakharuk  415:   if ($target ne 'tex') { $result.="<br />"; } else { $result.='\vskip 0 mm '; }
1.39      albertel  416:   return $result;
1.14      albertel  417: }
                    418: 
1.28      albertel  419: sub displayanswers {
1.49      albertel  420:   my ($max,$randomize)=@_;
                    421:   my ($answer,@whichopt) = &whichfoils($max,$randomize);
1.28      albertel  422:   my $result=&Apache::response::answer_header('radiobuttonresponse');
                    423:   foreach my $name (@whichopt) {
                    424:     $result.=&Apache::response::answer_part('radiobuttonresponse',
                    425: 		     $Apache::response::foilgroup{$name.'.value'})
                    426:   }
                    427:   $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    428:   return $result;
                    429: }
                    430: 
1.14      albertel  431: sub start_conceptgroup {
1.27      albertel  432:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22      albertel  433:   $Apache::radiobuttonresponse::conceptgroup=1;
1.43      albertel  434:   %Apache::response::conceptgroup=();
1.26      albertel  435:   my $result;
                    436:   if ($target eq 'edit') {
                    437:     $result.=&Apache::edit::tag_start($target,$token);
                    438:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
1.35      matthew   439: 	&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.26      albertel  440:   } elsif ($target eq 'modified') {
                    441:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    442: 						 $safeeval,'concept');
                    443:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    444:   }
                    445:   return $result;
1.14      albertel  446: }
                    447: 
                    448: sub end_conceptgroup {
1.22      albertel  449:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    450:   $Apache::radiobuttonresponse::conceptgroup=0;
1.26      albertel  451:   my $result;
1.38      sakharuk  452:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  || $target eq 'tex') {
1.27      albertel  453:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    454:       my @names = @{ $Apache::response::conceptgroup{'names'} };
1.51      albertel  455:       my $pick=int(&Math::Random::random_uniform() * ($#names+1));
1.27      albertel  456:       my $name=$names[$pick];
                    457:       push @{ $Apache::response::foilgroup{'names'} }, $name;
                    458:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
                    459:       $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
1.49      albertel  460:       $Apache::response::foilgroup{"$name.location"} = $Apache::response::conceptgroup{"$name.location"};
1.27      albertel  461:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    462:       $Apache::response::foilgroup{"$name.concept"} = $concept;
                    463:       &Apache::lonxml::debug("Selecting $name in $concept");
1.58      albertel  464:       my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
                    465:       push(@{ $Apache::hint::radiobutton{"$part_id.concepts"} },$concept);
                    466:       $Apache::hint::radiobutton{"$part_id.concept.$concept"}=
                    467: 	  $Apache::response::conceptgroup{'names'};
1.27      albertel  468:     }
1.26      albertel  469:   } elsif ($target eq 'edit') {
                    470:     $result=&Apache::edit::end_table();
1.22      albertel  471:   }
1.26      albertel  472:   return $result;
                    473: }
                    474: 
                    475: sub insert_conceptgroup {
                    476:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    477:   return $result;
1.1       albertel  478: }
                    479: 
                    480: sub start_foil {
1.24      albertel  481:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    482:   my $result='';
1.38      sakharuk  483:   if ($target eq 'web' || $target eq 'tex') {
1.27      albertel  484:     &Apache::lonxml::startredirection;
                    485:   } elsif ($target eq 'edit') {
                    486:     $result=&Apache::edit::tag_start($target,$token);
                    487:     $result.=&Apache::edit::text_arg('Name:','name',$token);
1.42      albertel  488:     $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
1.28      albertel  489: 				       ['unused','true','false'],$token);
1.51      albertel  490:     my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
                    491: 					     $safeeval,'-3');
1.53      albertel  492:     if ($randomize ne 'no') {
1.51      albertel  493:       $result.=&Apache::edit::select_arg('Location:','location',
1.52      albertel  494: 					 ['random','top','bottom'],$token);
1.51      albertel  495:     }
1.35      matthew   496:     $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.27      albertel  497:   } elsif ($target eq 'modified') {
                    498:      my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
1.48      albertel  499: 						  'value','name','location');
1.27      albertel  500:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38      sakharuk  501:   } 
1.27      albertel  502:   return $result;
1.1       albertel  503: }
                    504: 
                    505: sub end_foil {
1.22      albertel  506:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    507:   my $text='';
1.38      sakharuk  508:   if ($target eq 'web' || $target eq 'tex') { $text=&Apache::lonxml::endredirection; }
                    509:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  || $target eq 'tex') {
1.19      albertel  510:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18      albertel  511:     if ($value ne 'unused') {
1.19      albertel  512:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.48      albertel  513:       my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.27      albertel  514:       if (!$name) { $name=$Apache::lonxml::curdepth; }
1.41      albertel  515:       if ( $Apache::radiobuttonresponse::conceptgroup
                    516: 	   && !&Apache::response::showallfoils() ) {
1.18      albertel  517: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    518: 	$Apache::response::conceptgroup{"$name.value"} = $value;
                    519: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
1.48      albertel  520: 	$Apache::response::conceptgroup{"$name.location"} = $location;	
1.18      albertel  521:       } else {
                    522: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    523: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    524: 	$Apache::response::foilgroup{"$name.text"} = $text;
1.48      albertel  525: 	$Apache::response::foilgroup{"$name.location"} = $location;
1.18      albertel  526:       }
                    527:     }
1.4       albertel  528:   }
1.1       albertel  529:   return '';
                    530: }
                    531: 
1.27      albertel  532: sub insert_foil {
                    533:   return '
                    534: <foil name="" value="unused">
                    535: <startouttext />
                    536: <endouttext />
                    537: </foil>';
                    538: }
1.1       albertel  539: 1;
                    540: __END__
                    541:  

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