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