--- loncom/enrollment/Autoupdate.pl 2021/09/01 02:14:42 1.24 +++ loncom/enrollment/Autoupdate.pl 2023/12/10 17:43:06 1.28 @@ -1,7 +1,7 @@ #!/usr/bin/perl # # Automated Userinfo update script -# $Id: Autoupdate.pl,v 1.24 2021/09/01 02:14:42 raeburn Exp $ +# $Id: Autoupdate.pl,v 1.28 2023/12/10 17:43:06 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -49,7 +49,7 @@ # find out which users we need to examine my @domains = sort(&Apache::lonnet::current_machine_domains()); foreach my $dom (@domains) { - my %domconfig = &Apache::lonnet::get_dom('configuration',['autoupdate'], + my %domconfig = &Apache::lonnet::get_dom('configuration',['autoupdate','usercreation'], $dom); #only run if configured to my $run_update = 0; @@ -74,27 +74,41 @@ # get courseIDs for domain my %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.'); # get user information - my (%users,%instusers,%instids); + my (%users,%instusers,%instids,%counts,@idrules); my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom; &descend_tree($dom,$dir,0,\%users,\%courses,$allowed_inactivity,$check_unexpired); - my $resp = &localenroll::allusers_info($dom,\%instusers,\%instids,\%users); + my $resp = &localenroll::allusers_info($dom,\%instusers,\%instids,\%users,\%counts); if ($resp ne 'ok') { print $fh &mt('Problem retrieving institutional data for users in domain: [_1].',$dom)."\n". &mt('Error: [_1].',$resp)."\n". "-- \n".&Apache::lonlocal::locallocaltime(time).' '. &mt('Autoupdate messages end')."\n*******************\n\n"; + close($fh); next; + } else { + if (keys(%counts)) { + foreach my $key (sort(keys(%counts))) { + if ($counts{$key} ne '') { + print $fh "Count for $key = $counts{$key}\n"; + } + } + } } my (%unamechg,%possnames); my @types = ('active','future'); my @roles = ('st'); my @cdoms = ($dom); + if (ref($domconfig{'usercreation'}) eq 'HASH') { + if (ref($domconfig{'usercreation'}{'id_rule'}) eq 'ARRAY') { + @idrules = @{$domconfig{'usercreation'}{'id_rule'}}; + } + } foreach my $uname (sort(keys(%users))) { my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info); my (@inststatuses); if (!$userhash{'internalname'}) { if (defined($instusers{$uname})) { - (my $insttypechg,@inststatuses) = + (my $insttypechg,@inststatuses) = &affiliations_check(\%userhash,$instusers{$uname}); if ($insttypechg) { my $inststatusstr = join(':',map { &escape($_) } (@inststatuses)); @@ -114,21 +128,68 @@ push(@fields,$field); } } - } + } } } else { if (ref($settings->{fields}{'default'}) eq 'ARRAY') { @fields = @{$settings->{fields}{'default'}}; } } - foreach my $field (@fields) { + foreach my $field (@fields) { if ($userhash{$field} ne $instusers{$uname}{$field}) { - $changed = 1; - if ($settings->{'classlists'} == 1) { - if ($field eq 'id') { - $changes{'id'} = 1; - } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'generation') { - $changes{'fullname'} = 1; + # Do not change student/employee ID when field is modifiable + # but institutional data is absent or does not match rules + # defined for IDs for institutional users in domain + if ($field eq 'id') { + if ($instusers{$uname}{$field} eq '') { + $instusers{$uname}{$field} = $userhash{$field}; + next; + } elsif (@idrules > 0) { + my %idchecks; + if (&localenroll::id_check($dom,$instusers{$uname}{$field}, + \@idrules,\%idchecks) eq 'ok') { + my $matches; + foreach my $result (values(%idchecks)) { + if ($result) { + $matches = 1; + last; + } + } + if ($matches) { + $changed = 1; + } else { + $instusers{$uname}{$field} = $userhash{$field}; + next; + } + } + } + } else { + $changed = 1; + } + if ($field eq 'id') { + $changes{'id'} = 1; + } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'generation') { + $changes{'fullname'} = 1; + } + } + } + # Do not change name information when lastname and firstname are modifiable + # but institutional data is absent for both components of a user's name. + if ($changed) { + if ($changes{'fullname'}) { + my %modifiable; + map { $modifiable{$_} = 1; } @fields; + if (($modifiable{'lastname'}) && ($modifiable{'firstname'}) && + ($instusers{$uname}{'lastname'} eq '') && ($instusers{$uname}{'firstname'} eq '')) { + if ($changes{'fullname'}) { + undef($changes{'fullname'}); + } + if ($changes{'id'}) { + foreach my $field ('lastname','firstname','middlename','generation') { + $instusers{$uname}{$field} = $userhash{$field}; + } + } else { + undef($changed); } } } @@ -148,14 +209,14 @@ 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"; if ($settings->{'classlists'} == 1) { if ($changes{'id'} || $changes{'fullname'}) { - my %roleshash = + my %roleshash = &Apache::lonnet::get_my_roles($uname, $dom,'userroles',\@types,\@roles,\@cdoms); foreach my $item (keys(%roleshash)) { my ($cnum,$cdom,$role) = split(/:/,$item); my ($start,$end) = split(/:/,$roleshash{$item}); if (&Apache::lonnet::is_course($cdom,$cnum)) { - my $result = + my $result = &Apache::lonuserutils::update_classlist($cdom,$cnum,$dom,$uname,\%userupdate); if ($result eq 'ok') { print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n";