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

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

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