Annotation of loncom/interface/spreadsheet/classcalc.pm, revision 1.17

1.1       matthew     1: #
1.17    ! matthew     2: # $Id: classcalc.pm,v 1.16 2003/11/21 21:51:28 matthew Exp $
1.1       matthew     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;
1.11      matthew    49: use warnings FATAL=>'all';
                     50: no warnings 'uninitialized';
1.1       matthew    51: use Apache::Constants qw(:common :http);
                     52: use Apache::loncoursedata();
                     53: use Apache::lonhtmlcommon();
                     54: use Apache::Spreadsheet;
                     55: use Apache::studentcalc;
1.5       matthew    56: use Apache::lonstatistics();
1.1       matthew    57: use HTML::Entities();
                     58: use Spreadsheet::WriteExcel;
                     59: use Apache::lonnet;
                     60: use Time::HiRes;
1.14      www        61: use Apache::lonlocal;
1.1       matthew    62: 
                     63: @Apache::classcalc::ISA = ('Apache::Spreadsheet');
                     64: 
1.6       matthew    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: 
1.12      matthew    77: sub clear_package {
                     78:     undef(@Students);
                     79:     &Apache::studentcalc::clear_package();
                     80: }
                     81: 
1.3       matthew    82: sub html_header {
                     83:     my $self = shift;
                     84:     my ($toprow,$bottomrow);
1.5       matthew    85:     &Apache::lonstatistics::clear_classlist_variables();
                     86:     foreach (['Sections',&Apache::lonstatistics::SectionSelect('Section','multpile',3)],
1.3       matthew    87:              ['Enrollment Status',&Apache::lonhtmlcommon::StatusOptions(undef,undef,3)],
1.16      matthew    88:              ['Output Format',$self->output_selector()]) {
1.3       matthew    89:         my ($name,$selector) = @{$_};
1.14      www        90:         $toprow .= '<th align="center"><b>'.&mt($name).'</b></th>';
1.3       matthew    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: 
1.1       matthew    99: sub get_title {
                    100:     my $self = shift;
                    101:     # Section info should be included
1.14      www       102:     my @title = ($self->{'coursedesc'}, &Apache::lonlocal::locallocaltime(time) );
1.3       matthew   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";
1.1       matthew   110:     return $title;
                    111: }
                    112: 
                    113: sub parent_link {
                    114:     return '';
                    115: }
                    116: 
                    117: sub outsheet_html {
                    118:     my $self = shift;
                    119:     my ($r) = @_;
1.15      matthew   120:     ####################################
                    121:     # Report any calculation errors    #
                    122:     ####################################
                    123:     $r->print($self->html_report_error());
1.1       matthew   124:     ###################################
                    125:     # Determine table structure
                    126:     ###################################
1.10      matthew   127:     my $importcolor = '#88FF88';
                    128:     my $exportcolor = '#BBBBFF';
1.1       matthew   129:     my $num_uneditable = 26;
                    130:     my $num_left = 52-$num_uneditable;
1.16      matthew   131:     #
                    132:     my %header=&Apache::lonlocal::texthash(
                    133:                                            'course'       => 'Course',
                    134:                                            'import'       => 'Import',
                    135:                                            'calculations' => 'Calculations',
                    136:                                            'student'      => 'Student',
                    137:                                            'status'       => 'Status',
                    138:                                            'username'     => 'Username',
                    139:                                            'domain'       => 'Domain',
                    140:                                            'section'      => 'Section',
                    141:                                            'row'          => 'Row',
                    142:                                            );
1.1       matthew   143:     my $tableheader =<<"END";
1.2       matthew   144: <p>
1.1       matthew   145: <table border="2">
                    146: <tr>
1.16      matthew   147:   <th colspan="2" rowspan="2"><font size="+2">$header{'course'}</font></th>
1.10      matthew   148:   <td bgcolor="$importcolor" colspan="$num_uneditable">
1.16      matthew   149:       <b><font size="+1">$header{'import'}</font></b></td>
1.1       matthew   150:   <td colspan="$num_left">
1.16      matthew   151:       <b><font size="+1">$header{'calculations'}</font></b></td>
1.1       matthew   152: </tr><tr>
                    153: END
                    154:     my $label_num = 0;
                    155:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    156:         if ($label_num<$num_uneditable) { 
1.10      matthew   157:             $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1       matthew   158:         } else {
                    159:             $tableheader.='<th>';
                    160:         }
                    161:         $tableheader.="<b><font size=+1>$_</font></b></th>";
                    162:         $label_num++;
                    163:     }
                    164:     $tableheader.="</tr>\n";
                    165:     #
                    166:     $r->print($tableheader);
                    167:     #
                    168:     # Print out template row
1.14      www       169:     $r->print('<tr><td>'.&mt('Template').'</td><td>&nbsp;</td>'.
1.10      matthew   170: 	      $self->html_template_row($num_uneditable,$importcolor).
                    171:               "</tr>\n");
1.1       matthew   172:     #
                    173:     # Print out summary/export row
1.14      www       174:     $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
1.10      matthew   175: 	      $self->html_export_row($exportcolor)."</tr>\n");
1.1       matthew   176:     #
                    177:     # Prepare to output rows
                    178:     $tableheader =<<"END";
1.2       matthew   179: </p><p>
1.1       matthew   180: <table border="2">
1.16      matthew   181: <tr><th>$header{'row'}</th>
                    182:   <th>$header{'student'}</th>
                    183:   <th>$header{'username'}</th>
                    184:   <th>$header{'domain'}</th>
                    185:   <th>$header{'section'}</th>
                    186:   <th>$header{'status'}</th>
1.1       matthew   187: END
                    188:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    189: 	if ($label_num<$num_uneditable) { 
1.10      matthew   190:             $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1       matthew   191:         } else {
                    192:             $tableheader.='<th>';
                    193:         }
                    194:         $tableheader.="<b><font size=+1>$_</font></b></th>";
                    195:     }
                    196:     #
                    197:     my $num_output = 0;
