Annotation of loncom/enrollment/Autoupdate.pl, revision 1.6
1.1 raeburn 1: #!/usr/bin/perl
2: #
3: # Automated Userinfo update script
1.6 ! raeburn 4: # $Id: Autoupdate.pl,v 1.5 2007/05/11 17:25:28 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.5 raeburn 34: use LONCAPA::Configuration;
1.2 raeburn 35: use LONCAPA;
1.1 raeburn 36:
1.5 raeburn 37: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
1.6 ! raeburn 38: my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoupdate.log';
1.5 raeburn 39: my $hostid = $perlvarref->{'lonHostID'};
1.6 ! raeburn 40: my @info = ('inststatus','lockedname','internalname','lastname',
! 41: 'firstname','id');
! 42: # Initialize language handler
! 43: &Apache::lonlocal::get_language_handle();
1.1 raeburn 44: # find out which users we need to examine
45: my @domains = sort(&Apache::lonnet::current_machine_domains());
46: foreach my $dom (@domains) {
1.5 raeburn 47: my $primaryhost_id = &Apache::lonnet::domain($dom,'primary');
48: if ($primaryhost_id ne $hostid) {
49: next;
50: }
1.1 raeburn 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.6 ! raeburn 63: open (my $fh,">>$logfile");
! 64: print $fh "********************\n".localtime(time).' '.&mt('Autoupdate messages start for domain: [_1]',$dom).' --'."\n";
1.2 raeburn 65: # get user information
66: my (%instusers,%instids);
67: next if (&localenroll::allusers_info($dom,\%instusers,\%instids) ne 'ok');
68: my (%users,%unamechg,%possnames);
1.1 raeburn 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);
1.2 raeburn 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: }
1.1 raeburn 88: if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
1.2 raeburn 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'}};
1.1 raeburn 104: }
105: }
106: foreach my $field (@fields) {
1.2 raeburn 107: if ($userhash{$field} ne $instusers{$uname}{$field}) {
1.1 raeburn 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) {
1.2 raeburn 122: $userupdate{$field} = $instusers{$uname}{$field};
1.1 raeburn 123: }
1.2 raeburn 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') {
1.1 raeburn 126: if ($settings->{'classlists'} eq 'yes') {
127: if ($changes{'id'} || $changes{'fullname'}) {
128: my %roleshash =
129: &Apache::lonnet::get_my_roles($uname,
1.4 raeburn 130: $dom,'userroles',\@types,\@roles,\@cdoms);
1.1 raeburn 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: }
1.2 raeburn 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'}};
1.6 ! raeburn 154: print $fh &mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n";
1.2 raeburn 155: }
156: }
157: }
1.1 raeburn 158: }
159: }
160: }
1.4 raeburn 161: if (keys(%possnames) > 0) {
162: foreach my $uname (keys(%possnames)) {
163: my $altnames = join(' or ',@{$possnames{$uname}});
1.6 ! raeburn 164: print $fh &mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n";
1.4 raeburn 165: }
166: }
1.6 ! raeburn 167: print $fh "-- ".localtime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n";
! 168: close($fh);
1.1 raeburn 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:
1.2 raeburn 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>