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

raeburn raeburn at source.lon-capa.org
Thu Feb 1 10:36:51 EST 2018


raeburn		Thu Feb  1 15:36:51 2018 EDT

  Modified files:              
    /modules/msu	localenroll.pm 
  Log:
  - Add check_instclasses() for more efficient checking of access to enrollment
    data. new_course() accepts $dbh arg to reuse existing database handle.
  
  
Index: modules/msu/localenroll.pm
diff -u modules/msu/localenroll.pm:1.67 modules/msu/localenroll.pm:1.68
--- modules/msu/localenroll.pm:1.67	Wed Jan 31 20:52:37 2018
+++ modules/msu/localenroll.pm	Thu Feb  1 15:36:50 2018
@@ -1,6 +1,6 @@
 # functions to glue school database system into Lon-CAPA for
 # automated enrollment
-# $Id: localenroll.pm,v 1.67 2018/01/31 20:52:37 raeburn Exp $
+# $Id: localenroll.pm,v 1.68 2018/02/01 15:36:50 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -132,10 +132,7 @@
 <students>
 |;
           if ($class =~ m/^([suf]s\d{2})(\w{2,4})(\d{3,4}\w?)(\d{3})$/) {
-              my $sem = $1;
-              my $subj = $2;
-              my $crse = $3;
-              my $sec = $4;
+              my ($sem,$subj,$crse,$sec) = ($1,$2,$3,$4);
               $sem =~tr/a-z/A-Z/;
               $subj =~tr/a-z/A-Z/;
               $crse =~tr/a-z/A-Z/;
@@ -224,7 +221,13 @@
 }
 
 sub new_course {
-    my ($course_id,$owner,$dom,$coowners) = @_;
+    my ($course_id,$owner,$dom,$coowners,$dbh) = @_;
+    my $dbflag = 0;
+    my $disconnect = 1;
+    if (defined($dbh)) {
+        $disconnect = 0;
+        $dbflag = 1;
+    }
     my $outcome = '';
     if ($owner eq '') {
         $outcome = "Inclusion of enrollment could not be established for the course section $course_id because no owner was provided for this LON-CAPA course.";
@@ -242,16 +245,14 @@
             return $outcome;
         }
         if ($course_id =~ m/^([suf]s)(\d{2})(\w{2,4})(\d{3,4}\w?)(\d{3})$/) {
-            my $sem = $1;
-            my $yr = $2;
-            my $subj = $3;
-            my $crse = $4;
-            my $sec = $5;
+            my ($sem,$yr,$subj,$crse,$sec) = ($1,$2,$3,$4,$5);
             my $term = $sem.$yr;
             $term =~tr/a-z/A-Z/;
             $subj =~tr/a-z/A-Z/;
             $crse =~tr/a-z/A-Z/;
-            my ($dbh,$dbflag) = &connect_DB('RO');
+            if (!$dbflag) {
+                ($dbh,$dbflag) = &connect_DB('RO');
+            }
             if ($dbflag) {
 # Check if owner is in CLIFMS for this course
                 my $clifmscount = $dbh->selectrow_array("SELECT count(*) FROM RO_CLIFMS_VIEW WHERE Term_Code='$term' 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' OR Record_Type = '9')");
@@ -355,7 +356,9 @@
                        }
                     }
                 }
-                &disconnect_DB($dbh);
+                if ($disconnect) {
+                    &disconnect_DB($dbh);
+                }
 	    } else {
                 $outcome = "Inclusion of enrollment could not be established, because a connection to the Registrar's database failed.\n";
             }
@@ -604,6 +607,22 @@
     return $outcome.':'.$message;
 }
 
+sub check_instclasses {
+    my ($owners,$cdom,$classes,$validated) = @_;
+    if ((ref($classes) eq 'HASH') && (ref($validated) eq 'HASH')) {
+        my ($dbh,$dbflag) = &connect_DB('RO');
+        if ($dbflag) {
+            foreach my $class (keys(%{$classes})){
+                if (&check_section($class,$owners,$cdom,$dbh) eq 'ok') {
+                    $validated->{$class} = 1;
+                }
+            }
+            &disconnect_DB($dbh);
+        }
+    }
+    return 'ok';
+}
+
 sub create_password {
     my ($authparam,$dom) = @_;
     my $authchk = 'ok';
@@ -826,6 +845,11 @@
     my ($class,$owner,$dom,$dbh) = @_;
     my $sectioncheck;
     my $dbflag = 0;
+    my $disconnect = 1;
+    if (defined($dbh)) {
+        $disconnect = 0;
+        $dbflag = 1;
+    }
     my @owners = split(/,/,$owner);
     foreach my $person (@owners) {
         if ($person =~ /^([^:]+):([^:]+)$/) {
@@ -837,22 +861,32 @@
         }
         if (defined($person) && $person ne '') {
             if ($class =~ m/^([suf]s\d{2})(\w{2,4})(\d{3,4}\w?)(\d{3})$/) {
+                my ($semyr,$subj,$crs,$sec) = ($1,$2,$3,$4);
 # Check if section exists in LONCAPA table.
-                if (!defined($dbh)) {
+                if (!$dbflag) {
                     ($dbh,$dbflag) = &connect_DB('RO');
                     if (!$dbflag) {
                         return $sectioncheck;
                     }
                 }
-                my $outcome = &new_course($class,$person,$dom);
-                if ($outcome eq 'ok') {
+                my $statement = "SELECT count(MSUNetID) FROM LONCAPA ".
+                                "WHERE Term_Code = '$semyr' AND Subj_Code = '$subj' ".
+                                "AND Crse_Code = '$crs' AND Sctn_Code = '$sec' ".
+                                "AND MSUNetID = '$person'";
+                $sectioncheck = $dbh->selectrow_array($statement);
+                if ($sectioncheck == 0) {
+                    my $outcome = &new_course($class,$person,$dom,undef,$dbh);
+                    if ($outcome eq 'ok') {
+                        $sectioncheck = 'ok';
+                    }
+                } elsif ($sectioncheck > 0) {
                     $sectioncheck = 'ok';
-                    last;
                 }
+                last if ($sectioncheck eq 'ok');
             }
         }
     }
-    if ($dbflag) {
+    if (($dbflag) && ($disconnect)) {
         &disconnect_DB($dbh);
     }
     return $sectioncheck;




More information about the LON-CAPA-cvs mailing list