Annotation of loncom/interface/spreadsheet/studentcalc.pm, revision 1.16
1.1 matthew 1: #
1.16 ! matthew 2: # $Id: studentcalc.pm,v 1.15 2003/07/16 20:30:36 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:
1.14 matthew 147: sub convenience_links {
148: my $self = shift;
149: my ($resource) = @_;
150: my $symb = &Apache::lonnet::escape($resource->{'symb'});
151: my $result = <<"END";
152: <a href="/adm/grades?symb=$symb&command=submission" target="LONcatInfo">
153: <img src="/adm/lonMisc/subm_button.gif" border=0 />
154: </a>
155: <a href="/adm/grades?symb=$symb&command=gradingmenu" target="LONcatInfo">
156: <img src="/adm/lonMisc/pgrd_button.gif" border=0 />
157: </a>
158: <a href="/adm/parmset?symb=$symb" target="LONcatInfo">
159: <img src="/adm/lonMisc/pprm_button.gif" border=0 />
160: </a>
161: END
162: return $result;
163: }
164:
1.1 matthew 165: sub outsheet_html {
166: my $self = shift;
167: my ($r) = @_;
1.13 matthew 168: my $importcolor = '#FFFFAA';
1.12 matthew 169: my $exportcolor = '#88FF88';
1.1 matthew 170: ####################################
171: # Get the list of assessment files #
172: ####################################
173: my @AssessFileNames = $self->othersheets('assesscalc');
1.2 matthew 174: my $editing_is_allowed = &Apache::lonnet::allowed('mgr',
175: $ENV{'request.course.id'});
1.1 matthew 176: ####################################
177: # Determine table structure #
178: ####################################
179: my $num_uneditable = 26;
180: my $num_left = 52-$num_uneditable;
181: my $tableheader =<<"END";
1.2 matthew 182: <p>
1.1 matthew 183: <table border="2">
184: <tr>
185: <th colspan="2" rowspan="2"><font size="+2">Student</font></th>
1.12 matthew 186: <td bgcolor="$importcolor" colspan="$num_uneditable">
1.1 matthew 187: <b><font size="+1">Import</font></b></td>
188: <td colspan="$num_left">
189: <b><font size="+1">Calculations</font></b></td>
190: </tr><tr>
191: END
192: my $label_num = 0;
193: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
194: if ($label_num<$num_uneditable) {
1.12 matthew 195: $tableheader .='<td bgcolor="'.$importcolor.'">';
1.1 matthew 196: } else {
197: $tableheader .='<td>';
198: }
199: $tableheader .="<b><font size=+1>$_</font></b></td>";
200: $label_num++;
201: }
202: $tableheader .="</tr>\n";
1.4 matthew 203: if ($self->blackout()) {
204: $r->print('<font color="red" size="+2"><p>'.
205: 'Some computations are not available at this time.<br />'.
206: 'There are problems whose status you are allowed to view.'.
207: '</font></p>'."\n");
208: } else {
209: $r->print($tableheader);
210: #
211: # Print out template row
212: if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
213: $r->print('<tr><td>Template</td><td> </td>'.
1.12 matthew 214: $self->html_template_row($num_uneditable,
215: $importcolor)."</tr>\n");
1.4 matthew 216: }
217: #
218: # Print out summary/export row
219: $r->print('<tr><td>Summary</td><td>0</td>'.
1.12 matthew 220: $self->html_export_row($exportcolor)."</tr>\n");
1.4 matthew 221: }
1.1 matthew 222: $r->print("</table>\n");
223: #
224: # Prepare to output rows
1.4 matthew 225: if (exists($ENV{'request.role.adv'}) && $ENV{'request.role.adv'}) {
226: $tableheader =<<"END";
1.2 matthew 227: </p><p>
1.1 matthew 228: <table border="2">
1.14 matthew 229: <tr><th>Row</th><th> </th><th>Assessment</th>
1.1 matthew 230: END
1.4 matthew 231: } else {
232: $tableheader =<<"END";
233: </p><p>
234: <table border="2">
235: <tr><th> </th><th>Assessment</th>
236: END
237: }
1.1 matthew 238: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
239: if ($label_num<$num_uneditable) {
240: $tableheader.='<td bgcolor="#FFDDDD">';
241: } else {
242: $tableheader.='<td>';
243: }
244: $tableheader.="<b><font size=+1>$_</font></b></td>";
245: }
246: $tableheader.="\n";
247: #
248: my $num_output = 1;
249: if (scalar(@Sequences)< 1) {
250: &initialize_sequence_cache();
251: }
252: foreach my $Sequence (@Sequences) {
253: next if ($Sequence->{'num_assess'} < 1);
254: $r->print("<h3>".$Sequence->{'title'}."</h3>\n");
255: $r->print($tableheader);
256: foreach my $resource (@{$Sequence->{'contents'}}) {
257: next if ($resource->{'type'} ne 'assessment');
258: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
259: my $assess_filename = $self->{'row_source'}->{$rownum};
1.2 matthew 260: my $row_output = '<tr>';
261: if ($editing_is_allowed) {
262: $row_output .= '<td>'.$rownum.'</td>';
1.14 matthew 263: $row_output .= '<td>'.$self->convenience_links($resource).'</td>';
1.2 matthew 264: $row_output .= '<td>'.
265: '<a href="/adm/assesscalc?sname='.$self->{'name'}.
266: '&sdomain='.$self->{'domain'}.
267: '&filename='.$assess_filename.
1.4 matthew 268: '&usymb='.&Apache::lonnet::escape($resource->{'symb'}).
269: '">'.$resource->{'title'}.'</a><br />';
1.2 matthew 270: $row_output .= &assess_file_selector($rownum,
271: $assess_filename,
272: \@AssessFileNames).
273: '</td>';
274: } else {
275: $row_output .= '<td><a href="'.$resource->{'src'}.'?symb='.
1.4 matthew 276: &Apache::lonnet::escape($resource->{'symb'}).
277: '">Go To</a>';
1.2 matthew 278: $row_output .= '</td><td>'.$resource->{'title'}.'</td>';
279: }
1.4 matthew 280: if ($self->blackout() && $self->{'blackout_rows'}->{$rownum}>0) {
281: $row_output .=
282: '<td colspan="52">Unavailable at this time</td></tr>'."\n";
283: } else {
1.12 matthew 284: $row_output .= $self->html_row($num_uneditable,$rownum,
285: $exportcolor,$importcolor).
1.4 matthew 286: "</tr>\n";
287: }
1.1 matthew 288: $r->print($row_output);
289: }
290: $r->print("</table>\n");
291: }
1.2 matthew 292: $r->print("</p>\n");
1.1 matthew 293: return;
294: }
295:
296: ########################################################
297: ########################################################
298:
299: =pod
300:
301: =item &assess_file_selector()
302:
303: =cut
304:
305: ########################################################
306: ########################################################
307: sub assess_file_selector {
308: my ($row,$default,$AssessFiles)=@_;
1.2 matthew 309: if (!defined($AssessFiles) || ! @$AssessFiles) {
310: return '';
311: }
1.1 matthew 312: return '' if (! &Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}));
313: my $element_name = 'FileSelect_'.$row;
314: my $load_dialog = '<select size="1" name="'.$element_name.'" '.
315: 'onchange="'.
316: "document.sheet.cell.value='source_$row';".
317: "document.sheet.newformula.value=document.sheet.$element_name\.value;".
318: 'document.sheet.submit()" '.'>'."\n";
319: foreach my $file (@{$AssessFiles}) {
320: $load_dialog .= ' <option name="'.$file.'"';
321: $load_dialog .= ' selected' if ($default eq $file);
322: $load_dialog .= '>'.$file."</option>\n";
323: }
324: $load_dialog .= "</select>\n";
325: return $load_dialog;
326: }
327:
328: sub modify_cell {
329: my $self = shift;
330: my ($cell,$formula) = @_;
331: if ($cell =~ /^source_(\d+)$/) {
332: # Need to make sure $formula is a valid filename....
333: my $row = $1;
334: $cell = 'A'.$row;
335: $self->{'row_source'}->{$row} = $formula;
336: my $original_source = $self->formula($cell);
337: if ($original_source =~ /__&&&__/) {
338: ($original_source,undef) = split('__&&&__',$original_source);
339: }
340: $formula = $original_source.'__&&&__'.$formula;
341: } elsif ($cell =~ /([A-z])\-/) {
342: $cell = 'template_'.$1;
343: } elsif ($cell !~ /^([A-z](\d+)|template_[A-z])$/) {
344: return;
345: }
346: $self->set_formula($cell,$formula);
347: $self->rebuild_stats();
348: return;
349: }
350:
1.7 matthew 351: sub csv_rows {
352: # writes the meat of the spreadsheet to an excel worksheet. Called
353: # by Spreadsheet::outsheet_excel;
1.1 matthew 354: my $self = shift;
1.7 matthew 355: my ($filehandle) = @_;
356: #
357: # Write a header row
358: $self->csv_output_row($filehandle,undef,
1.10 matthew 359: ('Sequence or Folder','Assessment title'));
1.7 matthew 360: #
361: # Write each assessments row
362: if (scalar(@Sequences)< 1) {
363: &initialize_sequence_cache();
364: }
365: foreach my $Sequence (@Sequences) {
366: next if ($Sequence->{'num_assess'} < 1);
367: foreach my $resource (@{$Sequence->{'contents'}}) {
368: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
369: my @assessdata = ($Sequence->{'title'},
370: $resource->{'title'});
371: $self->csv_output_row($filehandle,$rownum,@assessdata);
372: }
373: }
374: return;
1.1 matthew 375: }
1.6 matthew 376:
377: sub excel_rows {
378: # writes the meat of the spreadsheet to an excel worksheet. Called
379: # by Spreadsheet::outsheet_excel;
1.1 matthew 380: my $self = shift;
1.6 matthew 381: my ($worksheet,$cols_output,$rows_output) = @_;
382: #
383: # Write a header row
384: $cols_output = 0;
385: foreach my $value ('Container','Assessment title') {
386: $worksheet->write($rows_output,$cols_output++,$value);
387: }
388: $rows_output++;
389: #
390: # Write each assessments row
391: if (scalar(@Sequences)< 1) {
392: &initialize_sequence_cache();
393: }
394: foreach my $Sequence (@Sequences) {
395: next if ($Sequence->{'num_assess'} < 1);
396: foreach my $resource (@{$Sequence->{'contents'}}) {
397: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
398: my @assessdata = ($Sequence->{'title'},
399: $resource->{'title'});
400: $self->excel_output_row($worksheet,$rownum,$rows_output++,
401: @assessdata);
402: }
403: }
404: return;
1.1 matthew 405: }
1.6 matthew 406:
1.1 matthew 407: sub outsheet_recursive_excel {
408: my $self = shift;
409: my ($r) = @_;
410: }
411:
412: sub compute {
413: my $self = shift;
414: if (! defined($current_course) ||
415: $current_course ne $ENV{'request.course.id'}) {
416: $current_course = $ENV{'request.course.id'};
417: &clear_package();
418: &initialize_sequence_cache();
419: }
420: $self->initialize_safe_space();
421: my @sequences = @Sequences;
422: if (@sequences < 1) {
423: my ($top,$sequences,$assessments) =
424: &Apache::loncoursedata::get_sequence_assessment_data();
425: if (! defined($top) || ! ref($top)) {
426: &Apache::lonnet::logthis('top is undefined');
427: return;
428: }
429: @sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
430: }
431: &Apache::assesscalc::initialize_package($self->{'name'},$self->{'domain'});
432: my %f = $self->formulas();
433: #
434: # Process the formulas list -
435: # the formula for the A column of a row is symb__&&__filename
436: my %c = $self->constants();
437: foreach my $seq (@sequences) {
438: next if ($seq->{'num_assess'}<1);
439: foreach my $resource (@{$seq->{'contents'}}) {
440: next if ($resource->{'type'} ne 'assessment');
441: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
442: my $cell = 'A'.$rownum;
443: my $assess_filename = 'Default';
444: if (exists($self->{'row_source'}->{$rownum})) {
445: $assess_filename = $self->{'row_source'}->{$rownum};
446: } else {
447: $self->{'row_source'}->{$rownum} = $assess_filename;
448: }
449: $f{$cell} = $resource->{'symb'}.'__&&&__'.$assess_filename;
450: my $assessSheet = Apache::assesscalc->new($self->{'name'},
451: $self->{'domain'},
452: $assess_filename,
453: $resource->{'symb'});
454: my @exportdata = $assessSheet->export_data();
1.4 matthew 455: if ($assessSheet->blackout()) {
456: $self->blackout(1);
457: $self->{'blackout_rows'}->{$rownum} = 1;
458: }
1.1 matthew 459: #
460: # Be sure not to disturb the formulas in the 'A' column
461: my $data = shift(@exportdata);
462: $c{$cell} = $data if (defined($data));
463: #
464: # Deal with the remaining columns
465: my $i=0;
466: foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
467: my $cell = $_.$rownum;
468: my $data = shift(@exportdata);
469: if (defined($data)) {
470: $f{$cell} = 'import';
471: $c{$cell} = $data;
472: }
473: $i++;
474: }
475: }
476: }
477: $self->constants(\%c);
478: $self->formulas(\%f);
479: $self->calcsheet();
480: #
481: # Store export row in cache
482: my @exportarray=$self->exportrow();
483: my $student = $self->{'name'}.':'.$self->{'domain'};
484: $Exportrows{$student}->{'time'} = time;
485: $Exportrows{$student}->{'data'} = \@exportarray;
486: # save export row
487: $self->save_export_data();
1.9 matthew 488: #
489: $self->save() if ($self->need_to_save());
490: return;
491: }
492:
493: sub set_row_sources {
494: my $self = shift;
495: while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
496: next if ($cell !~ /^A(\d+)/ && $1 > 0);
497: my $row = $1;
498: (undef,$value) = split('__&&&__',$value);
499: $value = 'Default' if (! defined($value));
500: $self->{'row_source'}->{$row} = $value;
501: }
1.1 matthew 502: return;
503: }
504:
505: sub set_row_numbers {
506: my $self = shift;
507: while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
508: next if ($cell !~ /^A(\d+)/);
509: my $row = $1;
510: next if ($row == 0);
511: my ($symb,undef) = split('__&&&__',$formula);
512: $self->{'row_numbers'}->{$symb} = $row;
1.9 matthew 513: $self->{'maxrow'} = $1 if ($1 > $self->{'maxrow'});
1.1 matthew 514: }
515: }
516:
517: sub get_row_number_from_symb {
518: my $self = shift;
519: my ($key) = @_;
520: ($key,undef) = split('__&&&__',$key) if ($key =~ /__&&&__/);
521: return $self->get_row_number_from_key($key);
522: }
523:
524: #############################################
525: #############################################
526:
527: =pod
528:
529: =item &load_cached_export_rows
530:
531: Retrieves and parsers the export rows of the student spreadsheets.
532: These rows are saved in the courses directory in the format:
533:
534: sname:sdom:studentcalc:.time => time
535:
536: sname:sdom:studentcalc => ___=___Adata___;___Bdata___;___Cdata___;___ .....
537:
538: =cut
539:
540: #############################################
541: #############################################
542: sub load_cached_export_rows {
543: %Exportrows = undef;
544: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
545: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
546: $ENV{'course.'.$ENV{'request.course.id'}.'.num'},undef);
547: my %Selected_Assess_Sheet;
548: if ($tmp[0] =~ /^error/) {
549: &Apache::lonnet::logthis('unable to read cached student export rows '.
550: 'for course '.$ENV{'request.course.id'});
551: return;
552: }
553: my %tmp = @tmp;
554: while (my ($key,$sheetdata) = each(%tmp)) {
555: my ($sname,$sdom,$sheettype,$remainder) = split(':',$key);
556: my $student = $sname.':'.$sdom;
557: if ($remainder =~ /\.time/) {
558: $Exportrows{$student}->{'time'} = $sheetdata;
559: } else {
560: $sheetdata =~ s/^___=___//;
561: my @Data = split('___;___',$sheetdata);
562: $Exportrows{$student}->{'data'} = \@Data;
563: }
564: }
565: }
566:
567: #############################################
568: #############################################
569:
570: =pod
571:
572: =item &save_export_data()
573:
574: Writes the export data for this student to the course cache.
575:
576: =cut
577:
578: #############################################
579: #############################################
580: sub save_export_data {
581: my $self = shift;
1.5 matthew 582: return if ($self->temporary());
1.1 matthew 583: my $student = $self->{'name'}.':'.$self->{'domain'};
584: return if (! exists($Exportrows{$student}));
585: return if (! $self->is_default());
586: my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
587: my $timekey = $key.'.time';
588: my $newstore = join('___;___',
589: @{$Exportrows{$student}->{'data'}});
590: $newstore = '___=___'.$newstore;
1.16 ! matthew 591: my $result= &Apache::lonnet::put('nohist_calculatedsheets',
1.1 matthew 592: { $key => $newstore,
593: $timekey => $Exportrows{$student}->{'time'} },
594: $self->{'cdom'},
595: $self->{'cnum'});
596: return;
597: }
598:
599: #############################################
600: #############################################
601:
602: =pod
603:
604: =item &export_data()
605:
606: Returns the export data associated with the spreadsheet. Computes the
607: spreadsheet only if necessary.
608:
609: =cut
610:
611: #############################################
612: #############################################
613: sub export_data {
614: my $self = shift;
615: my $student = $self->{'name'}.':'.$self->{'domain'};
616: if (! exists($Exportrows{$student}) ||
1.15 matthew 617: ! defined($Exportrows{$student}) ||
1.16 ! matthew 618: ! exists($Exportrows{$student}->{'data'}) ||
1.15 matthew 619: ! defined($Exportrows{$student}->{'data'}) ||
1.16 ! matthew 620: ! exists($Exportrows{$student}->{'time'}) ||
! 621: ! defined($Exportrows{$student}->{'time'}) ||
1.1 matthew 622: ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
623: $self->compute();
624: }
625: my @Data = @{$Exportrows{$student}->{'data'}};
626: for (my $i=0; $i<=$#Data;$i++) {
627: $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
628: }
629: return @Data;
630: }
631:
632: 1;
633:
634: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>