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

raeburn lon-capa-cvs@mail.lon-capa.org
Mon, 26 Feb 2007 22:12:18 -0000


This is a MIME encoded message

--raeburn1172527938
Content-Type: text/plain

raeburn		Mon Feb 26 17:12:18 2007 EDT

  Modified files:              
    /modules/msu	localenroll.pm 
  Log:
  - connect_DB() arguments reversed.  Second argument made optional; configvars now set within routine, as needed.
  - allusers_info() retrieves institutional data from tables in HR database.
  - get_userinfo() retrieves institutional data for a single user/ID
  - query_user_tables() performs the required database queries.
        if a user has an A-PID and a Z-PID the A-PID is the one returned.
        if a user is affiliated with multiple groups (e.g., Staff and Student,
           all are returned in the inststatus array).
  - inst_usertypes(): institutional types as defined in HR tables (with correspondng descriptions), and order to display in domainprefs screens.   
  
  
--raeburn1172527938
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20070226171218.txt"

Index: modules/msu/localenroll.pm
diff -u modules/msu/localenroll.pm:1.23 modules/msu/localenroll.pm:1.24
--- modules/msu/localenroll.pm:1.23	Wed Nov 15 15:12:27 2006
+++ modules/msu/localenroll.pm	Mon Feb 26 17:12:16 2007
@@ -1,6 +1,6 @@
 # functions to glue school database system into Lon-CAPA for
 # automated enrollment
-# $Id: localenroll.pm,v 1.23 2006/11/15 20:12:27 raeburn Exp $
+# $Id: localenroll.pm,v 1.24 2007/02/26 22:12:16 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -41,7 +41,7 @@
   my ($dom,$affiliatesref,$replyref) = @_;
   my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
  
