Diff for /loncom/interface/Attic/lonspreadsheet.pm between versions 1.132 and 1.134

version 1.132, 2002/11/04 22:35:45 version 1.134, 2002/11/06 20:00:13
Line 62  use GDBM_File; Line 62  use GDBM_File;
 use HTML::TokeParser;  use HTML::TokeParser;
 use Apache::lonhtmlcommon;  use Apache::lonhtmlcommon;
 use Apache::loncoursedata;  use Apache::loncoursedata;
   use Apache::File();
   use Spreadsheet::WriteExcel;
 #  #
 # Caches for coursewide information   # Caches for coursewide information 
 #  #
Line 1149  sub sort_indicies { Line 1151  sub sort_indicies {
 sub html_editable_cell {  sub html_editable_cell {
     my ($cell,$bgcolor) = @_;      my ($cell,$bgcolor) = @_;
     my $result;      my $result;
 #    if (defined($cell)) {  
 #        &Apache::lonnet::logthis("cell ".$cell->{'name'}.  
 #                                 " = ".$cell->{'value'}.  
 #                                 " : ".$cell->{'formula'});  
 #    }  
     my ($name,$formula,$value);      my ($name,$formula,$value);
     if (defined($cell)) {      if (defined($cell)) {
         $name    = $cell->{'name'};          $name    = $cell->{'name'};
Line 1168  sub html_editable_cell { Line 1165  sub html_editable_cell {
             $value = '<i>undefined value</i>';              $value = '<i>undefined value</i>';
         }          }
     }      }
     #      if ($value =~ /^\s*$/ ) {
           $value = '<font color="'.$bgcolor.'">#</font>';
       }
     $result .= '<a href="javascript:celledit(\''.      $result .= '<a href="javascript:celledit(\''.
         $name.'\',\''.$formula.'\');">'.$value.'</a>';          $name.'\',\''.$formula.'\');">'.$value.'</a>';
     return $result;      return $result;
Line 1231  END Line 1230  END
     # Print out template row      # Print out template row
     ####################################      ####################################
     my ($rowlabel,@rowdata) = &get_row($sheet,'-');      my ($rowlabel,@rowdata) = &get_row($sheet,'-');
     my $row_html = '<tr><td>'.&format_rowlabel($rowlabel).'</td>';      my $row_html = '<tr><td>'.&format_html_rowlabel($rowlabel).'</td>';
     my $num_cols_output = 0;      my $num_cols_output = 0;
     foreach my $cell (@rowdata) {      foreach my $cell (@rowdata) {
         if ($num_cols_output++ < $num_uneditable) {          if ($num_cols_output++ < $num_uneditable) {
Line 1250  END Line 1249  END
     ####################################      ####################################
     my ($rowlabel,@rowdata) = &get_row($sheet,'0');      my ($rowlabel,@rowdata) = &get_row($sheet,'0');
     my $rowcount = 0;      my $rowcount = 0;
     $row_html = '<tr><td>'.&format_rowlabel($rowlabel).'</td>';      $row_html = '<tr><td>'.&format_html_rowlabel($rowlabel).'</td>';
     $num_cols_output = 0;      $num_cols_output = 0;
     foreach my $cell (@rowdata) {      foreach my $cell (@rowdata) {
         if ($num_cols_output++ < 26) {          if ($num_cols_output++ < 26) {
Line 1281  END Line 1280  END
             '</font></b></td>';              '</font></b></td>';
         #          #
         if ($sheet->{'sheettype'} eq 'classcalc') {          if ($sheet->{'sheettype'} eq 'classcalc') {
             $row_html.='<td>'.&format_rowlabel($rowlabel).'</td>';              $row_html.='<td>'.&format_html_rowlabel($rowlabel).'</td>';
             # Output links for each student?              # Output links for each student?
             # Nope, that is already done for us in format_rowlabel (for now)              # Nope, that is already done for us in format_html_rowlabel (for now)
         } elsif ($sheet->{'sheettype'} eq 'studentcalc') {          } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
             $row_html.='<td>'.&format_rowlabel($rowlabel);              $row_html.='<td>'.&format_html_rowlabel($rowlabel);
             $row_html.= '<br>'.              $row_html.= '<br>'.
                 '<select name="sel_'.$rownum.'" '.                  '<select name="sel_'.$rownum.'" '.
                     'onChange="changesheet('.$rownum.')">'.                      'onChange="changesheet('.$rownum.')">'.
Line 1299  END Line 1298  END
             }              }
             $row_html.='</select></td>';              $row_html.='</select></td>';
         } elsif ($sheet->{'sheettype'} eq 'assesscalc') {          } elsif ($sheet->{'sheettype'} eq 'assesscalc') {
             $row_html.='<td>'.&format_rowlabel($rowlabel).'</td>';              $row_html.='<td>'.&format_html_rowlabel($rowlabel).'</td>';
         }          }
         #          #
         my $shown_cells = 0;          my $shown_cells = 0;
Line 1354  END Line 1353  END
   
 sub outsheet_csv   {  sub outsheet_csv   {
     my ($sheet,$r) = @_;      my ($sheet,$r) = @_;
       my $csvdata = '';
       my @Values;
       ####################################
       # Prepare to output rows
       ####################################
       my @Rows = &sort_indicies($sheet);
       #
       # Loop through the rows and output them one at a time
       my $rows_output=0;
       foreach my $rownum (@Rows) {
           my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
           push (@Values,&format_csv_rowlabel($rowlabel));
           foreach my $cell (@rowdata) {
               push (@Values,'"'.$cell->{'value'}.'"');
           }
           $csvdata.= join(',',@Values)."\n";
           @Values = ();
       }
       #
       # Write the CSV data to a file and serve up a link
       #
       my $filename = '/prtspool/'.
           $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
           time.'_'.rand(1000000000).'.csv';
       my $file;
       unless ($file = Apache::File->new('>'.'/home/httpd'.$filename)) {
           $r->log_error("Couldn't open $filename for output $!");
           $r->print("Problems occured in writing the csv file.  ".
                     "This error has been logged.  ".
                     "Please alert your LON-CAPA administrator.");
           $r->print("<pre>\n".$csvdata."</pre>\n");
           return 0;
       }
       print $file $csvdata;
       close($file);
       $r->print('<br /><br />'.
                 '<a href="'.$filename.'">Your CSV spreadsheet.</a>'."\n");
       #
       return 1;
 }  }
   
 sub outsheet_excel {  sub outsheet_excel {
     my ($sheet,$r) = @_;      my ($sheet,$r) = @_;
       my $filename = '/prtspool/'.
           $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
           time.'_'.rand(1000000000).'.xls';
       &Apache::lonnet::logthis("spreadsheet:filename = ".$filename);
       my $workbook  = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
       if (! defined($workbook)) {
           $r->log_error("Error creating excel spreadsheet $filename: $!");
           $r->print("Problems creating new Excel file.  ".
                     "This error has been logged.  ".
                     "Please alert your LON-CAPA administrator");
           return 0;
       }
       #
       # The spreadsheet stores temporary data in files, then put them
       # together.  If needed we should be able to disable this (memory only).
       # The temporary directory must be specified before calling 'addworksheet'.
       # File::Temp is used to determine the temporary directory.
       $workbook->set_tempdir('/home/httpd/perl/tmp');
       #
       # Determine the name to give the worksheet
       my $sheetname;
       if ($sheet->{'sheettype'} eq 'classcalc') {
           $sheetname = 'Main';
       } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
           $sheetname = $sheet->{'uname'}.'@'.$sheet->{'udom'};
       } elsif ($sheet->{'sheettype'} eq 'assesscalc') {
           $sheetname = $sheet->{'uname'}.'@'.$sheet->{'udom'}.' assessment';
       }
       my $worksheet = $workbook->addworksheet($sheetname);
       ####################################
       # Prepare to output rows
       ####################################
       my @Rows = &sort_indicies($sheet);
       #
       # Loop through the rows and output them one at a time
       my $rows_output=0;
       foreach my $rownum (@Rows) {
           my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
           my $cols_output = 0;
           my $label = &format_excel_rowlabel($rowlabel);
           $worksheet->write($rows_output,$cols_output++,$label);
           if (ref($label)) {
               $cols_output = (scalar(@$label));
           }
           foreach my $cell (@rowdata) {
               $worksheet->write($rows_output,$cols_output++,
                                 $cell->{'value'});
           }
           $rows_output++;
       }
       #
       $workbook->close();
       # Okay, the spreadsheet is taken care of, so give the user a link.
       $r->print('<br /><br />'.
                 '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
       return 1;
 }  }
   
 sub outsheet_xml   {  sub outsheet_xml   {
Line 1366  sub outsheet_xml   { Line 1460  sub outsheet_xml   {
   
 sub outsheet {  sub outsheet {
     my ($r,$sheet)=@_;      my ($r,$sheet)=@_;
     &outsheet_html($sheet,$r);      if (! exists($ENV{'form.output'})) {
 #    if (exists($ENV{'form.csv'})) {          $ENV{'form.output'} = 'HTML';
 #        &outsheet_csv($sheet,$r);      }
 #    } elsif (exists($ENV{'form.excel'})) {      if (lc($ENV{'form.output'}) eq 'csv') {
 #        &outsheet_excel($sheet,$r);          &outsheet_csv($sheet,$r);
 #    } elsif (exists($ENV{'form.xml'})) {      } elsif (lc($ENV{'form.output'}) eq 'excel') {
           &outsheet_excel($sheet,$r);
   #    } elsif (lc($ENV{'form.output'}) eq 'xml' ) {
 #        &outsheet_xml($sheet,$r);  #        &outsheet_xml($sheet,$r);
 #    } else {      } else {
 #        &outsheet_html($sheet,$r);          &outsheet_html($sheet,$r);
 #    }      }
 }  }
   
 ########################################################################  ########################################################################
Line 1515  sub makenewsheet { Line 1611  sub makenewsheet {
     $sheet->{'cnum'}  = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};      $sheet->{'cnum'}  = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
     $sheet->{'cdom'}  = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};      $sheet->{'cdom'}  = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
     $sheet->{'chome'} = $ENV{'course.'.$ENV{'request.course.id'}.'.home'};      $sheet->{'chome'} = $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
       $sheet->{'coursedesc'} = $ENV{'course.'.$ENV{'request.course.id'}.
                                         'description'};
     $sheet->{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);      $sheet->{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);
     #      #
     #      #
Line 1710  sub parmval { Line 1808  sub parmval {
     return &Apache::lonnet::metadata($fn,$rwhat.'.default');      return &Apache::lonnet::metadata($fn,$rwhat.'.default');
 }  }
   
 sub format_rowlabel {  
   ##################################################################
   ##                  Row label formatting routines               ##
   ##################################################################
   sub format_html_rowlabel {
       my $rowlabel = shift;
       return '' if ($rowlabel eq '');
       my ($type,$labeldata) = split(':',$rowlabel,2);
       my $result = '';
       if ($type eq 'symb') {
           my ($symb,$uname,$udom,$title) = split(':',$labeldata);
           $symb = &Apache::lonnet::unescape($symb);
           $result = '<a href="/adm/assesscalc?usymb='.$symb.
               '&uname='.$uname.'&udom='.$udom.'">'.$title.'</a>';
       } elsif ($type eq 'student') {
           my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
           $result ='<a href="/adm/studentcalc?uname='.$sname.
               '&udom='.$sdom.'">';
           $result.=$section.'&nbsp;'.$id."&nbsp;".$fullname.'</a>';
       } elsif ($type eq 'parameter') {
           $result = $labeldata;
       } else {
           $result = '<b><font size=+1>'.$rowlabel.'</font></b>';
       }
       return $result;
   }
   
   sub format_csv_rowlabel {
     my $rowlabel = shift;      my $rowlabel = shift;
     return '' if ($rowlabel eq '');      return '' if ($rowlabel eq '');
     my ($type,$labeldata) = split(':',$rowlabel,2);      my ($type,$labeldata) = split(':',$rowlabel,2);
Line 1718  sub format_rowlabel { Line 1843  sub format_rowlabel {
     if ($type eq 'symb') {      if ($type eq 'symb') {
         my ($symb,$uname,$udom,$title) = split(':',$labeldata);          my ($symb,$uname,$udom,$title) = split(':',$labeldata);
         $symb = &Apache::lonnet::unescape($symb);          $symb = &Apache::lonnet::unescape($symb);
         if ($ENV{'form.showcsv'}) {          $result = $title;
             $result = $title;  
         } else {  
             $result = '<a href="/adm/assesscalc?usymb='.$symb.  
                 '&uname='.$uname.'&udom='.$udom.'">'.$title.'</a>';  
         }  
     } elsif ($type eq 'student') {      } elsif ($type eq 'student') {
         my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);          my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
         if ($ENV{'form.showcsv'}) {          $result = join('","',($sname,$sdom,$fullname,$section,$id));
             $result = '"'.  
                 join('","',($sname,$sdom,$fullname,$section,$id).'"');  
         } else {  
             $result ='<a href="/adm/studentcalc?uname='.$sname.  
                 '&udom='.$sdom.'">';  
             $result.=$section.'&nbsp;'.$id."&nbsp;".$fullname.'</a>';  
         }  
     } elsif ($type eq 'parameter') {      } elsif ($type eq 'parameter') {
         if ($ENV{'form.showcsv'}) {          $labeldata =~ s/<br>/ /g;
             $labeldata =~ s/<br>/ /g;  
         }  
         $result = $labeldata;          $result = $labeldata;
     } else {      } else {
         if ($ENV{'form.showcsv'}) {          $result = $rowlabel;
             $result = $rowlabel;      }
         } else {      return '"'.$result.'"';
             $result = '<b><font size=+1>'.$rowlabel.'</font></b>';  }
         }  
   sub format_excel_rowlabel {
       my $rowlabel = shift;
       return '' if ($rowlabel eq '');
       my ($type,$labeldata) = split(':',$rowlabel,2);
       my $result = '';
       if ($type eq 'symb') {
           my ($symb,$uname,$udom,$title) = split(':',$labeldata);
           $symb = &Apache::lonnet::unescape($symb);
           $result = $title;
       } elsif ($type eq 'student') {
           my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
           $section = '' if (! defined($section));
           $id      = '' if (! defined($id));
           my @Data = ($sname,$sdom,$fullname,$section,$id);
           $result = \@Data;
       } elsif ($type eq 'parameter') {
           $labeldata =~ s/<br>/ /g;
           $result = $labeldata;
       } else {
           $result = $rowlabel;
     }      }
     return $result;      return $result;
 }  }
Line 2451  sub handler { Line 2582  sub handler {
     if (! exists($ENV{'form.Status'})) {      if (! exists($ENV{'form.Status'})) {
         $ENV{'form.Status'} = 'Active';          $ENV{'form.Status'} = 'Active';
     }      }
       if (! exists($ENV{'form.output'})) {
           $ENV{'form.output'} = 'HTML';
       }
     # Check this server      # Check this server
     my $loaderror=&Apache::lonnet::overloaderror($r);      my $loaderror=&Apache::lonnet::overloaderror($r);
     if ($loaderror) { return $loaderror; }      if ($loaderror) { return $loaderror; }
Line 2704  ENDSCRIPT Line 2838  ENDSCRIPT
     $r->print('>');      $r->print('>');
     #      #
     # CSV format checkbox (classcalc sheets only)      # CSV format checkbox (classcalc sheets only)
     $r->print(' Output CSV format: <input type="checkbox" '.      $r->print(' Output as <select name="output" size="1" onClick="submit()">'.
               'name="showcsv" onClick="submit()"');                "\n");
     $r->print(' checked') if ($ENV{'form.showcsv'});      foreach my $mode (qw/HTML CSV EXCEL/) {
     $r->print('>');          $r->print('<option value="'.$mode.'"');
           if ($ENV{'form.output'} eq $mode) {
               $r->print(' selected ');
           } 
           $r->print('>'.$mode.'</option>'."\n");
       }
       $r->print("</select>\n");
       #
     if ($sheet->{'sheettype'} eq 'classcalc') {      if ($sheet->{'sheettype'} eq 'classcalc') {
         $r->print('&nbsp;Student Status: '.          $r->print('&nbsp;Student Status: '.
                   &Apache::lonhtmlcommon::StatusOptions                    &Apache::lonhtmlcommon::StatusOptions
Line 2715  ENDSCRIPT Line 2856  ENDSCRIPT
     }      }
     #      #
     # Buttons to insert rows      # Buttons to insert rows
     $r->print(<<ENDINSERTBUTTONS);  #    $r->print(<<ENDINSERTBUTTONS);
 <br>  #<br>
 <input type='button' onClick='insertrow("top");'   #<input type='button' onClick='insertrow("top");' 
 value='Insert Row Top'>  #value='Insert Row Top'>
 <input type='button' onClick='insertrow("bottom");'   #<input type='button' onClick='insertrow("bottom");' 
 value='Insert Row Bottom'><br>  #value='Insert Row Bottom'><br>
 ENDINSERTBUTTONS  #ENDINSERTBUTTONS
     # Print out sheet      # Print out sheet
     &outsheet($r,$sheet);      &outsheet($r,$sheet);
     $r->print('</form></body></html>');      $r->print('</form></body></html>');

Removed from v.1.132  
changed lines
  Added in v.1.134


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