1.6       matthew   198:     foreach my $student (@Students) {
1.1       matthew   199: 	if ($num_output++ % 50 == 0) {
                    200: 	    $r->print("</table>\n".$tableheader);
                    201: 	}
                    202: 	my $rownum = $self->get_row_number_from_key
                    203: 	    ($student->{'username'}.':'.$student->{'domain'});
                    204:         my $link = '<a href="/adm/studentcalc?sname='.$student->{'username'}.
                    205:             '&sdomain='.$student->{'domain'}.'">';
                    206:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
                    207: 	$r->print('<tr>'.'<td>'.$rownum.'</td>'.
1.8       matthew   208: 		  '<td>'.$student->{'fullname'}.'</td>'.
                    209: 		  '<td>'.$link.$student->{'username'}.'</a></td>'.
1.1       matthew   210: 		  '<td>'.$student->{'domain'}  .'</td>'.
                    211: 		  '<td>'.$student->{'section'} .'</td>'.
                    212: 		  '<td>'.$student->{'status'}  .'</td>'.
1.10      matthew   213: 		  $self->html_row($num_uneditable,$rownum,$exportcolor,
                    214:                                   $importcolor).
                    215:                   "</tr>\n");
1.1       matthew   216:     }
1.2       matthew   217:     $r->print("</table></p>\n");
1.1       matthew   218:     return;
                    219: }
                    220: 
1.3       matthew   221: sub excel_rows {
                    222:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    223:     # by Spreadsheet::outsheet_excel;
1.1       matthew   224:     my $self = shift;
1.13      matthew   225:     my ($connection,$worksheet,$cols_output,$rows_output) = @_;
1.3       matthew   226:     #
                    227:     # Write a header row
                    228:     $cols_output = 0;
1.14      www       229:     foreach my $value ('Fullname','Username','Domain','Section','Status','ID') {
                    230:         $worksheet->write($rows_output,$cols_output++,&mt($value));
1.3       matthew   231:     }
                    232:     $rows_output++;    
                    233:     #
                    234:     # Write each students row
1.6       matthew   235:     foreach my $student (@Students) {
1.3       matthew   236:         $cols_output = 0;
                    237: 	my $rownum = $self->get_row_number_from_key
                    238: 	    ($student->{'username'}.':'.$student->{'domain'});
                    239:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
                    240:         my @studentdata = ($student->{'fullname'},
                    241:                            $student->{'username'},
                    242:                            $student->{'domain'},
                    243:                            $student->{'section'},
1.13      matthew   244:                            $student->{'status'},
                    245:                            $student->{'id'});
1.3       matthew   246:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    247:                                 @studentdata);
                    248:     }
                    249:     return;
1.1       matthew   250: }
                    251: 
1.4       matthew   252: sub csv_rows {
                    253:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    254:     # by Spreadsheet::outsheet_excel;
                    255:     my $self = shift;
1.13      matthew   256:     my ($connection,$filehandle) = @_;
1.4       matthew   257:     #
                    258:     # Write a header row
                    259:     $self->csv_output_row($filehandle,undef,
1.14      www       260:                    (&mt('Fullname'),&mt('Username'),&mt('Domain'),&mt('Section'),&mt('Status'),&mt('ID')));
1.4       matthew   261:     #
                    262:     # Write each students row
1.6       matthew   263:     foreach my $student (@Students) {
1.4       matthew   264: 	my $rownum = $self->get_row_number_from_key
                    265: 	    ($student->{'username'}.':'.$student->{'domain'});
                    266:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
                    267:         my @studentdata = ($student->{'fullname'},
                    268:                            $student->{'username'},
                    269:                            $student->{'domain'},
                    270:                            $student->{'section'},
1.13      matthew   271:                            $student->{'status'},
                    272:                            $student->{'id'});
1.4       matthew   273:         $self->csv_output_row($filehandle,$rownum,@studentdata);
                    274:     }
                    275:     return;
                    276: }
