[LON-CAPA-cvs] cvs: modules /msu localenroll.pm

raeburn raeburn at source.lon-capa.org
Wed Aug 5 15:54:41 EDT 2015


raeburn		Wed Aug  5 19:54:41 2015 EDT

  Modified files:              
    /modules/msu	localenroll.pm 
  Log:
  - Add &get_multusersinfo() and include some other speed-ups to reduce the
    time taken to retrieve institutional data when adding/enrolling users via 
    file uplood.
  
  
Index: modules/msu/localenroll.pm
diff -u modules/msu/localenroll.pm:1.65 modules/msu/localenroll.pm:1.66
--- modules/msu/localenroll.pm:1.65	Thu Apr 18 16:01:34 2013
+++ modules/msu/localenroll.pm	Wed Aug  5 19:54:41 2015
@@ -1,6 +1,6 @@
 # functions to glue school database system into Lon-CAPA for
 # automated enrollment
-# $Id: localenroll.pm,v 1.65 2013/04/18 16:01:34 raeburn Exp $
+# $Id: localenroll.pm,v 1.66 2015/08/05 19:54:41 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -884,11 +884,60 @@
     return $outcome;
 }
 
+sub get_multusersinfo {
+    my ($dom,$caller,$userhash,$instusers,$instids,$types) = @_;
+    my $outcome;
+    if (ref($userhash) eq 'HASH') { 
+        my ($srchby,$stoponmatch);
+        if ($caller eq 'id') {
+            $srchby = 'id';
+        } else {
+            $srchby = 'uname';
+            $stoponmatch = 1;
+        }
+        my @srchtables;
+        my @tableorder = qw(Student Faculty Staff StuAff Assistant StaffAff);
+        my %tables = (Faculty => 'FACULTY_VU',
+                      Staff => 'STAFF_VU',
+                      Student => 'STUDENT',
+                      Assistant => 'ASSISTANT',
+                      StaffAff => 'AFFILIATE',
+                      StuAff => 'STUDENT_AFFILIATE');
+        my ($dbh,$dbflag) = &connect_DB('HR');
+        if ($dbflag) {
+            if (ref($types) eq 'ARRAY') {
+                foreach my $type (@{$types}) {
+                    if (exists($tables{$type})) {
+                        push(@srchtables,$tables{$type});
+                    }
+                }
+            }
+            if (@srchtables == 0) {
+                foreach my $type (@tableorder) {
+                    if (exists($tables{$type})) {
+                        push(@srchtables,$tables{$type});
+                    }
+                }
+            }
+            foreach my $key (keys(%{$userhash})) {
+                &query_user_tables($dbflag,$dbh,\@srchtables,$instusers,$instids,
+                                   $srchby,$key,undef,$types,$stoponmatch);
+            }
+            &disconnect_DB($dbh);
+        } else {
+            $outcome = 'unavailable';
+        }
+    } else {
+        $outcome = 'invalid';
+    }
+    return $outcome;
+}
+
 sub get_userinfo {
     my ($dom,$uname,$id,$instusers,$instids,$types,
         $srchby,$srchterm,$srchtype) = @_;
-    my $outcome;
-    my @srchtables;
+    my ($outcome,$stoponmatch, at srchtables);
+    my @tableorder = qw(Student Faculty Staff StuAff Assistant StaffAff);
     my %tables = (Faculty => 'FACULTY_VU',
                   Staff => 'STAFF_VU',
                   Student => 'STUDENT',
@@ -902,14 +951,17 @@
         }
     }
     if (@srchtables == 0) {
-        foreach my $type (keys(%tables)) {
-            push(@srchtables,$tables{$type});
+        foreach my $type (@tableorder) {
+            if (exists($tables{$type})) {
+                push(@srchtables,$tables{$type});
+            }
         }
     }
     if ($srchby eq '' && $srchterm eq '') {
         if ($uname ne '') {
             $srchby = 'uname';
             $srchterm = $uname;
+            $stoponmatch = 1;
         } elsif ($id ne '') {
             $srchby = 'id';
             $srchterm = $id;
@@ -917,7 +969,7 @@
     }
     if ($srchterm ne '') {
         $outcome = &query_user_tables($dbflag,$dbh,\@srchtables,$instusers,$instids,
-                                      $srchby,$srchterm,$srchtype,$types);
+                                      $srchby,$srchterm,$srchtype,$types,$stoponmatch);
     }
     if ($dbflag) {
         &disconnect_DB($dbh);
@@ -926,8 +978,8 @@
 }
 
 sub query_user_tables {
-    my ($dbflag,$dbh,$srchtables,$instusers,$instids,$srchby,$srchterm,$srchtype,$types) = @_;
-    my ($outcome,$condition,%multipids,$ldapfilter);
+    my ($dbflag,$dbh,$srchtables,$instusers,$instids,$srchby,$srchterm,$srchtype,$types,$stoponmatch) = @_;
+    my ($outcome,$condition,%multipids,$ldapfilter,$omitldap);
     if ($srchby eq 'uname') {
         if ($srchterm =~ /^\w{2,8}$/) {
             if ($srchtype eq 'contains') {
@@ -939,6 +991,7 @@
             } else {
                 $condition = "WHERE MSUNetID = '$srchterm'";
                 $ldapfilter = '(uid='.$srchterm.')';
+                $omitldap = 1;
             }
         }
     } elsif ($srchby eq 'lastname') {
@@ -1004,8 +1057,10 @@
         }
     }
     if ($dbflag) {
+        my $matched = 0;
         foreach my $table (@{$srchtables}) {
-            next if ($srchby && $condition eq '');   
+            last if ($srchby && $condition eq '');   
+            last if ($stoponmatch && $matched);
             my $statement = "SELECT MSUNetID,Pid,FirstName,LastName,Person_Type FROM $table $condition";
             my $sth = $dbh->prepare("$statement");
             $sth->execute();
@@ -1063,6 +1118,7 @@
                                             permanentemail => $uname.'@msu.edu',
                                             inststatus => [$type],
                                          };
+                    $matched = 1;
                 }
                 if (defined($instids->{$pid})) {
                     if (ref($instids->{$pid}) eq 'ARRAY') {
@@ -1081,6 +1137,9 @@
             $outcome = 'ok';
         }
     }
+    if (($stoponmatch) && ($omitldap) && (exists($instusers->{$srchterm}))) {
+        return $outcome;
+    }
     if ($ldapfilter ne '') {
         my $ldapres = &ldap_search($ldapfilter,$instusers,$types);
         if (!$dbflag) {




More information about the LON-CAPA-cvs mailing list