Diff for /loncom/interface/Attic/lonspreadsheet.pm between versions 1.164 and 1.172

version 1.164, 2003/01/29 16:26:08 version 1.172, 2003/02/17 16:29:51
Line 262  ENDSCRIPT Line 262  ENDSCRIPT
     # Global directory configs      # Global directory configs
     #      #
     $sheet->includedir($r->dir_config('lonIncludes'));      $sheet->includedir($r->dir_config('lonIncludes'));
     $sheet->tmpdir($r->dir_config('lonDaemons').'/tmp/');  
     #      #
     # Check user permissions      # Check user permissions
     if (($sheet->{'type'}  eq 'classcalc'       ) ||       if (($sheet->{'type'}  eq 'classcalc'       ) || 
Line 487  use GDBM_File; Line 486  use GDBM_File;
 use HTML::Entities();  use HTML::Entities();
 use HTML::TokeParser;  use HTML::TokeParser;
 use Spreadsheet::WriteExcel;  use Spreadsheet::WriteExcel;
   use Time::HiRes;
   
 #  #
 # These global hashes are dependent on user, course and resource,   # These global hashes are dependent on user, course and resource, 
 # and need to be initialized every time when a sheet is calculated  # and need to be initialized every time when a sheet is calculated
Line 522  my %numbertimes; Line 523  my %numbertimes;
 # Directories  # Directories
 #  #
 my $includedir;  my $includedir;
 my $tmpdir;  
   
 sub includedir {  sub includedir {
     my $self = shift;      my $self = shift;
     $includedir = shift;      $includedir = shift;
 }  }
   
 sub tmpdir {  
     my $self = shift;  
     $tmpdir = shift;  
 }  
   
 my %spreadsheets;  my %spreadsheets;
 my %loadedcaches;  #my %loadedcaches;
 my %courserdatas;  my %courserdatas;
 my %userrdatas;  my %userrdatas;
 my %defaultsheets;  my %defaultsheets;
 my %rowlabel_cache;  my %rowlabel_cache;
 my %oldsheets;  #my %oldsheets;
   
 sub complete_recalc {  sub complete_recalc {
     my $self = shift;      my $self = shift;
Line 607  sub cachedssheets { Line 602  sub cachedssheets {
     my ($uname,$udom) = @_;      my ($uname,$udom) = @_;
     $uname = $uname || $self->{'uname'};      $uname = $uname || $self->{'uname'};
     $udom  = $udom  || $self->{'udom'};      $udom  = $udom  || $self->{'udom'};
     if (! $Apache::lonspreadsheet::loadedcaches{$uname.'_'.$udom}) {      if (! exists($Apache::lonspreadsheet::loadedcaches{$uname.'_'.$udom})) {
         my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.          my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
                                         $ENV{'request.course.id'},                                          $ENV{'request.course.id'},
                                         $self->{'udom'},                                          $self->{'udom'},
Line 756  sub new { Line 751  sub new {
         chome => $ENV{'course.'.$ENV{'request.course.id'}.'.home'},          chome => $ENV{'course.'.$ENV{'request.course.id'}.'.home'},
         coursefilename => $ENV{'request.course.fn'},          coursefilename => $ENV{'request.course.fn'},
         coursedesc => $ENV{'course.'.$ENV{'request.course.id'}.'.description'},          coursedesc => $ENV{'course.'.$ENV{'request.course.id'}.'.description'},
         A_column       => [],          rows       => [],
         template_cells => [],          template_cells => [],
         };          };
     $self->{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);      $self->{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);
Line 1583  sub sett { Line 1578  sub sett {
         $pattern='[A-Z]';          $pattern='[A-Z]';
     }      }
     # Deal with the template row      # Deal with the template row
     foreach ($self->template_cells()) {      foreach my $col ($self->template_cells()) {
         my ($col) = ($_=~/template\_(\w)/);  
         next if ($col=~/^$pattern/);          next if ($col=~/^$pattern/);
         foreach ($self->A_column()) {          foreach my $trow ($self->rows()) {
             my ($trow)=($_!~/A(\d+)/);  
             next if (! $trow);  
             # Get the name of this cell              # Get the name of this cell
             my $lb=$col.$trow;              my $lb=$col.$trow;
             # Grab the template declaration              # Grab the template declaration
Line 1746  sub calcsheet { Line 1738  sub calcsheet {
     my $result =  $self->{'safe'}->reval('&calc();');      my $result =  $self->{'safe'}->reval('&calc();');
     %{$self->{'values'}} = %{$self->{'safe'}->varglob('sheet_values')};      %{$self->{'values'}} = %{$self->{'safe'}->varglob('sheet_values')};
 #    $self->logthis($self->get_errorlog());  #    $self->logthis($self->get_errorlog());
 #    $self->logthis('number of values = '.(keys(%{$self->{'values'}})));  
     return $result;      return $result;
 }  }
   
Line 1787  sub clear_errorlog { Line 1778  sub clear_errorlog {
 #### Spreadsheet content retrieval/setting methods #####  #### Spreadsheet content retrieval/setting methods #####
 ########################################################  ########################################################
 ##  ##
 ## contents:  either set or get the constants.  Cannot do both.  It is just too  ## constants:  either set or get the constants
 ## clunky to swing around big hashes when we may not need to.  ##
 ##  ##
 sub constants {  sub constants {
     my $self=shift;      my $self=shift;
Line 1806  sub constants { Line 1797  sub constants {
 }  }
           
 ##  ##
 ## formulas: either set or get the formulas.  Cannot do both.  It is just too  ## formulas: either set or get the formulas
 ## clunky to swing around big hashes when we may not need to.  ##
 sub formulas {  sub formulas {
     my $self=shift;      my $self=shift;
     my ($formulas) = @_;      my ($formulas) = @_;
Line 1817  sub formulas { Line 1808  sub formulas {
             $formulas = \%tmp;              $formulas = \%tmp;
         }          }
         $self->{'formulas'} = $formulas;          $self->{'formulas'} = $formulas;
         $self->{'A_column'} = [];          $self->{'rows'} = [];
         $self->{'template_cells'} = [];          $self->{'template_cells'} = [];
         return;          return;
     } else {      } else {
Line 1831  sub formulas { Line 1822  sub formulas {
 sub formulas_keys {  sub formulas_keys {
     my $self = shift;      my $self = shift;
     my @keys = keys(%{$self->{'formulas'}});      my @keys = keys(%{$self->{'formulas'}});
 #    $self->logthis('formulas keys has '.@keys.' elements');  
     return keys(%{$self->{'formulas'}});      return keys(%{$self->{'formulas'}});
 }  }
   
Line 1857  sub logthis { Line 1847  sub logthis {
     &Apache::lonnet::logthis($self->{'type'}.':'.      &Apache::lonnet::logthis($self->{'type'}.':'.
                              $self->{'uname'}.':'.$self->{'udom'}.':'.                               $self->{'uname'}.':'.$self->{'udom'}.':'.
                              $message);                               $message);
       return;
 }  }
   
 ##  ##
Line 1895  sub dump_values_to_log { Line 1886  sub dump_values_to_log {
     }      }
     $self->logthis("--------------------------------------------------------");}      $self->logthis("--------------------------------------------------------");}
   
   ##
   ## Yet another debugging function
   ##
   sub dump_hash_to_log {
       my $self= shift();
       my %tmp = @_;
       if (@_<2) {
           %tmp = %{$_[0]};
       }
       $self->logthis('---------------------------- (entries end with ":"');
       while (my ($key,$val) = each (%tmp)) {
           $self->logthis($key.' = '.$val.':');
       }
       $self->logthis('---------------------------- (entries end with ":"');
   }
   
 ################################  ################################
 ##      Helper functions      ##  ##      Helper functions      ##
 ################################  ################################
 ##  ##
 ## rebuild_stats: rebuilds the A_column and template_cells arrays  ## rebuild_stats: rebuilds the rows and template_cells arrays
 ##  ##
 sub rebuild_stats {  sub rebuild_stats {
     my $self = shift;      my $self = shift;
     $self->{'A_column'}=[];      $self->{'rows'}=[];
     $self->{'template_cells'}=[];      $self->{'template_cells'}=[];
     foreach my $cell($self->formulas_keys()) {      foreach my $cell($self->formulas_keys()) {
         push(@{$self->{'A_column'}},$cell) if $cell =~ /^A\d+/;          push(@{$self->{'rows'}},$1) if ($cell =~ /^A(\d+)/ && $1 != 0);
         push(@{$self->{'template_cells'}},$cell) if ($cell =~ /^template_/);          push(@{$self->{'template_cells'}},$1) if ($cell =~ /^template_(\w+)/);
     }      }
     # $self->logthis('rebuilt A_column '.@{$self->{'A_column'}});  
     # $self->logthis('rebuilt tempate_cells '.@{$self->{'template_cells'}});  
     return;      return;
 }  }
   
Line 1924  sub template_cells { Line 1929  sub template_cells {
 }  }
   
 ##  ##
 ## A_column returns a list of the names of cells defined in the A column  ## rows returns a list of the names of cells defined in the A column
 ##  ##
 sub A_column {  sub rows {
     my $self = shift;      my $self = shift;
     $self->rebuild_stats() if (!@{$self->{'A_column'}});      $self->rebuild_stats() if (!@{$self->{'rows'}});
     return @{$self->{'A_column'}};      return @{$self->{'rows'}};
 }  }
   
 ##  ##
Line 1955  sub rowlabels { Line 1960  sub rowlabels {
         $self->{'rowlabel'}=$rowlabel;          $self->{'rowlabel'}=$rowlabel;
         return;          return;
     } else {      } else {
         return %{$self->{'rowlabel'}} if (defined($self->{'rowlabels'}));          return %{$self->{'rowlabel'}} if (defined($self->{'rowlabel'}));
     }      }
 }  }
   
Line 2545  sub export_sheet_as_excel { Line 2550  sub export_sheet_as_excel {
             }              }
             next if ($row_is_empty);              next if ($row_is_empty);
         }          }
           $worksheet->write($rows_output,$cols_output++,$rownum);
         $worksheet->write($rows_output,$cols_output++,$label);          $worksheet->write($rows_output,$cols_output++,$label);
         if (ref($label)) {          if (ref($label)) {
             $cols_output = (scalar(@$label));              $cols_output = (scalar(@$label));
Line 2669  sub readsheet { Line 2675  sub readsheet {
     # $fn now has a value      # $fn now has a value
     $self->{'filename'} = $fn;      $self->{'filename'} = $fn;
     # see if sheet is cached      # see if sheet is cached
     my $fstring='';      if (exists($spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn})) {
     if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {          
         my %tmp = split(/___;___/,$fstring);          my %tmp = split(/___;___/,
                           $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn});
         $self->formulas(\%tmp);          $self->formulas(\%tmp);
 #        $self->logthis('readsheet found cached ');  
 #        $self->dump_formulas_to_log();  
     } else {      } else {
         # Not cached, need to read          # Not cached, need to read
         my %f=();          my %f=();
Line 2725  sub readsheet { Line 2730  sub readsheet {
         # Cache and set          # Cache and set
         $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);            $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);  
         $self->formulas(\%f);          $self->formulas(\%f);
 #        $self->logthis('readsheet loaded ');  
 #        $self->dump_formulas_to_log();  
     }      }
 }  }
   
Line 2784  sub tmpwrite { Line 2787  sub tmpwrite {
         $ENV{'user.domain'}.'_spreadsheet_'.$self->{'usymb'}.'_'.          $ENV{'user.domain'}.'_spreadsheet_'.$self->{'usymb'}.'_'.
            $self->{'filename'};             $self->{'filename'};
     $fn=~s/\W/\_/g;      $fn=~s/\W/\_/g;
     $fn=$tmpdir.$fn.'.tmp';      $fn=$Apache::lonnet::tmpdir.$fn.'.tmp';
     my $fh;      my $fh;
     if ($fh=Apache::File->new('>'.$fn)) {      if ($fh=Apache::File->new('>'.$fn)) {
         my %f = $self->formulas();          my %f = $self->formulas();
Line 2803  sub tmpread { Line 2806  sub tmpread {
            $ENV{'user.domain'}.'_spreadsheet_'.$self->{'usymb'}.'_'.             $ENV{'user.domain'}.'_spreadsheet_'.$self->{'usymb'}.'_'.
            $self->{'filename'};             $self->{'filename'};
     $fn=~s/\W/\_/g;      $fn=~s/\W/\_/g;
     $fn=$tmpdir.$fn.'.tmp';      $fn=$Apache::lonnet::tmpdir.$fn.'.tmp';
     my $fh;      my $fh;
     my %fo=();      my %fo=();
     my $countrows=0;      my $countrows=0;
Line 2950  sub updateclasssheet { Line 2953  sub updateclasssheet {
     my %existing=();      my %existing=();
     #      #
     # Now obsolete rows      # Now obsolete rows
     foreach my $cell ($self->A_column()) {      foreach my $rownum ($self->rows()) {
         $cell =~ /^A(\d+)/;          my $cell = 'A'.$rownum;
         if ($1 > $self->{'maxrow'}) {          if ($rownum > $self->{'maxrow'}) {
             $self->{'maxrow'}= $1;              $self->{'maxrow'}= $rownum;
         }          }
         $existing{$f{$cell}}=1;          $existing{$f{$cell}}=1;
         unless ((defined($currentlist{$f{$cell}})) || (!$1) ||          if (! defined($currentlist{$f{$cell}}) && ($f{$cell}=~/^(~~~|---)/)) {
                 ($f{$cell}=~/^(~~~|---)/)) {  
             $f{$cell}='!!! Obsolete';              $f{$cell}='!!! Obsolete';
             $changed=1;              $changed=1;
         }          }
Line 2988  sub get_student_rowlabels { Line 2990  sub get_student_rowlabels {
     $self->{'rowlabel'} = {};      $self->{'rowlabel'} = {};
     #      #
     my $identifier =$self->{'coursefilename'}.'_'.$stype;      my $identifier =$self->{'coursefilename'}.'_'.$stype;
     if  ($rowlabel_cache{$identifier}) {      if  (exists($rowlabel_cache{$identifier})) {
         %{$self->{'rowlabel'}}=split(/___;___/,$rowlabel_cache{$identifier});          my %tmp = split(/___;___/,$rowlabel_cache{$identifier});
           $self->rowlabels(\%tmp);
     } else {      } else {
         # Get the data and store it in the cache          # Get the data and store it in the cache
         # Tie hash          # Tie hash
Line 3041  sub get_assess_rowlabels { Line 3044  sub get_assess_rowlabels {
     $self->rowlabels({});      $self->rowlabels({});
     my $identifier =$self->{'coursefilename'}.'_'.$stype.'_'.$usymb;      my $identifier =$self->{'coursefilename'}.'_'.$stype.'_'.$usymb;
     #      #
     if  ($rowlabel_cache{$identifier}) {      if (exists($rowlabel_cache{$identifier})) {
         $self->rowlabels(split(/___;___/,$rowlabel_cache{$identifier}));          my %tmp = split('___;___',$rowlabel_cache{$identifier});
           $self->rowlabels(\%tmp);
     } else {      } else {
         # Get the data and store it in the cache          # Get the data and store it in the cache
         # Tie hash          # Tie hash
Line 3086  sub get_assess_rowlabels { Line 3090  sub get_assess_rowlabels {
         untie(%course_db);          untie(%course_db);
         # Store away the results          # Store away the results
         $self->rowlabels(\%parameter_labels);          $self->rowlabels(\%parameter_labels);
         $rowlabel_cache{$identifier}=join('___;___',$self->rowlabels());          $rowlabel_cache{$identifier}=join('___;___',%parameter_labels);
     }      }
           
 }  }
   
 sub updatestudentassesssheet {  sub updatestudentassesssheet {
Line 3104  sub updatestudentassesssheet { Line 3107  sub updatestudentassesssheet {
     $self->{'maxrow'} = 0;      $self->{'maxrow'} = 0;
     my %existing=();      my %existing=();
     # Now obsolete rows      # Now obsolete rows
     foreach my $cell ($self->A_column()) {      foreach my $rownum ($self->rows()) {
           my $cell = 'A'.$rownum;
         my $formula = $f{$cell};          my $formula = $f{$cell};
         my ($n)= ($cell =~ /A(\d+)/);          $self->{'maxrow'} = $rownum if ($rownum > $self->{'maxrow'});
         next if ($n eq '0');  
         $self->{'maxrow'} = $n if ($n > $self->{'maxrow'});  
         my ($usy,$ufn)=split(/__&&&\__/,$formula);          my ($usy,$ufn)=split(/__&&&\__/,$formula);
         $existing{$usy}=1;          $existing{$usy}=1;
         if ( ! exists($self->{'rowlabel'}->{$usy})  ||          if ( ! exists($self->{'rowlabel'}->{$usy})  ||
Line 3116  sub updatestudentassesssheet { Line 3118  sub updatestudentassesssheet {
              ($formula =~ /^(~~~|---)/) ||               ($formula =~ /^(~~~|---)/) ||
              ($formula =~ /^\s*$/)) {               ($formula =~ /^\s*$/)) {
             $f{$cell}='!!! Obsolete';              $f{$cell}='!!! Obsolete';
 #            $self->logthis('obsoleted row '.$n);  
             $changed=1;              $changed=1;
         }          }
     }      }
Line 3141  sub loadstudent{ Line 3142  sub loadstudent{
     my %formulas  = $self->formulas();      my %formulas  = $self->formulas();
     $cachedassess = $self->{'uname'}.':'.$self->{'udom'};      $cachedassess = $self->{'uname'}.':'.$self->{'udom'};
     # Get ALL the student preformance data      # Get ALL the student preformance data
     my @tmp = &Apache::lonnet::currentdump($self->{'cid'},      my @tmp = &Apache::loncoursedata::get_current_state($self->{'uname'},
                                            $self->{'udom'},                                                          $self->{'udom'},
                                            $self->{'uname'});                                                          undef,
                                                           $self->{'cid'});
     if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {      if ((scalar @tmp > 0) && ($tmp[0] !~ /^error:/)) {
         %cachedstores = @tmp;          %cachedstores = @tmp;
     }      }
     undef @tmp;      undef @tmp;
     #       # debugging code
       # $self->dump_hash_to_log(\%cachedstores);
       #
     my @assessdata=();      my @assessdata=();
     foreach my $cell ($self->A_column()) {      foreach my $row ($self->rows()) {
           my $cell = 'A'.$row;
         my $value = $formulas{$cell};          my $value = $formulas{$cell};
         if(defined($c) && ($c->aborted())) {          if(defined($c) && ($c->aborted())) {
             last;              last;
         }          }
         my ($row)=($cell=~/A(\d+)/);          next if ($value =~ /^[!~-]/);
         next if (($value =~ /^[!~-]/) || ($row==0));  
         my ($usy,$ufn)=split(/__&&&\__/,$value);          my ($usy,$ufn)=split(/__&&&\__/,$value);
         @assessdata=$self->exportsheet($self->{'uname'},          @assessdata=$self->exportsheet($self->{'uname'},
                                         $self->{'udom'},                                          $self->{'udom'},
Line 3181  sub loadstudent{ Line 3185  sub loadstudent{
     $self->constants(\%constants);      $self->constants(\%constants);
 }  }
   
 # --------------------------------------------------- Load data for one student  # --------------------------------------------------- Load Course Sheet
 #  #
 sub loadcourse {  sub loadcourse {
     my $self = shift;      my $self = shift;
Line 3191  sub loadcourse { Line 3195  sub loadcourse {
     my %formulas=$self->formulas();      my %formulas=$self->formulas();
     #      #
     my $total=0;      my $total=0;
     foreach ($self->A_column()) {      foreach ($self->rows()) {
         $total++ if ($formulas{$_} !~ /^[!~-]/);          $total++ if ($formulas{'A'.$_} !~ /^[!~-]/);
     }      }
     my $now=0;      my $now=0;
     my $since=time;      my $since=time;
Line 3202  sub loadcourse { Line 3206  sub loadcourse {
     popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+      popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
       '<h3>Spreadsheet Calculation Progress</h3>'+        '<h3>Spreadsheet Calculation Progress</h3>'+
       '<form name=popremain>'+        '<form name=popremain>'+
       '<input type=text size=45 name=remaining value=Starting></form>'+        '<input type=text size=45 name=remaining value="Processing Course Assessment Data"></form>'+
       '</body></html>');        '</body></html>');
     popwin.document.close();      popwin.document.close();
 </script>  </script>
 ENDPOP  ENDPOP
     $r->rflush();      $r->rflush();
     foreach ($self->A_column()) {      # It would be nice to load in the classlist and assessment info at this 
       # point, before attacking the student spreadsheets.
       foreach my $row ($self->rows()) {
         if(defined($c) && ($c->aborted())) {          if(defined($c) && ($c->aborted())) {
             last;              last;
         }          }
         my ($row)=(/A(\d+)/);          my $cell = 'A'.$row;
         next if (($formulas{$_}=~/^[\!\~\-]/)  || ($row==0));          next if ($formulas{$cell}=~/^[\!\~\-]/);
         my ($sname,$sdom) = split(':',$formulas{$_});          my ($sname,$sdom) = split(':',$formulas{$cell});
         my $started = time;          my $started = time;
         my @studentdata=$self->exportsheet($sname,$sdom,'studentcalc',          my @studentdata=$self->exportsheet($sname,$sdom,'studentcalc',
                                      undef,undef,$r);                                       undef,undef,$r);
Line 3222  ENDPOP Line 3228  ENDPOP
         $now++;          $now++;
         $r->print('<script>popwin.document.popremain.remaining.value="'.          $r->print('<script>popwin.document.popremain.remaining.value="'.
                   $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).                    $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
                   ' secs remaining '.(time-$started).' last";</script>');                    ' secs remaining '.(time-$started).' last student";'.
                     '</script>');
         $r->rflush();           $r->rflush(); 
         #          #
         my $index=0;          my $index=0;
Line 3342  sub loadassessment { Line 3349  sub loadassessment {
     if (tie(%parmhash,'GDBM_File',      if (tie(%parmhash,'GDBM_File',
             $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {              $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
         my %f=$self->formulas();          my %f=$self->formulas();
         foreach my $cell ($self->A_column())  {          foreach my $row ($self->rows())  {
               my $cell = 'A'.$row;
             my $formula = $self->formula($cell);              my $formula = $self->formula($cell);
             next if ($formula =~/^[\!\~\-]/);              next if ($formula =~/^[\!\~\-]/);
             if ($formula =~ /^parameter/) {              if ($formula =~ /^parameter/) {

Removed from v.1.164  
changed lines
  Added in v.1.172


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