Annotation of loncom/enrollment/Autoupdate.pl, revision 1.23

1.1       raeburn     1: #!/usr/bin/perl
                      2: #
                      3: # Automated Userinfo update script
1.23    ! raeburn     4: # $Id: Autoupdate.pl,v 1.22 2011/12/18 01:23:21 raeburn Exp $
1.1       raeburn     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;
1.4       raeburn    33:     use Apache::lonlocal;
1.11      raeburn    34:     use Apache::lonuserutils();
1.5       raeburn    35:     use LONCAPA::Configuration;
1.2       raeburn    36:     use LONCAPA;
1.1       raeburn    37: 
1.5       raeburn    38:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
1.19      raeburn    39:     exit if (ref($perlvarref) ne 'HASH');
                     40:     my $logfile = $perlvarref->{'lonDaemons'}.'/logs/autoupdate.log';
                     41:     exit if ($perlvarref->{'lonRole'} ne 'library');
1.10      raeburn    42:     my $hostid = $perlvarref->{'lonHostID'};
1.19      raeburn    43: 
1.6       raeburn    44:     my @info = ('inststatus','lockedname','internalname','lastname',
1.14      raeburn    45:                 'firstname','middlename','generation','id','permanentemail');
1.6       raeburn    46:     # Initialize language handler
                     47:     &Apache::lonlocal::get_language_handle();
1.1       raeburn    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'};
1.2       raeburn    58:             if ($settings->{'run'} eq '1') {
1.1       raeburn    59:                 $run_update = 1;
                     60:             }
                     61:         }
                     62:         next if (!$run_update);
1.7       albertel   63:         open(my $fh,">>$logfile");
1.17      raeburn    64:         print $fh ("********************\n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages start for domain: [_1]',$dom).' --'."\n");
1.9       raeburn    65:         # get courseIDs for domain
1.10      raeburn    66:         my %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.');
1.2       raeburn    67:         # get user information
1.20      raeburn    68:         my (%users,%instusers,%instids);
                     69:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
1.21      raeburn    70:         &descend_tree($dom,$dir,0,\%users,\%courses);
1.22      raeburn    71:         my $resp = &localenroll::allusers_info($dom,\%instusers,\%instids,\%users);
                     72:         if ($resp ne 'ok') {
                     73:             print $fh &mt('Problem retrieving institutional data for users in domain: [_1].',$dom)."\n".
                     74:                       &mt('Error: [_1].',$resp)."\n".
                     75:                       "-- \n".&Apache::lonlocal::locallocaltime(time).' '.
                     76:                       &mt('Autoupdate messages end')."\n*******************\n\n";
                     77:             next;
                     78:         }
1.20      raeburn    79:         my (%unamechg,%possnames);
1.1       raeburn    80:         my @types = ('active','future');
                     81:         my @roles = ('st');
                     82:         my @cdoms = ($dom);
1.18      raeburn    83:         foreach my $uname (sort(keys(%users))) {
1.1       raeburn    84:             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
1.2       raeburn    85:             my (@inststatuses);
                     86:             if (!$userhash{'internalname'}) {
                     87:                 if (defined($instusers{$uname})) {
                     88:                     (my $insttypechg,@inststatuses) = 
                     89:                        &affiliations_check(\%userhash,$instusers{$uname});
                     90:                     if ($insttypechg) {
1.13      raeburn    91:                         my $inststatusstr = join(':',map { &escape($_) } (@inststatuses));
1.2       raeburn    92:                         my %statushash = ( inststatus => $inststatusstr );
                     93:                         my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
                     94:                     }
                     95:                 }
                     96:             }
1.1       raeburn    97:             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
1.2       raeburn    98:                 if (defined($instusers{$uname})) {
                     99:                     my (@fields,%changes,$changed);
                    100:                     if (@inststatuses > 0) {
                    101:                         foreach my $type (@inststatuses) {
                    102:                             if (ref($settings->{fields}{$type}) eq 'ARRAY') {
                    103:                                 foreach my $field (@{$settings->{fields}{$type}}) {
                    104:                                     if (!grep(/^\Q$field\E$/,@fields)) {
                    105:                                         push(@fields,$field);
                    106:                                     }
                    107:                                 }
                    108:                             } 
                    109:                         }
                    110:                     } else {
                    111:                         if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
                    112:                             @fields = @{$settings->{fields}{'default'}};
1.1       raeburn   113:                         }
                    114:                     }
                    115:                     foreach my $field (@fields) { 
1.2       raeburn   116:                         if ($userhash{$field} ne $instusers{$uname}{$field}) {
1.1       raeburn   117:                             $changed = 1;
1.9       raeburn   118:                             if ($settings->{'classlists'} == 1) { 
1.1       raeburn   119:                                 if ($field eq 'id') {
                    120:                                     $changes{'id'} = 1;
1.15      raeburn   121:                                 } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'generation') {
1.1       raeburn   122:                                     $changes{'fullname'} = 1;
                    123:                                 }
                    124:                             }
1.9       raeburn   125:                         }
1.1       raeburn   126:                     }
                    127:                     # Make the change
                    128:                     if ($changed) {
                    129:                         my %userupdate;
1.14      raeburn   130:                         foreach my $item ('lastname','firstname','middlename','generation','id',
                    131:                                           'permanentemail') {
                    132:                             $userupdate{$item} = $userhash{$item};
                    133:                         }
1.1       raeburn   134:                         foreach my $field (@fields) {
1.2       raeburn   135:                             $userupdate{$field} = $instusers{$uname}{$field};
1.1       raeburn   136:                         }
1.16      raeburn   137:                         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);
1.2       raeburn   138:                         if ($modresult eq 'ok') {
1.9       raeburn   139:                             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";
                    140:                             if ($settings->{'classlists'} == 1) {
1.1       raeburn   141:                                 if ($changes{'id'} || $changes{'fullname'}) {
                    142:                                     my %roleshash = 
                    143:                                         &Apache::lonnet::get_my_roles($uname,
1.4       raeburn   144:                                                   $dom,'userroles',\@types,\@roles,\@cdoms);
1.10      raeburn   145:                                     foreach my $item (keys(%roleshash)) {
1.1       raeburn   146:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
                    147:                                         my ($start,$end) = split(/:/,$roleshash{$item});
1.9       raeburn   148:                                         if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.11      raeburn   149:                                             my $result = 
                    150:                                                 &Apache::lonuserutils::update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
1.9       raeburn   151:                                             if ($result eq 'ok') {
                    152:                                                 print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n";
                    153:                                             } else {
                    154:                                                 print $fh "Error - $result -during classlist update for $uname:$dom in $cnum:$cdom\n";
                    155:                                             }
1.1       raeburn   156:                                         }
                    157:                                     }
                    158:                                 }
                    159:                             }
1.9       raeburn   160:                         } else {
                    161:                             print $fh "User change detected for $uname:$dom. Error during update: $modresult\n";
1.1       raeburn   162:                         }
                    163:                     }
