Diff for /loncom/homework/radiobuttonresponse.pm between versions 1.84 and 1.147

version 1.84, 2004/03/05 23:04:31 version 1.147, 2010/12/19 04:24:29
Line 18 Line 18
 # GNU General Public License for more details.  # GNU General Public License for more details.
 #  #
 # You should have received a copy of the GNU General Public License  # You should have received a copy of the GNU General Public License
 # along with LON-CAPA; if not, write to the Free Software  # along with LON-CAPA; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
 #  #
 # /home/httpd/html/adm/gpl.txt  # /home/httpd/html/adm/gpl.txt
 #  #
 # http://www.lon-capa.org/  # http://www.lon-capa.org/
 #  #
   
   
   
 package Apache::radiobuttonresponse;  package Apache::radiobuttonresponse;
 use strict;  use strict;
 use HTML::Entities();  use HTML::Entities();
   use Apache::lonlocal;
   use Apache::lonnet;
   use Apache::response;
   
   my $default_bubbles_per_line = 10;
   
   
 BEGIN {  BEGIN {
     &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));      &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
 }  }
   
   sub bubble_line_count {
       my ($numfoils, $bubbles_per_line) = @_;
       my $bubble_lines;
       $bubble_lines = int($numfoils / $bubbles_per_line);
       if (($numfoils % $bubbles_per_line) != 0) {
    $bubble_lines++;
       }
       return $bubble_lines;
       
   }
   
   
 sub start_radiobuttonresponse {  sub start_radiobuttonresponse {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result;      my $result;
   
     #when in a radiobutton response use these      #when in a radiobutton response use these
     &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));      &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
     push (@Apache::lonxml::namespace,'radiobuttonresponse');      push (@Apache::lonxml::namespace,'radiobuttonresponse');
     my $id = &Apache::response::start_response($parstack,$safeeval);      my $id = &Apache::response::start_response($parstack,$safeeval);
   
     %Apache::hint::radiobutton=();      %Apache::hint::radiobutton=();
       undef(%Apache::response::foilnames);
     if ($target eq 'meta') {      if ($target eq 'meta') {
  $result=&Apache::response::meta_package_write('radiobuttonresponse');   $result=&Apache::response::meta_package_write('radiobuttonresponse');
     } elsif ($target eq 'edit' ) {      } elsif ($target eq 'edit' ) {
  $result.=&Apache::edit::start_table($token).   $result.=&Apache::edit::start_table($token)
     '<tr><td>'.&Apache::lonxml::description($token).             .'<tr><td>'.&Apache::lonxml::description($token)
     &Apache::loncommon::help_open_topic('Radio_Response_Problems').             .&Apache::loncommon::help_open_topic('Radio_Response_Problems')
     "</td><td>Delete:".             .'</td>'
     &Apache::edit::deletelist($target,$token)             .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
     ."</td><td>&nbsp".&Apache::edit::end_row()             .&Apache::edit::deletelist($target,$token)
     .&Apache::edit::start_spanning_row();             .'</span></td>'
              .'<td>&nbsp;'.&Apache::edit::end_row()
              .&Apache::edit::start_spanning_row();
  $result.=   $result.=
     &Apache::edit::text_arg('Max Number Of Shown Foils:','max',      &Apache::edit::text_arg('Max Number Of Shown Foils:','max',
     $token,'4').      $token,'4').'&nbsp;'x 3 .
     &Apache::edit::select_arg('Randomize Foil Order','randomize',      &Apache::edit::select_arg('Randomize Foil Order:','randomize',
       ['yes','no'],$token).        ['yes','no'],$token).'&nbsp;'x 3 .
       &Apache::edit::select_arg('Display Direction:','direction',
         ['vertical','horizontal'],$token).
       &Apache::edit::end_row().        &Apache::edit::end_row().
       &Apache::edit::start_spanning_row()."\n";        &Apache::edit::start_spanning_row()."\n";
     } elsif ($target eq 'modified') {      } elsif ($target eq 'modified') {
  my $constructtag=&Apache::edit::get_new_args($token,$parstack,   my $constructtag=&Apache::edit::get_new_args($token,$parstack,
      $safeeval,'max',       $safeeval,'max',
      'randomize');       'randomize','direction');
  if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }   if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
     } elsif ($target eq 'tex') {      } elsif ($target eq 'tex') {
  my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,   my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,
Line 75  sub start_radiobuttonresponse { Line 101  sub start_radiobuttonresponse {
     $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';      $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
  } elsif ($type eq 'i') {   } elsif ($type eq 'i') {
     $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';      $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
    } else {
       $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
  }   }
  $result .= '\begin{enumerate}';          if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
               $result .= '\begin{itemize}';
           } else {
               $result .= '\begin{enumerate}';
           }
     } elsif ($target eq 'analyze') {      } elsif ($target eq 'analyze') {
  my $part_id="$Apache::inputtags::part.$id";   my $part_id="$Apache::inputtags::part.$id";
           $Apache::lonhomework::analyze{"$part_id.type"} = 'radiobuttonresponse';
  push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);   push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
     }      }
     return $result;      return $result;
