File:  [LON-CAPA] / loncom / enrollment / Autoupdate.pl
Revision 1.11: download - view: text, annotated - select for diffs
Thu Nov 15 21:24:56 2007 UTC (16 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_99_1, version_2_5_99_0, HEAD
Propagating changes in ID made by Domain Coordinator to classlist.db file in any courses in which user has active or future student roles. Update name changes too.

loncommon.pm
- Pass ref to %got_rules to &user_rule_check(), so retrieval of rules only occurs once per domain. Retrieve rules for both username and ID
- &instrule_disallow_msg() takes a fourth argument ($mode) so message is different for messages generated for uploaded CSV file of users and single user modification.

loncreateuser.pm
- Propagate name changes and ID change to courses if Domain Coordinator is updating user data.
- Only display permanent e-mail on results page (no personal data changes) if one is defined.
- &print_user_modification_page() takes additional argument - $context - if domain adds the checkbox to force an ID change.

lonuserutils.pm
- check for $context was mistakenly checking $setting
- check whether Domain Coordinator wishes to change authentication for existing users should be $checkauth eq 'Yes'
- propagate name changes and ID change to courses if Domain Coordinator is updating user data, and had checked boxes for force ID change, and force course changes
- &update_classlist() moved to lonuserutils.pm from Autoupdate.pl
- &propagate_id_change() added to propagate ID and name changes to courses
- include cr (for Custom role) in option for Course roles for DC when setting filters for user lists.
- summary of filters used (displayed above tabular data) is customized to be an appropriate message for the choice of filters used in all cases.
- &Apache::lonnet::flushcourselogs() after processing adds/modifications in CSV file of users called in all contexts.

Autoupdate.pl
- &update_classlist() moved to lonuserutils.pm

    1: #!/usr/bin/perl
    2: #
    3: # Automated Userinfo update script
    4: # $Id: Autoupdate.pl,v 1.11 2007/11/15 21:24:56 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:     my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoupdate.log';
   40:     my $hostid = $perlvarref->{'lonHostID'};
   41:     if (!&Apache::lonnet::is_library($hostid)) {
   42:         exit;
   43:     }
   44:     my @info = ('inststatus','lockedname','internalname','lastname',
   45:                 'firstname','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".localtime(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 (%instusers,%instids);
   69:         next if (&localenroll::allusers_info($dom,\%instusers,\%instids) ne 'ok');
   70:         my (%users,%unamechg,%possnames);
   71:         my @types = ('active','future');
   72:         my @roles = ('st');
   73:         my @cdoms = ($dom);
   74:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   75:         &descend_tree($dir,0,\%users);
   76:         foreach my $uname (keys(%users)) {
   77:             next if ($courses{$dom.'_'.$uname});
   78:             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
   79:             my (@inststatuses);
   80:             if (!$userhash{'internalname'}) {
   81:                 if (defined($instusers{$uname})) {
   82:                     (my $insttypechg,@inststatuses) = 
   83:                        &affiliations_check(\%userhash,$instusers{$uname});
   84:                     if ($insttypechg) {
   85:                         my $inststatusstr = join(':',&escape(@inststatuses));
   86:                         my %statushash = ( inststatus => $inststatusstr );
   87:                         my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
   88:                     }
   89:                 }
   90:             }
   91:             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
   92:                 if (defined($instusers{$uname})) {
   93:                     my (@fields,%changes,$changed);
   94:                     if (@inststatuses > 0) {
   95:                         foreach my $type (@inststatuses) {
   96:                             if (ref($settings->{fields}{$type}) eq 'ARRAY') {
   97:                                 foreach my $field (@{$settings->{fields}{$type}}) {
   98:                                     if (!grep(/^\Q$field\E$/,@fields)) {
   99:                                         push(@fields,$field);
  100:                                     }
  101:                                 }
  102:                             } 
  103:                         }
  104:                     } else {
  105:                         if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
  106:                             @fields = @{$settings->{fields}{'default'}};
  107:                         }
  108:                     }
  109:                     foreach my $field (@fields) { 
  110:                         if ($userhash{$field} ne $instusers{$uname}{$field}) {
  111:                             $changed = 1;
  112:                             if ($settings->{'classlists'} == 1) { 
  113:                                 if ($field eq 'id') {
  114:                                     $changes{'id'} = 1;
  115:                                 } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'gen') {
  116:                                     $changes{'fullname'} = 1;
  117:                                 }
  118:                             }
  119:                         }
  120:                     }
  121:                     # Make the change
  122:                     if ($changed) {
  123:                         my %userupdate;
  124:                         foreach my $field (@fields) {
  125:                             $userupdate{$field} = $instusers{$uname}{$field};
  126:                         }
  127:                         my $modresult = &Apache::lonnet::modifyuser($dom,$uname,$userupdate{'id'},undef,undef,$userupdate{'firstname'},$userupdate{'middlename'},$userupdate{'lastname'},$userupdate{'generation'},1,$hostid,$userupdate{'permanentemail'});
  128:                         if ($modresult eq 'ok') {
  129:                             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";
  130:                             if ($settings->{'classlists'} == 1) {
  131:                                 if ($changes{'id'} || $changes{'fullname'}) {
  132:                                     my %roleshash = 
  133:                                         &Apache::lonnet::get_my_roles($uname,
  134:                                                   $dom,'userroles',\@types,\@roles,\@cdoms);
  135:                                     foreach my $item (keys(%roleshash)) {
  136:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
  137:                                         my ($start,$end) = split(/:/,$roleshash{$item});
  138:                                         if (&Apache::lonnet::is_course($cdom,$cnum)) {
  139:                                             my $result = 
  140:                                                 &Apache::lonuserutils::update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
  141:                                             if ($result eq 'ok') {
  142:                                                 print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n";
  143:                                             } else {
  144:                                                 print $fh "Error - $result -during classlist update for $uname:$dom in $cnum:$cdom\n";
  145:                                             }
  146:                                         }
  147:                                     }
  148:                                 }
  149:                             }
  150:                         } else {
  151:                             print $fh "User change detected for $uname:$dom. Error during update: $modresult\n";
  152:                         }
  153:                     }
  154:                 } else {
  155:                     # check if the username has changed
  156:                     if (defined($instids{$userhash{'id'}})) {
  157:                         if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
  158:                             foreach my $name (@{$instids{$userhash{'id'}}}) {
  159:                                 if (!exists($users{$name})) {
  160:                                     push(@{$possnames{$uname}},$name);
  161:                                 }
  162:                             }
  163:                         } else {
  164:                             if (!exists($users{$instids{$userhash{'id'}}})) {
  165:                                 $unamechg{$uname} = $instids{$userhash{'id'}};
  166:                                 print $fh (&mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n");
  167:                             }
  168:                         }
  169:                     }
  170:                 }
  171:             }
  172:         }
  173:         if (keys(%possnames) > 0) {
  174:             foreach my $uname (keys(%possnames)) {
  175:                 my $altnames = join(' or ',@{$possnames{$uname}});
  176:                 print $fh (&mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n");
  177:             }
  178:         }
  179:         print $fh ("-- \n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n");
  180:         close($fh);
  181:     }
  182: 
  183: sub descend_tree {
  184:     my ($dir,$depth,$alldomusers) = @_;
  185:     if (-d $dir) {
  186:         opendir(DIR,$dir);
  187:         my @contents = grep(!/^\./,readdir(DIR));
  188:         closedir(DIR);
  189:         $depth ++;
  190:         foreach my $item (@contents) {
  191:             if ($depth < 4) {
  192:                 &descend_tree($dir.'/'.$item,$depth,$alldomusers);
  193:             } else {
  194:                 $$alldomusers{$item} = '';
  195:             }
  196:         }
  197:     }
  198: }
  199: 
  200: sub affiliations_check {
  201:     my ($userhash,$insthashref) = @_;
  202:     my (@inststatuses,$insttypechg);;
  203:     if (ref($insthashref) eq 'HASH') {
  204:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
  205:             @inststatuses = @{$insthashref->{'inststatus'}};
  206:         }
  207:     }
  208:     my @currstatuses = &unescape(split(/:/,$userhash->{'inststatus'}));
  209:     foreach my $status (@inststatuses) {
  210:         if (!grep/^\Q$status\E/,@currstatuses) {
  211:             $insttypechg = 1;
  212:         }
  213:     }
  214:     if (!$insttypechg) {
  215:         foreach my $status (@currstatuses) {
  216:             if (!grep/^\Q$status\E/,@inststatuses) {
  217:                 $insttypechg = 1;
  218:             }
  219:         }
  220:     }
  221:     return ($insttypechg,@inststatuses);
  222: }
  223: 

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