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

1.1       matthew     1: #
1.21    ! matthew     2: # $Id: classcalc.pm,v 1.20 2004/06/04 21:43:36 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:     }
1.21    ! matthew    93:     my $status .= '<nobr>'.&mt('Status: [_1]',
        !            94:                             '<input type="text" '.
        !            95:                             'name="spreadsheet_status" size="60" value="" />'
        !            96:                             ).'</nobr>';
        !            97: 
1.3       matthew    98:     return "<p>\n<table>\n".
                     99:         "<tr>".$toprow."</tr>\n".
                    100:         "<tr>".$bottomrow."</tr>\n".
1.21    ! matthew   101:         "</table>\n".$status."\n".
        !           102:         "</p>";
1.3       matthew   103: }
                    104: 
1.1       matthew   105: sub get_title {
                    106:     my $self = shift;
                    107:     # Section info should be included
1.14      www       108:     my @title = ($self->{'coursedesc'}, &Apache::lonlocal::locallocaltime(time) );
1.3       matthew   109:     return @title;
                    110: }
                    111: 
                    112: sub get_html_title {
                    113:     my $self = shift;
                    114:     my ($classcalc_title,$time) = $self->get_title();
                    115:     my $title = '<h1>'.$classcalc_title."</h1>\n".'<h3>'.$time."</h3>\n";
1.1       matthew   116:     return $title;
                    117: }
                    118: 
                    119: sub parent_link {
                    120:     return '';
                    121: }
                    122: 
                    123: sub outsheet_html {
                    124:     my $self = shift;
                    125:     my ($r) = @_;
1.15      matthew   126:     ####################################
                    127:     # Report any calculation errors    #
                    128:     ####################################
                    129:     $r->print($self->html_report_error());
1.1       matthew   130:     ###################################
                    131:     # Determine table structure
                    132:     ###################################
1.10      matthew   133:     my $importcolor = '#88FF88';
                    134:     my $exportcolor = '#BBBBFF';
1.1       matthew   135:     my $num_uneditable = 26;
                    136:     my $num_left = 52-$num_uneditable;
1.16      matthew   137:     #
                    138:     my %header=&Apache::lonlocal::texthash(
                    139:                                            'course'       => 'Course',
                    140:                                            'import'       => 'Import',
                    141:                                            'calculations' => 'Calculations',
                    142:                                            'student'      => 'Student',
                    143:                                            'status'       => 'Status',
                    144:                                            'username'     => 'Username',
                    145:                                            'domain'       => 'Domain',
                    146:                                            'section'      => 'Section',
                    147:                                            'row'          => 'Row',
                    148:                                            );
1.1       matthew   149:     my $tableheader =<<"END";
1.2       matthew   150: <p>
1.1       matthew   151: <table border="2">
                    152: <tr>
1.16      matthew   153:   <th colspan="2" rowspan="2"><font size="+2">$header{'course'}</font></th>
1.10      matthew   154:   <td bgcolor="$importcolor" colspan="$num_uneditable">
1.16      matthew   155:       <b><font size="+1">$header{'import'}</font></b></td>
1.1       matthew   156:   <td colspan="$num_left">
1.16      matthew   157:       <b><font size="+1">$header{'calculations'}</font></b></td>
1.1       matthew   158: </tr><tr>
                    159: END
                    160:     my $label_num = 0;
                    161:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    162:         if ($label_num<$num_uneditable) { 
1.10      matthew   163:             $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1       matthew   164:         } else {
                    165:             $tableheader.='<th>';
                    166:         }
                    167:         $tableheader.="<b><font size=+1>$_</font></b></th>";
                    168:         $label_num++;
                    169:     }
                    170:     $tableheader.="</tr>\n";
                    171:     #
                    172:     $r->print($tableheader);
                    173:     #
                    174:     # Print out template row
1.14      www       175:     $r->print('<tr><td>'.&mt('Template').'</td><td>&nbsp;</td>'.
1.10      matthew   176: 	      $self->html_template_row($num_uneditable,$importcolor).
                    177:               "</tr>\n");
1.1       matthew   178:     #
                    179:     # Print out summary/export row
