[LON-CAPA-cvs] cvs: loncom /enrollment Autoupdate.pl localenroll.pm

raeburn lon-capa-cvs@mail.lon-capa.org
Mon, 26 Feb 2007 20:52:55 -0000


This is a MIME encoded message

--raeburn1172523175
Content-Type: text/plain

raeburn		Mon Feb 26 15:52:55 2007 EDT

  Modified files:              
    /loncom/enrollment	Autoupdate.pl localenroll.pm 
  Log:
  Support automated update of instiutional users in a domain, based on domain preferences.
  Autoupdate.pl --
  - Institutional data now gathered for all users in a single call to localenroll.
  - Correction to check to see if Autoupdate should run
  - Accumulate updateable fields for all institutional types with which user is affiliated
  - Use modifyuser() in lonnet to store updated user information
  - affiliations_check() checks for changes in institutional status
  
  localenroll.pm --
  - correct documentation for instcodes hash reference
  - allusers_info() - retrieve institutional user data for all users.
  - get_userinfo() - retrieve institutional data for a single username or ID
  - inst_usertypes() - provide institutional types (short and long titles).
  
  
--raeburn1172523175
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20070226155255.txt"

Index: loncom/enrollment/Autoupdate.pl
diff -u loncom/enrollment/Autoupdate.pl:1.1 loncom/enrollment/Autoupdate.pl:1.2
--- loncom/enrollment/Autoupdate.pl:1.1	Thu Feb 15 20:37:54 2007
+++ loncom/enrollment/Autoupdate.pl	Mon Feb 26 15:52:55 2007
@@ -27,10 +27,9 @@
     use strict;
     use lib '/home/httpd/lib/perl';
     use localenroll;
-    use LONCAPA::Configuration;
-    use LONCAPA::Enrollment;
     use Apache::lonnet;
     use Apache::loncommon;
+    use LONCAPA;
 
     my @info = ('inststatus','lockedname','lastname','firstname','id');
     # find out which users we need to examine
