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