1.3       matthew   277: 
1.16      matthew   278: sub output_options {
                    279:     my $self = shift();
                    280:     return  ({value       => 'htmlclasslist',
                    281:               description => 'Student Sheet Links'},
                    282:              {value       => 'html',
                    283:               description => 'HTML'},
                    284:              {value       => 'excel',
                    285:               description => 'Excel'},
                    286:              {value       => 'csv',
                    287:               description => 'Comma Separated Values'},
                    288: );
                    289: }
                    290: 
1.1       matthew   291: sub outsheet_recursive_excel {
                    292:     my $self = shift;
                    293:     my ($r) = @_;
1.16      matthew   294: }
                    295: 
                    296: sub outsheet_htmlclasslist {
                    297:     my $self = shift;
                    298:     my ($r) = @_;
                    299:     #
                    300:     $r->print('<h3>'.
                    301:               &mt('Click on a student to be taken to their spreadsheet').
                    302:               '</h3>');
                    303:     #
                    304:     my %header=&Apache::lonlocal::texthash(
                    305:                                            'student'      => 'Student',
                    306:                                            'status'       => 'Status',
                    307:                                            'username'     => 'Username',
                    308:                                            'domain'       => 'Domain',
                    309:                                            'section'      => 'Section',
                    310:                                            );
                    311:     #
                    312:     # Prepare to output rows
                    313:     my $tableheader =<<"END";
                    314: </p><p>
                    315: <table border="2">
                    316: <tr>
1.17    ! matthew   317:   <th></th>
1.16      matthew   318:   <th>$header{'student'}</th>
                    319:   <th>$header{'username'}</th>
                    320:   <th>$header{'domain'}</th>
                    321:   <th>$header{'section'}</th>
                    322:   <th>$header{'status'}</th>
                    323: </tr>
                    324: END
                    325:     #
                    326:     my $num_output = 0;
                    327:     foreach my $student (@Students) {
                    328: 	if ($num_output++ % 50 == 0) {
                    329: 	    $r->print("</table>\n".$tableheader);
                    330: 	}
                    331:         my $link = '<a href="/adm/studentcalc?sname='.$student->{'username'}.
                    332:             '&sdomain='.$student->{'domain'}.'">';
                    333:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
                    334: 	$r->print('<tr>'.
1.17    ! matthew   335:                   '<td>'.$num_output.'</td>'.
1.16      matthew   336: 		  '<td>'.$link.$student->{'fullname'}.'</a></td>'.
                    337: 		  '<td>'.$link.$student->{'username'}.'</a></td>'.
                    338: 		  '<td>'.$student->{'domain'}  .'</td>'.
                    339: 		  '<td>'.$student->{'section'} .'</td>'.
                    340: 		  '<td>'.$student->{'status'}  .'</td>'.
                    341:                   "</tr>\n");
                    342:     }
                    343:     $r->print("</table></p>\n");
                    344:     return;
1.15      matthew   345: }
1.1       matthew   346: 
                    347: sub compute {
                    348:     my $self = shift;
                    349:     my ($r) = @_;
1.12      matthew   350:     my $connection = $r->connection();
                    351:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   352:     $self->initialize_safe_space();
                    353:     my %c = $self->constants();
                    354:     my %f = $self->formulas();
                    355:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1.14      www       356:         ($r,&mt('Spreadsheet Computation Status'),
                    357:          &mt('Spreadsheet Computation'), scalar(@Students));
1.9       matthew   358:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
1.14      www       359:                                           &mt('Processing course structure'));
1.1       matthew   360:     &Apache::studentcalc::initialize_package();
1.9       matthew   361:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
1.14      www       362:                                           &mt('Processing first student'));
1.6       matthew   363:     foreach my $student (@Students) {
1.12      matthew   364:         if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   365:         my $sname = $student->{'username'}.':'.$student->{'domain'};
                    366: 	my $studentsheet = Apache::studentcalc->new
                    367: 	    ($student->{'username'},$student->{'domain'},undef);
1.12      matthew   368:         if ($connection->aborted()) { $self->cleanup(); return; }
                    369: 	my @exportdata = $studentsheet->export_data($r);
1.15      matthew   370:         if ($studentsheet->badcalc()) {
                    371:             $self->set_calcerror($sname.' : '.
                    372:                                  $studentsheet->calcerror());
                    373:         }
1.12      matthew   374:         if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   375: 	my $rownum = $self->get_row_number_from_key($sname);
                    376:         $f{'A'.$rownum} = $sname;
                    377:         $self->{'row_source'}->{$rownum} = $sname;
                    378:         $c{'A'.$rownum} = shift(@exportdata);
                    379: 	foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
                    380:             my $cell = $_.$rownum;
                    381:             my $data = shift(@exportdata);
                    382:             if (defined($data)) {
                    383:                 $f{$cell} = 'import';
                    384:                 $c{$cell} = $data;
                    385:             }
                    386: 	}
                    387:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                    388:                                                  'last student');
                    389:     }
                    390:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
                    391:     $r->rflush();
                    392:     $self->constants(\%c);
                    393:     $self->formulas(\%f);
                    394:     $self->calcsheet();
1.7       matthew   395:     $self->save() if ($self->need_to_save());
1.1       matthew   396: }
                    397: 
                    398: 1;
                    399: 
                    400: __END__

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