@@ -43,12 +42,15 @@
         my $settings;
         if (ref($domconfig{'autoupdate'}) eq 'HASH') {
             $settings = $domconfig{'autoupdate'};
-            if ($settings->{'run'} eq 'yes') {
+            if ($settings->{'run'} eq '1') {
                 $run_update = 1;
             }
         }
         next if (!$run_update);
-        my %users;
+        # get user information
+        my (%instusers,%instids);
+        next if (&localenroll::allusers_info($dom,\%instusers,\%instids) ne 'ok');
+        my (%users,%unamechg,%possnames);
         my @types = ('active','future');
         my @roles = ('st');
         my @cdoms = ($dom);
@@ -56,19 +58,38 @@
         &descend_tree($dir,0,\%users);
         foreach my $uname (keys(%users)) {
             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
+            my (@inststatuses);
+            if (!$userhash{'internalname'}) {
+                if (defined($instusers{$uname})) {
+                    (my $insttypechg,@inststatuses) = 
+                       &affiliations_check(\%userhash,$instusers{$uname});
+                    if ($insttypechg) {
+                        my $inststatusstr = join(':',&escape(@inststatuses));
+                        my %statushash = ( inststatus => $inststatusstr );
+                        my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
+                    }
+                }
+            }
             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
-                my %userinfo = &localenroll::get_userinfo($dom,$uname,%userhash);
-                if (keys(%userinfo) > 0) {
-                    my @fields = @{$settings->{'default'}};
-                    if ($userhash{'inststatus'} ne '') {
-                        if (ref($settings->{$userhash{'inststatus'}}) eq 'ARRAY') {
-                            @fields = @{$settings->{$userhash{'inststatus'}}};
+                if (defined($instusers{$uname})) {
+                    my (@fields,%changes,$changed);
+                    if (@inststatuses > 0) {
+                        foreach my $type (@inststatuses) {
+                            if (ref($settings->{fields}{$type}) eq 'ARRAY') {
+                                foreach my $field (@{$settings->{fields}{$type}}) {
+                                    if (!grep(/^\Q$field\E$/,@fields)) {
+                                        push(@fields,$field);
+                                    }
+                                }
+                            } 
+                        }
+                    } else {
+                        if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
+                            @fields = @{$settings->{fields}{'default'}};
                         }
                     }
-                    my %changes;
-                    my $changed;
                     foreach my $field (@fields) { 
-                        if ($userhash{$field} ne $userinfo{$field}) {
+                        if ($userhash{$field} ne $instusers{$uname}{$field}) {
                             $changed = 1;
                             if ($settings->{'classlists'} eq 'yes') { 
                                 if ($field eq 'id') {
@@ -83,11 +104,10 @@
                     if ($changed) {
                         my %userupdate;
                         foreach my $field (@fields) {
-                            $userupdate{$field} = $userinfo{$field};
+                            $userupdate{$field} = $instusers{$uname}{$field};
                         }
-                        my $putresult = &Apache::lonnet::put
-                                       ('environment',\%userupdate,$dom,$uname);
-                        if ($putresult eq 'ok') {
+                        my $modresult = &Apache::lonnet::modifyuser($dom,$uname,$userupdate{'id'},undef,undef,$userupdate{'firstname'},$userupdate{'middlename'},$userupdate{'lastname'},$userupdate{'generation'},1);
+                        if ($modresult eq 'ok') {
                             if ($settings->{'classlists'} eq 'yes') {
                                 if ($changes{'id'} || $changes{'fullname'}) {
                                     my %roleshash = 
@@ -104,6 +124,21 @@
                             }
                         }
                     }
+                } else {
+                    # check if the username has changed
+                    if (defined($instids{$userhash{'id'}})) {
+                        if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
+                            foreach my $name (@{$instids{$userhash{'id'}}}) {
+                                if (!exists($users{$name})) {
+                                    push(@{$possnames{$uname}},$name);
+                                }
+                            }
+                        } else {
+                            if (!exists($users{$instids{$userhash{'id'}}})) {
+                                $unamechg{$uname} = $instids{$userhash{'id'}};
+                            }
+                        }
+                    }
                 }
             }
         }
@@ -165,3 +200,27 @@
     }
 }
 
+sub affiliations_check {
+    my ($userhash,$insthashref) = @_;
+    my (@inststatuses,$insttypechg);;
+    if (ref($insthashref) eq 'HASH') {
+        if (ref($insthashref->{type}) eq 'ARRAY') {
+            @inststatuses = @{$insthashref->{type}};
+        }
+    }
+    my @currstatuses = &unescape(split(/:/,$userhash->{'inststatus'}));
+    foreach my $status (@inststatuses) {
+        if (!grep/^\Q$status\E/,@currstatuses) {
+            $insttypechg = 1;
+        }
+    }
+    if (!$insttypechg) {
+        foreach my $status (@currstatuses) {
+            if (!grep/^\Q$status\E/,@inststatuses) {
+                $insttypechg = 1;
+            }
+        }
+    }
+    return ($insttypechg,@inststatuses);
+}
+
Index: loncom/enrollment/localenroll.pm
diff -u loncom/enrollment/localenroll.pm:1.17 loncom/enrollment/localenroll.pm:1.18
--- loncom/enrollment/localenroll.pm:1.17	Mon Oct 16 15:18:19 2006
+++ loncom/enrollment/localenroll.pm	Mon Feb 26 15:52:55 2007
@@ -1,6 +1,6 @@
 # functions to glue school database system into Lon-CAPA for 
 # automated enrollment
-# $Id: localenroll.pm,v 1.17 2006/10/16 19:18:19 raeburn Exp $
+# $Id: localenroll.pm,v 1.18 2007/02/26 20:52:55 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -304,7 +304,7 @@
 #
 # Incoming data:
 # $dom (domain)
-# $$instcodes{'43551dedcd43febmsul1'} = 'Title of course' (hash of courseIDs)
+# $$instcodes{'43551dedcd43febmsul1'} = 'fs03nop590' (hash of courseIDs)
 # 
 # fs03nop590 would be split as follows
 # @{$codetitles} = ("year","semester","department","number")
@@ -474,6 +474,95 @@
     return 'ok';
 }
 
+###############################
+# sub allusers_info
+#
+# Incoming data: three arguments
+# (a) $dom - domain
+# (b) $instusers - reference to hash which will contain hashes, 
+#                 where keys will be usernames and value will be a 
+#                 hash of user information. Keys in the inner hash 
+#                 will be some or all of: lastname,firstname,
+#                 middlename, generation, id, inststatus - 
+#                 institutional status (e.g., faculty,staff,student)
+#                 Values are all scalars except inststatus,
+#                 which is an array.
+# (c) $instids - reference to hash which will contain ID numbers. 
+#                keys will be unique IDs (student or faculty/staff ID)
+#                values will be either: scalar (username) or an array 
+#                if a single ID matches multiple usernames.
+# returns 1 parameter - ('ok' or other value).
+# side effects - populates the $instusers and $instids refs to hashes.
+#                with information for all users from all available 
+#                institutional datafeeds.
+#
+###############################
+
+sub allusers_info {
+    my ($dom,$instusers,$instids) = @_;
+    my $outcome = 'ok';
+    return $outcome; 
+}
+
+###############################
+# sub get_userinfo
+#
+# Incoming data: four required arguments and two optional arguments
+# Retrieves institutional data for a single user either by username
+# if $uname is included as second argument, or by ID if $id is 
+# included as a third argument.  Either (b) or (c) must be provided. 
+# (a) $dom - domain
+# (b) $uname - username of user
+# (c) $id - student/faculty ID of user
+# (d) $instusers - reference to hash which will contain info for user
+#                 as key = value; keys will be one or all of:
+#                 lastname,firstname,middlename,generation,id,inststatus -
+#                 institutional status (e.g., faculty,staff,student)
+#                 Values are all scalars except inststatus,
+#                 which is an array.
+# (e) $instids - reference to hash which will contain ID numbers - 
+#                 keys will be unique IDs (student or faculty/staff ID)  
+#                 values will be either: scalar (username) or an array
+#                 if a single ID matches multiple usernames.
+# (f) $types - optional reference to array which contains 
+#              institutional types to check.
+#
+# returns 1 parameter - ('ok' or other value).
+# side effects - populates the $instusers and $instids refs to hashes.
+#                with information for specified username, or specified
+#                id, if fifth argument provided, from all available, or 
+#                specified (e.g., faculty only) institutional datafeeds,
+#                if sixth argument provided.
+###############################
+
+sub get_userinfo {
+    my ($dom,$uname,$id,$instusers,$instids,$types) = @_;
+    my $outcome = 'ok';
+    return $outcome;
+}
+
+###############################
+# sub inst_usertypes 
+#
+# Incoming data: three arguments
+# (a) $dom - domain
+# (b) $usertypes - reference to hash which will contain 
+#                 key = value, where keys are institution 
+#                 affiliation types (e.g., Faculty, Student etc.)
+#                 and values are titles (e.g., Faculty/Academic Staff)
+# (c) $order - reference to array which will contain the order in
+#              which institutional types should be shown
+#              when displaying data tables (e.g., default quotas    
+#              or updateable user fields (see domainprefs.pm) 
+# returns 1 parameter - ('ok' or other value).
+#
+###############################
+
+sub inst_usertypes {
+    my ($dom,$usertypes,$order) = @_;
+    my $outcome = 'ok';
+    return $outcome;
+}
 
 ###############################
 # sub AUTOLOAD

--raeburn1172523175--