File:  [LON-CAPA] / loncom / interface / spreadsheet / classcalc.pm
Revision 1.15: download - view: text, annotated - select for diffs
Mon Nov 17 19:55:41 2003 UTC (20 years, 5 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fix bug in error reporting where errors in computation of the spreadsheet
were not reported and bad cache values were stored away.
Added in Spreadsheet: &set_badcalc, &badcalc, &set_calcerror, &calcerror,
and &html_report_error.  These are used to keep track of computation errors
and to report them to the user.  The &save_export_data routines were all
modified to not store data when an error has occured.

    1: #
    2: # $Id: classcalc.pm,v 1.15 2003/11/17 19:55:41 matthew Exp $
    3: #
    4: # Copyright Michigan State University Board of Trustees
    5: #
    6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    7: #
    8: # LON-CAPA is free software; you can redistribute it and/or modify
    9: # it under the terms of the GNU General Public License as published by
   10: # the Free Software Foundation; either version 2 of the License, or
   11: # (at your option) any later version.
   12: #
   13: # LON-CAPA is distributed in the hope that it will be useful,
   14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: # GNU General Public License for more details.
   17: #
   18: # You should have received a copy of the GNU General Public License
   19: # along with LON-CAPA; if not, write to the Free Software
   20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21: #
   22: # /home/httpd/html/adm/gpl.txt
   23: #
   24: # http://www.lon-capa.org/
   25: #
   26: # The LearningOnline Network with CAPA
   27: # Spreadsheet/Grades Display Handler
   28: #
   29: # POD required stuff:
   30: 
   31: =head1 NAME
   32: 
   33: classcalc
   34: 
   35: =head1 SYNOPSIS
   36: 
   37: =head1 DESCRIPTION
   38: 
   39: =over 4
   40: 
   41: =cut
   42: 
   43: ###################################################
   44: ###                 CourseSheet                 ###
   45: ###################################################
   46: package Apache::classcalc;
   47: 
   48: use strict;
   49: use warnings FATAL=>'all';
   50: no warnings 'uninitialized';
   51: use Apache::Constants qw(:common :http);
   52: use Apache::loncoursedata();
   53: use Apache::lonhtmlcommon();
   54: use Apache::Spreadsheet;
   55: use Apache::studentcalc;
   56: use Apache::lonstatistics();
   57: use HTML::Entities();
   58: use Spreadsheet::WriteExcel;
   59: use Apache::lonnet;
   60: use Time::HiRes;
   61: use Apache::lonlocal;
   62: 
   63: @Apache::classcalc::ISA = ('Apache::Spreadsheet');
   64: 
   65: ##
   66: ## Package variable
   67: ##
   68: 
   69: my @Students;
   70: 
   71: sub initialize {
   72:     &Apache::lonstatistics::clear_classlist_variables();
   73:     @Students = &Apache::lonstatistics::get_students();
   74:     return;
   75: }
   76: 
   77: sub clear_package {
   78:     undef(@Students);
   79:     &Apache::studentcalc::clear_package();
   80: }
   81: 
   82: sub html_header {
   83:     my $self = shift;
   84:     my ($toprow,$bottomrow);
   85:     &Apache::lonstatistics::clear_classlist_variables();
   86:     foreach (['Sections',&Apache::lonstatistics::SectionSelect('Section','multpile',3)],
   87:              ['Enrollment Status',&Apache::lonhtmlcommon::StatusOptions(undef,undef,3)],
   88:              ['Output Format',&Apache::Spreadsheet::output_selector()]) {
   89:         my ($name,$selector) = @{$_};
   90:         $toprow .= '<th align="center"><b>'.&mt($name).'</b></th>';
   91:         $bottomrow .= '<td>'.$selector.'</td>';
   92:     }
   93:     return "<p>\n<table>\n".
   94:         "<tr>".$toprow."</tr>\n".
   95:         "<tr>".$bottomrow."</tr>\n".
   96:         "</table>\n</p>";
   97: }
   98: 
   99: sub get_title {
  100:     my $self = shift;
  101:     # Section info should be included
  102:     my @title = ($self->{'coursedesc'}, &Apache::lonlocal::locallocaltime(time) );
  103:     return @title;
  104: }
  105: 
  106: sub get_html_title {
  107:     my $self = shift;
  108:     my ($classcalc_title,$time) = $self->get_title();
  109:     my $title = '<h1>'.$classcalc_title."</h1>\n".'<h3>'.$time."</h3>\n";
  110:     return $title;
  111: }
  112: 
  113: sub parent_link {
  114:     return '';
  115: }
  116: 
  117: sub outsheet_html {
  118:     my $self = shift;
  119:     my ($r) = @_;
  120:     ####################################
  121:     # Report any calculation errors    #
  122:     ####################################
  123:     $r->print($self->html_report_error());
  124:     ###################################
  125:     # Determine table structure
  126:     ###################################
  127:     my $importcolor = '#88FF88';
  128:     my $exportcolor = '#BBBBFF';
  129:     my $num_uneditable = 26;
  130:     my $num_left = 52-$num_uneditable;
  131:     my %lt=&Apache::lonlocal::texthash(
  132: 				       'co' => 'Course',
  133: 				       'im' => 'Import',
  134: 				       'ca' => 'Calculations',
  135: 				       'stu' => 'Student',
  136: 				       'sta' => 'Status',
  137: 				       'usr' => 'Username',
  138: 				       'dom' => 'Domain',
  139: 				       'sec' => 'Section',
  140: 				       );
  141:     my $tableheader =<<"END";
  142: <p>
  143: <table border="2">
  144: <tr>
  145:   <th colspan="2" rowspan="2"><font size="+2">$lt{'co'}</font></th>
  146:   <td bgcolor="$importcolor" colspan="$num_uneditable">
  147:       <b><font size="+1">$lt{'im'}</font></b></td>
  148:   <td colspan="$num_left">
  149:       <b><font size="+1">$lt{'ca'}</font></b></td>
  150: </tr><tr>
  151: END
  152:     my $label_num = 0;
  153:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
  154:         if ($label_num<$num_uneditable) { 
  155:             $tableheader.='<th bgcolor="'.$importcolor.'">';
  156:         } else {
  157:             $tableheader.='<th>';
  158:         }
  159:         $tableheader.="<b><font size=+1>$_</font></b></th>";
  160:         $label_num++;
  161:     }
  162:     $tableheader.="</tr>\n";
  163:     #
  164:     $r->print($tableheader);
  165:     #
  166:     # Print out template row
  167:     $r->print('<tr><td>'.&mt('Template').'</td><td>&nbsp;</td>'.
  168: 	      $self->html_template_row($num_uneditable,$importcolor).
  169:               "</tr>\n");
  170:     #
  171:     # Print out summary/export row
  172:     $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
  173: 	      $self->html_export_row($exportcolor)."</tr>\n");
  174:     #
  175:     # Prepare to output rows
  176:     $tableheader =<<"END";
  177: </p><p>
  178: <table border="2">
  179: <tr><th>$lt{'ro'}</th>
  180:   <th>$lt{'stu'}</th><th>$lt{'usr'}</th><th>$lt{'dom'}</th>
  181:   <th>$lt{'sec'}</th><th>$lt{'sta'}</th>
  182: END
  183:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
  184: 	if ($label_num<$num_uneditable) { 
  185:             $tableheader.='<th bgcolor="'.$importcolor.'">';
  186:         } else {
  187:             $tableheader.='<th>';
  188:         }
  189:         $tableheader.="<b><font size=+1>$_</font></b></th>";
  190:     }
  191:     #
  192:     my $num_output = 0;
  193:     foreach my $student (@Students) {
  194: 	if ($num_output++ % 50 == 0) {
  195: 	    $r->print("</table>\n".$tableheader);
  196: 	}
  197: 	my $rownum = $self->get_row_number_from_key
  198: 	    ($student->{'username'}.':'.$student->{'domain'});
  199:         my $link = '<a href="/adm/studentcalc?sname='.$student->{'username'}.
  200:             '&sdomain='.$student->{'domain'}.'">';
  201:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
  202: 	$r->print('<tr>'.'<td>'.$rownum.'</td>'.
  203: 		  '<td>'.$student->{'fullname'}.'</td>'.
  204: 		  '<td>'.$link.$student->{'username'}.'</a></td>'.
  205: 		  '<td>'.$student->{'domain'}  .'</td>'.
  206: 		  '<td>'.$student->{'section'} .'</td>'.
  207: 		  '<td>'.$student->{'status'}  .'</td>'.
  208: 		  $self->html_row($num_uneditable,$rownum,$exportcolor,
  209:                                   $importcolor).
  210:                   "</tr>\n");
  211:     }
  212:     $r->print("</table></p>\n");
  213:     return;
  214: }
  215: 
  216: sub excel_rows {
  217:     # writes the meat of the spreadsheet to an excel worksheet.  Called
  218:     # by Spreadsheet::outsheet_excel;
  219:     my $self = shift;
  220:     my ($connection,$worksheet,$cols_output,$rows_output) = @_;
  221:     #
  222:     # Write a header row
  223:     $cols_output = 0;
  224:     foreach my $value ('Fullname','Username','Domain','Section','Status','ID') {
  225:         $worksheet->write($rows_output,$cols_output++,&mt($value));
  226:     }
  227:     $rows_output++;    
  228:     #
  229:     # Write each students row
  230:     foreach my $student (@Students) {
  231:         $cols_output = 0;
  232: 	my $rownum = $self->get_row_number_from_key
  233: 	    ($student->{'username'}.':'.$student->{'domain'});
  234:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
  235:         my @studentdata = ($student->{'fullname'},
  236:                            $student->{'username'},
  237:                            $student->{'domain'},
  238:                            $student->{'section'},
  239:                            $student->{'status'},
  240:                            $student->{'id'});
  241:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
  242:                                 @studentdata);
  243:     }
  244:     return;
  245: }
  246: 
  247: sub csv_rows {
  248:     # writes the meat of the spreadsheet to an excel worksheet.  Called
  249:     # by Spreadsheet::outsheet_excel;
  250:     my $self = shift;
  251:     my ($connection,$filehandle) = @_;
  252:     #
  253:     # Write a header row
  254:     $self->csv_output_row($filehandle,undef,
  255:                    (&mt('Fullname'),&mt('Username'),&mt('Domain'),&mt('Section'),&mt('Status'),&mt('ID')));
  256:     #
  257:     # Write each students row
  258:     foreach my $student (@Students) {
  259: 	my $rownum = $self->get_row_number_from_key
  260: 	    ($student->{'username'}.':'.$student->{'domain'});
  261:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
  262:         my @studentdata = ($student->{'fullname'},
  263:                            $student->{'username'},
  264:                            $student->{'domain'},
  265:                            $student->{'section'},
  266:                            $student->{'status'},
  267:                            $student->{'id'});
  268:         $self->csv_output_row($filehandle,$rownum,@studentdata);
  269:     }
  270:     return;
  271: }
  272: 
  273: sub outsheet_recursive_excel {
  274:     my $self = shift;
  275:     my ($r) = @_;
  276: }
  277: 
  278: sub compute {
  279:     my $self = shift;
  280:     my ($r) = @_;
  281:     my $connection = $r->connection();
  282:     if ($connection->aborted()) { $self->cleanup(); return; }
  283:     $self->initialize_safe_space();
  284:     my %c = $self->constants();
  285:     my %f = $self->formulas();
  286:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  287:         ($r,&mt('Spreadsheet Computation Status'),
  288:          &mt('Spreadsheet Computation'), scalar(@Students));
  289:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
  290:                                           &mt('Processing course structure'));
  291:     &Apache::studentcalc::initialize_package();
  292:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
  293:                                           &mt('Processing first student'));
  294:     foreach my $student (@Students) {
  295:         if ($connection->aborted()) { $self->cleanup(); return; }
  296:         my $sname = $student->{'username'}.':'.$student->{'domain'};
  297: 	my $studentsheet = Apache::studentcalc->new
  298: 	    ($student->{'username'},$student->{'domain'},undef);
  299:         if ($connection->aborted()) { $self->cleanup(); return; }
  300: 	my @exportdata = $studentsheet->export_data($r);
  301:         if ($studentsheet->badcalc()) {
  302:             $self->set_calcerror($sname.' : '.
  303:                                  $studentsheet->calcerror());
  304:         }
  305:         if ($connection->aborted()) { $self->cleanup(); return; }
  306: 	my $rownum = $self->get_row_number_from_key($sname);
  307:         $f{'A'.$rownum} = $sname;
  308:         $self->{'row_source'}->{$rownum} = $sname;
  309:         $c{'A'.$rownum} = shift(@exportdata);
  310: 	foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
  311:             my $cell = $_.$rownum;
  312:             my $data = shift(@exportdata);
  313:             if (defined($data)) {
  314:                 $f{$cell} = 'import';
  315:                 $c{$cell} = $data;
  316:             }
  317: 	}
  318:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  319:                                                  'last student');
  320:     }
  321:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  322:     $r->rflush();
  323:     $self->constants(\%c);
  324:     $self->formulas(\%f);
  325:     $self->calcsheet();
  326:     $self->save() if ($self->need_to_save());
  327: }
  328: 
  329: 1;
  330: 
  331: __END__

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