Annotation of loncom/interface/spreadsheet/studentcalc.pm, revision 1.11
1.1 matthew 1: #
1.11 ! matthew 2: # $Id: studentcalc.pm,v 1.10 2003/05/29 18:39:58 matthew 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: studentcalc
34:
35: =head1 SYNOPSIS
36:
37: =head1 DESCRIPTION
38:
39: =over 4
40:
41: =cut
42:
43: ###################################################
44: ### StudentSheet ###
45: ###################################################
46: package Apache::studentcalc;
47:
48: use strict;
49: use Apache::Constants qw(:common :http);
50: use Apache::lonnet;
1.3 matthew 51: use Apache::loncommon();
52: use Apache::loncoursedata();
1.1 matthew 53: use Apache::lonnavmaps;
1.3 matthew 54: use Apache::Spreadsheet();
55: use Apache::assesscalc();
1.1 matthew 56: use HTML::Entities();
57: use Spreadsheet::WriteExcel;
58: use Time::HiRes;
59:
60: @Apache::studentcalc::ISA = ('Apache::Spreadsheet');
61:
62: my @Sequences = ();
63: my %Exportrows = ();
64:
65: my $current_course;
66:
1.8 matthew 67: sub initialize {
1.11 ! matthew 68: &Apache::assesscalc::initialize();
1.8 matthew 69: &initialize_sequence_cache();
70: }
71:
1.1 matthew 72: sub initialize_package {
73: $current_course = $ENV{'request.course.id'};
74: &initialize_sequence_cache();
75: &load_cached_export_rows();
1.8 matthew 76: }
77:
78: sub ensure_correct_sequence_data {
79: if ($current_course ne $ENV{'request.course.id'}) {
80: &initialize_sequence_cache();
81: $current_course = $ENV{'request.course.id'};
82: }
83: return;
1.1 matthew 84: }
85:
86: sub initialize_sequence_cache {
87: #
88: # Set up the sequences and assessments
89: @Sequences = ();
90: my ($top,$sequences,$assessments) =
91: &Apache::loncoursedata::get_sequence_assessment_data();
92: if (! defined($top) || ! ref($top)) {
93: # There has been an error, better report it
1.2 matthew 94: &Apache::lonnet::logthis('top is undefined (studentcalc.pm)');
1.1 matthew 95: return;
96: }
97: @Sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
98: }
99:
100: sub clear_package {
101: @Sequences = undef;
102: %Exportrows = undef;
103: }
104:
105: sub get_title {
106: my $self = shift;
1.6 matthew 107: my @title = ();
108: #
109: # Determine the students name
1.3 matthew 110: my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'},
111: $self->{'domain'});
1.6 matthew 112: my $name = join(' ',
113: @userenv{'firstname','middlename','lastname','generation'});
1.3 matthew 114: $name =~ s/\s+$//;
1.6 matthew 115:
116: push (@title,$name);
117: push (@title,$self->{'coursedesc'});
118: push (@title,scalar(localtime(time)));
119: return @title;
120: }
121:
122: sub get_html_title {
123: my $self = shift;
124: my ($name,$desc,$time) = $self->get_title();
125: my $title = '<h1>'.$name;
1.3 matthew 126: if ($ENV{'user.name'} ne $self->{'name'} &&
127: $ENV{'user.domain'} ne $self->{'domain'}) {
128: $title .= &Apache::loncommon::aboutmewrapper
129: ($self->{'name'}.'@'.$self->{'domain'},
130: $self->{'name'},$self->{'domain'});
131: }
132: $title .= "</h1>\n";
1.6 matthew 133: $title .= '<h2>'.$desc."</h2>\n";
134: $title .= '<h3>'.$time.'</h3>';
1.1 matthew 135: return $title;
136: }
137:
138: sub parent_link {
139: my $self = shift;
140: my $link .= '<p><a href="/adm/classcalc?'.
141: 'sname='.$self->{'name'}.
142: '&sdomain='.$self->{'domain'}.'">'.
143: 'Course level sheet</a></p>'."\n";
144: return $link;
145: }
146:
147: sub outsheet_html {
148: my $self = shift;
149: my ($r) = @_;
150: ####################################
151: # Get the list of assessment files #
152: ####################################
153: my @AssessFileNames = $self->othersheets('assesscalc');
1.2 matthew 154: my $editing_is_allowed = &Apache::lonnet::allowed('mgr',
155: $ENV{'request.course.id'});
1.1 matthew 156: ####################################
157: # Determine table structure #
158: ####################################
159: my $num_uneditable = 26;
160: my $num_left = 52-$num_uneditable;
161: my $tableheader =<<"END";
1.2 matthew 162: <p>
1.1 matthew 163: <table border="2">
164: <tr>
165: <th colspan="2" rowspan="2"><font size="+2">Student</font></th>
166: <td bgcolor="#FFDDDD" colspan="$num_uneditable">
167: <b><font size="+1">Import</font></b></td>
168: <td colspan="$num_left">
169: <b><font size="+1">Calculations</font></b></td>
170: </tr><tr>
171: END
172: my $label_num = 0;
173: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
174: if ($label_num<$num_uneditable) {
175: $tableheader .='<td bgcolor="#FFDDDD">';
176: } else {
177: $tableheader .='<td>';
178: }
179: $tableheader .="<b><font size=+1>$_</font></b></td>";
180: $label_num++;
181: }
182: $tableheader .="</tr>\n";
1.4 matthew 183: if ($self->blackout()) {
184: $r->print('<font color="red" size="+2"><p>'.
185: 'Some computations are not available at this time.<br />'.
186: 'There are problems whose status you are allowed to view.'.
187: '</font></p>'."\n");
188: } else {
189: $r->print($tableheader);
190: #
191: # Print out template row
192: if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
193: $r->print('<tr><td>Template</td><td> </td>'.
194: $self->html_template_row($num_uneditable)."</tr>\n");
195: }
196: #
197: # Print out summary/export row
198: $r->print('<tr><td>Summary</td><td>0</td>'.
199: $self->html_export_row()."</tr>\n");
200: }
1.1 matthew 201: $r->print("</table>\n");
202: #
203: # Prepare to output rows
1.4 matthew 204: if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
205: $tableheader =<<"END";
1.2 matthew 206: </p><p>
1.1 matthew 207: <table border="2">
208: <tr><th>Row</th><th>Assessment</th>
209: END
1.4 matthew 210: } else {
211: $tableheader =<<"END";
212: </p><p>
213: <table border="2">
214: <tr><th> </th><th>Assessment</th>
215: END
216: }
1.1 matthew 217: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
218: if ($label_num<$num_uneditable) {
219: $tableheader.='<td bgcolor="#FFDDDD">';
220: } else {
221: $tableheader.='<td>';
222: }
223: $tableheader.="<b><font size=+1>$_</font></b></td>";
224: }
225: $tableheader.="\n";
226: #
227: my $num_output = 1;
228: if (scalar(@Sequences)< 1) {
229: &initialize_sequence_cache();
230: }
231: foreach my $Sequence (@Sequences) {
232: next if ($Sequence->{'num_assess'} < 1);
233: $r->print("<h3>".$Sequence->{'title'}."</h3>\n");
234: $r->print($tableheader);
235: foreach my $resource (@{$Sequence->{'contents'}}) {
236: next if ($resource->{'type'} ne 'assessment');
237: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
238: my $assess_filename = $self->{'row_source'}->{$rownum};
1.2 matthew 239: my $row_output = '<tr>';
240: if ($editing_is_allowed) {
241: $row_output .= '<td>'.$rownum.'</td>';
242: $row_output .= '<td>'.
243: '<a href="/adm/assesscalc?sname='.$self->{'name'}.
244: '&sdomain='.$self->{'domain'}.
245: '&filename='.$assess_filename.
1.4 matthew 246: '&usymb='.&Apache::lonnet::escape($resource->{'symb'}).
247: '">'.$resource->{'title'}.'</a><br />';
1.2 matthew 248: $row_output .= &assess_file_selector($rownum,
249: $assess_filename,
250: \@AssessFileNames).
251: '</td>';
252: } else {
253: $row_output .= '<td><a href="'.$resource->{'src'}.'?symb='.
1.4 matthew 254: &Apache::lonnet::escape($resource->{'symb'}).
255: '">Go To</a>';
1.2 matthew 256: $row_output .= '</td><td>'.$resource->{'title'}.'</td>';
257: }
1.4 matthew 258: if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) {
259: $row_output .=
260: '<td colspan="52">Unavailable at this time</td></tr>'."\n";
261: } else {
262: $row_output .= $self->html_row($num_uneditable,$rownum).
263: "</tr>\n";
264: }
1.1 matthew 265: $r->print($row_output);
266: }
267: $r->print("</table>\n");
268: }
1.2 matthew 269: $r->print("</p>\n");
1.1 matthew 270: return;
271: }
272:
273: ########################################################
274: ########################################################
275:
276: =pod
277:
278: =item &assess_file_selector()
279:
280: =cut
281:
282: ########################################################
283: ########################################################
284: sub assess_file_selector {
285: my ($row,$default,$AssessFiles)=@_;
1.2 matthew 286: if (!defined($AssessFiles) || ! @$AssessFiles) {
287: return '';
288: }
1.1 matthew 289: return '' if (! &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}));
290: my $element_name = 'FileSelect_'.$row;
291: my $load_dialog = '<select size="1" name="'.$element_name.'" '.
292: 'onchange="'.
293: "document.sheet.cell.value='source_$row';".
294: "document.sheet.newformula.value=document.sheet.$element_name\.value;".
295: 'document.sheet.submit()" '.'>'."\n";
296: foreach my $file (@{$AssessFiles}) {
297: $load_dialog .= ' <option name="'.$file.'"';
298: $load_dialog .= ' selected' if ($default eq $file);
299: $load_dialog .= '>'.$file."</option>\n";
300: }
301: $load_dialog .= "</select>\n";
302: return $load_dialog;
303: }
304:
305: sub modify_cell {
306: my $self = shift;
307: my ($cell,$formula) = @_;
308: if ($cell =~ /^source_(\d+)$/) {
309: # Need to make sure $formula is a valid filename....
310: my $row = $1;
311: $cell = 'A'.$row;
312: $self->{'row_source'}->{$row} = $formula;
313: my $original_source = $self->formula($cell);
314: if ($original_source =~ /__&&&__/) {
315: ($original_source,undef) = split('__&&&__',$original_source);
316: }
317: $formula = $original_source.'__&&&__'.$formula;
318: } elsif ($cell =~ /([A-z])\-/) {
319: $cell = 'template_'.$1;
320: } elsif ($cell !~ /^([A-z](\d+)|template_[A-z])$/) {
321: return;
322: }
323: $self->set_formula($cell,$formula);
324: $self->rebuild_stats();
325: return;
326: }
327:
1.7 matthew 328: sub csv_rows {
329: # writes the meat of the spreadsheet to an excel worksheet. Called
330: # by Spreadsheet::outsheet_excel;
1.1 matthew 331: my $self = shift;
1.7 matthew 332: my ($filehandle) = @_;
333: #
334: # Write a header row
335: $self->csv_output_row($filehandle,undef,
1.10 matthew 336: ('Sequence or Folder','Assessment title'));
1.7 matthew 337: #
338: # Write each assessments row
339: if (scalar(@Sequences)< 1) {
340: &initialize_sequence_cache();
341: }
342: foreach my $Sequence (@Sequences) {
343: next if ($Sequence->{'num_assess'} < 1);
344: foreach my $resource (@{$Sequence->{'contents'}}) {
345: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
346: my @assessdata = ($Sequence->{'title'},
347: $resource->{'title'});
348: $self->csv_output_row($filehandle,$rownum,@assessdata);
349: }
350: }
351: return;
1.1 matthew 352: }
1.6 matthew 353:
354: sub excel_rows {
355: # writes the meat of the spreadsheet to an excel worksheet. Called
356: # by Spreadsheet::outsheet_excel;
1.1 matthew 357: my $self = shift;
1.6 matthew 358: my ($worksheet,$cols_output,$rows_output) = @_;
359: #
360: # Write a header row
361: $cols_output = 0;
362: foreach my $value ('Container','Assessment title') {
363: $worksheet->write($rows_output,$cols_output++,$value);
364: }
365: $rows_output++;
366: #
367: # Write each assessments row
368: if (scalar(@Sequences)< 1) {
369: &initialize_sequence_cache();
370: }
371: foreach my $Sequence (@Sequences) {
372: next if ($Sequence->{'num_assess'} < 1);
373: foreach my $resource (@{$Sequence->{'contents'}}) {
374: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
375: my @assessdata = ($Sequence->{'title'},
376: $resource->{'title'});
377: $self->excel_output_row($worksheet,$rownum,$rows_output++,
378: @assessdata);
379: }
380: }
381: return;
1.1 matthew 382: }
1.6 matthew 383:
1.1 matthew 384: sub outsheet_recursive_excel {
385: my $self = shift;
386: my ($r) = @_;
387: }
388:
389: sub compute {
390: my $self = shift;
391: $self->logthis('computing');
392: if (! defined($current_course) ||
393: $current_course ne $ENV{'request.course.id'}) {
394: $current_course = $ENV{'request.course.id'};
395: &clear_package();
396: &initialize_sequence_cache();
397: }
398: $self->initialize_safe_space();
399: my @sequences = @Sequences;
400: if (@sequences < 1) {
401: my ($top,$sequences,$assessments) =
402: &Apache::loncoursedata::get_sequence_assessment_data();
403: if (! defined($top) || ! ref($top)) {
404: &Apache::lonnet::logthis('top is undefined');
405: return;
406: }
407: @sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
408: }
409: &Apache::assesscalc::initialize_package($self->{'name'},$self->{'domain'});
410: my %f = $self->formulas();
411: #
412: # Process the formulas list -
413: # the formula for the A column of a row is symb__&&__filename
414: my %c = $self->constants();
415: foreach my $seq (@sequences) {
416: next if ($seq->{'num_assess'}<1);
417: foreach my $resource (@{$seq->{'contents'}}) {
418: next if ($resource->{'type'} ne 'assessment');
419: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
420: my $cell = 'A'.$rownum;
421: my $assess_filename = 'Default';
422: if (exists($self->{'row_source'}->{$rownum})) {
423: $assess_filename = $self->{'row_source'}->{$rownum};
424: } else {
425: $self->{'row_source'}->{$rownum} = $assess_filename;
426: }
427: $f{$cell} = $resource->{'symb'}.'__&&&__'.$assess_filename;
428: my $assessSheet = Apache::assesscalc->new($self->{'name'},
429: $self->{'domain'},
430: $assess_filename,
431: $resource->{'symb'});
432: my @exportdata = $assessSheet->export_data();
1.4 matthew 433: if ($assessSheet->blackout()) {
434: $self->blackout(1);
435: $self->{'blackout_rows'}->{$rownum} = 1;
436: }
1.1 matthew 437: #
438: # Be sure not to disturb the formulas in the 'A' column
439: my $data = shift(@exportdata);
440: $c{$cell} = $data if (defined($data));
441: #
442: # Deal with the remaining columns
443: my $i=0;
444: foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
445: my $cell = $_.$rownum;
446: my $data = shift(@exportdata);
447: if (defined($data)) {
448: $f{$cell} = 'import';
449: $c{$cell} = $data;
450: }
451: $i++;
452: }
453: }
454: }
455: $self->constants(\%c);
456: $self->formulas(\%f);
457: $self->calcsheet();
458: #
459: # Store export row in cache
460: my @exportarray=$self->exportrow();
461: my $student = $self->{'name'}.':'.$self->{'domain'};
462: $Exportrows{$student}->{'time'} = time;
463: $Exportrows{$student}->{'data'} = \@exportarray;
464: # save export row
465: $self->save_export_data();
1.9 matthew 466: #
467: $self->save() if ($self->need_to_save());
468: return;
469: }
470:
471: sub set_row_sources {
472: my $self = shift;
473: while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
474: next if ($cell !~ /^A(\d+)/ && $1 > 0);
475: my $row = $1;
476: (undef,$value) = split('__&&&__',$value);
477: $value = 'Default' if (! defined($value));
478: $self->{'row_source'}->{$row} = $value;
479: }
1.1 matthew 480: return;
481: }
482:
483: sub set_row_numbers {
484: my $self = shift;
485: while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
486: next if ($cell !~ /^A(\d+)/);
487: my $row = $1;
488: next if ($row == 0);
489: my ($symb,undef) = split('__&&&__',$formula);
490: $self->{'row_numbers'}->{$symb} = $row;
1.9 matthew 491: $self->{'maxrow'} = $1 if ($1 > $self->{'maxrow'});
1.1 matthew 492: }
493: }
494:
495: sub get_row_number_from_symb {
496: my $self = shift;
497: my ($key) = @_;
498: ($key,undef) = split('__&&&__',$key) if ($key =~ /__&&&__/);
499: return $self->get_row_number_from_key($key);
500: }
501:
502: #############################################
503: #############################################
504:
505: =pod
506:
507: =item &load_cached_export_rows
508:
509: Retrieves and parsers the export rows of the student spreadsheets.
510: These rows are saved in the courses directory in the format:
511:
512: sname:sdom:studentcalc:.time => time
513:
514: sname:sdom:studentcalc => ___=___Adata___;___Bdata___;___Cdata___;___ .....
515:
516: =cut
517:
518: #############################################
519: #############################################
520: sub load_cached_export_rows {
521: %Exportrows = undef;
522: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
523: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
524: $ENV{'course.'.$ENV{'request.course.id'}.'.num'},undef);
525: my %Selected_Assess_Sheet;
526: if ($tmp[0] =~ /^error/) {
527: &Apache::lonnet::logthis('unable to read cached student export rows '.
528: 'for course '.$ENV{'request.course.id'});
529: return;
530: }
531: my %tmp = @tmp;
532: while (my ($key,$sheetdata) = each(%tmp)) {
533: my ($sname,$sdom,$sheettype,$remainder) = split(':',$key);
534: my $student = $sname.':'.$sdom;
535: if ($remainder =~ /\.time/) {
536: $Exportrows{$student}->{'time'} = $sheetdata;
537: } else {
538: $sheetdata =~ s/^___=___//;
539: my @Data = split('___;___',$sheetdata);
540: $Exportrows{$student}->{'data'} = \@Data;
541: }
542: }
543: }
544:
545: #############################################
546: #############################################
547:
548: =pod
549:
550: =item &save_export_data()
551:
552: Writes the export data for this student to the course cache.
553:
554: =cut
555:
556: #############################################
557: #############################################
558: sub save_export_data {
559: my $self = shift;
1.5 matthew 560: return if ($self->temporary());
1.1 matthew 561: my $student = $self->{'name'}.':'.$self->{'domain'};
562: return if (! exists($Exportrows{$student}));
563: return if (! $self->is_default());
564: my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
565: my $timekey = $key.'.time';
566: my $newstore = join('___;___',
567: @{$Exportrows{$student}->{'data'}});
568: $newstore = '___=___'.$newstore;
569: &Apache::lonnet::put('nohist_calculatedsheets',
570: { $key => $newstore,
571: $timekey => $Exportrows{$student}->{'time'} },
572: $self->{'cdom'},
573: $self->{'cnum'});
574: return;
575: }
576:
577: #############################################
578: #############################################
579:
580: =pod
581:
582: =item &export_data()
583:
584: Returns the export data associated with the spreadsheet. Computes the
585: spreadsheet only if necessary.
586:
587: =cut
588:
589: #############################################
590: #############################################
591: sub export_data {
592: my $self = shift;
593: my $student = $self->{'name'}.':'.$self->{'domain'};
594: if (! exists($Exportrows{$student}) ||
595: ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
596: $self->compute();
597: }
598: my @Data = @{$Exportrows{$student}->{'data'}};
599: for (my $i=0; $i<=$#Data;$i++) {
600: $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
601: }
602: return @Data;
603: }
604:
605: 1;
606:
607: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>