File:  [LON-CAPA] / loncom / enrollment / Autoupdate.pl
Revision 1.6: download - view: text, annotated - select for diffs
Tue May 29 20:43:17 2007 UTC (17 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Initialize language handler
- Logging to an autoupdate.log file

    1: #!/usr/bin/perl
    2: #
    3: # Automated Userinfo update script
    4: # $Id: Autoupdate.pl,v 1.6 2007/05/29 20:43:17 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28:     use strict;
   29:     use lib '/home/httpd/lib/perl';
   30:     use localenroll;
   31:     use Apache::lonnet;
   32:     use Apache::loncommon;
   33:     use Apache::lonlocal;
   34:     use LONCAPA::Configuration;
   35:     use LONCAPA;
   36: 
   37:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   38:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoupdate.log';
   39:     my $hostid = $perlvarref->{'lonHostID'}; 
   40:     my @info = ('inststatus','lockedname','internalname','lastname',
   41:                 'firstname','id');
   42:     # Initialize language handler
   43:     &Apache::lonlocal::get_language_handle();
   44:     # find out which users we need to examine
   45:     my @domains = sort(&Apache::lonnet::current_machine_domains());
   46:     foreach my $dom (@domains) {
   47:         my $primaryhost_id = &Apache::lonnet::domain($dom,'primary');
   48:         if ($primaryhost_id ne $hostid) {
   49:             next;
   50:         } 
   51:         my %domconfig = &Apache::lonnet::get_dom('configuration',['autoupdate'],
   52:                                                  $dom);
   53:         #only run if configured to
   54:         my $run_update = 0;
   55:         my $settings;
   56:         if (ref($domconfig{'autoupdate'}) eq 'HASH') {
   57:             $settings = $domconfig{'autoupdate'};
   58:             if ($settings->{'run'} eq '1') {
   59:                 $run_update = 1;
   60:             }
   61:         }
   62:         next if (!$run_update);
   63:         open (my $fh,">>$logfile");
   64:         print $fh "********************\n".localtime(time).' '.&mt('Autoupdate messages start for domain: [_1]',$dom).' --'."\n";
   65:         # get user information
   66:         my (%instusers,%instids);
   67:         next if (&localenroll::allusers_info($dom,\%instusers,\%instids) ne 'ok');
   68:         my (%users,%unamechg,%possnames);
   69:         my @types = ('active','future');
   70:         my @roles = ('st');
   71:         my @cdoms = ($dom);
   72:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   73:         &descend_tree($dir,0,\%users);
   74:         foreach my $uname (keys(%users)) {
   75:             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
   76:             my (@inststatuses);
   77:             if (!$userhash{'internalname'}) {
   78:                 if (defined($instusers{$uname})) {
   79:                     (my $insttypechg,@inststatuses) = 
   80:                        &affiliations_check(\%userhash,$instusers{$uname});
   81:                     if ($insttypechg) {
   82:                         my $inststatusstr = join(':',&escape(@inststatuses));
   83:                         my %statushash = ( inststatus => $inststatusstr );
   84:                         my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
   85:                     }
   86:                 }
   87:             }
   88:             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
   89:                 if (defined($instusers{$uname})) {
   90:                     my (@fields,%changes,$changed);
   91:                     if (@inststatuses > 0) {
   92:                         foreach my $type (@inststatuses) {
   93:                             if (ref($settings->{fields}{$type}) eq 'ARRAY') {
   94:                                 foreach my $field (@{$settings->{fields}{$type}}) {
   95:                                     if (!grep(/^\Q$field\E$/,@fields)) {
   96:                                         push(@fields,$field);
   97:                                     }
   98:                                 }
   99:                             } 
  100:                         }
  101:                     } else {
  102:                         if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
  103:                             @fields = @{$settings->{fields}{'default'}};
  104:                         }
  105:                     }
  106:                     foreach my $field (@fields) { 
  107:                         if ($userhash{$field} ne $instusers{$uname}{$field}) {
  108:                             $changed = 1;
  109:                             if ($settings->{'classlists'} eq 'yes') { 
  110:                                 if ($field eq 'id') {
  111:                                     $changes{'id'} = 1;
  112:                                 } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'gen') {
  113:                                     $changes{'fullname'} = 1;
  114:                                 }
  115:                             }
  116:                         }  
  117:                     }
  118:                     # Make the change
  119:                     if ($changed) {
  120:                         my %userupdate;
  121:                         foreach my $field (@fields) {
  122:                             $userupdate{$field} = $instusers{$uname}{$field};
  123:                         }
  124:                         my $modresult = &Apache::lonnet::modifyuser($dom,$uname,$userupdate{'id'},undef,undef,$userupdate{'firstname'},$userupdate{'middlename'},$userupdate{'lastname'},$userupdate{'generation'},1);
  125:                         if ($modresult eq 'ok') {
  126:                             if ($settings->{'classlists'} eq 'yes') {
  127:                                 if ($changes{'id'} || $changes{'fullname'}) {
  128:                                     my %roleshash = 
  129:                                         &Apache::lonnet::get_my_roles($uname,
  130:                                                   $dom,'userroles',\@types,\@roles,\@cdoms);
  131:                                     foreach my $item (%roleshash) {
  132:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
  133:                                         my ($start,$end) = split(/:/,$roleshash{$item});
  134:                                         if (&Apache::loncommon::is_course($cdom,$cnum)) {
  135:                                             my $result = &update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
  136:                                         }
  137:                                     }
  138:                                 }
  139:                             }
  140:                         }
  141:                     }
  142:                 } else {
  143:                     # check if the username has changed
  144:                     if (defined($instids{$userhash{'id'}})) {
  145:                         if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
  146:                             foreach my $name (@{$instids{$userhash{'id'}}}) {
  147:                                 if (!exists($users{$name})) {
  148:                                     push(@{$possnames{$uname}},$name);
  149:                                 }
  150:                             }
  151:                         } else {
  152:                             if (!exists($users{$instids{$userhash{'id'}}})) {
  153:                                 $unamechg{$uname} = $instids{$userhash{'id'}};
  154:                                 print $fh &mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n";
  155:                             }
  156:                         }
  157:                     }
  158:                 }
  159:             }
  160:         }
  161:         if (keys(%possnames) > 0) {
  162:             foreach my $uname (keys(%possnames)) {
  163:                 my $altnames = join(' or ',@{$possnames{$uname}});
  164:                 print $fh &mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n";
  165:             }
  166:         }
  167:         print $fh "-- ".localtime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n";
  168:         close($fh);
  169:     }
  170: 
  171: sub descend_tree {
  172:     my ($dir,$depth,$alldomusers) = @_;
  173:     if (-d $dir) {
  174:         opendir(DIR,$dir);
  175:         my @contents = grep(!/^\./,readdir(DIR));
  176:         closedir(DIR);
  177:         $depth ++;
  178:         foreach my $item (@contents) {
  179:             if ($depth < 4) {
  180:                 &descend_tree($dir.'/'.$item,$depth,$alldomusers);
  181:             } else {
  182:                 if (-e $dir.'/'.$item.'/environment.db') {
  183:                     
  184:                     $$alldomusers{$item} = '';
  185:                 }
  186:             }
  187:         }
  188:     }
  189: }
  190: 
  191: sub update_classlist {
  192:     my ($cdom,$cnum,$udom,$uname,$user) = @_;
  193:     my ($uid,$fullname,$classlistentry);
  194:     my $fullname = 
  195:         &Apache::lonnet::format_name($user->{'first'},$user->{'middle'},
  196:                                      $user->{'last'},$user->{'gene'},'lastname');
  197:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
  198:                                          $cdom,$cnum);
  199:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
  200:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
  201:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
  202:     for (my $i=0; $i<@classinfo; $i++) {
  203:         if ($i == $ididx) {
  204:             if (defined($user->{'id'})) {
  205:                 $classlistentry .= $user->{'id'}.':';
  206:             } else {
  207:                 $classlistentry .= $classinfo[$i].':';
  208:             }
  209:         } elsif ($i == $nameidx) {
  210:             $classlistentry .= $fullname.':';
  211:         } else {
  212:             $classlistentry .= $classinfo[$i].':';
  213:         }
  214:     }
  215:     $classlistentry =~ s/:$//;
  216:     my $reply=&Apache::lonnet::cput('classlist',
  217:                                     {"$uname:$udom" => $classlistentry},
  218:                                     $cdom,$cnum);
  219:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
  220:         return 'ok';
  221:     } else { 
  222:         return 'error: '.$reply;
  223:     }
  224: }
  225: 
  226: sub affiliations_check {
  227:     my ($userhash,$insthashref) = @_;
  228:     my (@inststatuses,$insttypechg);;
  229:     if (ref($insthashref) eq 'HASH') {
  230:         if (ref($insthashref->{type}) eq 'ARRAY') {
  231:             @inststatuses = @{$insthashref->{type}};
  232:         }
  233:     }
  234:     my @currstatuses = &unescape(split(/:/,$userhash->{'inststatus'}));
  235:     foreach my $status (@inststatuses) {
  236:         if (!grep/^\Q$status\E/,@currstatuses) {
  237:             $insttypechg = 1;
  238:         }
  239:     }
  240:     if (!$insttypechg) {
  241:         foreach my $status (@currstatuses) {
  242:             if (!grep/^\Q$status\E/,@inststatuses) {
  243:                 $insttypechg = 1;
  244:             }
  245:         }
  246:     }
  247:     return ($insttypechg,@inststatuses);
  248: }
  249: 

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