1.14      www       180:     $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
1.10      matthew   181: 	      $self->html_export_row($exportcolor)."</tr>\n");
1.1       matthew   182:     #
                    183:     # Prepare to output rows
                    184:     $tableheader =<<"END";
1.2       matthew   185: </p><p>
1.1       matthew   186: <table border="2">
1.16      matthew   187: <tr><th>$header{'row'}</th>
                    188:   <th>$header{'student'}</th>
                    189:   <th>$header{'username'}</th>
                    190:   <th>$header{'domain'}</th>
                    191:   <th>$header{'section'}</th>
                    192:   <th>$header{'status'}</th>
1.1       matthew   193: END
                    194:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
                    195: 	if ($label_num<$num_uneditable) { 
1.10      matthew   196:             $tableheader.='<th bgcolor="'.$importcolor.'">';
1.1       matthew   197:         } else {
                    198:             $tableheader.='<th>';
                    199:         }
                    200:         $tableheader.="<b><font size=+1>$_</font></b></th>";
                    201:     }
                    202:     #
                    203:     my $num_output = 0;
1.6       matthew   204:     foreach my $student (@Students) {
1.1       matthew   205: 	if ($num_output++ % 50 == 0) {
                    206: 	    $r->print("</table>\n".$tableheader);
                    207: 	}
                    208: 	my $rownum = $self->get_row_number_from_key
                    209: 	    ($student->{'username'}.':'.$student->{'domain'});
                    210:         my $link = '<a href="/adm/studentcalc?sname='.$student->{'username'}.
                    211:             '&sdomain='.$student->{'domain'}.'">';
                    212:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
                    213: 	$r->print('<tr>'.'<td>'.$rownum.'</td>'.
1.8       matthew   214: 		  '<td>'.$student->{'fullname'}.'</td>'.
                    215: 		  '<td>'.$link.$student->{'username'}.'</a></td>'.
1.1       matthew   216: 		  '<td>'.$student->{'domain'}  .'</td>'.
                    217: 		  '<td>'.$student->{'section'} .'</td>'.
                    218: 		  '<td>'.$student->{'status'}  .'</td>'.
1.10      matthew   219: 		  $self->html_row($num_uneditable,$rownum,$exportcolor,
                    220:                                   $importcolor).
                    221:                   "</tr>\n");
1.1       matthew   222:     }
1.2       matthew   223:     $r->print("</table></p>\n");
1.1       matthew   224:     return;
                    225: }
                    226: 
1.3       matthew   227: sub excel_rows {
                    228:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    229:     # by Spreadsheet::outsheet_excel;
1.1       matthew   230:     my $self = shift;
1.13      matthew   231:     my ($connection,$worksheet,$cols_output,$rows_output) = @_;
1.3       matthew   232:     #
                    233:     # Write a header row
                    234:     $cols_output = 0;
1.14      www       235:     foreach my $value ('Fullname','Username','Domain','Section','Status','ID') {
                    236:         $worksheet->write($rows_output,$cols_output++,&mt($value));
1.3       matthew   237:     }
                    238:     $rows_output++;    
                    239:     #
                    240:     # Write each students row
1.6       matthew   241:     foreach my $student (@Students) {
1.3       matthew   242:         $cols_output = 0;
                    243: 	my $rownum = $self->get_row_number_from_key
                    244: 	    ($student->{'username'}.':'.$student->{'domain'});
                    245:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
                    246:         my @studentdata = ($student->{'fullname'},
                    247:                            $student->{'username'},
                    248:                            $student->{'domain'},
                    249:                            $student->{'section'},
1.13      matthew   250:                            $student->{'status'},
                    251:                            $student->{'id'});
1.3       matthew   252:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
                    253:                                 @studentdata);
                    254:     }
                    255:     return;
1.1       matthew   256: }
                    257: 
