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

raeburn lon-capa-cvs@mail.lon-capa.org
Mon, 28 Aug 2006 20:28:49 -0000


This is a MIME encoded message

--raeburn1156796929
Content-Type: text/plain

raeburn		Mon Aug 28 16:28:49 2006 EDT

  Modified files:              
    /modules/msu	localenroll.pm 
  Log:
  Move section checking to a subroutine to remove code duplication.  Section counts from LONCAPA table now filters based on MSUNetID of courseowner.
  
  
--raeburn1156796929
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20060828162849.txt"

Index: modules/msu/localenroll.pm
diff -u modules/msu/localenroll.pm:1.18 modules/msu/localenroll.pm:1.19
--- modules/msu/localenroll.pm:1.18	Tue Jul 11 08:14:15 2006
+++ modules/msu/localenroll.pm	Mon Aug 28 16:28:47 2006
@@ -1,6 +1,6 @@
 # functions to glue school database system into Lon-CAPA for
 # automated enrollment
-# $Id: localenroll.pm,v 1.18 2006/07/11 12:14:15 raeburn Exp $
+# $Id: localenroll.pm,v 1.19 2006/08/28 20:28:47 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -101,49 +101,35 @@
   $xmlstem .= $dom."_".$crs."_";
   my $pilotcount = 0;
   my $blankcount = 0;
