Annotation of loncom/interface/spreadsheet/studentcalc.pm, revision 1.14
1.1 matthew 1: #
1.14 ! matthew 2: # $Id: studentcalc.pm,v 1.13 2003/06/23 20:47:00 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: $self->logthis('computing');
415: if (! defined($current_course) ||
416: $current_course ne $ENV{'request.course.id'}) {
417: $current_course = $ENV{'request.course.id'};
418: &clear_package();
419: &initialize_sequence_cache();
420: }
421: $self->initialize_safe_space();
422: my @sequences = @Sequences;
423: if (@sequences < 1) {
424: my ($top,$sequences,$assessments) =
425: &Apache::loncoursedata::get_sequence_assessment_data();
426: if (! defined($top) || ! ref($top)) {
427: &Apache::lonnet::logthis('top is undefined');
428: return;
429: }
430: @sequences = @{$sequences} if (ref($sequences) eq 'ARRAY');
431: }
432: &Apache::assesscalc::initialize_package($self->{'name'},$self->{'domain'});
433: my %f = $self->formulas();
434: #
435: # Process the formulas list -
436: # the formula for the A column of a row is symb__&&__filename
437: my %c = $self->constants();
438: foreach my $seq (@sequences) {
439: next if ($seq->{'num_assess'}<1);
440: foreach my $resource (@{$seq->{'contents'}}) {
441: next if ($resource->{'type'} ne 'assessment');
442: my $rownum = $self->get_row_number_from_key($resource->{'symb'});
443: my $cell = 'A'.$rownum;
444: my $assess_filename = 'Default';
445: if (exists($self->{'row_source'}->{$rownum})) {
446: $assess_filename = $self->{'row_source'}->{$rownum};
447: } else {
448: $self->{'row_source'}->{$rownum} = $assess_filename;
449: }
450: $f{$cell} = $resource->{'symb'}.'__&&&__'.$assess_filename;
451: my $assessSheet = Apache::assesscalc->new($self->{'name'},
452: $self->{'domain'},
453: $assess_filename,
454: $resource->{'symb'});
455: my @exportdata = $assessSheet->export_data();
1.4 matthew 456: if ($assessSheet->blackout()) {
457: $self->blackout(1);
458: $self->{'blackout_rows'}->{$rownum} = 1;
459: }
1.1 matthew 460: #
461: # Be sure not to disturb the formulas in the 'A' column
462: my $data = shift(@exportdata);
463: $c{$cell} = $data if (defined($data));
464: #
465: # Deal with the remaining columns
466: my $i=0;
467: foreach (split(//,'BCDEFGHIJKLMNOPQRSTUVWXYZ')) {
468: my $cell = $_.$rownum;
469: my $data = shift(@exportdata);
470: if (defined($data)) {
471: $f{$cell} = 'import';
472: $c{$cell} = $data;
473: }
474: $i++;
475: }
476: }
477: }
478: $self->constants(\%c);
479: $self->formulas(\%f);
480: $self->calcsheet();
481: #
482: # Store export row in cache
483: my @exportarray=$self->exportrow();
484: my $student = $self->{'name'}.':'.$self->{'domain'};
485: $Exportrows{$student}->{'time'} = time;
486: $Exportrows{$student}->{'data'} = \@exportarray;
487: # save export row
488: $self->save_export_data();
1.9 matthew 489: #
490: $self->save() if ($self->need_to_save());
491: return;
492: }
493:
494: sub set_row_sources {
495: my $self = shift;
496: while (my ($cell,$value) = each(%{$self->{'formulas'}})) {
497: next if ($cell !~ /^A(\d+)/ && $1 > 0);
498: my $row = $1;
499: (undef,$value) = split('__&&&__',$value);
500: $value = 'Default' if (! defined($value));
501: $self->{'row_source'}->{$row} = $value;
502: }
1.1 matthew 503: return;
504: }
505:
506: sub set_row_numbers {
507: my $self = shift;
508: while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
509: next if ($cell !~ /^A(\d+)/);
510: my $row = $1;
511: next if ($row == 0);
512: my ($symb,undef) = split('__&&&__',$formula);
513: $self->{'row_numbers'}->{$symb} = $row;
1.9 matthew 514: $self->{'maxrow'} = $1 if ($1 > $self->{'maxrow'});
1.1 matthew 515: }
516: }
517:
518: sub get_row_number_from_symb {
519: my $self = shift;
520: my ($key) = @_;
521: ($key,undef) = split('__&&&__',$key) if ($key =~ /__&&&__/);
522: return $self->get_row_number_from_key($key);
523: }
524:
525: #############################################
526: #############################################
527:
528: =pod
529:
530: =item &load_cached_export_rows
531:
532: Retrieves and parsers the export rows of the student spreadsheets.
533: These rows are saved in the courses directory in the format:
534:
535: sname:sdom:studentcalc:.time => time
536:
537: sname:sdom:studentcalc => ___=___Adata___;___Bdata___;___Cdata___;___ .....
538:
539: =cut
540:
541: #############################################
542: #############################################
543: sub load_cached_export_rows {
544: %Exportrows = undef;
545: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
546: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
547: $ENV{'course.'.$ENV{'request.course.id'}.'.num'},undef);
548: my %Selected_Assess_Sheet;
549: if ($tmp[0] =~ /^error/) {
550: &Apache::lonnet::logthis('unable to read cached student export rows '.
551: 'for course '.$ENV{'request.course.id'});
552: return;
553: }
554: my %tmp = @tmp;
555: while (my ($key,$sheetdata) = each(%tmp)) {
556: my ($sname,$sdom,$sheettype,$remainder) = split(':',$key);
557: my $student = $sname.':'.$sdom;
558: if ($remainder =~ /\.time/) {
559: $Exportrows{$student}->{'time'} = $sheetdata;
560: } else {
561: $sheetdata =~ s/^___=___//;
562: my @Data = split('___;___',$sheetdata);
563: $Exportrows{$student}->{'data'} = \@Data;
564: }
565: }
566: }
567:
568: #############################################
569: #############################################
570:
571: =pod
572:
573: =item &save_export_data()
574:
575: Writes the export data for this student to the course cache.
576:
577: =cut
578:
579: #############################################
580: #############################################
581: sub save_export_data {
582: my $self = shift;
1.5 matthew 583: return if ($self->temporary());
1.1 matthew 584: my $student = $self->{'name'}.':'.$self->{'domain'};
585: return if (! exists($Exportrows{$student}));
586: return if (! $self->is_default());
587: my $key = join(':',($self->{'name'},$self->{'domain'},'studentcalc')).':';
588: my $timekey = $key.'.time';
589: my $newstore = join('___;___',
590: @{$Exportrows{$student}->{'data'}});
591: $newstore = '___=___'.$newstore;
592: &Apache::lonnet::put('nohist_calculatedsheets',
593: { $key => $newstore,
594: $timekey => $Exportrows{$student}->{'time'} },
595: $self->{'cdom'},
596: $self->{'cnum'});
597: return;
598: }
599:
600: #############################################
601: #############################################
602:
603: =pod
604:
605: =item &export_data()
606:
607: Returns the export data associated with the spreadsheet. Computes the
608: spreadsheet only if necessary.
609:
610: =cut
611:
612: #############################################
613: #############################################
614: sub export_data {
615: my $self = shift;
616: my $student = $self->{'name'}.':'.$self->{'domain'};
617: if (! exists($Exportrows{$student}) ||
618: ! $self->check_expiration_time($Exportrows{$student}->{'time'})) {
619: $self->compute();
620: }
621: my @Data = @{$Exportrows{$student}->{'data'}};
622: for (my $i=0; $i<=$#Data;$i++) {
623: $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
624: }
625: return @Data;
626: }
627:
628: 1;
629:
630: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>