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