File:  [LON-CAPA] / loncom / enrollment / Autoupdate.pl
Revision 1.1: download - view: text, annotated - select for diffs
Fri Feb 16 01:37:54 2007 UTC (17 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
First pass at a script to update user information based on an institutional datafeed. Will require a get_userinfo() routine in localenroll.pm which will retrieve the user's data from the institutional source. Work in progress.

    1: #!/usr/bin/perl
    2: #
    3: # Automated Userinfo update script
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27:     use strict;
   28:     use lib '/home/httpd/lib/perl';
   29:     use localenroll;
   30:     use LONCAPA::Configuration;
   31:     use LONCAPA::Enrollment;
   32:     use Apache::lonnet;
   33:     use Apache::loncommon;
   34: 
   35:     my @info = ('inststatus','lockedname','lastname','firstname','id');
   36:     # find out which users we need to examine
   37:     my @domains = sort(&Apache::lonnet::current_machine_domains());
   38:     foreach my $dom (@domains) {
   39:         my %domconfig = &Apache::lonnet::get_dom('configuration',['autoupdate'],
   40:                                                  $dom);
   41:         #only run if configured to
   42:         my $run_update = 0;
   43:         my $settings;
   44:         if (ref($domconfig{'autoupdate'}) eq 'HASH') {
   45:             $settings = $domconfig{'autoupdate'};
   46:             if ($settings->{'run'} eq 'yes') {
   47:                 $run_update = 1;
   48:             }
   49:         }
   50:         next if (!$run_update);
   51:         my %users;
   52:         my @types = ('active','future');
   53:         my @roles = ('st');
   54:         my @cdoms = ($dom);
   55:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   56:         &descend_tree($dir,0,\%users);
   57:         foreach my $uname (keys(%users)) {
   58:             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
   59:             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
   60:                 my %userinfo = &localenroll::get_userinfo($dom,$uname,%userhash);
   61:                 if (keys(%userinfo) > 0) {
   62:                     my @fields = @{$settings->{'default'}};
   63:                     if ($userhash{'inststatus'} ne '') {
   64:                         if (ref($settings->{$userhash{'inststatus'}}) eq 'ARRAY') {
   65:                             @fields = @{$settings->{$userhash{'inststatus'}}};
   66:                         }
   67:                     }
   68:                     my %changes;
   69:                     my $changed;
   70:                     foreach my $field (@fields) { 
   71:                         if ($userhash{$field} ne $userinfo{$field}) {
   72:                             $changed = 1;
   73:                             if ($settings->{'classlists'} eq 'yes') { 
   74:                                 if ($field eq 'id') {
   75:                                     $changes{'id'} = 1;
   76:                                 } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'gen') {
   77:                                     $changes{'fullname'} = 1;
   78:                                 }
   79:                             }
   80:                         }  
   81:                     }
   82:                     # Make the change
   83:                     if ($changed) {
   84:                         my %userupdate;
   85:                         foreach my $field (@fields) {
   86:                             $userupdate{$field} = $userinfo{$field};
   87:                         }
   88:                         my $putresult = &Apache::lonnet::put
   89:                                        ('environment',\%userupdate,$dom,$uname);
   90:                         if ($putresult eq 'ok') {
   91:                             if ($settings->{'classlists'} eq 'yes') {
   92:                                 if ($changes{'id'} || $changes{'fullname'}) {
   93:                                     my %roleshash = 
   94:                                         &Apache::lonnet::get_my_roles($uname,
   95:                                                   $dom,\@types,\@roles,\@cdoms);
   96:                                     foreach my $item (%roleshash) {
   97:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
   98:                                         my ($start,$end) = split(/:/,$roleshash{$item});
   99:                                         if (&Apache::loncommon::is_course($cdom,$cnum)) {
  100:                                             my $result = &update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
  101:                                         }
  102:                                     }
  103:                                 }
  104:                             }
  105:                         }
  106:                     }
  107:                 }
  108:             }
  109:         }
  110:     }
  111: 
  112: 
  113: sub descend_tree {
  114:     my ($dir,$depth,$alldomusers) = @_;
  115:     if (-d $dir) {
  116:         opendir(DIR,$dir);
  117:         my @contents = grep(!/^\./,readdir(DIR));
  118:         closedir(DIR);
  119:         $depth ++;
  120:         foreach my $item (@contents) {
  121:             if ($depth < 4) {
  122:                 &descend_tree($dir.'/'.$item,$depth,$alldomusers);
  123:             } else {
  124:                 if (-e $dir.'/'.$item.'/environment.db') {
  125:                     
  126:                     $$alldomusers{$item} = '';
  127:                 }
  128:             }
  129:         }
  130:     }
  131: }
  132: 
  133: sub update_classlist {
  134:     my ($cdom,$cnum,$udom,$uname,$user) = @_;
  135:     my ($uid,$fullname,$classlistentry);
  136:     my $fullname = 
  137:         &Apache::lonnet::format_name($user->{'first'},$user->{'middle'},
  138:                                      $user->{'last'},$user->{'gene'},'lastname');
  139:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
  140:                                          $cdom,$cnum);
  141:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
  142:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
  143:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
  144:     for (my $i=0; $i<@classinfo; $i++) {
  145:         if ($i == $ididx) {
  146:             if (defined($user->{'id'})) {
  147:                 $classlistentry .= $user->{'id'}.':';
  148:             } else {
  149:                 $classlistentry .= $classinfo[$i].':';
  150:             }
  151:         } elsif ($i == $nameidx) {
  152:             $classlistentry .= $fullname.':';
  153:         } else {
  154:             $classlistentry .= $classinfo[$i].':';
  155:         }
  156:     }
  157:     $classlistentry =~ s/:$//;
  158:     my $reply=&Apache::lonnet::cput('classlist',
  159:                                     {"$uname:$udom" => $classlistentry},
  160:                                     $cdom,$cnum);
  161:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
  162:         return 'ok';
  163:     } else { 
  164:         return 'error: '.$reply;
  165:     }
  166: }
  167: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>