Annotation of loncom/interface/lonspreadsheet.pm, revision 1.136
1.79 matthew 1: #
1.136 ! matthew 2: # $Id: lonspreadsheet.pm,v 1.135 2002/11/07 15:37:02 matthew Exp $
1.79 matthew 3: #
4: # Copyright Michigan State University Board of Trustees
5: #
6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
7: #
8: # LON-CAPA is free software; you can redistribute it and/or modify
9: # it under the terms of the GNU General Public License as published by
10: # the Free Software Foundation; either version 2 of the License, or
11: # (at your option) any later version.
12: #
13: # LON-CAPA is distributed in the hope that it will be useful,
14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: # GNU General Public License for more details.
17: #
18: # You should have received a copy of the GNU General Public License
19: # along with LON-CAPA; if not, write to the Free Software
20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: #
22: # /home/httpd/html/adm/gpl.txt
23: #
24: # http://www.lon-capa.org/
25: #
1.1 www 26: # The LearningOnline Network with CAPA
27: # Spreadsheet/Grades Display Handler
28: #
1.80 matthew 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
1.1 www 52:
53: package Apache::lonspreadsheet;
1.36 www 54:
1.1 www 55: use strict;
56: use Safe;
1.3 www 57: use Safe::Hole;
1.1 www 58: use Opcode;
59: use Apache::lonnet;
1.7 www 60: use Apache::Constants qw(:common :http);
1.19 www 61: use GDBM_File;
1.3 www 62: use HTML::TokeParser;
1.98 matthew 63: use Apache::lonhtmlcommon;
1.118 matthew 64: use Apache::loncoursedata;
1.134 matthew 65: use Apache::File();
66: use Spreadsheet::WriteExcel;
1.136 ! matthew 67:
1.11 www 68: #
1.113 matthew 69: # Caches for coursewide information
70: #
71: my %Section;
72:
73: #
1.44 www 74: # Caches for previously calculated spreadsheets
75: #
76:
77: my %oldsheets;
1.46 www 78: my %loadedcaches;
1.47 www 79: my %expiredates;
1.44 www 80:
81: #
1.39 www 82: # Cache for stores of an individual user
83: #
84:
85: my $cachedassess;
86: my %cachedstores;
87:
88: #
1.11 www 89: # These cache hashes need to be independent of user, resource and course
1.27 www 90: # (user and course can/should be in the keys)
1.11 www 91: #
1.33 www 92:
93: my %spreadsheets;
94: my %courserdatas;
95: my %userrdatas;
96: my %defaultsheets;
1.136 ! matthew 97: my %rowlabel_cache;
1.27 www 98:
1.11 www 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:
1.95 www 107: #
108: # Some hashes for stats on timing and performance
109: #
110:
111: my %starttimes;
112: my %usedtimes;
1.96 www 113: my %numbertimes;
1.95 www 114:
1.28 www 115: # Stuff that only the screen handler can know
116:
117: my $includedir;
118: my $tmpdir;
119:
1.5 www 120: # =============================================================================
121: # ===================================== Implements an instance of a spreadsheet
1.4 www 122:
1.118 matthew 123: ##
124: ## mask - used to reside in the safe space.
125: ##
1.1 www 126: sub mask {
127: my ($lower,$upper)=@_;
1.132 matthew 128: $upper = $lower if (! defined($upper));
1.125 matthew 129: #
130: my ($la,$ld) = ($lower=~/([A-Za-z]|\*)(\d+|\*)/);
131: my ($ua,$ud) = ($upper=~/([A-Za-z]|\*)(\d+|\*)/);
132: #
1.1 www 133: my $alpha='';
134: my $num='';
1.125 matthew 135: #
1.1 www 136: if (($la eq '*') || ($ua eq '*')) {
1.132 matthew 137: $alpha='[A-Za-z]';
1.1 www 138: } else {
1.7 www 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: }
1.1 www 145: }
146: if (($ld eq '*') || ($ud eq '*')) {
147: $num='\d+';
148: } else {
149: if (length($ld)!=length($ud)) {
150: $num.='(';
1.78 matthew 151: foreach ($ld=~m/\d/g) {
1.1 www 152: $num.='['.$_.'-9]';
1.78 matthew 153: }
1.1 www 154: if (length($ud)-length($ld)>1) {
155: $num.='|\d{'.(length($ld)+1).','.(length($ud)-1).'}';
156: }
157: $num.='|';
1.78 matthew 158: foreach ($ud=~m/\d/g) {
1.1 www 159: $num.='[0-'.$_.']';
1.78 matthew 160: }
1.1 www 161: $num.=')';
162: } else {
163: my @lda=($ld=~m/\d/g);
164: my @uda=($ud=~m/\d/g);
1.118 matthew 165: my $i;
166: my $j=0;
167: my $notdone=1;
1.7 www 168: for ($i=0;($i<=$#lda)&&($notdone);$i++) {
1.1 www 169: if ($lda[$i]==$uda[$i]) {
170: $num.=$lda[$i];
171: $j=$i;
1.7 www 172: } else {
173: $notdone=0;
1.1 www 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 {
1.125 matthew 191: if ($lda[-1]!=$uda[-1]) {
192: $num.='['.$lda[-1].'-'.$uda[-1].']';
1.7 www 193: }
1.1 www 194: }
195: }
196: }
1.4 www 197: return '^'.$alpha.$num."\$";
1.80 matthew 198: }
199:
1.118 matthew 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:
1.119 matthew 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.
1.132 matthew 242: #$errorlog = '';
1.118 matthew 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:
1.80 matthew 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
1.104 matthew 403: my @Temp = grep /$keymask/,keys(%sheet_values);
404: @Keys = $sheet_values{@Temp};
1.80 matthew 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);
1.104 matthew 415: my @Temp = grep /$valmask/,keys(%sheet_values);
416: @Values =$sheet_values{@Temp};
1.80 matthew 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+$/) {
1.104 matthew 450: $key = $sheet_values{$key};
1.80 matthew 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
1.104 matthew 507: my @Temp = grep /$keymask/,keys(%sheet_values);
508: @Keys = $sheet_values{@Temp};
1.80 matthew 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);
1.104 matthew 520: my @Temp = grep /$valmask/,keys(%sheet_values);
521: @Values =$sheet_values{@Temp};
1.80 matthew 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];
1.1 www 537: }
538:
1.84 matthew 539: #-------------------------------------------------------
540:
541: =item NUM(range)
542:
543: returns the number of items in the range.
544:
545: =cut
546:
547: #-------------------------------------------------------
1.1 www 548: sub NUM {
549: my $mask=mask(@_);
1.104 matthew 550: my $num= $#{@{grep(/$mask/,keys(%sheet_values))}}+1;
1.1 www 551: return $num;
552: }
553:
554: sub BIN {
555: my ($low,$high,$lower,$upper)=@_;
556: my $mask=mask($lower,$upper);
557: my $num=0;
1.104 matthew 558: foreach (grep /$mask/,keys(%sheet_values)) {
559: if (($sheet_values{$_}>=$low) && ($sheet_values{$_}<=$high)) {
1.1 www 560: $num++;
561: }
1.78 matthew 562: }
1.1 www 563: return $num;
564: }
565:
566:
1.84 matthew 567: #-------------------------------------------------------
568:
569: =item SUM(range)
570:
571: returns the sum of items in the range.
572:
573: =cut
574:
575: #-------------------------------------------------------
1.1 www 576: sub SUM {
577: my $mask=mask(@_);
578: my $sum=0;
1.104 matthew 579: foreach (grep /$mask/,keys(%sheet_values)) {
580: $sum+=$sheet_values{$_};
1.78 matthew 581: }
1.1 www 582: return $sum;
583: }
584:
1.84 matthew 585: #-------------------------------------------------------
586:
587: =item MEAN(range)
588:
589: compute the average of the items in the range.
590:
591: =cut
592:
593: #-------------------------------------------------------
1.1 www 594: sub MEAN {
595: my $mask=mask(@_);
1.132 matthew 596: my $sum=0;
597: my $num=0;
1.104 matthew 598: foreach (grep /$mask/,keys(%sheet_values)) {
599: $sum+=$sheet_values{$_};
1.1 www 600: $num++;
1.78 matthew 601: }
1.1 www 602: if ($num) {
603: return $sum/$num;
604: } else {
605: return undef;
606: }
607: }
608:
1.84 matthew 609: #-------------------------------------------------------
610:
611: =item STDDEV(range)
612:
613: compute the standard deviation of the items in the range.
614:
615: =cut
616:
617: #-------------------------------------------------------
1.1 www 618: sub STDDEV {
619: my $mask=mask(@_);
620: my $sum=0; my $num=0;
1.104 matthew 621: foreach (grep /$mask/,keys(%sheet_values)) {
622: $sum+=$sheet_values{$_};
1.1 www 623: $num++;
1.78 matthew 624: }
1.1 www 625: unless ($num>1) { return undef; }
626: my $mean=$sum/$num;
627: $sum=0;
1.104 matthew 628: foreach (grep /$mask/,keys(%sheet_values)) {
629: $sum+=($sheet_values{$_}-$mean)**2;
1.78 matthew 630: }
1.1 www 631: return sqrt($sum/($num-1));
632: }
633:
1.84 matthew 634: #-------------------------------------------------------
635:
636: =item PROD(range)
637:
638: compute the product of the items in the range.
639:
640: =cut
641:
642: #-------------------------------------------------------
1.1 www 643: sub PROD {
644: my $mask=mask(@_);
645: my $prod=1;
1.104 matthew 646: foreach (grep /$mask/,keys(%sheet_values)) {
647: $prod*=$sheet_values{$_};
1.78 matthew 648: }
1.1 www 649: return $prod;
650: }
651:
1.84 matthew 652: #-------------------------------------------------------
653:
654: =item MAX(range)
655:
656: compute the maximum of the items in the range.
657:
658: =cut
659:
660: #-------------------------------------------------------
1.1 www 661: sub MAX {
662: my $mask=mask(@_);
663: my $max='-';
1.104 matthew 664: foreach (grep /$mask/,keys(%sheet_values)) {
665: unless ($max) { $max=$sheet_values{$_}; }
666: if (($sheet_values{$_}>$max) || ($max eq '-')) { $max=$sheet_values{$_}; }
1.78 matthew 667: }
1.1 www 668: return $max;
669: }
670:
1.84 matthew 671: #-------------------------------------------------------
672:
673: =item MIN(range)
674:
675: compute the minimum of the items in the range.
676:
677: =cut
678:
679: #-------------------------------------------------------
1.1 www 680: sub MIN {
681: my $mask=mask(@_);
682: my $min='-';
1.104 matthew 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: }
1.78 matthew 688: }
1.1 www 689: return $min;
690: }
691:
1.84 matthew 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: #-------------------------------------------------------
1.1 www 702: sub SUMMAX {
703: my ($num,$lower,$upper)=@_;
704: my $mask=mask($lower,$upper);
705: my @inside=();
1.104 matthew 706: foreach (grep /$mask/,keys(%sheet_values)) {
707: push (@inside,$sheet_values{$_});
1.78 matthew 708: }
1.1 www 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:
1.84 matthew 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: #-------------------------------------------------------
1.1 www 727: sub SUMMIN {
728: my ($num,$lower,$upper)=@_;
729: my $mask=mask($lower,$upper);
730: my @inside=();
1.104 matthew 731: foreach (grep /$mask/,keys(%sheet_values)) {
732: $inside[$#inside+1]=$sheet_values{$_};
1.78 matthew 733: }
1.1 www 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:
1.103 matthew 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;
1.124 matthew 755: study($expression);
1.103 matthew 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;
1.124 matthew 778: study($expression);
1.103 matthew 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: #--------------------------------------------------------
1.59 www 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;
1.78 matthew 796: foreach ( @vars ) {
1.59 www 797: my $varname=$_;
798: if ($varname=~/\D/) {
799: $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge;
800: $varname=~s/$var/\(\\w\+\)/g;
1.78 matthew 801: foreach (keys(%c)) {
1.59 www 802: if ($_=~/$varname/) {
803: $values{$1}=1;
804: }
1.78 matthew 805: }
1.59 www 806: }
1.78 matthew 807: }
1.59 www 808: if ($func eq 'EXPANDSUM') {
809: my $result='';
1.78 matthew 810: foreach (keys(%values)) {
1.59 www 811: my $thissum=$formula;
812: $thissum=~s/$var/$_/g;
813: $result.=$thissum.'+';
1.78 matthew 814: }
1.59 www 815: $result=~s/\+$//;
816: return $result;
817: } else {
818: return 0;
819: }
820: } else {
1.88 matthew 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;
1.124 matthew 830: study $expression;
1.88 matthew 831: foreach $parameter (keys(%c)) {
832: push @matches,$parameter if ($parameter =~ /$expression/);
833: }
1.129 matthew 834: if (scalar(@matches) == 0) {
835: $returnvalue = 'unmatched parameter: '.$parameter;
1.128 matthew 836: } elsif (scalar(@matches) == 1) {
1.88 matthew 837: $returnvalue = '$c{\''.$matches[0].'\'}';
1.128 matthew 838: } elsif (scalar(@matches) > 0) {
1.100 matthew 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: }
1.129 matthew 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;
1.88 matthew 850: }
851: return $returnvalue;
1.59 www 852: }
853: }
854:
1.1 www 855: sub sett {
856: %t=();
1.16 www 857: my $pattern='';
858: if ($sheettype eq 'assesscalc') {
859: $pattern='A';
860: } else {
861: $pattern='[A-Z]';
862: }
1.104 matthew 863: # Deal with the template row
1.78 matthew 864: foreach (keys(%f)) {
1.104 matthew 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: }
1.78 matthew 885: }
1.104 matthew 886: # Deal with the normal cells
1.78 matthew 887: foreach (keys(%f)) {
1.112 matthew 888: if (exists($f{$_}) && ($_!~/template\_/)) {
1.42 www 889: my $matches=($_=~/^$pattern(\d+)/);
890: if (($matches) && ($1)) {
1.6 www 891: unless ($f{$_}=~/^\!/) {
892: $t{$_}=$c{$_};
893: }
894: } else {
895: $t{$_}=$f{$_};
1.7 www 896: $t{$_}=~s/\.\.+/\,/g;
1.104 matthew 897: $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
1.59 www 898: $t{$_}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
1.6 www 899: }
1.1 www 900: }
1.78 matthew 901: }
1.104 matthew 902: # For inserted lines, [B-Z] is also valid
1.97 www 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;
1.104 matthew 909: $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
1.97 www 910: $t{$_}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
911: }
912: }
913: }
914: }
1.88 matthew 915: # For some reason 'A0' gets special treatment... This seems superfluous
916: # but I imagine it is here for a reason.
1.17 www 917: $t{'A0'}=$f{'A0'};
918: $t{'A0'}=~s/\.\.+/\,/g;
1.104 matthew 919: $t{'A0'}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
1.59 www 920: $t{'A0'}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
1.1 www 921: }
922:
1.124 matthew 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)) {
1.132 matthew 932: #$errorlog .= "$_:".$t{$_};
1.124 matthew 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=$_; }
1.132 matthew 940: #$errorlog .= ":".$sheet_values{$_}."\n";
1.124 matthew 941: }
942: $depth++;
943: if ($depth>100) {
944: undef %sheet_values;
945: return $lastcalc.': Maximum calculation depth exceeded';
946: }
947: }
948: return '';
949: }
950:
1.122 matthew 951: # ------------------------------------------- End of "Inside of the safe space"
952: ENDDEFS
953: $safeeval->reval($code);
954: return $safeeval;
955: }
956:
1.104 matthew 957: #
1.132 matthew 958: #
1.104 matthew 959: #
1.122 matthew 960: sub templaterow {
961: my $sheet = shift;
962: my @cols=();
1.132 matthew 963: my $rowlabel = 'Template';
1.122 matthew 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;
1.132 matthew 970: push(@cols,{ name => 'template_'.$_,
971: formula => $fm,
972: value => $fm });
1.122 matthew 973: }
1.132 matthew 974: return ($rowlabel,@cols);
1.122 matthew 975: }
976:
1.16 www 977: sub outrowassess {
1.104 matthew 978: # $n is the current row number
1.132 matthew 979: my ($sheet,$n) = @_;
1.6 www 980: my @cols=();
1.132 matthew 981: my $rowlabel='';
1.6 www 982: if ($n) {
1.122 matthew 983: my ($usy,$ufn)=split(/__&&&\__/,$sheet->{'f'}->{'A'.$n});
1.132 matthew 984: if (exists($sheet->{'rowlabel'}->{$usy})) {
985: $rowlabel = $sheet->{'rowlabel'}->{$usy};
1.104 matthew 986: } else {
1.132 matthew 987: $rowlabel = '';
1.104 matthew 988: }
1.6 www 989: } else {
1.132 matthew 990: $rowlabel = 'Export';
1.6 www 991: }
1.78 matthew 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') {
1.122 matthew 996: my $fm=$sheet->{'f'}->{$_.$n};
1.6 www 997: $fm=~s/[\'\"]/\&\#34;/g;
1.132 matthew 998: push(@cols,{ name => $_.$n,
999: formula => $fm,
1000: value => $sheet->{'values'}->{$_.$n}});
1.78 matthew 1001: }
1.132 matthew 1002: return ($rowlabel,@cols);
1.6 www 1003: }
1004:
1.18 www 1005: sub outrow {
1.132 matthew 1006: my ($sheet,$n)=@_;
1.18 www 1007: my @cols=();
1.132 matthew 1008: my $rowlabel;
1.18 www 1009: if ($n) {
1.132 matthew 1010: $rowlabel = $sheet->{'rowlabel'}->{$sheet->{'f'}->{'A'.$n}};
1.18 www 1011: } else {
1.132 matthew 1012: if ($sheet->{'sheettype'} eq 'classcalc') {
1013: $rowlabel = 'Summary';
1014: } else {
1015: $rowlabel = 'Export';
1016: }
1.18 www 1017: }
1.78 matthew 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') {
1.122 matthew 1022: my $fm=$sheet->{'f'}->{$_.$n};
1.118 matthew 1023: $fm=~s/[\'\"]/\&\#34;/g;
1.132 matthew 1024: push(@cols,{ name => $_.$n,
1025: formula => $fm,
1026: value => $sheet->{'values'}->{$_.$n}});
1.118 matthew 1027: }
1.132 matthew 1028: return ($rowlabel,@cols);
1.118 matthew 1029: }
1030:
1.4 www 1031: # ------------------------------------------------ Add or change formula values
1032: sub setformulas {
1.124 matthew 1033: my ($sheet)=shift;
1.119 matthew 1034: %{$sheet->{'safe'}->varglob('f')}=%{$sheet->{'f'}};
1.6 www 1035: }
1036:
1037: # ------------------------------------------------ Add or change formula values
1038: sub setconstants {
1.119 matthew 1039: my ($sheet)=shift;
1.122 matthew 1040: my ($constants) = @_;
1041: if (! ref($constants)) {
1042: my %tmp = @_;
1043: $constants = \%tmp;
1044: }
1045: $sheet->{'constants'} = $constants;
1.119 matthew 1046: return %{$sheet->{'safe'}->varglob('c')}=%{$sheet->{'constants'}};
1.6 www 1047: }
1048:
1.55 www 1049: # --------------------------------------------- Set names of other spreadsheets
1050: sub setothersheets {
1.119 matthew 1051: my $sheet = shift;
1052: my @othersheets = @_;
1053: $sheet->{'othersheets'} = \@othersheets;
1054: @{$sheet->{'safe'}->varglob('os')}=@othersheets;
1055: return;
1.55 www 1056: }
1057:
1.6 www 1058: # ------------------------------------------------ Add or change formula values
1059: sub setrowlabels {
1.119 matthew 1060: my $sheet=shift;
1.125 matthew 1061: my ($rowlabel) = @_;
1062: if (! ref($rowlabel)) {
1063: my %tmp = @_;
1064: $rowlabel = \%tmp;
1065: }
1066: $sheet->{'rowlabel'}=$rowlabel;
1.4 www 1067: }
1068:
1069: # ------------------------------------------------------- Calculate spreadsheet
1070: sub calcsheet {
1.119 matthew 1071: my $sheet=shift;
1.124 matthew 1072: my $result = $sheet->{'safe'}->reval('&calc();');
1073: %{$sheet->{'values'}} = %{$sheet->{'safe'}->varglob('sheet_values')};
1074: return $result;
1.4 www 1075: }
1076:
1077: # ---------------------------------------------------------------- Get formulas
1.131 matthew 1078: # Return a copy of the formulas
1.4 www 1079: sub getformulas {
1.119 matthew 1080: my $sheet = shift;
1081: return %{$sheet->{'safe'}->varglob('f')};
1.4 www 1082: }
1083:
1.132 matthew 1084: sub geterrorlog {
1085: my $sheet = shift;
1086: return ${$sheet->{'safe'}->varglob('errorlog')};
1087: }
1088:
1.97 www 1089: # ----------------------------------------------------- Get value of $f{'A'.$n}
1090: sub getfa {
1.119 matthew 1091: my $sheet = shift;
1092: my ($n)=@_;
1093: return $sheet->{'safe'}->reval('$f{"A'.$n.'"}');
1.97 www 1094: }
1095:
1.14 www 1096: # ------------------------------------------------------------- Export of A-row
1.28 www 1097: sub exportdata {
1.119 matthew 1098: my $sheet=shift;
1.121 matthew 1099: my @exportarray=();
1100: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1101: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1.130 matthew 1102: if (exists($sheet->{'values'}->{$_.'0'})) {
1103: push(@exportarray,$sheet->{'values'}->{$_.'0'});
1104: } else {
1105: push(@exportarray,'');
1106: }
1.121 matthew 1107: }
1108: return @exportarray;
1.14 www 1109: }
1.55 www 1110:
1.136 ! matthew 1111:
! 1112:
! 1113: sub update_student_sheet{
! 1114: my $sheet = shift;
! 1115: # Load in the studentcalc sheet
! 1116: &readsheet($sheet,'default_studentcalc');
! 1117: # Determine the structure (contained assessments, etc) of the sheet
! 1118: &updatesheet($sheet);
! 1119: # Load in the cached sheets for this student
! 1120: &cachedssheets($sheet);
! 1121: # Load in the (possibly cached) data from the assessment sheets
! 1122: &loadstudent($sheet);
! 1123: # Compute the sheet
! 1124: &calcsheet($sheet);
! 1125: }
! 1126:
1.5 www 1127: # ========================================================== End of Spreadsheet
1128: # =============================================================================
1.27 www 1129: #
1.135 matthew 1130: # Procedures for spreadsheet output
1.27 www 1131: #
1.6 www 1132: # --------------------------------------------- Produce output row n from sheet
1133:
1.132 matthew 1134: sub get_row {
1135: my ($sheet,$n) = @_;
1136: my ($rowlabel,@rowdata);
1.104 matthew 1137: if ($n eq '-') {
1.132 matthew 1138: ($rowlabel,@rowdata) = &templaterow($sheet);
1139: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1140: ($rowlabel,@rowdata) = &outrowassess($sheet,$n);
1.61 www 1141: } else {
1.132 matthew 1142: ($rowlabel,@rowdata) = &outrow($sheet,$n);
1.61 www 1143: }
1.132 matthew 1144: return ($rowlabel,@rowdata);
1.6 www 1145: }
1146:
1.132 matthew 1147: ########################################################################
1148: ########################################################################
1149: sub sort_indicies {
1150: my $sheet = shift;
1.106 matthew 1151: #
1.128 matthew 1152: # Sort the rows in some manner
1153: #
1.65 www 1154: my @sortby=();
1155: my @sortidx=();
1.132 matthew 1156: for (my $row=1;$row<=$sheet->{'maxrow'};$row++) {
1.119 matthew 1157: push (@sortby, $sheet->{'safe'}->reval('$f{"A'.$row.'"}'));
1.132 matthew 1158: push (@sortidx, $row);
1.65 www 1159: }
1.111 matthew 1160: @sortidx=sort { lc($sortby[$a]) cmp lc($sortby[$b]); } @sortidx;
1.132 matthew 1161: return @sortidx;
1162: }
1163:
1.135 matthew 1164: #############################################################
1165: ### ###
1166: ### Spreadsheet Output Routines ###
1167: ### ###
1168: #############################################################
1169:
1170: ############################################
1171: ## HTML output routines ##
1172: ############################################
1.132 matthew 1173: sub html_editable_cell {
1174: my ($cell,$bgcolor) = @_;
1175: my $result;
1176: my ($name,$formula,$value);
1177: if (defined($cell)) {
1178: $name = $cell->{'name'};
1179: $formula = $cell->{'formula'};
1180: $value = $cell->{'value'};
1181: }
1182: $name = '' if (! defined($name));
1183: $formula = '' if (! defined($formula));
1184: if (! defined($value)) {
1185: $value = '<font color="'.$bgcolor.'">#</font>';
1186: if ($formula ne '') {
1187: $value = '<i>undefined value</i>';
1188: }
1189: }
1.133 matthew 1190: if ($value =~ /^\s*$/ ) {
1191: $value = '<font color="'.$bgcolor.'">#</font>';
1192: }
1.132 matthew 1193: $result .= '<a href="javascript:celledit(\''.
1194: $name.'\',\''.$formula.'\');">'.$value.'</a>';
1195: return $result;
1196: }
1197:
1198: sub html_uneditable_cell {
1199: my ($cell,$bgcolor) = @_;
1200: my $value = (defined($cell) ? $cell->{'value'} : '');
1201: return ' '.$value.' ';
1202: }
1203:
1204: sub outsheet_html {
1205: my ($sheet,$r) = @_;
1206: my ($num_uneditable,$realm,$row_type);
1.119 matthew 1207: if ($sheet->{'sheettype'} eq 'assesscalc') {
1.132 matthew 1208: $num_uneditable = 1;
1209: $realm = 'Assessment';
1210: $row_type = 'Item';
1.119 matthew 1211: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1.132 matthew 1212: $num_uneditable = 26;
1213: $realm = 'User';
1214: $row_type = 'Assessment';
1215: } elsif ($sheet->{'sheettype'} eq 'classcalc') {
1216: $num_uneditable = 26;
1217: $realm = 'Course';
1218: $row_type = 'Student';
1219: } else {
1220: return; # error
1221: }
1222: ####################################
1223: # Print out header table
1224: ####################################
1225: my $num_left = 52-$num_uneditable;
1226: my $tabledata =<<"END";
1227: <table border="2">
1228: <tr>
1229: <th colspan="1" rowspan="2"><font size="+2">$realm</font></th>
1230: <td bgcolor="#FFDDDD" colspan="$num_uneditable">
1231: <b><font size="+1">Import</font></b></td>
1232: <td colspan="$num_left">
1233: <b><font size="+1">Calculations</font></b></td>
1234: </tr><tr>
1235: END
1236: my $label_num = 0;
1237: foreach (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')){
1238: if ($label_num<$num_uneditable) {
1239: $tabledata.='<td bgcolor="#FFDDDD">';
1240: } else {
1241: $tabledata.='<td>';
1242: }
1243: $tabledata.="<b><font size=+1>$_</font></b></td>";
1244: $label_num++;
1.106 matthew 1245: }
1.132 matthew 1246: $tabledata.="</tr>\n";
1247: $r->print($tabledata);
1248: ####################################
1249: # Print out template row
1250: ####################################
1251: my ($rowlabel,@rowdata) = &get_row($sheet,'-');
1.133 matthew 1252: my $row_html = '<tr><td>'.&format_html_rowlabel($rowlabel).'</td>';
1.132 matthew 1253: my $num_cols_output = 0;
1254: foreach my $cell (@rowdata) {
1255: if ($num_cols_output++ < $num_uneditable) {
1256: $row_html .= '<td bgcolor="#FFDDDD">';
1257: $row_html .= &html_uneditable_cell($cell,'#FFDDDD');
1258: } else {
1259: $row_html .= '<td bgcolor="#EOFFDD">';
1260: $row_html .= &html_editable_cell($cell,'#E0FFDD');
1261: }
1262: $row_html .= '</td>';
1263: }
1264: $row_html.= "</tr>\n";
1265: $r->print($row_html);
1266: ####################################
1267: # Print out summary/export row
1268: ####################################
1269: my ($rowlabel,@rowdata) = &get_row($sheet,'0');
1270: my $rowcount = 0;
1.133 matthew 1271: $row_html = '<tr><td>'.&format_html_rowlabel($rowlabel).'</td>';
1.132 matthew 1272: $num_cols_output = 0;
1273: foreach my $cell (@rowdata) {
1274: if ($num_cols_output++ < 26) {
1275: $row_html .= '<td bgcolor="#CCCCFF">';
1276: $row_html .= &html_editable_cell($cell,'#CCCCFF');
1277: } else {
1278: $row_html .= '<td bgcolor="#DDCCFF">';
1279: $row_html .= &html_uneditable_cell(undef,'#CCCCFF');
1280: }
1281: $row_html .= '</td>';
1282: }
1283: $row_html.= "</tr>\n";
1284: $r->print($row_html);
1285: $r->print('</table>');
1286: ####################################
1287: # Prepare to output rows
1288: ####################################
1289: my @Rows = &sort_indicies($sheet);
1.106 matthew 1290: #
1291: # Loop through the rows and output them one at a time
1.132 matthew 1292: my $rows_output=0;
1293: foreach my $rownum (@Rows) {
1294: my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
1.136 ! matthew 1295: next if ($rowlabel =~ /^\s*$/);
1.132 matthew 1296: #
1297: my $defaultbg='#E0FF';
1298: #
1299: my $row_html ="\n".'<tr><td><b><font size=+1>'.$rownum.
1300: '</font></b></td>';
1301: #
1302: if ($sheet->{'sheettype'} eq 'classcalc') {
1.133 matthew 1303: $row_html.='<td>'.&format_html_rowlabel($rowlabel).'</td>';
1.132 matthew 1304: # Output links for each student?
1.133 matthew 1305: # Nope, that is already done for us in format_html_rowlabel (for now)
1.132 matthew 1306: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1.133 matthew 1307: $row_html.='<td>'.&format_html_rowlabel($rowlabel);
1.132 matthew 1308: $row_html.= '<br>'.
1309: '<select name="sel_'.$rownum.'" '.
1310: 'onChange="changesheet('.$rownum.')">'.
1311: '<option name="default">Default</option>';
1312: foreach (@{$sheet->{'othersheets'}}) {
1313: $row_html.='<option name="'.$_.'"';
1314: #if ($ufn eq $_) {
1315: # $row_html.=' selected';
1316: #}
1317: $row_html.='>'.$_.'</option>';
1318: }
1319: $row_html.='</select></td>';
1320: } elsif ($sheet->{'sheettype'} eq 'assesscalc') {
1.133 matthew 1321: $row_html.='<td>'.&format_html_rowlabel($rowlabel).'</td>';
1.132 matthew 1322: }
1323: #
1324: my $shown_cells = 0;
1325: foreach my $cell (@rowdata) {
1326: my $value = $cell->{'value'};
1327: my $formula = $cell->{'formula'};
1328: my $cellname = $cell->{'name'};
1329: #
1330: my $bgcolor;
1331: if ($shown_cells && ($shown_cells/5 == int($shown_cells/5))) {
1332: $bgcolor = $defaultbg.'99';
1333: } else {
1334: $bgcolor = $defaultbg.'DD';
1335: }
1336: $bgcolor='#FFDDDD' if ($shown_cells < $num_uneditable);
1337: #
1338: $row_html.='<td bgcolor='.$bgcolor.'>';
1339: if ($shown_cells < $num_uneditable) {
1340: $row_html .= &html_uneditable_cell($cell,$bgcolor);
1341: } else {
1342: $row_html .= &html_editable_cell($cell,$bgcolor);
1343: }
1344: $row_html.='</td>';
1345: $shown_cells++;
1346: }
1347: if ($row_html) {
1348: if ($rows_output % 25 == 0) {
1.102 matthew 1349: $r->print("</table>\n<br>\n");
1350: $r->rflush();
1.132 matthew 1351: $r->print('<table border=2>'.
1352: '<tr><td> <td>'.$row_type.'</td>'.
1353: '<td>'.
1.106 matthew 1354: join('</td><td>',
1355: (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
1356: 'abcdefghijklmnopqrstuvwxyz'))).
1.102 matthew 1357: "</td></tr>\n");
1358: }
1.132 matthew 1359: $rows_output++;
1360: $r->print($row_html);
1.78 matthew 1361: }
1.6 www 1362: }
1.132 matthew 1363: #
1364: $r->print('</table>');
1365: #
1366: # Debugging code (be sure to uncomment errorlog code in safe space):
1367: #
1368: # $r->print("\n<pre>");
1369: # $r->print(&geterrorlog($sheet));
1370: # $r->print("\n</pre>");
1371: return 1;
1372: }
1373:
1.135 matthew 1374: ############################################
1375: ## csv output routines ##
1376: ############################################
1.132 matthew 1377: sub outsheet_csv {
1378: my ($sheet,$r) = @_;
1.133 matthew 1379: my $csvdata = '';
1380: my @Values;
1381: ####################################
1382: # Prepare to output rows
1383: ####################################
1384: my @Rows = &sort_indicies($sheet);
1385: #
1386: # Loop through the rows and output them one at a time
1387: my $rows_output=0;
1388: foreach my $rownum (@Rows) {
1389: my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
1.136 ! matthew 1390: next if ($rowlabel =~ /^\s*$/);
1.133 matthew 1391: push (@Values,&format_csv_rowlabel($rowlabel));
1392: foreach my $cell (@rowdata) {
1393: push (@Values,'"'.$cell->{'value'}.'"');
1394: }
1395: $csvdata.= join(',',@Values)."\n";
1396: @Values = ();
1397: }
1398: #
1.134 matthew 1399: # Write the CSV data to a file and serve up a link
1400: #
1401: my $filename = '/prtspool/'.
1402: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1403: time.'_'.rand(1000000000).'.csv';
1404: my $file;
1405: unless ($file = Apache::File->new('>'.'/home/httpd'.$filename)) {
1406: $r->log_error("Couldn't open $filename for output $!");
1407: $r->print("Problems occured in writing the csv file. ".
1408: "This error has been logged. ".
1409: "Please alert your LON-CAPA administrator.");
1410: $r->print("<pre>\n".$csvdata."</pre>\n");
1411: return 0;
1412: }
1413: print $file $csvdata;
1414: close($file);
1415: $r->print('<br /><br />'.
1416: '<a href="'.$filename.'">Your CSV spreadsheet.</a>'."\n");
1.133 matthew 1417: #
1418: return 1;
1.132 matthew 1419: }
1420:
1.135 matthew 1421: ############################################
1422: ## Excel output routines ##
1423: ############################################
1424: sub outsheet_recursive_excel {
1425: my ($sheet,$r) = @_;
1.136 ! matthew 1426: my $c = $r->connection;
1.135 matthew 1427: return undef if ($sheet->{'sheettype'} ne 'classcalc');
1428: my ($workbook,$filename) = &create_excel_spreadsheet($sheet,$r);
1429: return undef if (! defined($workbook));
1430: #
1431: # Create main worksheet
1432: my $main_worksheet = $workbook->addworksheet('main');
1433: #
1434: # Figure out who the students are
1435: my %f=&getformulas($sheet);
1436: my $count = 0;
1.136 ! matthew 1437: $r->print(<<END);
! 1438: <p>
! 1439: Compiling Excel Workbook with a worksheet for each student.
! 1440: </p><p>
! 1441: This operation may take longer than a complete recalculation of the
! 1442: spreadsheet.
! 1443: </p><p>
! 1444: To abort this operation, hit the stop button on your browser.
! 1445: </p><p>
! 1446: A link to the spreadsheet will be available at the end of this process.
! 1447: </p>
! 1448: <p>
! 1449: END
1.135 matthew 1450: $r->rflush();
1.136 ! matthew 1451: my $starttime = time;
1.135 matthew 1452: foreach (keys(%f)) {
1453: next if ($_!~/^A(\d+)/ || $1 == 0 || ($f{$_}=~/^[!~-]/));
1454: $count++;
1455: my ($sname,$sdom) = split(':',$f{$_});
1456: my $student_excel_worksheet=$workbook->addworksheet($sname.'@'.$sdom);
1457: # Create a new spreadsheet
1458: my $studentsheet = &makenewsheet($sname,$sdom,'studentcalc',undef);
1459: # Read in the spreadsheet definition
1.136 ! matthew 1460: &update_student_sheet($studentsheet);
1.135 matthew 1461: # Stuff the sheet into excel
1462: &export_sheet_as_excel($studentsheet,$student_excel_worksheet);
1.136 ! matthew 1463: my $totaltime = int((time - $starttime) / $count * $sheet->{'maxrow'});
! 1464: my $timeleft = int((time - $starttime) / $count * ($sheet->{'maxrow'} - $count));
1.135 matthew 1465: if ($count % 5 == 0) {
1.136 ! matthew 1466: $r->print($count.' students completed.'.
! 1467: ' Time remaining: '.$timeleft.' sec. '.
! 1468: ' Estimated total time: '.$totaltime." sec <br />\n");
1.135 matthew 1469: $r->rflush();
1470: }
1.136 ! matthew 1471: if(defined($c) && ($c->aborted())) {
! 1472: last;
! 1473: }
1.135 matthew 1474: }
1475: #
1.136 ! matthew 1476: if(! $c->aborted() ) {
! 1477: $r->print('All students spreadsheets completed!<br />');
! 1478: $r->rflush();
! 1479: #
! 1480: # &export_sheet_as_excel fills $worksheet with the data from $sheet
! 1481: &export_sheet_as_excel($sheet,$main_worksheet);
! 1482: #
! 1483: $workbook->close();
! 1484: # Okay, the spreadsheet is taken care of, so give the user a link.
! 1485: $r->print('<br /><br />'.
! 1486: '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
! 1487: } else {
! 1488: $workbook->close(); # Not sure how necessary this is.
! 1489: #unlink('/home/httpd'.$filename); # No need to keep this around?
! 1490: }
1.135 matthew 1491: return 1;
1492: }
1493:
1.132 matthew 1494: sub outsheet_excel {
1495: my ($sheet,$r) = @_;
1.135 matthew 1496: my ($workbook,$filename) = &create_excel_spreadsheet($sheet,$r);
1497: return undef if (! defined($workbook));
1498: my $sheetname;
1499: if ($sheet->{'sheettype'} eq 'classcalc') {
1500: $sheetname = 'Main';
1501: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1502: $sheetname = $sheet->{'uname'}.'@'.$sheet->{'udom'};
1503: } elsif ($sheet->{'sheettype'} eq 'assesscalc') {
1504: $sheetname = $sheet->{'uname'}.'@'.$sheet->{'udom'}.' assessment';
1505: }
1506: my $worksheet = $workbook->addworksheet($sheetname);
1507: #
1508: # &export_sheet_as_excel fills $worksheet with the data from $sheet
1509: &export_sheet_as_excel($sheet,$worksheet);
1510: #
1511: $workbook->close();
1512: # Okay, the spreadsheet is taken care of, so give the user a link.
1513: $r->print('<br /><br />'.
1514: '<a href="'.$filename.'">Your Excel spreadsheet.</a>'."\n");
1515: return 1;
1516: }
1517:
1518: sub create_excel_spreadsheet {
1519: my ($sheet,$r) = @_;
1.134 matthew 1520: my $filename = '/prtspool/'.
1521: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1522: time.'_'.rand(1000000000).'.xls';
1.135 matthew 1523: #&Apache::lonnet::logthis("spreadsheet:filename = ".$filename);
1.134 matthew 1524: my $workbook = Spreadsheet::WriteExcel->new('/home/httpd'.$filename);
1525: if (! defined($workbook)) {
1526: $r->log_error("Error creating excel spreadsheet $filename: $!");
1527: $r->print("Problems creating new Excel file. ".
1528: "This error has been logged. ".
1529: "Please alert your LON-CAPA administrator");
1.135 matthew 1530: return undef;
1.134 matthew 1531: }
1532: #
1533: # The spreadsheet stores temporary data in files, then put them
1534: # together. If needed we should be able to disable this (memory only).
1535: # The temporary directory must be specified before calling 'addworksheet'.
1536: # File::Temp is used to determine the temporary directory.
1537: $workbook->set_tempdir('/home/httpd/perl/tmp');
1538: #
1539: # Determine the name to give the worksheet
1.135 matthew 1540: return ($workbook,$filename);
1541: }
1542:
1543: sub export_sheet_as_excel {
1544: my $sheet = shift;
1545: my $worksheet = shift;
1.134 matthew 1546: ####################################
1547: # Prepare to output rows
1548: ####################################
1549: my @Rows = &sort_indicies($sheet);
1550: #
1551: # Loop through the rows and output them one at a time
1552: my $rows_output=0;
1553: foreach my $rownum (@Rows) {
1554: my ($rowlabel,@rowdata) = &get_row($sheet,$rownum);
1.136 ! matthew 1555: next if ($rowlabel =~ /^\s*$/);
1.134 matthew 1556: my $cols_output = 0;
1557: my $label = &format_excel_rowlabel($rowlabel);
1558: $worksheet->write($rows_output,$cols_output++,$label);
1559: if (ref($label)) {
1560: $cols_output = (scalar(@$label));
1561: }
1562: foreach my $cell (@rowdata) {
1563: $worksheet->write($rows_output,$cols_output++,
1564: $cell->{'value'});
1565: }
1566: $rows_output++;
1567: }
1.135 matthew 1568: return;
1.132 matthew 1569: }
1570:
1.135 matthew 1571: ############################################
1572: ## XML output routines ##
1573: ############################################
1.132 matthew 1574: sub outsheet_xml {
1575: my ($sheet,$r) = @_;
1.135 matthew 1576: ## Someday XML
1577: ## Will be rendered for the user
1578: ## But not on this day
1.6 www 1579: }
1580:
1.135 matthew 1581: ##
1582: ## Outsheet - calls other outsheet_* functions
1583: ##
1.132 matthew 1584: sub outsheet {
1585: my ($r,$sheet)=@_;
1.134 matthew 1586: if (! exists($ENV{'form.output'})) {
1587: $ENV{'form.output'} = 'HTML';
1588: }
1589: if (lc($ENV{'form.output'}) eq 'csv') {
1.133 matthew 1590: &outsheet_csv($sheet,$r);
1.134 matthew 1591: } elsif (lc($ENV{'form.output'}) eq 'excel') {
1592: &outsheet_excel($sheet,$r);
1.135 matthew 1593: } elsif (lc($ENV{'form.output'}) eq 'recursive excel') {
1594: &outsheet_recursive_excel($sheet,$r);
1.134 matthew 1595: # } elsif (lc($ENV{'form.output'}) eq 'xml' ) {
1.132 matthew 1596: # &outsheet_xml($sheet,$r);
1.133 matthew 1597: } else {
1598: &outsheet_html($sheet,$r);
1599: }
1.132 matthew 1600: }
1601:
1602: ########################################################################
1603: ########################################################################
1.55 www 1604: sub othersheets {
1.119 matthew 1605: my ($sheet,$stype)=@_;
1606: $stype = $sheet->{'sheettype'} if (! defined($stype));
1.81 matthew 1607: #
1.119 matthew 1608: my $cnum = $sheet->{'cnum'};
1609: my $cdom = $sheet->{'cdom'};
1610: my $chome = $sheet->{'chome'};
1.81 matthew 1611: #
1.55 www 1612: my @alternatives=();
1.81 matthew 1613: my %results=&Apache::lonnet::dump($stype.'_spreadsheets',$cdom,$cnum);
1614: my ($tmp) = keys(%results);
1615: unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1616: @alternatives = sort (keys(%results));
1617: }
1.55 www 1618: return @alternatives;
1619: }
1620:
1.82 matthew 1621: #
1622: # -------------------------------------- Parse a spreadsheet
1623: #
1624: sub parse_sheet {
1625: # $sheetxml is a scalar reference or a scalar
1626: my ($sheetxml) = @_;
1627: if (! ref($sheetxml)) {
1628: my $tmp = $sheetxml;
1629: $sheetxml = \$tmp;
1630: }
1631: my %f;
1632: my $parser=HTML::TokeParser->new($sheetxml);
1633: my $token;
1634: while ($token=$parser->get_token) {
1635: if ($token->[0] eq 'S') {
1636: if ($token->[1] eq 'field') {
1637: $f{$token->[2]->{'col'}.$token->[2]->{'row'}}=
1638: $parser->get_text('/field');
1639: }
1640: if ($token->[1] eq 'template') {
1641: $f{'template_'.$token->[2]->{'col'}}=
1642: $parser->get_text('/template');
1643: }
1644: }
1645: }
1646: return \%f;
1647: }
1648:
1.55 www 1649: #
1.27 www 1650: # -------------------------------------- Read spreadsheet formulas for a course
1651: #
1652: sub readsheet {
1.119 matthew 1653: my ($sheet,$fn)=@_;
1.107 matthew 1654: #
1.119 matthew 1655: my $stype = $sheet->{'sheettype'};
1656: my $cnum = $sheet->{'cnum'};
1657: my $cdom = $sheet->{'cdom'};
1658: my $chome = $sheet->{'chome'};
1.107 matthew 1659: #
1.104 matthew 1660: if (! defined($fn)) {
1661: # There is no filename. Look for defaults in course and global, cache
1662: unless ($fn=$defaultsheets{$cnum.'_'.$cdom.'_'.$stype}) {
1663: my %tmphash = &Apache::lonnet::get('environment',
1664: ['spreadsheet_default_'.$stype],
1665: $cdom,$cnum);
1666: my ($tmp) = keys(%tmphash);
1667: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1668: $fn = 'default_'.$stype;
1669: } else {
1670: $fn = $tmphash{'spreadsheet_default_'.$stype};
1671: }
1672: unless (($fn) && ($fn!~/^error\:/)) {
1673: $fn='default_'.$stype;
1674: }
1675: $defaultsheets{$cnum.'_'.$cdom.'_'.$stype}=$fn;
1676: }
1677: }
1678: # $fn now has a value
1.119 matthew 1679: $sheet->{'filename'} = $fn;
1.104 matthew 1680: # see if sheet is cached
1681: my $fstring='';
1682: if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
1.119 matthew 1683: my %tmp = split(/___;___/,$fstring);
1.124 matthew 1684: $sheet->{'f'} = \%tmp;
1685: &setformulas($sheet);
1.104 matthew 1686: } else {
1687: # Not cached, need to read
1688: my %f=();
1689: if ($fn=~/^default\_/) {
1690: my $sheetxml='';
1691: my $fh;
1692: my $dfn=$fn;
1693: $dfn=~s/\_/\./g;
1694: if ($fh=Apache::File->new($includedir.'/'.$dfn)) {
1695: $sheetxml=join('',<$fh>);
1696: } else {
1697: $sheetxml='<field row="0" col="A">"Error"</field>';
1698: }
1699: %f=%{&parse_sheet(\$sheetxml)};
1700: } elsif($fn=~/\/*\.spreadsheet$/) {
1701: my $sheetxml=&Apache::lonnet::getfile
1702: (&Apache::lonnet::filelocation('',$fn));
1703: if ($sheetxml == -1) {
1704: $sheetxml='<field row="0" col="A">"Error loading spreadsheet '
1705: .$fn.'"</field>';
1706: }
1707: %f=%{&parse_sheet(\$sheetxml)};
1708: } else {
1709: my $sheet='';
1710: my %tmphash = &Apache::lonnet::dump($fn,$cdom,$cnum);
1711: my ($tmp) = keys(%tmphash);
1712: unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1713: foreach (keys(%tmphash)) {
1714: $f{$_}=$tmphash{$_};
1715: }
1716: }
1717: }
1718: # Cache and set
1719: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1.124 matthew 1720: $sheet->{'f'}=\%f;
1721: &setformulas($sheet);
1.3 www 1722: }
1723: }
1724:
1.28 www 1725: # -------------------------------------------------------- Make new spreadsheet
1726: sub makenewsheet {
1727: my ($uname,$udom,$stype,$usymb)=@_;
1.119 matthew 1728: my $sheet={};
1729: $sheet->{'uname'} = $uname;
1730: $sheet->{'udom'} = $udom;
1731: $sheet->{'sheettype'} = $stype;
1732: $sheet->{'usymb'} = $usymb;
1733: $sheet->{'cid'} = $ENV{'request.course.id'};
1734: $sheet->{'csec'} = $Section{$uname.':'.$udom};
1735: $sheet->{'coursefilename'} = $ENV{'request.course.fn'};
1736: $sheet->{'cnum'} = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
1737: $sheet->{'cdom'} = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1738: $sheet->{'chome'} = $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1.134 matthew 1739: $sheet->{'coursedesc'} = $ENV{'course.'.$ENV{'request.course.id'}.
1740: 'description'};
1.119 matthew 1741: $sheet->{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);
1742: #
1743: #
1744: $sheet->{'f'} = {};
1745: $sheet->{'constants'} = {};
1746: $sheet->{'othersheets'} = [];
1747: $sheet->{'rowlabel'} = {};
1748: #
1749: #
1750: $sheet->{'safe'}=&initsheet($sheet->{'sheettype'});
1.116 matthew 1751: #
1.119 matthew 1752: # Place all the %$sheet items into the safe space except the safe space
1753: # itself
1.105 matthew 1754: my $initstring = '';
1.119 matthew 1755: foreach (qw/uname udom sheettype usymb cid csec coursefilename
1756: cnum cdom chome uhome/) {
1757: $initstring.= qq{\$$_="$sheet->{$_}";};
1.105 matthew 1758: }
1.119 matthew 1759: $sheet->{'safe'}->reval($initstring);
1760: return $sheet;
1.28 www 1761: }
1762:
1.19 www 1763: # ------------------------------------------------------------ Save spreadsheet
1764: sub writesheet {
1.119 matthew 1765: my ($sheet,$makedef)=@_;
1766: my $cid=$sheet->{'cid'};
1.104 matthew 1767: if (&Apache::lonnet::allowed('opa',$cid)) {
1.119 matthew 1768: my %f=&getformulas($sheet);
1769: my $stype= $sheet->{'sheettype'};
1770: my $cnum = $sheet->{'cnum'};
1771: my $cdom = $sheet->{'cdom'};
1772: my $chome= $sheet->{'chome'};
1773: my $fn = $sheet->{'filename'};
1.104 matthew 1774: # Cache new sheet
1775: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1776: # Write sheet
1777: foreach (keys(%f)) {
1.131 matthew 1778: delete($f{$_}) if ($f{$_} eq 'import');
1.104 matthew 1779: }
1.131 matthew 1780: my $reply = &Apache::lonnet::put($fn,\%f,$cdom,$cnum);
1.104 matthew 1781: if ($reply eq 'ok') {
1.131 matthew 1782: $reply = &Apache::lonnet::put($stype.'_spreadsheets',
1783: {$fn => $ENV{'user.name'}.'@'.$ENV{'user.domain'}},
1784: $cdom,$cnum);
1.104 matthew 1785: if ($reply eq 'ok') {
1786: if ($makedef) {
1.131 matthew 1787: return &Apache::lonnet::put('environment',
1788: {'spreadsheet_default_'.$stype => $fn },
1789: $cdom,$cnum);
1.104 matthew 1790: }
1791: return $reply;
1792: }
1793: return $reply;
1794: }
1795: return $reply;
1796: }
1797: return 'unauthorized';
1.19 www 1798: }
1799:
1.10 www 1800: # ----------------------------------------------- Make a temp copy of the sheet
1.28 www 1801: # "Modified workcopy" - interactive only
1802: #
1.10 www 1803: sub tmpwrite {
1.119 matthew 1804: my ($sheet) = @_;
1.28 www 1805: my $fn=$ENV{'user.name'}.'_'.
1.119 matthew 1806: $ENV{'user.domain'}.'_spreadsheet_'.$sheet->{'usymb'}.'_'.
1807: $sheet->{'filename'};
1.10 www 1808: $fn=~s/\W/\_/g;
1809: $fn=$tmpdir.$fn.'.tmp';
1810: my $fh;
1811: if ($fh=Apache::File->new('>'.$fn)) {
1.119 matthew 1812: print $fh join("\n",&getformulas($sheet));
1.10 www 1813: }
1814: }
1815:
1816: # ---------------------------------------------------------- Read the temp copy
1817: sub tmpread {
1.119 matthew 1818: my ($sheet,$nfield,$nform)=@_;
1.28 www 1819: my $fn=$ENV{'user.name'}.'_'.
1.119 matthew 1820: $ENV{'user.domain'}.'_spreadsheet_'.$sheet->{'usymb'}.'_'.
1821: $sheet->{'filename'};
1.10 www 1822: $fn=~s/\W/\_/g;
1823: $fn=$tmpdir.$fn.'.tmp';
1824: my $fh;
1825: my %fo=();
1.92 www 1826: my $countrows=0;
1.10 www 1827: if ($fh=Apache::File->new($fn)) {
1828: my $name;
1829: while ($name=<$fh>) {
1830: chomp($name);
1831: my $value=<$fh>;
1832: chomp($value);
1833: $fo{$name}=$value;
1.93 www 1834: if ($name=~/^A(\d+)$/) {
1835: if ($1>$countrows) {
1836: $countrows=$1;
1837: }
1838: }
1.10 www 1839: }
1840: }
1.55 www 1841: if ($nform eq 'changesheet') {
1.128 matthew 1842: $fo{'A'.$nfield}=(split(/__&&&\__/,$fo{'A'.$nfield}))[0];
1.55 www 1843: unless ($ENV{'form.sel_'.$nfield} eq 'Default') {
1.57 www 1844: $fo{'A'.$nfield}.='__&&&__'.$ENV{'form.sel_'.$nfield};
1.55 www 1845: }
1.92 www 1846: } elsif ($nfield eq 'insertrow') {
1.93 www 1847: $countrows++;
1.95 www 1848: my $newrow=substr('000000'.$countrows,-7);
1.92 www 1849: if ($nform eq 'top') {
1.94 www 1850: $fo{'A'.$countrows}='--- '.$newrow;
1.92 www 1851: } else {
1.94 www 1852: $fo{'A'.$countrows}='~~~ '.$newrow;
1.92 www 1853: }
1.55 www 1854: } else {
1855: if ($nfield) { $fo{$nfield}=$nform; }
1856: }
1.124 matthew 1857: $sheet->{'f'}=\%fo;
1858: &setformulas($sheet);
1.10 www 1859: }
1860:
1.104 matthew 1861: ##################################################
1862: ##################################################
1.11 www 1863:
1.104 matthew 1864: =pod
1.11 www 1865:
1.104 matthew 1866: =item &parmval()
1.11 www 1867:
1.104 matthew 1868: Determine the value of a parameter.
1.11 www 1869:
1.119 matthew 1870: Inputs: $what, the parameter needed, $sheet, the safe space
1.11 www 1871:
1.104 matthew 1872: Returns: The value of a parameter, or '' if none.
1.11 www 1873:
1.104 matthew 1874: This function cascades through the possible levels searching for a value for
1875: a parameter. The levels are checked in the following order:
1876: user, course (at section level and course level), map, and lonnet::metadata.
1877: This function uses %parmhash, which must be tied prior to calling it.
1878: This function also requires %courseopt and %useropt to be initialized for
1879: this user and course.
1.11 www 1880:
1.104 matthew 1881: =cut
1.11 www 1882:
1.104 matthew 1883: ##################################################
1884: ##################################################
1885: sub parmval {
1.119 matthew 1886: my ($what,$sheet)=@_;
1887: my $symb = $sheet->{'usymb'};
1.104 matthew 1888: unless ($symb) { return ''; }
1889: #
1.119 matthew 1890: my $cid = $sheet->{'cid'};
1891: my $csec = $sheet->{'csec'};
1892: my $uname = $sheet->{'uname'};
1893: my $udom = $sheet->{'udom'};
1.104 matthew 1894: my $result='';
1895: #
1896: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1897: # Cascading lookup scheme
1898: my $rwhat=$what;
1899: $what =~ s/^parameter\_//;
1900: $what =~ s/\_([^\_]+)$/\.$1/;
1901: #
1902: my $symbparm = $symb.'.'.$what;
1903: my $mapparm = $mapname.'___(all).'.$what;
1904: my $usercourseprefix = $uname.'_'.$udom.'_'.$cid;
1905: #
1906: my $seclevel = $usercourseprefix.'.['.$csec.'].'.$what;
1907: my $seclevelr = $usercourseprefix.'.['.$csec.'].'.$symbparm;
1908: my $seclevelm = $usercourseprefix.'.['.$csec.'].'.$mapparm;
1909: #
1910: my $courselevel = $usercourseprefix.'.'.$what;
1911: my $courselevelr = $usercourseprefix.'.'.$symbparm;
1912: my $courselevelm = $usercourseprefix.'.'.$mapparm;
1913: # fourth, check user
1.115 albertel 1914: if (defined($uname)) {
1915: return $useropt{$courselevelr} if (defined($useropt{$courselevelr}));
1916: return $useropt{$courselevelm} if (defined($useropt{$courselevelm}));
1917: return $useropt{$courselevel} if (defined($useropt{$courselevel}));
1.104 matthew 1918: }
1919: # third, check course
1.115 albertel 1920: if (defined($csec)) {
1921: return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
1922: return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
1923: return $courseopt{$seclevel} if (defined($courseopt{$seclevel}));
1.104 matthew 1924: }
1925: #
1.115 albertel 1926: return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
1927: return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
1928: return $courseopt{$courselevel} if (defined($courseopt{$courselevel}));
1.104 matthew 1929: # second, check map parms
1930: my $thisparm = $parmhash{$symbparm};
1.115 albertel 1931: return $thisparm if (defined($thisparm));
1.104 matthew 1932: # first, check default
1933: return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1.11 www 1934: }
1935:
1.133 matthew 1936:
1937: ##################################################################
1938: ## Row label formatting routines ##
1939: ##################################################################
1940: sub format_html_rowlabel {
1941: my $rowlabel = shift;
1942: return '' if ($rowlabel eq '');
1943: my ($type,$labeldata) = split(':',$rowlabel,2);
1944: my $result = '';
1945: if ($type eq 'symb') {
1946: my ($symb,$uname,$udom,$title) = split(':',$labeldata);
1947: $symb = &Apache::lonnet::unescape($symb);
1948: $result = '<a href="/adm/assesscalc?usymb='.$symb.
1949: '&uname='.$uname.'&udom='.$udom.'">'.$title.'</a>';
1950: } elsif ($type eq 'student') {
1951: my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
1952: $result ='<a href="/adm/studentcalc?uname='.$sname.
1953: '&udom='.$sdom.'">';
1954: $result.=$section.' '.$id." ".$fullname.'</a>';
1955: } elsif ($type eq 'parameter') {
1956: $result = $labeldata;
1957: } else {
1958: $result = '<b><font size=+1>'.$rowlabel.'</font></b>';
1959: }
1960: return $result;
1961: }
1962:
1963: sub format_csv_rowlabel {
1964: my $rowlabel = shift;
1965: return '' if ($rowlabel eq '');
1966: my ($type,$labeldata) = split(':',$rowlabel,2);
1967: my $result = '';
1968: if ($type eq 'symb') {
1969: my ($symb,$uname,$udom,$title) = split(':',$labeldata);
1970: $symb = &Apache::lonnet::unescape($symb);
1971: $result = $title;
1972: } elsif ($type eq 'student') {
1973: my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
1974: $result = join('","',($sname,$sdom,$fullname,$section,$id));
1975: } elsif ($type eq 'parameter') {
1976: $labeldata =~ s/<br>/ /g;
1977: $result = $labeldata;
1978: } else {
1979: $result = $rowlabel;
1980: }
1981: return '"'.$result.'"';
1982: }
1983:
1.134 matthew 1984: sub format_excel_rowlabel {
1.125 matthew 1985: my $rowlabel = shift;
1.132 matthew 1986: return '' if ($rowlabel eq '');
1.125 matthew 1987: my ($type,$labeldata) = split(':',$rowlabel,2);
1988: my $result = '';
1989: if ($type eq 'symb') {
1990: my ($symb,$uname,$udom,$title) = split(':',$labeldata);
1991: $symb = &Apache::lonnet::unescape($symb);
1.133 matthew 1992: $result = $title;
1.125 matthew 1993: } elsif ($type eq 'student') {
1994: my ($sname,$sdom,$fullname,$section,$id) = split(':',$labeldata);
1.134 matthew 1995: $section = '' if (! defined($section));
1996: $id = '' if (! defined($id));
1997: my @Data = ($sname,$sdom,$fullname,$section,$id);
1998: $result = \@Data;
1.125 matthew 1999: } elsif ($type eq 'parameter') {
1.133 matthew 2000: $labeldata =~ s/<br>/ /g;
1.127 matthew 2001: $result = $labeldata;
1.125 matthew 2002: } else {
1.133 matthew 2003: $result = $rowlabel;
1.125 matthew 2004: }
2005: return $result;
2006: }
2007:
1.23 www 2008: # ---------------------------------------------- Update rows for course listing
1.28 www 2009: sub updateclasssheet {
1.119 matthew 2010: my ($sheet) = @_;
2011: my $cnum =$sheet->{'cnum'};
2012: my $cdom =$sheet->{'cdom'};
2013: my $cid =$sheet->{'cid'};
2014: my $chome =$sheet->{'chome'};
1.102 matthew 2015: #
1.113 matthew 2016: %Section = ();
2017: #
1.102 matthew 2018: # Read class list and row labels
1.118 matthew 2019: my $classlist = &Apache::loncoursedata::get_classlist();
2020: if (! defined($classlist)) {
2021: return 'Could not access course classlist';
2022: }
1.102 matthew 2023: #
1.23 www 2024: my %currentlist=();
1.118 matthew 2025: foreach my $student (keys(%$classlist)) {
2026: my ($studentDomain,$studentName,$end,$start,$id,$studentSection,
2027: $fullname,$status) = @{$classlist->{$student}};
2028: if ($ENV{'form.Status'} eq $status || $ENV{'form.Status'} eq 'Any') {
1.125 matthew 2029: $currentlist{$student}=join(':',('student',$studentName,
2030: $studentDomain,$fullname,
2031: $studentSection,$id));
1.118 matthew 2032: }
2033: }
1.102 matthew 2034: #
2035: # Find discrepancies between the course row table and this
2036: #
1.119 matthew 2037: my %f=&getformulas($sheet);
1.102 matthew 2038: my $changed=0;
2039: #
1.119 matthew 2040: $sheet->{'maxrow'}=0;
1.102 matthew 2041: my %existing=();
2042: #
2043: # Now obsolete rows
2044: foreach (keys(%f)) {
2045: if ($_=~/^A(\d+)/) {
1.119 matthew 2046: if ($1 > $sheet->{'maxrow'}) {
2047: $sheet->{'maxrow'}= $1;
2048: }
1.102 matthew 2049: $existing{$f{$_}}=1;
2050: unless ((defined($currentlist{$f{$_}})) || (!$1) ||
1.120 matthew 2051: ($f{$_}=~/^(~~~|---)/)) {
1.102 matthew 2052: $f{$_}='!!! Obsolete';
2053: $changed=1;
1.23 www 2054: }
1.78 matthew 2055: }
1.102 matthew 2056: }
2057: #
2058: # New and unknown keys
1.128 matthew 2059: foreach my $student (sort keys(%currentlist)) {
2060: unless ($existing{$student}) {
1.102 matthew 2061: $changed=1;
1.119 matthew 2062: $sheet->{'maxrow'}++;
1.128 matthew 2063: $f{'A'.$sheet->{'maxrow'}}=$student;
1.78 matthew 2064: }
1.23 www 2065: }
1.119 matthew 2066: if ($changed) {
1.124 matthew 2067: $sheet->{'f'} = \%f;
2068: &setformulas($sheet,%f);
1.119 matthew 2069: }
1.102 matthew 2070: #
1.125 matthew 2071: &setrowlabels($sheet,\%currentlist);
1.23 www 2072: }
1.5 www 2073:
1.28 www 2074: # ----------------------------------- Update rows for student and assess sheets
1.136 ! matthew 2075: sub get_student_rowlabels {
! 2076: my ($sheet) = @_;
! 2077: #
! 2078: my %course_db;
! 2079: #
! 2080: my $stype = $sheet->{'sheettype'};
! 2081: my $uname = $sheet->{'uname'};
! 2082: my $udom = $sheet->{'udom'};
! 2083: #
! 2084: $sheet->{'rowlabel'} = {};
! 2085: #
! 2086: my $identifier =$sheet->{'coursefilename'}.'_'.$stype;
! 2087: if ($rowlabel_cache{$identifier}) {
! 2088: %{$sheet->{'rowlabel'}}=split(/___;___/,$rowlabel_cache{$identifier});
! 2089: } else {
! 2090: # Get the data and store it in the cache
! 2091: # Tie hash
! 2092: tie(%course_db,'GDBM_File',$sheet->{'coursefilename'}.'.db',
! 2093: &GDBM_READER(),0640);
! 2094: if (! tied(%course_db)) {
! 2095: return 'Could not access course data';
! 2096: }
! 2097: #
! 2098: my %assesslist;
! 2099: foreach ('Feedback','Evaluation','Tutoring','Discussion') {
! 2100: my $symb = '_'.lc($_);
! 2101: $assesslist{$symb} = join(':',('symb',$symb,$uname,$udom,$_));
! 2102: }
! 2103: #
! 2104: while (my ($key,$srcf) = each(%course_db)) {
! 2105: next if ($key !~ /^src_(\d+)\.(\d+)$/);
! 2106: my $mapid = $1;
! 2107: my $resid = $2;
! 2108: my $id = $mapid.'.'.$resid;
! 2109: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
! 2110: my $symb=
! 2111: &Apache::lonnet::declutter($course_db{'map_id_'.$mapid}).
! 2112: '___'.$resid.'___'.&Apache::lonnet::declutter($srcf);
! 2113: $assesslist{$symb}='symb:'.&Apache::lonnet::escape($symb).':'
! 2114: .$uname.':'.$udom.':'.$course_db{'title_'.$id};
! 2115: }
! 2116: }
! 2117: untie(%course_db);
! 2118: # Store away the data
! 2119: $sheet->{'rowlabel'} = \%assesslist;
! 2120: $rowlabel_cache{$identifier}=join('___;___',%{$sheet->{'rowlabel'}});
! 2121: }
! 2122:
! 2123: }
! 2124:
! 2125: sub get_assess_rowlabels {
1.119 matthew 2126: my ($sheet) = @_;
1.128 matthew 2127: #
1.136 ! matthew 2128: my %course_db;
1.128 matthew 2129: #
2130: my $stype = $sheet->{'sheettype'};
2131: my $uname = $sheet->{'uname'};
2132: my $udom = $sheet->{'udom'};
1.136 ! matthew 2133: my $usymb = $sheet->{'usymb'};
! 2134: #
1.119 matthew 2135: $sheet->{'rowlabel'} = {};
1.136 ! matthew 2136: my $identifier =$sheet->{'coursefilename'}.'_'.$stype.'_'.$usymb;
! 2137: #
! 2138: if ($rowlabel_cache{$identifier}) {
! 2139: %{$sheet->{'rowlabel'}}=split(/___;___/,$rowlabel_cache{$identifier});
1.104 matthew 2140: } else {
1.136 ! matthew 2141: # Get the data and store it in the cache
1.104 matthew 2142: # Tie hash
1.136 ! matthew 2143: tie(%course_db,'GDBM_File',$sheet->{'coursefilename'}.'.db',
1.104 matthew 2144: &GDBM_READER(),0640);
1.136 ! matthew 2145: if (! tied(%course_db)) {
1.104 matthew 2146: return 'Could not access course data';
2147: }
1.125 matthew 2148: #
1.128 matthew 2149: my %parameter_labels=
2150: ('timestamp' =>
2151: 'parameter:Timestamp of Last Transaction<br>timestamp',
2152: 'subnumber' =>
2153: 'parameter:Number of Submissions<br>subnumber',
2154: 'tutornumber' =>
2155: 'parameter:Number of Tutor Responses<br>tutornumber',
2156: 'totalpoints' =>
2157: 'parameter:Total Points Granted<br>totalpoints');
1.136 ! matthew 2158: while (my ($key,$srcf) = each(%course_db)) {
! 2159: next if ($key !~ /^src_(\d+)\.(\d+)$/);
! 2160: my $mapid = $1;
! 2161: my $resid = $2;
! 2162: my $id = $mapid.'.'.$resid;
1.104 matthew 2163: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1.136 ! matthew 2164: # Loop through the metadata for this key
! 2165: my @Metadata = split(/,/,
! 2166: &Apache::lonnet::metadata($srcf,'keys'));
! 2167: foreach my $key (@Metadata) {
1.104 matthew 2168: next if ($key !~ /^(stores|parameter)_/);
2169: my $display=
2170: &Apache::lonnet::metadata($srcf,$key.'.display');
2171: unless ($display) {
2172: $display.=
2173: &Apache::lonnet::metadata($srcf,$key.'.name');
2174: }
2175: $display.='<br>'.$key;
1.128 matthew 2176: $parameter_labels{$key}='parameter:'.$display;
1.104 matthew 2177: } # end of foreach
2178: }
1.136 ! matthew 2179: }
! 2180: untie(%course_db);
! 2181: # Store away the results
! 2182: $sheet->{'rowlabel'} = \%parameter_labels;
! 2183: $rowlabel_cache{$identifier}=join('___;___',%{$sheet->{'rowlabel'}});
! 2184: }
! 2185:
! 2186: }
! 2187:
! 2188: sub updatestudentassesssheet {
! 2189: my $sheet = shift;
! 2190: if ($sheet->{'sheettype'} eq 'studentcalc') {
! 2191: &get_student_rowlabels($sheet);
! 2192: } else {
! 2193: &get_assess_rowlabels($sheet);
1.35 www 2194: }
1.136 ! matthew 2195: # Determine if any of the information has changed
1.119 matthew 2196: my %f=&getformulas($sheet);
1.104 matthew 2197: my $changed=0;
2198:
1.119 matthew 2199: $sheet->{'maxrow'} = 0;
1.104 matthew 2200: my %existing=();
2201: # Now obsolete rows
1.136 ! matthew 2202: while (my ($cell, $formula) = each (%f)) {
! 2203: next if ($cell !~ /^A(\d+)/);
! 2204: $sheet->{'maxrow'} = $1 if ($1 > $sheet->{'maxrow'});
! 2205: my ($usy,$ufn)=split(/__&&&\__/,$formula);
1.104 matthew 2206: $existing{$usy}=1;
1.119 matthew 2207: unless ((exists($sheet->{'rowlabel'}->{$usy}) &&
2208: (defined($sheet->{'rowlabel'}->{$usy})) || (!$1) ||
1.136 ! matthew 2209: ($formula =~ /^(~~~|---)/) )) {
1.104 matthew 2210: $f{$_}='!!! Obsolete';
2211: $changed=1;
2212: } elsif ($ufn) {
1.136 ! matthew 2213: # I do not think this works any more
1.119 matthew 2214: $sheet->{'rowlabel'}->{$usy}
1.136 ! matthew 2215: =~s/assesscalc\?usymb\=/assesscalc\?ufn\=$ufn&\usymb\=/;
1.104 matthew 2216: }
1.35 www 2217: }
1.104 matthew 2218: # New and unknown keys
1.119 matthew 2219: foreach (keys(%{$sheet->{'rowlabel'}})) {
1.104 matthew 2220: unless ($existing{$_}) {
2221: $changed=1;
1.119 matthew 2222: $sheet->{'maxrow'}++;
2223: $f{'A'.$sheet->{'maxrow'}}=$_;
1.78 matthew 2224: }
1.104 matthew 2225: }
1.119 matthew 2226: if ($changed) {
1.124 matthew 2227: $sheet->{'f'} = \%f;
2228: &setformulas($sheet);
1.119 matthew 2229: }
1.5 www 2230: }
1.3 www 2231:
1.24 www 2232: # ------------------------------------------------ Load data for one assessment
1.16 www 2233:
1.135 matthew 2234: sub loadstudent{
1.119 matthew 2235: my ($sheet)=@_;
1.16 www 2236: my %c=();
1.119 matthew 2237: my %f=&getformulas($sheet);
2238: $cachedassess=$sheet->{'uname'}.':'.$sheet->{'udom'};
1.102 matthew 2239: # Get ALL the student preformance data
1.119 matthew 2240: my @tmp = &Apache::lonnet::dump($sheet->{'cid'},
2241: $sheet->{'udom'},
2242: $sheet->{'uname'},
1.102 matthew 2243: undef);
2244: if ($tmp[0] !~ /^error:/) {
2245: %cachedstores = @tmp;
1.39 www 2246: }
1.102 matthew 2247: undef @tmp;
2248: #
1.36 www 2249: my @assessdata=();
1.136 ! matthew 2250: while (my ($cell,$value) = each (%f)) {
! 2251: next if ($cell !~ /^A(\d+)/);
1.104 matthew 2252: my $row=$1;
1.136 ! matthew 2253: next if (($value =~ /^[!~-]/) || ($row==0));
! 2254: my ($usy,$ufn)=split(/__&&&\__/,$value);
1.128 matthew 2255: @assessdata=&exportsheet($sheet,$sheet->{'uname'},
1.119 matthew 2256: $sheet->{'udom'},
1.104 matthew 2257: 'assesscalc',$usy,$ufn);
2258: my $index=0;
2259: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
2260: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1.136 ! matthew 2261: if (defined($assessdata[$index])) {
1.104 matthew 2262: my $col=$_;
2263: if ($assessdata[$index]=~/\D/) {
2264: $c{$col.$row}="'".$assessdata[$index]."'";
2265: } else {
2266: $c{$col.$row}=$assessdata[$index];
2267: }
2268: unless ($col eq 'A') {
2269: $f{$col.$row}='import';
2270: }
2271: }
2272: $index++;
1.16 www 2273: }
1.78 matthew 2274: }
1.39 www 2275: $cachedassess='';
2276: undef %cachedstores;
1.124 matthew 2277: $sheet->{'f'} = \%f;
2278: &setformulas($sheet);
1.122 matthew 2279: &setconstants($sheet,\%c);
1.16 www 2280: }
2281:
1.24 www 2282: # --------------------------------------------------- Load data for one student
1.109 matthew 2283: #
1.30 www 2284: sub loadcourse {
1.119 matthew 2285: my ($sheet,$r)=@_;
1.135 matthew 2286: #
1.24 www 2287: my %c=();
1.119 matthew 2288: my %f=&getformulas($sheet);
1.135 matthew 2289: #
1.37 www 2290: my $total=0;
1.78 matthew 2291: foreach (keys(%f)) {
1.37 www 2292: if ($_=~/^A(\d+)/) {
1.97 www 2293: unless ($f{$_}=~/^[\!\~\-]/) { $total++; }
1.37 www 2294: }
1.78 matthew 2295: }
1.37 www 2296: my $now=0;
2297: my $since=time;
1.39 www 2298: $r->print(<<ENDPOP);
2299: <script>
2300: popwin=open('','popwin','width=400,height=100');
2301: popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
1.50 www 2302: '<h3>Spreadsheet Calculation Progress</h3>'+
1.39 www 2303: '<form name=popremain>'+
2304: '<input type=text size=35 name=remaining value=Starting></form>'+
2305: '</body></html>');
1.42 www 2306: popwin.document.close();
1.39 www 2307: </script>
2308: ENDPOP
1.37 www 2309: $r->rflush();
1.78 matthew 2310: foreach (keys(%f)) {
1.104 matthew 2311: next if ($_!~/^A(\d+)/);
2312: my $row=$1;
2313: next if (($f{$_}=~/^[\!\~\-]/) || ($row==0));
1.130 matthew 2314: my ($sname,$sdom) = split(':',$f{$_});
2315: my @studentdata=&exportsheet($sheet,$sname,$sdom,'studentcalc');
1.104 matthew 2316: undef %userrdatas;
2317: $now++;
2318: $r->print('<script>popwin.document.popremain.remaining.value="'.
1.37 www 2319: $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
1.104 matthew 2320: ' secs remaining";</script>');
2321: $r->rflush();
2322: #
2323: my $index=0;
2324: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
2325: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1.132 matthew 2326: if (defined($studentdata[$index])) {
1.104 matthew 2327: my $col=$_;
2328: if ($studentdata[$index]=~/\D/) {
2329: $c{$col.$row}="'".$studentdata[$index]."'";
2330: } else {
2331: $c{$col.$row}=$studentdata[$index];
2332: }
2333: unless ($col eq 'A') {
2334: $f{$col.$row}='import';
2335: }
1.132 matthew 2336: }
2337: $index++;
1.24 www 2338: }
1.78 matthew 2339: }
1.124 matthew 2340: $sheet->{'f'}=\%f;
2341: &setformulas($sheet);
1.122 matthew 2342: &setconstants($sheet,\%c);
1.43 www 2343: $r->print('<script>popwin.close()</script>');
1.37 www 2344: $r->rflush();
1.24 www 2345: }
2346:
1.6 www 2347: # ------------------------------------------------ Load data for one assessment
1.109 matthew 2348: #
1.29 www 2349: sub loadassessment {
1.119 matthew 2350: my ($sheet)=@_;
1.29 www 2351:
1.119 matthew 2352: my $uhome = $sheet->{'uhome'};
2353: my $uname = $sheet->{'uname'};
2354: my $udom = $sheet->{'udom'};
2355: my $symb = $sheet->{'usymb'};
2356: my $cid = $sheet->{'cid'};
2357: my $cnum = $sheet->{'cnum'};
2358: my $cdom = $sheet->{'cdom'};
2359: my $chome = $sheet->{'chome'};
1.29 www 2360:
1.6 www 2361: my $namespace;
1.29 www 2362: unless ($namespace=$cid) { return ''; }
1.104 matthew 2363: # Get stored values
2364: my %returnhash=();
2365: if ($cachedassess eq $uname.':'.$udom) {
2366: #
2367: # get data out of the dumped stores
2368: #
2369: my $version=$cachedstores{'version:'.$symb};
2370: my $scope;
2371: for ($scope=1;$scope<=$version;$scope++) {
2372: foreach (split(/\:/,$cachedstores{$scope.':keys:'.$symb})) {
2373: $returnhash{$_}=$cachedstores{$scope.':'.$symb.':'.$_};
2374: }
2375: }
2376: } else {
2377: #
2378: # restore individual
2379: #
1.109 matthew 2380: %returnhash = &Apache::lonnet::restore($symb,$namespace,$udom,$uname);
2381: for (my $version=1;$version<=$returnhash{'version'};$version++) {
1.104 matthew 2382: foreach (split(/\:/,$returnhash{$version.':keys'})) {
2383: $returnhash{$_}=$returnhash{$version.':'.$_};
2384: }
2385: }
1.6 www 2386: }
1.109 matthew 2387: #
1.104 matthew 2388: # returnhash now has all stores for this resource
2389: # convert all "_" to "." to be able to use libraries, multiparts, etc
1.109 matthew 2390: #
2391: # This is dumb. It is also necessary :(
1.76 www 2392: my @oldkeys=keys %returnhash;
1.109 matthew 2393: #
1.116 matthew 2394: foreach my $name (@oldkeys) {
2395: my $value=$returnhash{$name};
2396: delete $returnhash{$name};
1.76 www 2397: $name=~s/\_/\./g;
2398: $returnhash{$name}=$value;
1.78 matthew 2399: }
1.104 matthew 2400: # initialize coursedata and userdata for this user
1.31 www 2401: undef %courseopt;
2402: undef %useropt;
1.29 www 2403:
2404: my $userprefix=$uname.'_'.$udom.'_';
1.116 matthew 2405:
1.11 www 2406: unless ($uhome eq 'no_host') {
1.104 matthew 2407: # Get coursedata
1.105 matthew 2408: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
1.116 matthew 2409: my %Tmp = &Apache::lonnet::dump('resourcedata',$cdom,$cnum);
2410: $courserdatas{$cid}=\%Tmp;
2411: $courserdatas{$cid.'.last_cache'}=time;
1.105 matthew 2412: }
1.116 matthew 2413: while (my ($name,$value) = each(%{$courserdatas{$cid}})) {
2414: $courseopt{$userprefix.$name}=$value;
1.104 matthew 2415: }
2416: # Get userdata (if present)
1.116 matthew 2417: unless ((time-$userrdatas{$uname.'@'.$udom.'.last_cache'})<240) {
2418: my %Tmp = &Apache::lonnet::dump('resourcedata',$udom,$uname);
2419: $userrdatas{$cid} = \%Tmp;
1.114 matthew 2420: # Most of the time the user does not have a 'resourcedata.db'
2421: # file. We need to cache that we got nothing instead of bothering
2422: # with requesting it every time.
1.116 matthew 2423: $userrdatas{$uname.'@'.$udom.'.last_cache'}=time;
1.109 matthew 2424: }
1.116 matthew 2425: while (my ($name,$value) = each(%{$userrdatas{$cid}})) {
2426: $useropt{$userprefix.$name}=$value;
1.104 matthew 2427: }
1.29 www 2428: }
1.104 matthew 2429: # now courseopt, useropt initialized for this user and course
2430: # (used by parmval)
2431: #
2432: # Load keys for this assessment only
2433: #
1.60 www 2434: my %thisassess=();
2435: my ($symap,$syid,$srcf)=split(/\_\_\_/,$symb);
1.78 matthew 2436: foreach (split(/\,/,&Apache::lonnet::metadata($srcf,'keys'))) {
1.60 www 2437: $thisassess{$_}=1;
1.78 matthew 2438: }
1.104 matthew 2439: #
2440: # Load parameters
2441: #
2442: my %c=();
2443: if (tie(%parmhash,'GDBM_File',
1.119 matthew 2444: $sheet->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
2445: my %f=&getformulas($sheet);
1.125 matthew 2446: foreach my $cell (keys(%f)) {
2447: next if ($cell !~ /^A/);
2448: next if ($f{$cell} =~/^[\!\~\-]/);
2449: if ($f{$cell}=~/^parameter/) {
2450: if (defined($thisassess{$f{$cell}})) {
2451: my $val = &parmval($f{$cell},$sheet);
2452: $c{$cell} = $val;
2453: $c{$f{$cell}} = $val;
1.104 matthew 2454: }
2455: } else {
1.125 matthew 2456: my $key=$f{$cell};
1.104 matthew 2457: my $ckey=$key;
2458: $key=~s/^stores\_/resource\./;
2459: $key=~s/\_/\./g;
1.125 matthew 2460: $c{$cell}=$returnhash{$key};
1.104 matthew 2461: $c{$ckey}=$returnhash{$key};
2462: }
1.6 www 2463: }
1.104 matthew 2464: untie(%parmhash);
1.78 matthew 2465: }
1.122 matthew 2466: &setconstants($sheet,\%c);
1.6 www 2467: }
2468:
1.10 www 2469: # --------------------------------------------------------- Various form fields
2470:
2471: sub textfield {
2472: my ($title,$name,$value)=@_;
2473: return "\n<p><b>$title:</b><br>".
1.104 matthew 2474: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
1.10 www 2475: }
2476:
2477: sub hiddenfield {
2478: my ($name,$value)=@_;
2479: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
2480: }
2481:
2482: sub selectbox {
2483: my ($title,$name,$value,%options)=@_;
2484: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
1.78 matthew 2485: foreach (sort keys(%options)) {
1.10 www 2486: $selout.='<option value="'.$_.'"';
2487: if ($_ eq $value) { $selout.=' selected'; }
2488: $selout.='>'.$options{$_}.'</option>';
1.78 matthew 2489: }
1.10 www 2490: return $selout.'</select>';
2491: }
2492:
1.28 www 2493: # =============================================== Update information in a sheet
2494: #
2495: # Add new users or assessments, etc.
2496: #
2497:
2498: sub updatesheet {
1.119 matthew 2499: my ($sheet)=@_;
1.136 ! matthew 2500: if ($sheet->{'sheettype'} eq 'classcalc') {
1.119 matthew 2501: return &updateclasssheet($sheet);
1.28 www 2502: } else {
1.119 matthew 2503: return &updatestudentassesssheet($sheet);
1.28 www 2504: }
2505: }
2506:
2507: # =================================================== Load the rows for a sheet
2508: #
2509: # Import the data for rows
2510: #
2511:
1.37 www 2512: sub loadrows {
1.119 matthew 2513: my ($sheet,$r)=@_;
2514: my $stype=$sheet->{'sheettype'};
1.28 www 2515: if ($stype eq 'classcalc') {
1.119 matthew 2516: &loadcourse($sheet,$r);
1.28 www 2517: } elsif ($stype eq 'studentcalc') {
1.119 matthew 2518: &loadstudent($sheet);
1.28 www 2519: } else {
1.119 matthew 2520: &loadassessment($sheet);
1.28 www 2521: }
2522: }
2523:
1.47 www 2524: # ======================================================= Forced recalculation?
2525:
2526: sub checkthis {
2527: my ($keyname,$time)=@_;
2528: return ($time<$expiredates{$keyname});
2529: }
1.104 matthew 2530:
1.47 www 2531: sub forcedrecalc {
2532: my ($uname,$udom,$stype,$usymb)=@_;
2533: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2534: my $time=$oldsheets{$key.'.time'};
1.53 www 2535: if ($ENV{'form.forcerecalc'}) { return 1; }
1.47 www 2536: unless ($time) { return 1; }
2537: if ($stype eq 'assesscalc') {
1.120 matthew 2538: my $map=(split(/___/,$usymb))[0];
1.47 www 2539: if (&checkthis('::assesscalc:',$time) ||
2540: &checkthis('::assesscalc:'.$map,$time) ||
2541: &checkthis('::assesscalc:'.$usymb,$time) ||
1.49 www 2542: &checkthis($uname.':'.$udom.':assesscalc:',$time) ||
2543: &checkthis($uname.':'.$udom.':assesscalc:'.$map,$time) ||
2544: &checkthis($uname.':'.$udom.':assesscalc:'.$usymb,$time)) {
1.47 www 2545: return 1;
2546: }
2547: } else {
2548: if (&checkthis('::studentcalc:',$time) ||
1.51 www 2549: &checkthis($uname.':'.$udom.':studentcalc:',$time)) {
1.47 www 2550: return 1;
2551: }
2552: }
2553: return 0;
2554: }
2555:
1.28 www 2556: # ============================================================== Export handler
1.128 matthew 2557: # exportsheet
2558: # returns the export row for a spreadsheet.
2559: #
1.28 www 2560: sub exportsheet {
1.128 matthew 2561: my ($sheet,$uname,$udom,$stype,$usymb,$fn)=@_;
2562: $uname = $uname || $sheet->{'uname'};
2563: $udom = $udom || $sheet->{'udom'};
2564: $stype = $stype || $sheet->{'sheettype'};
1.104 matthew 2565: my @exportarr=();
1.132 matthew 2566: if (defined($usymb) && ($usymb=~/^\_(\w+)/) &&
2567: (!defined($fn) || $fn eq '')) {
1.104 matthew 2568: $fn='default_'.$1;
2569: }
2570: #
2571: # Check if cached
2572: #
2573: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2574: my $found='';
2575: if ($oldsheets{$key}) {
1.136 ! matthew 2576: &Apache::lonnet::logthis("got cached $stype for $uname");
1.120 matthew 2577: foreach (split(/___&\___/,$oldsheets{$key})) {
2578: my ($name,$value)=split(/___=___/,$_);
1.46 www 2579: if ($name eq $fn) {
1.104 matthew 2580: $found=$value;
1.46 www 2581: }
1.104 matthew 2582: }
1.46 www 2583: }
1.104 matthew 2584: unless ($found) {
1.128 matthew 2585: &cachedssheets($sheet,$uname,$udom);
1.104 matthew 2586: if ($oldsheets{$key}) {
1.136 ! matthew 2587: &Apache::lonnet::logthis("got cached $stype for $uname");
1.120 matthew 2588: foreach (split(/___&\___/,$oldsheets{$key})) {
2589: my ($name,$value)=split(/___=___/,$_);
1.104 matthew 2590: if ($name eq $fn) {
2591: $found=$value;
2592: }
2593: }
2594: }
1.44 www 2595: }
1.104 matthew 2596: #
2597: # Check if still valid
2598: #
2599: if ($found) {
2600: if (&forcedrecalc($uname,$udom,$stype,$usymb)) {
2601: $found='';
2602: }
2603: }
2604: if ($found) {
2605: #
2606: # Return what was cached
2607: #
1.120 matthew 2608: @exportarr=split(/___;___/,$found);
2609: return @exportarr;
2610: }
2611: #
2612: # Not cached
1.135 matthew 2613: #
1.128 matthew 2614: my ($newsheet)=&makenewsheet($uname,$udom,$stype,$usymb);
2615: &readsheet($newsheet,$fn);
2616: &updatesheet($newsheet);
2617: &loadrows($newsheet);
2618: &calcsheet($newsheet);
2619: @exportarr=&exportdata($newsheet);
1.131 matthew 2620: ##
2621: ## Store now
2622: ##
1.120 matthew 2623: #
1.131 matthew 2624: # load in the old value
1.120 matthew 2625: #
1.131 matthew 2626: my %currentlystored=();
1.120 matthew 2627: if ($stype eq 'studentcalc') {
1.131 matthew 2628: my @tmp = &Apache::lonnet::get('nohist_calculatedsheets',
2629: [$key],
2630: $sheet->{'cdom'},$sheet->{'cnum'});
2631: if ($tmp[0]!~/^error/) {
2632: %currentlystored = @tmp;
2633: }
2634: } else {
2635: my @tmp = &Apache::lonnet::get('nohist_calculatedsheets_'.
2636: $sheet->{'cid'},[$key],
2637: $sheet->{'udom'},$sheet->{'uname'});
2638: if ($tmp[0]!~/^error/) {
2639: %currentlystored = @tmp;
1.120 matthew 2640: }
2641: }
1.131 matthew 2642: #
2643: # Add the new line
2644: #
1.120 matthew 2645: $currentlystored{$fn}=join('___;___',@exportarr);
2646: #
1.131 matthew 2647: # Stick everything back together
2648: #
1.120 matthew 2649: my $newstore='';
2650: foreach (keys(%currentlystored)) {
2651: if ($newstore) { $newstore.='___&___'; }
2652: $newstore.=$_.'___=___'.$currentlystored{$_};
2653: }
2654: my $now=time;
1.131 matthew 2655: #
2656: # Store away the new value
2657: #
1.120 matthew 2658: if ($stype eq 'studentcalc') {
2659: &Apache::lonnet::put('nohist_calculatedsheets',
2660: { $key => $newstore,
2661: $key.time => $now },
1.131 matthew 2662: $sheet->{'cdom'},$sheet->{'cnum'});
1.120 matthew 2663: } else {
2664: &Apache::lonnet::put('nohist_calculatedsheets_'.$sheet->{'cid'},
2665: { $key => $newstore,
2666: $key.time => $now },
2667: $sheet->{'udom'},
2668: $sheet->{'uname'})
1.78 matthew 2669: }
1.104 matthew 2670: return @exportarr;
1.44 www 2671: }
1.104 matthew 2672:
1.48 www 2673: # ============================================================ Expiration Dates
2674: #
2675: # Load previously cached student spreadsheets for this course
2676: #
1.136 ! matthew 2677: sub load_spreadsheet_expirationdates {
1.48 www 2678: undef %expiredates;
2679: my $cid=$ENV{'request.course.id'};
1.128 matthew 2680: my @tmp = &Apache::lonnet::dump('nohist_expirationdates',
2681: $ENV{'course.'.$cid.'.domain'},
2682: $ENV{'course.'.$cid.'.num'});
2683: if (lc($tmp[0])!~/^error/){
2684: %expiredates = @tmp;
1.48 www 2685: }
2686: }
1.44 www 2687:
2688: # ===================================================== Calculated sheets cache
2689: #
1.46 www 2690: # Load previously cached student spreadsheets for this course
1.44 www 2691: #
2692:
1.46 www 2693: sub cachedcsheets {
1.44 www 2694: my $cid=$ENV{'request.course.id'};
1.128 matthew 2695: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
2696: $ENV{'course.'.$cid.'.domain'},
2697: $ENV{'course.'.$cid.'.num'});
2698: if ($tmp[0] !~ /^error/) {
2699: my %StupidTempHash = @tmp;
2700: while (my ($key,$value) = each %StupidTempHash) {
2701: $oldsheets{$key} = $value;
1.78 matthew 2702: }
1.44 www 2703: }
1.28 www 2704: }
2705:
1.46 www 2706: # ===================================================== Calculated sheets cache
2707: #
2708: # Load previously cached assessment spreadsheets for this student
2709: #
2710:
2711: sub cachedssheets {
1.128 matthew 2712: my ($sheet,$uname,$udom) = @_;
2713: $uname = $uname || $sheet->{'uname'};
2714: $udom = $udom || $sheet->{'udom'};
1.136 ! matthew 2715: if (! $loadedcaches{$uname.'_'.$udom}) {
1.128 matthew 2716: my @tmp = &Apache::lonnet::dump('nohist_calculatedsheets',
2717: $sheet->{'udom'},
2718: $sheet->{'uname'});
2719: if ($tmp[0] !~ /^error/) {
1.136 ! matthew 2720: my %TempHash = @tmp;
! 2721: my $count = 0;
! 2722: while (my ($key,$value) = each %TempHash) {
1.128 matthew 2723: $oldsheets{$key} = $value;
1.136 ! matthew 2724: $count++;
1.128 matthew 2725: }
1.136 ! matthew 2726: &Apache::lonnet::logthis('saved '.$count.' cached sheets for '.$uname);
1.128 matthew 2727: $loadedcaches{$sheet->{'uname'}.'_'.$sheet->{'udom'}}=1;
1.78 matthew 2728: }
1.46 www 2729: }
1.136 ! matthew 2730:
1.46 www 2731: }
2732:
2733: # ===================================================== Calculated sheets cache
2734: #
2735: # Load previously cached assessment spreadsheets for this student
2736: #
2737:
1.12 www 2738: # ================================================================ Main handler
1.28 www 2739: #
2740: # Interactive call to screen
2741: #
2742: #
1.3 www 2743: sub handler {
1.7 www 2744: my $r=shift;
1.110 www 2745:
1.135 matthew 2746: my ($sheettype) = ($r->uri=~/\/(\w+)$/);
2747:
1.118 matthew 2748: if (! exists($ENV{'form.Status'})) {
2749: $ENV{'form.Status'} = 'Active';
2750: }
1.135 matthew 2751: if ( ! exists($ENV{'form.output'}) ||
2752: ($sheettype ne 'classcalc' &&
2753: lc($ENV{'form.output'}) eq 'recursive excel')) {
1.134 matthew 2754: $ENV{'form.output'} = 'HTML';
2755: }
1.136 ! matthew 2756: #
! 2757: # Overload checking
! 2758: #
1.116 matthew 2759: # Check this server
1.111 matthew 2760: my $loaderror=&Apache::lonnet::overloaderror($r);
2761: if ($loaderror) { return $loaderror; }
1.116 matthew 2762: # Check the course homeserver
1.111 matthew 2763: $loaderror= &Apache::lonnet::overloaderror($r,
2764: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
2765: if ($loaderror) { return $loaderror; }
1.136 ! matthew 2766: #
! 2767: # HTML Header
! 2768: #
1.28 www 2769: if ($r->header_only) {
1.104 matthew 2770: $r->content_type('text/html');
2771: $r->send_http_header;
2772: return OK;
2773: }
1.136 ! matthew 2774: #
1.104 matthew 2775: # Global directory configs
1.136 ! matthew 2776: #
1.106 matthew 2777: $includedir = $r->dir_config('lonIncludes');
2778: $tmpdir = $r->dir_config('lonDaemons').'/tmp/';
1.136 ! matthew 2779: #
! 2780: # Roles Checking
! 2781: #
1.104 matthew 2782: # Needs to be in a course
1.106 matthew 2783: if (! $ENV{'request.course.fn'}) {
2784: # Not in a course, or not allowed to modify parms
2785: $ENV{'user.error.msg'}=
2786: $r->uri.":opa:0:0:Cannot modify spreadsheet";
2787: return HTTP_NOT_ACCEPTABLE;
2788: }
1.136 ! matthew 2789: #
1.106 matthew 2790: # Get query string for limited number of parameters
1.136 ! matthew 2791: #
1.106 matthew 2792: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
2793: ['uname','udom','usymb','ufn']);
1.136 ! matthew 2794: #
! 2795: # Deal with restricted student permissions
! 2796: #
1.111 matthew 2797: if ($ENV{'request.role'} =~ /^st\./) {
2798: delete $ENV{'form.unewfield'} if (exists($ENV{'form.unewfield'}));
2799: delete $ENV{'form.unewformula'} if (exists($ENV{'form.unewformula'}));
2800: }
1.136 ! matthew 2801: #
! 2802: # Clean up symb and spreadsheet filename
! 2803: #
1.106 matthew 2804: if (($ENV{'form.usymb'}=~/^\_(\w+)/) && (!$ENV{'form.ufn'})) {
2805: $ENV{'form.ufn'}='default_'.$1;
2806: }
1.136 ! matthew 2807: #
1.106 matthew 2808: # Interactive loading of specific sheet?
1.136 ! matthew 2809: #
1.106 matthew 2810: if (($ENV{'form.load'}) && ($ENV{'form.loadthissheet'} ne 'Default')) {
2811: $ENV{'form.ufn'}=$ENV{'form.loadthissheet'};
2812: }
2813: #
2814: # Determine the user name and domain for the sheet.
2815: my $aname;
2816: my $adom;
2817: unless ($ENV{'form.uname'}) {
2818: $aname=$ENV{'user.name'};
2819: $adom=$ENV{'user.domain'};
2820: } else {
2821: $aname=$ENV{'form.uname'};
2822: $adom=$ENV{'form.udom'};
2823: }
2824: #
1.136 ! matthew 2825: # Open page, try to prevent browser cache.
! 2826: #
1.106 matthew 2827: $r->content_type('text/html');
2828: $r->header_out('Cache-control','no-cache');
2829: $r->header_out('Pragma','no-cache');
2830: $r->send_http_header;
1.136 ! matthew 2831: #
! 2832: # Header....
! 2833: #
1.106 matthew 2834: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
1.111 matthew 2835: if ($ENV{'request.role'} !~ /^st\./) {
2836: $r->print(<<ENDSCRIPT);
1.10 www 2837: <script language="JavaScript">
2838:
2839: function celledit(cn,cf) {
2840: var cnf=prompt(cn,cf);
1.86 matthew 2841: if (cnf!=null) {
2842: document.sheet.unewfield.value=cn;
1.10 www 2843: document.sheet.unewformula.value=cnf;
2844: document.sheet.submit();
2845: }
2846: }
2847:
1.55 www 2848: function changesheet(cn) {
2849: document.sheet.unewfield.value=cn;
2850: document.sheet.unewformula.value='changesheet';
2851: document.sheet.submit();
2852: }
2853:
1.92 www 2854: function insertrow(cn) {
2855: document.sheet.unewfield.value='insertrow';
2856: document.sheet.unewformula.value=cn;
2857: document.sheet.submit();
2858: }
2859:
1.10 www 2860: </script>
2861: ENDSCRIPT
1.111 matthew 2862: }
1.106 matthew 2863: $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
2864: '<form action="'.$r->uri.'" name=sheet method=post>');
2865: $r->print(&hiddenfield('uname',$ENV{'form.uname'}).
2866: &hiddenfield('udom',$ENV{'form.udom'}).
2867: &hiddenfield('usymb',$ENV{'form.usymb'}).
2868: &hiddenfield('unewfield','').
2869: &hiddenfield('unewformula',''));
2870: $r->rflush();
2871: #
2872: # Full recalc?
1.136 ! matthew 2873: #
1.106 matthew 2874: if ($ENV{'form.forcerecalc'}) {
2875: $r->print('<h4>Completely Recalculating Sheet ...</h4>');
2876: undef %spreadsheets;
2877: undef %courserdatas;
2878: undef %userrdatas;
2879: undef %defaultsheets;
1.136 ! matthew 2880: undef %rowlabel_cache;
1.106 matthew 2881: }
2882: # Read new sheet or modified worksheet
1.135 matthew 2883: my ($sheet)=&makenewsheet($aname,$adom,$sheettype,$ENV{'form.usymb'});
1.106 matthew 2884: #
2885: # If a new formula had been entered, go from work copy
2886: if ($ENV{'form.unewfield'}) {
2887: $r->print('<h2>Modified Workcopy</h2>');
2888: $ENV{'form.unewformula'}=~s/\'/\"/g;
2889: $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
2890: $ENV{'form.unewformula'}.'<p>');
1.119 matthew 2891: $sheet->{'filename'} = $ENV{'form.ufn'};
2892: &tmpread($sheet,$ENV{'form.unewfield'},$ENV{'form.unewformula'});
1.106 matthew 2893: } elsif ($ENV{'form.saveas'}) {
1.119 matthew 2894: $sheet->{'filename'} = $ENV{'form.ufn'};
2895: &tmpread($sheet);
1.106 matthew 2896: } else {
1.119 matthew 2897: &readsheet($sheet,$ENV{'form.ufn'});
1.106 matthew 2898: }
2899: # Print out user information
1.120 matthew 2900: if ($sheet->{'sheettype'} ne 'classcalc') {
1.119 matthew 2901: $r->print('<p><b>User:</b> '.$sheet->{'uname'}.
2902: '<br><b>Domain:</b> '.$sheet->{'udom'});
2903: $r->print('<br><b>Section/Group:</b> '.$sheet->{'csec'});
1.106 matthew 2904: if ($ENV{'form.usymb'}) {
2905: $r->print('<br><b>Assessment:</b> <tt>'.
2906: $ENV{'form.usymb'}.'</tt>');
1.30 www 2907: }
1.106 matthew 2908: }
2909: #
2910: # Check user permissions
1.119 matthew 2911: if (($sheet->{'sheettype'} eq 'classcalc' ) ||
2912: ($sheet->{'uname'} ne $ENV{'user.name'} ) ||
2913: ($sheet->{'udom'} ne $ENV{'user.domain'})) {
2914: unless (&Apache::lonnet::allowed('vgr',$sheet->{'cid'})) {
1.106 matthew 2915: $r->print('<h1>Access Permission Denied</h1>'.
2916: '</form></body></html>');
2917: return OK;
2918: }
2919: }
2920: # Additional options
2921: $r->print('<br />'.
2922: '<input type="submit" name="forcerecalc" '.
2923: 'value="Completely Recalculate Sheet"><p>');
1.119 matthew 2924: if ($sheet->{'sheettype'} eq 'assesscalc') {
1.106 matthew 2925: $r->print('<p><font size=+2>'.
2926: '<a href="/adm/studentcalc?'.
1.119 matthew 2927: 'uname='.$sheet->{'uname'}.
2928: '&udom='.$sheet->{'udom'}.'">'.
1.106 matthew 2929: 'Level up: Student Sheet</a></font><p>');
2930: }
1.119 matthew 2931: if (($sheet->{'sheettype'} eq 'studentcalc') &&
2932: (&Apache::lonnet::allowed('vgr',$sheet->{'cid'}))) {
1.106 matthew 2933: $r->print ('<p><font size=+2><a href="/adm/classcalc">'.
2934: 'Level up: Course Sheet</a></font><p>');
2935: }
2936: # Save dialog
2937: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2938: my $fname=$ENV{'form.ufn'};
2939: $fname=~s/\_[^\_]+$//;
2940: if ($fname eq 'default') { $fname='course_default'; }
2941: $r->print('<input type=submit name=saveas value="Save as ...">'.
2942: '<input type=text size=20 name=newfn value="'.$fname.'">'.
2943: 'make default: <input type=checkbox name="makedefufn"><p>');
2944: }
1.119 matthew 2945: $r->print(&hiddenfield('ufn',$sheet->{'filename'}));
1.106 matthew 2946: # Load dialog
2947: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2948: $r->print('<p><input type=submit name=load value="Load ...">'.
2949: '<select name="loadthissheet">'.
2950: '<option name="default">Default</option>');
1.119 matthew 2951: foreach (&othersheets($sheet)) {
1.106 matthew 2952: $r->print('<option name="'.$_.'"');
2953: if ($ENV{'form.ufn'} eq $_) {
2954: $r->print(' selected');
1.104 matthew 2955: }
1.106 matthew 2956: $r->print('>'.$_.'</option>');
2957: }
2958: $r->print('</select><p>');
1.119 matthew 2959: if ($sheet->{'sheettype'} eq 'studentcalc') {
1.116 matthew 2960: &setothersheets($sheet,
1.119 matthew 2961: &othersheets($sheet,'assesscalc'));
1.104 matthew 2962: }
1.106 matthew 2963: }
1.136 ! matthew 2964: #
! 2965: # Set up caching mechanisms
! 2966: #
! 2967: &load_spreadsheet_expirationdates();
! 2968: # Clear out old caches if we have not seen this class before.
! 2969: if (exists($oldsheets{'course'}) &&
! 2970: $oldsheets{'course'} ne $sheet->{'cid'}) {
! 2971: undef %oldsheets;
! 2972: undef %loadedcaches;
! 2973: }
! 2974: $oldsheets{'course'} = $sheet->{'cid'};
! 2975: #
1.119 matthew 2976: if ($sheet->{'sheettype'} eq 'classcalc') {
1.106 matthew 2977: $r->print("Loading previously calculated student sheets ...\n");
1.104 matthew 2978: $r->rflush();
1.106 matthew 2979: &cachedcsheets();
1.119 matthew 2980: } elsif ($sheet->{'sheettype'} eq 'studentcalc') {
1.106 matthew 2981: $r->print("Loading previously calculated assessment sheets ...\n");
1.46 www 2982: $r->rflush();
1.128 matthew 2983: &cachedssheets($sheet);
1.106 matthew 2984: }
2985: # Update sheet, load rows
2986: $r->print("Loaded sheet(s), updating rows ...<br>\n");
2987: $r->rflush();
2988: #
1.119 matthew 2989: &updatesheet($sheet);
1.106 matthew 2990: $r->print("Updated rows, loading row data ...\n");
2991: $r->rflush();
2992: #
1.119 matthew 2993: &loadrows($sheet,$r);
1.106 matthew 2994: $r->print("Loaded row data, calculating sheet ...<br>\n");
2995: $r->rflush();
2996: #
1.116 matthew 2997: my $calcoutput=&calcsheet($sheet);
1.106 matthew 2998: $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
2999: # See if something to save
3000: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
3001: my $fname='';
3002: if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
3003: $fname=~s/\W/\_/g;
3004: if ($fname eq 'default') { $fname='course_default'; }
1.119 matthew 3005: $fname.='_'.$sheet->{'sheettype'};
3006: $sheet->{'filename'} = $fname;
1.106 matthew 3007: $ENV{'form.ufn'}=$fname;
3008: $r->print('<p>Saving spreadsheet: '.
1.119 matthew 3009: &writesheet($sheet,$ENV{'form.makedefufn'}).
1.116 matthew 3010: '<p>');
1.104 matthew 3011: }
1.106 matthew 3012: }
3013: #
1.116 matthew 3014: # Write the modified worksheet
1.119 matthew 3015: $r->print('<b>Current sheet:</b> '.$sheet->{'filename'}.'<p>');
3016: &tmpwrite($sheet);
3017: if ($sheet->{'sheettype'} eq 'studentcalc') {
1.106 matthew 3018: $r->print('<br>Show rows with empty A column: ');
1.62 www 3019: } else {
3020: $r->print('<br>Show empty rows: ');
1.120 matthew 3021: }
1.106 matthew 3022: #
1.77 www 3023: $r->print(&hiddenfield('userselhidden','true').
1.106 matthew 3024: '<input type="checkbox" name="showall" onClick="submit()"');
3025: #
1.77 www 3026: if ($ENV{'form.showall'}) {
1.106 matthew 3027: $r->print(' checked');
1.77 www 3028: } else {
1.106 matthew 3029: unless ($ENV{'form.userselhidden'}) {
3030: unless
1.128 matthew 3031: ($ENV{'course.'.$sheet->{'cid'}.'.hideemptyrows'} eq 'yes') {
1.106 matthew 3032: $r->print(' checked');
3033: $ENV{'form.showall'}=1;
3034: }
3035: }
1.77 www 3036: }
1.61 www 3037: $r->print('>');
1.120 matthew 3038: #
3039: # CSV format checkbox (classcalc sheets only)
1.134 matthew 3040: $r->print(' Output as <select name="output" size="1" onClick="submit()">'.
3041: "\n");
1.135 matthew 3042: foreach my $mode (qw/HTML CSV Excel/) {
1.134 matthew 3043: $r->print('<option value="'.$mode.'"');
3044: if ($ENV{'form.output'} eq $mode) {
3045: $r->print(' selected ');
3046: }
3047: $r->print('>'.$mode.'</option>'."\n");
1.135 matthew 3048: }
3049: if ($sheet->{'sheettype'} eq 'classcalc') {
3050: $r->print('<option value="recursive excel"');
3051: if ($ENV{'form.output'} eq 'recursive excel') {
3052: $r->print(' selected ');
3053: }
3054: $r->print(">Multi-Sheet Excel</option>\n");
1.134 matthew 3055: }
3056: $r->print("</select>\n");
3057: #
1.119 matthew 3058: if ($sheet->{'sheettype'} eq 'classcalc') {
3059: $r->print(' Student Status: '.
3060: &Apache::lonhtmlcommon::StatusOptions
3061: ($ENV{'form.Status'},'sheet'));
1.69 www 3062: }
1.120 matthew 3063: #
3064: # Buttons to insert rows
1.134 matthew 3065: # $r->print(<<ENDINSERTBUTTONS);
3066: #<br>
3067: #<input type='button' onClick='insertrow("top");'
3068: #value='Insert Row Top'>
3069: #<input type='button' onClick='insertrow("bottom");'
3070: #value='Insert Row Bottom'><br>
3071: #ENDINSERTBUTTONS
1.106 matthew 3072: # Print out sheet
1.119 matthew 3073: &outsheet($r,$sheet);
1.10 www 3074: $r->print('</form></body></html>');
1.106 matthew 3075: # Done
1.3 www 3076: return OK;
1.1 www 3077: }
3078:
3079: 1;
3080: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>