[LON-CAPA-cvs] cvs: loncom /interface lonspreadsheet.pm

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 30 Aug 2002 20:56:08 -0000


This is a MIME encoded message

--matthew1030740968
Content-Type: text/plain

matthew		Fri Aug 30 16:56:08 2002 EDT

  Modified files:              
    /loncom/interface	lonspreadsheet.pm 
  Log:
  Attempt to reduce calls into the safe space by keeping two sets of books.
  Added use of $sheetdata and $asheetdata, which required changes to most
  functions used outside of the safe space.  This code should be considered
  beta (at best).  It runs, presumedly, but I wouldn't use it during a demo.
  
  
--matthew1030740968
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20020830165608.txt"

Index: loncom/interface/lonspreadsheet.pm
diff -u loncom/interface/lonspreadsheet.pm:1.104 loncom/interface/lonspreadsheet.pm:1.105
--- loncom/interface/lonspreadsheet.pm:1.104	Fri Aug 30 15:47:47 2002
+++ loncom/interface/lonspreadsheet.pm	Fri Aug 30 16:56:08 2002
@@ -1,5 +1,5 @@
 #
-# $Id: lonspreadsheet.pm,v 1.104 2002/08/30 19:47:47 matthew Exp $
+# $Id: lonspreadsheet.pm,v 1.105 2002/08/30 20:56:08 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1461,21 +1461,27 @@
 
 sub makenewsheet {
     my ($uname,$udom,$stype,$usymb)=@_;
+    my %sheetdata=();
+    $sheetdata{'uname'} = $uname;
+    $sheetdata{'udom'}  = $udom;
+    $sheetdata{'sheettype'} = $stype;
+    $sheetdata{'usymb'} = $usymb;
+    $sheetdata{'cid'}   = $ENV{'request.course.id'};
+    $sheetdata{'csec'}  = &Apache::lonnet::usection
+                               ($udom,$uname,$ENV{'request.course.id'});
+    $sheetdata{'cfn'}   = $ENV{'request.course.fn'};
+    $sheetdata{'cnum'}  = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
+    $sheetdata{'cdom'}  = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
+    $sheetdata{'chome'} = $ENV{'course.'.$ENV{'request.course.id'}.'.home'};
+    $sheetdata{'uhome'} = &Apache::lonnet::homeserver($uname,$udom);
+    
     my $safeeval=initsheet($stype);
-    $safeeval->reval(
-       '$uname="'.$uname.
-      '";$udom="'.$udom.
-      '";$uhome="'.&Apache::lonnet::homeserver($uname,$udom).
-      '";$sheettype="'.$stype.
-      '";$usymb="'.$usymb.
-      '";$csec="'.&Apache::lonnet::usection($udom,$uname,
-                                            $ENV{'request.course.id'}).
-      '";$cid="'.$ENV{'request.course.id'}.
-      '";$cfn="'.$ENV{'request.course.fn'}.
-      '";$cnum="'.$ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
-      '";$cdom="'.$ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.
-      '";$chome="'.$ENV{'course.'.$ENV{'request.course.id'}.'.home'}.'";');
-    return $safeeval;
+    my $initstring = '';
+    foreach (keys(%sheetdata)) {
+        $initstring.= qq{\$$_="$sheetdata{$_}";};
+    }
+    $safeeval->reval($initstring);
+    return $safeeval,\%sheetdata;
 }
 
 # ------------------------------------------------------------ Save spreadsheet
