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