Line 88  sub end_radiobuttonresponse { Line 121  sub end_radiobuttonresponse {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result;      my $result;
     if ($target eq 'edit') { $result=&Apache::edit::end_table(); }      if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
     if ($target eq 'tex') { $result .= '\end{enumerate}'; }      if ($target eq 'tex' ) {
           if($env{'form.pdfFormFields'} eq 'yes' and $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
               $result .= '\end{itemize}';
           } else {
               $result .= '\end{enumerate}';
           }
       }
     &Apache::response::end_response;      &Apache::response::end_response;
     pop @Apache::lonxml::namespace;      pop @Apache::lonxml::namespace;
     &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));      &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
       undef(%Apache::response::foilnames);
     return $result;      return $result;
 }  }
   
Line 101  sub start_foilgroup { Line 141  sub start_foilgroup {
     my $result;      my $result;
     %Apache::response::foilgroup=();      %Apache::response::foilgroup=();
     $Apache::radiobuttonresponse::conceptgroup=0;      $Apache::radiobuttonresponse::conceptgroup=0;
     &Apache::response::setrandomnumber();      &Apache::response::pushrandomnumber(undef,$target);
     if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {      if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
  $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';   $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
     }      }
Line 109  sub start_foilgroup { Line 149  sub start_foilgroup {
 }  }
   
 sub storesurvey {  sub storesurvey {
     if ( !defined($ENV{'form.submitted'})) { return ''; }      my ($style) = @_;
     my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1']};      if ( !&Apache::response::submitted() ) { return ''; }
       my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
     &Apache::lonxml::debug("Here I am!:$response:");      &Apache::lonxml::debug("Here I am!:$response:");
     if ( $response !~ /[0-9]+/) { return ''; }      if ( $response !~ /[0-9]+/) { return ''; }
       my $part = $Apache::inputtags::part;
     my $id = $Apache::inputtags::response['-1'];      my $id = $Apache::inputtags::response['-1'];
     my @whichfoils=@{ $Apache::response::foilgroup{'names'} };      my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
     my %responsehash;      my %responsehash;
     $responsehash{$whichfoils[$response]}=$response;      $responsehash{$whichfoils[$response]}=$response;
     $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);      my $responsestr=&Apache::lonnet::hash2str(%responsehash);
     $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';      $Apache::lonhomework::results{"resource.$part.$id.submission"}=
    $responsestr;
       my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
       my $ad;
       if ($style eq 'anonsurvey') {
           $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
       } elsif ($style eq 'anonsurveycred') {
           $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
       } elsif ($style eq 'surveycred') {
           $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
       } else {
           $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
       }
       &Apache::response::handle_previous(\%previous,$ad);
     &Apache::lonxml::debug("submitted a $response<br />\n");      &Apache::lonxml::debug("submitted a $response<br />\n");
     return '';      return '';
 }  }
   
   
 sub grade_response {  sub grade_response {
     my ($max,$randomize)=@_;      my ($answer, $whichfoils, $bubbles_per_line)=@_;
     #keep the random numbers the same must always call this  
     my ($answer,@whichfoils)=&whichfoils($max,$randomize);      if ( !&Apache::response::submitted() ) { return; }
     if (!defined($ENV{'form.submitted'})) { return; }  
     my $response;      my $response;
     if ($ENV{'form.submitted'} eq 'scantron') {      
  $response=&Apache::response::getresponse();      if ($env{'form.submitted'} eq 'scantron') {
    $response = &Apache::response::getresponse(1,undef,
      &bubble_line_count(scalar(@{ $whichfoils}),
         $bubbles_per_line),
      $bubbles_per_line);
   
     } else {      } else {
  $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1']};   $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
     }      }
   
   
     if ( $response !~ /[0-9]+/) { return; }      if ( $response !~ /[0-9]+/) { return; }
     my $part=$Apache::inputtags::part;      my $part=$Apache::inputtags::part;
     my $id = $Apache::inputtags::response['-1'];      my $id = $Apache::inputtags::response['-1'];
     my %responsehash;      my %responsehash;
     $responsehash{$whichfoils[$response]}=$response;      $responsehash{$whichfoils->[$response]}=$response;
     my $responsestr=&Apache::lonnet::hash2str(%responsehash);      my $responsestr=&Apache::lonnet::hash2str(%responsehash);
     my %previous=&Apache::response::check_for_previous($responsestr,      my %previous=&Apache::response::check_for_previous($responsestr,
        $part,$id);         $part,$id);
Line 159  sub end_foilgroup { Line 221  sub end_foilgroup {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   
     my $result;      my $result;
       my $bubble_lines;
       my $bubbles_per_line;
       my $answer_count;
       my $id   = $Apache::inputtags::response['-1'];
       my $part = $Apache::inputtags::part;
       $bubbles_per_line = 
    &Apache::response::get_response_param($Apache::inputtags::part."_$id",
         'numbubbles',
         $default_bubbles_per_line);
   
   
     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||      if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  $target eq 'tex' || $target eq 'analyze') {   $target eq 'tex' || $target eq 'analyze') {
  my $style = $Apache::lonhomework::type;   my $style = $Apache::lonhomework::type;
  if ( $style eq 'survey'  && $target ne 'analyze') {   my $direction = &Apache::lonxml::get_param('direction',$parstack,
      $safeeval,'-2');
    if ( (($style eq 'survey') || ($style eq 'surveycred') || 
                 ($style eq 'anonsurvey') || ($style eq 'anonsurveycred')) 
                && ($target ne 'analyze')) {
     if ($target eq 'web' || $target eq 'tex') {      if ($target eq 'web' || $target eq 'tex') {
  $result=&displayallfoils();   $result=&displayallfoils($direction, $target);
     } elsif ( $target eq 'answer' ) {      } elsif ( $target eq 'answer' ) {
  $result=&displayallanswers();   $result=&displayallanswers();
     } elsif ( $target eq 'grade' ) {      } elsif ( $target eq 'grade' ) {
  $result=&storesurvey();   $result=&storesurvey($style);
     }      }
       $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
   
  } else {   } else {
   
     my $name;      my $name;
     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,      my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
  '-2');   '-2');
     my $randomize = &Apache::lonxml::get_param('randomize',$parstack,      my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
        $safeeval,'-2');         $safeeval,'-2');
       my ($answer, @shown) = &whichfoils($max, $randomize);
       $answer_count = scalar(@shown);
   
     if ($target eq 'web' || $target eq 'tex') {      if ($target eq 'web' || $target eq 'tex') {
  $result=&displayfoils($target,$max,$randomize);   $result=&displayfoils($target,
         $answer, \@shown,
         $direction,
         $bubbles_per_line);
     } elsif ($target eq 'answer' ) {      } elsif ($target eq 'answer' ) {
  $result=&displayanswers($max,$randomize);   $result=&displayanswers($answer, \@shown, $bubbles_per_line);
     } elsif ( $target eq 'grade') {      } elsif ( $target eq 'grade') {
  &grade_response($max,$randomize);   &grade_response($answer, \@shown, $bubbles_per_line);
     }  elsif ( $target eq 'analyze') {      }  elsif ( $target eq 'analyze') {
  my @shown = &whichfoils($max,$randomize);   my $bubble_lines = &bubble_line_count($answer_count, 
         $bubbles_per_line);
  &Apache::response::analyze_store_foilgroup(\@shown,   &Apache::response::analyze_store_foilgroup(\@shown,
    ['text','value','location']);     ['text','value','location']);
  my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";   my $part_id="$part.$id";
  push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },   push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
       ('true','false'));        ('true','false'));
   
     }      }
  }   }
    $Apache::lonxml::post_evaluate=0;
       }
       if ($target eq 'web') {
    &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
     [\%Apache::response::foilgroup]);
       }
       &Apache::response::poprandomnumber();
       $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
       &Apache::lonxml::increment_counter($bubble_lines,
          "$part.$id");
       if ($target eq 'analyze') {
    &Apache::lonhomework::set_bubble_lines();
     }      }
     &Apache::lonxml::increment_counter();  
     return $result;      return $result;
 }  }
   
