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

raeburn lon-capa-cvs@mail.lon-capa.org
Wed, 19 Nov 2003 06:44:14 -0000


This is a MIME encoded message

--raeburn1069224254
Content-Type: text/plain

raeburn		Wed Nov 19 01:44:14 2003 EDT

  Added files:                 
    /modules/raeburn	Autoenroll.pl 
  Log:
  Stuart's Automated Enrollment script -to be run as nightly cron job.
  
  
--raeburn1069224254
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20031119014414.txt"


Index: modules/raeburn/Autoenroll.pl
+++ modules/raeburn/Autoenroll.pl
#!/usr/bin/perl -w

 use strict;
 use lib '/home/httpd/lib/perl';
 use LONCAPA::Configuration;
 use LONCAPA::InfoServices;
 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";

# Determine the present 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;
   }
# 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)
     {
# 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);

# 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))
     {
# 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});

# 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]);

 
# 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};
         }
       }
# 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;
         }
       }
# 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; 
         }
       }
# 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)
         {
# 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"; 
             }
           }
# 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 '')
         {
# Check for changed usernames by checking studentIDs
           if (grep/^$$currlist{$uname}[$pid]$/,@allPids)
           {
             foreach my $match @{$unameFromPid{$$currlist{$student}[$pid]}}
             {
               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}[$pid].  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 $reply=&Apache::lonnet::modifystudent($dom,$uname,$cid,$stuinfo[$amode],$stuinfo[$password],$stuinfo[$fname],$stuinfo[$mname],$studinfo[$lname],$stuinfo[$gen],$stuinfo[$sec],$stuinfo[$enddate],$stuinfo[$startdate],'',undef,$stuinfo[$email]);
           }
         }
       }
# Do drops
         if ($enrollvar{$crs}{autodrops} == 1)
         {
# Check for changed usernames by checking studentIDs

         }
       }
     }
   }
 }

--raeburn1069224254--