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

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

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