Line 214  sub getfoilcounts { Line 313  sub getfoilcounts {
     return ($truecnt,$falsecnt);      return ($truecnt,$falsecnt);
 }  }
   
   sub format_prior_answer {
       my ($mode,$answer,$other_data) = @_;
       my $foil_data = $other_data->[0];
       my %response = &Apache::lonnet::str2hash($answer);
       my ($name)   = keys(%response);
       return '<span class="LC_prior_radiobutton">'.
    $foil_data->{$name.'.text'}.'</span>';
   
   }
   
 sub displayallfoils {  sub displayallfoils {
       my ($direction, $target)=@_;
     my $result;      my $result;
     &Apache::lonxml::debug("survey style display");      &Apache::lonxml::debug("survey style display");
     my @names = @{ $Apache::response::foilgroup{'names'} };      my @names;
       if ( $Apache::response::foilgroup{'names'} ) {
    @names= @{ $Apache::response::foilgroup{'names'} };
       }
   
     my $temp=0;      my $temp=0;
       my $i   =0;
     my $id=$Apache::inputtags::response['-1'];      my $id=$Apache::inputtags::response['-1'];
     my $part=$Apache::inputtags::part;      my $part=$Apache::inputtags::part;
     my $lastresponse=      my $lastresponse;
  $Apache::lonhomework::history{"resource.$part.$id.submission"};      my ($lastresponse,$newvariation,$showanswer);
       if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
           ($Apache::lonhomework::type eq 'randomizetry')) &&
           ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
           if ($env{'form.'.$part.'.rndseed'} ne
               $Apache::lonhomework::history{"resource.$part.rndseed"}) {
               $newvariation = 1;
           }
       }
       $showanswer = &Apache::response::show_answer();
       unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred')) && (defined($env{'form.grade_symb'})) ||
              ($newvariation && !$showanswer)) {
           $lastresponse = 
       $Apache::lonhomework::history{"resource.$part.$id.submission"};
       }
       if ($direction eq 'horizontal') { $result.='<table><tr>'; }
     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);      my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
     if (&Apache::response::show_answer() ) {      if ($showanswer) {
  foreach my $name (@names) {   foreach my $name (@names) {
     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {      if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  $result.="<br />";   if (($direction eq 'horizontal') && ($target ne 'tex')) {
       $result.="<td>";
    } else {
       if ($target eq 'tex') {
    $result .= '\item \vskip -2mm ';
       } else {
    $result.="<br />";
       }
    }
  if (defined($lastresponse{$name})) {   if (defined($lastresponse{$name})) {
     $result.='<b>';      if ($target eq 'tex') {
    $result .= '}';
       } else {
    $result.='<b>';
       }
  }   }
  $result .= $Apache::response::foilgroup{$name.'.text'};   $result .= $Apache::response::foilgroup{$name.'.text'};
  if (defined($lastresponse{$name})) {   if (defined($lastresponse{$name}) && ($target ne 'tex')) {
     $result.='</b>';      $result.='</b>';
  }   }
    if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
     }      }
  }   }
     } else {      } else {
  foreach my $name (@names) {   foreach my $name (@names) {
     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {      if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  $result.="<br /><input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";   if ($direction eq 'horizontal') {
  if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }      $result.="<td>";
  $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";   } else {
       if ($target eq 'tex') {
           if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                               my $fieldname = $env{'request.symb'}.'&part_'. $Apache::inputtags::part
                                               .'&radiobuttonresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'];
                               $result .= '\item[{'.&Apache::lonxml::print_pdf_radiobutton($fieldname,$temp).'}]'
                                          .$Apache::response::foilgroup{$name.'.text'}."\n";
                           } else {
                               $result .= '\item \vskip -2mm ';
                           }
                       } else {
    $result.="<br />";
       }
    }
    if ($target eq 'tex') {
       if($env{'form.pdfFormFields'} ne 'yes' or $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
                           $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
                       }
       $i++;
    } else {
       $result .= '<label>';
       $result.="<input
                          onchange=\"javascript:setSubmittedPart('$part');\"
                          type=\"radio\"
                          name=\"HWVAL_$Apache::inputtags::response['-1']\"
                          value=\"$temp\"";
   
       if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
       $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
    '</label>';
    }
  $temp++;   $temp++;
    if ($target ne 'tex') {
       if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
    } else {
       $result.='\vskip 0 mm ';
    }
     }      }
  }   }
     }      }
       
       if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
     return $result;      return $result;
 }  }
   
   
 sub whichfoils {  sub whichfoils {
     my ($max,$randomize)=@_;      my ($max,$randomize)=@_;
   
Line 259  sub whichfoils { Line 440  sub whichfoils {
     my ($truecnt,$falsecnt) = &getfoilcounts();      my ($truecnt,$falsecnt) = &getfoilcounts();
     my $count=0;      my $count=0;
     # we will add in 1 of the true statements      # we will add in 1 of the true statements
     if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }      if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
     my $answer=int(&Math::Random::random_uniform() * ($count));      my $answer=int(&Math::Random::random_uniform() * ($count));
     &Apache::lonxml::debug("Count is $count, $answer is $answer");      &Apache::lonxml::debug("Count is $count, $answer is $answer");
     my @names;      my @names;
Line 280  sub whichfoils { Line 461  sub whichfoils {
  push (@whichfalse,$name);   push (@whichfalse,$name);
     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {      } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
     } else {      } else {
  &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));   &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
     }      }
  }   }
    if ((!$havetrue) && 
               ($Apache::lonhomework::type ne 'survey') && 
               ($Apache::lonhomework::type ne 'surveycred') &&
               ($Apache::lonhomework::type ne 'anonsurvey') &&
               ($Apache::lonhomework::type ne 'anonsurveycred')) {
       &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
    }
     } else {      } else {
  my $current=0;   my $current=0;
  &Apache::lonhomework::showhash(%Apache::response::foilgroup);   &Apache::lonhomework::showhash(%Apache::response::foilgroup);
Line 306  sub whichfoils { Line 494  sub whichfoils {
  }   }
     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {      } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
     } else {      } else {
  &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));   &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
     }      }
  }   }
  #pick a true statement   #pick a true statement
