[LON-CAPA-cvs] cvs: modules /raeburn Autoenroll.pl

raeburn lon-capa-cvs@mail.lon-capa.org
Fri, 21 Nov 2003 00:30:25 -0000


This is a MIME encoded message

--raeburn1069374625
Content-Type: text/plain

raeburn		Thu Nov 20 19:30:25 2003 EDT

  Modified files:              
    /modules/raeburn	Autoenroll.pl 
  Log:
  Automated enroolment script - style changes - 11/20 SPR
  
  
--raeburn1069374625
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20031120193025.txt"

Index: modules/raeburn/Autoenroll.pl
diff -u modules/raeburn/Autoenroll.pl:1.2 modules/raeburn/Autoenroll.pl:1.3
--- modules/raeburn/Autoenroll.pl:1.2	Wed Nov 19 19:19:41 2003
+++ modules/raeburn/Autoenroll.pl	Thu Nov 20 19:30:25 2003
@@ -1,311 +1,252 @@
 #!/usr/bin/perl -w
 
- use strict;
- use lib '/home/httpd/lib/perl';
- use LONCAPA::Configuration;
- use LONCAPA::InfoServices;
- use Apache::lonnet;
- use Apache::loncoursedata;
+  use strict;
+  use lib '/home/httpd/lib/perl';
+  use LONCAPA::Configuration;
+  use LONCAPA::InfoServices;
+  use LONCAPA::Enrollment:
+  use Apache::lonnet;
+  use Apache::loncoursedata;
 
 # Determine the library server's domain
- my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
- my $dom = $$perlvarref{'lonDefDomain'};
- print "Domain is $dom\n";
+  my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
+  my $dom = $$perlvarref{'lonDefDomain'};
+  print "Domain is $dom\n";
 
 # Determine the present time;
- my $timenow = time();
+  my $timenow = time();
 
 # Determine the courses
- my %courses = &Apache::lonnet::courseiddump($dom,'.',1); 
- my %affiliates = ();
- my %enrollvar = ();
- foreach my $key (sort keys %courses)
- {
-   if ($key =~ m/^($dom)_(\w+)$/)
-   {
-     $crs = $2;
-   }
+  my %courses = &Apache::lonnet::courseiddump($dom,'.',1); 
+  my %affiliates = ();
+  my %enrollvar = ();
+  foreach my $key (sort keys %courses) {
+      my $crs;
+      if ($key =~ m/^($dom)_(\w+)$/) {
+          $crs = $2;
+      }
+
 # Get course settings
-   my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
-   %{$enrollvar{$crs}} = ();
-   @{$affiliates{$crs}} = ();
-   foreach my $item (keys %settings)
-   {
-     if ($item =~ m/^internal\.(.+)$/)
-     {
-       $enrollvar{$crs}{$1} = $settings{$item};
-     }
-   }
-   if (($enrollvar{$crs}{autostart} >= $timenow) && ($enrollvar{$crs}{autoend} < $timenow))
-   {
-     if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)
-     {
+      my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
+      %{$enrollvar{$crs}} = ();
+      @{$affiliates{$crs}} = ();
+      foreach my $item (keys %settings) {
+          if ($item =~ m/^internal\.(.+)$/) {
+              $enrollvar{$crs}{$1} = $settings{$item};
+          }
+      }
+      if (($enrollvar{$crs}{autostart} >= $timenow) && ($enrollvar{$crs}{autoend} < $timenow)) {
+          if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) {
 # Add to list of classes for retrieval
-       $enrollvar{$crs}{sectionnums} =~ s/ //g;
-       $enrollvar{$crs}{crosslistings} =~ s/ //g;
-       my @sections = ();
-       my @crosslistings = ();
-       if ($enrollvar{$crs}{sectionnums} =~ m/,/)
-       {
-         @sections = split/,/,$enrollvar{$crs}{sectionnums};
-       }
-       else
-       {
-         @$sections = $enrollvar{$crs}{sectionnums};
-       }
-       if ($enrollvar{$crs}{crosslistings} =~ m/,/)
-       {
-         @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
-       }
-       else
-       {
-         @crosslistings = $enrollvar{$crs}{crosslistings};
-       }
-       foreach my $sec (@sections)
-       {
-         my $course_id = $enrollvar{$crs}{coursecode}.$sec;
-         if (!grep/^$course_id$/,@{$affiliates{$crs}})
-         {
-           push @{$affiliates{$crs}}, $course_id;
-         }
-       }
-       foreach my $xlist (@crosslistings)
-       {
-         if (!grep/^$xlist$/,@{$affiliates{$crs}})
-         {
-           push @{$affiliates{$crs}}, $xlist;
-         }
-       }
-     }
-   }
-   print "Key is $key, Value is $courses{$key}\n";
- }
- &LONCAPA::InfoServices::update_officialclasslists(\%affiliates);
+              $enrollvar{$crs}{sectionnums} =~ s/ //g;
+              $enrollvar{$crs}{crosslistings} =~ s/ //g;
+              my @sections = ();
+              my @crosslistings = ();
+              if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
+                  @sections = split/,/,$enrollvar{$crs}{sectionnums};
+              } else {
+                  @$sections = $enrollvar{$crs}{sectionnums};
+              }
+              if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
+                  @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
+              } else {
+                  @crosslistings = $enrollvar{$crs}{crosslistings};
+              }
+              foreach my $sec (@sections) {
+                  my $course_id = $enrollvar{$crs}{coursecode}.$sec;
+                  if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
+                      push @{$affiliates{$crs}}, $course_id;
+                  }
+              }
+              foreach my $xlist (@crosslistings) {
+                  if (!grep/^$xlist$/,@{$affiliates{$crs}}) {
+                      push @{$affiliates{$crs}}, $xlist;
+                  }
+              }
+          }
+      }
+      print "Key is $key, Value is $courses{$key}\n";
+  }
+  &LONCAPA::InfoServices::update_officialclasslists(\%affiliates);
 
 # Now go through classes and perform required enrollment changes.
