Annotation of loncom/interface/spreadsheet/assesscalc.pm, revision 1.2
1.1 matthew 1: #
1.2 ! matthew 2: # $Id: assesscalc.pm,v 1.1 2003/05/16 20:55:11 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: assesscalc
34:
35: =head1 SYNOPSIS
36:
37: =head1 DESCRIPTION
38:
39: =cut
40:
41: ###################################################
42: ### AssessSheet ###
43: ###################################################
44: package Apache::assesscalc;
45:
46: use strict;
47: use Apache::Constants qw(:common :http);
48: use Apache::lonnet;
1.2 ! matthew 49: use Apache::loncommon;
1.1 matthew 50: use Apache::Spreadsheet;
51: use HTML::Entities();
52: use Spreadsheet::WriteExcel;
53: use GDBM_File;
54: use Time::HiRes;
55:
56: @Apache::assesscalc::ISA = ('Apache::Spreadsheet');
57:
58: ########################################################
59: ########################################################
60:
61: =pod
62:
63: =head2 Package Variables
64:
65: =over 4
66:
67: =item %Exportrows
68:
69: =item $current_name
70:
71: =item $current_domain
72:
73: =item $current_course
74:
75: =item %parmhash
76:
77: =item %nice_parameter_name
78:
79: =item %useropt
80:
81: =item %courseopt
82:
83: =back
84:
85: =cut
86:
87: ########################################################
88: ########################################################
89:
90: my %Exportrows;
91:
92: my $current_name;
93: my $current_domain;
94: my $current_course;
95:
96: my %parmhash;
97: my %nice_parameter_name;
98:
99: my %useropt;
100: my %courseopt;
101:
102: ########################################################
103: ########################################################
104:
105: =pod
106:
107: =head2 Package Subroutines
108:
109: =item &clear_package()
110:
111: Reset all package variables.
112:
113: =cut
114:
115: ########################################################
116: ########################################################
117: sub clear_package {
118: undef(%Exportrows);
119: undef($current_name);
120: undef($current_domain);
121: undef($current_course);
122: undef(%useropt);
123: undef(%courseopt);
124: }
125:
126: ########################################################
127: ########################################################
128:
129: =pod
130:
131: =item &initialize_package()
132:
133: =cut
134:
135: ########################################################
136: ########################################################
137: sub initialize_package {
138: my ($sname,$sdomain) = @_;
139: $current_course = $ENV{'request.course.id'};
140: $current_name = $sname;
141: $current_domain = $sdomain;
142: undef(%courseopt);
143: &load_cached_export_rows();
144: &load_parameter_caches();
145: }
146:
147: ########################################################
148: ########################################################
149:
150: =pod
151:
152: =item &load_parameter_caches()
153:
154: =cut
155:
156: ########################################################
157: ########################################################
158: sub load_parameter_caches {
159: my $userprefix = $current_name.':'.$current_domain.'_';
160: $userprefix =~ s/:/_/g;
161: #
162: # Course Parameters Cache
163: if (! %courseopt) {
164: &Apache::lonnet::logthis("loading course options");
165: $current_course = $ENV{'request.course.id'};
166: undef(%courseopt);
167: if (! defined($current_name) || ! defined($current_domain)) {
168: &Apache::lonnet::logthis('bad call to setup_parameter_caches');
169: return;
170: }
171: my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
172: my $id = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
173: my %Tmp = &Apache::lonnet::dump('resourcedata',$dom,$id);
174: while (my ($name,$value) = each(%Tmp)) {
175: $courseopt{$userprefix.$name}=$value;
176: }
177: }
178: if (! %useropt) {
179: my %Tmp = &Apache::lonnet::dump('resourcedata',
180: $current_domain,$current_name);
181: while (my ($name,$value) = each(%Tmp)) {
182: if ($name =~ /^error: 2/ || $name =~ /no such file/) {
183: undef(%useropt);
184: last;
185: }
186: $useropt{$userprefix.$name}=$value;
187: }
188: }
189: }
190:
191: ########################################################
192: ########################################################
193:
194: =pod
195:
196: =head2 assesscalc object methods
197:
198: =cut
199:
200: ########################################################
201: ########################################################
202:
203: sub ensure_current_parameter_caches {
204: my $self = shift;
205: if (! defined($current_course) ||
206: $current_course ne $ENV{'request.course.id'} ) {
207: $current_course = $ENV{'request.course.id'};
208: undef(%courseopt);
209: }
210: if (! defined($current_name) || $current_name ne $self->{'name'} ||
211: ! defined($current_domain) || $current_domain ne $self->{'domain'}) {
212: $current_domain = $self->{'domain'};
213: $current_name = $self->{'name'};
214: undef(%useropt);
215: }
216: &load_parameter_caches();
217: }
218:
219: ##################################################
220: ##################################################
221:
222: =pod
223:
224: =item &parmval()
225:
226: Determine the value of a parameter.
227:
228: Inputs: $what, the parameter needed, $symb, $uname, $udom, $csec
229:
230: Returns: The value of a parameter, or '' if none.
231:
232: This function cascades through the possible levels searching for a value for
233: a parameter. The levels are checked in the following order:
234: user, course (at section level and course level), map, and lonnet::metadata.
235: This function uses %parmhash, which must be tied prior to calling it.
236: This function also requires %courseopt and %useropt to be initialized for
237: this user and course.
238:
239: =cut
240:
241: ##################################################
242: ##################################################
243: sub parmval {
244: my $self = shift;
245: my ($what,$symb,$uname,$udom,$csec)=@_;
246: $uname = $self->{'name'} if (! defined($uname));
247: $udom = $self->{'domain'} if (! defined($udom));
248: $csec = $self->{'section'} if (! defined($csec));
249: $symb = $self->{'symb'} if (! defined($symb));
250: #
251: my $result='';
252: #
253: # This should be a
254: my ($mapname,$id,$fn)=split(/___/,$symb);
255: # Cascading lookup scheme
256: my $rwhat=$what;
257: $what =~ s/^parameter\_//;
258: $what =~ s/\_([^\_]+)$/\.$1/;
259: #
260: my $symbparm = $symb.'.'.$what;
261: my $mapparm = $mapname.'___(all).'.$what;
262: my $usercourseprefix = $uname.'_'.$udom.'_'.$self->{'cid'};
263: #
264: my $seclevel = $usercourseprefix.'.['.$csec.'].'.$what;
265: my $seclevelr = $usercourseprefix.'.['.$csec.'].'.$symbparm;
266: my $seclevelm = $usercourseprefix.'.['.$csec.'].'.$mapparm;
267: #
268: my $courselevel = $usercourseprefix.'.'.$what;
269: my $courselevelr = $usercourseprefix.'.'.$symbparm;
270: my $courselevelm = $usercourseprefix.'.'.$mapparm;
271: # check user
272: if (defined($uname)) {
273: return $useropt{$courselevelr} if (defined($useropt{$courselevelr}));
274: return $useropt{$courselevelm} if (defined($useropt{$courselevelm}));
275: return $useropt{$courselevel} if (defined($useropt{$courselevel}));
276: }
277: # check section
278: if (defined($csec)) {
279: return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
280: return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
281: return $courseopt{$seclevel} if (defined($courseopt{$seclevel}));
282: }
283: #
284: # check course
285: return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
286: return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
287: return $courseopt{$courselevel} if (defined($courseopt{$courselevel}));
288: # check map parms
289: my $thisparm = $parmhash{$symbparm};
290: return $thisparm if (defined($thisparm));
291: # check default
292: $thisparm = &Apache::lonnet::metadata($fn,$rwhat.'.default');
293: return $thisparm if (defined($thisparm));
294: #
295: # Cascade Up
296: my $space=$what;
297: $space=~s/\.\w+$//;
298: if ($space ne '0') {
299: my @parts=split(/_/,$space);
300: my $id=pop(@parts);
301: my $part=join('_',@parts);
302: if ($part eq '') { $part='0'; }
303: my $newwhat=$rwhat;
304: $newwhat=~s/\Q$space\E/$part/;
305: my $partgeneral=$self->parmval($newwhat,$symb,$uname,$udom,$csec);
306: if (defined($partgeneral)) { return $partgeneral; }
307: }
308: #nothing defined
309: return '';
310: }
311:
312: sub get_title {
313: my $self = shift;
314: my $title;
315: if (($self->{'usymb'} eq '_feedback') ||
316: ($self->{'usymb'} eq '_evaluation') ||
317: ($self->{'usymb'} eq '_discussion') ||
318: ($self->{'usymb'} eq '_tutoring')) {
319: $title = $self->{'usymb'};
320: $title =~ s/^_//;
321: $title = '<h1>'.ucfirst($title)."</h1>\n";
322: } else {
323: $title = '<h1>'.&Apache::lonnet::gettitle($self->{'symb'})."</h1>\n";
324: }
1.2 ! matthew 325: # Look up the users identifying information
! 326: # Get the users information
! 327: my %userenv = &Apache::loncoursedata::GetUserName($self->{'name'},
! 328: $self->{'domain'});
! 329: my $name =
! 330: join(' ',@userenv{'firstname','middlename','lastname','generation'});
! 331: $name =~ s/\s+$//;
! 332: $title .= '<h2>'.$name.', '.
! 333: &Apache::loncommon::aboutmewrapper($self->{'name'}.'@'.$self->{'domain'},
! 334: $self->{'name'},$self->{'domain'}).
! 335: "</h2>\n";
1.1 matthew 336: $title .= '<h3>'.localtime(time).'</h3>';
337: #
338: return $title;
339: }
340:
341: sub parent_link {
342: my $self = shift;
343: my $link .= '<p><a href="/adm/studentcalc?'.
344: 'sname='.$self->{'name'}.
345: '&sdomain='.$self->{'domain'}.'">'.
346: 'Student level sheet</a></p>'."\n";
347: return $link;
348: }
349:
350: sub outsheet_html {
351: my $self = shift;
352: my ($r) = @_;
353: ###################################
354: # Determine table structure
355: ###################################
356: my $num_uneditable = 1;
357: my $num_left = 52-$num_uneditable;
358: my $tableheader =<<"END";
359: <table border="2">
360: <tr>
361: <th colspan="2" rowspan="2"><font size="+2">Assessment</font></th>
362: <td bgcolor="#FFDDDD" colspan="$num_uneditable"> </td>
363: <td colspan="$num_left">
364: <b><font size="+1">Calculations</font></b></td>
365: </tr><tr>
366: END
367: my $label_num = 0;
368: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
369: if ($label_num<$num_uneditable) {
370: $tableheader .= '<td bgcolor="#FFDDDD">';
371: } else {
372: $tableheader .= '<td>';
373: }
374: $tableheader .= "<b><font size=+1>$_</font></b></td>";
375: $label_num++;
376: }
377: $tableheader.="</tr>\n";
378: #
379: $r->print($tableheader);
380: #
381: # Print out template row
382: $r->print('<tr><td>Template</td><td> </td>'.
383: $self->html_template_row($num_uneditable)."</tr>\n");
384: #
385: # Print out summary/export row
386: $r->print('<tr><td>Export</td><td>0</td>'.
387: $self->html_export_row()."</tr>\n");
388: #
389: # Prepare to output rows
390: $tableheader =<<"END";
391: <table border="2">
392: <tr><th>row</th><th>Item</th>
393: END
394: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
395: if ($label_num<$num_uneditable) {
396: $tableheader.='<th bgcolor="#FFDDDD">';
397: } else {
398: $tableheader.='<th>';
399: }
400: $tableheader.="<b><font size=+1>$_</font></b></th>";
401: }
402: #
403: my $num_output = 0;
404: foreach my $rownum ($self->rows()) {
405: if ($num_output++ % 50 == 0) {
406: $r->print("</table>\n".$tableheader);
407: }
408: $r->print('<tr><td>'.$rownum.'</td>'.
409: $self->assess_html_row($num_uneditable,$rownum)."</tr>\n");
410: }
411: $r->print("</table>\n");
412: return;
413: }
414:
415: sub assess_html_row {
416: my $self = shift();
417: my ($num_uneditable,$row) = @_;
418: my $requester_is_student = ($ENV{'request.role'} =~ /^st\./);
419: my $parameter_name = $self->{'formulas'}->{'A'.$row};
420: my @rowdata = $self->get_row($row);
421: my $num_cols_output = 0;
422: my $row_html;
423: if (exists($nice_parameter_name{$parameter_name})) {
424: my $name = $nice_parameter_name{$parameter_name};
425: $name =~ s/ /\ /g;
426: $row_html .= '<td>'.$name.'<br />'.$parameter_name.'</td>';
427: } else {
428: $row_html .= '<td>'.$parameter_name.'</td>';
429: }
430: foreach my $cell (@rowdata) {
431: if ($requester_is_student ||
432: $num_cols_output++ < $num_uneditable) {
433: $row_html .= '<td bgcolor="#FFDDDD">';
434: $row_html .= &Apache::Spreadsheet::html_uneditable_cell($cell,'#FFDDDD');
435: } else {
436: $row_html .= '<td bgcolor="#EOFFDD">';
437: $row_html .= &Apache::Spreadsheet::html_editable_cell($cell,'#E0FFDD');
438: }
439: $row_html .= '</td>';
440: }
441: return $row_html;
442: }
443:
444: sub outsheet_csv {
445: my $self = shift;
446: my ($r)=@_;
447: }
448:
449: sub outsheet_excel {
450: my $self = shift;
451: my ($r)=@_;
452: }
453:
454: sub display {
455: my $self = shift;
456: my ($r) = @_;
457: $self->compute();
458: $self->outsheet_html($r);
459: }
460:
461: sub compute {
462: my $self = shift;
463: $self->logthis('computing');
464: $self->initialize_safe_space();
465: #
466: # Definitions
467: undef(%nice_parameter_name);
468: my %parameters; # holds underscored parameters by name
469: #
470: # Get the metadata fields and determine their proper names
471: my ($symap,$syid,$srcf)=split(/___/,$self->{'symb'});
472: my @Metadata = split(/\,/,&Apache::lonnet::metadata($srcf,'keys'));
473: foreach my $parm (@Metadata) {
474: next if ($parm !~ /^(resource\.|stores|parameter)_/);
475: my $cleaned_name = $parm;
476: $cleaned_name =~ s/^resource\./stores_/;
477: $cleaned_name =~ s/\./_/g;
478: my $display = &Apache::lonnet::metadata($srcf,
479: $cleaned_name.'.display');
480: if (! $display) {
481: $display .= &Apache::lonnet::metadata($srcf,$cleaned_name.'.name');
482: }
483: $parameters{$cleaned_name}++;
484: $nice_parameter_name{$cleaned_name} = $display;
485: }
486: #
487: # Get the values of the metadata fields
488: $self->ensure_current_parameter_caches();
489: my $filename = $self->{'coursefilename'}.'_parms.db';
490: if (tie(%parmhash,'GDBM_File',
491: $self->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
492: foreach my $parmname (keys(%parameters)) {
493: my $value = $self->parmval($parmname);
494: $parameters{$parmname} =$value;
495: }
496: untie(%parmhash);
497: } else {
498: $self->logthis('unable to tie '.$filename);
499: }
500: #
501: # Clean out unnecessary parameters
502: foreach (keys(%parameters)) {
503: delete($parameters{$_}) if (! /(resource\.|stores_|parameter_)/);
504: }
505: #
506: # Get the students performance data
507: my %student_parameters =
508: &Apache::loncoursedata::get_current_state($self->{'name'},
509: $self->{'domain'},
510: $self->{'symb'},
511: $self->{'cid'});
512: while (my ($parm,$value) = each(%student_parameters)) {
513: $parm =~ s/^resource\./stores_/;
514: $parm =~ s/\./_/g;
515: $parameters{$parm} = $value;
516: }
517: #
518: # Set up the formulas and parameter values
519: my %f=$self->formulas();
520: my %c;
521: #
522: while (my ($parm,$value) = each(%parameters)) {
523: my $cell = 'A'.$self->get_row_number_from_key($parm);
524: $f{$cell} = $parm;
525: $c{$parm} = $value;
526: }
527: $self->formulas(%f);
528: $self->constants(%c);
529: $self->calcsheet();
530: #
531: # Store export row in cache
532: my @exportarray = $self->exportrow();
533: $Exportrows{$self->{'symb'}}->{'time'} = time;
534: $Exportrows{$self->{'symb'}}->{$self->{'filename'}} = \@exportarray;
535: #
536: # Save the export data
537: $self->save_export_data();
538: return;
539: }
540:
541: ##
542: ## sett overrides Spreadsheet::sett
543: ##
544: sub sett {
545: my $self = shift;
546: my %t=();
547: #
548: # Deal with the template row by copying the template formulas into each
549: # row.
550: foreach my $col ($self->template_cells()) {
551: next if ($col=~/^A/);
552: foreach my $row ($self->rows()) {
553: # Get the name of this cell
554: my $cell=$col.$row;
555: # Grab the template declaration
556: $t{$cell}=$self->formula('template_'.$col);
557: # Replace '#' with the row number
558: $t{$cell}=~s/\#/$row/g;
559: # Replace '....' with ','
560: $t{$cell}=~s/\.\.+/\,/g;
561: # Replace 'A0' with the value from 'A0'
562: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
563: # Replace parameters
564: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
565: }
566: }
567: #
568: # Deal with the cells which have formulas
569: while (my ($cell,$formula) = each(%{$self->{'formulas'}})) {
570: next if ($cell =~ /template_/);
571: if ($cell =~ /^A/ && $cell ne 'A0') {
572: if ($formula !~ /^\!/) {
573: $t{$cell}=$self->{'constants'}->{$formula};
574: }
575: } else {
576: $t{$cell}=$formula;
577: $t{$cell}=~s/\.\.+/\,/g;
578: $t{$cell}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
579: $t{$cell}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.$self->expandnamed($2)/ge;
580: }
581: }
582: # Put %t into the safe space
583: %{$self->{'safe'}->varglob('t')}=%t;
584: }
585:
586:
587: ########################################################
588: ########################################################
589:
590: =pod
591:
592: =item &load_cached_export_rows()
593:
594: Retrieves and parsers the export rows of the assessment spreadsheets.
595: These rows are saved in the students directory in the format:
596:
597: sname:sdom:assesscalc:symb.time => time
598:
599: sname:sdom:assesscalc:symb => filename___=___Adata___;___Bdata___;___ ...
600:
601: =cut
602:
603: ########################################################
604: ########################################################
605: sub load_cached_export_rows {
606: %Exportrows = undef;
607: &Apache::lonnet::logthis("loading cached assess sheets for $current_name $current_domain");
608: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets_'.
609: $ENV{'request.course.id'},
610: $current_domain,$current_name,undef);
611: if ($tmp[0]!~/^error/) {
612: my %tmp = @tmp;
613: my $default_filename = $ENV{'course.'.$ENV{'request.course.id'}.
614: '.spreadsheet_default_assesscalc'};
615: # We only got one key, so we will access it directly.
616: while (my ($key,$sheetdata) = each(%tmp)) {
617: my ($sname,$sdom,$sheettype,$symb) = split(':',$key);
618: if ($symb =~ /\.time$/) {
619: $symb =~ s/\.time$//;
620: $Exportrows{$symb}->{'time'} = $sheetdata;
621: } else {
622: $sheetdata =~ s/^(.*)___=___//;
623: my $filename = $1;
624: $filename = $default_filename if (! defined($filename));
625: my @Data = split('___;___',$sheetdata);
626: $Exportrows{$symb}->{$filename} = \@Data;
627: }
628: }
629: }
630: }
631:
632: #############################################
633: #############################################
634:
635: =pod
636:
637: =item &export_data
638:
639: Returns the export data associated with the spreadsheet. Computes the
640: spreadsheet only if necessary.
641:
642: =cut
643:
644: #############################################
645: #############################################
646: sub export_data {
647: my $self = shift;
648: my $symb = $self->{'symb'};
649: if (! exists($Exportrows{$symb}) || ! defined($Exportrows{$symb}) ||
650: ! $self->check_expiration_time($Exportrows{$symb}->{'time'}) ||
651: ! exists($Exportrows{$symb}->{$self->{'filename'}}) ||
652: ! defined($Exportrows{$symb}->{$self->{'filename'}})) {
653: $self->compute();
654: }
655: my @Data = @{$Exportrows{$symb}->{$self->{'filename'}}};
656: if ($Data[0] =~ /^(.*)___=___/) {
657: $self->{'sheetname'} = $1;
658: $Data[0] =~ s/^(.*)___=___//;
659: }
660: for (my $i=0;$i<$#Data;$i++) {
661: $Data[$i]="'".$Data[$i]."'" if ($Data[$i]=~/\D/ && defined($Data[$i]));
662: }
663: return @Data;
664: }
665:
666: #############################################
667: #############################################
668:
669: =pod
670:
671: =item &save_export_data()
672:
673: Writes the export data for this spreadsheet to the students cache.
674:
675: =cut
676:
677: #############################################
678: #############################################
679: sub save_export_data {
680: my $self = shift;
681: my $student = $self->{'name'}.':'.$self->{'domain'};
682: my $symb = $self->{'symb'};
683: if (! exists($Exportrows{$symb}) ||
684: ! exists($Exportrows{$symb}->{$self->{'filename'}})) {
685: return;
686: }
687: my $key = join(':',($self->{'name'},$self->{'domain'},'assesscalc',$symb));
688: my $timekey = $key.'.time';
689: my $newstore= join('___;___',@{$Exportrows{$symb}->{$self->{'filename'}}});
690: $newstore = $self->{'filename'}.'___=___'.$newstore;
691: my $result = &Apache::lonnet::put
692: ('nohist_calculatedsheets_'.$ENV{'request.course.id'},
693: { $key => $newstore,
694: $timekey => $Exportrows{$symb}->{'time'} },
695: $self->{'domain'},
696: $self->{'name'});
697:
698: return;
699: }
700:
701: 1;
702:
703: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>