Diff for /loncom/homework/caparesponse/caparesponse.pm between versions 1.73 and 1.84

version 1.73, 2002/11/25 21:36:42 version 1.84, 2003/03/21 15:33:46
Line 85  sub start_numericalresponse { Line 85  sub start_numericalresponse {
     $result.=&Apache::edit::tag_start($target,$token);      $result.=&Apache::edit::tag_start($target,$token);
     $result.=&Apache::edit::text_arg('Answer:','answer',$token);      $result.=&Apache::edit::text_arg('Answer:','answer',$token);
     if ($token->[1] eq 'numericalresponse') {      if ($token->[1] eq 'numericalresponse') {
       $result.=&Apache::edit::text_arg('Unit:','unit',$token,5);        $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
       $result.=&Apache::edit::text_arg('Format:','format',$token,4);                           &Apache::loncommon::help_open_topic('Physical_Units');
         $result.=&Apache::edit::text_arg('Format:','format',$token,4).
                 &Apache::loncommon::help_open_topic('Numerical_Response_Format');
     } elsif ($token->[1] eq 'stringresponse') {      } elsif ($token->[1] eq 'stringresponse') {
       $result.=&Apache::edit::select_arg('Type:','type',        $result.=&Apache::edit::select_arg('Type:','type',
  [['cs','Case Sensitive'],['ci','Case Insensitive'],   [['cs','Case Sensitive'],['ci','Case Insensitive'],
   ['mc','Case Insensitive, Any Order']],$token);    ['mc','Case Insensitive, Any Order']],$token);
     } elsif ($token->[1] eq 'formularesponse') {      } elsif ($token->[1] eq 'formularesponse') {
       $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);        $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40).
                &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
     }      }
     $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();      $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
   } elsif ($target eq 'modified') {    } elsif ($target eq 'modified') {
Line 122  sub start_numericalresponse { Line 125  sub start_numericalresponse {
 sub end_numericalresponse {  sub end_numericalresponse {
   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   my $result = '';    my $result = '';
     if (!$Apache::lonxml::default_homework_loaded) {
         &Apache::lonxml::default_homework_load($safeeval);
     }
   if ( $target eq 'grade' ) {    if ( $target eq 'grade' ) {
     if ( defined $ENV{'form.submitted'}) {      if ( defined $ENV{'form.submitted'}) {
       &Apache::response::setup_params($$tagstack[-1]);        &Apache::response::setup_params($$tagstack[-1]);
Line 130  sub end_numericalresponse { Line 136  sub end_numericalresponse {
       my $id = $Apache::inputtags::response['-1'];        my $id = $Apache::inputtags::response['-1'];
       my $response = $ENV{'form.HWVAL'.$id};        my $response = $ENV{'form.HWVAL'.$id};
       if ( $response =~ /[^\s]/) {        if ( $response =~ /[^\s]/) {
  if (!$Apache::lonxml::default_homework_loaded) {  
     &Apache::lonxml::default_homework_load($safeeval);  
  }  
  my %previous = &Apache::response::check_for_previous($response,$partid,$id);   my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;   $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  &Apache::lonxml::debug("submitted a $response<br>\n");   &Apache::lonxml::debug("submitted a $response<br>\n");
Line 141  sub end_numericalresponse { Line 144  sub end_numericalresponse {
  $response =~ s/\'/\\\'/g;   $response =~ s/\'/\\\'/g;
  &Apache::lonxml::debug("current $response");   &Apache::lonxml::debug("current $response");
  my $expression="&caparesponse_check_list('".$response."','".   my $expression="&caparesponse_check_list('".$response."','".
   $$parstack[$#$parstack];      $$parstack[-1];
  foreach my $key (keys(%Apache::inputtags::params)) {   foreach my $key (keys(%Apache::inputtags::params)) {
   $expression.= ';my $'. #'    $expression.= ';my $'. #'
     $key.'="'.$Apache::inputtags::params{$key}.'"';      $key.'="'.$Apache::inputtags::params{$key}.'"';
Line 175  sub end_numericalresponse { Line 178  sub end_numericalresponse {
  if ($target eq 'web') {   if ($target eq 'web') {
   $result="<br />The correct answer is ";    $result="<br />The correct answer is ";
         } elsif ($target eq 'tex') {          } elsif ($target eq 'tex') {
   $result='\vskip 0 mm The correct answer is \\texttt{';    #$result='\vskip 0 mm The correct answer is \\texttt{';
         }          }
  for (my $i=0; $i <= $#answers; $i++) {   for (my $i=0; $i <= $#answers; $i++) {
    my $answer=$answers[$i];     my $answer=$answers[$i];
Line 193  sub end_numericalresponse { Line 196  sub end_numericalresponse {
      &Apache::lonxml::debug("no format answer :$answer:");       &Apache::lonxml::debug("no format answer :$answer:");
      $formatted="$answer,";       $formatted="$answer,";
    }     }
    if ($target eq 'tex') {$formatted=&Apache::lonxml::latex_special_symbols($formatted);}  
      $result.=$formatted;     if ($target eq 'tex') {
            }         $formatted='';
          #$formatted=&Apache::lonxml::latex_special_symbols($formatted);
      }
      $result.=$formatted;
          }
    chop $result;     chop $result;
    if ($target eq 'web') {     if ($target eq 'web') {
      $result.=" $unit.<br />";       $result.=" $unit.<br />";
    } elsif ($target eq 'tex') {     } elsif ($target eq 'tex') {
      $result.=&Apache::lonxml::latex_special_symbols($unit);       #$result.=&Apache::lonxml::latex_special_symbols($unit);
      $result.="}. \\vskip 0 mm ";       #$result.="}. \\vskip 0 mm ";
    }     }
        }         }
       if ($Apache::lonhomework::type eq 'exam') {        if ($Apache::lonhomework::type eq 'exam') {
Line 216  sub end_numericalresponse { Line 223  sub end_numericalresponse {
  my $powers_number = 8; #default values for number of powers   my $powers_number = 8; #default values for number of powers
  my $symb;   my $symb;
  if ($ENV{'form.symb'}=~/___\d+___/) {$symb=$ENV{'form.symb'};} else {$symb=$ENV{'request.symb'};}   if ($ENV{'form.symb'}=~/___\d+___/) {$symb=$ENV{'form.symb'};} else {$symb=$ENV{'request.symb'};}
  my $randomseed = srand(&Apache::lonnet::rndseed($symb,$ENV{'request.course.id'},  
                                                               $ENV{'user.domain'},$ENV{'user.name'}));   &Apache::response::setrandomnumber();
  my $ind=1+int(rand()*($factors_number-1));   my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
  my $factor = $factors[$ind];  
  $ind=1+int(rand()*($powers_number-1));  
  my $power = $powers[$ind];   my $power = $powers[$ind];
    $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
    my $factor = $factors[$ind];
  for ($ind=0;$ind<$number_of_bubbles;$ind++) {   for ($ind=0;$ind<$number_of_bubbles;$ind++) {
     $bubbles_values[$ind] = $answers[0]*$factor**($power-$powers[$powers_number-$ind-1]);      $bubbles_values[$ind] = $answers[0]*($factor**($power-$powers[$#powers-$ind]));
  }       }
  my @alphabet = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',   my @alphabet=('A'..'Z');
  'Q','R','S','T','U','V','W','X','Y','Z');  
         if ($target eq 'web') {          if ($target eq 'web') {
     if ($$tagstack[-1] eq 'numericalresponse') {
     my $id=$Apache::inputtags::response[-1];      my $id=$Apache::inputtags::response[-1];
       $result.=' (<i>in '.$unit.'</i>)<br /><br />';
     $result.= '<table border="1"><tr>';      $result.= '<table border="1"><tr>';
     for ($ind=0;$ind<$number_of_bubbles;$ind++) {      for ($ind=0;$ind<$number_of_bubbles;$ind++) {
  my $ans;   my $ans;
Line 250  sub end_numericalresponse { Line 258  sub end_numericalresponse {
     $result.='<td><input type="radio" name="HWVAL'.$id.'" value="'.$ans.'"><b>'.$alphabet[$ind].'</b>: '.$ans.'</td>';      $result.='<td><input type="radio" name="HWVAL'.$id.'" value="'.$ans.'"><b>'.$alphabet[$ind].'</b>: '.$ans.'</td>';
     }      }
     $result.='</tr></table>';      $result.='</tr></table>';
     } elsif ($$tagstack[-1] eq 'formularesponse') {
       $result.= '<br /><br /><font color="red"> Formula have to be entered here! </font><br /><br />';
     }
  } elsif ($target eq 'tex') {   } elsif ($target eq 'tex') {
     my $max_val = 0;   if (defined $unit and $Apache::lonhomework::type eq 'exam') {
     if ($formats[0]=~m/^(\d+)E([^\d]*)(\d*)$/) {     $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
  $max_val=$1+$2+4;           }
     } else {   if ($$tagstack[-1] eq 'numericalresponse') {
  $max_val=4;       my $max_val = 0;
     }       if ($formats[0]=~m/^(\d+)E([^\d]*)(\d*)$/) {
     $max_val = int(0.9*$ENV{'form.textwidth'}/(($max_val+6)*2));   $max_val=$1+$2+4;
             my $celllength = 0.9*$ENV{'form.textwidth'}/$max_val-10;       } else {
     my @table_range = ();   $max_val=4;
     my $number_of_tables = int($number_of_bubbles/$max_val);       }
     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}       $max_val = int(0.9*$ENV{'form.textwidth'}/(($max_val+6)*2));
     if ($number_of_bubbles % $max_val != 0) {   my $celllength = 0.9*$ENV{'form.textwidth'}/$max_val-10;
  $number_of_tables++;       my @table_range = ();
  push @table_range,($number_of_bubbles % $max_val);       my $number_of_tables = int($number_of_bubbles/$max_val);
     }   for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}
     my $j=0;       if ($number_of_bubbles % $max_val != 0) {
     my $cou=0;      $number_of_tables++;
     $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';      push @table_range,($number_of_bubbles % $max_val);
     for (my $i=0;$i<$number_of_tables;$i++) {  
  $result.='\vskip -1 mm \noindent \begin{tabular}{';  
  for ($ind=0;$ind<$table_range[$j];$ind++) {  
     $result.='lp{'.$celllength.' mm}';  
  }  
  $result.='}';  
  for ($ind=$cou;$ind<$cou+$table_range[$j];$ind++) {  
     my $ans;  
     if ($formats[0] ne '') {  
  $ans = sprintf('%.'.$formats[0],$bubbles_values[$ind]);  
  if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {  
     my $number = $1;  
     my $power = $2;  
     $power=~s/^\+//;  
     $power=~s/^(-?)0+(\d+)//;  
     $ans=$number.'$\times 10^{'.$1.$2.'}$'; #'stupidemacs  
  }   
     } else {  
  my $badans = $bubbles_values[$ind];  
  my $format = '';   
  #What is the number? (integer,decimal,floating point)  
  if ($badans=~/^(\d*\.?\d*)(E|e)(\d*)$/) {  
     $format = 'e'.$2;  
  } elsif ($badans=~/^(\d*)\.(\d*)$/) {  
     $format = '4f';  
  } elsif ($badans=~/^(\d*)$/) {  
     $format = 'd';  
  }  
  $ans = sprintf('%.'.$format,$bubbles_values[$ind]);  
  if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {  
     my $number = $1;  
     my $power = $2;  
     $power=~s/^\+//;  
     $power=~s/^(-?)0+(\d+)//;  
     $ans=$number.'$\times 10^{'.$1.$2.'}$'; #'stupidemacs  
  }   
     }  
     $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$ans.'} ';  
     if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}  
  }   }
  $cou += $table_range[$j];       my $j=0;
  $j++;       my $cou=0;
  $result.='\\\\\end{tabular}\vskip 0 mm ';           $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
        for (my $i=0;$i<$number_of_tables;$i++) {
    $result.='\vskip -1 mm \noindent \begin{tabular}{';
    for ($ind=0;$ind<$table_range[$j];$ind++) {
        $result.='lp{'.$celllength.' mm}';
    }
    $result.='}';
    for ($ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
        my $ans;
        if ($formats[0] ne '') {
    $ans = sprintf('%.'.$formats[0],$bubbles_values[$ind]);
    if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
        my $number = $1;
        my $power = $2;
        $power=~s/^\+//;
        $power=~s/^(-?)0+(\d+)//;
        $ans=$number.'$\times 10^{'.$1.$2.'}$'; #'stupidemacs
    } 
        } else {
    my $badans = $bubbles_values[$ind];
    my $format = ''; 
    #What is the number? (integer,decimal,floating point)
    if ($badans=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
        $format = 'e'.$2;
    } elsif ($badans=~/^(\d*)\.(\d*)$/) {
        $format = '4f';
    } elsif ($badans=~/^(\d*)$/) {
        $format = 'd';
    }
    $ans = sprintf('%.'.$format,$bubbles_values[$ind]);
    if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
        my $number = $1;
        my $power = $2;
        $power=~s/^\+//;
        $power=~s/^(-?)0+(\d+)//;
        $ans=$number.'$\times 10^{'.$1.$2.'}$'; #'stupidemacs
    } 
        }
        $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$ans.'} ';
        if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
    }
    $cou += $table_range[$j];
    $j++;
    $result.='\\\\\end{tabular}\vskip 0 mm ';    
        }
        $result.='\end{enumerate}';
        &Apache::lonxml::increment_counter();
            } else {
        $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
       my $id = $Apache::inputtags::part;
       my $weight = &Apache::lonnet::EXT("resource.$id.weight");
               my $repetition = int $weight/9;
       if ($weight % 9 != 0) {$repetition++;} 
         $result.='\begin{enumerate}';
         for (my $i=0;$i<$repetition;$i++) {
    $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
    &Apache::lonxml::increment_counter();
         }
         $result.= '\end{enumerate}';
     }      }
     $result.='\end{enumerate}';  
     &Apache::lonxml::increment_counter();  
  }   }
     }      }
   } elsif ($target eq 'edit') {    } elsif ($target eq 'edit') {

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


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