-  my $owner;
+  my $owner = &get_owner($dom,$crs);
   foreach my $class (@{$coursesref}) {
-      if ($class =~ m/^([suf]s\d{2})(\w{2,3})(\d{3,4}\w?)(\d{3})$/) {
-# Check if section exists in LONCAPA table.
-	  my $sectioncheck = $dbh->selectrow_array("SELECT count(*) FROM LONCAPA WHERE Term_Code = '$1' AND Subj_Code = '$2' AND Crse_Code = '$3' AND Sctn_Code = '$4'");
-          if ($sectioncheck == 0) {
-              if ($owner eq '') {
-                  $owner = &get_owner($dom,$crs);
-              }
-              unless ($owner eq '' || !defined($owner)) {
-                  my $outcome = &new_course($class,$owner,$dom);
-                  if ($outcome eq 'ok') {
-                      $sectioncheck = 1;
-                  }
-              } 
-          }
-          if ($sectioncheck > 0) {  
-              my $xmlfile = $xmlstem.$class."_classlist.xml";
-              open(FILE, ">$xmlfile");
-              print FILE qq|<?xml version="1.0" encoding="UTF-8"?>
+      if (&check_section($class,$owner,$dom) > 0) {
+          my $xmlfile = $xmlstem.$class."_classlist.xml";
+          open(FILE, ">$xmlfile");
+          print FILE qq|<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE text>
 <students>
 |;
-              my $sth = $dbh->prepare("SELECT Pid,Pilot_Id,Student_Name FROM LONCAPA_ClassList WHERE Term_Code = '$1' AND Subj_Code = '$2' AND Crse_Code = '$3' AND Sctn_Code = '$4'  ORDER BY Student_Name");
-              $sth->execute();
-              while ( my($pid,$pilot,$name)  = $sth->fetchrow_array ) {
-                  if ($pilot =~ m/^\w{1,8}$/) {
-                      $pilotcount ++;
-                      $name =~ s/^\s//g;
-                      $name =~ s/\s$//g;
-                      my ($last,$given,$first,$middle);
-                      $last = substr($name,0,index($name,","));
-                      $given = substr($name,index($name,",")+1);
-                      $given =~ s/^\s//g;
-                      if ($given =~ m/\w\s\w/) {
-                          $first = substr($given,0,index($given," "));
-                          $middle = substr($given,index($given," ")+1);
-                          $middle =~ s/\s//g;
-                      } else {
-                          $first = $given;
-                      }
-                      $first =~ s/\s$//g;
-                      print FILE qq| <student username="$pilot">
+          my $sth = $dbh->prepare("SELECT Pid,Pilot_Id,Student_Name FROM LONCAPA_ClassList WHERE Term_Code = '$1' AND Subj_Code = '$2' AND Crse_Code = '$3' AND Sctn_Code = '$4'  ORDER BY Student_Name");
+          $sth->execute();
+          while ( my($pid,$pilot,$name)  = $sth->fetchrow_array ) {
+              if ($pilot =~ m/^\w{1,8}$/) {
+                  $pilotcount ++;
+                  $name =~ s/^\s//g;
+                  $name =~ s/\s$//g;
+                  my ($last,$given,$first,$middle);
+                  $last = substr($name,0,index($name,","));
+                  $given = substr($name,index($name,",")+1);
+                  $given =~ s/^\s//g;
+                  if ($given =~ m/\w\s\w/) {
+                      $first = substr($given,0,index($given," "));
+                      $middle = substr($given,index($given," ")+1);
+                      $middle =~ s/\s//g;
+                  } else {
+                      $first = $given;
+                  }
+                  $first =~ s/\s$//g;
+                  print FILE qq| <student username="$pilot">
   <autharg>MSU.EDU</autharg>
   <authtype>krb4</authtype>
   <email>$pilot\@msu.edu</email>
@@ -157,19 +143,18 @@
   <studentID>$pid</studentID>
  </student>
 |;
-                  } elsif ($pilot eq '') {
-                      $blankcount ++;
-                  }
+              } elsif ($pilot eq '') {
+                  $blankcount ++;
+              }
 # Format for startdate is YYYY:MM:DD:HH:MM:SS
 # Format forenddate is YYYY:MM:DD:HH:MM:SS
 # Authentication is one of: krb4, krb5, int or loc
 # Password is either the password for int, or Kerberos domain (for krb4 or krb5), or argument (for loc).
 # If authentication, password, startdate or enddate are blank, the default for the course is used.  These defaults can be modified using the Automated Enrollment Manager.
-              }
-              $sth->finish;
-              print FILE qq|</students>|;
-              close(FILE);
           }
+          $sth->finish;
+          print FILE qq|</students>|;
+          close(FILE);
       }
   }
   return ($pilotcount,$blankcount);
@@ -353,37 +338,19 @@
     if ($action eq 'update') {
         my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
         my ($dbh,$dbflag) = &connect_DB($$configvars{'lonDaemons'},'RO');
-        my $owner;
+        my $owner = &get_owner($dom,$crs);
         if ($dbflag) {
             my %stuinfo;
             foreach my $class (@{$$affiliates{$crs}}) {
-                if ($class =~ m/^([suf]s\d{2})(\w{2,3})(\d{3,4}\w?)(\d{3})$/) {
-# Check if section exists in LONCAPA table.
-                    my $sectioncheck = $dbh->selectrow_array(
-                         "SELECT count(*) FROM LONCAPA ". 
-                         "WHERE Term_Code = '$1' AND Subj_Code = '$2' ". 
-                         "AND Crse_Code = '$3' AND Sctn_Code = '$4'");
-                    if ($sectioncheck == 0) {
-                        unless ($owner eq '') {
-                            $owner = &get_owner($dom,$crs);
-                        }
-                        unless ($owner eq '' || !defined($owner)) {
-                            my $outcome = &new_course($class,$owner,$dom);
-                            if ($outcome eq 'ok') {
-                                $sectioncheck = 1;
-                            }
-                        }
-                    }
-                    if ($sectioncheck > 0) {
-                        my $pidcount = 0;
-                        my $blankcount = 1;
-                        my $maxtries = 5;
-                        my $numtries = 0;
-                        while (($numtries < $maxtries) && ($blankcount > 0)) {
-                            ($pidcount,$blankcount) = &get_pids_usernames($dbh,
-                                                             $class,\%stuinfo);
-                            $numtries ++;
-                        }
+                if (&check_section($class,$owner,$dom) > 0) {
+                    my $pidcount = 0;
+                    my $blankcount = 1;
+                    my $maxtries = 5;
+                    my $numtries = 0;
+                    while (($numtries < $maxtries) && ($blankcount > 0)) {
+                        ($pidcount,$blankcount) = &get_pids_usernames($dbh,
+                                                         $class,\%stuinfo);
+                        $numtries ++;
                     }
                 }
             }
@@ -457,4 +424,34 @@
     return ($update,$comment);
 }
 
+sub check_section {
+    my ($class,$owner,$dom) = @_;
+    my $sectioncheck = 0;
+    if ($owner =~ /^([^:]+):([^:]+)$/) {
+        $owner = $1;
+        $ownerdom = $2;
+        if ($ownerdom ne $dom) {
+            return $sectioncheck;
+        }
+    }
+    if (defined($owner) && $owner ne '') {
+        if ($class =~ m/^([suf]s\d{2})(\w{2,3})(\d{3,4}\w?)(\d{3})$/) {
+# Check if section exists in LONCAPA table.
+            $sectioncheck = $dbh->selectrow_array(
+                                 "SELECT count(*) FROM LONCAPA ".
+                                 "WHERE Term_Code = '$1' AND Subj_Code = '$2' ".
+                                 "AND Crse_Code = '$3' AND Sctn_Code = '$4' ".
+                                 "AND MSUNetID = '$owner'");
+            if ($sectioncheck == 0) {
+                my $outcome = &new_course($class,$owner,$dom);
+                if ($outcome eq 'ok') {
+                    $sectioncheck = 1;
+                }
+            }
+        }
+    }
+    return $sectioncheck;
+}
+
+
 1;

--raeburn1156796929--