File:
[LON-CAPA] /
loncom /
interface /
Attic /
lonspreadsheet.pm
Revision
1.139:
download - view:
text,
annotated -
select for diffs
Tue Nov 12 22:44:28 2002 UTC (21 years, 7 months ago) by
matthew
Branches:
MAIN
CVS tags:
HEAD
Added &gettitle to get the title of a spreadsheet. Changes to the rowlabel
handling to give the map id and the resource id of symbs. Many changes to
&handler to reorganize the output. excel spreadsheets now include the export
row of the spreadsheet (but still not the template row).
Many cosmetic changes.
1: #
2: # $Id: lonspreadsheet.pm,v 1.139 2002/11/12 22:44:28 matthew 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: lonspreadsheet
34:
35: =head1 SYNOPSIS
36:
37: Spreadsheet interface to internal LON-CAPA data
38:
39: =head1 DESCRIPTION
40:
41: Lonspreadsheet provides course coordinators the ability to manage their
42: students grades online. The students are able to view their own grades, but
43: not the grades of their peers. The spreadsheet is highly customizable,
44: offering the ability to use Perl code to manipulate data, as well as many
45: built-in functions.
46:
47: =head2 Functions available to user of lonspreadsheet
48:
49: =over 4
50:
51: =cut
52:
53: package Apache::lonspreadsheet;
54:
55: use strict;
56: use Safe;
57: use Safe::Hole;
58: use Opcode;
59: use Apache::lonnet;
60: use Apache::Constants qw(:common :http);
61: use GDBM_File;
62: use HTML::TokeParser;
63: use Apache::lonhtmlcommon;
64: use Apache::loncoursedata;
65: use Apache::File();
66: use Spreadsheet::WriteExcel;
67:
68: #
69: # Caches for coursewide information
70: #
71: my %Section;
72:
73: #
74: # Caches for previously calculated spreadsheets
75: #
76:
77: my %oldsheets;
78: my %loadedcaches;
79: my %expiredates;
80:
81: #
82: # Cache for stores of an individual user
83: #
84:
85: my $cachedassess;
86: my %cachedstores;
87:
88: #
89: # These cache hashes need to be independent of user, resource and course
90: # (user and course can/should be in the keys)
91: #
92:
93: my %spreadsheets;
94: my %courserdatas;
95: my %userrdatas;
96: my %defaultsheets;
97: my %rowlabel_cache;
98:
99: #
100: # These global hashes are dependent on user, course and resource,
101: # and need to be initialized every time when a sheet is calculated
102: #
103: my %courseopt;
104: my %useropt;
105: my %parmhash;
106:
107: #
108: # Some hashes for stats on timing and performance
109: #
110:
111: my %starttimes;
112: my %usedtimes;
113: my %numbertimes;
114:
115: # Stuff that only the screen handler can know
116:
117: my $includedir;
118: my $tmpdir;
119:
120: # =============================================================================
121: # ===================================== Implements an instance of a spreadsheet
122:
123: ##
124: ## mask - used to reside in the safe space.
125: ##
126: sub mask {
127: my ($lower,$upper)=@_;
128: $upper = $lower if (! defined($upper));
129: #
130: my ($la,$ld) = ($lower=~/([A-Za-z]|\*)(\d+|\*)/);
131: my ($ua,$ud) = ($upper=~/([A-Za-z]|\*)(\d+|\*)/);
132: #
133: my $alpha='';
134: my $num='';
135: #
136: if (($la eq '*') || ($ua eq '*')) {
137: $alpha='[A-Za-z]';
138: } else {
139: if (($la=~/[A-Z]/) && ($ua=~/[A-Z]/) ||
140: ($la=~/[a-z]/) && ($ua=~/[a-z]/)) {
141: $alpha='['.$la.'-'.$ua.']';
142: } else {
143: $alpha='['.$la.'-Za-'.$ua.']';
144: }
145: }
146: if (($ld eq '*') || ($ud eq '*')) {
147: $num='\d+';
148: } else {
149: if (length($ld)!=length($ud)) {
150: $num.='(';
151: foreach ($ld=~m/\d/g) {
152: $num.='['.$_.'-9]';
153: }
154: if (length($ud)-length($ld)>1) {
155: $num.='|\d{'.(length($ld)+1).','.(length($ud)-1).'}';
156: }
157: $num.='|';
158: foreach ($ud=~m/\d/g) {
159: $num.='[0-'.$_.']';
160: }
161: $num.=')';
162: } else {
163: my @lda=($ld=~m/\d/g);
164: my @uda=($ud=~m/\d/g);
165: my $i;
166: my $j=0;
167: my $notdone=1;
168: for ($i=0;($i<=$#lda)&&($notdone);$i++) {
169: if ($lda[$i]==$uda[$i]) {
170: $num.=$lda[$i];
171: $j=$i;
172: } else {
173: $notdone=0;
174: }
175: }
176: if ($j<$#lda-1) {
177: $num.='('.$lda[$j+1];
178: for ($i=$j+2;$i<=$#lda;$i++) {
179: $num.='['.$lda[$i].'-9]';
180: }
181: if ($uda[$j+1]-$lda[$j+1]>1) {
182: $num.='|['.($lda[$j+1]+1).'-'.($uda[$j+1]-1).']\d{'.
183: ($#lda-$j-1).'}';
184: }
185: $num.='|'.$uda[$j+1];
186: for ($i=$j+2;$i<=$#uda;$i++) {
187: $num.='[0-'.$uda[$i].']';
188: }
189: $num.=')';
190: } else {
191: if ($lda[-1]!=$uda[-1]) {
192: $num.='['.$lda[-1].'-'.$uda[-1].']';
193: }
194: }
195: }
196: }
197: return '^'.$alpha.$num."\$";
198: }
199:
200: sub initsheet {
201: my $safeeval = new Safe(shift);
202: my $safehole = new Safe::Hole;
203: $safeeval->permit("entereval");
204: $safeeval->permit(":base_math");
205: $safeeval->permit("sort");
206: $safeeval->deny(":base_io");
207: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
208: $safehole->wrap(\&Apache::lonspreadsheet::mask,$safeeval,'&mask');
209: $safeeval->share('$@');
210: my $code=<<'ENDDEFS';
211: # ---------------------------------------------------- Inside of the safe space
212:
213: #
214: # f: formulas
215: # t: intermediate format (variable references expanded)
216: # v: output values
217: # c: preloaded constants (A-column)
218: # rl: row label
219: # os: other spreadsheets (for student spreadsheet only)
220:
221: undef %sheet_values; # Holds the (computed, final) values for the sheet
222: # This is only written to by &calc, the spreadsheet computation routine.
223: # It is read by many functions
224: undef %t; # Holds the values of the spreadsheet temporarily. Set in &sett,
225: # which does the translation of strings like C5 into the value in C5.
226: # Used in &calc - %t holds the values that are actually eval'd.
227: undef %f; # Holds the formulas for each cell. This is the users
228: # (spreadsheet authors) data for each cell.
229: # set by &setformulas and returned by &getformulas
230: # &setformulas is called by &readsheet, &tmpread, &updateclasssheet,
231: # &updatestudentassesssheet, &loadstudent, &loadcourse
232: # &getformulas is called by &writesheet, &tmpwrite, &updateclasssheet,
233: # &updatestudentassesssheet, &loadstudent, &loadcourse, &loadassessment,
234: undef %c; # Holds the constants for a sheet. In the assessment
235: # sheets, this is the A column. Used in &MINPARM, &MAXPARM, &expandnamed,
236: # &sett, and &setconstants. There is no &getconstants.
237: # &setconstants is called by &loadstudent, &loadcourse, &load assessment,
238: undef @os; # Holds the names of other spreadsheets - this is used to specify
239: # the spreadsheets that are available for the assessment sheet.
240: # Set by &setothersheets. &setothersheets is called by &handler. A
241: # related subroutine is &othersheets.
242: #$errorlog = '';
243:
244: $maxrow = 0;
245: $sheettype = '';
246:
247: # filename/reference of the sheet
248: $filename = '';
249:
250: # user data
251: $uname = '';
252: $uhome = '';
253: $udom = '';
254:
255: # course data
256:
257: $csec = '';
258: $chome= '';
259: $cnum = '';
260: $cdom = '';
261: $cid = '';
262: $coursefilename = '';
263:
264: # symb
265:
266: $usymb = '';
267:
268: # error messages
269: $errormsg = '';
270:
271:
272: #-------------------------------------------------------
273:
274: =item UWCALC(hashname,modules,units,date)
275:
276: returns the proportion of the module
277: weights not previously completed by the student.
278:
279: =over 4
280:
281: =item hashname
282:
283: name of the hash the module dates have been inserted into
284:
285: =item modules
286:
287: reference to a cell which contains a comma deliminated list of modules
288: covered by the assignment.
289:
290: =item units
291:
292: reference to a cell which contains a comma deliminated list of module
293: weights with respect to the assignment
294:
295: =item date
296:
297: reference to a cell which contains the date the assignment was completed.
298:
299: =back
300:
301: =cut
302:
303: #-------------------------------------------------------
304: sub UWCALC {
305: my ($hashname,$modules,$units,$date) = @_;
306: my @Modules = split(/,/,$modules);
307: my @Units = split(/,/,$units);
308: my $total_weight;
309: foreach (@Units) {
310: $total_weight += $_;
311: }
312: my $usum=0;
313: for (my $i=0; $i<=$#Modules; $i++) {
314: if (&HASH($hashname,$Modules[$i]) eq $date) {
315: $usum += $Units[$i];
316: }
317: }
318: return $usum/$total_weight;
319: }
320:
321: #-------------------------------------------------------
322:
323: =item CDLSUM(list)
324:
325: returns the sum of the elements in a cell which contains
326: a Comma Deliminate List of numerical values.
327: 'list' is a reference to a cell which contains a comma deliminated list.
328:
329: =cut
330:
331: #-------------------------------------------------------
332: sub CDLSUM {
333: my ($list)=@_;
334: my $sum;
335: foreach (split/,/,$list) {
336: $sum += $_;
337: }
338: return $sum;
339: }
340:
341: #-------------------------------------------------------
342:
343: =item CDLITEM(list,index)
344:
345: returns the item at 'index' in a Comma Deliminated List.
346:
347: =over 4
348:
349: =item list
350:
351: reference to a cell which contains a comma deliminated list.
352:
353: =item index
354:
355: the Perl index of the item requested (first element in list has
356: an index of 0)
357:
358: =back
359:
360: =cut
361:
362: #-------------------------------------------------------
363: sub CDLITEM {
364: my ($list,$index)=@_;
365: my @Temp = split/,/,$list;
366: return $Temp[$index];
367: }
368:
369: #-------------------------------------------------------
370:
371: =item CDLHASH(name,key,value)
372:
373: loads a comma deliminated list of keys into
374: the hash 'name', all with a value of 'value'.
375:
376: =over 4
377:
378: =item name
379:
380: name of the hash.
381:
382: =item key
383:
384: (a pointer to) a comma deliminated list of keys.
385:
386: =item value
387:
388: a single value to be entered for each key.
389:
390: =back
391:
392: =cut
393:
394: #-------------------------------------------------------
395: sub CDLHASH {
396: my ($name,$key,$value)=@_;
397: my @Keys;
398: my @Values;
399: # Check to see if we have multiple $key values
400: if ($key =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
401: my $keymask = &mask($key);
402: # Assume the keys are addresses
403: my @Temp = grep /$keymask/,keys(%sheet_values);
404: @Keys = $sheet_values{@Temp};
405: } else {
406: $Keys[0]= $key;
407: }
408: my @Temp;
409: foreach $key (@Keys) {
410: @Temp = (@Temp, split/,/,$key);
411: }
412: @Keys = @Temp;
413: if ($value =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
414: my $valmask = &mask($value);
415: my @Temp = grep /$valmask/,keys(%sheet_values);
416: @Values =$sheet_values{@Temp};
417: } else {
418: $Values[0]= $value;
419: }
420: $value = $Values[0];
421: # Add values to hash
422: for (my $i = 0; $i<=$#Keys; $i++) {
423: my $key = $Keys[$i];
424: if (! exists ($hashes{$name}->{$key})) {
425: $hashes{$name}->{$key}->[0]=$value;
426: } else {
427: my @Temp = sort(@{$hashes{$name}->{$key}},$value);
428: $hashes{$name}->{$key} = \@Temp;
429: }
430: }
431: return "hash '$name' updated";
432: }
433:
434: #-------------------------------------------------------
435:
436: =item GETHASH(name,key,index)
437:
438: returns the element in hash 'name'
439: reference by the key 'key', at index 'index' in the values list.
440:
441: =cut
442:
443: #-------------------------------------------------------
444: sub GETHASH {
445: my ($name,$key,$index)=@_;
446: if (! defined($index)) {
447: $index = 0;
448: }
449: if ($key =~ /^[A-z]\d+$/) {
450: $key = $sheet_values{$key};
451: }
452: return $hashes{$name}->{$key}->[$index];
453: }
454:
455: #-------------------------------------------------------
456:
457: =item CLEARHASH(name)
458:
459: clears all the values from the hash 'name'
460:
461: =item CLEARHASH(name,key)
462:
463: clears all the values from the hash 'name' associated with the given key.
464:
465: =cut
466:
467: #-------------------------------------------------------
468: sub CLEARHASH {
469: my ($name,$key)=@_;
470: if (defined($key)) {
471: if (exists($hashes{$name}->{$key})) {
472: $hashes{$name}->{$key}=undef;
473: return "hash '$name' key '$key' cleared";
474: }
475: } else {
476: if (exists($hashes{$name})) {
477: $hashes{$name}=undef;
478: return "hash '$name' cleared";
479: }
480: }
481: return "Error in clearing hash";
482: }
483:
484: #-------------------------------------------------------
485:
486: =item HASH(name,key,value)
487:
488: loads values into an internal hash. If a key
489: already has a value associated with it, the values are sorted numerically.
490:
491: =item HASH(name,key)
492:
493: returns the 0th value in the hash 'name' associated with 'key'.
494:
495: =cut
496:
497: #-------------------------------------------------------
498: sub HASH {
499: my ($name,$key,$value)=@_;
500: my @Keys;
501: undef @Keys;
502: my @Values;
503: # Check to see if we have multiple $key values
504: if ($key =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
505: my $keymask = &mask($key);
506: # Assume the keys are addresses
507: my @Temp = grep /$keymask/,keys(%sheet_values);
508: @Keys = $sheet_values{@Temp};
509: } else {
510: $Keys[0]= $key;
511: }
512: # If $value is empty, return the first value associated
513: # with the first key.
514: if (! $value) {
515: return $hashes{$name}->{$Keys[0]}->[0];
516: }
517: # Check to see if we have multiple $value(s)
518: if ($value =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
519: my $valmask = &mask($value);
520: my @Temp = grep /$valmask/,keys(%sheet_values);
521: @Values =$sheet_values{@Temp};
522: } else {
523: $Values[0]= $value;
524: }
525: # Add values to hash
526: for (my $i = 0; $i<=$#Keys; $i++) {
527: my $key = $Keys[$i];
528: my $value = ($i<=$#Values ? $Values[$i] : $Values[0]);
529: if (! exists ($hashes{$name}->{$key})) {
530: $hashes{$name}->{$key}->[0]=$value;
531: } else {
532: my @Temp = sort(@{$hashes{$name}->{$key}},$value);
533: $hashes{$name}->{$key} = \@Temp;
534: }
535: }
536: return $Values[-1];
537: }
538:
539: #-------------------------------------------------------
540:
541: =item NUM(range)
542:
543: returns the number of items in the range.
544:
545: =cut
546:
547: #-------------------------------------------------------
548: sub NUM {
549: my $mask=mask(@_);
550: my $num= $#{@{grep(/$mask/,keys(%sheet_values))}}+1;
551: return $num;
552: }
553:
554: sub BIN {
555: my ($low,$high,$lower,$upper)=@_;
556: my $mask=mask($lower,$upper);
557: my $num=0;
558: foreach (grep /$mask/,keys(%sheet_values)) {
559: if (($sheet_values{$_}>=$low) && ($sheet_values{$_}<=$high)) {
560: $num++;
561: }
562: }
563: return $num;
564: }
565:
566:
567: #-------------------------------------------------------
568:
569: =item SUM(range)
570:
571: returns the sum of items in the range.
572:
573: =cut
574:
575: #-------------------------------------------------------
576: sub SUM {
577: my $mask=mask(@_);
578: my $sum=0;
579: foreach (grep /$mask/,keys(%sheet_values)) {
580: $sum+=$sheet_values{$_};
581: }
582: return $sum;
583: }
584:
585: #-------------------------------------------------------
586:
587: =item MEAN(range)
588:
589: compute the average of the items in the range.
590:
591: =cut
592:
593: #-------------------------------------------------------
594: sub MEAN {
595: my $mask=mask(@_);
596: my $sum=0;
597: my $num=0;
598: foreach (grep /$mask/,keys(%sheet_values)) {
599: $sum+=$sheet_values{$_};
600: $num++;
601: }
602: if ($num) {
603: return $sum/$num;
604: } else {
605: return undef;
606: }
607: }
608:
609: #-------------------------------------------------------
610:
611: =item STDDEV(range)
612:
613: compute the standard deviation of the items in the range.
614:
615: =cut
616:
617: #-------------------------------------------------------
618: sub STDDEV {
619: my $mask=mask(@_);
620: my $sum=0; my $num=0;
621: foreach (grep /$mask/,keys(%sheet_values)) {
622: $sum+=$sheet_values{$_};
623: $num++;
624: }
625: unless ($num>1) { return undef; }
626: my $mean=$sum/$num;
627: $sum=0;
628: foreach (grep /$mask/,keys(%sheet_values)) {
629: $sum+=($sheet_values{$_}-$mean)**2;
630: }
631: return sqrt($sum/($num-1));
632: }
633:
634: #-------------------------------------------------------
635:
636: =item PROD(range)
637:
638: compute the product of the items in the range.
639:
640: =cut
641:
642: #-------------------------------------------------------
643: sub PROD {
644: my $mask=mask(@_);
645: my $prod=1;
646: foreach (grep /$mask/,keys(%sheet_values)) {
647: $prod*=$sheet_values{$_};
648: }
649: return $prod;
650: }
651:
652: #-------------------------------------------------------
653:
654: =item MAX(range)
655:
656: compute the maximum of the items in the range.
657:
658: =cut
659:
660: #-------------------------------------------------------
661: sub MAX {
662: my $mask=mask(@_);
663: my $max='-';
664: foreach (grep /$mask/,keys(%sheet_values)) {
665: unless ($max) { $max=$sheet_values{$_}; }
666: if (($sheet_values{$_}>$max) || ($max eq '-')) { $max=$sheet_values{$_}; }
667: }
668: return $max;
669: }
670:
671: #-------------------------------------------------------
672:
673: =item MIN(range)
674:
675: compute the minimum of the items in the range.
676:
677: =cut
678:
679: #-------------------------------------------------------
680: sub MIN {
681: my $mask=mask(@_);
682: my $min='-';
683: foreach (grep /$mask/,keys(%sheet_values)) {
684: unless ($max) { $max=$sheet_values{$_}; }
685: if (($sheet_values{$_}<$min) || ($min eq '-')) {
686: $min=$sheet_values{$_};
687: }
688: }
689: return $min;
690: }
691:
692: #-------------------------------------------------------
693:
694: =item SUMMAX(num,lower,upper)
695:
696: compute the sum of the largest 'num' items in the range from
697: 'lower' to 'upper'
698:
699: =cut
700:
701: #-------------------------------------------------------
702: sub SUMMAX {
703: my ($num,$lower,$upper)=@_;
704: my $mask=mask($lower,$upper);
705: my @inside=();
706: foreach (grep /$mask/,keys(%sheet_values)) {
707: push (@inside,$sheet_values{$_});
708: }
709: @inside=sort(@inside);
710: my $sum=0; my $i;
711: for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) {
712: $sum+=$inside[$i];
713: }
714: return $sum;
715: }
716:
717: #-------------------------------------------------------
718:
719: =item SUMMIN(num,lower,upper)
720:
721: compute the sum of the smallest 'num' items in the range from
722: 'lower' to 'upper'
723:
724: =cut
725:
726: #-------------------------------------------------------
727: sub SUMMIN {
728: my ($num,$lower,$upper)=@_;
729: my $mask=mask($lower,$upper);
730: my @inside=();
731: foreach (grep /$mask/,keys(%sheet_values)) {
732: $inside[$#inside+1]=$sheet_values{$_};
733: }
734: @inside=sort(@inside);
735: my $sum=0; my $i;
736: for ($i=0;(($i<$num) && ($i<=$#inside));$i++) {
737: $sum+=$inside[$i];
738: }
739: return $sum;
740: }
741:
742: #-------------------------------------------------------
743:
744: =item MINPARM(parametername)
745:
746: Returns the minimum value of the parameters matching the parametername.
747: parametername should be a string such as 'duedate'.
748:
749: =cut
750:
751: #-------------------------------------------------------
752: sub MINPARM {
753: my ($expression) = @_;
754: my $min = undef;
755: study($expression);
756: foreach $parameter (keys(%c)) {
757: next if ($parameter !~ /$expression/);
758: if ((! defined($min)) || ($min > $c{$parameter})) {
759: $min = $c{$parameter}
760: }
761: }
762: return $min;
763: }
764:
765: #-------------------------------------------------------
766:
767: =item MAXPARM(parametername)
768:
769: Returns the maximum value of the parameters matching the input parameter name.
770: parametername should be a string such as 'duedate'.
771:
772: =cut
773:
774: #-------------------------------------------------------
775: sub MAXPARM {
776: my ($expression) = @_;
777: my $max = undef;
778: study($expression);
779: foreach $parameter (keys(%c)) {
780: next if ($parameter !~ /$expression/);
781: if ((! defined($min)) || ($max < $c{$parameter})) {
782: $max = $c{$parameter}
783: }
784: }
785: return $max;
786: }
787:
788: #--------------------------------------------------------
789: sub expandnamed {
790: my $expression=shift;
791: if ($expression=~/^\&/) {
792: my ($func,$var,$formula)=($expression=~/^\&(\w+)\(([^\;]+)\;(.*)\)/);
793: my @vars=split(/\W+/,$formula);
794: my %values=();
795: undef %values;
796: foreach ( @vars ) {
797: my $varname=$_;
798: if ($varname=~/\D/) {
799: $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge;
800: $varname=~s/$var/\(\\w\+\)/g;
801: foreach (keys(%c)) {
802: if ($_=~/$varname/) {
803: $values{$1}=1;
804: }
805: }
806: }
807: }
808: if ($func eq 'EXPANDSUM') {
809: my $result='';
810: foreach (keys(%values)) {
811: my $thissum=$formula;
812: $thissum=~s/$var/$_/g;
813: $result.=$thissum.'+';
814: }
815: $result=~s/\+$//;
816: return $result;
817: } else {
818: return 0;
819: }
820: } else {
821: # it is not a function, so it is a parameter name
822: # We should do the following:
823: # 1. Take the list of parameter names
824: # 2. look through the list for ones that match the parameter we want
825: # 3. If there are no collisions, return the one that matches
826: # 4. If there is a collision, return 'bad parameter name error'
827: my $returnvalue = '';
828: my @matches = ();
829: $#matches = -1;
830: study $expression;
831: foreach $parameter (keys(%c)) {
832: push @matches,$parameter if ($parameter =~ /$expression/);
833: }
834: if (scalar(@matches) == 0) {
835: $returnvalue = 'unmatched parameter: '.$parameter;
836: } elsif (scalar(@matches) == 1) {
837: $returnvalue = '$c{\''.$matches[0].'\'}';
838: } elsif (scalar(@matches) > 0) {
839: # more than one match. Look for a concise one
840: $returnvalue = "'non-unique parameter name : $expression'";
841: foreach (@matches) {
842: if (/^$expression$/) {
843: $returnvalue = '$c{\''.$_.'\'}';
844: }
845: }
846: } else {
847: # There was a negative number of matches, which indicates
848: # something is wrong with reality. Better warn the user.
849: $returnvalue = 'bizzare parameter: '.$parameter;
850: }
851: return $returnvalue;
852: }
853: }
854:
855: sub sett {
856: %t=();
857: my $pattern='';
858: if ($sheettype eq 'assesscalc') {
859: $pattern='A';
860: } else {
861: $pattern='[A-Z]';
862: }
863: # Deal with the template row
864: foreach (keys(%f)) {
865: next if ($_!~/template\_(\w)/);
866: my $col=$1;
867: next if ($col=~/^$pattern/);
868: foreach (keys(%f)) {
869: next if ($_!~/A(\d+)/);
870: my $trow=$1;
871: next if (! $trow);
872: # Get the name of this cell
873: my $lb=$col.$trow;
874: # Grab the template declaration
875: $t{$lb}=$f{'template_'.$col};
876: # Replace '#' with the row number
877: $t{$lb}=~s/\#/$trow/g;
878: # Replace '....' with ','
879: $t{$lb}=~s/\.\.+/\,/g;
880: # Replace 'A0' with the value from 'A0'
881: $t{$lb}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
882: # Replace parameters
883: $t{$lb}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
884: }
885: }
886: # Deal with the normal cells
887: foreach (keys(%f)) {
888: if (exists($f{$_}) && ($_!~/template\_/)) {
889: my $matches=($_=~/^$pattern(\d+)/);
890: if (($matches) && ($1)) {
891: unless ($f{$_}=~/^\!/) {
892: $t{$_}=$c{$_};
893: }
894: } else {
895: $t{$_}=$f{$_};
896: $t{$_}=~s/\.\.+/\,/g;
897: $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
898: $t{$_}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
899: }
900: }
901: }
902: # For inserted lines, [B-Z] is also valid
903: unless ($sheettype eq 'assesscalc') {
904: foreach (keys(%f)) {
905: if ($_=~/[B-Z](\d+)/) {
906: if ($f{'A'.$1}=~/^[\~\-]/) {
907: $t{$_}=$f{$_};
908: $t{$_}=~s/\.\.+/\,/g;
909: $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
910: $t{$_}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
911: }
912: }
913: }
914: }
915: # For some reason 'A0' gets special treatment... This seems superfluous
916: # but I imagine it is here for a reason.
917: $t{'A0'}=$f{'A0'};
918: $t{'A0'}=~s/\.\.+/\,/g;
919: $t{'A0'}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
920: $t{'A0'}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
921: }
922:
923: sub calc {
924: undef %sheet_values;
925: &sett();
926: my $notfinished=1;
927: my $lastcalc='';
928: my $depth=0;
929: while ($notfinished) {
930: $notfinished=0;
931: foreach (keys(%t)) {
932: #$errorlog .= "$_:".$t{$_};
933: my $old=$sheet_values{$_};
934: $sheet_values{$_}=eval $t{$_};
935: if ($@) {
936: undef %sheet_values;
937: return $_.': '.$@;
938: }
939: if ($sheet_values{$_} ne $old) { $notfinished=1; $lastcalc=$_; }
940: #$errorlog .= ":".$sheet_values{$_}."\n";
941: }
942: $depth++;
943: if ($depth>100) {
944: undef %sheet_values;
945: return $lastcalc.': Maximum calculation depth exceeded';
946: }
947: }
948: return '';
949: }
950:
951: # ------------------------------------------- End of "Inside of the safe space"
952: ENDDEFS
953: $safeeval->reval($code);
954: return $safeeval;
955: }
956:
957: #
958: #
959: #
960: sub templaterow {
961: my $sheet = shift;
962: my @cols=();
963: my $rowlabel = 'Template';
964: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
965: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
966: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
967: 'n','o','p','q','r','s','t','u','v','w','x','y','z') {
968: my $fm=$sheet->{'f'}->{'template_'.$_};
969: $fm=~s/[\'\"]/\&\#34;/g;
970: push(@cols,{ name => 'template_'.$_,
971: formula => $fm,
972: value => $fm });
973: }
974: return ($rowlabel,@cols);
975: }
976:
977: sub outrowassess {
978: # $n is the current row number
979: my ($sheet,$n) = @_;
980: my @cols=();
981: my $rowlabel='';
982: if ($n) {
983: my ($usy,$ufn)=split(/__&&&\__/,$sheet->{'f'}->{'A'.$n});
984: if (exists($sheet->{'rowlabel'}->{$usy})) {
985: $rowlabel = $sheet->{'rowlabel'}->{$usy};
986: } else {
987: $rowlabel = '';
988: }
989: } else {
990: $rowlabel = 'Export';
991: }
992: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
993: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
994: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
995: 'n','o','p','q','r','s','t','u','v','w','x','y','z') {
996: my $fm=$sheet->{'f'}->{$_.$n};
997: $fm=~s/[\'\"]/\&\#34;/g;
998: push(@cols,{ name => $_.$n,
999: formula => $fm,
1000: value => $sheet->{'values'}->{$_.$n}});
1001: }
1002: return ($rowlabel,@cols);
1003: }
1004:
1005: sub outrow {
1006: my ($sheet,$n)=@_;
1007: my @cols=();
1008: my $rowlabel;
1009: if ($n) {
1010: $rowlabel = $sheet->{'rowlabel'}->{$sheet->{'f'}->{'A'.$n}};
1011: } else {
1012: if ($sheet->{'sheettype'} eq 'classcalc') {
1013: $rowlabel = 'Summary';
1014: } else {
1015: $rowlabel = 'Export';
1016: }
1017: }
1018: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1019: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
1020: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
1021: 'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1022: my $fm=$sheet->{'f'}->{$_.$n};
1023: $fm=~s/[\'\"]/\&\#34;/g;
1024: push(@cols,{ name => $_.$n,
1025: formula => $fm,
1026: value => $sheet->{'values'}->{$_.$n}});
1027: }
1028: return ($rowlabel,@cols);
1029: }
1030:
1031: # ------------------------------------------------ Add or change formula values
1032: sub setformulas {
1033: my ($sheet)=shift;
1034: %{$sheet->{'safe'}->varglob('f')}=%{$sheet->{'f'}};
1035: }
1036:
1037: # ------------------------------------------------ Add or change formula values
1038: sub setconstants {
1039: my ($sheet)=shift;
1040: my ($constants) = @_;
1041: if (! ref($constants)) {
1042: my %tmp = @_;
1043: $constants = \%tmp;
1044: }
1045: $sheet->{'constants'} = $constants;
1046: return %{$sheet->{'safe'}->varglob('c')}=%{$sheet->{'constants'}};
1047: }
1048:
1049: # --------------------------------------------- Set names of other spreadsheets
1050: sub setothersheets {
1051: my $sheet = shift;
1052: my @othersheets = @_;
1053: $sheet->{'othersheets'} = \@othersheets;
1054: @{$sheet->{'safe'}->varglob('os')}=@othersheets;
1055: return;
1056: }
1057:
1058: # ------------------------------------------------ Add or change formula values
1059: sub setrowlabels {
1060: my $sheet=shift;
1061: my ($rowlabel) = @_;
1062: if (! ref($rowlabel)) {
1063: my %tmp = @_;
1064: $rowlabel = \%tmp;
1065: }
1066: $sheet->{'rowlabel'}=$rowlabel;
1067: }
1068:
1069: # ------------------------------------------------------- Calculate spreadsheet
1070: sub calcsheet {
1071: my $sheet=shift;
1072: my $result = $sheet->{'safe'}->reval('&calc();');
1073: %{$sheet->{'values'}} = %{$sheet->{'safe'}->varglob('sheet_values')};
1074: return $result;
1075: }
1076:
1077: # ---------------------------------------------------------------- Get formulas
1078: # Return a copy of the formulas
1079: sub getformulas {
1080: my $sheet = shift;
1081: return %{$sheet->{'safe'}->varglob('f')};
1082: }
1083:
1084: sub geterrorlog {
1085: my $sheet = shift;
1086: return ${$sheet->{'safe'}->varglob('errorlog')};
1087: }
1088:
1089: sub gettitle {
1090: my $sheet = shift;
1091: if ($sheet->{'sheettype'} eq 'classcalc') {
1092: return $sheet->{'coursedesc'};
1093: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1094: return 'Grades for '.$sheet->{'uname'}.'@'.$sheet->{'udom'};
1095: } elsif ($sheet->{'sheettype'} eq 'assesscalc') {
1096: if (($sheet->{'usymb'} eq '_feedback') ||
1097: ($sheet->{'usymb'} eq '_evaluation') ||
1098: ($sheet->{'usymb'} eq '_discussion') ||
1099: ($sheet->{'usymb'} eq '_tutoring')) {
1100: my $title = $sheet->{'usymb'};
1101: $title =~ s/^_//;
1102: $title = ucfirst($title);
1103: return $title;
1104: }
1105: return if (! defined($sheet->{'mapid'}) ||
1106: $sheet->{'mapid'} !~ /^\d+$/);
1107: my $mapid = $sheet->{'mapid'};
1108: return if (! defined($sheet->{'resid'}) ||
1109: $sheet->{'resid'} !~ /^\d+$/);
1110: my $resid = $sheet->{'resid'};
1111: my %course_db;
1112: tie(%course_db,'GDBM_File',$sheet->{'coursefilename'}.'.db',
1113: &GDBM_READER(),0640);
1114: return if (! tied(%course_db));
1115: my $key = 'title_'.$mapid.'.'.$resid;
1116: my $title = '';
1117: if (exists($course_db{$key})) {
1118: $title = $course_db{$key};
1119: } else {
1120: $title = $sheet->{'usymb'};
1121: }
1122: untie (%course_db);
1123: return $title;
1124: }
1125: }
1126:
1127: # ----------------------------------------------------- Get value of $f{'A'.$n}
1128: sub getfa {
1129: my $sheet = shift;
1130: my ($n)=@_;
1131: return $sheet->{'safe'}->reval('$f{"A'.$n.'"}');
1132: }
1133:
1134: # ------------------------------------------------------------- Export of A-row
1135: sub exportdata {
1136: my $sheet=shift;
1137: my @exportarray=();
1138: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1139: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1140: if (exists($sheet->{'values'}->{$_.'0'})) {
1141: push(@exportarray,$sheet->{'values'}->{$_.'0'});
1142: } else {
1143: push(@exportarray,'');
1144: }
1145: }
1146: return @exportarray;
1147: }
1148:
1149:
1150:
1151: sub update_student_sheet{
1152: my $sheet = shift;
1153: # Load in the studentcalc sheet
1154: &readsheet($sheet,'default_studentcalc');
1155: # Determine the structure (contained assessments, etc) of the sheet
1156: &updatesheet($sheet);
1157: # Load in the cached sheets for this student
1158: &cachedssheets($sheet);
1159: # Load in the (possibly cached) data from the assessment sheets
1160: &loadstudent($sheet);
1161: # Compute the sheet
1162: &calcsheet($sheet);
1163: }
1164:
1165: # ========================================================== End of Spreadsheet
1166: # =============================================================================
1167: #
1168: # Procedures for spreadsheet output
1169: #
1170: # --------------------------------------------- Produce output row n from sheet
1171:
1172: sub get_row {
1173: my ($sheet,$n) = @_;
1174: my ($rowlabel,@rowdata);
1175: if ($n eq '-') {
1176: ($rowlabel,@rowdata) = &templaterow($sheet);
1177: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1178: ($rowlabel,@rowdata) = &outrowassess($sheet,$n);
1179: } else {
1180: ($rowlabel,@rowdata) = &outrow($sheet,$n);
1181: }
1182: return ($rowlabel,@rowdata);
1183: }
1184:
1185: ########################################################################
1186: ########################################################################
1187: sub sort_indicies {
1188: my $sheet = shift;
1189: #
1190: # Sort the rows in some manner
1191: #
1192: my @sortby=();
1193: my @sortidx=();
1194: # Skip row 0
1195: for (my $row=1;$row<=$sheet->{'maxrow'};$row++) {
1196: push (@sortby, $sheet->{'safe'}->reval('$f{"A'.$row.'"}'));
1197: push (@sortidx, $row);
1198: }
1199: @sortidx=sort { lc($sortby[$a]) cmp lc($sortby[$b]); } @sortidx;
1200: return @sortidx;
1201: }
1202:
1203: #############################################################
1204: ### ###
1205: ### Spreadsheet Output Routines ###
1206: ### ###
1207: #############################################################
1208:
1209: ############################################
1210: ## HTML output routines ##
1211: ############################################
1212: sub html_editable_cell {
1213: my ($cell,$bgcolor) = @_;
1214: my $result;
1215: my ($name,$formula,$value);
1216: if (defined($cell)) {
1217: $name = $cell->{'name'};
1218: $formula = $cell->{'formula'};
1219: $value = $cell->{'value'};
1220: }
1221: $name = '' if (! defined($name));
1222: $formula = '' if (! defined($formula));
1223: if (! defined($value)) {
1224: $value = '<font color="'.$bgcolor.'">#</font>';
1225: if ($formula ne '') {
1226: $value = '<i>undefined value</i>';
1227: }
1228: }
1229: if ($value =~ /^\s*$/ ) {
1230: $value = '<font color="'.$bgcolor.'">#</font>';
1231: }
1232: $formula =~ s/\n/\\n/gs;
1233: $result .= '<a href="javascript:celledit(\''.
1234: $name.'\',\''.$formula.'\');">'.$value.'</a>';
1235: return $result;
1236: }
1237:
1238: sub html_uneditable_cell {
1239: my ($cell,$bgcolor) = @_;
1240: my $value = (defined($cell) ? $cell->{'value'} : '');
1241: return ' '.$value.' ';
1242: }
1243:
1244: sub outsheet_html {
1245: my ($sheet,$r) = @_;
1246: my ($num_uneditable,$realm,$row_type);
1247: if ($sheet->{'sheettype'} eq 'assesscalc') {
1248: $num_uneditable = 1;
1249: $realm = 'Assessment';
1250: $row_type = 'Item';
1251: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1252: $num_uneditable = 26;
1253: $realm = 'User';
1254: $row_type = 'Assessment';
1255: } elsif ($sheet->{'sheettype'} eq 'classcalc') {
1256: $num_uneditable = 26;
1257: $realm = 'Course';
1258: $row_type = 'Student';
1259: } else {
1260: return; # error
1261: }
1262: ####################################
1263: # Print out header table
1264: ####################################
1265: my $num_left = 52-$num_uneditable;
1266: my $tabledata =<<"END";
1267: <table border="2">
1268: <tr>
1269: <th colspan="1" rowspan="2"><font size="+2">$realm</font></th>
1270: <td bgcolor="#FFDDDD" colspan="$num_uneditable">
1271: <b><font size="+1">Import</font></b></td>
1272: <td colspan="$num_left">
1273: <b><font size="+1">Calculations</font></b></td>
1274: </tr><tr>
1275: END
1276: my $label_num = 0;
1277: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
1278: if ($label_num<$num_uneditable) {
1279: $tabledata.='<td bgcolor="#FFDDDD">';
1280: } else {
1281: $tabledata.='<td>';
1282: }
1283: $tabledata.="<b><font size=+1>$_</font></b></td>";
1284: $label_num++;
1285: }
1286: $tabledata.="</tr>\n";
1287: $r->print($tabledata);
1288: ####################################
1289: # Print out template row
1290: ####################################
1291: my ($rowlabel,@rowdata) = &get_row($sheet,'-');
1292: my $row_html = '<tr><td>'.&format_html_rowlabel($rowlabel).'</td>';
1293: my $num_cols_output = 0;
1294: foreach my $cell (@rowdata) {
1295: if ($num_cols_output++ < $num_uneditable) {
1296: $row_html .= '<td bgcolor="#FFDDDD">';
1297: $row_html .= &html_uneditable_cell($cell,'#FFDDDD');
1298: } else {
1299: $row_html .= '<td bgcolor="#EOFFDD">';
1300: $row_html .= &html_editable_cell($cell,'#E0FFDD');
1301: }
1302: $row_html .= '</td>';
1303: }
1304: $row_html.= "</tr>\n";
1305: $r->print($row_html);
1306: ####################################
1307: # Print out summary/export row
1308: ####################################
1309: my ($rowlabel,@rowdata) = &get_row($sheet,'0');
1310: $row_html = '<tr><td>'.&format_html_rowlabel($rowlabel).'</td>';
1311: $num_cols_output = 0;
1312: foreach my $cell (@rowdata) {
1313: if ($num_cols_output++ < 26) {
1314: $row_html .= '<td bgcolor="#CCCCFF">';
1315: $row_html .= &html_editable_cell($cell,'#CCCCFF');
1316: } else {
1317: $row_html .= '<td bgcolor="#DDCCFF">';
1318: $row_html .= &html_uneditable_cell(undef,'#CCCCFF');
1319: }
1320: $row_html .= '</td>';
1321: }
1322: $row_html.= "</tr>\n";
1323: $r->print($row_html);
1324: $r->print('</table>');
1325: ####################################
1326: # Prepare to output rows
1327: ####################################
1328: my @Rows = &sort_indicies($sheet);
1329: #
1330: # Loop through the rows and output them one at a time
1331: my $rows_output=0;
1332: foreach my $rownum (@Rows) {
1333: my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
1334: next if ($rowlabel =~ /^\s*$/);
1335: #
1336: my $defaultbg='#E0FF';
1337: #
1338: my $row_html ="\n".'<tr><td><b><font size=+1>'.$rownum.
1339: '</font></b></td>';
1340: #
1341: if ($sheet->{'sheettype'} eq 'classcalc') {
1342: $row_html.='<td>'.&format_html_rowlabel($rowlabel).'</td>';
1343: # Output links for each student?
1344: # Nope, that is already done for us in format_html_rowlabel (for now)
1345: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1346: $row_html.='<td>'.&format_html_rowlabel($rowlabel);
1347: $row_html.= '<br>'.
1348: '<select name="sel_'.$rownum.'" '.
1349: 'onChange="changesheet('.$rownum.')">'.
1350: '<option name="default">Default</option>';
1351: foreach (@{$sheet->{'othersheets'}}) {
1352: $row_html.='<option name="'.$_.'"';
1353: #if ($ufn eq $_) {
1354: # $row_html.=' selected';
1355: #}
1356: $row_html.='>'.$_.'</option>';
1357: }
1358: $row_html.='</select></td>';
1359: } elsif ($sheet->{'sheettype'} eq 'assesscalc') {
1360: $row_html.='<td>'.&format_html_rowlabel($rowlabel).'</td>';
1361: }
1362: #
1363: my $shown_cells = 0;
1364: foreach my $cell (@rowdata) {
1365: my $value = $cell->{'value'};
1366: my $formula = $cell->{'formula'};
1367: my $cellname = $cell->{'name'};
1368: #
1369: my $bgcolor;
1370: if ($shown_cells && ($shown_cells/5 == int($shown_cells/5))) {
1371: $bgcolor = $defaultbg.'99';
1372: } else {
1373: $bgcolor = $defaultbg.'DD';
1374: }
1375: $bgcolor='#FFDDDD' if ($shown_cells < $num_uneditable);
1376: #
1377: $row_html.='<td bgcolor='.$bgcolor.'>';
1378: if ($shown_cells < $num_uneditable) {
1379: $row_html .= &html_uneditable_cell($cell,$bgcolor);
1380: } else {
1381: $row_html .= &html_editable_cell($cell,$bgcolor);
1382: }
1383: $row_html.='</td>';
1384: $shown_cells++;
1385: }
1386: if ($row_html) {
1387: if ($rows_output % 25 == 0) {
1388: $r->print("</table>\n<br>\n");
1389: $r->rflush();
1390: $r->print('<table border=2>'.
1391: '<tr><td> <td>'.$row_type.'</td>'.
1392: '<td>'.
1393: join('</td><td>',
1394: (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
1395: 'abcdefghijklmnopqrstuvwxyz'))).
1396: "</td></tr>\n");
1397: }
1398: $rows_output++;
1399: $r->print($row_html);
1400: }
1401: }
1402: #
1403: $r->print('</table>');
1404: #
1405: # Debugging code (be sure to uncomment errorlog code in safe space):
1406: #
1407: # $r->print("\n<pre>");
1408: # $r->print(&geterrorlog($sheet));
1409: # $r->print("\n</pre>");
1410: return 1;
1411: }
1412:
1413: ############################################
1414: ## csv output routines ##
1415: ############################################
1416: sub outsheet_csv {
1417: my ($sheet,$r) = @_;
1418: my $csvdata = '';
1419: my @Values;
1420: ####################################
1421: # Prepare to output rows
1422: ####################################
1423: my @Rows = &sort_indicies($sheet);
1424: #
1425: # Loop through the rows and output them one at a time
1426: my $rows_output=0;
1427: foreach my $rownum (@Rows) {
1428: my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
1429: next if ($rowlabel =~ /^\s*$/);
1430: push (@Values,&format_csv_rowlabel($rowlabel));
1431: foreach my $cell (@rowdata) {
1432: push (@Values,'"'.$cell->{'value'}.'"');
1433: }
1434: $csvdata.= join(',',@Values)."\n";
1435: @Values = ();
1436: }
1437: #
1438: # Write the CSV data to a file and serve up a link
1439: #
1440: my $filename = '/prtspool/'.
1441: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1442: time.'_'.rand(1000000000).'.csv';
1443: my $file;
1444: unless ($file = Apache::File->new('>'.'/home/httpd'.$filename)) {
1445: $r->log_error("Couldn't open $filename for output $!");
1446: $r->print("Problems occured in writing the csv file. ".
1447: "This error has been logged. ".
1448: "Please alert your LON-CAPA administrator.");
1449: $r->print("<pre>\n".$csvdata."</pre>\n");
1450: return 0;
1451: }
1452: print $file $csvdata;
1453: close($file);
1454: $r->print('<br /><br />'.
1455: '<a href="'.$filename.'">Your CSV spreadsheet.</a>'."\n");
1456: #
1457: return 1;
1458: }
1459:
1460: ############################################
1461: ## Excel output routines ##
1462: ############################################
1463: sub outsheet_recursive_excel {
1464: my ($sheet,$r) = @_;
1465: my $c = $r->connection;
1466: return undef if ($sheet->{'sheettype'} ne 'classcalc');
1467: my ($workbook,$filename) = &create_excel_spreadsheet($sheet,$r);
1468: return undef if (! defined($workbook));
1469: #
1470: # Create main worksheet
1471: my $main_worksheet = $workbook->addworksheet('main');
1472: #
1473: # Figure out who the students are
1474: my %f=&getformulas($sheet);
1475: my $count = 0;
1476: $r->print(<<END);
1477: <p>
1478: Compiling Excel Workbook with a worksheet for each student.
1479: </p><p>
1480: This operation may take longer than a complete recalculation of the
1481: spreadsheet.
1482: </p><p>
1483: To abort this operation, hit the stop button on your browser.
1484: </p><p>
1485: A link to the spreadsheet will be available at the end of this process.
1486: </p>
1487: <p>
1488: END
1489: $r->rflush();
1490: my $starttime = time;
1491: foreach (keys(%f)) {
1492: next if ($_!~/^A(\d+)/ || $1 == 0 || ($f{$_}=~/^[!~-]/));
1493: $count++;
1494: my ($sname,$sdom) = split(':',$f{$_});
1495: my $student_excel_worksheet=$workbook->addworksheet($sname.'@'.$sdom);
1496: # Create a new spreadsheet
1497: my $studentsheet = &makenewsheet($sname,$sdom,'studentcalc',undef);
1498: # Read in the spreadsheet definition
1499: &update_student_sheet($studentsheet);
1500: # Stuff the sheet into excel
1501: &export_sheet_as_excel($studentsheet,$student_excel_worksheet);
1502: my $totaltime = int((time - $starttime) / $count * $sheet->{'maxrow'});
1503: my $timeleft = int((time - $starttime) / $count * ($sheet->{'maxrow'} - $count));
1504: if ($count % 5 == 0) {
1505: $r->print($count.' students completed.'.
1506: ' Time remaining: '.$timeleft.' sec. '.
1507: ' Estimated total time: '.$totaltime." sec <br />\n");
1508: $r->rflush();
1509: }
1510: if(defined($c) && ($c->aborted())) {
1511: last;
1512: }
1513: }
1514: #
1515: if(! $c->aborted() ) {
1516: $r->print('All students spreadsheets completed!<br />');
1517: $r->rflush();
1518: #
1519: # &export_sheet_as_excel fills $worksheet with the data from $sheet
1520: &export_sheet_as_excel($sheet,$main_worksheet);
1521: #
1522: $workbook->close();
1523: # Okay, the spreadsheet is taken care of, so give the user a link.
1524: $r->print('<br /><br />'.
1525: '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
1526: } else {
1527: $workbook->close(); # Not sure how necessary this is.
1528: #unlink('/home/httpd'.$filename); # No need to keep this around?
1529: }
1530: return 1;
1531: }
1532:
1533: sub outsheet_excel {
1534: my ($sheet,$r) = @_;
1535: my ($workbook,$filename) = &create_excel_spreadsheet($sheet,$r);
1536: return undef if (! defined($workbook));
1537: my $sheetname;
1538: if ($sheet->{'sheettype'} eq 'classcalc') {
1539: $sheetname = 'Main';
1540: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1541: $sheetname = $sheet->{'uname'}.'@'.$sheet->{'udom'};
1542: } elsif ($sheet->{'sheettype'} eq 'assesscalc') {
1543: $sheetname = $sheet->{'uname'}.'@'.$sheet->{'udom'}.' assessment';
1544: }
1545: my $worksheet = $workbook->addworksheet($sheetname);
1546: #
1547: # &export_sheet_as_excel fills $worksheet with the data from $sheet
1548: &export_sheet_as_excel($sheet,$worksheet);
1549: #
1550: $workbook->close();
1551: # Okay, the spreadsheet is taken care of, so give the user a link.
1552: $r->print('<br /><br />'.
1553: '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
1554: return 1;
1555: }
1556:
1557: sub create_excel_spreadsheet {
1558: my ($sheet,$r) = @_;
1559: my $filename = '/prtspool/'.
1560: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1561: time.'_'.rand(1000000000).'.xls';
1562: my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
1563: if (! defined($workbook)) {
1564: $r->log_error("Error creating excel spreadsheet $filename: $!");
1565: $r->print("Problems creating new Excel file. ".
1566: "This error has been logged. ".
1567: "Please alert your LON-CAPA administrator");
1568: return undef;
1569: }
1570: #
1571: # The spreadsheet stores temporary data in files, then put them
1572: # together. If needed we should be able to disable this (memory only).
1573: # The temporary directory must be specified before calling 'addworksheet'.
1574: # File::Temp is used to determine the temporary directory.
1575: $workbook->set_tempdir('/home/httpd/perl/tmp');
1576: #
1577: # Determine the name to give the worksheet
1578: return ($workbook,$filename);
1579: }
1580:
1581: sub export_sheet_as_excel {
1582: my $sheet = shift;
1583: my $worksheet = shift;
1584: #
1585: my $rows_output = 0;
1586: my $cols_output = 0;
1587: ####################################
1588: # Write an identifying row #
1589: ####################################
1590: my @Headerinfo = ($sheet->{'coursedesc'});
1591: my $title = &gettitle($sheet);
1592: $cols_output = 0;
1593: if (defined($title)) {
1594: $worksheet->write($rows_output++,$cols_output++,$title);
1595: }
1596: ####################################
1597: # Write the summary/export row #
1598: ####################################
1599: my ($rowlabel,@rowdata) = &get_row($sheet,'0');
1600: my $label = &format_excel_rowlabel($rowlabel);
1601: $cols_output = 0;
1602: $worksheet->write($rows_output,$cols_output++,$label);
1603: foreach my $cell (@rowdata) {
1604: $worksheet->write($rows_output,$cols_output++,$cell->{'value'});
1605: }
1606: $rows_output+= 2; # Skip a row, just for fun
1607: ####################################
1608: # Prepare to output rows
1609: ####################################
1610: my @Rows = &sort_indicies($sheet);
1611: #
1612: # Loop through the rows and output them one at a time
1613: foreach my $rownum (@Rows) {
1614: my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
1615: next if ($rowlabel =~ /^\s*$/);
1616: $cols_output = 0;
1617: my $label = &format_excel_rowlabel($rowlabel);
1618: $worksheet->write($rows_output,$cols_output++,$label);
1619: if (ref($label)) {
1620: $cols_output = (scalar(@$label));
1621: }
1622: foreach my $cell (@rowdata) {
1623: $worksheet->write($rows_output,$cols_output++,$cell->{'value'});
1624: }
1625: $rows_output++;
1626: }
1627: return;
1628: }
1629:
1630: ############################################
1631: ## XML output routines ##
1632: ############################################
1633: sub outsheet_xml {
1634: my ($sheet,$r) = @_;
1635: ## Someday XML
1636: ## Will be rendered for the user
1637: ## But not on this day
1638: }
1639:
1640: ##
1641: ## Outsheet - calls other outsheet_* functions
1642: ##
1643: sub outsheet {
1644: my ($r,$sheet)=@_;
1645: if (! exists($ENV{'form.output'})) {
1646: $ENV{'form.output'} = 'HTML';
1647: }
1648: if (lc($ENV{'form.output'}) eq 'csv') {
1649: &outsheet_csv($sheet,$r);
1650: } elsif (lc($ENV{'form.output'}) eq 'excel') {
1651: &outsheet_excel($sheet,$r);
1652: } elsif (lc($ENV{'form.output'}) eq 'recursive excel') {
1653: &outsheet_recursive_excel($sheet,$r);
1654: # } elsif (lc($ENV{'form.output'}) eq 'xml' ) {
1655: # &outsheet_xml($sheet,$r);
1656: } else {
1657: &outsheet_html($sheet,$r);
1658: }
1659: }
1660:
1661: ########################################################################
1662: ########################################################################
1663: sub othersheets {
1664: my ($sheet,$stype)=@_;
1665: $stype = $sheet->{'sheettype'} if (! defined($stype));
1666: #
1667: my $cnum = $sheet->{'cnum'};
1668: my $cdom = $sheet->{'cdom'};
1669: my $chome = $sheet->{'chome'};
1670: #
1671: my @alternatives=();
1672: my %results=&Apache::lonnet::dump($stype.'_spreadsheets',$cdom,$cnum);
1673: my ($tmp) = keys(%results);
1674: unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1675: @alternatives = sort (keys(%results));
1676: }
1677: return @alternatives;
1678: }
1679:
1680: #
1681: # -------------------------------------- Parse a spreadsheet
1682: #
1683: sub parse_sheet {
1684: # $sheetxml is a scalar reference or a scalar
1685: my ($sheetxml) = @_;
1686: if (! ref($sheetxml)) {
1687: my $tmp = $sheetxml;
1688: $sheetxml = \$tmp;
1689: }
1690: my %f;
1691: my $parser=HTML::TokeParser->new($sheetxml);
1692: my $token;
1693: while ($token=$parser->get_token) {
1694: if ($token->[0] eq 'S') {
1695: if ($token->[1] eq 'field') {
1696: $f{$token->[2]->{'col'}.$token->[2]->{'row'}}=
1697: $parser->get_text('/field');
1698: }
1699: if ($token->[1] eq 'template') {
1700: $f{'template_'.$token->[2]->{'col'}}=
1701: $parser->get_text('/template');
1702: }
1703: }
1704: }
1705: return \%f;
1706: }
1707:
1708: #
1709: # -------------------------------------- Read spreadsheet formulas for a course
1710: #
1711: sub readsheet {
1712: my ($sheet,$fn)=@_;
1713: #
1714: my $stype = $sheet->{'sheettype'};
1715: my $cnum = $sheet->{'cnum'};
1716: my $cdom = $sheet->{'cdom'};
1717: my $chome = $sheet->{'chome'};
1718: #
1719: if (! defined($fn)) {
1720: # There is no filename. Look for defaults in course and global, cache
1721: unless ($fn=$defaultsheets{$cnum.'_'.$cdom.'_'.$stype}) {
1722: my %tmphash = &Apache::lonnet::get('environment',
1723: ['spreadsheet_default_'.$stype],
1724: $cdom,$cnum);
1725: my ($tmp) = keys(%tmphash);
1726: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1727: $fn = 'default_'.$stype;
1728: } else {
1729: $fn = $tmphash{'spreadsheet_default_'.$stype};
1730: }
1731: unless (($fn) && ($fn!~/^error\:/)) {
1732: $fn='default_'.$stype;
1733: }
1734: $defaultsheets{$cnum.'_'.$cdom.'_'.$stype}=$fn;
1735: }
1736: }
1737: # $fn now has a value
1738: $sheet->{'filename'} = $fn;
1739: # see if sheet is cached
1740: my $fstring='';
1741: if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
1742: my %tmp = split(/___;___/,$fstring);
1743: $sheet->{'f'} = \%tmp;
1744: &setformulas($sheet);
1745: } else {
1746: # Not cached, need to read
1747: my %f=();
1748: if ($fn=~/^default\_/) {
1749: my $sheetxml='';
1750: my $fh;
1751: my $dfn=$fn;
1752: $dfn=~s/\_/\./g;
1753: if ($fh=Apache::File->new($includedir.'/'.$dfn)) {
1754: $sheetxml=join('',<$fh>);
1755: } else {
1756: $sheetxml='<field row="0" col="A">"Error"</field>';
1757: }
1758: %f=%{&parse_sheet(\$sheetxml)};
1759: } elsif($fn=~/\/*\.spreadsheet$/) {
1760: my $sheetxml=&Apache::lonnet::getfile
1761: (&Apache::lonnet::filelocation('',$fn));
1762: if ($sheetxml == -1) {
1763: $sheetxml='<field row="0" col="A">"Error loading spreadsheet '
1764: .$fn.'"</field>';
1765: }
1766: %f=%{&parse_sheet(\$sheetxml)};
1767: } else {
1768: my $sheet='';
1769: my %tmphash = &Apache::lonnet::dump($fn,$cdom,$cnum);
1770: my ($tmp) = keys(%tmphash);
1771: unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1772: foreach (keys(%tmphash)) {
1773: $f{$_}=$tmphash{$_};
1774: }
1775: }
1776: }
1777: # Cache and set
1778: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1779: $sheet->{'f'}=\%f;
1780: &setformulas($sheet);
1781: }
1782: }
1783:
1784: # -------------------------------------------------------- Make new spreadsheet
1785: sub makenewsheet {
1786: my ($uname,$udom,$stype,$usymb)=@_;
1787: my $sheet={};
1788: $sheet->{'uname'} = $uname;
1789: $sheet->{'udom'} = $udom;
1790: $sheet->{'sheettype'} = $stype;
1791: $sheet->{'usymb'} = $usymb;
1792: $sheet->{'mapid'} = $ENV{'form.mapid'};
1793: $sheet->{'resid'} = $ENV{'form.resid'};
1794: $sheet->{'cid'} = $ENV{'request.course.id'};
1795: $sheet->{'csec'} = $Section{$uname.':'.$udom};
1796: $sheet->{'coursefilename'} = $ENV{'request.course.fn'};
1797: $sheet->{'cnum'} = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
1798: $sheet->{'cdom'} = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1799: $sheet->{'chome'} = $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1800: $sheet->{'coursedesc'} = $ENV{'course.'.$ENV{'request.course.id'}.
1801: '.description'};
1802: $sheet->{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);
1803: #
1804: #
1805: $sheet->{'f'} = {};
1806: $sheet->{'constants'} = {};
1807: $sheet->{'othersheets'} = [];
1808: $sheet->{'rowlabel'} = {};
1809: #
1810: #
1811: $sheet->{'safe'}=&initsheet($sheet->{'sheettype'});
1812: #
1813: # Place all the %$sheet items into the safe space except the safe space
1814: # itself
1815: my $initstring = '';
1816: foreach (qw/uname udom sheettype usymb cid csec coursefilename
1817: cnum cdom chome uhome/) {
1818: $initstring.= qq{\$$_="$sheet->{$_}";};
1819: }
1820: $sheet->{'safe'}->reval($initstring);
1821: return $sheet;
1822: }
1823:
1824: # ------------------------------------------------------------ Save spreadsheet
1825: sub writesheet {
1826: my ($sheet,$makedef)=@_;
1827: my $cid=$sheet->{'cid'};
1828: if (&Apache::lonnet::allowed('opa',$cid)) {
1829: my %f=&getformulas($sheet);
1830: my $stype= $sheet->{'sheettype'};
1831: my $cnum = $sheet->{'cnum'};
1832: my $cdom = $sheet->{'cdom'};
1833: my $chome= $sheet->{'chome'};
1834: my $fn = $sheet->{'filename'};
1835: # Cache new sheet
1836: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1837: # Write sheet
1838: foreach (keys(%f)) {
1839: delete($f{$_}) if ($f{$_} eq 'import');
1840: }
1841: my $reply = &Apache::lonnet::put($fn,\%f,$cdom,$cnum);
1842: if ($reply eq 'ok') {
1843: $reply = &Apache::lonnet::put($stype.'_spreadsheets',
1844: {$fn => $ENV{'user.name'}.'@'.$ENV{'user.domain'}},
1845: $cdom,$cnum);
1846: if ($reply eq 'ok') {
1847: if ($makedef) {
1848: return &Apache::lonnet::put('environment',
1849: {'spreadsheet_default_'.$stype => $fn },
1850: $cdom,$cnum);
1851: }
1852: return $reply;
1853: }
1854: return $reply;
1855: }
1856: return $reply;
1857: }
1858: return 'unauthorized';
1859: }
1860:
1861: # ----------------------------------------------- Make a temp copy of the sheet
1862: # "Modified workcopy" - interactive only
1863: #
1864: sub tmpwrite {
1865: my ($sheet) = @_;
1866: my $fn=$ENV{'user.name'}.'_'.
1867: $ENV{'user.domain'}.'_spreadsheet_'.$sheet->{'usymb'}.'_'.
1868: $sheet->{'filename'};
1869: $fn=~s/\W/\_/g;
1870: $fn=$tmpdir.$fn.'.tmp';
1871: my $fh;
1872: if ($fh=Apache::File->new('>'.$fn)) {
1873: print $fh join("\n",&getformulas($sheet));
1874: }
1875: }
1876:
1877: # ---------------------------------------------------------- Read the temp copy
1878: sub tmpread {
1879: my ($sheet,$nfield,$nform)=@_;
1880: my $fn=$ENV{'user.name'}.'_'.
1881: $ENV{'user.domain'}.'_spreadsheet_'.$sheet->{'usymb'}.'_'.
1882: $sheet->{'filename'};
1883: $fn=~s/\W/\_/g;
1884: $fn=$tmpdir.$fn.'.tmp';
1885: my $fh;
1886: my %fo=();
1887: my $countrows=0;
1888: if ($fh=Apache::File->new($fn)) {
1889: my $name;
1890: while ($name=<$fh>) {
1891: chomp($name);
1892: my $value=<$fh>;
1893: chomp($value);
1894: $fo{$name}=$value;
1895: if ($name=~/^A(\d+)$/) {
1896: if ($1>$countrows) {
1897: $countrows=$1;
1898: }
1899: }
1900: }
1901: }
1902: if ($nform eq 'changesheet') {
1903: $fo{'A'.$nfield}=(split(/__&&&\__/,$fo{'A'.$nfield}))[0];
1904: unless ($ENV{'form.sel_'.$nfield} eq 'Default') {
1905: $fo{'A'.$nfield}.='__&&&__'.$ENV{'form.sel_'.$nfield};
1906: }
1907: } elsif ($nfield eq 'insertrow') {
1908: $countrows++;
1909: my $newrow=substr('000000'.$countrows,-7);
1910: if ($nform eq 'top') {
1911: $fo{'A'.$countrows}='--- '.$newrow;
1912: } else {
1913: $fo{'A'.$countrows}='~~~ '.$newrow;
1914: }
1915: } else {
1916: if ($nfield) { $fo{$nfield}=$nform; }
1917: }
1918: $sheet->{'f'}=\%fo;
1919: &setformulas($sheet);
1920: }
1921:
1922: ##################################################
1923: ##################################################
1924:
1925: =pod
1926:
1927: =item &parmval()
1928:
1929: Determine the value of a parameter.
1930:
1931: Inputs: $what, the parameter needed, $sheet, the safe space
1932:
1933: Returns: The value of a parameter, or '' if none.
1934:
1935: This function cascades through the possible levels searching for a value for
1936: a parameter. The levels are checked in the following order:
1937: user, course (at section level and course level), map, and lonnet::metadata.
1938: This function uses %parmhash, which must be tied prior to calling it.
1939: This function also requires %courseopt and %useropt to be initialized for
1940: this user and course.
1941:
1942: =cut
1943:
1944: ##################################################
1945: ##################################################
1946: sub parmval {
1947: my ($what,$sheet)=@_;
1948: my $symb = $sheet->{'usymb'};
1949: unless ($symb) { return ''; }
1950: #
1951: my $cid = $sheet->{'cid'};
1952: my $csec = $sheet->{'csec'};
1953: my $uname = $sheet->{'uname'};
1954: my $udom = $sheet->{'udom'};
1955: my $result='';
1956: #
1957: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1958: # Cascading lookup scheme
1959: my $rwhat=$what;
1960: $what =~ s/^parameter\_//;
1961: $what =~ s/\_([^\_]+)$/\.$1/;
1962: #
1963: my $symbparm = $symb.'.'.$what;
1964: my $mapparm = $mapname.'___(all).'.$what;
1965: my $usercourseprefix = $uname.'_'.$udom.'_'.$cid;
1966: #
1967: my $seclevel = $usercourseprefix.'.['.$csec.'].'.$what;
1968: my $seclevelr = $usercourseprefix.'.['.$csec.'].'.$symbparm;
1969: my $seclevelm = $usercourseprefix.'.['.$csec.'].'.$mapparm;
1970: #
1971: my $courselevel = $usercourseprefix.'.'.$what;
1972: my $courselevelr = $usercourseprefix.'.'.$symbparm;
1973: my $courselevelm = $usercourseprefix.'.'.$mapparm;
1974: # fourth, check user
1975: if (defined($uname)) {
1976: return $useropt{$courselevelr} if (defined($useropt{$courselevelr}));
1977: return $useropt{$courselevelm} if (defined($useropt{$courselevelm}));
1978: return $useropt{$courselevel} if (defined($useropt{$courselevel}));
1979: }
1980: # third, check course
1981: if (defined($csec)) {
1982: return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
1983: return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
1984: return $courseopt{$seclevel} if (defined($courseopt{$seclevel}));
1985: }
1986: #
1987: return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
1988: return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
1989: return $courseopt{$courselevel} if (defined($courseopt{$courselevel}));
1990: # second, check map parms
1991: my $thisparm = $parmhash{$symbparm};
1992: return $thisparm if (defined($thisparm));
1993: # first, check default
1994: return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1995: }
1996:
1997:
1998: ##################################################################
1999: ## Row label formatting routines ##
2000: ##################################################################
2001: sub format_html_rowlabel {
2002: my $rowlabel = shift;
2003: return '' if ($rowlabel eq '');
2004: my ($type,$labeldata) = split(':',$rowlabel,2);
2005: my $result = '';
2006: if ($type eq 'symb') {
2007: my ($symb,$uname,$udom,$mapid,$resid,$title) = split(':',$labeldata);
2008: $symb = &Apache::lonnet::unescape($symb);
2009: $result = '<a href="/adm/assesscalc?usymb='.$symb.
2010: '&uname='.$uname.'&udom='.$udom.
2011: '&mapid='.$mapid.'&resid='.$resid.'">'.$title.'</a>';
2012: } elsif ($type eq 'student') {
2013: my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
2014: $result ='<a href="/adm/studentcalc?uname='.$sname.
2015: '&udom='.$sdom.'">';
2016: $result.=$section.' '.$id." ".$fullname.'</a>';
2017: } elsif ($type eq 'parameter') {
2018: $result = $labeldata;
2019: } else {
2020: $result = '<b><font size=+1>'.$rowlabel.'</font></b>';
2021: }
2022: return $result;
2023: }
2024:
2025: sub format_csv_rowlabel {
2026: my $rowlabel = shift;
2027: return '' if ($rowlabel eq '');
2028: my ($type,$labeldata) = split(':',$rowlabel,2);
2029: my $result = '';
2030: if ($type eq 'symb') {
2031: my ($symb,$uname,$udom,$mapid,$resid,$title) = split(':',$labeldata);
2032: $symb = &Apache::lonnet::unescape($symb);
2033: $result = $title;
2034: } elsif ($type eq 'student') {
2035: my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
2036: $result = join('","',($sname,$sdom,$fullname,$section,$id));
2037: } elsif ($type eq 'parameter') {
2038: $labeldata =~ s/<br>/ /g;
2039: $result = $labeldata;
2040: } else {
2041: $result = $rowlabel;
2042: }
2043: return '"'.$result.'"';
2044: }
2045:
2046: sub format_excel_rowlabel {
2047: my $rowlabel = shift;
2048: return '' if ($rowlabel eq '');
2049: my ($type,$labeldata) = split(':',$rowlabel,2);
2050: my $result = '';
2051: if ($type eq 'symb') {
2052: my ($symb,$uname,$udom,$mapid,$resid,$title) = split(':',$labeldata);
2053: $symb = &Apache::lonnet::unescape($symb);
2054: $result = $title;
2055: } elsif ($type eq 'student') {
2056: my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
2057: $section = '' if (! defined($section));
2058: $id = '' if (! defined($id));
2059: my @Data = ($sname,$sdom,$fullname,$section,$id);
2060: $result = \@Data;
2061: } elsif ($type eq 'parameter') {
2062: $labeldata =~ s/<br>/ /g;
2063: $result = $labeldata;
2064: } else {
2065: $result = $rowlabel;
2066: }
2067: return $result;
2068: }
2069:
2070: # ---------------------------------------------- Update rows for course listing
2071: sub updateclasssheet {
2072: my ($sheet) = @_;
2073: my $cnum =$sheet->{'cnum'};
2074: my $cdom =$sheet->{'cdom'};
2075: my $cid =$sheet->{'cid'};
2076: my $chome =$sheet->{'chome'};
2077: #
2078: %Section = ();
2079: #
2080: # Read class list and row labels
2081: my $classlist = &Apache::loncoursedata::get_classlist();
2082: if (! defined($classlist)) {
2083: return 'Could not access course classlist';
2084: }
2085: #
2086: my %currentlist=();
2087: foreach my $student (keys(%$classlist)) {
2088: my ($studentDomain,$studentName,$end,$start,$id,$studentSection,
2089: $fullname,$status) = @{$classlist->{$student}};
2090: $Section{$studentName.':'.$studentDomain} = $studentSection;
2091: if ($ENV{'form.Status'} eq $status || $ENV{'form.Status'} eq 'Any') {
2092: $currentlist{$student}=join(':',('student',$studentName,
2093: $studentDomain,$fullname,
2094: $studentSection,$id));
2095: }
2096: }
2097: #
2098: # Find discrepancies between the course row table and this
2099: #
2100: my %f=&getformulas($sheet);
2101: my $changed=0;
2102: #
2103: $sheet->{'maxrow'}=0;
2104: my %existing=();
2105: #
2106: # Now obsolete rows
2107: foreach (keys(%f)) {
2108: if ($_=~/^A(\d+)/) {
2109: if ($1 > $sheet->{'maxrow'}) {
2110: $sheet->{'maxrow'}= $1;
2111: }
2112: $existing{$f{$_}}=1;
2113: unless ((defined($currentlist{$f{$_}})) || (!$1) ||
2114: ($f{$_}=~/^(~~~|---)/)) {
2115: $f{$_}='!!! Obsolete';
2116: $changed=1;
2117: }
2118: }
2119: }
2120: #
2121: # New and unknown keys
2122: foreach my $student (sort keys(%currentlist)) {
2123: unless ($existing{$student}) {
2124: $changed=1;
2125: $sheet->{'maxrow'}++;
2126: $f{'A'.$sheet->{'maxrow'}}=$student;
2127: }
2128: }
2129: if ($changed) {
2130: $sheet->{'f'} = \%f;
2131: &setformulas($sheet,%f);
2132: }
2133: #
2134: &setrowlabels($sheet,\%currentlist);
2135: }
2136:
2137: # ----------------------------------- Update rows for student and assess sheets
2138: sub get_student_rowlabels {
2139: my ($sheet) = @_;
2140: #
2141: my %course_db;
2142: #
2143: my $stype = $sheet->{'sheettype'};
2144: my $uname = $sheet->{'uname'};
2145: my $udom = $sheet->{'udom'};
2146: #
2147: $sheet->{'rowlabel'} = {};
2148: #
2149: my $identifier =$sheet->{'coursefilename'}.'_'.$stype;
2150: if ($rowlabel_cache{$identifier}) {
2151: %{$sheet->{'rowlabel'}}=split(/___;___/,$rowlabel_cache{$identifier});
2152: } else {
2153: # Get the data and store it in the cache
2154: # Tie hash
2155: tie(%course_db,'GDBM_File',$sheet->{'coursefilename'}.'.db',
2156: &GDBM_READER(),0640);
2157: if (! tied(%course_db)) {
2158: return 'Could not access course data';
2159: }
2160: #
2161: my %assesslist;
2162: foreach ('Feedback','Evaluation','Tutoring','Discussion') {
2163: my $symb = '_'.lc($_);
2164: $assesslist{$symb} = join(':',('symb',$symb,$uname,$udom,0,0,$_));
2165: }
2166: #
2167: while (my ($key,$srcf) = each(%course_db)) {
2168: next if ($key !~ /^src_(\d+)\.(\d+)$/);
2169: my $mapid = $1;
2170: my $resid = $2;
2171: my $id = $mapid.'.'.$resid;
2172: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
2173: my $symb=
2174: &Apache::lonnet::declutter($course_db{'map_id_'.$mapid}).
2175: '___'.$resid.'___'.&Apache::lonnet::declutter($srcf);
2176: $assesslist{$symb}='symb:'.&Apache::lonnet::escape($symb).':'
2177: .$uname.':'.$udom.':'.$mapid.':'.$resid.':'.
2178: $course_db{'title_'.$id};
2179: }
2180: }
2181: untie(%course_db);
2182: # Store away the data
2183: $sheet->{'rowlabel'} = \%assesslist;
2184: $rowlabel_cache{$identifier}=join('___;___',%{$sheet->{'rowlabel'}});
2185: }
2186:
2187: }
2188:
2189: sub get_assess_rowlabels {
2190: my ($sheet) = @_;
2191: #
2192: my %course_db;
2193: #
2194: my $stype = $sheet->{'sheettype'};
2195: my $uname = $sheet->{'uname'};
2196: my $udom = $sheet->{'udom'};
2197: my $usymb = $sheet->{'usymb'};
2198: #
2199: $sheet->{'rowlabel'} = {};
2200: my $identifier =$sheet->{'coursefilename'}.'_'.$stype.'_'.$usymb;
2201: #
2202: if ($rowlabel_cache{$identifier}) {
2203: %{$sheet->{'rowlabel'}}=split(/___;___/,$rowlabel_cache{$identifier});
2204: } else {
2205: # Get the data and store it in the cache
2206: # Tie hash
2207: tie(%course_db,'GDBM_File',$sheet->{'coursefilename'}.'.db',
2208: &GDBM_READER(),0640);
2209: if (! tied(%course_db)) {
2210: return 'Could not access course data';
2211: }
2212: #
2213: my %parameter_labels=
2214: ('timestamp' =>
2215: 'parameter:Timestamp of Last Transaction<br>timestamp',
2216: 'subnumber' =>
2217: 'parameter:Number of Submissions<br>subnumber',
2218: 'tutornumber' =>
2219: 'parameter:Number of Tutor Responses<br>tutornumber',
2220: 'totalpoints' =>
2221: 'parameter:Total Points Granted<br>totalpoints');
2222: while (my ($key,$srcf) = each(%course_db)) {
2223: next if ($key !~ /^src_(\d+)\.(\d+)$/);
2224: my $mapid = $1;
2225: my $resid = $2;
2226: my $id = $mapid.'.'.$resid;
2227: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
2228: # Loop through the metadata for this key
2229: my @Metadata = split(/,/,
2230: &Apache::lonnet::metadata($srcf,'keys'));
2231: foreach my $key (@Metadata) {
2232: next if ($key !~ /^(stores|parameter)_/);
2233: my $display=
2234: &Apache::lonnet::metadata($srcf,$key.'.display');
2235: unless ($display) {
2236: $display.=
2237: &Apache::lonnet::metadata($srcf,$key.'.name');
2238: }
2239: $display.='<br>'.$key;
2240: $parameter_labels{$key}='parameter:'.$display;
2241: } # end of foreach
2242: }
2243: }
2244: untie(%course_db);
2245: # Store away the results
2246: $sheet->{'rowlabel'} = \%parameter_labels;
2247: $rowlabel_cache{$identifier}=join('___;___',%{$sheet->{'rowlabel'}});
2248: }
2249:
2250: }
2251:
2252: sub updatestudentassesssheet {
2253: my $sheet = shift;
2254: if ($sheet->{'sheettype'} eq 'studentcalc') {
2255: &get_student_rowlabels($sheet);
2256: } else {
2257: &get_assess_rowlabels($sheet);
2258: }
2259: # Determine if any of the information has changed
2260: my %f=&getformulas($sheet);
2261: my $changed=0;
2262:
2263: $sheet->{'maxrow'} = 0;
2264: my %existing=();
2265: # Now obsolete rows
2266: while (my ($cell, $formula) = each (%f)) {
2267: next if ($cell !~ /^A(\d+)/);
2268: $sheet->{'maxrow'} = $1 if ($1 > $sheet->{'maxrow'});
2269: my ($usy,$ufn)=split(/__&&&\__/,$formula);
2270: $existing{$usy}=1;
2271: unless ((exists($sheet->{'rowlabel'}->{$usy}) &&
2272: (defined($sheet->{'rowlabel'}->{$usy})) || (!$1) ||
2273: ($formula =~ /^(~~~|---)/) )) {
2274: $f{$_}='!!! Obsolete';
2275: $changed=1;
2276: } elsif ($ufn) {
2277: # I do not think this works any more
2278: $sheet->{'rowlabel'}->{$usy}
2279: =~s/assesscalc\?usymb\=/assesscalc\?ufn\=$ufn&\usymb\=/;
2280: }
2281: }
2282: # New and unknown keys
2283: foreach (keys(%{$sheet->{'rowlabel'}})) {
2284: unless ($existing{$_}) {
2285: $changed=1;
2286: $sheet->{'maxrow'}++;
2287: $f{'A'.$sheet->{'maxrow'}}=$_;
2288: }
2289: }
2290: if ($changed) {
2291: $sheet->{'f'} = \%f;
2292: &setformulas($sheet);
2293: }
2294: }
2295:
2296: # ------------------------------------------------ Load data for one assessment
2297:
2298: sub loadstudent{
2299: my ($sheet)=@_;
2300: my %c=();
2301: my %f=&getformulas($sheet);
2302: $cachedassess=$sheet->{'uname'}.':'.$sheet->{'udom'};
2303: # Get ALL the student preformance data
2304: my @tmp = &Apache::lonnet::dump($sheet->{'cid'},
2305: $sheet->{'udom'},
2306: $sheet->{'uname'},
2307: undef);
2308: if ($tmp[0] !~ /^error:/) {
2309: %cachedstores = @tmp;
2310: }
2311: undef @tmp;
2312: #
2313: my @assessdata=();
2314: while (my ($cell,$value) = each (%f)) {
2315: next if ($cell !~ /^A(\d+)/);
2316: my $row=$1;
2317: next if (($value =~ /^[!~-]/) || ($row==0));
2318: my ($usy,$ufn)=split(/__&&&\__/,$value);
2319: @assessdata=&exportsheet($sheet,$sheet->{'uname'},
2320: $sheet->{'udom'},
2321: 'assesscalc',$usy,$ufn);
2322: my $index=0;
2323: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
2324: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
2325: if (defined($assessdata[$index])) {
2326: my $col=$_;
2327: if ($assessdata[$index]=~/\D/) {
2328: $c{$col.$row}="'".$assessdata[$index]."'";
2329: } else {
2330: $c{$col.$row}=$assessdata[$index];
2331: }
2332: unless ($col eq 'A') {
2333: $f{$col.$row}='import';
2334: }
2335: }
2336: $index++;
2337: }
2338: }
2339: $cachedassess='';
2340: undef %cachedstores;
2341: $sheet->{'f'} = \%f;
2342: &setformulas($sheet);
2343: &setconstants($sheet,\%c);
2344: }
2345:
2346: # --------------------------------------------------- Load data for one student
2347: #
2348: sub loadcourse {
2349: my ($sheet,$r)=@_;
2350: #
2351: my %c=();
2352: my %f=&getformulas($sheet);
2353: #
2354: my $total=0;
2355: foreach (keys(%f)) {
2356: if ($_=~/^A(\d+)/) {
2357: unless ($f{$_}=~/^[\!\~\-]/) { $total++; }
2358: }
2359: }
2360: my $now=0;
2361: my $since=time;
2362: $r->print(<<ENDPOP);
2363: <script>
2364: popwin=open('','popwin','width=400,height=100');
2365: popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
2366: '<h3>Spreadsheet Calculation Progress</h3>'+
2367: '<form name=popremain>'+
2368: '<input type=text size=35 name=remaining value=Starting></form>'+
2369: '</body></html>');
2370: popwin.document.close();
2371: </script>
2372: ENDPOP
2373: $r->rflush();
2374: foreach (keys(%f)) {
2375: next if ($_!~/^A(\d+)/);
2376: my $row=$1;
2377: next if (($f{$_}=~/^[\!\~\-]/) || ($row==0));
2378: my ($sname,$sdom) = split(':',$f{$_});
2379: my @studentdata=&exportsheet($sheet,$sname,$sdom,'studentcalc');
2380: undef %userrdatas;
2381: $now++;
2382: $r->print('<script>popwin.document.popremain.remaining.value="'.
2383: $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
2384: ' secs remaining";</script>');
2385: $r->rflush();
2386: #
2387: my $index=0;
2388: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
2389: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
2390: if (defined($studentdata[$index])) {
2391: my $col=$_;
2392: if ($studentdata[$index]=~/\D/) {
2393: $c{$col.$row}="'".$studentdata[$index]."'";
2394: } else {
2395: $c{$col.$row}=$studentdata[$index];
2396: }
2397: unless ($col eq 'A') {
2398: $f{$col.$row}='import';
2399: }
2400: }
2401: $index++;
2402: }
2403: }
2404: $sheet->{'f'}=\%f;
2405: &setformulas($sheet);
2406: &setconstants($sheet,\%c);
2407: $r->print('<script>popwin.close()</script>');
2408: $r->rflush();
2409: }
2410:
2411: # ------------------------------------------------ Load data for one assessment
2412: #
2413: sub loadassessment {
2414: my ($sheet)=@_;
2415:
2416: my $uhome = $sheet->{'uhome'};
2417: my $uname = $sheet->{'uname'};
2418: my $udom = $sheet->{'udom'};
2419: my $symb = $sheet->{'usymb'};
2420: my $cid = $sheet->{'cid'};
2421: my $cnum = $sheet->{'cnum'};
2422: my $cdom = $sheet->{'cdom'};
2423: my $chome = $sheet->{'chome'};
2424:
2425: my $namespace;
2426: unless ($namespace=$cid) { return ''; }
2427: # Get stored values
2428: my %returnhash=();
2429: if ($cachedassess eq $uname.':'.$udom) {
2430: #
2431: # get data out of the dumped stores
2432: #
2433: my $version=$cachedstores{'version:'.$symb};
2434: my $scope;
2435: for ($scope=1;$scope<=$version;$scope++) {
2436: foreach (split(/\:/,$cachedstores{$scope.':keys:'.$symb})) {
2437: $returnhash{$_}=$cachedstores{$scope.':'.$symb.':'.$_};
2438: }
2439: }
2440: } else {
2441: #
2442: # restore individual
2443: #
2444: %returnhash = &Apache::lonnet::restore($symb,$namespace,$udom,$uname);
2445: for (my $version=1;$version<=$returnhash{'version'};$version++) {
2446: foreach (split(/\:/,$returnhash{$version.':keys'})) {
2447: $returnhash{$_}=$returnhash{$version.':'.$_};
2448: }
2449: }
2450: }
2451: #
2452: # returnhash now has all stores for this resource
2453: # convert all "_" to "." to be able to use libraries, multiparts, etc
2454: #
2455: # This is dumb. It is also necessary :(
2456: my @oldkeys=keys %returnhash;
2457: #
2458: foreach my $name (@oldkeys) {
2459: my $value=$returnhash{$name};
2460: delete $returnhash{$name};
2461: $name=~s/\_/\./g;
2462: $returnhash{$name}=$value;
2463: }
2464: # initialize coursedata and userdata for this user
2465: undef %courseopt;
2466: undef %useropt;
2467:
2468: my $userprefix=$uname.'_'.$udom.'_';
2469:
2470: unless ($uhome eq 'no_host') {
2471: # Get coursedata
2472: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
2473: my %Tmp = &Apache::lonnet::dump('resourcedata',$cdom,$cnum);
2474: $courserdatas{$cid}=\%Tmp;
2475: $courserdatas{$cid.'.last_cache'}=time;
2476: }
2477: while (my ($name,$value) = each(%{$courserdatas{$cid}})) {
2478: $courseopt{$userprefix.$name}=$value;
2479: }
2480: # Get userdata (if present)
2481: unless ((time-$userrdatas{$uname.'@'.$udom.'.last_cache'})<240) {
2482: my %Tmp = &Apache::lonnet::dump('resourcedata',$udom,$uname);
2483: $userrdatas{$cid} = \%Tmp;
2484: # Most of the time the user does not have a 'resourcedata.db'
2485: # file. We need to cache that we got nothing instead of bothering
2486: # with requesting it every time.
2487: $userrdatas{$uname.'@'.$udom.'.last_cache'}=time;
2488: }
2489: while (my ($name,$value) = each(%{$userrdatas{$cid}})) {
2490: $useropt{$userprefix.$name}=$value;
2491: }
2492: }
2493: # now courseopt, useropt initialized for this user and course
2494: # (used by parmval)
2495: #
2496: # Load keys for this assessment only
2497: #
2498: my %thisassess=();
2499: my ($symap,$syid,$srcf)=split(/\_\_\_/,$symb);
2500: foreach (split(/\,/,&Apache::lonnet::metadata($srcf,'keys'))) {
2501: $thisassess{$_}=1;
2502: }
2503: #
2504: # Load parameters
2505: #
2506: my %c=();
2507: if (tie(%parmhash,'GDBM_File',
2508: $sheet->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
2509: my %f=&getformulas($sheet);
2510: foreach my $cell (keys(%f)) {
2511: next if ($cell !~ /^A/);
2512: next if ($f{$cell} =~/^[\!\~\-]/);
2513: if ($f{$cell}=~/^parameter/) {
2514: if (defined($thisassess{$f{$cell}})) {
2515: my $val = &parmval($f{$cell},$sheet);
2516: $c{$cell} = $val;
2517: $c{$f{$cell}} = $val;
2518: }
2519: } else {
2520: my $key=$f{$cell};
2521: my $ckey=$key;
2522: $key=~s/^stores\_/resource\./;
2523: $key=~s/\_/\./g;
2524: $c{$cell}=$returnhash{$key};
2525: $c{$ckey}=$returnhash{$key};
2526: }
2527: }
2528: untie(%parmhash);
2529: }
2530: &setconstants($sheet,\%c);
2531: }
2532:
2533: # --------------------------------------------------------- Various form fields
2534:
2535: sub textfield {
2536: my ($title,$name,$value)=@_;
2537: return "\n<p><b>$title:</b><br>".
2538: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
2539: }
2540:
2541: sub hiddenfield {
2542: my ($name,$value)=@_;
2543: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
2544: }
2545:
2546: sub selectbox {
2547: my ($title,$name,$value,%options)=@_;
2548: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
2549: foreach (sort keys(%options)) {
2550: $selout.='<option value="'.$_.'"';
2551: if ($_ eq $value) { $selout.=' selected'; }
2552: $selout.='>'.$options{$_}.'</option>';
2553: }
2554: return $selout.'</select>';
2555: }
2556:
2557: # =============================================== Update information in a sheet
2558: #
2559: # Add new users or assessments, etc.
2560: #
2561:
2562: sub updatesheet {
2563: my ($sheet)=@_;
2564: if ($sheet->{'sheettype'} eq 'classcalc') {
2565: return &updateclasssheet($sheet);
2566: } else {
2567: return &updatestudentassesssheet($sheet);
2568: }
2569: }
2570:
2571: # =================================================== Load the rows for a sheet
2572: #
2573: # Import the data for rows
2574: #
2575:
2576: sub loadrows {
2577: my ($sheet,$r)=@_;
2578: my $stype=$sheet->{'sheettype'};
2579: if ($stype eq 'classcalc') {
2580: &loadcourse($sheet,$r);
2581: } elsif ($stype eq 'studentcalc') {
2582: &loadstudent($sheet);
2583: } else {
2584: &loadassessment($sheet);
2585: }
2586: }
2587:
2588: # ======================================================= Forced recalculation?
2589:
2590: sub checkthis {
2591: my ($keyname,$time)=@_;
2592: return ($time<$expiredates{$keyname});
2593: }
2594:
2595: sub forcedrecalc {
2596: my ($uname,$udom,$stype,$usymb)=@_;
2597: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2598: my $time=$oldsheets{$key.'.time'};
2599: if ($ENV{'form.forcerecalc'}) { return 1; }
2600: unless ($time) { return 1; }
2601: if ($stype eq 'assesscalc') {
2602: my $map=(split(/___/,$usymb))[0];
2603: if (&checkthis('::assesscalc:',$time) ||
2604: &checkthis('::assesscalc:'.$map,$time) ||
2605: &checkthis('::assesscalc:'.$usymb,$time) ||
2606: &checkthis($uname.':'.$udom.':assesscalc:',$time) ||
2607: &checkthis($uname.':'.$udom.':assesscalc:'.$map,$time) ||
2608: &checkthis($uname.':'.$udom.':assesscalc:'.$usymb,$time)) {
2609: return 1;
2610: }
2611: } else {
2612: if (&checkthis('::studentcalc:',$time) ||
2613: &checkthis($uname.':'.$udom.':studentcalc:',$time)) {
2614: return 1;
2615: }
2616: }
2617: return 0;
2618: }
2619:
2620: # ============================================================== Export handler
2621: # exportsheet
2622: # returns the export row for a spreadsheet.
2623: #
2624: sub exportsheet {
2625: my ($sheet,$uname,$udom,$stype,$usymb,$fn)=@_;
2626: $uname = $uname || $sheet->{'uname'};
2627: $udom = $udom || $sheet->{'udom'};
2628: $stype = $stype || $sheet->{'sheettype'};
2629: my @exportarr=();
2630: if (defined($usymb) && ($usymb=~/^\_(\w+)/) &&
2631: (!defined($fn) || $fn eq '')) {
2632: $fn='default_'.$1;
2633: }
2634: #
2635: # Check if cached
2636: #
2637: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2638: my $found='';
2639: if ($oldsheets{$key}) {
2640: foreach (split(/___&\___/,$oldsheets{$key})) {
2641: my ($name,$value)=split(/___=___/,$_);
2642: if ($name eq $fn) {
2643: $found=$value;
2644: }
2645: }
2646: }
2647: unless ($found) {
2648: &cachedssheets($sheet,$uname,$udom);
2649: if ($oldsheets{$key}) {
2650: foreach (split(/___&\___/,$oldsheets{$key})) {
2651: my ($name,$value)=split(/___=___/,$_);
2652: if ($name eq $fn) {
2653: $found=$value;
2654: }
2655: }
2656: }
2657: }
2658: #
2659: # Check if still valid
2660: #
2661: if ($found) {
2662: if (&forcedrecalc($uname,$udom,$stype,$usymb)) {
2663: $found='';
2664: }
2665: }
2666: if ($found) {
2667: #
2668: # Return what was cached
2669: #
2670: @exportarr=split(/___;___/,$found);
2671: return @exportarr;
2672: }
2673: #
2674: # Not cached
2675: #
2676: my ($newsheet)=&makenewsheet($uname,$udom,$stype,$usymb);
2677: &readsheet($newsheet,$fn);
2678: &updatesheet($newsheet);
2679: &loadrows($newsheet);
2680: &calcsheet($newsheet);
2681: @exportarr=&exportdata($newsheet);
2682: ##
2683: ## Store now
2684: ##
2685: #
2686: # load in the old value
2687: #
2688: my %currentlystored=();
2689: if ($stype eq 'studentcalc') {
2690: my @tmp = &Apache::lonnet::get('nohist_calculatedsheets',
2691: [$key],
2692: $sheet->{'cdom'},$sheet->{'cnum'});
2693: if ($tmp[0]!~/^error/) {
2694: %currentlystored = @tmp;
2695: }
2696: } else {
2697: my @tmp = &Apache::lonnet::get('nohist_calculatedsheets_'.
2698: $sheet->{'cid'},[$key],
2699: $sheet->{'udom'},$sheet->{'uname'});
2700: if ($tmp[0]!~/^error/) {
2701: %currentlystored = @tmp;
2702: }
2703: }
2704: #
2705: # Add the new line
2706: #
2707: $currentlystored{$fn}=join('___;___',@exportarr);
2708: #
2709: # Stick everything back together
2710: #
2711: my $newstore='';
2712: foreach (keys(%currentlystored)) {
2713: if ($newstore) { $newstore.='___&___'; }
2714: $newstore.=$_.'___=___'.$currentlystored{$_};
2715: }
2716: my $now=time;
2717: #
2718: # Store away the new value
2719: #
2720: if ($stype eq 'studentcalc') {
2721: &Apache::lonnet::put('nohist_calculatedsheets',
2722: { $key => $newstore,
2723: $key.time => $now },
2724: $sheet->{'cdom'},$sheet->{'cnum'});
2725: } else {
2726: &Apache::lonnet::put('nohist_calculatedsheets_'.$sheet->{'cid'},
2727: { $key => $newstore,
2728: $key.time => $now },
2729: $sheet->{'udom'},
2730: $sheet->{'uname'})
2731: }
2732: return @exportarr;
2733: }
2734:
2735: # ============================================================ Expiration Dates
2736: #
2737: # Load previously cached student spreadsheets for this course
2738: #
2739: sub load_spreadsheet_expirationdates {
2740: undef %expiredates;
2741: my $cid=$ENV{'request.course.id'};
2742: my @tmp = &Apache::lonnet::dump('nohist_expirationdates',
2743: $ENV{'course.'.$cid.'.domain'},
2744: $ENV{'course.'.$cid.'.num'});
2745: if (lc($tmp[0])!~/^error/){
2746: %expiredates = @tmp;
2747: }
2748: }
2749:
2750: # ===================================================== Calculated sheets cache
2751: #
2752: # Load previously cached student spreadsheets for this course
2753: #
2754:
2755: sub cachedcsheets {
2756: my $cid=$ENV{'request.course.id'};
2757: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
2758: $ENV{'course.'.$cid.'.domain'},
2759: $ENV{'course.'.$cid.'.num'});
2760: if ($tmp[0] !~ /^error/) {
2761: my %StupidTempHash = @tmp;
2762: while (my ($key,$value) = each %StupidTempHash) {
2763: $oldsheets{$key} = $value;
2764: }
2765: }
2766: }
2767:
2768: # ===================================================== Calculated sheets cache
2769: #
2770: # Load previously cached assessment spreadsheets for this student
2771: #
2772:
2773: sub cachedssheets {
2774: my ($sheet,$uname,$udom) = @_;
2775: $uname = $uname || $sheet->{'uname'};
2776: $udom = $udom || $sheet->{'udom'};
2777: if (! $loadedcaches{$uname.'_'.$udom}) {
2778: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
2779: $sheet->{'udom'},
2780: $sheet->{'uname'});
2781: if ($tmp[0] !~ /^error/) {
2782: my %TempHash = @tmp;
2783: my $count = 0;
2784: while (my ($key,$value) = each %TempHash) {
2785: $oldsheets{$key} = $value;
2786: $count++;
2787: }
2788: $loadedcaches{$sheet->{'uname'}.'_'.$sheet->{'udom'}}=1;
2789: }
2790: }
2791:
2792: }
2793:
2794: # ===================================================== Calculated sheets cache
2795: #
2796: # Load previously cached assessment spreadsheets for this student
2797: #
2798:
2799: # ================================================================ Main handler
2800: #
2801: # Interactive call to screen
2802: #
2803: #
2804: sub handler {
2805: my $r=shift;
2806:
2807: my ($sheettype) = ($r->uri=~/\/(\w+)$/);
2808:
2809: if (! exists($ENV{'form.Status'})) {
2810: $ENV{'form.Status'} = 'Active';
2811: }
2812: if ( ! exists($ENV{'form.output'}) ||
2813: ($sheettype ne 'classcalc' &&
2814: lc($ENV{'form.output'}) eq 'recursive excel')) {
2815: $ENV{'form.output'} = 'HTML';
2816: }
2817: #
2818: # Overload checking
2819: #
2820: # Check this server
2821: my $loaderror=&Apache::lonnet::overloaderror($r);
2822: if ($loaderror) { return $loaderror; }
2823: # Check the course homeserver
2824: $loaderror= &Apache::lonnet::overloaderror($r,
2825: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
2826: if ($loaderror) { return $loaderror; }
2827: #
2828: # HTML Header
2829: #
2830: if ($r->header_only) {
2831: $r->content_type('text/html');
2832: $r->send_http_header;
2833: return OK;
2834: }
2835: #
2836: # Global directory configs
2837: #
2838: $includedir = $r->dir_config('lonIncludes');
2839: $tmpdir = $r->dir_config('lonDaemons').'/tmp/';
2840: #
2841: # Roles Checking
2842: #
2843: # Needs to be in a course
2844: if (! $ENV{'request.course.fn'}) {
2845: # Not in a course, or not allowed to modify parms
2846: $ENV{'user.error.msg'}=
2847: $r->uri.":opa:0:0:Cannot modify spreadsheet";
2848: return HTTP_NOT_ACCEPTABLE;
2849: }
2850: #
2851: # Get query string for limited number of parameters
2852: #
2853: &Apache::loncommon::get_unprocessed_cgi
2854: ($ENV{'QUERY_STRING'},['uname','udom','usymb','ufn','mapid','resid']);
2855: #
2856: # Deal with restricted student permissions
2857: #
2858: if ($ENV{'request.role'} =~ /^st\./) {
2859: delete $ENV{'form.unewfield'} if (exists($ENV{'form.unewfield'}));
2860: delete $ENV{'form.unewformula'} if (exists($ENV{'form.unewformula'}));
2861: }
2862: #
2863: # Clean up symb and spreadsheet filename
2864: #
2865: if (($ENV{'form.usymb'}=~/^\_(\w+)/) && (!$ENV{'form.ufn'})) {
2866: $ENV{'form.ufn'}='default_'.$1;
2867: }
2868: #
2869: # Interactive loading of specific sheet?
2870: #
2871: if (($ENV{'form.load'}) && ($ENV{'form.loadthissheet'} ne 'Default')) {
2872: $ENV{'form.ufn'}=$ENV{'form.loadthissheet'};
2873: }
2874: #
2875: # Determine the user name and domain for the sheet.
2876: my $aname;
2877: my $adom;
2878: unless ($ENV{'form.uname'}) {
2879: $aname=$ENV{'user.name'};
2880: $adom=$ENV{'user.domain'};
2881: } else {
2882: $aname=$ENV{'form.uname'};
2883: $adom=$ENV{'form.udom'};
2884: }
2885: #
2886: # Open page, try to prevent browser cache.
2887: #
2888: $r->content_type('text/html');
2889: $r->header_out('Cache-control','no-cache');
2890: $r->header_out('Pragma','no-cache');
2891: $r->send_http_header;
2892: #
2893: # Header....
2894: #
2895: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
2896: my $nothing = "''";
2897: if ($ENV{'browser.type'} eq 'explorer') {
2898: $nothing = "'javascript:void(0);'";
2899: }
2900:
2901: if ($ENV{'request.role'} !~ /^st\./) {
2902: $r->print(<<ENDSCRIPT);
2903: <script language="JavaScript">
2904:
2905: var editwin;
2906:
2907: function celledit(cellname,cellformula) {
2908: var edit_text = '';
2909: edit_text +='<html><head><title>Cell Edit Window</title></head><body>';
2910: edit_text += '<form name="editwinform">';
2911: edit_text += '<center><h3>Cell '+cellname+'</h3>';
2912: edit_text += '<textarea name="newformula" cols="40" rows="6"';
2913: edit_text += ' wrap="off" >'+cellformula+'</textarea>';
2914: edit_text += '</br>';
2915: edit_text += '<input type="button" name="accept" value="Accept"';
2916: edit_text += ' onClick=\\\'javascript:';
2917: edit_text += 'opener.document.sheet.unewfield.value=';
2918: edit_text += '"'+cellname+'";';
2919: edit_text += 'opener.document.sheet.unewformula.value=';
2920: edit_text += 'document.editwinform.newformula.value;';
2921: edit_text += 'opener.document.sheet.submit();';
2922: edit_text += 'self.close()\\\' />';
2923: edit_text += ' ';
2924: edit_text += '<input type="button" name="abort" ';
2925: edit_text += 'value="Discard Changes"';
2926: edit_text += ' onClick="javascript:self.close()" />';
2927: edit_text += '</center></body></html>';
2928:
2929: if (editwin != null && !(editwin.closed) ) {
2930: editwin.close();
2931: }
2932:
2933: editwin = window.open($nothing,'CellEditWin','height=200,width=350,scrollbars=no,resizeable=yes,alwaysRaised=yes,dependent=yes',true);
2934: editwin.document.write(edit_text);
2935: }
2936:
2937: function changesheet(cn) {
2938: document.sheet.unewfield.value=cn;
2939: document.sheet.unewformula.value='changesheet';
2940: document.sheet.submit();
2941: }
2942:
2943: function insertrow(cn) {
2944: document.sheet.unewfield.value='insertrow';
2945: document.sheet.unewformula.value=cn;
2946: document.sheet.submit();
2947: }
2948:
2949: </script>
2950: ENDSCRIPT
2951: }
2952: $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
2953: '<form action="'.$r->uri.'" name="sheet" method="post">');
2954: $r->print(&hiddenfield('uname',$ENV{'form.uname'}).
2955: &hiddenfield('udom',$ENV{'form.udom'}).
2956: &hiddenfield('usymb',$ENV{'form.usymb'}).
2957: &hiddenfield('unewfield','').
2958: &hiddenfield('unewformula',''));
2959: $r->rflush();
2960: #
2961: # Full recalc?
2962: #
2963: if ($ENV{'form.forcerecalc'}) {
2964: $r->print('<h4>Completely Recalculating Sheet ...</h4>');
2965: undef %spreadsheets;
2966: undef %courserdatas;
2967: undef %userrdatas;
2968: undef %defaultsheets;
2969: undef %rowlabel_cache;
2970: }
2971: # Read new sheet or modified worksheet
2972: my ($sheet)=&makenewsheet($aname,$adom,$sheettype,$ENV{'form.usymb'});
2973: #
2974: # Check user permissions
2975: if (($sheet->{'sheettype'} eq 'classcalc' ) ||
2976: ($sheet->{'uname'} ne $ENV{'user.name'} ) ||
2977: ($sheet->{'udom'} ne $ENV{'user.domain'})) {
2978: unless (&Apache::lonnet::allowed('vgr',$sheet->{'cid'})) {
2979: $r->print('<h1>Access Permission Denied</h1>'.
2980: '</form></body></html>');
2981: return OK;
2982: }
2983: }
2984: # Print out user information
2985: $r->print('<h2>'.$sheet->{'coursedesc'}.'</h2>');
2986: if ($sheet->{'sheettype'} ne 'classcalc') {
2987: $r->print('<h2>'.&gettitle($sheet).'</h2><p>');
2988: }
2989: if ($sheet->{'sheettype'} eq 'assesscalc') {
2990: $r->print('<b>User:</b> '.$sheet->{'uname'}.
2991: '<br /><b>Domain:</b> '.$sheet->{'udom'}.'<br />');
2992: }
2993: if ($sheet->{'sheettype'} eq 'studentcalc' ||
2994: $sheet->{'sheettype'} eq 'assesscalc') {
2995: $r->print('<b>Section/Group:</b>'.$sheet->{'csec'}.'</p>');
2996: }
2997: #
2998: # If a new formula had been entered, go from work copy
2999: if ($ENV{'form.unewfield'}) {
3000: $r->print('<h2>Modified Workcopy</h2>');
3001: $ENV{'form.unewformula'}=~s/\'/\"/g;
3002: $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
3003: $ENV{'form.unewformula'}.'<p>');
3004: $sheet->{'filename'} = $ENV{'form.ufn'};
3005: &tmpread($sheet,$ENV{'form.unewfield'},$ENV{'form.unewformula'});
3006: } elsif ($ENV{'form.saveas'}) {
3007: $sheet->{'filename'} = $ENV{'form.ufn'};
3008: &tmpread($sheet);
3009: } else {
3010: &readsheet($sheet,$ENV{'form.ufn'});
3011: }
3012: # Additional options
3013: if ($sheet->{'sheettype'} eq 'assesscalc') {
3014: $r->print('<p><font size=+2>'.
3015: '<a href="/adm/studentcalc?'.
3016: 'uname='.$sheet->{'uname'}.
3017: '&udom='.$sheet->{'udom'}.'">'.
3018: 'Level up: Student Sheet</a></font></p>');
3019: }
3020: if (($sheet->{'sheettype'} eq 'studentcalc') &&
3021: (&Apache::lonnet::allowed('vgr',$sheet->{'cid'}))) {
3022: $r->print ('<p><font size=+2><a href="/adm/classcalc">'.
3023: 'Level up: Course Sheet</a></font></p>');
3024: }
3025: # Recalc button
3026: $r->print('<br />'.
3027: '<input type="submit" name="forcerecalc" '.
3028: 'value="Completely Recalculate Sheet"></p>');
3029: # Save dialog
3030: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
3031: my $fname=$ENV{'form.ufn'};
3032: $fname=~s/\_[^\_]+$//;
3033: if ($fname eq 'default') { $fname='course_default'; }
3034: $r->print('<input type=submit name=saveas value="Save as ...">'.
3035: '<input type=text size=20 name=newfn value="'.$fname.'">'.
3036: 'make default: <input type=checkbox name="makedefufn"><p>');
3037: }
3038: $r->print(&hiddenfield('ufn',$sheet->{'filename'}));
3039: # Load dialog
3040: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
3041: $r->print('<p><input type=submit name=load value="Load ...">'.
3042: '<select name="loadthissheet">'.
3043: '<option name="default">Default</option>');
3044: foreach (&othersheets($sheet)) {
3045: $r->print('<option name="'.$_.'"');
3046: if ($ENV{'form.ufn'} eq $_) {
3047: $r->print(' selected');
3048: }
3049: $r->print('>'.$_.'</option>');
3050: }
3051: $r->print('</select><p>');
3052: if ($sheet->{'sheettype'} eq 'studentcalc') {
3053: &setothersheets($sheet,
3054: &othersheets($sheet,'assesscalc'));
3055: }
3056: }
3057: #
3058: # Set up caching mechanisms
3059: #
3060: &load_spreadsheet_expirationdates();
3061: # Clear out old caches if we have not seen this class before.
3062: if (exists($oldsheets{'course'}) &&
3063: $oldsheets{'course'} ne $sheet->{'cid'}) {
3064: undef %oldsheets;
3065: undef %loadedcaches;
3066: }
3067: $oldsheets{'course'} = $sheet->{'cid'};
3068: #
3069: if ($sheet->{'sheettype'} eq 'classcalc') {
3070: $r->print("Loading previously calculated student sheets ...\n");
3071: $r->rflush();
3072: &cachedcsheets();
3073: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
3074: $r->print("Loading previously calculated assessment sheets ...\n");
3075: $r->rflush();
3076: &cachedssheets($sheet);
3077: }
3078: # Update sheet, load rows
3079: $r->print("Loaded sheet(s), updating rows ...<br>\n");
3080: $r->rflush();
3081: #
3082: &updatesheet($sheet);
3083: $r->print("Updated rows, loading row data ...\n");
3084: $r->rflush();
3085: #
3086: &loadrows($sheet,$r);
3087: $r->print("Loaded row data, calculating sheet ...<br>\n");
3088: $r->rflush();
3089: #
3090: my $calcoutput=&calcsheet($sheet);
3091: $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
3092: # See if something to save
3093: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
3094: my $fname='';
3095: if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
3096: $fname=~s/\W/\_/g;
3097: if ($fname eq 'default') { $fname='course_default'; }
3098: $fname.='_'.$sheet->{'sheettype'};
3099: $sheet->{'filename'} = $fname;
3100: $ENV{'form.ufn'}=$fname;
3101: $r->print('<p>Saving spreadsheet: '.
3102: &writesheet($sheet,$ENV{'form.makedefufn'}).
3103: '<p>');
3104: }
3105: }
3106: #
3107: # Write the modified worksheet
3108: $r->print('<b>Current sheet:</b> '.$sheet->{'filename'}.'</p>');
3109: &tmpwrite($sheet);
3110: if ($sheet->{'sheettype'} eq 'studentcalc') {
3111: $r->print('<p>Show rows with empty A column: ');
3112: } else {
3113: $r->print('<p>>Show empty rows: ');
3114: }
3115: #
3116: $r->print(&hiddenfield('userselhidden','true').
3117: '<input type="checkbox" name="showall" onClick="submit()"');
3118: #
3119: if ($ENV{'form.showall'}) {
3120: $r->print(' checked');
3121: } else {
3122: unless ($ENV{'form.userselhidden'}) {
3123: unless
3124: ($ENV{'course.'.$sheet->{'cid'}.'.hideemptyrows'} eq 'yes') {
3125: $r->print(' checked');
3126: $ENV{'form.showall'}=1;
3127: }
3128: }
3129: }
3130: $r->print('>');
3131: #
3132: # CSV format checkbox (classcalc sheets only)
3133: $r->print(' Output as <select name="output" size="1" onClick="submit()">'.
3134: "\n");
3135: foreach my $mode (qw/HTML CSV Excel/) {
3136: $r->print('<option value="'.$mode.'"');
3137: if ($ENV{'form.output'} eq $mode) {
3138: $r->print(' selected ');
3139: }
3140: $r->print('>'.$mode.'</option>'."\n");
3141: }
3142: if ($sheet->{'sheettype'} eq 'classcalc') {
3143: $r->print('<option value="recursive excel"');
3144: if ($ENV{'form.output'} eq 'recursive excel') {
3145: $r->print(' selected ');
3146: }
3147: $r->print(">Multi-Sheet Excel</option>\n");
3148: }
3149: $r->print("</select>\n");
3150: #
3151: if ($sheet->{'sheettype'} eq 'classcalc') {
3152: $r->print(' Student Status: '.
3153: &Apache::lonhtmlcommon::StatusOptions
3154: ($ENV{'form.Status'},'sheet'));
3155: }
3156: #
3157: # Buttons to insert rows
3158: # $r->print(<<ENDINSERTBUTTONS);
3159: #<br>
3160: #<input type='button' onClick='insertrow("top");'
3161: #value='Insert Row Top'>
3162: #<input type='button' onClick='insertrow("bottom");'
3163: #value='Insert Row Bottom'><br>
3164: #ENDINSERTBUTTONS
3165: # Print out sheet
3166: &outsheet($r,$sheet);
3167: $r->print('</form></body></html>');
3168: # Done
3169: return OK;
3170: }
3171:
3172: 1;
3173: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>