File:  [LON-CAPA] / loncom / enrollment / Autoupdate.pl
Revision 1.10: download - view: text, annotated - select for diffs
Tue Jun 5 23:23:01 2007 UTC (16 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, HEAD
- replace limit to primary domain server with library servers only.
- filter out courseIDs from just the current server n search for real users
- iterate over the keys of the roleshash
- correct name information keys.

    1: #!/usr/bin/perl
    2: #
    3: # Automated Userinfo update script
    4: # $Id: Autoupdate.pl,v 1.10 2007/06/05 23:23:01 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:     if (!&Apache::lonnet::is_library($hostid)) {
   41:         exit;
   42:     }
   43:     my @info = ('inststatus','lockedname','internalname','lastname',
   44:                 'firstname','id','permanentemail');
   45:     # Initialize language handler
   46:     &Apache::lonlocal::get_language_handle();
   47:     # find out which users we need to examine
   48:     my @domains = sort(&Apache::lonnet::current_machine_domains());
   49:     foreach my $dom (@domains) {
   50:         my %domconfig = &Apache::lonnet::get_dom('configuration',['autoupdate'],
   51:                                                  $dom);
   52:         #only run if configured to
   53:         my $run_update = 0;
   54:         my $settings;
   55:         if (ref($domconfig{'autoupdate'}) eq 'HASH') {
   56:             $settings = $domconfig{'autoupdate'};
   57:             if ($settings->{'run'} eq '1') {
   58:                 $run_update = 1;
   59:             }
   60:         }
   61:         next if (!$run_update);
   62:         open(my $fh,">>$logfile");
   63:         print $fh ("********************\n".localtime(time).' '.&mt('Autoupdate messages start for domain: [_1]',$dom).' --'."\n");
   64:         # get courseIDs for domain
   65:         my %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.');
   66:         # get user information
   67:         my (%instusers,%instids);
   68:         next if (&localenroll::allusers_info($dom,\%instusers,\%instids) ne 'ok');
   69:         my (%users,%unamechg,%possnames);
   70:         my @types = ('active','future');
   71:         my @roles = ('st');
   72:         my @cdoms = ($dom);
   73:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   74:         &descend_tree($dir,0,\%users);
   75:         foreach my $uname (keys(%users)) {
   76:             next if ($courses{$dom.'_'.$uname});
   77:             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
   78:             my (@inststatuses);
   79:             if (!$userhash{'internalname'}) {
   80:                 if (defined($instusers{$uname})) {
   81:                     (my $insttypechg,@inststatuses) = 
   82:                        &affiliations_check(\%userhash,$instusers{$uname});
   83:                     if ($insttypechg) {
   84:                         my $inststatusstr = join(':',&escape(@inststatuses));
   85:                         my %statushash = ( inststatus => $inststatusstr );
   86:                         my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
   87:                     }
   88:                 }
   89:             }
   90:             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
   91:                 if (defined($instusers{$uname})) {
   92:                     my (@fields,%changes,$changed);
   93:                     if (@inststatuses > 0) {
   94:                         foreach my $type (@inststatuses) {
   95:                             if (ref($settings->{fields}{$type}) eq 'ARRAY') {
   96:                                 foreach my $field (@{$settings->{fields}{$type}}) {
   97:                                     if (!grep(/^\Q$field\E$/,@fields)) {
   98:                                         push(@fields,$field);
   99:                                     }
  100:                                 }
  101:                             } 
  102:                         }
  103:                     } else {
  104:                         if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
  105:                             @fields = @{$settings->{fields}{'default'}};
  106:                         }
  107:                     }
  108:                     foreach my $field (@fields) { 
  109:                         if ($userhash{$field} ne $instusers{$uname}{$field}) {
  110:                             $changed = 1;
  111:                             if ($settings->{'classlists'} == 1) { 
  112:                                 if ($field eq 'id') {
  113:                                     $changes{'id'} = 1;
  114:                                 } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'gen') {
  115:                                     $changes{'fullname'} = 1;
  116:                                 }
  117:                             }
  118:                         }
  119:                     }
  120:                     # Make the change
  121:                     if ($changed) {
  122:                         my %userupdate;
  123:                         foreach my $field (@fields) {
  124:                             $userupdate{$field} = $instusers{$uname}{$field};
  125:                         }
  126:                         my $modresult = &Apache::lonnet::modifyuser($dom,$uname,$userupdate{'id'},undef,undef,$userupdate{'firstname'},$userupdate{'middlename'},$userupdate{'lastname'},$userupdate{'generation'},1,$hostid,$userupdate{'permanentemail'});
  127:                         if ($modresult eq 'ok') {
  128:                             print $fh "User change: $uname:$dom; New values: 1.Name: $userupdate{'firstname'} $userupdate{'middlename'} $userupdate{'lastname'} $userupdate{'generation'}; 2. StuID: $userupdate{'id'}; 3. Email: $userupdate{'permanentemail'}.\n";
  129:                             if ($settings->{'classlists'} == 1) {
  130:                                 if ($changes{'id'} || $changes{'fullname'}) {
  131:                                     my %roleshash = 
  132:                                         &Apache::lonnet::get_my_roles($uname,
  133:                                                   $dom,'userroles',\@types,\@roles,\@cdoms);
  134:                                     foreach my $item (keys(%roleshash)) {
  135:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
  136:                                         my ($start,$end) = split(/:/,$roleshash{$item});
  137:                                         if (&Apache::lonnet::is_course($cdom,$cnum)) {
  138:                                             my $result = &update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
  139:                                             if ($result eq 'ok') {
  140:                                                 print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n";
  141:                                             } else {
  142:                                                 print $fh "Error - $result -during classlist update for $uname:$dom in $cnum:$cdom\n";
  143:                                             }
  144:                                         }
  145:                                     }
  146:                                 }
  147:                             }
  148:                         } else {
  149:                             print $fh "User change detected for $uname:$dom. Error during update: $modresult\n";
  150:                         }
  151:                     }
  152:                 } else {
  153:                     # check if the username has changed
  154:                     if (defined($instids{$userhash{'id'}})) {
  155:                         if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
  156:                             foreach my $name (@{$instids{$userhash{'id'}}}) {
  157:                                 if (!exists($users{$name})) {
  158:                                     push(@{$possnames{$uname}},$name);
  159:                                 }
  160:                             }
  161:                         } else {
  162:                             if (!exists($users{$instids{$userhash{'id'}}})) {
  163:                                 $unamechg{$uname} = $instids{$userhash{'id'}};
  164:                                 print $fh (&mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n");
  165:                             }
  166:                         }
  167:                     }
  168:                 }
  169:             }
  170:         }
  171:         if (keys(%possnames) > 0) {
  172:             foreach my $uname (keys(%possnames)) {
  173:                 my $altnames = join(' or ',@{$possnames{$uname}});
  174:                 print $fh (&mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n");
  175:             }
  176:         }
  177:         print $fh ("-- \n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n");
  178:         close($fh);
  179:     }
  180: 
  181: sub descend_tree {
  182:     my ($dir,$depth,$alldomusers) = @_;
  183:     if (-d $dir) {
  184:         opendir(DIR,$dir);
  185:         my @contents = grep(!/^\./,readdir(DIR));
  186:         closedir(DIR);
  187:         $depth ++;
  188:         foreach my $item (@contents) {
  189:             if ($depth < 4) {
  190:                 &descend_tree($dir.'/'.$item,$depth,$alldomusers);
  191:             } else {
  192:                 $$alldomusers{$item} = '';
  193:             }
  194:         }
  195:     }
  196: }
  197: 
  198: sub update_classlist {
  199:     my ($cdom,$cnum,$udom,$uname,$user) = @_;
  200:     my ($uid,$fullname,$classlistentry);
  201:     my $fullname = 
  202:         &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
  203:                                      $user->{'lastname'},$user->{'generation'},
  204:                                      'lastname');
  205:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
  206:                                          $cdom,$cnum);
  207:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
  208:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
  209:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
  210:     for (my $i=0; $i<@classinfo; $i++) {
  211:         if ($i == $ididx) {
  212:             if (defined($user->{'id'})) {
  213:                 $classlistentry .= $user->{'id'}.':';
  214:             } else {
  215:                 $classlistentry .= $classinfo[$i].':';
  216:             }
  217:         } elsif ($i == $nameidx) {
  218:             $classlistentry .= $fullname.':';
  219:         } else {
  220:             $classlistentry .= $classinfo[$i].':';
  221:         }
  222:     }
  223:     $classlistentry =~ s/:$//;
  224:     my $reply=&Apache::lonnet::cput('classlist',
  225:                                     {"$uname:$udom" => $classlistentry},
  226:                                     $cdom,$cnum);
  227:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
  228:         return 'ok';
  229:     } else { 
  230:         return 'error: '.$reply;
  231:     }
  232: }
  233: 
  234: sub affiliations_check {
  235:     my ($userhash,$insthashref) = @_;
  236:     my (@inststatuses,$insttypechg);;
  237:     if (ref($insthashref) eq 'HASH') {
  238:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
  239:             @inststatuses = @{$insthashref->{'inststatus'}};
  240:         }
  241:     }
  242:     my @currstatuses = &unescape(split(/:/,$userhash->{'inststatus'}));
  243:     foreach my $status (@inststatuses) {
  244:         if (!grep/^\Q$status\E/,@currstatuses) {
  245:             $insttypechg = 1;
  246:         }
  247:     }
  248:     if (!$insttypechg) {
  249:         foreach my $status (@currstatuses) {
  250:             if (!grep/^\Q$status\E/,@inststatuses) {
  251:                 $insttypechg = 1;
  252:             }
  253:         }
  254:     }
  255:     return ($insttypechg,@inststatuses);
  256: }
  257: 

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