File:
[LON-CAPA] /
loncom /
interface /
spreadsheet /
classcalc.pm
Revision
1.27:
download - view:
text,
annotated -
select for diffs
Mon May 1 06:17:16 2006 UTC (18 years, 5 months ago) by
raeburn
Branches:
MAIN
CVS tags:
version_2_1_99_3,
version_2_1_99_2,
version_2_1_99_1,
version_2_1_99_0,
HEAD
Determination of parameters for spreadsheet now correctly cascades for cases where a user has multiple active groups. Also groups are passed in argument list for Spreadsheet object as array reference. Lastly lonnet::get_users_groups function modified to only return user's active groups, except in case when user status has expired (and default end access date for students has also passed), in which case user's groups which were still active less than 24 hours before default end date are also included in user's groups. [For consistency with students groups returned by loncoursedata::get_students_groups()].
1: #
2: # $Id: classcalc.pm,v 1.27 2006/05/01 06:17:16 raeburn 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: ['Enrollment 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 .= '<nobr>'.&mt('Status: [_1]',
95: '<input type="text" '.
96: 'name="spreadsheet_status" size="60" value="" />'
97: ).'</nobr>';
98:
99: return "<p>\n<table>\n".
100: "<tr>".$toprow."</tr>\n".
101: "<tr>".$bottomrow."</tr>\n".
102: "</table>\n".$status."\n".
103: "</p>";
104: }
105:
106: sub get_title {
107: my $self = shift;
108: # Section info should be included
109: my @title = ($self->{'coursedesc'}, &Apache::lonlocal::locallocaltime(time) );
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";
117: return $title;
118: }
119:
120: sub parent_link {
121: return '';
122: }
123:
124: sub outsheet_html {
125: my $self = shift;
126: my ($r) = @_;
127: ####################################
128: # Report any calculation errors #
129: ####################################
130: $r->print($self->html_report_error());
131: ###################################
132: # Determine table structure
133: ###################################
134: my $importcolor = '#88FF88';
135: my $exportcolor = '#BBBBFF';
136: my $num_uneditable = 26;
137: my $num_left = 52-$num_uneditable;
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',
148: 'groups' => 'Groups',
149: 'row' => 'Row',
150: );
151: my $tableheader =<<"END";
152: <p>
153: <table border="2">
154: <tr>
155: <th colspan="2" rowspan="2"><font size="+2">$header{'course'}</font></th>
156: <td bgcolor="$importcolor" colspan="$num_uneditable">
157: <b><font size="+1">$header{'import'}</font></b></td>
158: <td colspan="$num_left">
159: <b><font size="+1">$header{'calculations'}</font></b></td>
160: </tr><tr>
161: END
162: my $label_num = 0;
163: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
164: if ($label_num<$num_uneditable) {
165: $tableheader.='<th bgcolor="'.$importcolor.'">';
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
177: $r->print('<tr><td>'.&mt('Template').'</td><td> </td>'.
178: $self->html_template_row($num_uneditable,$importcolor).
179: "</tr>\n");
180: #
181: # Print out summary/export row
182: $r->print('<tr><td>'.&mt('Summary').'</td><td>0</td>'.
183: $self->html_export_row($exportcolor)."</tr>\n");
184: #
185: # Prepare to output rows
186: $tableheader =<<"END";
187: </p><p>
188: <table border="2">
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>
194: <th>$header{'groups'}</th>
195: <th>$header{'status'}</th>
196: END
197: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
198: if ($label_num<$num_uneditable) {
199: $tableheader.='<th bgcolor="'.$importcolor.'">';
200: } else {
201: $tableheader.='<th>';
202: }
203: $tableheader.="<b><font size=+1>$_</font></b></th>";
204: }
205: #
206: my $num_output = 0;
207: foreach my $student (@Students) {
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>'.
217: '<td>'.$student->{'fullname'}.'</td>'.
218: '<td>'.$link.$student->{'username'}.'</a></td>'.
219: '<td>'.$student->{'domain'} .'</td>'.
220: '<td>'.$student->{'section'} .'</td>'.
221: '<td>'.$student->{'groups'} .'</td>'.
222: '<td>'.$student->{'status'} .'</td>'.
223: $self->html_row($num_uneditable,$rownum,$exportcolor,
224: $importcolor).
225: "</tr>\n");
226: }
227: $r->print("</table></p>\n");
228: return;
229: }
230:
231: sub excel_rows {
232: # writes the meat of the spreadsheet to an excel worksheet. Called
233: # by Spreadsheet::outsheet_excel;
234: my $self = shift;
235: my ($connection,$worksheet,$cols_output,$rows_output,$format) = @_;
236: #
237: # Write a header row
238: $cols_output = 0;
239: foreach my $value ('Fullname','Username','Domain','Section','Group','Status','ID') {
240: $worksheet->write($rows_output,$cols_output++,&mt($value),$format->{'h4'});
241: }
242: $rows_output++;
243: #
244: # Write each students row
245: foreach my $student (@Students) {
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'},
254: $student->{'groups'},
255: $student->{'status'},
256: $student->{'id'});
257: $self->excel_output_row($worksheet,$rownum,$rows_output++,
258: @studentdata);
259: }
260: return;
261: }
262:
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;
267: my ($connection,$filehandle) = @_;
268: #
269: # Write a header row
270: $self->csv_output_row($filehandle,undef,
271: (&mt('Fullname'),&mt('Username'),&mt('Domain'),&mt('Section'),&mt('Group'),&mt('Status'),&mt('ID')));
272: #
273: # Write each students row
274: foreach my $student (@Students) {
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'},
282: $student->{'groups'},
283: $student->{'status'},
284: $student->{'id'});
285: $self->csv_output_row($filehandle,$rownum,@studentdata);
286: }
287: return;
288: }
289:
290: sub output_options {
291: my $self = shift();
292: return ({value => 'htmlclasslist',
293: description => 'Student Sheet Links'},
294: {value => 'source',
295: description => 'Show Source'},
296: {value => 'html',
297: description => 'HTML'},
298: {value => 'excel',
299: description => 'Excel'},
300: {value => 'csv',
301: description => 'Comma Separated Values'},
302: # {value => 'xml',
303: # description => 'XML'},
304: );
305: }
306:
307: sub outsheet_recursive_excel {
308: my $self = shift;
309: my ($r) = @_;
310: }
311:
312: sub outsheet_htmlclasslist {
313: my $self = shift;
314: my ($r) = @_;
315: #
316: # Determine if we should output expire caches links...
317: my $show_expire_link = 0;
318: if (exists($env{'user.role.dc./'.$env{'request.role.domain'}.'/'})){
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: #
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',
338: 'groups' => 'Groups',
339: );
340: #
341: # Prepare to output rows
342: my $tableheader =<<"END";
343: </p><p>
344: <table border="2">
345: <tr>
346: <th></th>
347: <th>$header{'student'}</th>
348: <th>$header{'username'}</th>
349: <th>$header{'domain'}</th>
350: <th>$header{'section'}</th>
351: <th>$header{'groups'}</th>
352: <th>$header{'status'}</th>
353: END
354: if ($show_expire_link) {
355: $tableheader.= '<th> </th>';
356: }
357: $tableheader.= "</tr>\n";
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>'.
368: '<td>'.$num_output.'</td>'.
369: '<td>'.$link.$student->{'fullname'}.'</a></td>'.
370: '<td>'.$link.$student->{'username'}.'</a></td>'.
371: '<td>'.$student->{'domain'} .'</td>'.
372: '<td>'.$student->{'section'} .'</td>'.
373: '<td>'.$student->{'groups'} .'</td>'.
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");
383: }
384: $r->print("</table></p>\n");
385: return;
386: }
387:
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:
398: sub compute {
399: my $self = shift;
400: my ($r) = @_;
401: my $connection = $r->connection();
402: if ($connection->aborted()) { $self->cleanup(); return; }
403: $self->initialize_safe_space();
404: my %c = $self->constants();
405: my %f = $self->formulas();
406: &update_status($r,'Initializing Course Structure');
407: &Apache::studentcalc::initialize_package();
408: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
409: ($r,&mt('Spreadsheet Computation Status'),
410: &mt('Spreadsheet Computation'), scalar(@Students),'inline',undef,
411: 'sheet','spreadsheet_status');
412: &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,
413: &mt('Processing first student'));
414: foreach my $student (@Students) {
415: if ($connection->aborted()) { $self->cleanup(); return; }
416: my $sname = $student->{'username'}.':'.$student->{'domain'};
417: my $studentsheet = Apache::studentcalc->new
418: ($student->{'username'},$student->{'domain'},undef,undef,
419: $student->{'section'},$student->{'groupref'} );
420: if ($connection->aborted()) { $self->cleanup(); return; }
421: my @exportdata = $studentsheet->export_data($r);
422: if ($studentsheet->badcalc()) {
423: $self->set_calcerror($sname.' : '.
424: $studentsheet->calcerror());
425: }
426: if ($connection->aborted()) { $self->cleanup(); return; }
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);
443: &update_status($r,'Done computing student sheets');
444: $r->rflush();
445: $self->constants(\%c);
446: $self->formulas(\%f);
447: $self->calcsheet();
448: $self->save() if ($self->need_to_save());
449: &update_status($r,'Done!');
450: }
451:
452: 1;
453:
454: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>