1.2       raeburn   164:                 } else {
                    165:                     # check if the username has changed
                    166:                     if (defined($instids{$userhash{'id'}})) {
                    167:                         if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
                    168:                             foreach my $name (@{$instids{$userhash{'id'}}}) {
                    169:                                 if (!exists($users{$name})) {
                    170:                                     push(@{$possnames{$uname}},$name);
                    171:                                 }
                    172:                             }
                    173:                         } else {
                    174:                             if (!exists($users{$instids{$userhash{'id'}}})) {
                    175:                                 $unamechg{$uname} = $instids{$userhash{'id'}};
1.7       albertel  176:                                 print $fh (&mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n");
1.2       raeburn   177:                             }
                    178:                         }
                    179:                     }
1.1       raeburn   180:                 }
                    181:             }
                    182:         }
1.4       raeburn   183:         if (keys(%possnames) > 0) {
                    184:             foreach my $uname (keys(%possnames)) {
                    185:                 my $altnames = join(' or ',@{$possnames{$uname}});
1.7       albertel  186:                 print $fh (&mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n");
1.4       raeburn   187:             }
                    188:         }
1.9       raeburn   189:         print $fh ("-- \n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n");
1.6       raeburn   190:         close($fh);
1.1       raeburn   191:     }
                    192: 
                    193: sub descend_tree {
1.21      raeburn   194:     my ($dom,$dir,$depth,$alldomusers,$coursesref) = @_;
1.1       raeburn   195:     if (-d $dir) {
                    196:         opendir(DIR,$dir);
                    197:         my @contents = grep(!/^\./,readdir(DIR));
                    198:         closedir(DIR);
                    199:         $depth ++;
                    200:         foreach my $item (@contents) {
1.23    ! raeburn   201:             if (($depth < 4) && (length($item) == 1)) {
1.21      raeburn   202:                 &descend_tree($dom,$dir.'/'.$item,$depth,$alldomusers,$coursesref);
1.19      raeburn   203:             } elsif (-e $dir.'/'.$item.'/passwd') {
1.20      raeburn   204:                 if (ref($coursesref) eq 'HASH') {
1.21      raeburn   205:                     next if (exists($coursesref->{$dom.'_'.$item}));
1.20      raeburn   206:                 }
1.9       raeburn   207:                 $$alldomusers{$item} = '';
1.1       raeburn   208:             }
                    209:         }
                    210:     }
                    211: }
                    212: 
1.2       raeburn   213: sub affiliations_check {
                    214:     my ($userhash,$insthashref) = @_;
                    215:     my (@inststatuses,$insttypechg);;
                    216:     if (ref($insthashref) eq 'HASH') {
1.8       raeburn   217:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                    218:             @inststatuses = @{$insthashref->{'inststatus'}};
1.2       raeburn   219:         }
                    220:     }
1.12      raeburn   221:     my @currstatuses = map { &unescape($_); } (split(/:/,$userhash->{'inststatus'}));
1.2       raeburn   222:     foreach my $status (@inststatuses) {
                    223:         if (!grep/^\Q$status\E/,@currstatuses) {
                    224:             $insttypechg = 1;
                    225:         }
                    226:     }
                    227:     if (!$insttypechg) {
                    228:         foreach my $status (@currstatuses) {
                    229:             if (!grep/^\Q$status\E/,@inststatuses) {
                    230:                 $insttypechg = 1;
                    231:             }
                    232:         }
                    233:     }
                    234:     return ($insttypechg,@inststatuses);
                    235: }
                    236: 

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