1.4       matthew   258: sub csv_rows {
                    259:     # writes the meat of the spreadsheet to an excel worksheet.  Called
                    260:     # by Spreadsheet::outsheet_excel;
                    261:     my $self = shift;
1.13      matthew   262:     my ($connection,$filehandle) = @_;
1.4       matthew   263:     #
                    264:     # Write a header row
                    265:     $self->csv_output_row($filehandle,undef,
1.14      www       266:                    (&mt('Fullname'),&mt('Username'),&mt('Domain'),&mt('Section'),&mt('Status'),&mt('ID')));
1.4       matthew   267:     #
                    268:     # Write each students row
1.6       matthew   269:     foreach my $student (@Students) {
1.4       matthew   270: 	my $rownum = $self->get_row_number_from_key
                    271: 	    ($student->{'username'}.':'.$student->{'domain'});
                    272:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
                    273:         my @studentdata = ($student->{'fullname'},
                    274:                            $student->{'username'},
                    275:                            $student->{'domain'},
                    276:                            $student->{'section'},
1.13      matthew   277:                            $student->{'status'},
                    278:                            $student->{'id'});
1.4       matthew   279:         $self->csv_output_row($filehandle,$rownum,@studentdata);
                    280:     }
                    281:     return;
                    282: }
1.3       matthew   283: 
1.16      matthew   284: sub output_options {
                    285:     my $self = shift();
                    286:     return  ({value       => 'htmlclasslist',
                    287:               description => 'Student Sheet Links'},
                    288:              {value       => 'html',
                    289:               description => 'HTML'},
                    290:              {value       => 'excel',
                    291:               description => 'Excel'},
                    292:              {value       => 'csv',
                    293:               description => 'Comma Separated Values'},
1.18      matthew   294: #             {value       => 'xml',
                    295: #              description => 'XML'},
                    296:              );
1.16      matthew   297: }
                    298: 
1.1       matthew   299: sub outsheet_recursive_excel {
                    300:     my $self = shift;
                    301:     my ($r) = @_;
1.16      matthew   302: }
                    303: 
                    304: sub outsheet_htmlclasslist {
                    305:     my $self = shift;
                    306:     my ($r) = @_;
                    307:     #
1.19      matthew   308:     # Determine if we should output expire caches links...
                    309:     my $show_expire_link = 0;
                    310:     if (exists($ENV{'user.role.dc./'.$ENV{'request.role.domain'}.'/'})){
                    311:         $show_expire_link = 1;
                    312:     }
                    313:     #
                    314:     if ($show_expire_link) {
                    315:         $r->print('<a href="/adm/classcalc?output_format=htmlclasslist&'.
                    316:                   'recalc=ilovewastingtime&not_first_run=1">'.
                    317:                   &mt('Expire all student spreadsheets').'</a>'.$/);
                    318:     }
                    319:     #
1.16      matthew   320:     $r->print('<h3>'.
                    321:               &mt('Click on a student to be taken to their spreadsheet').
                    322:               '</h3>');
                    323:     #
                    324:     my %header=&Apache::lonlocal::texthash(
                    325:                                            'student'      => 'Student',
                    326:                                            'status'       => 'Status',
                    327:                                            'username'     => 'Username',
                    328:                                            'domain'       => 'Domain',
                    329:                                            'section'      => 'Section',
                    330:                                            );
                    331:     #
                    332:     # Prepare to output rows
                    333:     my $tableheader =<<"END";
                    334: </p><p>
                    335: <table border="2">
                    336: <tr>
1.17      matthew   337:   <th></th>
1.16      matthew   338:   <th>$header{'student'}</th>
                    339:   <th>$header{'username'}</th>
                    340:   <th>$header{'domain'}</th>
                    341:   <th>$header{'section'}</th>
                    342:   <th>$header{'status'}</th>
                    343: END
1.19      matthew   344:     if ($show_expire_link) {
                    345:         $tableheader.= '<th>&nbsp;</th>';
                    346:     }
                    347:     $tableheader.= "</tr>\n";
1.16      matthew   348:     #
                    349:     my $num_output = 0;
                    350:     foreach my $student (@Students) {
                    351: 	if ($num_output++ % 50 == 0) {
                    352: 	    $r->print("</table>\n".$tableheader);
                    353: 	}
                    354:         my $link = '<a href="/adm/studentcalc?sname='.$student->{'username'}.
                    355:             '&sdomain='.$student->{'domain'}.'">';
                    356:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
                    357: 	$r->print('<tr>'.
1.17      matthew   358:                   '<td>'.$num_output.'</td>'.
1.16      matthew   359: 		  '<td>'.$link.$student->{'fullname'}.'</a></td>'.
                    360: 		  '<td>'.$link.$student->{'username'}.'</a></td>'.
                    361: 		  '<td>'.$student->{'domain'}  .'</td>'.
                    362: 		  '<td>'.$student->{'section'} .'</td>'.
1.19      matthew   363: 		  '<td>'.$student->{'status'}  .'</td>');
                    364:         if ($show_expire_link) {
                    365:             $r->print('<td>'.
                    366:                       '<a href="/adm/classcalc?recalc=student:'.
                    367:                       $student->{'username'}.':'.$student->{'domain'}.'&'.
                    368:                       'output_format=htmlclasslist&'.
                    369:                       'not_first_run=1'.'">Expire Record</a>'.$/);
                    370:         }
                    371:         $r->print("</tr>\n");
1.16      matthew   372:     }
                    373:     $r->print("</table></p>\n");
                    374:     return;
1.15      matthew   375: }
1.1       matthew   376: 
1.21    ! matthew   377: sub update_status {
        !           378:     my ($r,$message) = @_;
        !           379:     $r->print('<script>'.
        !           380:               'window.document.sheet.spreadsheet_status.value="'.
        !           381:               $message.
        !           382:               '";</script>');
        !           383:     $r->rflush();
        !           384:     return;
        !           385: }
        !           386: 
