File:  [LON-CAPA] / loncom / interface / spreadsheet / classcalc.pm
Revision 1.3: download - view: text, annotated - select for diffs
Fri May 23 19:36:04 2003 UTC (20 years, 11 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added output selector and implemented excel output.

Moved $spreadsheet->display() to Spreadsheet.pm instead of having it reside
    three times in the descendents of the spreadsheet object.
Moved $spreadsheet->outsheet_excel() to Spreadsheet.pm for the same reason.
Created Spreadsheet::output_selector, which allows the user to select the
    output mode they desire.
Created $spreadsheet->excel_output_row which takes care of writing a row
    to an excel worksheet.
classcalc.pm, studentcalc.pm, and assesscalc.pm each had the same work done
on them:
  Created &excel_rows(), which writes the body of the spreadsheets to
     an excel file.
  Rewrote &get_title to return an array of title lines.
  Created &get_html_title to return an HTML version of the results of
     &get_title.

classcalc.pm and Spreadsheet.pm each have the following change:
    Created &html_header to return appropriate HTML for the spreadsheet.

    1: #
    2: # $Id: classcalc.pm,v 1.3 2003/05/23 19:36:04 matthew 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 Apache::Constants qw(:common :http);
   50: use Apache::loncoursedata();
   51: use Apache::lonhtmlcommon();
   52: use Apache::Spreadsheet;
   53: use Apache::studentcalc;
   54: use HTML::Entities();
   55: use Spreadsheet::WriteExcel;
   56: use Apache::lonnet;
   57: use Time::HiRes;
   58: 
   59: @Apache::classcalc::ISA = ('Apache::Spreadsheet');
   60: 
   61: sub html_header {
   62:     my $self = shift;
   63:     my ($toprow,$bottomrow);
   64:     foreach (['Sections','Section selector goes here'],
   65:              ['Enrollment Status',&Apache::lonhtmlcommon::StatusOptions(undef,undef,3)],
   66:              ['Output Format',&Apache::Spreadsheet::output_selector()]) {
   67:         my ($name,$selector) = @{$_};
   68:         $toprow .= '<th align="center"><b>'.$name.'</b></th>';
   69:         $bottomrow .= '<td>'.$selector.'</td>';
   70:     }
   71:     return "<p>\n<table>\n".
   72:         "<tr>".$toprow."</tr>\n".
   73:         "<tr>".$bottomrow."</tr>\n".
   74:         "</table>\n</p>";
   75: }
   76: 
   77: sub get_classlist {
   78:     my $self = shift;
   79:     # Retrieve the classlist
   80:     my @Students = ();
   81:     my ($classlist,$field_names) = &Apache::loncoursedata::get_classlist
   82:         ($self->{'cid'},$self->{'cdom'},$self->{'cnum'});
   83:     while (my ($student,$student_data) = each (%$classlist)) {
   84:         my $studenthash = ();
   85:         for (my $i=0; $i< scalar(@$field_names);$i++) {
   86:             my $field = $field_names->[$i];
   87:             $studenthash->{$field}=$student_data->[$i];
   88:         }
   89:         # This is where we can skip students because they are in 
   90:         # the wrong section, have expired or pending roles, whatever...
   91:         push (@Students,$studenthash);
   92:     }
   93:     my @SortedStudents = sort { $a->{'fullname'} cmp $b->{'fullname'} } @Students;
   94:     return @SortedStudents;
   95: }
   96: 
   97: sub get_title {
   98:     my $self = shift;
   99:     # Section info should be included
  100:     my @title = ($self->{'coursedesc'}, scalar(localtime(time)) );
  101:     return @title;
  102: }
  103: 
  104: sub get_html_title {
  105:     my $self = shift;
  106:     my ($classcalc_title,$time) = $self->get_title();
  107:     my $title = '<h1>'.$classcalc_title."</h1>\n".'<h3>'.$time."</h3>\n";
  108:     return $title;
  109: }
  110: 
  111: sub parent_link {
  112:     return '';
  113: }
  114: 
  115: sub outsheet_html {
  116:     my $self = shift;
  117:     my ($r) = @_;
  118:     ###################################
  119:     # Determine table structure
  120:     ###################################
  121:     my $num_uneditable = 26;
  122:     my $num_left = 52-$num_uneditable;
  123:     my $tableheader =<<"END";
  124: <p>
  125: <table border="2">
  126: <tr>
  127:   <th colspan="2" rowspan="2"><font size="+2">Course</font></th>
  128:   <td bgcolor="#FFDDDD" colspan="$num_uneditable">
  129:       <b><font size="+1">Import</font></b></td>
  130:   <td colspan="$num_left">
  131:       <b><font size="+1">Calculations</font></b></td>
  132: </tr><tr>
  133: END
  134:     my $label_num = 0;
  135:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
  136:         if ($label_num<$num_uneditable) { 
  137:             $tableheader.='<th bgcolor="#FFDDDD">';
  138:         } else {
  139:             $tableheader.='<th>';
  140:         }
  141:         $tableheader.="<b><font size=+1>$_</font></b></th>";
  142:         $label_num++;
  143:     }
  144:     $tableheader.="</tr>\n";
  145:     #
  146:     $r->print($tableheader);
  147:     #
  148:     # Print out template row
  149:     $r->print('<tr><td>Template</td><td>&nbsp;</td>'.
  150: 	      $self->html_template_row($num_uneditable)."</tr>\n");
  151:     #
  152:     # Print out summary/export row
  153:     $r->print('<tr><td>Summary</td><td>0</td>'.
  154: 	      $self->html_export_row()."</tr>\n");
  155:     #
  156:     # Prepare to output rows
  157:     $tableheader =<<"END";
  158: </p><p>
  159: <table border="2">
  160: <tr><th>Row</th>
  161:   <th>student</th><th>username</th><th>domain</th>
  162:   <th>section</th><th>status</th>
  163: END
  164:     foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
  165: 	if ($label_num<$num_uneditable) { 
  166:             $tableheader.='<th bgcolor="#FFDDDD">';
  167:         } else {
  168:             $tableheader.='<th>';
  169:         }
  170:         $tableheader.="<b><font size=+1>$_</font></b></th>";
  171:     }
  172:     #
  173:     my $num_output = 0;
  174:     foreach my $student ($self->get_classlist()) {
  175: 	if ($num_output++ % 50 == 0) {
  176: 	    $r->print("</table>\n".$tableheader);
  177: 	}
  178: 	my $rownum = $self->get_row_number_from_key
  179: 	    ($student->{'username'}.':'.$student->{'domain'});
  180:         my $link = '<a href="/adm/studentcalc?sname='.$student->{'username'}.
  181:             '&sdomain='.$student->{'domain'}.'">';
  182:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
  183: 	$r->print('<tr>'.'<td>'.$rownum.'</td>'.
  184: 		  '<td>'.$link.$student->{'fullname'}.'</a></td>'.
  185: 		  '<td>'.$student->{'username'}.'</td>'.
  186: 		  '<td>'.$student->{'domain'}  .'</td>'.
  187: 		  '<td>'.$student->{'section'} .'</td>'.
  188: 		  '<td>'.$student->{'status'}  .'</td>'.
  189: 		  $self->html_row($num_uneditable,$rownum)."</tr>\n");
  190:     }
  191:     $r->print("</table></p>\n");
  192:     return;
  193: }
  194: 
  195: sub outsheet_csv {
  196:     my $self = shift;
  197:     my ($r) = @_;
  198:     $r->print('<h1>csv output is not supported yet</h1>');
  199: }
  200: 
  201: 
  202: sub excel_rows {
  203:     # writes the meat of the spreadsheet to an excel worksheet.  Called
  204:     # by Spreadsheet::outsheet_excel;
  205:     my $self = shift;
  206:     my ($worksheet,$cols_output,$rows_output) = @_;
  207:     #
  208:     # Write a header row
  209:     $cols_output = 0;
  210:     foreach my $value ('fullname','username','domain','section','status') {
  211:         $worksheet->write($rows_output,$cols_output++,$value);
  212:     }
  213:     $rows_output++;    
  214:     #
  215:     # Write each students row
  216:     foreach my $student ($self->get_classlist()) {
  217:         $cols_output = 0;
  218: 	my $rownum = $self->get_row_number_from_key
  219: 	    ($student->{'username'}.':'.$student->{'domain'});
  220:         $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
  221:         my @studentdata = ($student->{'fullname'},
  222:                            $student->{'username'},
  223:                            $student->{'domain'},
  224:                            $student->{'section'},
  225:                            $student->{'status'});
  226:         $self->excel_output_row($worksheet,$rownum,$rows_output++,
  227:                                 @studentdata);
  228:     }
  229:     return;
  230: }
  231: 
  232: 
  233: sub outsheet_recursive_excel {
  234:     my $self = shift;
  235:     my ($r) = @_;
  236: } 
  237: 
  238: sub compute {
  239:     my $self = shift;
  240:     my ($r) = @_;
  241:     $self->initialize_safe_space();
  242:     my %c = $self->constants();
  243:     my %f = $self->formulas();
  244:     my @Students = $self->get_classlist();
  245:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  246:         ($r,'Spreadsheet Computation Status',
  247:          'Spreadsheet Computation', scalar(@Students));
  248:     &Apache::studentcalc::initialize_package();
  249:     foreach my $student ($self->get_classlist()) {
  250:         my $sname = $student->{'username'}.':'.$student->{'domain'};
  251: 	my $studentsheet = Apache::studentcalc->new
  252: 	    ($student->{'username'},$student->{'domain'},undef);
  253: 	my @exportdata = $studentsheet->export_data();
  254: 	my $rownum = $self->get_row_number_from_key($sname);
  255:         $f{'A'.$rownum} = $sname;
  256:         $self->{'row_source'}->{$rownum} = $sname;
  257:         $c{'A'.$rownum} = shift(@exportdata);
  258: 	foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
  259:             my $cell = $_.$rownum;
  260:             my $data = shift(@exportdata);
  261:             if (defined($data)) {
  262:                 $f{$cell} = 'import';
  263:                 $c{$cell} = $data;
  264:             }
  265: 	}
  266:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  267:                                                  'last student');
  268:     }
  269:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  270:     $r->rflush();
  271:     $self->constants(\%c);
  272:     $self->formulas(\%f);
  273:     $self->calcsheet();
  274: }
  275: 
  276: 1;
  277: 
  278: __END__

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