- foreach my $crs (sort keys %enrollvar)
- {
-   if (($enrollvar{$crs}{autostart} >= $timenow) && ($enrollvar{$crs}{autoend} < $timenow))
-   {
-     if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1))
-     {
+  foreach my $crs (sort keys %enrollvar) {
+      if (($enrollvar{$crs}{autostart} >= $timenow) && ($enrollvar{$crs}{autoend} < $timenow)) {
+          if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
 # Get current LON-CAPA student enrollment for this class
-       my $cid = $dom."_".$crs;
-       my ($currlist) = &Apache::loncoursedata::get_classlist($cid,$dom,$crs);
-       my $cend = &Apache::loncoursedata::CL_END
-       my $cstart = &Apache::loncoursedata::CL_START; 
-       my $stuid=&Apache::loncoursedata::CL_ID;
-       my $sec=&Apache::loncoursedata::CL_SECTION;
-       my $status=&Apache::loncoursedata::CL_STATUS;
-       my @currstudents = sort(keys %{$currlist});
+              my $cid = $dom."_".$crs;
+              my ($currlist) = &Apache::loncoursedata::get_classlist($cid,$dom,$crs);
+              my $cend = &Apache::loncoursedata::CL_END
+              my $cstart = &Apache::loncoursedata::CL_START; 
+              my $stuid=&Apache::loncoursedata::CL_ID;
+              my $sec=&Apache::loncoursedata::CL_SECTION;
+              my $status=&Apache::loncoursedata::CL_STATUS;
+              my @currstudents = sort(keys %{$currlist});
 
 # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class 
-       my @LCids = ();
-       my %unameFromLCid = ();
-       foreach my $uname (sort keys %{$currlist})
-       {
-         my $stuID = $$currlist{$uname}[$stuid];
-         if (!grep/^$stuID$/,@LCids)
-         {
-           push @LCids, $stuID;
-           @{$unameFromLCid{$stuID}} = ();
-         }
-         push @{$unameFromLCid{$stuID}},$uname;
-       }
-             my $reply=&Apache::lonnet::modifystudent($dom,$uname,$cid,$stuinfo[$amode],$stuinfo[$password],$stuinfo[$fname],$stuinfo[$mname],$stuinfo[$lname],$stuinfo[$gen],$stuinfo[$sec],$stuinfo[$enddate],$stuinfo[$startdate],'',undef,$stuinfo[$email]);
-
+              my @LCids = ();
+              my %unameFromLCid = ();
+              foreach my $uname (sort keys %{$currlist}) {
+                  my $stuID = $$currlist{$uname}[$stuid];
+                  if (!grep/^$stuID$/,@LCids) {
+                      push @LCids, $stuID;
+                      @{$unameFromLCid{$stuID}} = ();
+                  }
+                  push @{$unameFromLCid{$stuID}},$uname;
+              }
  
 # Get latest institutional enrollment for this class.
-       my %allenrolled = ();
-       my @reg_students = ();
-       my $id = &LONCAPA::Enrollment::CL_ID;
-       my $amode = &LONCAPA::Enrollment::CL_AUTHMECH;
-       my $password = &LONCAPA::Enrollment::CL_PASSWD;
-       my $fname = &LONCAPA::Enrollment::CL_FIRST;
-       my $mname = &LONCAPA::Enrollment::CL_MIDDLE;
-       my $lname = &LONCAPA::Enrollment::CL_LAST;
-       my $gen = &LONCAPA::Enrollment::CL_GEN;
-       my $sect = &LONCAPA::Enrollment::CL_SECTION;
-       my $end =  &LONCAPA::Enrollment::CL_END;
-       my $start = &LONCAPA::Enrollment::CL_START;
-       my $email =  &LONCAPA::Enrollment::CL_EMAIL;
-
-       foreach my $class (@{$affiliates{$crs}})
-       {
-         my $enrolled = &LONCAPA::Enrollment::parse_classlist($class);
-         foreach my $uname (sort keys %{$enrolled})
-         {
-           if (!grep/^$uname$/,@reg_students)
-           {
-             push @reg_students,$uname;
-             @{$allenrolled{$uname}} = ();
-           }
-           push @{$allenrolled{$uname}},$enrolled{$uname};
-         }
-       }
+              my %allenrolled = ();
+              my @reg_students = ();
+              my $id = &LONCAPA::Enrollment::CL_ID;
+              my $amode = &LONCAPA::Enrollment::CL_AUTHMECH;
+              my $password = &LONCAPA::Enrollment::CL_PASSWD;
+              my $fname = &LONCAPA::Enrollment::CL_FIRST;
+              my $mname = &LONCAPA::Enrollment::CL_MIDDLE;
+              my $lname = &LONCAPA::Enrollment::CL_LAST;
+              my $gen = &LONCAPA::Enrollment::CL_GEN;
+              my $sect = &LONCAPA::Enrollment::CL_SECTION;
+              my $start = &LONCAPA::Enrollment::CL_START;
+              my $end =  &LONCAPA::Enrollment::CL_END;
+              my $email =  &LONCAPA::Enrollment::CL_EMAIL;
+
+              foreach my $class (@{$affiliates{$crs}}) {
+                  my $enrolled = &LONCAPA::Enrollment::parse_classlist($class);
+                  foreach my $uname (sort keys %{$enrolled}) {
+                      if (!grep/^$uname$/,@reg_students) {
+                          push @reg_students,$uname;
+                          @{$allenrolled{$uname}} = ();
+                      }
+                      push @{$allenrolled{$uname}},$enrolled{$uname};
+                  }
+              }
 # Check for multiple sections for a single student 
-       my @okusers = ();
-       foreach my $uname (@reg_students) 
-       {
-         if (@{$allenrolled{$uname}} > 1)
-         {
-           my @sections = ();
-           my $saved;
-           for (my $i=0; $i<@{$allenrolled{$uname}; $i++)
-           {
-             my @stuinfo = split/:/,$$allenrolled{$uname}[$i];
-             my $secnum = $stuinfo[$sect];
-             unless ($secnum eq '')
-             {
-               unless (grep/^$secnum$/,@sections)
-               {
-                 $saved = $i; 
-                 push @sections,$secnum;
-               }
-             }
-           }
-           if (@sections == 0)
-           {
-             @{$allenrolled{$uname}} = ("$$allenrolled{$uname}[0]");
-             push @okusers, $uname;
-           }
-           elsif (@sections == 1)
-           {
-             @{$allenrolled{$uname}} = ("$$allenrolled{$uname}[$saved]");
-             push @okusers, $uname;
-           }
-           elsif (@sections > 1)
-           {
-             my $dupmsg =  "$uname appears in classlists for two sections of $crs -";
-             foreach (@sections)
-             {
-               $dupmsg .= " $_,";
-             }
-             chop($dupmsg);
-             $dupmsg .= " No automated enrollment action taken for this student\n";
-             print "$dupmsg\n";
-           }
-         }
-         else
-         {
-           push @okusers, $uname;
-         }
-       }
+              my @okusers = ();
+              foreach my $uname (@reg_students)  {
+                  if (@{$allenrolled{$uname}} > 1) {
+                      my @sections = ();
+                      my $saved;
+                      for (my $i=0; $i<@{$allenrolled{$uname}; $i++) {
+                          my @stuinfo = split/:/,$$allenrolled{$uname}[$i];
+                          my $secnum = $stuinfo[$sect];
+                          unless ($secnum eq '') {
+                              unless (grep/^$secnum$/,@sections) {
+                                  $saved = $i; 
+                                  push @sections,$secnum;
+                              }
+                          }
+                      }
+                      if (@sections == 0) {
+                          @{$allenrolled{$uname}} = ("$$allenrolled{$uname}[0]");
+                          push @okusers, $uname;
+                      }
+                      elsif (@sections == 1) {
+                          @{$allenrolled{$uname}} = ("$$allenrolled{$uname}[$saved]");
+                          push @okusers, $uname;
+                      }
+                      elsif (@sections > 1) {
+                          my $dupmsg =  "$uname appears in classlists for two sections of $crs -";
+                          foreach (@sections) {
+                              $dupmsg .= " $_,";
+                          }
+                          chop($dupmsg);
+                          $dupmsg .= " No automated enrollment action taken for this student.\n";
+                          print "$dupmsg\n";
+                      }
+                  } else {
+                      push @okusers, $uname;
+                  }
+              }
 # Get mapping of student IDs to usernames for users in institutional data for this class  
-       my @allINids = ();
-       foreach my $uname (@okusers)
-       {
-         @stuinfo = split/:/,$$allenrolled{$uname}[0];
-         my $stuID = $stuinfo[$id];
-         if (grep/^$stuID$/,@allINids) 
-         {
-           push @{$unameFromINid{$stuID}},$uname;
-         }
-         else
-         {
-           push @allINids, $stuID;
-           @{$unameFromINid{$stuID}} = $uname; 
-         }
-       }
+              my @allINids = ();
+              foreach my $uname (@okusers) {
+                  @stuinfo = split/:/,$$allenrolled{$uname}[0];
+                  my $stuID = $stuinfo[$id];
+                  if (grep/^$stuID$/,@allINids)  {
+                      push @{$unameFromINid{$stuID}},$uname;
+                  } else {
+                      push @allINids, $stuID;
+                      @{$unameFromINid{$stuID}} = $uname; 
+                  }
+              }
 # Compare IDs with existing LON-CAPA enrollment for this class
-       foreach my $uname (@okusers)
-       {
-         my %uidhash=&Apache::lonnet::idrget($dom,$uname);a
-         my @stuinfo = split/:/,$$allenrolled{$uname}[0];
-         if (grep/^$uname$/,@currstudents)
-         {
+              foreach my $uname (@okusers) {
+                  my %uidhash=&Apache::lonnet::idrget($dom,$uname);
+                  my @stuinfo = split/:/,$$allenrolled{$uname}[0];
+                  if (grep/^$uname$/,@currstudents) {
 # Check for studentID changes
-           if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)
-           {
-             unless ($uidhash{$uname} eq $stuinfo[$id]) 
-             {
-               print "Change in ID for $uname in class: $crs. StudentID in LON-CAPA system is $uidhash{$uname}, StudentID in institutional data is $stuinfo[$id]\n"; 
-             }
-           }
+                      if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) {
+                          unless ($uidhash{$uname} eq $stuinfo[$id]) {
+                              print "Change in ID for $uname in class: $crs. StudentID in LON-CAPA system is $uidhash{$uname}, StudentID in institutional data is $stuinfo[$id]\n"; 
+                          }
+                      }
 # Check for section changes
-           unless ($$currlist{$uname}[$sec] eq $stuinfo[$sec])
-           {
-             if ($$currlist{$uname}[$status] eq "auto")
-             {
-               my $modify_section_result = &Apache::londropadd::modifysection($dom,$uname,$cid,$stuinfo[$sec],undef);
-               if ($modify_section_results !~ /^ok/) {
-                    print("An error occured during the attempt to change the section for $uname from $$currlist{$uname}[$sec] to $stuinfo[$sec]\n");
-               }
-             }
-           }
-         }
-         elsif ($uname ne '')
-         {
+                      unless ($$currlist{$uname}[$sec] eq $stuinfo[$sec]) {
+                          if ($$currlist{$uname}[$status] eq "auto") {
+                              my $modify_section_result = &Apache::londropadd::modifysection($dom,$uname,$cid,$stuinfo[$sec],undef);
+                              if ($modify_section_results !~ /^ok/) {
+                                  print("An error occured during the attempt to change the section for $uname from $$currlist{$uname}[$sec] to $stuinfo[$sec]\n");
+                              }
+                          }
+                      }
+                  }
+                  elsif ($uname ne '') {
 # Check for changed usernames by checking studentIDs
-           if (grep/^$$currlist{$uname}[$id]$/,@allINids)
-           {
-             foreach my $match @{$unameFromINid{$$currlist{$student}[$id]}}
-             {
-               if (grep/^$match$/,@okusers)
-               {
-                 print "A possible change in username has been detected for a student enrolled in $crs. The existing LON-CAPA classlist contains user: $uname and student ID: $$currlist{$uname}[$id].  This username has been dropped from the institutional classlist, but the same student ID is used for user: $match who still appears in the institutional classlist. You may need to move the student data files for user: $uname to $match\n";
-               }
-             }
-           }
+                      if (grep/^$$currlist{$uname}[$id]$/,@allINids) {
+                          foreach my $match @{$unameFromINid{$$currlist{$student}[$id]}} {
+                              if (grep/^$match$/,@okusers) {
+                                  print "A possible change in username has been detected for a student enrolled in $crs. The existing LON-CAPA classlist contains user: $uname and student ID: $$currlist{$uname}[$id].  This username has been dropped from the institutional classlist, but the same student ID is used for user: $match who still appears in the institutional classlist. You may need to move the student data files for user: $uname to $match\n";
+                              }
+                          }
+                      } 
 # Add student to LON-CAPA classlist
-           elsif ($enrollvar{$crs}{autoadds} == 1)
-           {
-             my $authtype = $stuinfo[$amode];
-             my $passwd = $stuinfo[$password];
-             my $first = $stuinfo[$fname];
-             my $middle = $stuinfo[$mname];
-             my $last = $stuinfo[$lname];
-             my $gene = $stuinfo[$gen];
-             my $section = $stuinfo[$sect];
-             my $enddate = $stuinfo[$end];
-             my $startdate = $stuinfo[$start];
-             my $address = $stuinfo[$email];
-             my $pid = $stuinfo[$id];
-
-             # remove non alphanumeric values from section
-             $section =~ s/\W//g;
-
-             unless ($address=~/^[^\@]+\@[^\@]+$/) { $address=''; }
-
- # Use course defaults where entry is absent
-             if ($authtype eq '')
-             {
-               $authtype =  $enrollvar{$crs}{authtype};
-             }
-             if ($enddate eq '')
-             {
-               $enddate = $enrollvar{$crs}{enddate};
-             }
-             if ($startdate eq '')
-             {
-               $startdate = $enrollvar{$crs}{startdate};
-             }
-             if ($passwd eq '')
-             {
+                      elsif ($enrollvar{$crs}{autoadds} == 1) {
+                          my $authtype = $stuinfo[$amode];
+                          my $passwd = $stuinfo[$password];
+                          my $first = $stuinfo[$fname];
+                          my $middle = $stuinfo[$mname];
+                          my $last = $stuinfo[$lname];
+                          my $gene = $stuinfo[$gen];
+                          my $section = $stuinfo[$sect];
+                          my $enddate = $stuinfo[$end];
+                          my $startdate = $stuinfo[$start];
+                          my $address = $stuinfo[$email];
+                          my $pid = $stuinfo[$id];
+
+# remove non alphanumeric values from section
+                          $section =~ s/\W//g;
+
+                          unless ($address=~/^[^\@]+\@[^\@]+$/) { $address=''; }
+
+# Use course defaults where entry is absent
+                          if ($authtype eq '') {
+                              $authtype =  $enrollvar{$crs}{authtype};
+                          }
+                          if ($enddate eq '') {
+                              $enddate = $enrollvar{$crs}{enddate};
+                          }
+                          if ($startdate eq '') {
+                              $startdate = $enrollvar{$crs}{startdate};
+                          }
+                          if ($passwd eq '') {
 # If no account exists and passwords should be generated
-               $passwd = &LONCAPA::Enrollment::create_password();
-             }
-
-             # Clean up whitespace
-             foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$section)
-             {
-               $$_ =~ s/(\s+$|^\s+)//g;
-             }
-
-             my $reply=&modifystudent($dom,$cid,$uname,$pid,$authtype,$passwd,$first,$middle,$last,$gene,$section,$enddate,$startdate,'',undef,$stuinfo[$email]);
-           }
-         }
-       }
+                              $passwd = &LONCAPA::Enrollment::create_password();
+                          }
+# Clean up whitespace
+                          foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$section) {
+                              $$_ =~ s/(\s+$|^\s+)//g;
+                          }
+                          my $reply=&modifystudent($dom,$cid,$uname,$pid,$authtype,$passwd,$first,$middle,$last,$gene,$section,$enddate,$startdate,'',undef,$stuinfo[$email]);
+                      }
+                  }
+              }
 # Do drops
-         if ($enrollvar{$crs}{autodrops} == 1)
-         {
+              if ($enrollvar{$crs}{autodrops} == 1) {
 # Check for changed usernames by checking studentIDs
-
-         }
-       }
-     }
-   }
- }
+              }
+          }
+      }
+  }
 
 
 sub modifystudent {

--raeburn1069374625--