Annotation of loncom/interface/lonspreadsheet.pm, revision 1.107
1.79 matthew 1: #
1.107 ! matthew 2: # $Id: lonspreadsheet.pm,v 1.106 2002/09/01 18:06:52 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.107 ! matthew 1398: my ($safeeval,$sheetdata,$fn)=@_;
! 1399: #
! 1400: my $stype = $sheetdata->{'sheettype'};
! 1401: my $cnum = $sheetdata->{'cnum'};
! 1402: my $cdom = $sheetdata->{'cdom'};
! 1403: my $chome = $sheetdata->{'chome'};
! 1404: #
1.104 matthew 1405: if (! defined($fn)) {
1406: # There is no filename. Look for defaults in course and global, cache
1407: unless ($fn=$defaultsheets{$cnum.'_'.$cdom.'_'.$stype}) {
1408: my %tmphash = &Apache::lonnet::get('environment',
1409: ['spreadsheet_default_'.$stype],
1410: $cdom,$cnum);
1411: my ($tmp) = keys(%tmphash);
1412: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1413: $fn = 'default_'.$stype;
1414: } else {
1415: $fn = $tmphash{'spreadsheet_default_'.$stype};
1416: }
1417: unless (($fn) && ($fn!~/^error\:/)) {
1418: $fn='default_'.$stype;
1419: }
1420: $defaultsheets{$cnum.'_'.$cdom.'_'.$stype}=$fn;
1421: }
1422: }
1423: # $fn now has a value
1424: &setfilename($safeeval,$fn);
1425: # see if sheet is cached
1426: my $fstring='';
1427: if ($fstring=$spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}) {
1428: &setformulas($safeeval,split(/\_\_\_\;\_\_\_/,$fstring));
1429: } else {
1430: # Not cached, need to read
1431: my %f=();
1432: if ($fn=~/^default\_/) {
1433: my $sheetxml='';
1434: my $fh;
1435: my $dfn=$fn;
1436: $dfn=~s/\_/\./g;
1437: if ($fh=Apache::File->new($includedir.'/'.$dfn)) {
1438: $sheetxml=join('',<$fh>);
1439: } else {
1440: $sheetxml='<field row="0" col="A">"Error"</field>';
1441: }
1442: %f=%{&parse_sheet(\$sheetxml)};
1443: } elsif($fn=~/\/*\.spreadsheet$/) {
1444: my $sheetxml=&Apache::lonnet::getfile
1445: (&Apache::lonnet::filelocation('',$fn));
1446: if ($sheetxml == -1) {
1447: $sheetxml='<field row="0" col="A">"Error loading spreadsheet '
1448: .$fn.'"</field>';
1449: }
1450: %f=%{&parse_sheet(\$sheetxml)};
1451: } else {
1452: my $sheet='';
1453: my %tmphash = &Apache::lonnet::dump($fn,$cdom,$cnum);
1454: my ($tmp) = keys(%tmphash);
1455: unless ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1456: foreach (keys(%tmphash)) {
1457: $f{$_}=$tmphash{$_};
1458: }
1459: }
1460: }
1461: # Cache and set
1462: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1463: &setformulas($safeeval,%f);
1.3 www 1464: }
1465: }
1466:
1.28 www 1467: # -------------------------------------------------------- Make new spreadsheet
1468:
1469: sub makenewsheet {
1470: my ($uname,$udom,$stype,$usymb)=@_;
1.105 matthew 1471: my %sheetdata=();
1472: $sheetdata{'uname'} = $uname;
1473: $sheetdata{'udom'} = $udom;
1474: $sheetdata{'sheettype'} = $stype;
1475: $sheetdata{'usymb'} = $usymb;
1476: $sheetdata{'cid'} = $ENV{'request.course.id'};
1477: $sheetdata{'csec'} = &Apache::lonnet::usection
1478: ($udom,$uname,$ENV{'request.course.id'});
1479: $sheetdata{'cfn'} = $ENV{'request.course.fn'};
1480: $sheetdata{'cnum'} = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
1481: $sheetdata{'cdom'} = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
1482: $sheetdata{'chome'} = $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1483: $sheetdata{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);
1484:
1.36 www 1485: my $safeeval=initsheet($stype);
1.105 matthew 1486: my $initstring = '';
1487: foreach (keys(%sheetdata)) {
1488: $initstring.= qq{\$$_="$sheetdata{$_}";};
1489: }
1490: $safeeval->reval($initstring);
1491: return $safeeval,\%sheetdata;
1.28 www 1492: }
1493:
1.19 www 1494: # ------------------------------------------------------------ Save spreadsheet
1495:
1496: sub writesheet {
1.104 matthew 1497: my ($safeeval,$makedef)=@_;
1498: my $cid=&getcid($safeeval);
1499: if (&Apache::lonnet::allowed('opa',$cid)) {
1500: my %f=&getformulas($safeeval);
1501: my $stype=&gettype($safeeval);
1502: my $cnum=&getcnum($safeeval);
1503: my $cdom=&getcdom($safeeval);
1504: my $chome=&getchome($safeeval);
1505: my $fn=&getfilename($safeeval);
1506: # Cache new sheet
1507: $spreadsheets{$cnum.'_'.$cdom.'_'.$stype.'_'.$fn}=join('___;___',%f);
1508: # Write sheet
1509: my $sheetdata='';
1510: foreach (keys(%f)) {
1511: unless ($f{$_} eq 'import') {
1512: $sheetdata.=&Apache::lonnet::escape($_).'='.
1513: &Apache::lonnet::escape($f{$_}).'&';
1514: }
1515: }
1516: $sheetdata=~s/\&$//;
1517: my $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.$fn.':'.
1518: $sheetdata,$chome);
1519: if ($reply eq 'ok') {
1520: $reply=&Apache::lonnet::reply('put:'.$cdom.':'.$cnum.':'.
1521: $stype.'_spreadsheets:'.
1522: &Apache::lonnet::escape($fn).
1523: '='.$ENV{'user.name'}.'@'.
1524: $ENV{'user.domain'},
1525: $chome);
1526: if ($reply eq 'ok') {
1527: if ($makedef) {
1528: return &Apache::lonnet::reply('put:'.$cdom.':'.$cnum.
1529: ':environment:'.
1530: 'spreadsheet_default_'.
1531: $stype.'='.
1532: &Apache::lonnet::escape($fn),
1533: $chome);
1534: }
1535: return $reply;
1536: }
1537: return $reply;
1538: }
1539: return $reply;
1540: }
1541: return 'unauthorized';
1.19 www 1542: }
1543:
1.10 www 1544: # ----------------------------------------------- Make a temp copy of the sheet
1.28 www 1545: # "Modified workcopy" - interactive only
1546: #
1.10 www 1547:
1548: sub tmpwrite {
1.28 www 1549: my $safeeval=shift;
1550: my $fn=$ENV{'user.name'}.'_'.
1.104 matthew 1551: $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
1.28 www 1552: &getfilename($safeeval);
1.10 www 1553: $fn=~s/\W/\_/g;
1554: $fn=$tmpdir.$fn.'.tmp';
1555: my $fh;
1556: if ($fh=Apache::File->new('>'.$fn)) {
1557: print $fh join("\n",&getformulas($safeeval));
1558: }
1559: }
1560:
1561: # ---------------------------------------------------------- Read the temp copy
1562:
1563: sub tmpread {
1.28 www 1564: my ($safeeval,$nfield,$nform)=@_;
1565: my $fn=$ENV{'user.name'}.'_'.
1566: $ENV{'user.domain'}.'_spreadsheet_'.&getusymb($safeeval).'_'.
1567: &getfilename($safeeval);
1.10 www 1568: $fn=~s/\W/\_/g;
1569: $fn=$tmpdir.$fn.'.tmp';
1570: my $fh;
1571: my %fo=();
1.92 www 1572: my $countrows=0;
1.10 www 1573: if ($fh=Apache::File->new($fn)) {
1574: my $name;
1575: while ($name=<$fh>) {
1576: chomp($name);
1577: my $value=<$fh>;
1578: chomp($value);
1579: $fo{$name}=$value;
1.93 www 1580: if ($name=~/^A(\d+)$/) {
1581: if ($1>$countrows) {
1582: $countrows=$1;
1583: }
1584: }
1.10 www 1585: }
1586: }
1.55 www 1587: if ($nform eq 'changesheet') {
1.57 www 1588: $fo{'A'.$nfield}=(split(/\_\_\&\&\&\_\_/,$fo{'A'.$nfield}))[0];
1.55 www 1589: unless ($ENV{'form.sel_'.$nfield} eq 'Default') {
1.57 www 1590: $fo{'A'.$nfield}.='__&&&__'.$ENV{'form.sel_'.$nfield};
1.55 www 1591: }
1.92 www 1592: } elsif ($nfield eq 'insertrow') {
1.93 www 1593: $countrows++;
1.95 www 1594: my $newrow=substr('000000'.$countrows,-7);
1.92 www 1595: if ($nform eq 'top') {
1.94 www 1596: $fo{'A'.$countrows}='--- '.$newrow;
1.92 www 1597: } else {
1.94 www 1598: $fo{'A'.$countrows}='~~~ '.$newrow;
1.92 www 1599: }
1.55 www 1600: } else {
1601: if ($nfield) { $fo{$nfield}=$nform; }
1602: }
1.10 www 1603: &setformulas($safeeval,%fo);
1604: }
1605:
1.104 matthew 1606: ##################################################
1607: ##################################################
1.11 www 1608:
1.104 matthew 1609: =pod
1.11 www 1610:
1.104 matthew 1611: =item &parmval()
1.11 www 1612:
1.104 matthew 1613: Determine the value of a parameter.
1.11 www 1614:
1.104 matthew 1615: Inputs: $what, the parameter needed, $safeeval, the safe space
1.11 www 1616:
1.104 matthew 1617: Returns: The value of a parameter, or '' if none.
1.11 www 1618:
1.104 matthew 1619: This function cascades through the possible levels searching for a value for
1620: a parameter. The levels are checked in the following order:
1621: user, course (at section level and course level), map, and lonnet::metadata.
1622: This function uses %parmhash, which must be tied prior to calling it.
1623: This function also requires %courseopt and %useropt to be initialized for
1624: this user and course.
1.11 www 1625:
1.104 matthew 1626: =cut
1.11 www 1627:
1.104 matthew 1628: ##################################################
1629: ##################################################
1630: sub parmval {
1.105 matthew 1631: my ($what,$safeeval,$sheetdata)=@_;
1632: my $symb = $sheetdata->{'usymb'};
1.104 matthew 1633: unless ($symb) { return ''; }
1634: #
1.105 matthew 1635: my $cid = $sheetdata->{'cid'};
1636: my $csec = $sheetdata->{'csec'};
1637: my $uname = $sheetdata->{'uname'};
1638: my $udom = $sheetdata->{'udom'};
1.104 matthew 1639: my $result='';
1640: #
1641: my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
1642: # Cascading lookup scheme
1643: my $rwhat=$what;
1644: $what =~ s/^parameter\_//;
1645: $what =~ s/\_([^\_]+)$/\.$1/;
1646: #
1647: my $symbparm = $symb.'.'.$what;
1648: my $mapparm = $mapname.'___(all).'.$what;
1649: my $usercourseprefix = $uname.'_'.$udom.'_'.$cid;
1650: #
1651: my $seclevel = $usercourseprefix.'.['.$csec.'].'.$what;
1652: my $seclevelr = $usercourseprefix.'.['.$csec.'].'.$symbparm;
1653: my $seclevelm = $usercourseprefix.'.['.$csec.'].'.$mapparm;
1654: #
1655: my $courselevel = $usercourseprefix.'.'.$what;
1656: my $courselevelr = $usercourseprefix.'.'.$symbparm;
1657: my $courselevelm = $usercourseprefix.'.'.$mapparm;
1658: # fourth, check user
1659: if ($uname) {
1660: return $useropt{$courselevelr} if ($useropt{$courselevelr});
1661: return $useropt{$courselevelm} if ($useropt{$courselevelm});
1.105 matthew 1662: return $useropt{$courselevel} if ($useropt{$courselevel});
1.104 matthew 1663: }
1664: # third, check course
1665: if ($csec) {
1666: return $courseopt{$seclevelr} if ($courseopt{$seclevelr});
1667: return $courseopt{$seclevelm} if ($courseopt{$seclevelm});
1.105 matthew 1668: return $courseopt{$seclevel} if ($courseopt{$seclevel});
1.104 matthew 1669: }
1670: #
1671: return $courseopt{$courselevelr} if ($courseopt{$courselevelr});
1672: return $courseopt{$courselevelm} if ($courseopt{$courselevelm});
1.105 matthew 1673: return $courseopt{$courselevel} if ($courseopt{$courselevel});
1.104 matthew 1674: # second, check map parms
1675: my $thisparm = $parmhash{$symbparm};
1676: return $thisparm if ($thisparm);
1677: # first, check default
1678: return &Apache::lonnet::metadata($fn,$rwhat.'.default');
1.11 www 1679: }
1680:
1.23 www 1681: # ---------------------------------------------- Update rows for course listing
1.28 www 1682: sub updateclasssheet {
1.23 www 1683: my $safeeval=shift;
1.28 www 1684: my $cnum=&getcnum($safeeval);
1685: my $cdom=&getcdom($safeeval);
1686: my $cid=&getcid($safeeval);
1687: my $chome=&getchome($safeeval);
1.102 matthew 1688: #
1689: # Read class list and row labels
1690: my %classlist;
1691: my @tmp = &Apache::lonnet::dump('classlist',$cdom,$cnum);
1692: if ($tmp[0] !~ /^error/) {
1693: %classlist = @tmp;
1694: } else {
1695: return 'Could not access course data';
1696: }
1697: undef @tmp;
1698: #
1.23 www 1699: my %currentlist=();
1700: my $now=time;
1.102 matthew 1701: foreach my $student (keys(%classlist)) {
1702: my ($end,$start)=split(/\:/,$classlist{$student});
1703: my $active=1;
1704: $active = 0 if (($end) && ($now>$end));
1705: $active = 1 if ($ENV{'form.Status'} eq 'Any');
1706: $active = !$active if ($ENV{'form.Status'} eq 'Expired');
1707: if ($active) {
1708: my $rowlabel='';
1709: my ($studentName,$studentDomain)=split(/\:/,$student);
1710: my $studentSection=&Apache::lonnet::usection($studentDomain,
1711: $studentName,$cid);
1712: if ($studentSection==-1) {
1713: unless ($ENV{'form.showcsv'}) {
1714: $rowlabel='<font color=red>Data not available: '.
1715: $studentName.'</font>';
1.24 www 1716: } else {
1.102 matthew 1717: $rowlabel='ERROR","'.$studentName.
1718: '","Data not available","","","';
1719: }
1720: } else {
1721: my %reply=&Apache::lonnet::idrget($studentDomain,$studentName);
1722: my %studentInformation=&Apache::lonnet::get
1723: ('environment',
1724: ['lastname','generation','firstname','middlename','id'],
1725: $studentDomain,$studentName);
1726: if (! $ENV{'form.showcsv'}) {
1727: $rowlabel='<a href="/adm/studentcalc?uname='.$studentName.
1728: '&udom='.$studentDomain.'">'.
1729: $studentSection.' ';
1730: foreach ('id','firstname','middlename',
1731: 'lastname','generation'){
1732: $rowlabel.=$studentInformation{$_}." ";
1.78 matthew 1733: }
1.45 www 1734: $rowlabel.='</a>';
1.102 matthew 1735: } else {
1736: $rowlabel= '"'.join('","',
1737: ($studentSection,
1738: $studentInformation{'id'},
1739: $studentInformation{'firstname'},
1740: $studentInformation{'middlename'},
1741: $studentInformation{'lastname'},
1742: $studentInformation{'generation'})
1743: ).'"';
1.24 www 1744: }
1.23 www 1745: }
1.102 matthew 1746: $currentlist{$student}=$rowlabel;
1747: } # end of if ($active)
1748: } # end of foreach my $student (keys(%classlist))
1749: #
1750: # Find discrepancies between the course row table and this
1751: #
1752: my %f=&getformulas($safeeval);
1753: my $changed=0;
1754: #
1755: my $maxrow=0;
1756: my %existing=();
1757: #
1758: # Now obsolete rows
1759: foreach (keys(%f)) {
1760: if ($_=~/^A(\d+)/) {
1761: $maxrow=($1>$maxrow)?$1:$maxrow;
1762: $existing{$f{$_}}=1;
1763: unless ((defined($currentlist{$f{$_}})) || (!$1) ||
1764: ($f{$_}=~/^(\~\~\~|\-\-\-)/)) {
1765: $f{$_}='!!! Obsolete';
1766: $changed=1;
1.23 www 1767: }
1.78 matthew 1768: }
1.102 matthew 1769: }
1770: #
1771: # New and unknown keys
1772: foreach (sort keys(%currentlist)) {
1773: unless ($existing{$_}) {
1774: $changed=1;
1775: $maxrow++;
1776: $f{'A'.$maxrow}=$_;
1.78 matthew 1777: }
1.23 www 1778: }
1.102 matthew 1779: if ($changed) { &setformulas($safeeval,%f); }
1780: #
1781: &setmaxrow($safeeval,$maxrow);
1782: &setrowlabels($safeeval,%currentlist);
1.23 www 1783: }
1.5 www 1784:
1.28 www 1785: # ----------------------------------- Update rows for student and assess sheets
1786: sub updatestudentassesssheet {
1.5 www 1787: my $safeeval=shift;
1788: my %bighash;
1.35 www 1789: my $stype=&gettype($safeeval);
1790: my %current=();
1.104 matthew 1791: if ($updatedata{$ENV{'request.course.fn'}.'_'.$stype}) {
1792: %current=split(/\_\_\_\;\_\_\_/,
1793: $updatedata{$ENV{'request.course.fn'}.'_'.$stype});
1794: } else {
1795: # Tie hash
1796: tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1797: &GDBM_READER(),0640);
1798: if (! tied(%bighash)) {
1799: return 'Could not access course data';
1800: }
1801: # Get all assessments
1802: my %allkeys=('timestamp' =>
1.75 www 1803: 'Timestamp of Last Transaction<br>timestamp',
1804: 'subnumber' =>
1805: 'Number of Submissions<br>subnumber',
1806: 'tutornumber' =>
1807: 'Number of Tutor Responses<br>tutornumber',
1808: 'totalpoints' =>
1809: 'Total Points Granted<br>totalpoints');
1.50 www 1810: my $adduserstr='';
1811: if ((&getuname($safeeval) ne $ENV{'user.name'}) ||
1812: (&getudom($safeeval) ne $ENV{'user.domain'})) {
1813: $adduserstr='&uname='.&getuname($safeeval).
1.104 matthew 1814: '&udom='.&getudom($safeeval);
1.50 www 1815: }
1.104 matthew 1816: my %allassess =
1817: ('_feedback' =>'<a href="/adm/assesscalc?usymb=_feedback'.
1818: $adduserstr.'">Feedback</a>',
1819: '_evaluation' =>'<a href="/adm/assesscalc?usymb=_evaluation'.
1820: $adduserstr.'">Evaluation</a>',
1821: '_tutoring' =>'<a href="/adm/assesscalc?usymb=_tutoring'.
1822: $adduserstr.'">Tutoring</a>',
1823: '_discussion' =>'<a href="/adm/assesscalc?usymb=_discussion'.
1824: $adduserstr.'">Discussion</a>'
1825: );
1.107 ! matthew 1826: while (($_,undef) = each(%bighash)) {
1.104 matthew 1827: next if ($_!~/^src\_(\d+)\.(\d+)$/);
1828: my $mapid=$1;
1829: my $resid=$2;
1830: my $id=$mapid.'.'.$resid;
1831: my $srcf=$bighash{$_};
1832: if ($srcf=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1833: my $symb=
1834: &Apache::lonnet::declutter($bighash{'map_id_'.$mapid}).
1835: '___'.$resid.'___'.&Apache::lonnet::declutter($srcf);
1836: $allassess{$symb}=
1837: '<a href="/adm/assesscalc?usymb='.$symb.$adduserstr.'">'.
1838: $bighash{'title_'.$id}.'</a>';
1839: next if ($stype ne 'assesscalc');
1840: foreach my $key (split(/\,/,
1841: &Apache::lonnet::metadata($srcf,'keys')
1842: )) {
1843: next if ($key !~ /^(stores|parameter)_/);
1844: my $display=
1845: &Apache::lonnet::metadata($srcf,$key.'.display');
1846: unless ($display) {
1847: $display.=
1848: &Apache::lonnet::metadata($srcf,$key.'.name');
1849: }
1850: $display.='<br>'.$key;
1851: $allkeys{$key}=$display;
1852: } # end of foreach
1853: }
1.78 matthew 1854: } # end of foreach (keys(%bighash))
1.5 www 1855: untie(%bighash);
1.104 matthew 1856: #
1857: # %allkeys has a list of storage and parameter displays by unikey
1858: # %allassess has a list of all resource displays by symb
1859: #
1.6 www 1860: if ($stype eq 'assesscalc') {
1.104 matthew 1861: %current=%allkeys;
1.6 www 1862: } elsif ($stype eq 'studentcalc') {
1863: %current=%allassess;
1864: }
1.35 www 1865: $updatedata{$ENV{'request.course.fn'}.'_'.$stype}=
1.104 matthew 1866: join('___;___',%current);
1867: # Get current from cache
1.35 www 1868: }
1.104 matthew 1869: # Find discrepancies between the course row table and this
1870: #
1871: my %f=&getformulas($safeeval);
1872: my $changed=0;
1873:
1874: my $maxrow=0;
1875: my %existing=();
1876: # Now obsolete rows
1877: foreach (keys(%f)) {
1878: next if ($_!~/^A(\d+)/);
1879: $maxrow=($1>$maxrow)?$1:$maxrow;
1880: my ($usy,$ufn)=split(/\_\_\&\&\&\_\_/,$f{$_});
1881: $existing{$usy}=1;
1882: unless ((defined($current{$usy})) || (!$1) ||
1883: ($f{$_}=~/^(\~\~\~|\-\-\-)/)){
1884: $f{$_}='!!! Obsolete';
1885: $changed=1;
1886: } elsif ($ufn) {
1887: $current{$usy}
1888: =~s/assesscalc\?usymb\=/assesscalc\?ufn\=$ufn\&usymb\=/;
1889: }
1.35 www 1890: }
1.104 matthew 1891: # New and unknown keys
1892: foreach (keys(%current)) {
1893: unless ($existing{$_}) {
1894: $changed=1;
1895: $maxrow++;
1896: $f{'A'.$maxrow}=$_;
1.78 matthew 1897: }
1.104 matthew 1898: }
1899: if ($changed) { &setformulas($safeeval,%f); }
1900: &setmaxrow($safeeval,$maxrow);
1901: &setrowlabels($safeeval,%current);
1902: #
1903: undef %current;
1904: undef %existing;
1.5 www 1905: }
1.3 www 1906:
1.24 www 1907: # ------------------------------------------------ Load data for one assessment
1.16 www 1908:
1.29 www 1909: sub loadstudent {
1.105 matthew 1910: my ($safeeval,$sheetdata)=@_;
1.16 www 1911: my %c=();
1912: my %f=&getformulas($safeeval);
1.105 matthew 1913: $cachedassess=$sheetdata->{'uname'}.':'.$sheetdata->{'udom'};
1.102 matthew 1914: # Get ALL the student preformance data
1.105 matthew 1915: my @tmp = &Apache::lonnet::dump($sheetdata->{'cid'},
1916: $sheetdata->{'udom'},
1917: $sheetdata->{'uname'},
1.102 matthew 1918: undef);
1919: if ($tmp[0] !~ /^error:/) {
1920: %cachedstores = @tmp;
1.39 www 1921: }
1.102 matthew 1922: undef @tmp;
1923: #
1.36 www 1924: my @assessdata=();
1.78 matthew 1925: foreach (keys(%f)) {
1.104 matthew 1926: next if ($_!~/^A(\d+)/);
1927: my $row=$1;
1928: next if (($f{$_}=~/^[\!\~\-]/) || ($row==0));
1929: my ($usy,$ufn)=split(/__&&&\__/,$f{$_});
1.105 matthew 1930: @assessdata=&exportsheet($sheetdata->{'uname'},
1931: $sheetdata->{'udom'},
1.104 matthew 1932: 'assesscalc',$usy,$ufn);
1933: my $index=0;
1934: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1935: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1936: if ($assessdata[$index]) {
1937: my $col=$_;
1938: if ($assessdata[$index]=~/\D/) {
1939: $c{$col.$row}="'".$assessdata[$index]."'";
1940: } else {
1941: $c{$col.$row}=$assessdata[$index];
1942: }
1943: unless ($col eq 'A') {
1944: $f{$col.$row}='import';
1945: }
1946: }
1947: $index++;
1.16 www 1948: }
1.78 matthew 1949: }
1.39 www 1950: $cachedassess='';
1951: undef %cachedstores;
1.18 www 1952: &setformulas($safeeval,%f);
1.16 www 1953: &setconstants($safeeval,%c);
1954: }
1955:
1.24 www 1956: # --------------------------------------------------- Load data for one student
1957:
1.30 www 1958: sub loadcourse {
1.105 matthew 1959: my ($safeeval,$sheetdata,$r)=@_;
1.24 www 1960: my %c=();
1961: my %f=&getformulas($safeeval);
1.37 www 1962: my $total=0;
1.78 matthew 1963: foreach (keys(%f)) {
1.37 www 1964: if ($_=~/^A(\d+)/) {
1.97 www 1965: unless ($f{$_}=~/^[\!\~\-]/) { $total++; }
1.37 www 1966: }
1.78 matthew 1967: }
1.37 www 1968: my $now=0;
1969: my $since=time;
1.39 www 1970: $r->print(<<ENDPOP);
1971: <script>
1972: popwin=open('','popwin','width=400,height=100');
1973: popwin.document.writeln('<html><body bgcolor="#FFFFFF">'+
1.50 www 1974: '<h3>Spreadsheet Calculation Progress</h3>'+
1.39 www 1975: '<form name=popremain>'+
1976: '<input type=text size=35 name=remaining value=Starting></form>'+
1977: '</body></html>');
1.42 www 1978: popwin.document.close();
1.39 www 1979: </script>
1980: ENDPOP
1.37 www 1981: $r->rflush();
1.78 matthew 1982: foreach (keys(%f)) {
1.104 matthew 1983: next if ($_!~/^A(\d+)/);
1984: my $row=$1;
1985: next if (($f{$_}=~/^[\!\~\-]/) || ($row==0));
1986: my @studentdata=&exportsheet(split(/\:/,$f{$_}),
1987: 'studentcalc');
1988: undef %userrdatas;
1989: $now++;
1990: $r->print('<script>popwin.document.popremain.remaining.value="'.
1.37 www 1991: $now.'/'.$total.': '.int((time-$since)/$now*($total-$now)).
1.104 matthew 1992: ' secs remaining";</script>');
1993: $r->rflush();
1994: #
1995: my $index=0;
1996: foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
1997: 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z') {
1998: if ($studentdata[$index]) {
1999: my $col=$_;
2000: if ($studentdata[$index]=~/\D/) {
2001: $c{$col.$row}="'".$studentdata[$index]."'";
2002: } else {
2003: $c{$col.$row}=$studentdata[$index];
2004: }
2005: unless ($col eq 'A') {
2006: $f{$col.$row}='import';
2007: }
2008: $index++;
2009: }
1.24 www 2010: }
1.78 matthew 2011: }
1.24 www 2012: &setformulas($safeeval,%f);
2013: &setconstants($safeeval,%c);
1.43 www 2014: $r->print('<script>popwin.close()</script>');
1.37 www 2015: $r->rflush();
1.24 www 2016: }
2017:
1.6 www 2018: # ------------------------------------------------ Load data for one assessment
2019:
1.29 www 2020: sub loadassessment {
1.105 matthew 2021: my ($safeeval,$sheetdata)=@_;
1.29 www 2022:
1.105 matthew 2023: my $uhome = $sheetdata->{'uhome'};
2024: my $uname = $sheetdata->{'uname'};
2025: my $udom = $sheetdata->{'udom'};
2026: my $symb = $sheetdata->{'usymb'};
2027: my $cid = $sheetdata->{'cid'};
2028: my $cnum = $sheetdata->{'cnum'};
2029: my $cdom = $sheetdata->{'cdom'};
2030: my $chome = $sheetdata->{'chome'};
1.29 www 2031:
1.6 www 2032: my $namespace;
1.29 www 2033: unless ($namespace=$cid) { return ''; }
1.104 matthew 2034: # Get stored values
2035: my %returnhash=();
2036: if ($cachedassess eq $uname.':'.$udom) {
2037: #
2038: # get data out of the dumped stores
2039: #
2040: my $version=$cachedstores{'version:'.$symb};
2041: my $scope;
2042: for ($scope=1;$scope<=$version;$scope++) {
2043: foreach (split(/\:/,$cachedstores{$scope.':keys:'.$symb})) {
2044: $returnhash{$_}=$cachedstores{$scope.':'.$symb.':'.$_};
2045: }
2046: }
2047: } else {
2048: #
2049: # restore individual
2050: #
2051: my $answer=&Apache::lonnet::reply(
2052: "restore:$udom:$uname:".
2053: &Apache::lonnet::escape($namespace).":".
2054: &Apache::lonnet::escape($symb),$uhome);
2055: foreach (split(/\&/,$answer)) {
2056: my ($name,$value)=split(/\=/,$_);
2057: $returnhash{&Apache::lonnet::unescape($name)}=
2058: &Apache::lonnet::unescape($value);
2059: }
2060: my $version;
2061: for ($version=1;$version<=$returnhash{'version'};$version++) {
2062: foreach (split(/\:/,$returnhash{$version.':keys'})) {
2063: $returnhash{$_}=$returnhash{$version.':'.$_};
2064: }
2065: }
1.6 www 2066: }
1.104 matthew 2067: # returnhash now has all stores for this resource
2068: # convert all "_" to "." to be able to use libraries, multiparts, etc
1.76 www 2069: my @oldkeys=keys %returnhash;
2070:
1.78 matthew 2071: foreach (@oldkeys) {
1.76 www 2072: my $name=$_;
2073: my $value=$returnhash{$_};
2074: delete $returnhash{$_};
2075: $name=~s/\_/\./g;
2076: $returnhash{$name}=$value;
1.78 matthew 2077: }
1.104 matthew 2078: # initialize coursedata and userdata for this user
1.31 www 2079: undef %courseopt;
2080: undef %useropt;
1.29 www 2081:
2082: my $userprefix=$uname.'_'.$udom.'_';
1.104 matthew 2083:
1.11 www 2084: unless ($uhome eq 'no_host') {
1.104 matthew 2085: # Get coursedata
1.105 matthew 2086: unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
2087: my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
2088: ':resourcedata',$chome);
2089: if ($reply!~/^error\:/) {
2090: $courserdatas{$cid}=$reply;
2091: $courserdatas{$cid.'.last_cache'}=time;
1.104 matthew 2092: }
1.105 matthew 2093: }
1.104 matthew 2094: foreach (split(/\&/,$courserdatas{$cid})) {
2095: my ($name,$value)=split(/\=/,$_);
2096: $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
2097: &Apache::lonnet::unescape($value);
2098: }
2099: # Get userdata (if present)
2100: unless
2101: ((time-$userrdatas{$uname.'___'.$udom.'.last_cache'})<240) {
2102: my $reply=
2103: &Apache::lonnet::reply('dump:'.$udom.':'.$uname.':resourcedata',$uhome);
2104: if ($reply!~/^error\:/) {
2105: $userrdatas{$uname.'___'.$udom}=$reply;
2106: $userrdatas{$uname.'___'.$udom.'.last_cache'}=time;
2107: }
2108: }
2109: foreach (split(/\&/,$userrdatas{$uname.'___'.$udom})) {
2110: my ($name,$value)=split(/\=/,$_);
2111: $useropt{$userprefix.&Apache::lonnet::unescape($name)}=
2112: &Apache::lonnet::unescape($value);
2113: }
1.29 www 2114: }
1.104 matthew 2115: # now courseopt, useropt initialized for this user and course
2116: # (used by parmval)
2117: #
2118: # Load keys for this assessment only
2119: #
1.60 www 2120: my %thisassess=();
2121: my ($symap,$syid,$srcf)=split(/\_\_\_/,$symb);
1.78 matthew 2122: foreach (split(/\,/,&Apache::lonnet::metadata($srcf,'keys'))) {
1.60 www 2123: $thisassess{$_}=1;
1.78 matthew 2124: }
1.104 matthew 2125: #
2126: # Load parameters
2127: #
2128: my %c=();
2129: if (tie(%parmhash,'GDBM_File',
2130: &getcfn($safeeval).'_parms.db',&GDBM_READER(),0640)) {
2131: my %f=&getformulas($safeeval);
2132: foreach (keys(%f)) {
2133: next if ($_!~/^A/);
2134: next if ($f{$_}=~/^[\!\~\-]/);
2135: if ($f{$_}=~/^parameter/) {
2136: if ($thisassess{$f{$_}}) {
1.105 matthew 2137: my $val=&parmval($f{$_},$safeeval,$sheetdata);
1.104 matthew 2138: $c{$_}=$val;
2139: $c{$f{$_}}=$val;
2140: }
2141: } else {
2142: my $key=$f{$_};
2143: my $ckey=$key;
2144: $key=~s/^stores\_/resource\./;
2145: $key=~s/\_/\./g;
2146: $c{$_}=$returnhash{$key};
2147: $c{$ckey}=$returnhash{$key};
2148: }
1.6 www 2149: }
1.104 matthew 2150: untie(%parmhash);
1.78 matthew 2151: }
1.104 matthew 2152: &setconstants($safeeval,%c);
1.6 www 2153: }
2154:
1.10 www 2155: # --------------------------------------------------------- Various form fields
2156:
2157: sub textfield {
2158: my ($title,$name,$value)=@_;
2159: return "\n<p><b>$title:</b><br>".
1.104 matthew 2160: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
1.10 www 2161: }
2162:
2163: sub hiddenfield {
2164: my ($name,$value)=@_;
2165: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
2166: }
2167:
2168: sub selectbox {
2169: my ($title,$name,$value,%options)=@_;
2170: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
1.78 matthew 2171: foreach (sort keys(%options)) {
1.10 www 2172: $selout.='<option value="'.$_.'"';
2173: if ($_ eq $value) { $selout.=' selected'; }
2174: $selout.='>'.$options{$_}.'</option>';
1.78 matthew 2175: }
1.10 www 2176: return $selout.'</select>';
2177: }
2178:
1.28 www 2179: # =============================================== Update information in a sheet
2180: #
2181: # Add new users or assessments, etc.
2182: #
2183:
2184: sub updatesheet {
1.105 matthew 2185: my ($safeeval,$sheetdata)=@_;
2186: my $stype=$sheetdata->{'sheettype'};
1.28 www 2187: if ($stype eq 'classcalc') {
2188: return &updateclasssheet($safeeval);
2189: } else {
2190: return &updatestudentassesssheet($safeeval);
2191: }
2192: }
2193:
2194: # =================================================== Load the rows for a sheet
2195: #
2196: # Import the data for rows
2197: #
2198:
1.37 www 2199: sub loadrows {
1.105 matthew 2200: my ($safeeval,$sheetdata,$r)=@_;
2201: my $stype=$sheetdata->{'sheettype'};
1.28 www 2202: if ($stype eq 'classcalc') {
1.105 matthew 2203: &loadcourse($safeeval,$sheetdata,$r);
1.28 www 2204: } elsif ($stype eq 'studentcalc') {
1.105 matthew 2205: &loadstudent($safeeval,$sheetdata);
1.28 www 2206: } else {
1.105 matthew 2207: &loadassessment($safeeval,$sheetdata);
1.28 www 2208: }
2209: }
2210:
1.47 www 2211: # ======================================================= Forced recalculation?
2212:
2213: sub checkthis {
2214: my ($keyname,$time)=@_;
2215: return ($time<$expiredates{$keyname});
2216: }
1.104 matthew 2217:
1.47 www 2218: sub forcedrecalc {
2219: my ($uname,$udom,$stype,$usymb)=@_;
2220: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2221: my $time=$oldsheets{$key.'.time'};
1.53 www 2222: if ($ENV{'form.forcerecalc'}) { return 1; }
1.47 www 2223: unless ($time) { return 1; }
2224: if ($stype eq 'assesscalc') {
2225: my $map=(split(/\_\_\_/,$usymb))[0];
2226: if (&checkthis('::assesscalc:',$time) ||
2227: &checkthis('::assesscalc:'.$map,$time) ||
2228: &checkthis('::assesscalc:'.$usymb,$time) ||
1.49 www 2229: &checkthis($uname.':'.$udom.':assesscalc:',$time) ||
2230: &checkthis($uname.':'.$udom.':assesscalc:'.$map,$time) ||
2231: &checkthis($uname.':'.$udom.':assesscalc:'.$usymb,$time)) {
1.47 www 2232: return 1;
2233: }
2234: } else {
2235: if (&checkthis('::studentcalc:',$time) ||
1.51 www 2236: &checkthis($uname.':'.$udom.':studentcalc:',$time)) {
1.47 www 2237: return 1;
2238: }
2239: }
2240: return 0;
2241: }
2242:
1.28 www 2243: # ============================================================== Export handler
2244: #
2245: # Non-interactive call from with program
2246: #
2247:
2248: sub exportsheet {
1.104 matthew 2249: my ($uname,$udom,$stype,$usymb,$fn)=@_;
2250: my @exportarr=();
2251: if (($usymb=~/^\_(\w+)/) && (!$fn)) {
2252: $fn='default_'.$1;
2253: }
2254: #
2255: # Check if cached
2256: #
2257: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
2258: my $found='';
2259: if ($oldsheets{$key}) {
2260: foreach (split(/\_\_\_\&\_\_\_/,$oldsheets{$key})) {
1.46 www 2261: my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
2262: if ($name eq $fn) {
1.104 matthew 2263: $found=$value;
1.46 www 2264: }
1.104 matthew 2265: }
1.46 www 2266: }
1.104 matthew 2267: unless ($found) {
2268: &cachedssheets($uname,$udom,&Apache::lonnet::homeserver($uname,$udom));
2269: if ($oldsheets{$key}) {
2270: foreach (split(/\_\_\_\&\_\_\_/,$oldsheets{$key})) {
2271: my ($name,$value)=split(/\_\_\_\=\_\_\_/,$_);
2272: if ($name eq $fn) {
2273: $found=$value;
2274: }
2275: }
2276: }
1.44 www 2277: }
1.104 matthew 2278: #
2279: # Check if still valid
2280: #
2281: if ($found) {
2282: if (&forcedrecalc($uname,$udom,$stype,$usymb)) {
2283: $found='';
2284: }
2285: }
2286: if ($found) {
2287: #
2288: # Return what was cached
2289: #
2290: @exportarr=split(/\_\_\_\;\_\_\_/,$found);
2291: } else {
2292: #
2293: # Not cached
2294: #
1.105 matthew 2295: my ($thissheet,$sheetdata)=&makenewsheet($uname,$udom,$stype,$usymb);
1.107 ! matthew 2296: &readsheet($thissheet,$sheetdata,$fn);
1.105 matthew 2297: &updatesheet($thissheet,$sheetdata);
2298: &loadrows($thissheet,$sheetdata);
2299: &calcsheet($thissheet,$sheetdata);
2300: @exportarr=&exportdata($thissheet,$sheetdata);
1.104 matthew 2301: #
2302: # Store now
2303: #
2304: my $cid=$ENV{'request.course.id'};
2305: my $current='';
2306: if ($stype eq 'studentcalc') {
2307: $current=&Apache::lonnet::reply('get:'.
2308: $ENV{'course.'.$cid.'.domain'}.':'.
2309: $ENV{'course.'.$cid.'.num'}.
2310: ':nohist_calculatedsheets:'.
2311: &Apache::lonnet::escape($key),
2312: $ENV{'course.'.$cid.'.home'});
2313: } else {
1.105 matthew 2314: $current=&Apache::lonnet::reply('get:'.$sheetdata->{'udom'}.':'.
2315: $sheetdata->{'uname'}.
1.104 matthew 2316: ':nohist_calculatedsheets_'.
2317: $ENV{'request.course.id'}.':'.
2318: &Apache::lonnet::escape($key),
1.105 matthew 2319: $sheetdata->{'uhome'});
1.104 matthew 2320: }
2321: my %currentlystored=();
2322: unless ($current=~/^error\:/) {
2323: foreach (split(/___&\___/,&Apache::lonnet::unescape($current))) {
2324: my ($name,$value)=split(/___=___/,$_);
2325: $currentlystored{$name}=$value;
2326: }
2327: }
2328: $currentlystored{$fn}=join('___;___',@exportarr);
2329: #
2330: my $newstore='';
2331: foreach (keys(%currentlystored)) {
2332: if ($newstore) { $newstore.='___&___'; }
2333: $newstore.=$_.'___=___'.$currentlystored{$_};
2334: }
2335: my $now=time;
2336: if ($stype eq 'studentcalc') {
2337: &Apache::lonnet::reply('put:'.
2338: $ENV{'course.'.$cid.'.domain'}.':'.
2339: $ENV{'course.'.$cid.'.num'}.
2340: ':nohist_calculatedsheets:'.
2341: &Apache::lonnet::escape($key).'='.
2342: &Apache::lonnet::escape($newstore).'&'.
2343: &Apache::lonnet::escape($key).'.time='.$now,
2344: $ENV{'course.'.$cid.'.home'});
2345: } else {
2346: &Apache::lonnet::reply('put:'.
1.105 matthew 2347: $sheetdata->{'udom'}.':'.
2348: $sheetdata->{'uname'}.
1.104 matthew 2349: ':nohist_calculatedsheets_'.
2350: $ENV{'request.course.id'}.':'.
2351: &Apache::lonnet::escape($key).'='.
2352: &Apache::lonnet::escape($newstore).'&'.
2353: &Apache::lonnet::escape($key).'.time='.$now,
1.105 matthew 2354: $sheetdata->{'uhome'});
1.104 matthew 2355: }
1.78 matthew 2356: }
1.104 matthew 2357: return @exportarr;
1.44 www 2358: }
1.104 matthew 2359:
1.48 www 2360: # ============================================================ Expiration Dates
2361: #
2362: # Load previously cached student spreadsheets for this course
2363: #
2364: sub expirationdates {
2365: undef %expiredates;
2366: my $cid=$ENV{'request.course.id'};
2367: my $reply=&Apache::lonnet::reply('dump:'.
2368: $ENV{'course.'.$cid.'.domain'}.':'.
2369: $ENV{'course.'.$cid.'.num'}.
2370: ':nohist_expirationdates',
2371: $ENV{'course.'.$cid.'.home'});
2372: unless ($reply=~/^error\:/) {
1.78 matthew 2373: foreach (split(/\&/,$reply)) {
1.48 www 2374: my ($name,$value)=split(/\=/,$_);
2375: $expiredates{&Apache::lonnet::unescape($name)}
2376: =&Apache::lonnet::unescape($value);
1.78 matthew 2377: }
1.48 www 2378: }
2379: }
1.44 www 2380:
2381: # ===================================================== Calculated sheets cache
2382: #
1.46 www 2383: # Load previously cached student spreadsheets for this course
1.44 www 2384: #
2385:
1.46 www 2386: sub cachedcsheets {
1.44 www 2387: my $cid=$ENV{'request.course.id'};
2388: my $reply=&Apache::lonnet::reply('dump:'.
2389: $ENV{'course.'.$cid.'.domain'}.':'.
2390: $ENV{'course.'.$cid.'.num'}.
2391: ':nohist_calculatedsheets',
2392: $ENV{'course.'.$cid.'.home'});
2393: unless ($reply=~/^error\:/) {
1.78 matthew 2394: foreach ( split(/\&/,$reply)) {
1.44 www 2395: my ($name,$value)=split(/\=/,$_);
2396: $oldsheets{&Apache::lonnet::unescape($name)}
2397: =&Apache::lonnet::unescape($value);
1.78 matthew 2398: }
1.44 www 2399: }
1.28 www 2400: }
2401:
1.46 www 2402: # ===================================================== Calculated sheets cache
2403: #
2404: # Load previously cached assessment spreadsheets for this student
2405: #
2406:
2407: sub cachedssheets {
2408: my ($sname,$sdom,$shome)=@_;
2409: unless (($loadedcaches{$sname.'_'.$sdom}) || ($shome eq 'no_host')) {
2410: my $cid=$ENV{'request.course.id'};
2411: my $reply=&Apache::lonnet::reply('dump:'.$sdom.':'.$sname.
2412: ':nohist_calculatedsheets_'.
2413: $ENV{'request.course.id'},
2414: $shome);
2415: unless ($reply=~/^error\:/) {
1.78 matthew 2416: foreach ( split(/\&/,$reply)) {
1.46 www 2417: my ($name,$value)=split(/\=/,$_);
2418: $oldsheets{&Apache::lonnet::unescape($name)}
2419: =&Apache::lonnet::unescape($value);
1.78 matthew 2420: }
1.46 www 2421: }
2422: $loadedcaches{$sname.'_'.$sdom}=1;
2423: }
2424: }
2425:
2426: # ===================================================== Calculated sheets cache
2427: #
2428: # Load previously cached assessment spreadsheets for this student
2429: #
2430:
1.12 www 2431: # ================================================================ Main handler
1.28 www 2432: #
2433: # Interactive call to screen
2434: #
2435: #
2436:
1.3 www 2437:
2438: sub handler {
1.7 www 2439: my $r=shift;
1.28 www 2440: if ($r->header_only) {
1.104 matthew 2441: $r->content_type('text/html');
2442: $r->send_http_header;
2443: return OK;
2444: }
2445: # Global directory configs
1.106 matthew 2446: $includedir = $r->dir_config('lonIncludes');
2447: $tmpdir = $r->dir_config('lonDaemons').'/tmp/';
1.104 matthew 2448: # Needs to be in a course
1.106 matthew 2449: if (! $ENV{'request.course.fn'}) {
2450: # Not in a course, or not allowed to modify parms
2451: $ENV{'user.error.msg'}=
2452: $r->uri.":opa:0:0:Cannot modify spreadsheet";
2453: return HTTP_NOT_ACCEPTABLE;
2454: }
2455: # Get query string for limited number of parameters
2456: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
2457: ['uname','udom','usymb','ufn']);
2458: if (($ENV{'form.usymb'}=~/^\_(\w+)/) && (!$ENV{'form.ufn'})) {
2459: $ENV{'form.ufn'}='default_'.$1;
2460: }
2461: # Interactive loading of specific sheet?
2462: if (($ENV{'form.load'}) && ($ENV{'form.loadthissheet'} ne 'Default')) {
2463: $ENV{'form.ufn'}=$ENV{'form.loadthissheet'};
2464: }
2465: #
2466: # Determine the user name and domain for the sheet.
2467: my $aname;
2468: my $adom;
2469: unless ($ENV{'form.uname'}) {
2470: $aname=$ENV{'user.name'};
2471: $adom=$ENV{'user.domain'};
2472: } else {
2473: $aname=$ENV{'form.uname'};
2474: $adom=$ENV{'form.udom'};
2475: }
2476: #
2477: # Open page
2478: $r->content_type('text/html');
2479: $r->header_out('Cache-control','no-cache');
2480: $r->header_out('Pragma','no-cache');
2481: $r->send_http_header;
2482: # Screen output
2483: $r->print('<html><head><title>LON-CAPA Spreadsheet</title>');
2484: $r->print(<<ENDSCRIPT);
1.10 www 2485: <script language="JavaScript">
2486:
2487: function celledit(cn,cf) {
2488: var cnf=prompt(cn,cf);
1.86 matthew 2489: if (cnf!=null) {
2490: document.sheet.unewfield.value=cn;
1.10 www 2491: document.sheet.unewformula.value=cnf;
2492: document.sheet.submit();
2493: }
2494: }
2495:
1.55 www 2496: function changesheet(cn) {
2497: document.sheet.unewfield.value=cn;
2498: document.sheet.unewformula.value='changesheet';
2499: document.sheet.submit();
2500: }
2501:
1.92 www 2502: function insertrow(cn) {
2503: document.sheet.unewfield.value='insertrow';
2504: document.sheet.unewformula.value=cn;
2505: document.sheet.submit();
2506: }
2507:
1.10 www 2508: </script>
2509: ENDSCRIPT
1.106 matthew 2510: $r->print('</head>'.&Apache::loncommon::bodytag('Grades Spreadsheet').
2511: '<form action="'.$r->uri.'" name=sheet method=post>');
2512: $r->print(&hiddenfield('uname',$ENV{'form.uname'}).
2513: &hiddenfield('udom',$ENV{'form.udom'}).
2514: &hiddenfield('usymb',$ENV{'form.usymb'}).
2515: &hiddenfield('unewfield','').
2516: &hiddenfield('unewformula',''));
2517: $r->rflush();
2518: #
2519: # Full recalc?
2520: if ($ENV{'form.forcerecalc'}) {
2521: $r->print('<h4>Completely Recalculating Sheet ...</h4>');
2522: undef %spreadsheets;
2523: undef %courserdatas;
2524: undef %userrdatas;
2525: undef %defaultsheets;
2526: undef %updatedata;
2527: }
2528: # Read new sheet or modified worksheet
2529: $r->uri=~/\/(\w+)$/;
2530: my ($asheet,$asheetdata)=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
2531: #
2532: # If a new formula had been entered, go from work copy
2533: if ($ENV{'form.unewfield'}) {
2534: $r->print('<h2>Modified Workcopy</h2>');
2535: $ENV{'form.unewformula'}=~s/\'/\"/g;
2536: $r->print('<p>New formula: '.$ENV{'form.unewfield'}.'='.
2537: $ENV{'form.unewformula'}.'<p>');
2538: &setfilename($asheet,$ENV{'form.ufn'});
2539: &tmpread($asheet,$ENV{'form.unewfield'},$ENV{'form.unewformula'});
2540: } elsif ($ENV{'form.saveas'}) {
2541: &setfilename($asheet,$ENV{'form.ufn'});
2542: &tmpread($asheet);
2543: } else {
1.107 ! matthew 2544: &readsheet($asheet,$asheetdata,$ENV{'form.ufn'});
1.106 matthew 2545: }
2546: # Print out user information
2547: unless ($asheetdata->{'sheettype'} eq 'classcalc') {
2548: $r->print('<p><b>User:</b> '.$asheetdata->{'uname'}.
2549: '<br><b>Domain:</b> '.$asheetdata->{'udom'});
2550: if (&getcsec($asheet) eq '-1') {
2551: $r->print('<h3><font color=red>'.
2552: 'Not a student in this course</font></h3>');
1.30 www 2553: } else {
1.106 matthew 2554: $r->print('<br><b>Section/Group:</b> '.$asheetdata->{'csec'});
2555: }
2556: if ($ENV{'form.usymb'}) {
2557: $r->print('<br><b>Assessment:</b> <tt>'.
2558: $ENV{'form.usymb'}.'</tt>');
1.30 www 2559: }
1.106 matthew 2560: }
2561: #
2562: # Check user permissions
2563: if (($asheetdata->{'sheettype'} eq 'classcalc' ) ||
2564: ($asheetdata->{'uname'} ne $ENV{'user.name'} ) ||
2565: ($asheetdata->{'udom'} ne $ENV{'user.domain'})) {
2566: unless (&Apache::lonnet::allowed('vgr',$asheetdata->{'cid'})) {
2567: $r->print('<h1>Access Permission Denied</h1>'.
2568: '</form></body></html>');
2569: return OK;
2570: }
2571: }
2572: # Additional options
2573: $r->print('<br />'.
2574: '<input type="submit" name="forcerecalc" '.
2575: 'value="Completely Recalculate Sheet"><p>');
2576: if ($asheetdata->{'sheettype'} eq 'assesscalc') {
2577: $r->print('<p><font size=+2>'.
2578: '<a href="/adm/studentcalc?'.
2579: 'uname='.$asheetdata->{'uname'}.
2580: '&udom='.$asheetdata->{'udom'}.'">'.
2581: 'Level up: Student Sheet</a></font><p>');
2582: }
2583: if (($asheetdata->{'sheettype'} eq 'studentcalc') &&
2584: (&Apache::lonnet::allowed('vgr',$asheetdata->{'cid'}))) {
2585: $r->print ('<p><font size=+2><a href="/adm/classcalc">'.
2586: 'Level up: Course Sheet</a></font><p>');
2587: }
2588: # Save dialog
2589: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2590: my $fname=$ENV{'form.ufn'};
2591: $fname=~s/\_[^\_]+$//;
2592: if ($fname eq 'default') { $fname='course_default'; }
2593: $r->print('<input type=submit name=saveas value="Save as ...">'.
2594: '<input type=text size=20 name=newfn value="'.$fname.'">'.
2595: 'make default: <input type=checkbox name="makedefufn"><p>');
2596: }
2597: $r->print(&hiddenfield('ufn',&getfilename($asheet)));
2598: # Load dialog
2599: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2600: $r->print('<p><input type=submit name=load value="Load ...">'.
2601: '<select name="loadthissheet">'.
2602: '<option name="default">Default</option>');
2603: foreach (&othersheets($asheet,$asheetdata->{'sheettype'})) {
2604: $r->print('<option name="'.$_.'"');
2605: if ($ENV{'form.ufn'} eq $_) {
2606: $r->print(' selected');
1.104 matthew 2607: }
1.106 matthew 2608: $r->print('>'.$_.'</option>');
2609: }
2610: $r->print('</select><p>');
2611: if (&gettype($asheet) eq 'studentcalc') {
2612: &setothersheets($asheet,&othersheets($asheet,'assesscalc'));
1.104 matthew 2613: }
1.106 matthew 2614: }
2615: # Cached sheets
2616: &expirationdates();
2617: undef %oldsheets;
2618: undef %loadedcaches;
2619: if ($asheetdata->{'sheettype'} eq 'classcalc') {
2620: $r->print("Loading previously calculated student sheets ...\n");
1.104 matthew 2621: $r->rflush();
1.106 matthew 2622: &cachedcsheets();
2623: } elsif ($asheetdata->{'sheettype'} eq 'studentcalc') {
2624: $r->print("Loading previously calculated assessment sheets ...\n");
1.46 www 2625: $r->rflush();
1.106 matthew 2626: &cachedssheets($asheetdata->{'uname'},$asheetdata->{'udom'},
2627: $asheetdata->{'uhome'});
2628: }
2629: # Update sheet, load rows
2630: $r->print("Loaded sheet(s), updating rows ...<br>\n");
2631: $r->rflush();
2632: #
2633: &updatesheet($asheet,$asheetdata);
2634: $r->print("Updated rows, loading row data ...\n");
2635: $r->rflush();
2636: #
2637: &loadrows($asheet,$asheetdata,$r);
2638: $r->print("Loaded row data, calculating sheet ...<br>\n");
2639: $r->rflush();
2640: #
2641: my $calcoutput=&calcsheet($asheet);
2642: $r->print('<h3><font color=red>'.$calcoutput.'</h3></font>');
2643: # See if something to save
2644: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
2645: my $fname='';
2646: if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
2647: $fname=~s/\W/\_/g;
2648: if ($fname eq 'default') { $fname='course_default'; }
2649: $fname.='_'.$asheetdata->{'sheettype'};
2650: &setfilename($asheet,$fname);
2651: $ENV{'form.ufn'}=$fname;
2652: $r->print('<p>Saving spreadsheet: '.
2653: &writesheet($asheet,$ENV{'form.makedefufn'}).'<p>');
1.104 matthew 2654: }
1.106 matthew 2655: }
2656: #
2657: #Write the modified worksheet
2658: $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
2659: &tmpwrite($asheet);
1.105 matthew 2660: if ($asheetdata->{'sheettype'} eq 'studentcalc') {
1.106 matthew 2661: $r->print('<br>Show rows with empty A column: ');
1.62 www 2662: } else {
2663: $r->print('<br>Show empty rows: ');
2664: }
1.106 matthew 2665: #
1.77 www 2666: $r->print(&hiddenfield('userselhidden','true').
1.106 matthew 2667: '<input type="checkbox" name="showall" onClick="submit()"');
2668: #
1.77 www 2669: if ($ENV{'form.showall'}) {
1.106 matthew 2670: $r->print(' checked');
1.77 www 2671: } else {
1.106 matthew 2672: unless ($ENV{'form.userselhidden'}) {
2673: unless
2674: ($ENV{'course.'.$ENV{'request.course.id'}.'.hideemptyrows'} eq 'yes') {
2675: $r->print(' checked');
2676: $ENV{'form.showall'}=1;
2677: }
2678: }
1.77 www 2679: }
1.61 www 2680: $r->print('>');
1.106 matthew 2681: #
2682: # CSV format checkbox (classcalc sheets only)
1.105 matthew 2683: if ($asheetdata->{'sheettype'} eq 'classcalc') {
1.106 matthew 2684: $r->print(' Output CSV format: <input type="checkbox" '.
2685: 'name="showcsv" onClick="submit()"');
2686: if ($ENV{'form.showcsv'}) { $r->print(' checked'); }
2687: $r->print('>');
1.69 www 2688: }
1.106 matthew 2689: #
2690: # Buttons to insert rows
1.98 matthew 2691: $r->print(' Student Status: '.
2692: &Apache::lonhtmlcommon::StatusOptions
2693: ($ENV{'form.Status'},'sheet'));
1.106 matthew 2694: $r->print(<<ENDINSERTBUTTONS);
1.92 www 2695: <br>
2696: <input type='button' onClick='insertrow("top");'
2697: value='Insert Row Top'>
2698: <input type='button' onClick='insertrow("bottom");'
2699: value='Insert Row Bottom'><br>
2700: ENDINSERTBUTTONS
1.106 matthew 2701: # Print out sheet
2702: &outsheet($r,$asheet,$asheetdata);
1.10 www 2703: $r->print('</form></body></html>');
1.106 matthew 2704: # Done
1.3 www 2705: return OK;
1.1 www 2706: }
2707:
2708: 1;
2709: __END__
1.77 www 2710:
2711:
2712:
2713:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>