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

matthew lon-capa-cvs@mail.lon-capa.org
Tue, 11 Nov 2003 22:11:09 -0000


matthew		Tue Nov 11 17:11:09 2003 EDT

  Modified files:              
    /loncom/interface	loncommon.pm 
  Log:
  Added routines &store_course_settings and &restore_course_settings which
  are designed to take care of 'remembering' form element values.  
  Handles array (multiple select) elements properly.
  
  
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.152 loncom/interface/loncommon.pm:1.153
--- loncom/interface/loncommon.pm:1.152	Mon Nov 10 18:25:52 2003
+++ loncom/interface/loncommon.pm	Tue Nov 11 17:11:09 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.152 2003/11/10 23:25:52 albertel Exp $
+# $Id: loncommon.pm,v 1.153 2003/11/11 22:11:09 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -3094,6 +3094,108 @@
         '&SelectedStudent='.&Apache::lonnet::escape($sname.':'.$sdomain).
         '&chartoutputmode='.HTML::Entities::encode('html, with all links').
        '">'.$linktext.'</a>';
+}
+
+#######################################################
+#######################################################
+
+=pod
+
+=head1 Course Environment Routines
+
+=item &restore_course_settings 
+
+=item &store_course_settings
+
+Restores/Store indicated form parameters from the course environment.
+Will not overwrite existing values of the form parameters.
+
+Inputs: 
+a scalar describing the data (e.g. 'chart', 'problem_analysis')
+
+a hash ref describing the data to be stored.  For example:
+   
+%Save_Parameters = ('Status' => 'scalar',
+    'chartoutputmode' => 'scalar',
+    'chartoutputdata' => 'scalar',
+    'Section' => 'array',
+    'StudentData' => 'array',
+    'Maps' => 'array');
+
+Returns: both routines return nothing
+
+=cut
+
+#######################################################
+#######################################################
+sub store_course_settings {
+    # save to the environment
+    # appenv the same items, just to be safe
+    my $courseid = $ENV{'request.course.id'};
+    my $coursedom = $ENV{'course.'.$courseid.'.domain'};
+    my ($prefix,$Settings) = @_;
+    my %SaveHash;
+    my %AppHash;
+    while (my ($setting,$type) = each(%$Settings)) {
+        my $basename = 'env.internal.'.$prefix.'.'.$setting;
+        my $envname = 'course.'.$courseid.'.'.$basename;
+        if (exists($ENV{'form.'.$setting})) {
+            # Save this value away
+            if ($type eq 'scalar' &&
+                (! exists($ENV{$envname}) || 
+                 $ENV{$envname} ne $ENV{'form.'.$setting})) {
+                $SaveHash{$basename} = $ENV{'form.'.$setting};
+                $AppHash{$envname}   = $ENV{'form.'.$setting};
+            } elsif ($type eq 'array') {
+                my $stored_form;
+                if (ref($ENV{'form.'.$setting})) {
+                    $stored_form = join(',',
+                                        map {
+                                            &Apache::lonnet::escape($_);
+                                        } sort(@{$ENV{'form.'.$setting}}));
+                } else {
+                    $stored_form = 
+                        &Apache::lonnet::escape($ENV{'form.'.$setting});
+                }
+                # Determine if the array contents are the same.
+                if ($stored_form ne $ENV{$envname}) {
+                    $SaveHash{$basename} = $stored_form;
+                    $AppHash{$envname}   = $stored_form;
+                }
+            }
+        }
+    }
+    my $put_result = &Apache::lonnet::put('environment',\%SaveHash,
+                                          $coursedom,
+                                          $ENV{'course.'.$courseid.'.num'});
+    if ($put_result !~ /^(ok|delayed)/) {
+        &Apache::lonnet::logthis('unable to save form parameters, '.
+                                 'got error:'.$put_result);
+    }
+    # Make sure these settings stick around in this session, too
+    &Apache::lonnet::appenv(%AppHash);
+    return;
+}
+
+sub restore_course_settings {
+    my $courseid = $ENV{'request.course.id'};
+    my ($prefix,$Settings) = @_;
+    while (my ($setting,$type) = each(%$Settings)) {
+        next if (exists($ENV{'form.'.$setting}));
+        my $envname = 'course.'.$courseid.'.env.internal.'.$prefix.
+            '.'.$setting;
+        if (exists($ENV{$envname})) {
+            if ($type eq 'scalar') {
+                $ENV{'form.'.$setting} = $ENV{$envname};
+            } elsif ($type eq 'array') {
+                $ENV{'form.'.$setting} = [ 
+                                           map { 
+                                               &Apache::lonnet::unescape($_); 
+                                           } split(',',$ENV{$envname})
+                                           ];
+            }
+        }
+    }
 }
 
 ############################################################