-  my ($dbh,$dbflag) = &connect_DB($$configvars{'lonDaemons'},'RO');
+  my ($dbh,$dbflag) = &connect_DB('RO',$$configvars{'lonDaemons'});
   if ($dbflag) {
      foreach my $crs (sort keys %{$affiliatesref}) {
          my $xmlstem =  $$configvars{'lonDaemons'}.'/tmp/';
@@ -61,11 +61,15 @@
 }
 
 sub connect_DB {
-    my ($keydir,$type) = @_;
+    my ($type,$keydir) = @_;
     $ENV{SYBASE} = '/usr/local/freetds';
     my $DB_PATH;
     my $DB_USER = '';
     my $DB_PASSWD = '';
+    if ($keydir eq '') {
+        my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
+        $keydir = $$configvars{'lonDaemons'};
+    }
     if (open (my $fh, "<$keydir/autoenroll.dat") ) {
         ($DB_USER,$DB_PASSWD) = split/:/,<$fh>;
         chomp($DB_PASSWD);
@@ -76,6 +80,8 @@
         $DB_PATH = "dbi:Sybase:server=ESDB1;database=RO_ClassList";
     } elsif ($type eq 'SIS') {
         $DB_PATH = "dbi:Sybase:server=msudata;database=SISInfo";
+    } elsif ($type eq 'HR') {
+        $DB_PATH = "dbi:Sybase:server=msudata;database=HR";
     }
 
     my $dbh;
@@ -84,7 +90,7 @@
     if ($@) {
         $dbflag = 0;
     } else {
-        if (defined $dbh) {
+        if (defined($dbh)) {
             $dbflag = 1;
         }
     }
@@ -170,9 +176,7 @@
         $subj =~tr/a-z/A-Z/;
         $crse =~tr/a-z/A-Z/;
         $term =~tr/a-z/A-Z/;
-        my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
-                                                                                                           
-        my ($dbh3,$dbflag) = &connect_DB($$configvars{'lonDaemons'},'SIS');
+        my ($dbh3,$dbflag) = &connect_DB('SIS');
         if ($dbflag) {
             eval {
                 my $quotedsubj = $dbh3->quote($subj);
@@ -211,7 +215,6 @@
             $outcome = "Inclusion of enrollment could not be established for the course section $course_id because the username of the owner contains invalid characters.";
             return $outcome;
         }
-        my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
         if ($course_id =~ m/^([suf]s\d{2})(\w{2,3})(\d{3,4}\w?)(\d{3})$/) {
             my $sem = $1;
             my $subj = $2;
@@ -220,7 +223,7 @@
             $sem =~tr/a-z/A-Z/;
             $subj =~tr/a-z/A-Z/;
             $crse =~tr/a-z/A-Z/;
-            my ($dbh,$dbflag) = &connect_DB($$configvars{'lonDaemons'},'RO');
+            my ($dbh,$dbflag) = &connect_DB('RO');
             if ($dbflag) {
 # Check if instructor is in CLIFMS for this course
                 my $clifmscount = $dbh->selectrow_array("SELECT count(*) FROM RO_CLIFMS_VIEW WHERE Term_Code='$sem' AND Subj_Code='$subj' AND Crse_Code='$crse' AND Sctn_Code='$sec' AND MSUNetID='$owner' AND (Record_Type='1' OR Record_Type='2' OR Record_Type ='3')");
@@ -257,10 +260,7 @@
         $subj =~tr/a-z/A-Z/;
         $crse =~tr/a-z/A-Z/;
         $term =~tr/a-z/A-Z/;
-        
-        my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
-                                                                                                           
-        my ($dbh3,$dbflag) = &connect_DB($$configvars{'lonDaemons'},'SIS');
+        my ($dbh3,$dbflag) = &connect_DB('SIS');
         if ($dbflag) {
             eval {
                 my $quotedsubj = $dbh3->quote($subj);
@@ -336,8 +336,7 @@
     my ($dom,$crs,$affiliates,$result,$action,$students) = @_;
     my $outcome;
     if ($action eq 'update') {
-        my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
-        my ($dbh,$dbflag) = &connect_DB($$configvars{'lonDaemons'},'RO');
+        my ($dbh,$dbflag) = &connect_DB('RO');
         my $owner = &get_owner($dom,$crs);
         if ($dbflag) {
             my %stuinfo;
@@ -439,7 +438,7 @@
         if ($class =~ m/^([suf]s\d{2})(\w{2,3})(\d{3,4}\w?)(\d{3})$/) {
 # Check if section exists in LONCAPA table.
             if (!defined($dbh)) {
-                ($dbh,$dbflag) = &connect_DB($$configvars{'lonDaemons'},'RO');
+                ($dbh,$dbflag) = &connect_DB('RO');
                 if (!$dbflag) {
                     return $sectioncheck;
                 }
@@ -477,6 +476,130 @@
     return 'ok';
 }
 
+sub allusers_info {
+    my ($dom,$instusers,$instids) = @_;
+    my $outcome;
+    my ($dbh,$dbflag) = &connect_DB('HR');
+    if ($dbflag) {
+        my @srchtables = ('FACULTY_VU','STAFF_VU','STUDENT','AFFILIATE',
+                          'ASSISTANT','STUDENT_AFFILIATE');
+        &query_user_tables($dbh,\@srchtables,$instusers,$instids);
+        $outcome = 'ok';
+        &disconnect_DB($dbh);
+    }
+    return $outcome;
+}
+
+sub get_userinfo {
+    my ($dom,$uname,$id,$instusers,$instids,$types) = @_;
+    my $outcome;
+    my @srchtables;
+    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) {
+        foreach my $type (@{$types}) {
+            if (exists($tables{$type})) {
+                push(@srchtables,$tables{$type});           
+            }
+        }
+        if (@srchtables == 0) {
+            foreach my $type (keys(%tables)) {
+                push(@srchtables,$tables{$type});
+            }
+        }
+        &query_user_tables($dbh,\@srchtables,$instusers,$instids,$uname,$id);
+        $outcome = 'ok';
+        &disconnect_DB($dbh);
+    }
+    return $outcome; 
+}
+
+sub query_user_tables  {
+    my ($dbh,$srchtables,$instusers,$instids,$uname,$id) = @_;
+    my ($condition,%multipids);
+    if ($uname =~ /^\w{2,8}$/) {
+        $condition = "WHERE MSUNetID = '$uname'";
+    } elsif ($id =~ /^[AZ]\d{8}$/) {
+        $condition = "WHERE Pid = '$id'";
+    }
+    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 ) {
+            if (ref($instusers->{$uname}) eq 'HASH') {
+                if (ref($instusers->{$uname}{'inststatus'}) eq 'ARRAY') {
+                    if (!grep(/^$type$/,@{$instusers->{$uname}{'inststatus'}})) {
+                        push(@{$instusers->{$uname}{'inststatus'}},$type);
+                    }
+                }
+                if ($pid ne $instusers->{$uname}{'id'}) {
+                    if ($instusers->{$uname}{'id'} =~ /^A\d{8}$/) {
+                        if ($pid =~ /^A\d{8}$/) {
+                            if (ref($multipids{$uname}) eq 'ARRAY') {
+                                if (!grep(/^$pid$/,@{$multipids{$uname}})) {
+                                    push(@{$multipids{$uname}},$pid);
+                                }
+                            } else {
+                                @{$multipids{$uname}} = ($instusers->{$uname}{'id'},$pid);
+                            }
+                            $instusers->{$uname}{'id'} = $pid;
+                        }
+                    } 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);
+                                }
+                            } else {
+                                @{$multipids{$uname}} = ($instusers->{$uname}{'id'},$pid);
+                            }
+                        } elsif ($pid =~ /^A\d{8}$/) {
+                            $instusers->{$uname}{'id'} = $pid;
+                        }
+                    }
+                }
+            } else {
+                $instusers->{$uname} = {firstname => $first,
+                                       lastname => $last,
+                                       id => $pid,
+                                       inststatus => [$type],
+                                     };
+            }
+            if (defined($instids->{$pid})) {
+                if (ref($instids->{$pid}) eq 'ARRAY') {
+                    if (!grep(/^$uname$/,@{$instids->{$pid}})) {
+                        push(@{$instids->{$pid}},$uname);
+                    }
+                } elsif ($instids->{$pid} ne $uname) {
+                    @{$instids->{$pid}} = ($instids->{$pid},$uname);
+                }
+            } else {
+                $instids->{$pid} = $uname;
+            }
+        }
+    }
+    return;
+}
+
+sub inst_usertypes {
+    my ($dom,$usertypes,$order) = @_;
+    my $outcome = 'ok';
+    $usertypes = { 
+                  Faculty => 'Faculty/Academic Staff',
+                  Staff => 'Support Staff',
+                  Student => 'Student',
+                  Assistant => 'Assistant',
+                  StaffAff => 'Affiliate',
+                  StuAff => 'Student Affiliate'
+                 };
+    $order=['Faculty','Staff','Student','Assistant','StaffAff','StuAff'];
+    return $outcome;
+}
+
 ###############################
 # sub AUTOLOAD
 #

--raeburn1172527938--