1.1       matthew   387: sub compute {
                    388:     my $self = shift;
                    389:     my ($r) = @_;
1.12      matthew   390:     my $connection = $r->connection();
                    391:     if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   392:     $self->initialize_safe_space();
                    393:     my %c = $self->constants();
                    394:     my %f = $self->formulas();
1.21    ! matthew   395:     &update_status($r,'Initializing Course Structure');
1.20      matthew   396:     &Apache::studentcalc::initialize_package();
1.1       matthew   397:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1.14      www       398:         ($r,&mt('Spreadsheet Computation Status'),
1.21    ! matthew   399:          &mt('Spreadsheet Computation'), scalar(@Students),'inline',undef,
        !           400:          'sheet','spreadsheet_status');
1.9       matthew   401:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
1.14      www       402:                                           &mt('Processing first student'));
1.6       matthew   403:     foreach my $student (@Students) {
1.12      matthew   404:         if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   405:         my $sname = $student->{'username'}.':'.$student->{'domain'};
                    406: 	my $studentsheet = Apache::studentcalc->new
                    407: 	    ($student->{'username'},$student->{'domain'},undef);
1.12      matthew   408:         if ($connection->aborted()) { $self->cleanup(); return; }
                    409: 	my @exportdata = $studentsheet->export_data($r);
1.15      matthew   410:         if ($studentsheet->badcalc()) {
                    411:             $self->set_calcerror($sname.' : '.
                    412:                                  $studentsheet->calcerror());
                    413:         }
1.12      matthew   414:         if ($connection->aborted()) { $self->cleanup(); return; }
1.1       matthew   415: 	my $rownum = $self->get_row_number_from_key($sname);
                    416:         $f{'A'.$rownum} = $sname;
                    417:         $self->{'row_source'}->{$rownum} = $sname;
                    418:         $c{'A'.$rownum} = shift(@exportdata);
                    419: 	foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
                    420:             my $cell = $_.$rownum;
                    421:             my $data = shift(@exportdata);
                    422:             if (defined($data)) {
                    423:                 $f{$cell} = 'import';
                    424:                 $c{$cell} = $data;
                    425:             }
                    426: 	}
                    427:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                    428:                                                  'last student');
                    429:     }
                    430:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.21    ! matthew   431:     &update_status($r,'Done computing student sheets');
1.1       matthew   432:     $r->rflush();
                    433:     $self->constants(\%c);
                    434:     $self->formulas(\%f);
                    435:     $self->calcsheet();
1.7       matthew   436:     $self->save() if ($self->need_to_save());
1.21    ! matthew   437:     &update_status($r,'Done!');
1.1       matthew   438: }
                    439: 
                    440: 1;
                    441: 
                    442: __END__

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