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

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

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