@@ -1615,14 +1621,14 @@
 ##################################################
 ##################################################
 sub parmval {
-    my ($what,$safeeval)=@_;
-    my $symb  = &getusymb($safeeval);
+    my ($what,$safeeval,$sheetdata)=@_;
+    my $symb  = $sheetdata->{'usymb'};
     unless ($symb) { return ''; }
     #
-    my $cid   = &getcid($safeeval);
-    my $csec  = &getcsec($safeeval);
-    my $uname = &getuname($safeeval);
-    my $udom  = &getudom($safeeval);
+    my $cid   = $sheetdata->{'cid'};
+    my $csec  = $sheetdata->{'csec'};
+    my $uname = $sheetdata->{'uname'};
+    my $udom  = $sheetdata->{'udom'};
     my $result='';
     #
     my ($mapname,$id,$fn)=split(/\_\_\_/,$symb);
@@ -1646,18 +1652,18 @@
     if ($uname) { 
         return $useropt{$courselevelr} if ($useropt{$courselevelr});
         return $useropt{$courselevelm} if ($useropt{$courselevelm});
-        return $useropt{$courselevel} if ($useropt{$courselevel});
+        return $useropt{$courselevel}  if ($useropt{$courselevel});
     }
     # third, check course
     if ($csec) {
         return $courseopt{$seclevelr} if ($courseopt{$seclevelr});
         return $courseopt{$seclevelm} if ($courseopt{$seclevelm});
-        return $courseopt{$seclevel} if ($courseopt{$seclevel});
+        return $courseopt{$seclevel}  if ($courseopt{$seclevel});
     }
     #
     return $courseopt{$courselevelr} if ($courseopt{$courselevelr});
     return $courseopt{$courselevelm} if ($courseopt{$courselevelm});
-    return $courseopt{$courselevel} if ($courseopt{$courselevel});
+    return $courseopt{$courselevel}  if ($courseopt{$courselevel});
     # second, check map parms
     my $thisparm = $parmhash{$symbparm};
     return $thisparm if ($thisparm);
@@ -1894,14 +1900,14 @@
 # ------------------------------------------------ Load data for one assessment
 
 sub loadstudent {
-    my $safeeval=shift;
+    my ($safeeval,$sheetdata)=@_;
     my %c=();
     my %f=&getformulas($safeeval);
-    $cachedassess=&getuname($safeeval).':'.&getudom($safeeval);
+    $cachedassess=$sheetdata->{'uname'}.':'.$sheetdata->{'udom'};
     # Get ALL the student preformance data
-    my @tmp = &Apache::lonnet::dump(&getcid($safeeval),
-                                    &getudom($safeeval),
-                                    &getuname($safeeval),
+    my @tmp = &Apache::lonnet::dump($sheetdata->{'cid'},
+                                    $sheetdata->{'udom'},
+                                    $sheetdata->{'uname'},
                                     undef);
     if ($tmp[0] !~ /^error:/) {
         %cachedstores = @tmp;
@@ -1914,8 +1920,8 @@
         my $row=$1;
         next if (($f{$_}=~/^[\!\~\-]/) || ($row==0));
         my ($usy,$ufn)=split(/__&&&\__/,$f{$_});
-        @assessdata=&exportsheet(&getuname($safeeval),
-                                 &getudom($safeeval),
+        @assessdata=&exportsheet($sheetdata->{'uname'},
+                                 $sheetdata->{'udom'},
                                  'assesscalc',$usy,$ufn);
         my $index=0;
         foreach ('A','B','C','D','E','F','G','H','I','J','K','L','M',
@@ -1943,7 +1949,7 @@
 # --------------------------------------------------- Load data for one student
 
 sub loadcourse {
-    my ($safeeval,$r)=@_;
+    my ($safeeval,$sheetdata,$r)=@_;
     my %c=();
     my %f=&getformulas($safeeval);
     my $total=0;
@@ -2005,16 +2011,16 @@
 # ------------------------------------------------ Load data for one assessment
 
 sub loadassessment {
-    my $safeeval=shift;
+    my ($safeeval,$sheetdata)=@_;
 
-    my $uhome = &getuhome($safeeval);
-    my $uname = &getuname($safeeval);
-    my $udom  = &getudom($safeeval);
-    my $symb  = &getusymb($safeeval);
-    my $cid   = &getcid($safeeval);
-    my $cnum  = &getcnum($safeeval);
-    my $cdom  = &getcdom($safeeval);
-    my $chome = &getchome($safeeval);
+    my $uhome = $sheetdata->{'uhome'};
+    my $uname = $sheetdata->{'uname'};
+    my $udom  = $sheetdata->{'udom'};
+    my $symb  = $sheetdata->{'usymb'};
+    my $cid   = $sheetdata->{'cid'};
+    my $cnum  = $sheetdata->{'cnum'};
+    my $cdom  = $sheetdata->{'cdom'};
+    my $chome = $sheetdata->{'chome'};
 
     my $namespace;
     unless ($namespace=$cid) { return ''; }
@@ -2070,15 +2076,14 @@
     
     unless ($uhome eq 'no_host') { 
         # Get coursedata
-        unless
-            ((time-$courserdatas{$cid.'.last_cache'})<240) {
-                my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
-                                                 ':resourcedata',$chome);
-                if ($reply!~/^error\:/) {
-                    $courserdatas{$cid}=$reply;
-                    $courserdatas{$cid.'.last_cache'}=time;
-                }
+        unless ((time-$courserdatas{$cid.'.last_cache'})<240) {
+            my $reply=&Apache::lonnet::reply('dump:'.$cdom.':'.$cnum.
+                                             ':resourcedata',$chome);
+            if ($reply!~/^error\:/) {
+                $courserdatas{$cid}=$reply;
+                $courserdatas{$cid.'.last_cache'}=time;
             }
+        }
         foreach (split(/\&/,$courserdatas{$cid})) {
             my ($name,$value)=split(/\=/,$_);
             $courseopt{$userprefix.&Apache::lonnet::unescape($name)}=
@@ -2122,7 +2127,7 @@
             next if  ($f{$_}=~/^[\!\~\-]/);
             if ($f{$_}=~/^parameter/) {
                 if ($thisassess{$f{$_}}) {
-                    my $val=&parmval($f{$_},$safeeval);
+                    my $val=&parmval($f{$_},$safeeval,$sheetdata);
                     $c{$_}=$val;
                     $c{$f{$_}}=$val;
                 }
@@ -2170,8 +2175,8 @@
 #
 
 sub updatesheet {
-    my $safeeval=shift;
-    my $stype=&gettype($safeeval);
+    my ($safeeval,$sheetdata)=@_;
+    my $stype=$sheetdata->{'sheettype'};
     if ($stype eq 'classcalc') {
 	return &updateclasssheet($safeeval);
     } else {
@@ -2185,14 +2190,14 @@
 #
 
 sub loadrows {
-    my ($safeeval,$r)=@_;
-    my $stype=&gettype($safeeval);
+    my ($safeeval,$sheetdata,$r)=@_;
+    my $stype=$sheetdata->{'sheettype'};
     if ($stype eq 'classcalc') {
-	&loadcourse($safeeval,$r);
+	&loadcourse($safeeval,$sheetdata,$r);
     } elsif ($stype eq 'studentcalc') {
-        &loadstudent($safeeval);
+        &loadstudent($safeeval,$sheetdata);
     } else {
-        &loadassessment($safeeval);
+        &loadassessment($safeeval,$sheetdata);
     }
 }
 
@@ -2280,12 +2285,12 @@
         #
         # Not cached
         #        
-        my $thissheet=&makenewsheet($uname,$udom,$stype,$usymb);
+        my ($thissheet,$sheetdata)=&makenewsheet($uname,$udom,$stype,$usymb);
         &readsheet($thissheet,$fn);
-        &updatesheet($thissheet);
-        &loadrows($thissheet);
-        &calcsheet($thissheet); 
-        @exportarr=&exportdata($thissheet);
+        &updatesheet($thissheet,$sheetdata);
+        &loadrows($thissheet,$sheetdata);
+        &calcsheet($thissheet,$sheetdata); 
+        @exportarr=&exportdata($thissheet,$sheetdata);
         #
         # Store now
         #
@@ -2299,13 +2304,12 @@
                                             &Apache::lonnet::escape($key),
                                             $ENV{'course.'.$cid.'.home'});
         } else {
-            $current=&Apache::lonnet::reply('get:'.
-                                            &getudom($thissheet).':'.
-                                            &getuname($thissheet).
+            $current=&Apache::lonnet::reply('get:'.$sheetdata->{'udom'}.':'.
+                                            $sheetdata->{'uname'}.
                                             ':nohist_calculatedsheets_'.
                                             $ENV{'request.course.id'}.':'.
                                             &Apache::lonnet::escape($key),
-                                            &getuhome($thissheet));
+                                            $sheetdata->{'uhome'});
         }
         my %currentlystored=();
         unless ($current=~/^error\:/) {
@@ -2333,14 +2337,14 @@
                                    $ENV{'course.'.$cid.'.home'});
         } else {
             &Apache::lonnet::reply('put:'.
-                                   &getudom($thissheet).':'.
-                                   &getuname($thissheet).
+                                   $sheetdata->{'udom'}.':'.
+                                   $sheetdata->{'uname'}.
                                    ':nohist_calculatedsheets_'.
                                    $ENV{'request.course.id'}.':'.
                                    &Apache::lonnet::escape($key).'='.
                                    &Apache::lonnet::escape($newstore).'&'.
                                    &Apache::lonnet::escape($key).'.time='.$now,
-                                   &getuhome($thissheet));
+                                   $sheetdata->{'uhome'});
         }
     }
     return @exportarr;
@@ -2510,7 +2514,8 @@
         }
         # Read new sheet or modified worksheet
         $r->uri=~/\/(\w+)$/;
-        my $asheet=&makenewsheet($aname,$adom,$1,$ENV{'form.usymb'});
+        my ($asheet,$asheetdata)=&makenewsheet
+                                      ($aname,$adom,$1,$ENV{'form.usymb'});
         # If a new formula had been entered, go from work copy
         if ($ENV{'form.unewfield'}) {
             $r->print('<h2>Modified Workcopy</h2>');
@@ -2528,14 +2533,14 @@
             &readsheet($asheet,$ENV{'form.ufn'});
         }
         # Print out user information
-        unless (&gettype($asheet) eq 'classcalc') {
-            $r->print('<p><b>User:</b> '.&getuname($asheet).
-                      '<br><b>Domain:</b> '.&getudom($asheet));
+        unless ($asheetdata->{'sheettype'} eq 'classcalc') {
+            $r->print('<p><b>User:</b> '.$asheetdata->{'uname'}.
+                      '<br><b>Domain:</b> '.$asheetdata->{'udom'});
             if (&getcsec($asheet) eq '-1') {
                 $r->print('<h3><font color=red>'.
                           'Not a student in this course</font></h3>');
             } else {
-                $r->print('<br><b>Section/Group:</b> '.&getcsec($asheet));
+                $r->print('<br><b>Section/Group:</b> '.$asheetdata->{'csec'});
             }
             if ($ENV{'form.usymb'}) {
                 $r->print('<br><b>Assessment:</b> <tt>'.
@@ -2543,10 +2548,10 @@
             }
         }
         # See if user can see this
-        if ((&gettype($asheet) eq 'classcalc') || 
-            (&getuname($asheet) ne $ENV{'user.name'}) ||
-            (&getudom($asheet) ne $ENV{'user.domain'})) {
-            unless (&Apache::lonnet::allowed('vgr',&getcid($asheet))) {
+        if (($asheetdata->{'sheettype'} eq 'classcalc'       ) || 
+            ($asheetdata->{'uname'}     ne $ENV{'user.name'} ) ||
+            ($asheetdata->{'udom'}      ne $ENV{'user.domain'})) {
+            unless (&Apache::lonnet::allowed('vgr',$asheetdata->{'cid'})) {
                 $r->print('<h1>Access Permission Denied</h1>'.
                           '</form></body></html>');
                 return OK;
@@ -2556,13 +2561,15 @@
         $r->print(
  '<input type=submit name=forcerecalc value="Completely Recalculate Sheet"><p>'
                   );
-        if (&gettype($asheet) eq 'assesscalc') {
-            $r->print ('<p><font size=+2><a href="/adm/studentcalc?uname='.
-                       &getuname($asheet).'&udom='.&getudom($asheet).'">'.
-                       'Level up: Student Sheet</a></font><p>');
+        if ($asheetdata->{'sheettype'} eq 'assesscalc') {
+            $r->print('<p><font size=+2>'.
+                      '<a href="/adm/studentcalc?'.
+                      'uname='.$asheetdata->{'uname'}.
+                      '&udom='.$asheetdata->{'udom'}.'">'.
+                      'Level up: Student Sheet</a></font><p>');
         }
-        if ((&gettype($asheet) eq 'studentcalc') && 
-            (&Apache::lonnet::allowed('vgr',&getcid($asheet)))) {
+        if (($asheetdata->{'sheettype'} eq 'studentcalc') && 
+            (&Apache::lonnet::allowed('vgr',$asheetdata->{'cid'}))) {
             $r->print (
                        '<p><font size=+2><a href="/adm/classcalc">'.
                        'Level up: Course Sheet</a></font><p>');
@@ -2583,7 +2590,7 @@
             $r->print('<p><input type=submit name=load value="Load ...">'.
                       '<select name="loadthissheet">'.
                       '<option name="default">Default</option>');
-            foreach (&othersheets($asheet,&gettype($asheet))) {
+            foreach (&othersheets($asheet,$asheetdata->{'sheettype'})) {
                 $r->print('<option name="'.$_.'"');
                 if ($ENV{'form.ufn'} eq $_) {
                     $r->print(' selected');
@@ -2599,27 +2606,27 @@
         &expirationdates();
         undef %oldsheets;
         undef %loadedcaches;
-        if (&gettype($asheet) eq 'classcalc') {
+        if ($asheetdata->{'sheettype'} eq 'classcalc') {
             $r->print
                 ("Loading previously calculated student sheets ...<br>\n");
             $r->rflush();
             &cachedcsheets();
-        } elsif (&gettype($asheet) eq 'studentcalc') {
+        } elsif ($asheetdata->{'sheettype'} eq 'studentcalc') {
             $r->print
                 ("Loading previously calculated assessment sheets ...<br>\n");
             $r->rflush();
-            &cachedssheets(&getuname($asheet),&getudom($asheet),
-                           &getuhome($asheet));
+            &cachedssheets($asheetdata->{'uname'},$asheetdata->{'udom'},
+                           $asheetdata->{'uhome'});
         }
         # Update sheet, load rows
         $r->print("Loaded sheet(s), updating rows ...<br>\n");
         $r->rflush();
         #
-        &updatesheet($asheet);
+        &updatesheet($asheet,$asheetdata);
         $r->print("Updated rows, loading row data ...<br>\n");
         $r->rflush();
         #
-        &loadrows($asheet,$r);
+        &loadrows($asheet,$asheetdata,$r);
         $r->print("Loaded row data, calculating sheet ...<br>\n");
         $r->rflush();
         #
@@ -2631,7 +2638,7 @@
             if ($ENV{'form.saveas'} && ($fname=$ENV{'form.newfn'})) {
                 $fname=~s/\W/\_/g;
                 if ($fname eq 'default') { $fname='course_default'; }
-                $fname.='_'.&gettype($asheet);
+                $fname.='_'.$asheetdata->{'sheettype'};
                 &setfilename($asheet,$fname);
                 $ENV{'form.ufn'}=$fname;
                 $r->print('<p>Saving spreadsheet: '.
@@ -2640,11 +2647,11 @@
         }
         #Write the modified worksheet
 
-   $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
+            $r->print('<b>Current sheet:</b> '.&getfilename($asheet).'<p>');
 
-   &tmpwrite($asheet);
+            &tmpwrite($asheet);
 
-    if (&gettype($asheet) eq 'studentcalc') {
+    if ($asheetdata->{'sheettype'} eq 'studentcalc') {
 	$r->print('<br>Show rows with empty A column: ');
     } else {
         $r->print('<br>Show empty rows: ');
@@ -2666,7 +2673,7 @@
     }
     $r->print('>');
 
-    if (&gettype($asheet) eq 'classcalc') {
+    if ($asheetdata->{'sheettype'} eq 'classcalc') {
        $r->print(
    ' Output CSV format: <input type=checkbox name=showcsv onClick="submit()"');
        if ($ENV{'form.showcsv'}) { $r->print(' checked'); }

--matthew1030740968--