File:  [LON-CAPA] / loncom / interface / spreadsheet / classcalc.pm
Revision 1.30: download - view: text, annotated - select for diffs
Fri Dec 3 21:33:56 2010 UTC (13 years, 4 months ago) by www
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
Work-in-progress: Grading

    1: #
    2: # $Id: classcalc.pm,v 1.30 2010/12/03 21:33:56 www Exp $
    3: #
    4: # Copyright Michigan State University Board of Trustees
    5: #
    6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    7: #
    8: # LON-CAPA is free software; you can redistribute it and/or modify
    9: # it under the terms of the GNU General Public License as published by
   10: # the Free Software Foundation; either version 2 of the License, or
   11: # (at your option) any later version.
   12: #
   13: # LON-CAPA is distributed in the hope that it will be useful,
   14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: # GNU General Public License for more details.
   17: #
   18: # You should have received a copy of the GNU General Public License
   19: # along with LON-CAPA; if not, write to the Free Software
   20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21: #
   22: # /home/httpd/html/adm/gpl.txt
   23: #
   24: # http://www.lon-capa.org/
   25: #
   26: # The LearningOnline Network with CAPA
   27: # Spreadsheet/Grades Display Handler
   28: #
   29: # POD required stuff:
   30: 
   31: =head1 NAME
   32: 
   33: classcalc
   34: 
   35: =head1 SYNOPSIS
   36: 
   37: =head1 DESCRIPTION
   38: 
   39: =over 4
   40: 
   41: =cut
   42: 
   43: ###################################################
   44: ###                 CourseSheet                 ###
   45: ###################################################
   46: package Apache::classcalc;
   47: 
   48: use strict;
   49: use warnings FATAL=>'all';
   50: no warnings 'uninitialized';
   51: use Apache::Constants qw(:common :http);
   52: use Apache::loncoursedata();
   53: use Apache::lonhtmlcommon();
   54: use Apache::Spreadsheet;
   55: use Apache::studentcalc;
   56: use Apache::lonstatistics();
   57: use HTML::Entities();
   58: use Spreadsheet::WriteExcel;
   59: use Apache::lonnet;
   60: use Time::HiRes;
   61: use Apache::lonlocal;
   62: 
   63: @Apache::classcalc::ISA = ('Apache::Spreadsheet');
   64: 
   65: ##
   66: ## Package variable
   67: ##
   68: 
   69: my @Students;
   70: 
   71: sub initialize {
   72:     &Apache::lonstatistics::clear_classlist_variables();
   73:     @Students = &Apache::lonstatistics::get_students();
   74:     return;
   75: }
   76: 
   77: sub clear_package {
   78:     undef(@Students);
   79:     &Apache::studentcalc::clear_package();
   80: }
   81: 
   82: sub html_header {
   83:     my $self = shift;
   84:     my ($toprow,$bottomrow);
   85:     &Apache::lonstatistics::clear_classlist_variables();
   86:     foreach (['Sections',&Apache::lonstatistics::SectionSelect('Section','multiple',3)],
   87:              ['Groups',&Apache::lonstatistics::GroupSelect('Group','multiple',3)], 
   88:              ['Access Status',&Apache::lonhtmlcommon::StatusOptions(undef,undef,3)],
   89:              ['Output Format',$self->output_selector()]) {
   90:         my ($name,$selector) = @{$_};
   91:         $toprow .= '<th align="center"><b>'.&mt($name).'</b></th>';
   92:         $bottomrow .= '<td>'.$selector.'</td>';
   93:     }
   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>';
   99: 
  100:     return "<p>\n<table>\n".
  101:         "<tr>".$toprow."</tr>\n".
  102:         "<tr>".$bottomrow."</tr>\n".
  103:         "</table>\n".$status."\n".
  104:         "</p>";
  105: }
  106: 
  107: sub get_title {
  108:     my $self = shift;
  109:     # Section info should be included
  110:     my @title = ($self->{'coursedesc'}, &Apache::lonlocal::locallocaltime(time) );
  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";
  118:     return $title;
  119: }
  120: 
  121: sub parent_link {
  122:     return '';
  123: }
  124: 
  125: sub outsheet_html {
  126:     my $self = shift;
  127:     my ($r) = @_;
  128:     ####################################
  129:     # Report any calculation errors    #
  130:     ####################################
  131:     $r->print($self->html_report_error());
  132:     ###################################
  133:     # Determine table structure
  134:     ###################################
  135:     my $importcolor = '#88FF88';
  136:     my $exportcolor = '#BBBBFF';
  137:     my $num_uneditable = 26;
  138:     my $num_left = 52-$num_uneditable;
  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',
  149:                                            'groups'       => 'Groups',
  150:                                            'row'          => 'Row',
  151:                                            );
  152:     my $tableheader =<<"END";
  153: <p>
  154: <table border="2">
  155: <tr>
  156:   <th colspan="2" rowspan="2"><font size="+2">$header{'course'}</font></th>
  157:   <td bgcolor="$importcolor" colspan="$num_uneditable">
  158:       <b><font size="+1">$header{'import'}</font></b></td>
  159:   <td colspan="$num_left">
  160:       <b><font size="+1">$header{'calculations'}</font></b></td>
  161: </tr><tr>
  162: END
  163:     my $label_num = 0;
  164:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
  165:         if ($label_num<$num_uneditable) { 
  166:             $tableheader.='<th bgcolor="'.$importcolor.'">';
  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
  178:     $r->print('<tr><td>'.&mt('Template').'</td><td>&nbsp;</td>'.
  179: 	      $self->html_template_row($num_uneditable,$importcolor).
  180:               "</tr>\n");
  181:     #
  182:     # Print out summary/export row
  183:     $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
  184: 	      $self->html_export_row($exportcolor)."</tr>\n");
  185:     #
  186:     # Prepare to output rows
  187:     $tableheader =<<"END";
  188: </p><p>
  189: <table border="2">
  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>
  195:   <th>$header{'groups'}</th>
  196:   <th>$header{'status'}</th>
  197: END
  198:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
  199: 	if ($label_num<$num_uneditable) { 
  200:             $tableheader.='<th bgcolor="'.$importcolor.'">';
  201:         } else {
  202:             $tableheader.='<th>';
  203:         }
  204:         $tableheader.="<b><font size=+1>$_</font></b></th>";
  205:     }
  206:     #
  207:     my $num_output = 0;
  208:     foreach my $student (@Students) {
  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>'.
  218: 		  '<td>'.$student->{'fullname'}.'</td>'.
  219: 		  '<td>'.$link.$student->{'username'}.'</a></td>'.
  220: 		  '<td>'.$student->{'domain'}  .'</td>'.
  221: 		  '<td>'.$student->{'section'} .'</td>'.
  222:                   '<td>'.$student->{'groups'} .'</td>'.
  223: 		  '<td>'.$student->{'status'}  .'</td>'.
  224: 		  $self->html_row($num_uneditable,$rownum,$exportcolor,
  225:                                   $importcolor).
  226:                   "</tr>\n");
  227:     }
  228:     $r->print("</table></p>\n");
  229:     return;
  230: }
  231: 
  232: sub excel_rows {
  233:     # writes the meat of the spreadsheet to an excel worksheet.  Called
  234:     # by Spreadsheet::outsheet_excel;
  235:     my $self = shift;
  236:     my ($connection,$worksheet,$cols_output,$rows_output,$format) = @_;
  237:     #
  238:     # Write a header row
  239:     $cols_output = 0;
  240:     foreach my $value ('Fullname','Username','Domain','Section','Group','Status','ID') {
  241:         $worksheet->write($rows_output,$cols_output++,&mt($value),$format->{'h4'});
  242:     }
  243:     $rows_output++;    
  244:     #
  245:     # Write each students row
  246:     foreach my $student (@Students) {
  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'},
  255:                            $student->{'groups'},
  256:                            $student->{'status'},
  257:                            $student->{'id'});
  258:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
  259:                                 @studentdata);
  260:     }
  261:     return;
  262: }
  263: 
  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;
  268:     my ($connection,$filehandle) = @_;
  269:     #
  270:     # Write a header row
  271:     $self->csv_output_row($filehandle,undef,
  272:                    (&mt('Fullname'),&mt('Username'),&mt('Domain'),&mt('Section'),&mt('Group'),&mt('Status'),&mt('ID')));
  273:     #
  274:     # Write each students row
  275:     foreach my $student (@Students) {
  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'},
  283:                            $student->{'groups'},
  284:                            $student->{'status'},
  285:                            $student->{'id'});
  286:         $self->csv_output_row($filehandle,$rownum,@studentdata);
  287:     }
  288:     return;
  289: }
  290: 
  291: sub output_options {
  292:     my $self = shift();
  293:     return  ({value       => 'htmlclasslist',
  294:               description => 'Student Sheet Links'},
  295:              {value       => 'source',
  296:               description => 'Show Source'},
  297:              {value       => 'html',
  298:               description => 'HTML'},
  299:              {value       => 'excel',
  300:               description => 'Excel'},
  301:              {value       => 'csv',
  302:               description => 'Comma Separated Values'},
  303: #             {value       => 'xml',
  304: #              description => 'XML'},
  305:              );
  306: }
  307: 
  308: sub outsheet_recursive_excel {
  309:     my $self = shift;
  310:     my ($r) = @_;
  311: }
  312: 
  313: sub outsheet_htmlclasslist {
  314:     my $self = shift;
  315:     my ($r) = @_;
  316:     #
  317:     # Determine if we should output expire caches links...
  318:     my $show_expire_link = 0;
  319:     if (exists($env{'user.role.dc./'.$env{'request.role.domain'}.'/'})){
  320:         $show_expire_link = 1;
  321:     }
  322:     #
  323:     if ($show_expire_link) {
  324:         $r->print('<a href="/adm/classcalc?output_format=htmlclasslist&'.
  325:                   'recalc=expireallsheets&not_first_run=1">'.
  326:                   &mt('Expire all student spreadsheets').'</a>'.$/);
  327:     }
  328:     #
  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',
  339:                                            'groups'       => 'Groups',
  340:                                            );
  341:     #
  342:     # Prepare to output rows
  343:     my $tableheader =<<"END";
  344: </p><p>
  345: <table border="2">
  346: <tr>
  347:   <th></th>
  348:   <th>$header{'student'}</th>
  349:   <th>$header{'username'}</th>
  350:   <th>$header{'domain'}</th>
  351:   <th>$header{'section'}</th>
  352:   <th>$header{'groups'}</th>
  353:   <th>$header{'status'}</th>
  354: END
  355:     if ($show_expire_link) {
  356:         $tableheader.= '<th>&nbsp;</th>';
  357:     }
  358:     $tableheader.= "</tr>\n";
  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>'.
  369:                   '<td>'.$num_output.'</td>'.
  370: 		  '<td>'.$link.$student->{'fullname'}.'</a></td>'.
  371: 		  '<td>'.$link.$student->{'username'}.'</a></td>'.
  372: 		  '<td>'.$student->{'domain'}  .'</td>'.
  373: 		  '<td>'.$student->{'section'} .'</td>'.
  374:                   '<td>'.$student->{'groups'} .'</td>'.
  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");
  384:     }
  385:     $r->print("</table></p>\n");
  386:     return;
  387: }
  388: 
  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: 
  399: sub compute {
  400:     my $self = shift;
  401:     my ($r) = @_;
  402:     my $connection = $r->connection();
  403:     if ($connection->aborted()) { $self->cleanup(); return; }
  404:     $self->initialize_safe_space();
  405:     my %c = $self->constants();
  406:     my %f = $self->formulas();
  407:     &update_status($r,'Initializing Course Structure');
  408:     &Apache::studentcalc::initialize_package();
  409:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  410:         ($r,&mt('Spreadsheet Computation Status'),
  411:          &mt('Spreadsheet Computation'), scalar(@Students),'inline',undef,
  412:          'sheet','spreadsheet_status');
  413:     &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
  414:                                           &mt('Processing first student'));
  415:     foreach my $student (@Students) {
  416:         if ($connection->aborted()) { $self->cleanup(); return; }
  417:         my $sname = $student->{'username'}.':'.$student->{'domain'};
  418: 	my $studentsheet = Apache::studentcalc->new
  419: 	    ($student->{'username'},$student->{'domain'},undef,undef,
  420: 	     $student->{'section'},$student->{'groupref'} );
  421:         if ($connection->aborted()) { $self->cleanup(); return; }
  422: 	my @exportdata = $studentsheet->export_data($r);
  423:         if ($studentsheet->badcalc()) {
  424:             $self->set_calcerror($sname.' : '.
  425:                                  $studentsheet->calcerror());
  426:         }
  427:         if ($connection->aborted()) { $self->cleanup(); return; }
  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);
  444:     &update_status($r,'Done computing student sheets');
  445:     $r->rflush();
  446:     $self->constants(\%c);
  447:     $self->formulas(\%f);
  448:     $self->calcsheet();
  449:     $self->save() if ($self->need_to_save());
  450:     &update_status($r,'Done!');
  451: }
  452: 
  453: 1;
  454: 
  455: __END__

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