Annotation of loncom/interface/lonspreadsheet.pm, revision 1.118
1.79 matthew 1: #
1.118 ! matthew 2: # $Id: lonspreadsheet.pm,v 1.117 2002/10/16 16:35:42 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.11 www 65: #
1.113 matthew 66: # Caches for coursewide information
67: #
68: my %Section;
69:
70: #
1.44 www 71: # Caches for previously calculated spreadsheets
72: #
73:
74: my %oldsheets;
1.46 www 75: my %loadedcaches;
1.47 www 76: my %expiredates;
1.44 www 77:
78: #
1.39 www 79: # Cache for stores of an individual user
80: #
81:
82: my $cachedassess;
83: my %cachedstores;
84:
85: #
1.11 www 86: # These cache hashes need to be independent of user, resource and course
1.27 www 87: # (user and course can/should be in the keys)
1.11 www 88: #
1.33 www 89:
90: my %spreadsheets;
91: my %courserdatas;
92: my %userrdatas;
93: my %defaultsheets;
1.35 www 94: my %updatedata;
1.27 www 95:
1.11 www 96: #
97: # These global hashes are dependent on user, course and resource,
98: # and need to be initialized every time when a sheet is calculated
99: #
100: my %courseopt;
101: my %useropt;
102: my %parmhash;
103:
1.95 www 104: #
105: # Some hashes for stats on timing and performance
106: #
107:
108: my %starttimes;
109: my %usedtimes;
1.96 www 110: my %numbertimes;
1.95 www 111:
1.28 www 112: # Stuff that only the screen handler can know
113:
114: my $includedir;
115: my $tmpdir;
116:
1.5 www 117: # =============================================================================
118: # ===================================== Implements an instance of a spreadsheet
1.4 www 119:
1.118 ! matthew 120: ##
! 121: ## mask - used to reside in the safe space.
! 122: ##
1.1 www 123: sub mask {
124: my ($lower,$upper)=@_;
125:
1.7 www 126: $lower=~/([A-Za-z]|\*)(\d+|\*)/;
1.1 www 127: my $la=$1;
128: my $ld=$2;
129:
1.7 www 130: $upper=~/([A-Za-z]|\*)(\d+|\*)/;
1.1 www 131: my $ua=$1;
132: my $ud=$2;
133: my $alpha='';
134: my $num='';
135:
136: if (($la eq '*') || ($ua eq '*')) {
1.7 www 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.7 www 191: if ($lda[$#lda]!=$uda[$#uda]) {
192: $num.='['.$lda[$#lda].'-'.$uda[$#uda].']';
193: }
1.1 www 194: }
195: }
196: }
1.4 www 197: return '^'.$alpha.$num."\$";
1.80 matthew 198: }
199:
1.118 ! matthew 200:
! 201:
! 202: sub initsheet {
! 203: my $safeeval = new Safe(shift);
! 204: my $safehole = new Safe::Hole;
! 205: $safeeval->permit("entereval");
! 206: $safeeval->permit(":base_math");
! 207: $safeeval->permit("sort");
! 208: $safeeval->deny(":base_io");
! 209: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
! 210: $safehole->wrap(\&Apache::lonspreadsheet::mask,$safeeval,'&mask');
! 211: $safehole->wrap(\&Apache::lonspreadsheet::templaterow,$safeeval,'&templaterow');
! 212: $safeeval->share('$@');
! 213: my $code=<<'ENDDEFS';
! 214: # ---------------------------------------------------- Inside of the safe space
! 215:
! 216: #
! 217: # f: formulas
! 218: # t: intermediate format (variable references expanded)
! 219: # v: output values
! 220: # c: preloaded constants (A-column)
! 221: # rl: row label
! 222: # os: other spreadsheets (for student spreadsheet only)
! 223:
! 224: undef %sheet_values;
! 225: undef %t;
! 226: undef %f;
! 227: undef %c;
! 228: undef %rowlabel;
! 229: undef @os;
! 230:
! 231: $maxrow = 0;
! 232: $sheettype = '';
! 233:
! 234: # filename/reference of the sheet
! 235: $filename = '';
! 236:
! 237: # user data
! 238: $uname = '';
! 239: $uhome = '';
! 240: $udom = '';
! 241:
! 242: # course data
! 243:
! 244: $csec = '';
! 245: $chome= '';
! 246: $cnum = '';
! 247: $cdom = '';
! 248: $cid = '';
! 249: $coursefilename = '';
! 250:
! 251: # symb
! 252:
! 253: $usymb = '';
! 254:
! 255: # error messages
! 256: $errormsg = '';
! 257:
! 258:
1.80 matthew 259: #-------------------------------------------------------
260:
261: =item UWCALC(hashname,modules,units,date)
262:
263: returns the proportion of the module
264: weights not previously completed by the student.
265:
266: =over 4
267:
268: =item hashname
269:
270: name of the hash the module dates have been inserted into
271:
272: =item modules
273:
274: reference to a cell which contains a comma deliminated list of modules
275: covered by the assignment.
276:
277: =item units
278:
279: reference to a cell which contains a comma deliminated list of module
280: weights with respect to the assignment
281:
282: =item date
283:
284: reference to a cell which contains the date the assignment was completed.
285:
286: =back
287:
288: =cut
289:
290: #-------------------------------------------------------
291: sub UWCALC {
292: my ($hashname,$modules,$units,$date) = @_;
293: my @Modules = split(/,/,$modules);
294: my @Units = split(/,/,$units);
295: my $total_weight;
296: foreach (@Units) {
297: $total_weight += $_;
298: }
299: my $usum=0;
300: for (my $i=0; $i<=$#Modules; $i++) {
301: if (&HASH($hashname,$Modules[$i]) eq $date) {
302: $usum += $Units[$i];
303: }
304: }
305: return $usum/$total_weight;
306: }
307:
308: #-------------------------------------------------------
309:
310: =item CDLSUM(list)
311:
312: returns the sum of the elements in a cell which contains
313: a Comma Deliminate List of numerical values.
314: 'list' is a reference to a cell which contains a comma deliminated list.
315:
316: =cut
317:
318: #-------------------------------------------------------
319: sub CDLSUM {
320: my ($list)=@_;
321: my $sum;
322: foreach (split/,/,$list) {
323: $sum += $_;
324: }
325: return $sum;
326: }
327:
328: #-------------------------------------------------------
329:
330: =item CDLITEM(list,index)
331:
332: returns the item at 'index' in a Comma Deliminated List.
333:
334: =over 4
335:
336: =item list
337:
338: reference to a cell which contains a comma deliminated list.
339:
340: =item index
341:
342: the Perl index of the item requested (first element in list has
343: an index of 0)
344:
345: =back
346:
347: =cut
348:
349: #-------------------------------------------------------
350: sub CDLITEM {
351: my ($list,$index)=@_;
352: my @Temp = split/,/,$list;
353: return $Temp[$index];
354: }
355:
356: #-------------------------------------------------------
357:
358: =item CDLHASH(name,key,value)
359:
360: loads a comma deliminated list of keys into
361: the hash 'name', all with a value of 'value'.
362:
363: =over 4
364:
365: =item name
366:
367: name of the hash.
368:
369: =item key
370:
371: (a pointer to) a comma deliminated list of keys.
372:
373: =item value
374:
375: a single value to be entered for each key.
376:
377: =back
378:
379: =cut
380:
381: #-------------------------------------------------------
382: sub CDLHASH {
383: my ($name,$key,$value)=@_;
384: my @Keys;
385: my @Values;
386: # Check to see if we have multiple $key values
387: if ($key =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
388: my $keymask = &mask($key);
389: # Assume the keys are addresses
1.104 matthew 390: my @Temp = grep /$keymask/,keys(%sheet_values);
391: @Keys = $sheet_values{@Temp};
1.80 matthew 392: } else {
393: $Keys[0]= $key;
394: }
395: my @Temp;
396: foreach $key (@Keys) {
397: @Temp = (@Temp, split/,/,$key);
398: }
399: @Keys = @Temp;
400: if ($value =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
401: my $valmask = &mask($value);
1.104 matthew 402: my @Temp = grep /$valmask/,keys(%sheet_values);
403: @Values =$sheet_values{@Temp};
1.80 matthew 404: } else {
405: $Values[0]= $value;
406: }
407: $value = $Values[0];
408: # Add values to hash
409: for (my $i = 0; $i<=$#Keys; $i++) {
410: my $key = $Keys[$i];
411: if (! exists ($hashes{$name}->{$key})) {
412: $hashes{$name}->{$key}->[0]=$value;
413: } else {
414: my @Temp = sort(@{$hashes{$name}->{$key}},$value);
415: $hashes{$name}->{$key} = \@Temp;
416: }
417: }
418: return "hash '$name' updated";
419: }
420:
421: #-------------------------------------------------------
422:
423: =item GETHASH(name,key,index)
424:
425: returns the element in hash 'name'
426: reference by the key 'key', at index 'index' in the values list.
427:
428: =cut
429:
430: #-------------------------------------------------------
431: sub GETHASH {
432: my ($name,$key,$index)=@_;
433: if (! defined($index)) {
434: $index = 0;
435: }
436: if ($key =~ /^[A-z]\d+$/) {
1.104 matthew 437: $key = $sheet_values{$key};
1.80 matthew 438: }
439: return $hashes{$name}->{$key}->[$index];
440: }
441:
442: #-------------------------------------------------------
443:
444: =item CLEARHASH(name)
445:
446: clears all the values from the hash 'name'
447:
448: =item CLEARHASH(name,key)
449:
450: clears all the values from the hash 'name' associated with the given key.
451:
452: =cut
453:
454: #-------------------------------------------------------
455: sub CLEARHASH {
456: my ($name,$key)=@_;
457: if (defined($key)) {
458: if (exists($hashes{$name}->{$key})) {
459: $hashes{$name}->{$key}=undef;
460: return "hash '$name' key '$key' cleared";
461: }
462: } else {
463: if (exists($hashes{$name})) {
464: $hashes{$name}=undef;
465: return "hash '$name' cleared";
466: }
467: }
468: return "Error in clearing hash";
469: }
470:
471: #-------------------------------------------------------
472:
473: =item HASH(name,key,value)
474:
475: loads values into an internal hash. If a key
476: already has a value associated with it, the values are sorted numerically.
477:
478: =item HASH(name,key)
479:
480: returns the 0th value in the hash 'name' associated with 'key'.
481:
482: =cut
483:
484: #-------------------------------------------------------
485: sub HASH {
486: my ($name,$key,$value)=@_;
487: my @Keys;
488: undef @Keys;
489: my @Values;
490: # Check to see if we have multiple $key values
491: if ($key =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
492: my $keymask = &mask($key);
493: # Assume the keys are addresses
1.104 matthew 494: my @Temp = grep /$keymask/,keys(%sheet_values);
495: @Keys = $sheet_values{@Temp};
1.80 matthew 496: } else {
497: $Keys[0]= $key;
498: }
499: # If $value is empty, return the first value associated
500: # with the first key.
501: if (! $value) {
502: return $hashes{$name}->{$Keys[0]}->[0];
503: }
504: # Check to see if we have multiple $value(s)
505: if ($value =~ /[A-z](\-[A-z])?\d+(\-\d+)?/) {
506: my $valmask = &mask($value);
1.104 matthew 507: my @Temp = grep /$valmask/,keys(%sheet_values);
508: @Values =$sheet_values{@Temp};
1.80 matthew 509: } else {
510: $Values[0]= $value;
511: }
512: # Add values to hash
513: for (my $i = 0; $i<=$#Keys; $i++) {
514: my $key = $Keys[$i];
515: my $value = ($i<=$#Values ? $Values[$i] : $Values[0]);
516: if (! exists ($hashes{$name}->{$key})) {
517: $hashes{$name}->{$key}->[0]=$value;
518: } else {
519: my @Temp = sort(@{$hashes{$name}->{$key}},$value);
520: $hashes{$name}->{$key} = \@Temp;
521: }
522: }
523: return $Values[-1];
1.1 www 524: }
525:
1.84 matthew 526: #-------------------------------------------------------
527:
528: =item NUM(range)
529:
530: returns the number of items in the range.
531:
532: =cut
533:
534: #-------------------------------------------------------
1.1 www 535: sub NUM {
536: my $mask=mask(@_);
1.104 matthew 537: my $num= $#{@{grep(/$mask/,keys(%sheet_values))}}+1;
1.1 www 538: return $num;
539: }
540:
541: sub BIN {
542: my ($low,$high,$lower,$upper)=@_;
543: my $mask=mask($lower,$upper);
544: my $num=0;
1.104 matthew 545: foreach (grep /$mask/,keys(%sheet_values)) {
546: if (($sheet_values{$_}>=$low) && ($sheet_values{$_}<=$high)) {
1.1 www 547: $num++;
548: }
1.78 matthew 549: }
1.1 www 550: return $num;
551: }
552:
553:
1.84 matthew 554: #-------------------------------------------------------
555:
556: =item SUM(range)
557:
558: returns the sum of items in the range.
559:
560: =cut
561:
562: #-------------------------------------------------------
1.1 www 563: sub SUM {
564: my $mask=mask(@_);
565: my $sum=0;
1.104 matthew 566: foreach (grep /$mask/,keys(%sheet_values)) {
567: $sum+=$sheet_values{$_};
1.78 matthew 568: }
1.1 www 569: return $sum;
570: }
571:
1.84 matthew 572: #-------------------------------------------------------
573:
574: =item MEAN(range)
575:
576: compute the average of the items in the range.
577:
578: =cut
579:
580: #-------------------------------------------------------
1.1 www 581: sub MEAN {
582: my $mask=mask(@_);
583: my $sum=0; my $num=0;
1.104 matthew 584: foreach (grep /$mask/,keys(%sheet_values)) {
585: $sum+=$sheet_values{$_};
1.1 www 586: $num++;
1.78 matthew 587: }
1.1 www 588: if ($num) {
589: return $sum/$num;
590: } else {
591: return undef;
592: }
593: }
594:
1.84 matthew 595: #-------------------------------------------------------
596:
597: =item STDDEV(range)
598:
599: compute the standard deviation of the items in the range.
600:
601: =cut
602:
603: #-------------------------------------------------------
1.1 www 604: sub STDDEV {
605: my $mask=mask(@_);
606: my $sum=0; my $num=0;
1.104 matthew 607: foreach (grep /$mask/,keys(%sheet_values)) {
608: $sum+=$sheet_values{$_};
1.1 www 609: $num++;
1.78 matthew 610: }
1.1 www 611: unless ($num>1) { return undef; }
612: my $mean=$sum/$num;
613: $sum=0;
1.104 matthew 614: foreach (grep /$mask/,keys(%sheet_values)) {
615: $sum+=($sheet_values{$_}-$mean)**2;
1.78 matthew 616: }
1.1 www 617: return sqrt($sum/($num-1));
618: }
619:
1.84 matthew 620: #-------------------------------------------------------
621:
622: =item PROD(range)
623:
624: compute the product of the items in the range.
625:
626: =cut
627:
628: #-------------------------------------------------------
1.1 www 629: sub PROD {
630: my $mask=mask(@_);
631: my $prod=1;
1.104 matthew 632: foreach (grep /$mask/,keys(%sheet_values)) {
633: $prod*=$sheet_values{$_};
1.78 matthew 634: }
1.1 www 635: return $prod;
636: }
637:
1.84 matthew 638: #-------------------------------------------------------
639:
640: =item MAX(range)
641:
642: compute the maximum of the items in the range.
643:
644: =cut
645:
646: #-------------------------------------------------------
1.1 www 647: sub MAX {
648: my $mask=mask(@_);
649: my $max='-';
1.104 matthew 650: foreach (grep /$mask/,keys(%sheet_values)) {
651: unless ($max) { $max=$sheet_values{$_}; }
652: if (($sheet_values{$_}>$max) || ($max eq '-')) { $max=$sheet_values{$_}; }
1.78 matthew 653: }
1.1 www 654: return $max;
655: }
656:
1.84 matthew 657: #-------------------------------------------------------
658:
659: =item MIN(range)
660:
661: compute the minimum of the items in the range.
662:
663: =cut
664:
665: #-------------------------------------------------------
1.1 www 666: sub MIN {
667: my $mask=mask(@_);
668: my $min='-';
1.104 matthew 669: foreach (grep /$mask/,keys(%sheet_values)) {
670: unless ($max) { $max=$sheet_values{$_}; }
671: if (($sheet_values{$_}<$min) || ($min eq '-')) {
672: $min=$sheet_values{$_};
673: }
1.78 matthew 674: }
1.1 www 675: return $min;
676: }
677:
1.84 matthew 678: #-------------------------------------------------------
679:
680: =item SUMMAX(num,lower,upper)
681:
682: compute the sum of the largest 'num' items in the range from
683: 'lower' to 'upper'
684:
685: =cut
686:
687: #-------------------------------------------------------
1.1 www 688: sub SUMMAX {
689: my ($num,$lower,$upper)=@_;
690: my $mask=mask($lower,$upper);
691: my @inside=();
1.104 matthew 692: foreach (grep /$mask/,keys(%sheet_values)) {
693: push (@inside,$sheet_values{$_});
1.78 matthew 694: }
1.1 www 695: @inside=sort(@inside);
696: my $sum=0; my $i;
697: for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) {
698: $sum+=$inside[$i];
699: }
700: return $sum;
701: }
702:
1.84 matthew 703: #-------------------------------------------------------
704:
705: =item SUMMIN(num,lower,upper)
706:
707: compute the sum of the smallest 'num' items in the range from
708: 'lower' to 'upper'
709:
710: =cut
711:
712: #-------------------------------------------------------
1.1 www 713: sub SUMMIN {
714: my ($num,$lower,$upper)=@_;
715: my $mask=mask($lower,$upper);
716: my @inside=();
1.104 matthew 717: foreach (grep /$mask/,keys(%sheet_values)) {
718: $inside[$#inside+1]=$sheet_values{$_};
1.78 matthew 719: }
1.1 www 720: @inside=sort(@inside);
721: my $sum=0; my $i;
722: for ($i=0;(($i<$num) && ($i<=$#inside));$i++) {
723: $sum+=$inside[$i];
724: }
725: return $sum;
726: }
727:
1.103 matthew 728: #-------------------------------------------------------
729:
730: =item MINPARM(parametername)
731:
732: Returns the minimum value of the parameters matching the parametername.
733: parametername should be a string such as 'duedate'.
734:
735: =cut
736:
737: #-------------------------------------------------------
738: sub MINPARM {
739: my ($expression) = @_;
740: my $min = undef;
741: study($expression);
742: foreach $parameter (keys(%c)) {
743: next if ($parameter !~ /$expression/);
744: if ((! defined($min)) || ($min > $c{$parameter})) {
745: $min = $c{$parameter}
746: }
747: }
748: return $min;
749: }
750:
751: #-------------------------------------------------------
752:
753: =item MAXPARM(parametername)
754:
755: Returns the maximum value of the parameters matching the input parameter name.
756: parametername should be a string such as 'duedate'.
757:
758: =cut
759:
760: #-------------------------------------------------------
761: sub MAXPARM {
762: my ($expression) = @_;
763: my $max = undef;
764: study($expression);
765: foreach $parameter (keys(%c)) {
766: next if ($parameter !~ /$expression/);
767: if ((! defined($min)) || ($max < $c{$parameter})) {
768: $max = $c{$parameter}
769: }
770: }
771: return $max;
772: }
773:
774: #--------------------------------------------------------
1.59 www 775: sub expandnamed {
776: my $expression=shift;
777: if ($expression=~/^\&/) {
778: my ($func,$var,$formula)=($expression=~/^\&(\w+)\(([^\;]+)\;(.*)\)/);
779: my @vars=split(/\W+/,$formula);
780: my %values=();
781: undef %values;
1.78 matthew 782: foreach ( @vars ) {
1.59 www 783: my $varname=$_;
784: if ($varname=~/\D/) {
785: $formula=~s/$varname/'$c{\''.$varname.'\'}'/ge;
786: $varname=~s/$var/\(\\w\+\)/g;
1.78 matthew 787: foreach (keys(%c)) {
1.59 www 788: if ($_=~/$varname/) {
789: $values{$1}=1;
790: }
1.78 matthew 791: }
1.59 www 792: }
1.78 matthew 793: }
1.59 www 794: if ($func eq 'EXPANDSUM') {
795: my $result='';
1.78 matthew 796: foreach (keys(%values)) {
1.59 www 797: my $thissum=$formula;
798: $thissum=~s/$var/$_/g;
799: $result.=$thissum.'+';
1.78 matthew 800: }
1.59 www 801: $result=~s/\+$//;
802: return $result;
803: } else {
804: return 0;
805: }
806: } else {
1.88 matthew 807: # it is not a function, so it is a parameter name
808: # We should do the following:
809: # 1. Take the list of parameter names
810: # 2. look through the list for ones that match the parameter we want
811: # 3. If there are no collisions, return the one that matches
812: # 4. If there is a collision, return 'bad parameter name error'
813: my $returnvalue = '';
814: my @matches = ();
815: $#matches = -1;
816: study $expression;
817: foreach $parameter (keys(%c)) {
818: push @matches,$parameter if ($parameter =~ /$expression/);
819: }
820: if ($#matches == 0) {
821: $returnvalue = '$c{\''.$matches[0].'\'}';
1.100 matthew 822: } elsif ($#matches > 0) {
823: # more than one match. Look for a concise one
824: $returnvalue = "'non-unique parameter name : $expression'";
825: foreach (@matches) {
826: if (/^$expression$/) {
827: $returnvalue = '$c{\''.$_.'\'}';
828: }
829: }
1.88 matthew 830: } else {
831: $returnvalue = "'bad parameter name : $expression'";
832: }
833: return $returnvalue;
1.59 www 834: }
835: }
836:
1.1 www 837: sub sett {
838: %t=();
1.16 www 839: my $pattern='';
840: if ($sheettype eq 'assesscalc') {
841: $pattern='A';
842: } else {
843: $pattern='[A-Z]';
844: }
1.104 matthew 845: # Deal with the template row
1.78 matthew 846: foreach (keys(%f)) {
1.104 matthew 847: next if ($_!~/template\_(\w)/);
848: my $col=$1;
849: next if ($col=~/^$pattern/);
850: foreach (keys(%f)) {
851: next if ($_!~/A(\d+)/);
852: my $trow=$1;
853: next if (! $trow);
854: # Get the name of this cell
855: my $lb=$col.$trow;
856: # Grab the template declaration
857: $t{$lb}=$f{'template_'.$col};
858: # Replace '#' with the row number
859: $t{$lb}=~s/\#/$trow/g;
860: # Replace '....' with ','
861: $t{$lb}=~s/\.\.+/\,/g;
862: # Replace 'A0' with the value from 'A0'
863: $t{$lb}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
864: # Replace parameters
865: $t{$lb}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
866: }
1.78 matthew 867: }
1.104 matthew 868: # Deal with the normal cells
1.78 matthew 869: foreach (keys(%f)) {
1.112 matthew 870: if (exists($f{$_}) && ($_!~/template\_/)) {
1.42 www 871: my $matches=($_=~/^$pattern(\d+)/);
872: if (($matches) && ($1)) {
1.6 www 873: unless ($f{$_}=~/^\!/) {
874: $t{$_}=$c{$_};
875: }
876: } else {
877: $t{$_}=$f{$_};
1.7 www 878: $t{$_}=~s/\.\.+/\,/g;
1.104 matthew 879: $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
1.59 www 880: $t{$_}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
1.6 www 881: }
1.1 www 882: }
1.78 matthew 883: }
1.104 matthew 884: # For inserted lines, [B-Z] is also valid
1.97 www 885: unless ($sheettype eq 'assesscalc') {
886: foreach (keys(%f)) {
887: if ($_=~/[B-Z](\d+)/) {
888: if ($f{'A'.$1}=~/^[\~\-]/) {
889: $t{$_}=$f{$_};
890: $t{$_}=~s/\.\.+/\,/g;
1.104 matthew 891: $t{$_}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
1.97 www 892: $t{$_}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
893: }
894: }
895: }
896: }
1.88 matthew 897: # For some reason 'A0' gets special treatment... This seems superfluous
898: # but I imagine it is here for a reason.
1.17 www 899: $t{'A0'}=$f{'A0'};
900: $t{'A0'}=~s/\.\.+/\,/g;
1.104 matthew 901: $t{'A0'}=~s/(^|[^\"\'])([A-Za-z]\d+)/$1\$sheet_values\{\'$2\'\}/g;
1.59 www 902: $t{'A0'}=~s/(^|[^\"\'])\[([^\]]+)\]/$1.&expandnamed($2)/ge;
1.1 www 903: }
904:
1.4 www 905: sub calc {
1.104 matthew 906: undef %sheet_values;
1.4 www 907: &sett();
1.1 www 908: my $notfinished=1;
1.97 www 909: my $lastcalc='';
1.1 www 910: my $depth=0;
911: while ($notfinished) {
912: $notfinished=0;
1.78 matthew 913: foreach (keys(%t)) {
1.104 matthew 914: my $old=$sheet_values{$_};
915: $sheet_values{$_}=eval $t{$_};
1.1 www 916: if ($@) {
1.104 matthew 917: undef %sheet_values;
1.96 www 918: return $_.': '.$@;
1.1 www 919: }
1.104 matthew 920: if ($sheet_values{$_} ne $old) { $notfinished=1; $lastcalc=$_; }
1.78 matthew 921: }
1.1 www 922: $depth++;
923: if ($depth>100) {
1.104 matthew 924: undef %sheet_values;
1.97 www 925: return $lastcalc.': Maximum calculation depth exceeded';
1.1 www 926: }
927: }
1.96 www 928: return '';
1.1 www 929: }
930:
1.104 matthew 931: #
932: # This is actually used for the student spreadsheet, not the assessment sheet
933: # Do not be fooled by the name!
934: #
1.16 www 935: sub outrowassess {
1.104 matthew 936: # $n is the current row number
1.6 www 937: my $n=shift;
938: my @cols=();
939: if ($n) {
1.104 matthew 940: my ($usy,$ufn)=split(/__&&&\__/,$f{'A'.$n});
941: if ($rowlabel{$usy}) {
942: $cols[0]=$rowlabel{$usy}.'<br>'.
943: '<select name="sel_'.$n.'" onChange="changesheet('.$n.')">'.
944: '<option name="default">Default</option>';
945: } else {
946: $cols[0]='';
947: }
948: foreach (@os) {
949: $cols[0].='<option name="'.$_.'"';
1.55 www 950: if ($ufn eq $_) {
1.104 matthew 951: $cols[0].=' selected';
1.55 www 952: }
953: $cols[0].='>'.$_.'</option>';
1.104 matthew 954: }
955: $cols[0].='</select>';
1.6 www 956: } else {
1.104 matthew 957: $cols[0]='<b><font size=+1>Export</font></b>';
1.6 www 958: }
1.78 matthew 959: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
960: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
961: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
962: 'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1.6 www 963: my $fm=$f{$_.$n};
964: $fm=~s/[\'\"]/\&\#34;/g;
1.104 matthew 965: push(@cols,"'$_$n','$fm'".'___eq___'.$sheet_values{$_.$n});
1.78 matthew 966: }
1.6 www 967: return @cols;
968: }
969:
1.18 www 970: sub outrow {
971: my $n=shift;
972: my @cols=();
973: if ($n) {
1.102 matthew 974: $cols[0]=$rowlabel{$f{'A'.$n}};
1.18 www 975: } else {
976: $cols[0]='<b><font size=+1>Export</font></b>';
977: }
1.78 matthew 978: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
979: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
980: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
981: 'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1.18 www 982: my $fm=$f{$_.$n};
983: $fm=~s/[\'\"]/\&\#34;/g;
1.104 matthew 984: push(@cols,"'$_$n','$fm'".'___eq___'.$sheet_values{$_.$n});
1.78 matthew 985: }
1.18 www 986: return @cols;
987: }
988:
1.14 www 989: sub exportrowa {
1.28 www 990: my @exportarray=();
1.78 matthew 991: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
992: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1.104 matthew 993: push(@exportarray,$sheet_values{$_.'0'});
1.78 matthew 994: }
1.28 www 995: return @exportarray;
1.14 www 996: }
997:
1.118 ! matthew 998: sub templaterow {
! 999: my @cols=();
! 1000: $cols[0]='<b><font size=+1>Template</font></b>';
! 1001: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
! 1002: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
! 1003: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
! 1004: 'n','o','p','q','r','s','t','u','v','w','x','y','z') {
! 1005: my $fm=$f{'template_'.$_};
! 1006: $fm=~s/[\'\"]/\&\#34;/g;
! 1007: push(@cols,"'template_$_','$fm'".'___eq___'.$fm);
! 1008: }
! 1009: return @cols;
! 1010: }
! 1011:
! 1012:
1.4 www 1013: # ------------------------------------------- End of "Inside of the safe space"
1014: ENDDEFS
1015: $safeeval->reval($code);
1016: return $safeeval;
1017: }
1018:
1.118 ! matthew 1019:
1.4 www 1020: # ------------------------------------------------ Add or change formula values
1021: sub setformulas {
1.34 www 1022: my ($safeeval,%f)=@_;
1023: %{$safeeval->varglob('f')}=%f;
1.6 www 1024: }
1025:
1026: # ------------------------------------------------ Add or change formula values
1027: sub setconstants {
1.34 www 1028: my ($safeeval,%c)=@_;
1029: %{$safeeval->varglob('c')}=%c;
1.6 www 1030: }
1031:
1.55 www 1032: # --------------------------------------------- Set names of other spreadsheets
1033: sub setothersheets {
1034: my ($safeeval,@os)=@_;
1035: @{$safeeval->varglob('os')}=@os;
1036: }
1037:
1.6 www 1038: # ------------------------------------------------ Add or change formula values
1039: sub setrowlabels {
1.102 matthew 1040: my ($safeeval,%rowlabel)=@_;
1041: %{$safeeval->varglob('rowlabel')}=%rowlabel;
1.4 www 1042: }
1043:
1044: # ------------------------------------------------------- Calculate spreadsheet
1045: sub calcsheet {
1046: my $safeeval=shift;
1.95 www 1047: return $safeeval->reval('&calc();');
1.4 www 1048: }
1049:
1050: # ---------------------------------------------------------------- Get formulas
1051: sub getformulas {
1052: my $safeeval=shift;
1.34 www 1053: return %{$safeeval->varglob('f')};
1.4 www 1054: }
1055:
1.97 www 1056: # ----------------------------------------------------- Get value of $f{'A'.$n}
1057: sub getfa {
1058: my ($safeeval,$n)=@_;
1059: return $safeeval->reval('$f{"A'.$n.'"}');
1060: }
1061:
1.14 www 1062: # ------------------------------------------------------------- Export of A-row
1.28 www 1063: sub exportdata {
1.14 www 1064: my $safeeval=shift;
1065: return $safeeval->reval('&exportrowa()');
1066: }
1067:
1.55 www 1068:
1.5 www 1069: # ========================================================== End of Spreadsheet
1070: # =============================================================================
1071:
1.27 www 1072: #
1073: # Procedures for screen output
1074: #
1.6 www 1075: # --------------------------------------------- Produce output row n from sheet
1076:
1077: sub rown {
1.116 matthew 1078: my ($safeeval,$n,$sheetdata)=@_;
1.21 www 1079: my $defaultbg;
1.24 www 1080: my $rowdata='';
1.61 www 1081: my $dataflag=0;
1.21 www 1082: unless ($n eq '-') {
1.106 matthew 1083: $defaultbg=((($n-1)/5)==int(($n-1)/5))?'#E0E0':'#FFFF';
1.21 www 1084: } else {
1.106 matthew 1085: $defaultbg='#E0FF';
1.21 www 1086: }
1.71 www 1087: unless ($ENV{'form.showcsv'}) {
1.106 matthew 1088: $rowdata.="\n<tr><td><b><font size=+1>$n</font></b></td>";
1.71 www 1089: } else {
1.106 matthew 1090: $rowdata.="\n".'"'.$n.'"';
1.71 www 1091: }
1.6 www 1092: my $showf=0;
1.16 www 1093: my $proc;
1.97 www 1094: my $maxred=1;
1.116 matthew 1095: my $sheettype=$sheetdata->{'sheettype'};
1.62 www 1096: if ($sheettype eq 'studentcalc') {
1.55 www 1097: $proc='&outrowassess';
1098: $maxred=26;
1099: } else {
1100: $proc='&outrow';
1101: }
1.62 www 1102: if ($sheettype eq 'assesscalc') {
1.18 www 1103: $maxred=1;
1.16 www 1104: } else {
1.18 www 1105: $maxred=26;
1.16 www 1106: }
1.97 www 1107: if (&getfa($safeeval,$n)=~/^[\~\-]/) { $maxred=1; }
1.104 matthew 1108: if ($n eq '-') {
1109: $proc='&templaterow';
1110: $n=-1;
1111: $dataflag=1;
1112: }
1.78 matthew 1113: foreach ($safeeval->reval($proc.'('.$n.')')) {
1.106 matthew 1114: my $bgcolor=$defaultbg.((($showf-1)/5==int(($showf-1)/5))?'99':'DD');
1115: my ($fm,$vl)=split(/\_\_\_eq\_\_\_/,$_);
1116: if ((($vl ne '') || ($vl eq '0')) &&
1117: (($showf==1) || ($sheettype ne 'studentcalc'))) { $dataflag=1; }
1118: if ($showf==0) { $vl=$_; }
1119: unless ($ENV{'form.showcsv'}) {
1120: if ($showf<=$maxred) { $bgcolor='#FFDDDD'; }
1121: if (($n==0) && ($showf<=26)) { $bgcolor='#CCCCFF'; }
1122: if (($showf>$maxred) || ((!$n) && ($showf>0))) {
1123: if ($vl eq '') {
1124: $vl='<font size=+2 color='.$bgcolor.'>#</font>';
1125: }
1.111 matthew 1126: $rowdata.='<td bgcolor='.$bgcolor.'>';
1127: if ($ENV{'request.role'} =~ /^st\./) {
1128: $rowdata.=$vl;
1129: } else {
1130: $rowdata.='<a href="javascript:celledit('.$fm.');">'.
1131: $vl.'</a>';
1132: }
1133: $rowdata.='</td>';
1.106 matthew 1134: } else {
1135: $rowdata.='<td bgcolor='.$bgcolor.'> '.$vl.' </td>';
1136: }
1137: } else {
1138: $rowdata.=',"'.$vl.'"';
1139: }
1140: $showf++;
1.78 matthew 1141: } # End of foreach($safeval...)
1.61 www 1142: if ($ENV{'form.showall'} || ($dataflag)) {
1.106 matthew 1143: return $rowdata.($ENV{'form.showcsv'}?'':'</tr>');
1.61 www 1144: } else {
1.106 matthew 1145: return '';
1.61 www 1146: }
1.6 www 1147: }
1148:
1149: # ------------------------------------------------------------- Print out sheet
1150:
1151: sub outsheet {
1.106 matthew 1152: my ($r,$safeeval,$sheetdata)=@_;
1153: my $maxred = 26; # The maximum number of cells to show as
1154: # red (uneditable)
1155: # To make student sheets uneditable could we
1156: # set $maxred = 52?
1157: #
1158: my $realm='Course'; # 'assessment', 'user', or 'course' sheet
1159: if ($sheetdata->{'sheettype'} eq 'assesscalc') {
1.18 www 1160: $maxred=1;
1161: $realm='Assessment';
1.106 matthew 1162: } elsif ($sheetdata->{'sheettype'} eq 'studentcalc') {
1.18 www 1163: $maxred=26;
1164: $realm='User';
1165: }
1.106 matthew 1166: #
1167: # Column label
1.71 www 1168: my $tabledata;
1.106 matthew 1169: if ($ENV{'form.showcsv'}) {
1170: $tabledata='<pre>';
1171: } else {
1172: $tabledata='<table border=2><tr><th colspan=2 rowspan=2>'.
1173: '<font size=+2>'.$realm.'</font></th>'.
1.18 www 1174: '<td bgcolor=#FFDDDD colspan='.$maxred.
1175: '><b><font size=+1>Import</font></b></td>'.
1.106 matthew 1176: '<td colspan='.(52-$maxred).
1.18 www 1177: '><b><font size=+1>Calculations</font></b></td></tr><tr>';
1.106 matthew 1178: my $showf=0;
1179: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1180: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
1181: 'a','b','c','d','e','f','g','h','i','j','k','l','m',
1182: 'n','o','p','q','r','s','t','u','v','w','x','y','z') {
1183: $showf++;
1184: if ($showf<=$maxred) {
1185: $tabledata.='<td bgcolor="#FFDDDD">';
1186: } else {
1187: $tabledata.='<td>';
1188: }
1189: $tabledata.="<b><font size=+1>$_</font></b></td>";
1190: }
1.116 matthew 1191: $tabledata.='</tr>'.&rown($safeeval,'-',$sheetdata).
1192: &rown($safeeval,0,$sheetdata);
1.106 matthew 1193: }
1.71 www 1194: $r->print($tabledata);
1.106 matthew 1195: #
1196: # Prepare to output rows
1.6 www 1197: my $row;
1.116 matthew 1198: my $maxrow=$sheetdata->{'maxrow'};
1.106 matthew 1199: #
1.65 www 1200: my @sortby=();
1201: my @sortidx=();
1202: for ($row=1;$row<=$maxrow;$row++) {
1.106 matthew 1203: push (@sortby, $safeeval->reval('$f{"A'.$row.'"}'));
1204: push (@sortidx, $row-1);
1.65 www 1205: }
1.111 matthew 1206: @sortidx=sort { lc($sortby[$a]) cmp lc($sortby[$b]); } @sortidx;
1.106 matthew 1207: #
1208: # Determine the type of child spreadsheets
1209: my $what='Student';
1210: if ($sheetdata->{'sheettype'} eq 'assesscalc') {
1211: $what='Item';
1212: } elsif ($sheetdata->{'sheettype'} eq 'studentcalc') {
1213: $what='Assessment';
1214: }
1215: #
1216: # Loop through the rows and output them one at a time
1.65 www 1217: my $n=0;
1218: for ($row=0;$row<$maxrow;$row++) {
1.116 matthew 1219: my $thisrow=&rown($safeeval,$sortidx[$row]+1,$sheetdata);
1.102 matthew 1220: if ($thisrow) {
1221: if (($n/25==int($n/25)) && (!$ENV{'form.showcsv'})) {
1222: $r->print("</table>\n<br>\n");
1223: $r->rflush();
1224: $r->print('<table border=2><tr><td> <td>'.$what.'</td>');
1.106 matthew 1225: $r->print('<td>'.
1226: join('</td><td>',
1227: (split(//,'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
1228: 'abcdefghijklmnopqrstuvwxyz'))).
1.102 matthew 1229: "</td></tr>\n");
1230: }
1231: $n++;
1232: $r->print($thisrow);
1.78 matthew 1233: }
1.6 www 1234: }
1.71 www 1235: $r->print($ENV{'form.showcsv'}?'</pre>':'</table>');
1.6 www 1236: }
1237:
1.27 www 1238: #
1.55 www 1239: # ----------------------------------------------- Read list of available sheets
1240: #
1241: sub othersheets {
1.116 matthew 1242: my ($safeeval,$stype,$sheetdata)=@_;
1.81 matthew 1243: #
1.116 matthew 1244: my $cnum = $sheetdata->{'cnum'};
1245: my $cdom = $sheetdata->{'cdom'};
1246: my $chome = $sheetdata->{'chome'};
1.81 matthew 1247: #
1.55 www 1248: my @alternatives=();
1.81 matthew 1249: my %results=&Apache::lonnet::dump($stype.'_spreadsheets',$cdom,$cnum);
1250: my ($tmp) = keys(%results);
1251: unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1252: @alternatives = sort (keys(%results));
1253: }
1.55 www 1254: return @alternatives;
1255: }
1256:
1.82 matthew 1257:
1258: #
1259: # -------------------------------------- Parse a spreadsheet
1260: #
1261: sub parse_sheet {
1262: # $sheetxml is a scalar reference or a scalar
1263: my ($sheetxml) = @_;
1264: if (! ref($sheetxml)) {
1265: my $tmp = $sheetxml;
1266: $sheetxml = \$tmp;
1267: }
1268: my %f;
1269: my $parser=HTML::TokeParser->new($sheetxml);
1270: my $token;
1271: while ($token=$parser->get_token) {
1272: if ($token->[0] eq 'S') {
1273: if ($token->[1] eq 'field') {
1274: $f{$token->[2]->{'col'}.$token->[2]->{'row'}}=
1275: $parser->get_text('/field');
1276: }
1277: if ($token->[1] eq 'template') {
1278: $f{'template_'.$token->[2]->{'col'}}=
1279: $parser->get_text('/template');
1280: }
1281: }
1282: }
1283: return \%f;
1284: }
1285:
1.55 www 1286: #
1.27 www 1287: # -------------------------------------- Read spreadsheet formulas for a course
1288: #
1289: sub readsheet {
1.107 matthew 1290: my ($safeeval,$sheetdata,$fn)=@_;
1291: #
1292: my $stype = $sheetdata->{'sheettype'};
1293: my $cnum = $sheetdata->{'cnum'};
1294: my $cdom = $sheetdata->{'cdom'};
1295: my $chome = $sheetdata->{'chome'};
1296: #
1.104 matthew 1297: if (! defined($fn)) {
1298: # There is no filename. Look for defaults in course and global, cache
1299: unless ($fn=$defaultsheets{$cnum.'_'.$cdom.'_'.$stype}) {
1300: my %tmphash = &Apache::lonnet::get('environment',
1301: ['spreadsheet_default_'.$stype],
1302: $cdom,$cnum);
1303: my ($tmp) = keys(%tmphash);
1304: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1305: $fn = 'default_'.$stype;
1306: } else {
1307: $fn = $tmphash{'spreadsheet_default_'.$stype};
1308: }
1309: unless (($fn) && ($fn!~/^error\:/)) {
1310: $fn='default_'.$stype;
1311: }
1312: $defaultsheets{$cnum.'_'.$cdom.'_'.$stype}=$fn;
1313: }
1314: }
1315: # $fn now has a value
1.116 matthew 1316: $sheetdata->{'filename'} = $fn;
1.104 matthew 1317: # see if sheet is cached
1318: my $fstring='';
1319: if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
1320: &setformulas($safeeval,split(/\_\_\_\;\_\_\_/,$fstring));
1321: } else {
1322: # Not cached, need to read
1323: my %f=();
1324: if ($fn=~/^default\_/) {
1325: my $sheetxml='';
1326: my $fh;
1327: my $dfn=$fn;
1328: $dfn=~s/\_/\./g;
1329: if ($fh=Apache::File->new($includedir.'/'.$dfn)) {
1330: $sheetxml=join('',<$fh>);
1331: } else {
1332: $sheetxml='<field row="0" col="A">"Error"</field>';
1333: }
1334: %f=%{&parse_sheet(\$sheetxml)};
1335: } elsif($fn=~/\/*\.spreadsheet$/) {
1336: my $sheetxml=&Apache::lonnet::getfile
1337: (&Apache::lonnet::filelocation('',$fn));
1338: if ($sheetxml == -1) {
1339: $sheetxml='<field row="0" col="A">"Error loading spreadsheet '
1340: .$fn.'"</field>';
1341: }
1342: %f=%{&parse_sheet(\$sheetxml)};
1343: } else {
1344: my $sheet='';
1345: my %tmphash = &Apache::lonnet::dump($fn,$cdom,$cnum);
1346: my ($tmp) = keys(%tmphash);
1347: unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1348: foreach (keys(%tmphash)) {
1349: $f{$_}=$tmphash{$_};
1350: }
1351: }
1352: }
1353: # Cache and set
1354: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1355: &setformulas($safeeval,%f);
1.3 www 1356: }
1357: }
1358:
1.28 www 1359: # -------------------------------------------------------- Make new spreadsheet
1360: sub makenewsheet {
1361: my ($uname,$udom,$stype,$usymb)=@_;
1.105 matthew 1362: my %sheetdata=();
1363: $sheetdata{'uname'} = $uname;
1364: $sheetdata{'udom'} = $udom;
1365: $sheetdata{'sheettype'} = $stype;
1366: $sheetdata{'usymb'} = $usymb;
1367: $sheetdata{'cid'} = $ENV{'request.course.id'};
1.113 matthew 1368: $sheetdata{'csec'} = $Section{$uname.':'.$udom};
1.109 matthew 1369: $sheetdata{'coursefilename'} = $ENV{'request.course.fn'};
1.105 matthew 1370: $sheetdata{'cnum'} = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
1371: $sheetdata{'cdom'} = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1372: $sheetdata{'chome'} = $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1373: $sheetdata{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);
1374:
1.36 www 1375: my $safeeval=initsheet($stype);
1.116 matthew 1376: #
1377: # Place all the %sheetdata items into the safe space
1.105 matthew 1378: my $initstring = '';
1379: foreach (keys(%sheetdata)) {
1380: $initstring.= qq{\$$_="$sheetdata{$_}";};
1381: }
1382: $safeeval->reval($initstring);
1383: return $safeeval,\%sheetdata;
1.28 www 1384: }
1385:
1.19 www 1386: # ------------------------------------------------------------ Save spreadsheet
1387: sub writesheet {
1.116 matthew 1388: my ($safeeval,$makedef,$sheetdata)=@_;
1389: my $cid=$sheetdata->{'cid'};
1.104 matthew 1390: if (&Apache::lonnet::allowed('opa',$cid)) {
1391: my %f=&getformulas($safeeval);
1.116 matthew 1392: my $stype= $sheetdata->{'sheettype'};
1393: my $cnum = $sheetdata->{'cnum'};
1394: my $cdom = $sheetdata->{'cdom'};
1395: my $chome= $sheetdata->{'chome'};
1396: my $fn = $sheetdata->{'filename'};
1.104 matthew 1397: # Cache new sheet
1398: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1399: # Write sheet
1400: my $sheetdata='';
1401: foreach (keys(%f)) {
1402: unless ($f{$_} eq 'import') {
1403: $sheetdata.=&Apache::lonnet::escape($_).'='.
1404: &Apache::lonnet::escape($f{$_}).'&';
1405: }
1406: }
1407: $sheetdata=~s/\&$//;
1408: my $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.$fn.':'.
1409: $sheetdata,$chome);
1410: if ($reply eq 'ok') {
1411: $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.
1412: $stype.'_spreadsheets:'.
1413: &Apache::lonnet::escape($fn).
1414: '='.$ENV{'user.name'}.'@'.
1415: $ENV{'user.domain'},
1416: $chome);
1417: if ($reply eq 'ok') {
1418: if ($makedef) {
1419: return &Apache::lonnet::reply('put:'.$cdom.':'.$cnum.
1420: ':environment:'.
1421: 'spreadsheet_default_'.
1422: $stype.'='.
1423: &Apache::lonnet::escape($fn),
1424: $chome);
1425: }
1426: return $reply;
1427: }
1428: return $reply;
1429: }
1430: return $reply;
1431: }
1432: return 'unauthorized';
1.19 www 1433: }
1434:
1.10 www 1435: # ----------------------------------------------- Make a temp copy of the sheet
1.28 www 1436: # "Modified workcopy" - interactive only
1437: #
1.10 www 1438: sub tmpwrite {
1.116 matthew 1439: my ($safeeval,$sheetdata) = @_;
1.28 www 1440: my $fn=$ENV{'user.name'}.'_'.
1.116 matthew 1441: $ENV{'user.domain'}.'_spreadsheet_'.$sheetdata->{'usymb'}.'_'.
1442: $sheetdata->{'filename'};
1.10 www 1443: $fn=~s/\W/\_/g;
1444: $fn=$tmpdir.$fn.'.tmp';
1445: my $fh;
1446: if ($fh=Apache::File->new('>'.$fn)) {
1447: print $fh join("\n",&getformulas($safeeval));
1448: }
1449: }
1450:
1451: # ---------------------------------------------------------- Read the temp copy
1452: sub tmpread {
1.116 matthew 1453: my ($safeeval,$sheetdata,$nfield,$nform)=@_;
1.28 www 1454: my $fn=$ENV{'user.name'}.'_'.
1.116 matthew 1455: $ENV{'user.domain'}.'_spreadsheet_'.$sheetdata->{'usymb'}.'_'.
1456: $sheetdata->{'filename'};
1.10 www 1457: $fn=~s/\W/\_/g;
1458: $fn=$tmpdir.$fn.'.tmp';
1459: my $fh;
1460: my %fo=();
1.92 www 1461: my $countrows=0;
1.10 www 1462: if ($fh=Apache::File->new($fn)) {
1463: my $name;
1464: while ($name=<$fh>) {
1465: chomp($name);
1466: my $value=<$fh>;
1467: chomp($value);
1468: $fo{$name}=$value;
1.93 www 1469: if ($name=~/^A(\d+)$/) {
1470: if ($1>$countrows) {
1471: $countrows=$1;
1472: }
1473: }
1.10 www 1474: }
1475: }
1.55 www 1476: if ($nform eq 'changesheet') {
1.57 www 1477: $fo{'A'.$nfield}=(split(/\_\_\&\&\&\_\_/,$fo{'A'.$nfield}))[0];
1.55 www 1478: unless ($ENV{'form.sel_'.$nfield} eq 'Default') {
1.57 www 1479: $fo{'A'.$nfield}.='__&&&__'.$ENV{'form.sel_'.$nfield};
1.55 www 1480: }
1.92 www 1481: } elsif ($nfield eq 'insertrow') {
1.93 www 1482: $countrows++;
1.95 www 1483: my $newrow=substr('000000'.$countrows,-7);
1.92 www 1484: if ($nform eq 'top') {
1.94 www 1485: $fo{'A'.$countrows}='--- '.$newrow;
1.92 www 1486: } else {
1.94 www 1487: $fo{'A'.$countrows}='~~~ '.$newrow;
1.92 www 1488: }
1.55 www 1489: } else {
1490: if ($nfield) { $fo{$nfield}=$nform; }
1491: }
1.10 www 1492: &setformulas($safeeval,%fo);
1493: }
1494:
1.104 matthew 1495: ##################################################
1496: ##################################################
1.11 www 1497:
1.104 matthew 1498: =pod
1.11 www 1499:
1.104 matthew 1500: =item &parmval()
1.11 www 1501:
1.104 matthew 1502: Determine the value of a parameter.
1.11 www 1503:
1.104 matthew 1504: Inputs: $what, the parameter needed, $safeeval, the safe space
1.11 www 1505:
1.104 matthew 1506: Returns: The value of a parameter, or '' if none.
1.11 www 1507:
1.104 matthew 1508: This function cascades through the possible levels searching for a value for
1509: a parameter. The levels are checked in the following order:
1510: user, course (at section level and course level), map, and lonnet::metadata.
1511: This function uses %parmhash, which must be tied prior to calling it.
1512: This function also requires %courseopt and %useropt to be initialized for
1513: this user and course.
1.11 www 1514:
1.104 matthew 1515: =cut
1.11 www 1516:
1.104 matthew 1517: ##################################################
1518: ##################################################
1519: sub parmval {
1.105 matthew 1520: my ($what,$safeeval,$sheetdata)=@_;
1521: my $symb = $sheetdata->{'usymb'};
1.104 matthew 1522: unless ($symb) { return ''; }
1523: #
1.105 matthew 1524: my $cid = $sheetdata->{'cid'};
1525: my $csec = $sheetdata->{'csec'};
1526: my $uname = $sheetdata->{'uname'};
1527: my $udom = $sheetdata->{'udom'};
1.104 matthew 1528: my $result='';
1529: #
1530: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1531: # Cascading lookup scheme
1532: my $rwhat=$what;
1533: $what =~ s/^parameter\_//;
1534: $what =~ s/\_([^\_]+)$/\.$1/;
1535: #
1536: my $symbparm = $symb.'.'.$what;
1537: my $mapparm = $mapname.'___(all).'.$what;
1538: my $usercourseprefix = $uname.'_'.$udom.'_'.$cid;
1539: #
1540: my $seclevel = $usercourseprefix.'.['.$csec.'].'.$what;
1541: my $seclevelr = $usercourseprefix.'.['.$csec.'].'.$symbparm;
1542: my $seclevelm = $usercourseprefix.'.['.$csec.'].'.$mapparm;
1543: #
1544: my $courselevel = $usercourseprefix.'.'.$what;
1545: my $courselevelr = $usercourseprefix.'.'.$symbparm;
1546: my $courselevelm = $usercourseprefix.'.'.$mapparm;
1547: # fourth, check user
1.115 albertel 1548: if (defined($uname)) {
1549: return $useropt{$courselevelr} if (defined($useropt{$courselevelr}));
1550: return $useropt{$courselevelm} if (defined($useropt{$courselevelm}));
1551: return $useropt{$courselevel} if (defined($useropt{$courselevel}));
1.104 matthew 1552: }
1553: # third, check course
1.115 albertel 1554: if (defined($csec)) {
1555: return $courseopt{$seclevelr} if (defined($courseopt{$seclevelr}));
1556: return $courseopt{$seclevelm} if (defined($courseopt{$seclevelm}));
1557: return $courseopt{$seclevel} if (defined($courseopt{$seclevel}));
1.104 matthew 1558: }
1559: #
1.115 albertel 1560: return $courseopt{$courselevelr} if (defined($courseopt{$courselevelr}));
1561: return $courseopt{$courselevelm} if (defined($courseopt{$courselevelm}));
1562: return $courseopt{$courselevel} if (defined($courseopt{$courselevel}));
1.104 matthew 1563: # second, check map parms
1564: my $thisparm = $parmhash{$symbparm};
1.115 albertel 1565: return $thisparm if (defined($thisparm));
1.104 matthew 1566: # first, check default
1567: return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1.11 www 1568: }
1569:
1.23 www 1570: # ---------------------------------------------- Update rows for course listing
1.28 www 1571: sub updateclasssheet {
1.108 matthew 1572: my ($safeeval,$sheetdata) = @_;
1573: my $cnum =$sheetdata->{'cnum'};
1574: my $cdom =$sheetdata->{'cdom'};
1575: my $cid =$sheetdata->{'cid'};
1576: my $chome =$sheetdata->{'chome'};
1.102 matthew 1577: #
1.113 matthew 1578: %Section = ();
1579:
1580: #
1.102 matthew 1581: # Read class list and row labels
1.118 ! matthew 1582: my $classlist = &Apache::loncoursedata::get_classlist();
! 1583: if (! defined($classlist)) {
! 1584: return 'Could not access course classlist';
! 1585: }
1.102 matthew 1586: #
1.23 www 1587: my %currentlist=();
1.118 ! matthew 1588: foreach my $student (keys(%$classlist)) {
! 1589: my ($studentDomain,$studentName,$end,$start,$id,$studentSection,
! 1590: $fullname,$status) = @{$classlist->{$student}};
! 1591: if ($ENV{'form.Status'} eq $status || $ENV{'form.Status'} eq 'Any') {
1.102 matthew 1592: my $rowlabel='';
1.118 ! matthew 1593: if ($ENV{'form.showcsv'}) {
! 1594: $rowlabel= '"'.join('","',($studentName,$studentDomain,
! 1595: $fullname,$studentSection,$id).'"');
! 1596: } else {
! 1597: $rowlabel='<a href="/adm/studentcalc?uname='.$studentName.
! 1598: '&udom='.$studentDomain.'">';
! 1599: $rowlabel.=$studentSection.' '.$id." ".$fullname;
! 1600: $rowlabel.='</a>';
! 1601: }
1.102 matthew 1602: $currentlist{$student}=$rowlabel;
1.118 ! matthew 1603: }
! 1604: }
1.102 matthew 1605: #
1606: # Find discrepancies between the course row table and this
1607: #
1608: my %f=&getformulas($safeeval);
1609: my $changed=0;
1610: #
1611: my $maxrow=0;
1612: my %existing=();
1613: #
1614: # Now obsolete rows
1615: foreach (keys(%f)) {
1616: if ($_=~/^A(\d+)/) {
1617: $maxrow=($1>$maxrow)?$1:$maxrow;
1618: $existing{$f{$_}}=1;
1619: unless ((defined($currentlist{$f{$_}})) || (!$1) ||
1620: ($f{$_}=~/^(\~\~\~|\-\-\-)/)) {
1621: $f{$_}='!!! Obsolete';
1622: $changed=1;
1.23 www 1623: }
1.78 matthew 1624: }
1.102 matthew 1625: }
1626: #
1627: # New and unknown keys
1628: foreach (sort keys(%currentlist)) {
1629: unless ($existing{$_}) {
1630: $changed=1;
1631: $maxrow++;
1632: $f{'A'.$maxrow}=$_;
1.78 matthew 1633: }
1.23 www 1634: }
1.102 matthew 1635: if ($changed) { &setformulas($safeeval,%f); }
1636: #
1.116 matthew 1637: $sheetdata->{'maxrow'} = $maxrow;
1.102 matthew 1638: &setrowlabels($safeeval,%currentlist);
1.23 www 1639: }
1.5 www 1640:
1.28 www 1641: # ----------------------------------- Update rows for student and assess sheets
1642: sub updatestudentassesssheet {
1.108 matthew 1643: my ($safeeval,$sheetdata) = @_;
1.5 www 1644: my %bighash;
1.108 matthew 1645: my $stype=$sheetdata->{'sheettype'};
1646: my $uname=$sheetdata->{'uname'};
1647: my $udom =$sheetdata->{'udom'};
1.35 www 1648: my %current=();
1.108 matthew 1649: if ($updatedata
1650: {$ENV{'request.course.fn'}.'_'.$stype.'_'.$uname.'_'.$udom}) {
1.104 matthew 1651: %current=split(/\_\_\_\;\_\_\_/,
1.108 matthew 1652: $updatedata{$ENV{'request.course.fn'}.
1653: '_'.$stype.'_'.$uname.'_'.$udom});
1.104 matthew 1654: } else {
1655: # Tie hash
1656: tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1657: &GDBM_READER(),0640);
1658: if (! tied(%bighash)) {
1659: return 'Could not access course data';
1660: }
1661: # Get all assessments
1662: my %allkeys=('timestamp' =>
1.75 www 1663: 'Timestamp of Last Transaction<br>timestamp',
1664: 'subnumber' =>
1665: 'Number of Submissions<br>subnumber',
1666: 'tutornumber' =>
1667: 'Number of Tutor Responses<br>tutornumber',
1668: 'totalpoints' =>
1669: 'Total Points Granted<br>totalpoints');
1.50 www 1670: my $adduserstr='';
1.108 matthew 1671: if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})){
1672: $adduserstr='&uname='.$uname.'&udom='.$udom;
1.50 www 1673: }
1.104 matthew 1674: my %allassess =
1675: ('_feedback' =>'<a href="/adm/assesscalc?usymb=_feedback'.
1676: $adduserstr.'">Feedback</a>',
1677: '_evaluation' =>'<a href="/adm/assesscalc?usymb=_evaluation'.
1678: $adduserstr.'">Evaluation</a>',
1679: '_tutoring' =>'<a href="/adm/assesscalc?usymb=_tutoring'.
1680: $adduserstr.'">Tutoring</a>',
1681: '_discussion' =>'<a href="/adm/assesscalc?usymb=_discussion'.
1682: $adduserstr.'">Discussion</a>'
1683: );
1.107 matthew 1684: while (($_,undef) = each(%bighash)) {
1.104 matthew 1685: next if ($_!~/^src\_(\d+)\.(\d+)$/);
1686: my $mapid=$1;
1687: my $resid=$2;
1688: my $id=$mapid.'.'.$resid;
1689: my $srcf=$bighash{$_};
1690: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1691: my $symb=
1692: &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
1693: '___'.$resid.'___'.&Apache::lonnet::declutter($srcf);
1694: $allassess{$symb}=
1695: '<a href="/adm/assesscalc?usymb='.$symb.$adduserstr.'">'.
1696: $bighash{'title_'.$id}.'</a>';
1697: next if ($stype ne 'assesscalc');
1698: foreach my $key (split(/\,/,
1699: &Apache::lonnet::metadata($srcf,'keys')
1700: )) {
1701: next if ($key !~ /^(stores|parameter)_/);
1702: my $display=
1703: &Apache::lonnet::metadata($srcf,$key.'.display');
1704: unless ($display) {
1705: $display.=
1706: &Apache::lonnet::metadata($srcf,$key.'.name');
1707: }
1708: $display.='<br>'.$key;
1709: $allkeys{$key}=$display;
1710: } # end of foreach
1711: }
1.78 matthew 1712: } # end of foreach (keys(%bighash))
1.5 www 1713: untie(%bighash);
1.104 matthew 1714: #
1715: # %allkeys has a list of storage and parameter displays by unikey
1716: # %allassess has a list of all resource displays by symb
1717: #
1.6 www 1718: if ($stype eq 'assesscalc') {
1.104 matthew 1719: %current=%allkeys;
1.6 www 1720: } elsif ($stype eq 'studentcalc') {
1721: %current=%allassess;
1722: }
1.108 matthew 1723: $updatedata{$ENV{'request.course.fn'}.'_'.$stype.'_'.$uname.'_'.$udom}=
1.104 matthew 1724: join('___;___',%current);
1725: # Get current from cache
1.35 www 1726: }
1.104 matthew 1727: # Find discrepancies between the course row table and this
1728: #
1729: my %f=&getformulas($safeeval);
1730: my $changed=0;
1731:
1732: my $maxrow=0;
1733: my %existing=();
1734: # Now obsolete rows
1735: foreach (keys(%f)) {
1736: next if ($_!~/^A(\d+)/);
1737: $maxrow=($1>$maxrow)?$1:$maxrow;
1738: my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
1739: $existing{$usy}=1;
1740: unless ((defined($current{$usy})) || (!$1) ||
1741: ($f{$_}=~/^(\~\~\~|\-\-\-)/)){
1742: $f{$_}='!!! Obsolete';
1743: $changed=1;
1744: } elsif ($ufn) {
1745: $current{$usy}
1746: =~s/assesscalc\?usymb\=/assesscalc\?ufn\=$ufn\&usymb\=/;
1747: }
1.35 www 1748: }
1.104 matthew 1749: # New and unknown keys
1750: foreach (keys(%current)) {
1751: unless ($existing{$_}) {
1752: $changed=1;
1753: $maxrow++;
1754: $f{'A'.$maxrow}=$_;
1.78 matthew 1755: }
1.104 matthew 1756: }
1757: if ($changed) { &setformulas($safeeval,%f); }
1.116 matthew 1758: $sheetdata->{'maxrow'} = $maxrow;
1.104 matthew 1759: &setrowlabels($safeeval,%current);
1760: #
1761: undef %current;
1762: undef %existing;
1.5 www 1763: }
1.3 www 1764:
1.24 www 1765: # ------------------------------------------------ Load data for one assessment
1.16 www 1766:
1.29 www 1767: sub loadstudent {
1.105 matthew 1768: my ($safeeval,$sheetdata)=@_;
1.16 www 1769: my %c=();
1770: my %f=&getformulas($safeeval);
1.105 matthew 1771: $cachedassess=$sheetdata->{'uname'}.':'.$sheetdata->{'udom'};
1.102 matthew 1772: # Get ALL the student preformance data
1.105 matthew 1773: my @tmp = &Apache::lonnet::dump($sheetdata->{'cid'},
1774: $sheetdata->{'udom'},
1775: $sheetdata->{'uname'},
1.102 matthew 1776: undef);
1777: if ($tmp[0] !~ /^error:/) {
1778: %cachedstores = @tmp;
1.39 www 1779: }
1.102 matthew 1780: undef @tmp;
1781: #
1.36 www 1782: my @assessdata=();
1.78 matthew 1783: foreach (keys(%f)) {
1.104 matthew 1784: next if ($_!~/^A(\d+)/);
1785: my $row=$1;
1786: next if (($f{$_}=~/^[\!\~\-]/) || ($row==0));
1787: my ($usy,$ufn)=split(/__&&&\__/,$f{$_});
1.105 matthew 1788: @assessdata=&exportsheet($sheetdata->{'uname'},
1789: $sheetdata->{'udom'},
1.104 matthew 1790: 'assesscalc',$usy,$ufn);
1791: my $index=0;
1792: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1793: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1794: if ($assessdata[$index]) {
1795: my $col=$_;
1796: if ($assessdata[$index]=~/\D/) {
1797: $c{$col.$row}="'".$assessdata[$index]."'";
1798: } else {
1799: $c{$col.$row}=$assessdata[$index];
1800: }
1801: unless ($col eq 'A') {
1802: $f{$col.$row}='import';
1803: }
1804: }
1805: $index++;
1.16 www 1806: }
1.78 matthew 1807: }
1.39 www 1808: $cachedassess='';
1809: undef %cachedstores;
1.18 www 1810: &setformulas($safeeval,%f);
1.16 www 1811: &setconstants($safeeval,%c);
1812: }
1813:
1.24 www 1814: # --------------------------------------------------- Load data for one student
1.109 matthew 1815: #
1.30 www 1816: sub loadcourse {
1.105 matthew 1817: my ($safeeval,$sheetdata,$r)=@_;
1.24 www 1818: my %c=();
1819: my %f=&getformulas($safeeval);
1.37 www 1820: my $total=0;
1.78 matthew 1821: foreach (keys(%f)) {
1.37 www 1822: if ($_=~/^A(\d+)/) {
1.97 www 1823: unless ($f{$_}=~/^[\!\~\-]/) { $total++; }
1.37 www 1824: }
1.78 matthew 1825: }
1.37 www 1826: my $now=0;
1827: my $since=time;
1.39 www 1828: $r->print(<<ENDPOP);
1829: <script>
1830: popwin=open('','popwin','width=400,height=100');
1831: popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
1.50 www 1832: '<h3>Spreadsheet Calculation Progress</h3>'+
1.39 www 1833: '<form name=popremain>'+
1834: '<input type=text size=35 name=remaining value=Starting></form>'+
1835: '</body></html>');
1.42 www 1836: popwin.document.close();
1.39 www 1837: </script>
1838: ENDPOP
1.37 www 1839: $r->rflush();
1.78 matthew 1840: foreach (keys(%f)) {
1.104 matthew 1841: next if ($_!~/^A(\d+)/);
1842: my $row=$1;
1843: next if (($f{$_}=~/^[\!\~\-]/) || ($row==0));
1844: my @studentdata=&exportsheet(split(/\:/,$f{$_}),
1845: 'studentcalc');
1846: undef %userrdatas;
1847: $now++;
1848: $r->print('<script>popwin.document.popremain.remaining.value="'.
1.37 www 1849: $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
1.104 matthew 1850: ' secs remaining";</script>');
1851: $r->rflush();
1852: #
1853: my $index=0;
1854: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1855: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1856: if ($studentdata[$index]) {
1857: my $col=$_;
1858: if ($studentdata[$index]=~/\D/) {
1859: $c{$col.$row}="'".$studentdata[$index]."'";
1860: } else {
1861: $c{$col.$row}=$studentdata[$index];
1862: }
1863: unless ($col eq 'A') {
1864: $f{$col.$row}='import';
1865: }
1866: $index++;
1867: }
1.24 www 1868: }
1.78 matthew 1869: }
1.24 www 1870: &setformulas($safeeval,%f);
1871: &setconstants($safeeval,%c);
1.43 www 1872: $r->print('<script>popwin.close()</script>');
1.37 www 1873: $r->rflush();
1.24 www 1874: }
1875:
1.6 www 1876: # ------------------------------------------------ Load data for one assessment
1.109 matthew 1877: #
1.29 www 1878: sub loadassessment {
1.105 matthew 1879: my ($safeeval,$sheetdata)=@_;
1.29 www 1880:
1.105 matthew 1881: my $uhome = $sheetdata->{'uhome'};
1882: my $uname = $sheetdata->{'uname'};
1883: my $udom = $sheetdata->{'udom'};
1884: my $symb = $sheetdata->{'usymb'};
1885: my $cid = $sheetdata->{'cid'};
1886: my $cnum = $sheetdata->{'cnum'};
1887: my $cdom = $sheetdata->{'cdom'};
1888: my $chome = $sheetdata->{'chome'};
1.29 www 1889:
1.6 www 1890: my $namespace;
1.29 www 1891: unless ($namespace=$cid) { return ''; }
1.104 matthew 1892: # Get stored values
1893: my %returnhash=();
1894: if ($cachedassess eq $uname.':'.$udom) {
1895: #
1896: # get data out of the dumped stores
1897: #
1898: my $version=$cachedstores{'version:'.$symb};
1899: my $scope;
1900: for ($scope=1;$scope<=$version;$scope++) {
1901: foreach (split(/\:/,$cachedstores{$scope.':keys:'.$symb})) {
1902: $returnhash{$_}=$cachedstores{$scope.':'.$symb.':'.$_};
1903: }
1904: }
1905: } else {
1906: #
1907: # restore individual
1908: #
1.109 matthew 1909: %returnhash = &Apache::lonnet::restore($symb,$namespace,$udom,$uname);
1910: for (my $version=1;$version<=$returnhash{'version'};$version++) {
1.104 matthew 1911: foreach (split(/\:/,$returnhash{$version.':keys'})) {
1912: $returnhash{$_}=$returnhash{$version.':'.$_};
1913: }
1914: }
1.6 www 1915: }
1.109 matthew 1916: #
1.104 matthew 1917: # returnhash now has all stores for this resource
1918: # convert all "_" to "." to be able to use libraries, multiparts, etc
1.109 matthew 1919: #
1920: # This is dumb. It is also necessary :(
1.76 www 1921: my @oldkeys=keys %returnhash;
1.109 matthew 1922: #
1.116 matthew 1923: foreach my $name (@oldkeys) {
1924: my $value=$returnhash{$name};
1925: delete $returnhash{$name};
1.76 www 1926: $name=~s/\_/\./g;
1927: $returnhash{$name}=$value;
1.78 matthew 1928: }
1.104 matthew 1929: # initialize coursedata and userdata for this user
1.31 www 1930: undef %courseopt;
1931: undef %useropt;
1.29 www 1932:
1933: my $userprefix=$uname.'_'.$udom.'_';
1.116 matthew 1934:
1.11 www 1935: unless ($uhome eq 'no_host') {
1.104 matthew 1936: # Get coursedata
1.105 matthew 1937: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
1.116 matthew 1938: my %Tmp = &Apache::lonnet::dump('resourcedata',$cdom,$cnum);
1939: $courserdatas{$cid}=\%Tmp;
1940: $courserdatas{$cid.'.last_cache'}=time;
1.105 matthew 1941: }
1.116 matthew 1942: while (my ($name,$value) = each(%{$courserdatas{$cid}})) {
1943: $courseopt{$userprefix.$name}=$value;
1.104 matthew 1944: }
1945: # Get userdata (if present)
1.116 matthew 1946: unless ((time-$userrdatas{$uname.'@'.$udom.'.last_cache'})<240) {
1947: my %Tmp = &Apache::lonnet::dump('resourcedata',$udom,$uname);
1948: $userrdatas{$cid} = \%Tmp;
1.114 matthew 1949: # Most of the time the user does not have a 'resourcedata.db'
1950: # file. We need to cache that we got nothing instead of bothering
1951: # with requesting it every time.
1.116 matthew 1952: $userrdatas{$uname.'@'.$udom.'.last_cache'}=time;
1.109 matthew 1953: }
1.116 matthew 1954: while (my ($name,$value) = each(%{$userrdatas{$cid}})) {
1955: $useropt{$userprefix.$name}=$value;
1.104 matthew 1956: }
1.29 www 1957: }
1.104 matthew 1958: # now courseopt, useropt initialized for this user and course
1959: # (used by parmval)
1960: #
1961: # Load keys for this assessment only
1962: #
1.60 www 1963: my %thisassess=();
1964: my ($symap,$syid,$srcf)=split(/\_\_\_/,$symb);
1.78 matthew 1965: foreach (split(/\,/,&Apache::lonnet::metadata($srcf,'keys'))) {
1.60 www 1966: $thisassess{$_}=1;
1.78 matthew 1967: }
1.104 matthew 1968: #
1969: # Load parameters
1970: #
1971: my %c=();
1972: if (tie(%parmhash,'GDBM_File',
1.116 matthew 1973: $sheetdata->{'coursefilename'}.'_parms.db',&GDBM_READER(),0640)) {
1.104 matthew 1974: my %f=&getformulas($safeeval);
1975: foreach (keys(%f)) {
1976: next if ($_!~/^A/);
1977: next if ($f{$_}=~/^[\!\~\-]/);
1978: if ($f{$_}=~/^parameter/) {
1979: if ($thisassess{$f{$_}}) {
1.105 matthew 1980: my $val=&parmval($f{$_},$safeeval,$sheetdata);
1.104 matthew 1981: $c{$_}=$val;
1982: $c{$f{$_}}=$val;
1983: }
1984: } else {
1985: my $key=$f{$_};
1986: my $ckey=$key;
1987: $key=~s/^stores\_/resource\./;
1988: $key=~s/\_/\./g;
1989: $c{$_}=$returnhash{$key};
1990: $c{$ckey}=$returnhash{$key};
1991: }
1.6 www 1992: }
1.104 matthew 1993: untie(%parmhash);
1.78 matthew 1994: }
1.104 matthew 1995: &setconstants($safeeval,%c);
1.6 www 1996: }
1997:
1.10 www 1998: # --------------------------------------------------------- Various form fields
1999:
2000: sub textfield {
2001: my ($title,$name,$value)=@_;
2002: return "\n<p><b>$title:</b><br>".
1.104 matthew 2003: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
1.10 www 2004: }
2005:
2006: sub hiddenfield {
2007: my ($name,$value)=@_;
2008: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
2009: }
2010:
2011: sub selectbox {
2012: my ($title,$name,$value,%options)=@_;
2013: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
1.78 matthew 2014: foreach (sort keys(%options)) {
1.10 www 2015: $selout.='<option value="'.$_.'"';
2016: if ($_ eq $value) { $selout.=' selected'; }
2017: $selout.='>'.$options{$_}.'</option>';
1.78 matthew 2018: }
1.10 www 2019: return $selout.'</select>';
2020: }
2021:
1.28 www 2022: # =============================================== Update information in a sheet
2023: #
2024: # Add new users or assessments, etc.
2025: #
2026:
2027: sub updatesheet {
1.105 matthew 2028: my ($safeeval,$sheetdata)=@_;
2029: my $stype=$sheetdata->{'sheettype'};
1.28 www 2030: if ($stype eq 'classcalc') {
1.108 matthew 2031: return &updateclasssheet($safeeval,$sheetdata);
1.28 www 2032: } else {
1.108 matthew 2033: return &updatestudentassesssheet($safeeval,$sheetdata);
1.28 www 2034: }
2035: }
2036:
2037: # =================================================== Load the rows for a sheet
2038: #
2039: # Import the data for rows
2040: #
2041:
1.37 www 2042: sub loadrows {
1.105 matthew 2043: my ($safeeval,$sheetdata,$r)=@_;
2044: my $stype=$sheetdata->{'sheettype'};
1.28 www 2045: if ($stype eq 'classcalc') {
1.105 matthew 2046: &loadcourse($safeeval,$sheetdata,$r);
1.28 www 2047: } elsif ($stype eq 'studentcalc') {
1.105 matthew 2048: &loadstudent($safeeval,$sheetdata);
1.28 www 2049: } else {
1.105 matthew 2050: &loadassessment($safeeval,$sheetdata);
1.28 www 2051: }
2052: }
2053:
1.47 www 2054: # ======================================================= Forced recalculation?
2055:
2056: sub checkthis {
2057: my ($keyname,$time)=@_;
2058: return ($time<$expiredates{$keyname});
2059: }
1.104 matthew 2060:
1.47 www 2061: sub forcedrecalc {
2062: my ($uname,$udom,$stype,$usymb)=@_;
2063: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2064: my $time=$oldsheets{$key.'.time'};
1.53 www 2065: if ($ENV{'form.forcerecalc'}) { return 1; }
1.47 www 2066: unless ($time) { return 1; }
2067: if ($stype eq 'assesscalc') {
2068: my $map=(split(/\_\_\_/,$usymb))[0];
2069: if (&checkthis('::assesscalc:',$time) ||
2070: &checkthis('::assesscalc:'.$map,$time) ||
2071: &checkthis('::assesscalc:'.$usymb,$time) ||
1.49 www 2072: &checkthis($uname.':'.$udom.':assesscalc:',$time) ||
2073: &checkthis($uname.':'.$udom.':assesscalc:'.$map,$time) ||
2074: &checkthis($uname.':'.$udom.':assesscalc:'.$usymb,$time)) {
1.47 www 2075: return 1;
2076: }
2077: } else {
2078: if (&checkthis('::studentcalc:',$time) ||
1.51 www 2079: &checkthis($uname.':'.$udom.':studentcalc:',$time)) {
1.47 www 2080: return 1;
2081: }
2082: }
2083: return 0;
2084: }
2085:
1.28 www 2086: # ============================================================== Export handler
2087: #
2088: # Non-interactive call from with program
2089: #
2090:
2091: sub exportsheet {
1.104 matthew 2092: my ($uname,$udom,$stype,$usymb,$fn)=@_;
2093: my @exportarr=();
2094: if (($usymb=~/^\_(\w+)/) && (!$fn)) {
2095: $fn='default_'.$1;
2096: }
2097: #
2098: # Check if cached
2099: #
2100: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2101: my $found='';
2102: if ($oldsheets{$key}) {
2103: foreach (split(/\_\_\_\&\_\_\_/,$oldsheets{$key})) {
1.46 www 2104: my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
2105: if ($name eq $fn) {
1.104 matthew 2106: $found=$value;
1.46 www 2107: }
1.104 matthew 2108: }
1.46 www 2109: }
1.104 matthew 2110: unless ($found) {
2111: &cachedssheets($uname,$udom,&Apache::lonnet::homeserver($uname,$udom));
2112: if ($oldsheets{$key}) {
2113: foreach (split(/\_\_\_\&\_\_\_/,$oldsheets{$key})) {
2114: my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
2115: if ($name eq $fn) {
2116: $found=$value;
2117: }
2118: }
2119: }
1.44 www 2120: }
1.104 matthew 2121: #
2122: # Check if still valid
2123: #
2124: if ($found) {
2125: if (&forcedrecalc($uname,$udom,$stype,$usymb)) {
2126: $found='';
2127: }
2128: }
2129: if ($found) {
2130: #
2131: # Return what was cached
2132: #
2133: @exportarr=split(/\_\_\_\;\_\_\_/,$found);
2134: } else {
2135: #
2136: # Not cached
2137: #
1.105 matthew 2138: my ($thissheet,$sheetdata)=&makenewsheet($uname,$udom,$stype,$usymb);
1.107 matthew 2139: &readsheet($thissheet,$sheetdata,$fn);
1.105 matthew 2140: &updatesheet($thissheet,$sheetdata);
2141: &loadrows($thissheet,$sheetdata);
1.116 matthew 2142: &calcsheet($thissheet);
2143: @exportarr=&exportdata($thissheet);
1.104 matthew 2144: #
2145: # Store now
2146: #
2147: my $cid=$ENV{'request.course.id'};
2148: my $current='';
2149: if ($stype eq 'studentcalc') {
2150: $current=&Apache::lonnet::reply('get:'.
2151: $ENV{'course.'.$cid.'.domain'}.':'.
2152: $ENV{'course.'.$cid.'.num'}.
2153: ':nohist_calculatedsheets:'.
2154: &Apache::lonnet::escape($key),
2155: $ENV{'course.'.$cid.'.home'});
2156: } else {
1.105 matthew 2157: $current=&Apache::lonnet::reply('get:'.$sheetdata->{'udom'}.':'.
2158: $sheetdata->{'uname'}.
1.104 matthew 2159: ':nohist_calculatedsheets_'.
2160: $ENV{'request.course.id'}.':'.
2161: &Apache::lonnet::escape($key),
1.105 matthew 2162: $sheetdata->{'uhome'});
1.104 matthew 2163: }
2164: my %currentlystored=();
2165: unless ($current=~/^error\:/) {
2166: foreach (split(/___&\___/,&Apache::lonnet::unescape($current))) {
2167: my ($name,$value)=split(/___=___/,$_);
2168: $currentlystored{$name}=$value;
2169: }
2170: }
2171: $currentlystored{$fn}=join('___;___',@exportarr);
2172: #
2173: my $newstore='';
2174: foreach (keys(%currentlystored)) {
2175: if ($newstore) { $newstore.='___&___'; }
2176: $newstore.=$_.'___=___'.$currentlystored{$_};
2177: }
2178: my $now=time;
2179: if ($stype eq 'studentcalc') {
2180: &Apache::lonnet::reply('put:'.
2181: $ENV{'course.'.$cid.'.domain'}.':'.
2182: $ENV{'course.'.$cid.'.num'}.
2183: ':nohist_calculatedsheets:'.
2184: &Apache::lonnet::escape($key).'='.
2185: &Apache::lonnet::escape($newstore).'&'.
2186: &Apache::lonnet::escape($key).'.time='.$now,
2187: $ENV{'course.'.$cid.'.home'});
2188: } else {
2189: &Apache::lonnet::reply('put:'.
1.105 matthew 2190: $sheetdata->{'udom'}.':'.
2191: $sheetdata->{'uname'}.
1.104 matthew 2192: ':nohist_calculatedsheets_'.
2193: $ENV{'request.course.id'}.':'.
2194: &Apache::lonnet::escape($key).'='.
2195: &Apache::lonnet::escape($newstore).'&'.
2196: &Apache::lonnet::escape($key).'.time='.$now,
1.105 matthew 2197: $sheetdata->{'uhome'});
1.104 matthew 2198: }
1.78 matthew 2199: }
1.104 matthew 2200: return @exportarr;
1.44 www 2201: }
1.104 matthew 2202:
1.48 www 2203: # ============================================================ Expiration Dates
2204: #
2205: # Load previously cached student spreadsheets for this course
2206: #
2207: sub expirationdates {
2208: undef %expiredates;
2209: my $cid=$ENV{'request.course.id'};
2210: my $reply=&Apache::lonnet::reply('dump:'.
2211: $ENV{'course.'.$cid.'.domain'}.':'.
2212: $ENV{'course.'.$cid.'.num'}.
2213: ':nohist_expirationdates',
2214: $ENV{'course.'.$cid.'.home'});
2215: unless ($reply=~/^error\:/) {
1.78 matthew 2216: foreach (split(/\&/,$reply)) {
1.48 www 2217: my ($name,$value)=split(/\=/,$_);
2218: $expiredates{&Apache::lonnet::unescape($name)}
2219: =&Apache::lonnet::unescape($value);
1.78 matthew 2220: }
1.48 www 2221: }
2222: }
1.44 www 2223:
2224: # ===================================================== Calculated sheets cache
2225: #
1.46 www 2226: # Load previously cached student spreadsheets for this course
1.44 www 2227: #
2228:
1.46 www 2229: sub cachedcsheets {
1.44 www 2230: my $cid=$ENV{'request.course.id'};
2231: my $reply=&Apache::lonnet::reply('dump:'.
2232: $ENV{'course.'.$cid.'.domain'}.':'.
2233: $ENV{'course.'.$cid.'.num'}.
2234: ':nohist_calculatedsheets',
2235: $ENV{'course.'.$cid.'.home'});
2236: unless ($reply=~/^error\:/) {
1.78 matthew 2237: foreach ( split(/\&/,$reply)) {
1.44 www 2238: my ($name,$value)=split(/\=/,$_);
2239: $oldsheets{&Apache::lonnet::unescape($name)}
2240: =&Apache::lonnet::unescape($value);
1.78 matthew 2241: }
1.44 www 2242: }
1.28 www 2243: }
2244:
1.46 www 2245: # ===================================================== Calculated sheets cache
2246: #
2247: # Load previously cached assessment spreadsheets for this student
2248: #
2249:
2250: sub cachedssheets {
2251: my ($sname,$sdom,$shome)=@_;
2252: unless (($loadedcaches{$sname.'_'.$sdom}) || ($shome eq 'no_host')) {
2253: my $cid=$ENV{'request.course.id'};
2254: my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.
2255: ':nohist_calculatedsheets_'.
2256: $ENV{'request.course.id'},
2257: $shome);
2258: unless ($reply=~/^error\:/) {
1.78 matthew 2259: foreach ( split(/\&/,$reply)) {
1.46 www 2260: my ($name,$value)=split(/\=/,$_);
2261: $oldsheets{&Apache::lonnet::unescape($name)}
2262: =&Apache::lonnet::unescape($value);
1.78 matthew 2263: }
1.46 www 2264: }
2265: $loadedcaches{$sname.'_'.$sdom}=1;
2266: }
2267: }
2268:
2269: # ===================================================== Calculated sheets cache
2270: #
2271: # Load previously cached assessment spreadsheets for this student
2272: #
2273:
1.12 www 2274: # ================================================================ Main handler
1.28 www 2275: #
2276: # Interactive call to screen
2277: #
2278: #
1.3 www 2279: sub handler {
1.7 www 2280: my $r=shift;
1.110 www 2281:
1.118 ! matthew 2282: if (! exists($ENV{'form.Status'})) {
! 2283: $ENV{'form.Status'} = 'Active';
! 2284: }
1.116 matthew 2285: # Check this server
1.111 matthew 2286: my $loaderror=&Apache::lonnet::overloaderror($r);
2287: if ($loaderror) { return $loaderror; }
1.116 matthew 2288: # Check the course homeserver
1.111 matthew 2289: $loaderror= &Apache::lonnet::overloaderror($r,
2290: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
2291: if ($loaderror) { return $loaderror; }
1.116 matthew 2292:
1.28 www 2293: if ($r->header_only) {
1.104 matthew 2294: $r->content_type('text/html');
2295: $r->send_http_header;
2296: return OK;
2297: }
2298: # Global directory configs
1.106 matthew 2299: $includedir = $r->dir_config('lonIncludes');
2300: $tmpdir = $r->dir_config('lonDaemons').'/tmp/';
1.104 matthew 2301: # Needs to be in a course
1.106 matthew 2302: if (! $ENV{'request.course.fn'}) {
2303: # Not in a course, or not allowed to modify parms
2304: $ENV{'user.error.msg'}=
2305: $r->uri.":opa:0:0:Cannot modify spreadsheet";
2306: return HTTP_NOT_ACCEPTABLE;
2307: }
2308: # Get query string for limited number of parameters
2309: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
2310: ['uname','udom','usymb','ufn']);
1.111 matthew 2311: if ($ENV{'request.role'} =~ /^st\./) {
2312: delete $ENV{'form.unewfield'} if (exists($ENV{'form.unewfield'}));
2313: delete $ENV{'form.unewformula'} if (exists($ENV{'form.unewformula'}));
2314: }
1.106 matthew 2315: if (($ENV{'form.usymb'}=~/^\_(\w+)/) && (!$ENV{'form.ufn'})) {
2316: $ENV{'form.ufn'}='default_'.$1;
2317: }
2318: # Interactive loading of specific sheet?
2319: if (($ENV{'form.load'}) && ($ENV{'form.loadthissheet'} ne 'Default')) {
2320: $ENV{'form.ufn'}=$ENV{'form.loadthissheet'};
2321: }
2322: #
2323: # Determine the user name and domain for the sheet.
2324: my $aname;
2325: my $adom;
2326: unless ($ENV{'form.uname'}) {
2327: $aname=$ENV{'user.name'};
2328: $adom=$ENV{'user.domain'};
2329: } else {
2330: $aname=$ENV{'form.uname'};
2331: $adom=$ENV{'form.udom'};
2332: }
2333: #
2334: # Open page
2335: $r->content_type('text/html');
2336: $r->header_out('Cache-control','no-cache');
2337: $r->header_out('Pragma','no-cache');
2338: $r->send_http_header;
2339: # Screen output
2340: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
1.111 matthew 2341: if ($ENV{'request.role'} !~ /^st\./) {
2342: $r->print(<<ENDSCRIPT);
1.10 www 2343: <script language="JavaScript">
2344:
2345: function celledit(cn,cf) {
2346: var cnf=prompt(cn,cf);
1.86 matthew 2347: if (cnf!=null) {
2348: document.sheet.unewfield.value=cn;
1.10 www 2349: document.sheet.unewformula.value=cnf;
2350: document.sheet.submit();
2351: }
2352: }
2353:
1.55 www 2354: function changesheet(cn) {
2355: document.sheet.unewfield.value=cn;
2356: document.sheet.unewformula.value='changesheet';
2357: document.sheet.submit();
2358: }
2359:
1.92 www 2360: function insertrow(cn) {
2361: document.sheet.unewfield.value='insertrow';
2362: document.sheet.unewformula.value=cn;
2363: document.sheet.submit();
2364: }
2365:
1.10 www 2366: </script>
2367: ENDSCRIPT
1.111 matthew 2368: }
1.106 matthew 2369: $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
2370: '<form action="'.$r->uri.'" name=sheet method=post>');
2371: $r->print(&hiddenfield('uname',$ENV{'form.uname'}).
2372: &hiddenfield('udom',$ENV{'form.udom'}).
2373: &hiddenfield('usymb',$ENV{'form.usymb'}).
2374: &hiddenfield('unewfield','').
2375: &hiddenfield('unewformula',''));
2376: $r->rflush();
2377: #
2378: # Full recalc?
2379: if ($ENV{'form.forcerecalc'}) {
2380: $r->print('<h4>Completely Recalculating Sheet ...</h4>');
2381: undef %spreadsheets;
2382: undef %courserdatas;
2383: undef %userrdatas;
2384: undef %defaultsheets;
2385: undef %updatedata;
2386: }
2387: # Read new sheet or modified worksheet
2388: $r->uri=~/\/(\w+)$/;
1.116 matthew 2389: my ($sheet,$sheetdata)=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
1.106 matthew 2390: #
2391: # If a new formula had been entered, go from work copy
2392: if ($ENV{'form.unewfield'}) {
2393: $r->print('<h2>Modified Workcopy</h2>');
2394: $ENV{'form.unewformula'}=~s/\'/\"/g;
2395: $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
2396: $ENV{'form.unewformula'}.'<p>');
1.116 matthew 2397: $sheetdata->{'filename'} = $ENV{'form.ufn'};
2398: &tmpread($sheet,$sheetdata,
2399: $ENV{'form.unewfield'},$ENV{'form.unewformula'});
1.106 matthew 2400: } elsif ($ENV{'form.saveas'}) {
1.116 matthew 2401: $sheetdata->{'filename'} = $ENV{'form.ufn'};
2402: &tmpread($sheet,$sheetdata);
1.106 matthew 2403: } else {
1.116 matthew 2404: &readsheet($sheet,$sheetdata,$ENV{'form.ufn'});
1.106 matthew 2405: }
2406: # Print out user information
1.116 matthew 2407: unless ($sheetdata->{'sheettype'} eq 'classcalc') {
2408: $r->print('<p><b>User:</b> '.$sheetdata->{'uname'}.
2409: '<br><b>Domain:</b> '.$sheetdata->{'udom'});
2410: $r->print('<br><b>Section/Group:</b> '.$sheetdata->{'csec'});
1.106 matthew 2411: if ($ENV{'form.usymb'}) {
2412: $r->print('<br><b>Assessment:</b> <tt>'.
2413: $ENV{'form.usymb'}.'</tt>');
1.30 www 2414: }
1.106 matthew 2415: }
2416: #
2417: # Check user permissions
1.116 matthew 2418: if (($sheetdata->{'sheettype'} eq 'classcalc' ) ||
2419: ($sheetdata->{'uname'} ne $ENV{'user.name'} ) ||
2420: ($sheetdata->{'udom'} ne $ENV{'user.domain'})) {
2421: unless (&Apache::lonnet::allowed('vgr',$sheetdata->{'cid'})) {
1.106 matthew 2422: $r->print('<h1>Access Permission Denied</h1>'.
2423: '</form></body></html>');
2424: return OK;
2425: }
2426: }
2427: # Additional options
2428: $r->print('<br />'.
2429: '<input type="submit" name="forcerecalc" '.
2430: 'value="Completely Recalculate Sheet"><p>');
1.116 matthew 2431: if ($sheetdata->{'sheettype'} eq 'assesscalc') {
1.106 matthew 2432: $r->print('<p><font size=+2>'.
2433: '<a href="/adm/studentcalc?'.
1.116 matthew 2434: 'uname='.$sheetdata->{'uname'}.
2435: '&udom='.$sheetdata->{'udom'}.'">'.
1.106 matthew 2436: 'Level up: Student Sheet</a></font><p>');
2437: }
1.116 matthew 2438: if (($sheetdata->{'sheettype'} eq 'studentcalc') &&
2439: (&Apache::lonnet::allowed('vgr',$sheetdata->{'cid'}))) {
1.106 matthew 2440: $r->print ('<p><font size=+2><a href="/adm/classcalc">'.
2441: 'Level up: Course Sheet</a></font><p>');
2442: }
2443: # Save dialog
2444: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2445: my $fname=$ENV{'form.ufn'};
2446: $fname=~s/\_[^\_]+$//;
2447: if ($fname eq 'default') { $fname='course_default'; }
2448: $r->print('<input type=submit name=saveas value="Save as ...">'.
2449: '<input type=text size=20 name=newfn value="'.$fname.'">'.
2450: 'make default: <input type=checkbox name="makedefufn"><p>');
2451: }
1.116 matthew 2452: $r->print(&hiddenfield('ufn',$sheetdata->{'filename'}));
1.106 matthew 2453: # Load dialog
2454: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2455: $r->print('<p><input type=submit name=load value="Load ...">'.
2456: '<select name="loadthissheet">'.
2457: '<option name="default">Default</option>');
1.116 matthew 2458: foreach (&othersheets($sheet,$sheetdata->{'sheettype'},$sheetdata)) {
1.106 matthew 2459: $r->print('<option name="'.$_.'"');
2460: if ($ENV{'form.ufn'} eq $_) {
2461: $r->print(' selected');
1.104 matthew 2462: }
1.106 matthew 2463: $r->print('>'.$_.'</option>');
2464: }
2465: $r->print('</select><p>');
1.116 matthew 2466: if ($sheetdata->{'sheettype'} eq 'studentcalc') {
2467: &setothersheets($sheet,
2468: &othersheets($sheet,'assesscalc',$sheetdata));
1.104 matthew 2469: }
1.106 matthew 2470: }
2471: # Cached sheets
2472: &expirationdates();
2473: undef %oldsheets;
2474: undef %loadedcaches;
1.116 matthew 2475: if ($sheetdata->{'sheettype'} eq 'classcalc') {
1.106 matthew 2476: $r->print("Loading previously calculated student sheets ...\n");
1.104 matthew 2477: $r->rflush();
1.106 matthew 2478: &cachedcsheets();
1.116 matthew 2479: } elsif ($sheetdata->{'sheettype'} eq 'studentcalc') {
1.106 matthew 2480: $r->print("Loading previously calculated assessment sheets ...\n");
1.46 www 2481: $r->rflush();
1.116 matthew 2482: &cachedssheets($sheetdata->{'uname'},$sheetdata->{'udom'},
2483: $sheetdata->{'uhome'});
1.106 matthew 2484: }
2485: # Update sheet, load rows
2486: $r->print("Loaded sheet(s), updating rows ...<br>\n");
2487: $r->rflush();
2488: #
1.116 matthew 2489: &updatesheet($sheet,$sheetdata);
1.106 matthew 2490: $r->print("Updated rows, loading row data ...\n");
2491: $r->rflush();
2492: #
1.116 matthew 2493: &loadrows($sheet,$sheetdata,$r);
1.106 matthew 2494: $r->print("Loaded row data, calculating sheet ...<br>\n");
2495: $r->rflush();
2496: #
1.116 matthew 2497: my $calcoutput=&calcsheet($sheet);
1.106 matthew 2498: $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
2499: # See if something to save
2500: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2501: my $fname='';
2502: if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
2503: $fname=~s/\W/\_/g;
2504: if ($fname eq 'default') { $fname='course_default'; }
1.116 matthew 2505: $fname.='_'.$sheetdata->{'sheettype'};
1.117 matthew 2506: $sheetdata->{'filename'} = $fname;
1.106 matthew 2507: $ENV{'form.ufn'}=$fname;
2508: $r->print('<p>Saving spreadsheet: '.
1.116 matthew 2509: &writesheet($sheet,$ENV{'form.makedefufn'},$sheetdata).
2510: '<p>');
1.104 matthew 2511: }
1.106 matthew 2512: }
2513: #
1.116 matthew 2514: # Write the modified worksheet
2515: $r->print('<b>Current sheet:</b> '.$sheetdata->{'filename'}.'<p>');
2516: &tmpwrite($sheet,$sheetdata);
2517: if ($sheetdata->{'sheettype'} eq 'studentcalc') {
1.106 matthew 2518: $r->print('<br>Show rows with empty A column: ');
1.62 www 2519: } else {
2520: $r->print('<br>Show empty rows: ');
2521: }
1.106 matthew 2522: #
1.77 www 2523: $r->print(&hiddenfield('userselhidden','true').
1.106 matthew 2524: '<input type="checkbox" name="showall" onClick="submit()"');
2525: #
1.77 www 2526: if ($ENV{'form.showall'}) {
1.106 matthew 2527: $r->print(' checked');
1.77 www 2528: } else {
1.106 matthew 2529: unless ($ENV{'form.userselhidden'}) {
2530: unless
2531: ($ENV{'course.'.$ENV{'request.course.id'}.'.hideemptyrows'} eq 'yes') {
2532: $r->print(' checked');
2533: $ENV{'form.showall'}=1;
2534: }
2535: }
1.77 www 2536: }
1.61 www 2537: $r->print('>');
1.106 matthew 2538: #
2539: # CSV format checkbox (classcalc sheets only)
1.116 matthew 2540: if ($sheetdata->{'sheettype'} eq 'classcalc') {
1.106 matthew 2541: $r->print(' Output CSV format: <input type="checkbox" '.
2542: 'name="showcsv" onClick="submit()"');
2543: if ($ENV{'form.showcsv'}) { $r->print(' checked'); }
2544: $r->print('>');
1.69 www 2545: }
1.106 matthew 2546: #
2547: # Buttons to insert rows
1.98 matthew 2548: $r->print(' Student Status: '.
2549: &Apache::lonhtmlcommon::StatusOptions
2550: ($ENV{'form.Status'},'sheet'));
1.106 matthew 2551: $r->print(<<ENDINSERTBUTTONS);
1.92 www 2552: <br>
2553: <input type='button' onClick='insertrow("top");'
2554: value='Insert Row Top'>
2555: <input type='button' onClick='insertrow("bottom");'
2556: value='Insert Row Bottom'><br>
2557: ENDINSERTBUTTONS
1.106 matthew 2558: # Print out sheet
1.116 matthew 2559: &outsheet($r,$sheet,$sheetdata);
1.10 www 2560: $r->print('</form></body></html>');
1.106 matthew 2561: # Done
1.3 www 2562: return OK;
1.1 www 2563: }
2564:
2565: 1;
2566: __END__
1.77 www 2567:
2568:
2569:
2570:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>