File:  [LON-CAPA] / loncom / enrollment / Autoupdate.pl
Revision 1.21: download - view: text, annotated - select for diffs
Fri Oct 14 18:14:31 2011 UTC (12 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
- &descend_tree() - new first arg ($dom).
  - required for exclusion of courseIDs as users when building %users hash

    1: #!/usr/bin/perl
    2: #
    3: # Automated Userinfo update script
    4: # $Id: Autoupdate.pl,v 1.21 2011/10/14 18:14:31 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 Apache::lonuserutils();
   35:     use LONCAPA::Configuration;
   36:     use LONCAPA;
   37: 
   38:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   39:     exit if (ref($perlvarref) ne 'HASH');
   40:     my $logfile = $perlvarref->{'lonDaemons'}.'/logs/autoupdate.log';
   41:     exit if ($perlvarref->{'lonRole'} ne 'library');
   42:     my $hostid = $perlvarref->{'lonHostID'};
   43: 
   44:     my @info = ('inststatus','lockedname','internalname','lastname',
   45:                 'firstname','middlename','generation','id','permanentemail');
   46:     # Initialize language handler
   47:     &Apache::lonlocal::get_language_handle();
   48:     # find out which users we need to examine
   49:     my @domains = sort(&Apache::lonnet::current_machine_domains());
   50:     foreach my $dom (@domains) {
   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".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages start for domain: [_1]',$dom).' --'."\n");
   65:         # get courseIDs for domain
   66:         my %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.');
   67:         # get user information
   68:         my (%users,%instusers,%instids);
   69:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   70:         &descend_tree($dom,$dir,0,\%users,\%courses);
   71:         next if (&localenroll::allusers_info($dom,\%instusers,\%instids,\%users) ne 'ok');
   72:         my (%unamechg,%possnames);
   73:         my @types = ('active','future');
   74:         my @roles = ('st');
   75:         my @cdoms = ($dom);
   76:         foreach my $uname (sort(keys(%users))) {
   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(':',map { &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 'generation') {
  115:                                     $changes{'fullname'} = 1;
  116:                                 }
  117:                             }
  118:                         }
  119:                     }
  120:                     # Make the change
  121:                     if ($changed) {
  122:                         my %userupdate;
  123:                         foreach my $item ('lastname','firstname','middlename','generation','id',
  124:                                           'permanentemail') {
  125:                             $userupdate{$item} = $userhash{$item};
  126:                         }
  127:                         foreach my $field (@fields) {
  128:                             $userupdate{$field} = $instusers{$uname}{$field};
  129:                         }
  130:                         my $modresult = &Apache::lonnet::modifyuser($dom,$uname,$userupdate{'id'},undef,undef,$userupdate{'firstname'},$userupdate{'middlename'},$userupdate{'lastname'},$userupdate{'generation'},1,$hostid,$userupdate{'permanentemail'},undef,\@fields);
  131:                         if ($modresult eq 'ok') {
  132:                             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";
  133:                             if ($settings->{'classlists'} == 1) {
  134:                                 if ($changes{'id'} || $changes{'fullname'}) {
  135:                                     my %roleshash = 
  136:                                         &Apache::lonnet::get_my_roles($uname,
  137:                                                   $dom,'userroles',\@types,\@roles,\@cdoms);
  138:                                     foreach my $item (keys(%roleshash)) {
  139:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
  140:                                         my ($start,$end) = split(/:/,$roleshash{$item});
  141:                                         if (&Apache::lonnet::is_course($cdom,$cnum)) {
  142:                                             my $result = 
  143:                                                 &Apache::lonuserutils::update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
  144:                                             if ($result eq 'ok') {
  145:                                                 print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n";
  146:                                             } else {
  147:                                                 print $fh "Error - $result -during classlist update for $uname:$dom in $cnum:$cdom\n";
  148:                                             }
  149:                                         }
  150:                                     }
  151:                                 }
  152:                             }
  153:                         } else {
  154:                             print $fh "User change detected for $uname:$dom. Error during update: $modresult\n";
  155:                         }
  156:                     }
  157:                 } else {
  158:                     # check if the username has changed
  159:                     if (defined($instids{$userhash{'id'}})) {
  160:                         if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
  161:                             foreach my $name (@{$instids{$userhash{'id'}}}) {
  162:                                 if (!exists($users{$name})) {
  163:                                     push(@{$possnames{$uname}},$name);
  164:                                 }
  165:                             }
  166:                         } else {
  167:                             if (!exists($users{$instids{$userhash{'id'}}})) {
  168:                                 $unamechg{$uname} = $instids{$userhash{'id'}};
  169:                                 print $fh (&mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n");
  170:                             }
  171:                         }
  172:                     }
  173:                 }
  174:             }
  175:         }
  176:         if (keys(%possnames) > 0) {
  177:             foreach my $uname (keys(%possnames)) {
  178:                 my $altnames = join(' or ',@{$possnames{$uname}});
  179:                 print $fh (&mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n");
  180:             }
  181:         }
  182:         print $fh ("-- \n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n");
  183:         close($fh);
  184:     }
  185: 
  186: sub descend_tree {
  187:     my ($dom,$dir,$depth,$alldomusers,$coursesref) = @_;
  188:     if (-d $dir) {
  189:         opendir(DIR,$dir);
  190:         my @contents = grep(!/^\./,readdir(DIR));
  191:         closedir(DIR);
  192:         $depth ++;
  193:         foreach my $item (@contents) {
  194:             if ($depth < 4) {
  195:                 &descend_tree($dom,$dir.'/'.$item,$depth,$alldomusers,$coursesref);
  196:             } elsif (-e $dir.'/'.$item.'/passwd') {
  197:                 if (ref($coursesref) eq 'HASH') {
  198:                     next if (exists($coursesref->{$dom.'_'.$item}));
  199:                 }
  200:                 $$alldomusers{$item} = '';
  201:             }
  202:         }
  203:     }
  204: }
  205: 
  206: sub affiliations_check {
  207:     my ($userhash,$insthashref) = @_;
  208:     my (@inststatuses,$insttypechg);;
  209:     if (ref($insthashref) eq 'HASH') {
  210:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
  211:             @inststatuses = @{$insthashref->{'inststatus'}};
  212:         }
  213:     }
  214:     my @currstatuses = map { &unescape($_); } (split(/:/,$userhash->{'inststatus'}));
  215:     foreach my $status (@inststatuses) {
  216:         if (!grep/^\Q$status\E/,@currstatuses) {
  217:             $insttypechg = 1;
  218:         }
  219:     }
  220:     if (!$insttypechg) {
  221:         foreach my $status (@currstatuses) {
  222:             if (!grep/^\Q$status\E/,@inststatuses) {
  223:                 $insttypechg = 1;
  224:             }
  225:         }
  226:     }
  227:     return ($insttypechg,@inststatuses);
  228: }
  229: 

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