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

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Wed, 25 Jul 2007 20:29:36 -0000


raeburn		Wed Jul 25 16:29:36 2007 EDT

  Modified files:              
    /modules/msu	localenroll.pm 
  Log:
  Extra arguments ($srchby,$srchterm,$srchtype) added to arguments accepted by get_userinfo.
  
  Same additional arguments propagated to query_user_tables(), to allow searching of institutional directory by username, last name, or last name, first name for $srchterm. (available to loncreateuser - if permitted by domain settings).   
  
  
Index: modules/msu/localenroll.pm
diff -u modules/msu/localenroll.pm:1.25 modules/msu/localenroll.pm:1.26
--- modules/msu/localenroll.pm:1.25	Mon May 14 16:15:26 2007
+++ modules/msu/localenroll.pm	Wed Jul 25 16:29:35 2007
@@ -1,6 +1,6 @@
 # functions to glue school database system into Lon-CAPA for
 # automated enrollment
-# $Id: localenroll.pm,v 1.25 2007/05/14 20:15:26 raeburn Exp $
+# $Id: localenroll.pm,v 1.26 2007/07/25 20:29:35 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -491,7 +491,8 @@
 }
 
 sub get_userinfo {
-    my ($dom,$uname,$id,$instusers,$instids,$types) = @_;
+    my ($dom,$uname,$id,$instusers,$instids,$types,
+        $srchby,$srchterm,$srchtype) = @_;
     my $outcome;
     my @srchtables;
     my %tables = (Faculty => 'FACULTY_VU',
@@ -512,25 +513,72 @@
                 push(@srchtables,$tables{$type});
             }
         }
-        &query_user_tables($dbh,\@srchtables,$instusers,$instids,$uname,$id);
-        $outcome = 'ok';
+        if ($srchby eq '' && $srchterm eq '') {
+            if ($uname ne '') {
+                $searchby = 'uname';
+                $searchterm = $uname;
+            } elsif ($id ne '') {
+                $searchby = 'id';
+                $searchterm = $id;
+            }
+        }
+        if ($searchterm ne '') {
+            &query_user_tables($dbh,\@srchtables,$instusers,$instids,
+                               $srchby,$srchterm,$srchtype);
+            $outcome = 'ok';
+        }
         &disconnect_DB($dbh);
     }
     return $outcome; 
 }
 
-sub query_user_tables  {
-    my ($dbh,$srchtables,$instusers,$instids,$uname,$id) = @_;
+sub query_user_tables {
+    my ($dbh,$srchtables,$instusers,$instids,$srchby,$srchterm,$srchtype) = @_;
     my ($condition,%multipids);
-    if ($uname =~ /^\w{2,8}$/) {
-        $condition = "WHERE MSUNetID = '$uname'";
-    } elsif ($id =~ /^[AZ]\d{8}$/) {
-        $condition = "WHERE Pid = '$id'";
+    if ($srchby eq 'uname') {
+        if ($srchterm =~ /^\w{2,8}$/) {
+            if ($srchtype eq 'contains') {
+                $condition = "WHERE MSUNetID LIKE '%$srchterm%';
+            } else {
+                $condition = "WHERE MSUNetID = '$srchterm'";
+            }
+        }
+    } elsif ($srchby eq 'last') {
+        if ($srchterm =~ /[A-Za-z\-\.']+/) {
+            if ($srchtype eq 'contains') {
+                my $quoted_last = $dbh->quote('%'.$srchterm.'%');
+                $condition = "WHERE LastName LIKE $quotedlast";
+            } else {
+                my $quotedlast = $dbh->quote($srchterm);
+                $condition = "WHERE LastName = $quoted_last";
+            }
+        }
+    } elsif ($srchby eq 'lastfirst') {
+        my ($srchlast,$srchfirst) = split(/,/,$srchterm);
+        if (($srchlast =~ /[A-Za-z\-\.']+/) && 
+            ($srchfirst  =~ /[A-Za-z\-\.']+/)) { 
+            if ($srchtype eq 'contains') {
+                my $quoted_last = $dbh->quote('%'.$srchlast.'%');
+                my $quoted_first = $dbh->quote('%'.$srchfirst.'%');
+                $condition = "WHERE LastName LIKE $quoted_last AND
+                                    FirstName LIKE $quoted_first";
+            } else {
+                my $quotedlast = $dbh->quote($srchterm);
+                my $quotedfirst = $dbh->quote($srchterm);
+                $condition = "WHERE LastName = $quoted_last AND
+                                    FirstName = $quoted_first";
+            }
+        }
+    } elsif ($srchby eq 'id') {
+        if ($srchterm =~ /^[AZ]\d{8}$/) {
+            $condition = "WHERE Pid = '$srchterm'";
+        }
     }
     foreach my $table (@{$srchtables}) {
         my $sth = $dbh->prepare("SELECT MSUNetID,Pid,FirstName,LastName,Person_Type FROM $table $condition");
         $sth->execute();
         while ( my($uname,$pid,$first,$last,$type) = $sth->fetchrow_array ) {
+            $pid=lc($pid);
             if (ref($instusers->{$uname}) eq 'HASH') {
                 if (ref($instusers->{$uname}{'inststatus'}) eq 'ARRAY') {
                     if (!grep(/^$type$/,@{$instusers->{$uname}{'inststatus'}})) {
@@ -552,7 +600,8 @@
                     } elsif ($instusers->{$uname}{'id'} =~ /^Z\d{8}$/) {
                         if ($pid =~ /^Z\d{8}$/) {
                             if (ref($multipids{$uname}) eq 'ARRAY') {
-                                if (!grep(/^$pid$/,@{$multipids{$uname}})) {                                    push(@{$multipids{$uname}},$pid);
+                                if (!grep(/^$pid$/,@{$multipids{$uname}})) {
+                                    push(@{$multipids{$uname}},$pid);
                                 }
                             } else {
                                 @{$multipids{$uname}} = ($instusers->{$uname}{'id'},$pid);
@@ -566,6 +615,7 @@
                 $instusers->{$uname} = {firstname => $first,
                                        lastname => $last,
                                        id => $pid,
+                                       permanentemail => $uname.'@msu.edu',
                                        inststatus => [$type],
                                      };
             }