Line 340  sub whichfoils { Line 528  sub whichfoils {
  &Apache::lonxml::debug("Answer wants $answer");   &Apache::lonxml::debug("Answer wants $answer");
  my $truename=$truelist[$whichtrue];   my $truename=$truelist[$whichtrue];
  my $dosplice=1;   my $dosplice=1;
  if ($notrue && $Apache::lonhomework::type ne 'survey') {   if (($notrue) && 
               ($Apache::lonhomework::type ne 'survey') &&
               ($Apache::lonhomework::type ne 'surveycred') &&
               ($Apache::lonhomework::type ne 'anonsurvey') &&
               ($Apache::lonhomework::type ne 'anonsurveycred')) {
     $dosplice=0;      $dosplice=0;
     &Apache::lonxml::error("There are no true statements available.<br />");      &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
  }   }
  #insert the true statement, keeping track of where it wants to be   #insert the true statement, keeping track of where it wants to be
  if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {   if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
Line 384  sub whichfoils { Line 576  sub whichfoils {
 }  }
   
 sub displayfoils {  sub displayfoils {
     my ($target,$max,$randomize)=@_;      my ($target,$answer,$whichfoils,$direction, $bubbles_per_line)=@_;
     my $result;      my $result;
   
     my ($answer,@whichfoils)=&whichfoils($max,$randomize);  
     my $part=$Apache::inputtags::part;      my $part=$Apache::inputtags::part;
     my $solved=$Apache::lonhomework::history{"resource.$part.solved"};      my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
     if ( ($target ne 'tex') &&      if ( ($target ne 'tex') &&
  &Apache::response::show_answer() ) {   &Apache::response::show_answer() ) {
  foreach my $name (@whichfoils) {   if ($direction eq 'horizontal') {
       if ($target ne 'tex') {
    $result.='<table><tr>';
       }
    }
    foreach my $name (@{ $whichfoils }) {
       if ($direction eq 'horizontal') {
    if ($target ne 'tex') { $result.='<td>'; }
       }
     if ($target ne 'tex') {      if ($target ne 'tex') {
  $result.="<br />";   $result.="<br />";
     } else {      } else {
  $result.='\item \vskip -2 mm  ';   $result.='\item \vskip -2 mm  ';
     }      }
     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}   if ($target ne 'tex') {
                       $result.=&mt('Correct:').'<b>';
                   } else {
                       $result.=&mt('Correct:').' \textbf{';
                   }
     } else {      } else {
  $result.='Incorrect:';   $result.=&mt('Incorrect:');
     }  
     if ($target ne 'tex') {  
  $result.=$Apache::response::foilgroup{$name.'.text'}."</input>\n";  
     } else {  
  $result.=$Apache::response::foilgroup{$name.'.text'};  
     }      }
       if ($target eq 'web') { $result.="<label>"; }
       $result.=$Apache::response::foilgroup{$name.'.text'};
       if ($target eq 'web') { $result.="</label>"; }
     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {      if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}   if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
     }      }
       if ($direction eq 'horizontal') {
    if ($target ne 'tex') { $result.='</td>'; }
       }
    }
    if ($direction eq 'horizontal') {
       if ($target ne 'tex') {
    $result.='</tr></table>';
       }
  }   }
     } else {      } else {
  my @alphabet = ('A'..'Z');   my @alphabet = ('A'..'Z');
  my $i = 0;   my $i = 0;
    my $bubble_number = 0;
    my $line = 0;
  my $temp=0;     my $temp=0;  
  my $id=$Apache::inputtags::response['-1'];   my $id=$Apache::inputtags::response['-1'];
  my $part=$Apache::inputtags::part;   my $part=$Apache::inputtags::part;
  my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};          my ($lastresponse,$newvariation);
           if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
                ($Apache::lonhomework::type eq 'randomizetry')) && 
           ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
               if ($env{'form.'.$part.'.rndseed'} ne
                   $Apache::lonhomework::history{"resource.$part.rndseed"}) {
                   $newvariation = 1;
               }
           }
           unless ($newvariation) {
       $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
           }
  my %lastresponse=&Apache::lonnet::str2hash($lastresponse);   my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  foreach my $name (@whichfoils) {   if ($target ne 'tex' && $direction eq 'horizontal') {
       $result.="<table><tr>";
    }
    foreach my $name (@{ $whichfoils }) {
     if ($target ne 'tex') {      if ($target ne 'tex') {
  $result.="<br /><input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";   if ($direction eq 'horizontal') {
  if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }      $result.="<td>"; 
  $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";   } else { 
       $result.="<br />";
    } 
       }
       if ($target ne 'tex') { 
                   $result.= '<label>';
    $result.=
       "<input type=\"radio\"
                               onchange=\"javascript:setSubmittedPart('$part');\"
                               name=\"HWVAL_$Apache::inputtags::response['-1']\"
                               value=\"$temp\"";
    if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
    $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
     } else {      } else {
  if ($Apache::lonhomework::type eq 'exam') {   if ($Apache::lonhomework::type eq 'exam') {
       if($bubble_number >= $bubbles_per_line) {
    $line++;
    $i = 0;
    $bubble_number = 0;
    $result.='\item[\textbf{'.($Apache::lonxml::counter+$line).'}.]';
       }
     $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs      $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\';  #' stupid emacs
     $i++;      $i++;
       $bubble_number++;
  } else {   } else {
     $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};                      if($env{'form.pdfFormFields'} eq 'yes' && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  }                          my $fieldname = $env{'request.symb'}.'&part_'. $Apache::inputtags::part
                                           .'&radiobuttonresponse'.'&HWVAL_'.$Apache::inputtags::response['-1'];
                           $result .= '\item[{'.&Apache::lonxml::print_pdf_radiobutton($fieldname,$temp).'}]'
                                      .$Apache::response::foilgroup{$name.'.text'}."\n";
                       } else { 
                           $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
                       }
                   }
       }
       if ($target ne 'tex' && $direction eq 'horizontal') {
    $result.="</td>"; 
     }      }
     $temp++;      $temp++;
  }   }
    if ($target ne 'tex' && $direction eq 'horizontal') {
       $result.="</tr></table>";
    }
     }      }
     if ($target ne 'tex') { $result.="<br />"; } else { $result.='\vskip 0 mm '; }      if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
     return $result;      return $result;
 }  }
   
 sub displayallanswers {  sub displayallanswers {
     my @names = @{ $Apache::response::foilgroup{'names'} };      my @names;
         if ( $Apache::response::foilgroup{'names'} ) {
    @names= @{ $Apache::response::foilgroup{'names'} };
       }
     my $result=&Apache::response::answer_header('radiobuttonresponse');      my $result=&Apache::response::answer_header('radiobuttonresponse');
     foreach my $name (@names) {      foreach my $name (@names) {
  $result.=&Apache::response::answer_part('radiobuttonresponse',   $result.=&Apache::response::answer_part('radiobuttonresponse',
Line 453  sub displayallanswers { Line 712  sub displayallanswers {
 }  }
   
 sub displayanswers {  sub displayanswers {
     my ($max,$randomize)=@_;      my ($answer, $whichopt, $bubbles_per_line)=@_;
     my ($answer,@whichopt) = &whichfoils($max,$randomize);      my $result;
     my $result=&Apache::response::answer_header('radiobuttonresponse');  
     foreach my $name (@whichopt) {      if ($Apache::lonhomework::type eq 'exam') {
    my $line = int($answer/$bubbles_per_line);
    my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
    $result .= &Apache::response::answer_header('radiobuttonresponse',
       $line);
    $result .= &Apache::response::answer_part('radiobuttonresponse',
     $correct);
       } else {
    $result .= &Apache::response::answer_header('radiobuttonresponse');
       }
       foreach my $name (@{ $whichopt }) {
  $result.=&Apache::response::answer_part('radiobuttonresponse',   $result.=&Apache::response::answer_part('radiobuttonresponse',
  $Apache::response::foilgroup{$name.'.value'})   $Apache::response::foilgroup{$name.'.value'});
  }      }
     $result.=&Apache::response::answer_footer('radiobuttonresponse');      $result.=&Apache::response::answer_footer('radiobuttonresponse');
     return $result;      return $result;
 }  }
Line 507  sub start_foil { Line 776  sub start_foil {
     my $result='';      my $result='';
     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {      if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  &Apache::lonxml::startredirection;   &Apache::lonxml::startredirection;
    if ($target eq 'analyze') {
       &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
    }
     } elsif ($target eq 'edit') {      } elsif ($target eq 'edit') {
  $result=&Apache::edit::tag_start($target,$token);   $result=&Apache::edit::tag_start($target,$token);
  $result.=&Apache::edit::text_arg('Name:','name',$token);   $result.=&Apache::edit::text_arg('Name:','name',$token);
Line 535  sub end_foil { Line 807  sub end_foil {
     if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {      if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
  $text=&Apache::lonxml::endredirection;   $text=&Apache::lonxml::endredirection;
     }      }
     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'  || $target eq 'tex' || $target eq 'analyze') {      if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
    || $target eq 'tex' || $target eq 'analyze') {
  my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);   my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  if ($value ne 'unused') {   if ($value ne 'unused') {
     my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);      my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
     my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);      if ($name eq "") {
     if (!$name) { $name=$Apache::lonxml::curdepth; }   &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
    $name=$Apache::lonxml::curdepth;
       }
       if (defined($Apache::response::foilnames{$name})) {
    &Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
       }
       $Apache::response::foilnames{$name}++;
       my $location =&Apache::lonxml::get_param('location',$parstack,
        $safeeval);
     if ( $Apache::radiobuttonresponse::conceptgroup      if ( $Apache::radiobuttonresponse::conceptgroup
  && !&Apache::response::showallfoils() ) {   && !&Apache::response::showallfoils() ) {
  push @{ $Apache::response::conceptgroup{'names'} }, $name;   push @{ $Apache::response::conceptgroup{'names'} }, $name;
Line 567  sub insert_foil { Line 848  sub insert_foil {
 }  }
 1;  1;
 __END__  __END__
   
   
   
   =head1 NAME
   
   Apache::radiobuttonresponse
   
   =head1 SYNOPSIS
   
   Handles multiple-choice style responses.
   
   This is part of the LearningOnline Network with CAPA project
   described at http://www.lon-capa.org.
   
   =head1 SUBROUTINES
   
   =over
   
   =item start_radiobuttonresponse()
   
   =item bubble_line_count()
   
   =item end_radiobuttonresponse()
   
   =item start_foilgroup()
   
   =item storesurvey()
   
   =item grade_response()
   
   =item end_foilgroup()
   
   =item getfoilcounts()
   
   =item format_prior_answer()
   
   =item displayallfoils()
   
   =item &whichfoils($max,$randomize)
   
   Randomizes the list of foils.
   Respects
     - each foils desire to be randomized
     - the existance of Concept groups of foils (select 1 foil from each)
     - and selects a single correct statement from all possilble true statments
     - and limits it to a toal of $max foils
   
   WARNING: this routine uses the random number generator, it should only
   be called once per target, otherwise it can cause randomness changes in
   homework problems.
   
   Arguments
     $max - maximum number of foils to select (including the true one)
            (so a max of 5 is: 1 true, 4 false)
   
     $randomize - whether to randomize the listing of foils, by default
                  will randomize, only if randomize is 'no' will it not
   
   Returns
     $answer - location in the array of the correct answer
     @foils  - array of foil names in to display order
   
   =item displayfoils()
   
   =item displayallanswers()
   
   =item displayanswers()
   
   =item start_conceptgroup()
   
   =item end_conceptgroup()
   
   =item insert_conceptgroup()
   
   =item start_foil()
   
   =item end_foil()
   
   =item insert_foil()
   
   =back
   
   =cut
     

Removed from v.1.84  
changed lines
  Added in v.1.147


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