Annotation of loncom/interface/spreadsheet/classcalc.pm, revision 1.24
1.1 matthew 1: #
1.24 ! bowersj2 2: # $Id: classcalc.pm,v 1.23 2005/04/07 06:56:23 albertel 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> </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.22 matthew 231: my ($connection,$worksheet,$cols_output,$rows_output,$format) = @_;
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') {
1.22 matthew 236: $worksheet->write($rows_output,$cols_output++,&mt($value),$format->{'h4'});
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'},
1.24 ! bowersj2 288: {value => 'source',
! 289: description => 'Show Source'},
1.16 matthew 290: {value => 'html',
291: description => 'HTML'},
292: {value => 'excel',
293: description => 'Excel'},
294: {value => 'csv',
295: description => 'Comma Separated Values'},
1.18 matthew 296: # {value => 'xml',
297: # description => 'XML'},
298: );
1.16 matthew 299: }
300:
1.1 matthew 301: sub outsheet_recursive_excel {
302: my $self = shift;
303: my ($r) = @_;
1.16 matthew 304: }
305:
306: sub outsheet_htmlclasslist {
307: my $self = shift;
308: my ($r) = @_;
309: #
1.19 matthew 310: # Determine if we should output expire caches links...
311: my $show_expire_link = 0;
1.23 albertel 312: if (exists($env{'user.role.dc./'.$env{'request.role.domain'}.'/'})){
1.19 matthew 313: $show_expire_link = 1;
314: }
315: #
316: if ($show_expire_link) {
317: $r->print('<a href="/adm/classcalc?output_format=htmlclasslist&'.
318: 'recalc=ilovewastingtime¬_first_run=1">'.
319: &mt('Expire all student spreadsheets').'</a>'.$/);
320: }
321: #
1.16 matthew 322: $r->print('<h3>'.
323: &mt('Click on a student to be taken to their spreadsheet').
324: '</h3>');
325: #
326: my %header=&Apache::lonlocal::texthash(
327: 'student' => 'Student',
328: 'status' => 'Status',
329: 'username' => 'Username',
330: 'domain' => 'Domain',
331: 'section' => 'Section',
332: );
333: #
334: # Prepare to output rows
335: my $tableheader =<<"END";
336: </p><p>
337: <table border="2">
338: <tr>
1.17 matthew 339: <th></th>
1.16 matthew 340: <th>$header{'student'}</th>
341: <th>$header{'username'}</th>
342: <th>$header{'domain'}</th>
343: <th>$header{'section'}</th>
344: <th>$header{'status'}</th>
345: END
1.19 matthew 346: if ($show_expire_link) {
347: $tableheader.= '<th> </th>';
348: }
349: $tableheader.= "</tr>\n";
1.16 matthew 350: #
351: my $num_output = 0;
352: foreach my $student (@Students) {
353: if ($num_output++ % 50 == 0) {
354: $r->print("</table>\n".$tableheader);
355: }
356: my $link = '<a href="/adm/studentcalc?sname='.$student->{'username'}.
357: '&sdomain='.$student->{'domain'}.'">';
358: $student->{'section'} = 'none' if ($student->{'section'} eq '-1');
359: $r->print('<tr>'.
1.17 matthew 360: '<td>'.$num_output.'</td>'.
1.16 matthew 361: '<td>'.$link.$student->{'fullname'}.'</a></td>'.
362: '<td>'.$link.$student->{'username'}.'</a></td>'.
363: '<td>'.$student->{'domain'} .'</td>'.
364: '<td>'.$student->{'section'} .'</td>'.
1.19 matthew 365: '<td>'.$student->{'status'} .'</td>');
366: if ($show_expire_link) {
367: $r->print('<td>'.
368: '<a href="/adm/classcalc?recalc=student:'.
369: $student->{'username'}.':'.$student->{'domain'}.'&'.
370: 'output_format=htmlclasslist&'.
371: 'not_first_run=1'.'">Expire Record</a>'.$/);
372: }
373: $r->print("</tr>\n");
1.16 matthew 374: }
375: $r->print("</table></p>\n");
376: return;
1.15 matthew 377: }
1.1 matthew 378:
1.21 matthew 379: sub update_status {
380: my ($r,$message) = @_;
381: $r->print('<script>'.
382: 'window.document.sheet.spreadsheet_status.value="'.
383: $message.
384: '";</script>');
385: $r->rflush();
386: return;
387: }
388:
1.1 matthew 389: sub compute {
390: my $self = shift;
391: my ($r) = @_;
1.12 matthew 392: my $connection = $r->connection();
393: if ($connection->aborted()) { $self->cleanup(); return; }
1.1 matthew 394: $self->initialize_safe_space();
395: my %c = $self->constants();
396: my %f = $self->formulas();
1.21 matthew 397: &update_status($r,'Initializing Course Structure');
1.20 matthew 398: &Apache::studentcalc::initialize_package();
1.1 matthew 399: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
1.14 www 400: ($r,&mt('Spreadsheet Computation Status'),
1.21 matthew 401: &mt('Spreadsheet Computation'), scalar(@Students),'inline',undef,
402: 'sheet','spreadsheet_status');
1.9 matthew 403: &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
1.14 www 404: &mt('Processing first student'));
1.6 matthew 405: foreach my $student (@Students) {
1.12 matthew 406: if ($connection->aborted()) { $self->cleanup(); return; }
1.1 matthew 407: my $sname = $student->{'username'}.':'.$student->{'domain'};
408: my $studentsheet = Apache::studentcalc->new
409: ($student->{'username'},$student->{'domain'},undef);
1.12 matthew 410: if ($connection->aborted()) { $self->cleanup(); return; }
411: my @exportdata = $studentsheet->export_data($r);
1.15 matthew 412: if ($studentsheet->badcalc()) {
413: $self->set_calcerror($sname.' : '.
414: $studentsheet->calcerror());
415: }
1.12 matthew 416: if ($connection->aborted()) { $self->cleanup(); return; }
1.1 matthew 417: my $rownum = $self->get_row_number_from_key($sname);
418: $f{'A'.$rownum} = $sname;
419: $self->{'row_source'}->{$rownum} = $sname;
420: $c{'A'.$rownum} = shift(@exportdata);
421: foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
422: my $cell = $_.$rownum;
423: my $data = shift(@exportdata);
424: if (defined($data)) {
425: $f{$cell} = 'import';
426: $c{$cell} = $data;
427: }
428: }
429: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
430: 'last student');
431: }
432: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.21 matthew 433: &update_status($r,'Done computing student sheets');
1.1 matthew 434: $r->rflush();
435: $self->constants(\%c);
436: $self->formulas(\%f);
437: $self->calcsheet();
1.7 matthew 438: $self->save() if ($self->need_to_save());
1.21 matthew 439: &update_status($r,'Done!');
1.1 matthew 440: }
441:
442: 1;
443:
444: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>