File:  [LON-CAPA] / loncom / enrollment / Autoupdate.pl
Revision 1.2: download - view: text, annotated - select for diffs
Mon Feb 26 20:52:55 2007 UTC (17 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Support automated update of instiutional users in a domain, based on domain preferences.
Autoupdate.pl --
- Institutional data now gathered for all users in a single call to localenroll.
- Correction to check to see if Autoupdate should run
- Accumulate updateable fields for all institutional types with which user is affiliated
- Use modifyuser() in lonnet to store updated user information
- affiliations_check() checks for changes in institutional status

localenroll.pm --
- correct documentation for instcodes hash reference
- allusers_info() - retrieve institutional user data for all users.
- get_userinfo() - retrieve institutional data for a single username or ID
- inst_usertypes() - provide institutional types (short and long titles).

    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 Apache::lonnet;
   31:     use Apache::loncommon;
   32:     use LONCAPA;
   33: 
   34:     my @info = ('inststatus','lockedname','lastname','firstname','id');
   35:     # find out which users we need to examine
   36:     my @domains = sort(&Apache::lonnet::current_machine_domains());
   37:     foreach my $dom (@domains) {
   38:         my %domconfig = &Apache::lonnet::get_dom('configuration',['autoupdate'],
   39:                                                  $dom);
   40:         #only run if configured to
   41:         my $run_update = 0;
   42:         my $settings;
   43:         if (ref($domconfig{'autoupdate'}) eq 'HASH') {
   44:             $settings = $domconfig{'autoupdate'};
   45:             if ($settings->{'run'} eq '1') {
   46:                 $run_update = 1;
   47:             }
   48:         }
   49:         next if (!$run_update);
   50:         # get user information
   51:         my (%instusers,%instids);
   52:         next if (&localenroll::allusers_info($dom,\%instusers,\%instids) ne 'ok');
   53:         my (%users,%unamechg,%possnames);
   54:         my @types = ('active','future');
   55:         my @roles = ('st');
   56:         my @cdoms = ($dom);
   57:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   58:         &descend_tree($dir,0,\%users);
   59:         foreach my $uname (keys(%users)) {
   60:             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
   61:             my (@inststatuses);
   62:             if (!$userhash{'internalname'}) {
   63:                 if (defined($instusers{$uname})) {
   64:                     (my $insttypechg,@inststatuses) = 
   65:                        &affiliations_check(\%userhash,$instusers{$uname});
   66:                     if ($insttypechg) {
   67:                         my $inststatusstr = join(':',&escape(@inststatuses));
   68:                         my %statushash = ( inststatus => $inststatusstr );
   69:                         my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
   70:                     }
   71:                 }
   72:             }
   73:             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
   74:                 if (defined($instusers{$uname})) {
   75:                     my (@fields,%changes,$changed);
   76:                     if (@inststatuses > 0) {
   77:                         foreach my $type (@inststatuses) {
   78:                             if (ref($settings->{fields}{$type}) eq 'ARRAY') {
   79:                                 foreach my $field (@{$settings->{fields}{$type}}) {
   80:                                     if (!grep(/^\Q$field\E$/,@fields)) {
   81:                                         push(@fields,$field);
   82:                                     }
   83:                                 }
   84:                             } 
   85:                         }
   86:                     } else {
   87:                         if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
   88:                             @fields = @{$settings->{fields}{'default'}};
   89:                         }
   90:                     }
   91:                     foreach my $field (@fields) { 
   92:                         if ($userhash{$field} ne $instusers{$uname}{$field}) {
   93:                             $changed = 1;
   94:                             if ($settings->{'classlists'} eq 'yes') { 
   95:                                 if ($field eq 'id') {
   96:                                     $changes{'id'} = 1;
   97:                                 } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'gen') {
   98:                                     $changes{'fullname'} = 1;
   99:                                 }
  100:                             }
  101:                         }  
  102:                     }
  103:                     # Make the change
  104:                     if ($changed) {
  105:                         my %userupdate;
  106:                         foreach my $field (@fields) {
  107:                             $userupdate{$field} = $instusers{$uname}{$field};
  108:                         }
  109:                         my $modresult = &Apache::lonnet::modifyuser($dom,$uname,$userupdate{'id'},undef,undef,$userupdate{'firstname'},$userupdate{'middlename'},$userupdate{'lastname'},$userupdate{'generation'},1);
  110:                         if ($modresult eq 'ok') {
  111:                             if ($settings->{'classlists'} eq 'yes') {
  112:                                 if ($changes{'id'} || $changes{'fullname'}) {
  113:                                     my %roleshash = 
  114:                                         &Apache::lonnet::get_my_roles($uname,
  115:                                                   $dom,\@types,\@roles,\@cdoms);
  116:                                     foreach my $item (%roleshash) {
  117:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
  118:                                         my ($start,$end) = split(/:/,$roleshash{$item});
  119:                                         if (&Apache::loncommon::is_course($cdom,$cnum)) {
  120:                                             my $result = &update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
  121:                                         }
  122:                                     }
  123:                                 }
  124:                             }
  125:                         }
  126:                     }
  127:                 } else {
  128:                     # check if the username has changed
  129:                     if (defined($instids{$userhash{'id'}})) {
  130:                         if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
  131:                             foreach my $name (@{$instids{$userhash{'id'}}}) {
  132:                                 if (!exists($users{$name})) {
  133:                                     push(@{$possnames{$uname}},$name);
  134:                                 }
  135:                             }
  136:                         } else {
  137:                             if (!exists($users{$instids{$userhash{'id'}}})) {
  138:                                 $unamechg{$uname} = $instids{$userhash{'id'}};
  139:                             }
  140:                         }
  141:                     }
  142:                 }
  143:             }
  144:         }
  145:     }
  146: 
  147: 
  148: sub descend_tree {
  149:     my ($dir,$depth,$alldomusers) = @_;
  150:     if (-d $dir) {
  151:         opendir(DIR,$dir);
  152:         my @contents = grep(!/^\./,readdir(DIR));
  153:         closedir(DIR);
  154:         $depth ++;
  155:         foreach my $item (@contents) {
  156:             if ($depth < 4) {
  157:                 &descend_tree($dir.'/'.$item,$depth,$alldomusers);
  158:             } else {
  159:                 if (-e $dir.'/'.$item.'/environment.db') {
  160:                     
  161:                     $$alldomusers{$item} = '';
  162:                 }
  163:             }
  164:         }
  165:     }
  166: }
  167: 
  168: sub update_classlist {
  169:     my ($cdom,$cnum,$udom,$uname,$user) = @_;
  170:     my ($uid,$fullname,$classlistentry);
  171:     my $fullname = 
  172:         &Apache::lonnet::format_name($user->{'first'},$user->{'middle'},
  173:                                      $user->{'last'},$user->{'gene'},'lastname');
  174:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
  175:                                          $cdom,$cnum);
  176:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
  177:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
  178:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
  179:     for (my $i=0; $i<@classinfo; $i++) {
  180:         if ($i == $ididx) {
  181:             if (defined($user->{'id'})) {
  182:                 $classlistentry .= $user->{'id'}.':';
  183:             } else {
  184:                 $classlistentry .= $classinfo[$i].':';
  185:             }
  186:         } elsif ($i == $nameidx) {
  187:             $classlistentry .= $fullname.':';
  188:         } else {
  189:             $classlistentry .= $classinfo[$i].':';
  190:         }
  191:     }
  192:     $classlistentry =~ s/:$//;
  193:     my $reply=&Apache::lonnet::cput('classlist',
  194:                                     {"$uname:$udom" => $classlistentry},
  195:                                     $cdom,$cnum);
  196:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
  197:         return 'ok';
  198:     } else { 
  199:         return 'error: '.$reply;
  200:     }
  201: }
  202: 
  203: sub affiliations_check {
  204:     my ($userhash,$insthashref) = @_;
  205:     my (@inststatuses,$insttypechg);;
  206:     if (ref($insthashref) eq 'HASH') {
  207:         if (ref($insthashref->{type}) eq 'ARRAY') {
  208:             @inststatuses = @{$insthashref->{type}};
  209:         }
  210:     }
  211:     my @currstatuses = &unescape(split(/:/,$userhash->{'inststatus'}));
  212:     foreach my $status (@inststatuses) {
  213:         if (!grep/^\Q$status\E/,@currstatuses) {
  214:             $insttypechg = 1;
  215:         }
  216:     }
  217:     if (!$insttypechg) {
  218:         foreach my $status (@currstatuses) {
  219:             if (!grep/^\Q$status\E/,@inststatuses) {
  220:                 $insttypechg = 1;
  221:             }
  222:         }
  223:     }
  224:     return ($insttypechg,@inststatuses);
  225: }
  226: 

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