Annotation of loncom/misc/refresh_courseids_db.pl, revision 1.3

1.1       raeburn     1: #!/usr/bin/perl
                      2: # The LearningOnline Network
                      3: #
1.3     ! raeburn     4: # $Id: refresh_courseids_db.pl,v 1.2 2010/03/15 20:13:14 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: #################################################
                     29: 
                     30: =pod
                     31: 
                     32: =head1 NAME
                     33: 
                     34: refresh_courseids_db.pl
                     35: 
                     36: =head1 SYNOPSIS
                     37: 
                     38: refresh_courseids_db.pl is run on a library server and gathers 
                     39: course information for each course for which the current server is
                     40: the home server.  Entries (excluding last access time) for each course 
                     41: in nohist_courseids.db are updated.   
                     42: 
                     43: =head1 DESCRIPTION
                     44: 
                     45: refresh_courseids_db.pl will update course information, apart 
                     46: from last access time, in nohist_courseids.db, using course data   
                     47: from each course's environment.db file.
                     48: 
                     49: =cut
                     50: 
                     51: #################################################
                     52: 
                     53: use strict;
                     54: use lib '/home/httpd/lib/perl/';
                     55: use Apache::lonnet;
                     56: use Apache::loncommon;
                     57: use LONCAPA qw(:DEFAULT :match);
                     58: 
                     59: exit if ($Apache::lonnet::perlvar{'lonRole'} ne 'library');
                     60: 
                     61: #  Make sure this process is running from user=www
                     62: my $wwwid=getpwnam('www');
                     63: if ($wwwid!=$<) {
                     64:     my $emailto="$Apache::lonnet::perlvar{'lonAdmEMail'},$Apache::lonnet::perlvar{'lonSysEMail'}";
                     65:     my $subj="LON: $Apache::lonnet::perlvar{'lonHostID'} User ID mismatch";
                     66:     system("echo 'User ID mismatch. refresh_courseids_db.pl must be run as user www.' |\
                     67:  mail -s '$subj' $emailto > /dev/null");
                     68:     exit 1;
                     69: }
                     70: #
                     71: # Let people know we are running
                     72: open(my $fh,'>>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/logs/refreshcourseids_db.log');
                     73: print $fh "==== refresh_courseids_db.pl Run ".localtime()."====\n";
                     74: 
                     75: my @domains = sort(&Apache::lonnet::current_machine_domains());
                     76: foreach my $dom (@domains) {
                     77:     my %courseshash;
                     78:     my @ids=&Apache::lonnet::current_machine_ids();
                     79:     my %currhash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@ids,'.');
                     80:     my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
1.2       raeburn    81:     my %domdesign = &Apache::loncommon::get_domainconf($dom);
                     82:     my $autoassign = $domdesign{$dom.'.autoassign.co-owners'};
                     83:     &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,$autoassign,$fh);
1.1       raeburn    84:     foreach my $lonhost (keys(%courseshash)) {
                     85:         if (ref($courseshash{$lonhost}) eq 'HASH') {
                     86:             if (&Apache::lonnet::courseidput($dom,$courseshash{$lonhost},$lonhost,'notime') eq 'ok') {
                     87:                 print $fh "nohist_courseids.db updated successfully for domain $dom on lonHostID $lonhost\n";
                     88:             } else {
                     89:                 print $fh "Error occurred when updating nohist_courseids.db for domain $dom on lonHostID $lonhost\n";
                     90:             }
                     91:         }
                     92:     }
                     93: }
                     94: 
                     95: ## Finished!
                     96: print $fh "==== refresh_courseids.db completed ".localtime()." ====\n";
                     97: close($fh);
                     98: 
                     99: sub recurse_courses {
1.2       raeburn   100:     my ($cdom,$dir,$depth,$courseshash,$currhash,$autoassign,$fh) = @_;
1.1       raeburn   101:     next unless (ref($currhash) eq 'HASH');
                    102:     if (-d $dir) {
                    103:         opendir(DIR,$dir);
                    104:         my @contents = grep(!/^\./,readdir(DIR));
                    105:         closedir(DIR);
                    106:         $depth ++;
                    107:         foreach my $item (@contents) {
                    108:             if ($depth < 4) {
1.2       raeburn   109:                 &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash,
                    110:                                  $currhash,$autoassign,$fh);
1.1       raeburn   111:             } elsif ($item =~ /^$match_courseid$/) {
                    112:                 my $cnum = $item;
                    113:                 my $cid = $cdom.'_'.$cnum;
                    114:                 unless (ref($currhash->{$cid}) eq 'HASH') {
                    115:                     my $is_course = 0;
                    116:                     if (-e "$dir/$cnum/passwd") {
                    117:                         if (open(my $pwfh,"<$dir/$cnum/passwd")) {
                    118:                             while (<$pwfh>) {
                    119:                                 if (/^none:/) {
                    120:                                     $is_course = 1;
                    121:                                     last;
                    122:                                 }
                    123:                             } 
                    124:                         }
                    125:                     }
                    126:                     next unless ($is_course);
                    127:                     my @stats = stat("$dir/$cnum/passwd");
                    128:                     print $fh "Course missing from nohist_courseids.db: $cid, created:".localtime($stats[9])."\n";
                    129:                 }
                    130:                 my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
                    131:                 my %changes = ();
                    132:                 my $crstype = $courseinfo{'type'};
                    133:                 if ($crstype eq '') {
                    134:                     if ($cnum =~ /^$match_community$/) {
                    135:                         $crstype = 'Community';
                    136:                     } else {
                    137:                         $crstype = 'Course';
                    138:                     }
                    139:                     $changes{'type'} = $crstype;
                    140:                 }
                    141:                 my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                    142:                 my $owner = $courseinfo{'internal.courseowner'};
1.2       raeburn   143:                 my (%roleshash,$gotcc);
1.1       raeburn   144:                 if ($owner eq '') {
1.2       raeburn   145:                     %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
                    146:                     $gotcc = 1;
1.1       raeburn   147:                     if (keys(%roleshash) == 1) {
                    148:                         foreach my $key (keys(%roleshash)) {
                    149:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
                    150:                                 $owner = $1;
                    151:                                 $changes{'internal.courseowner'} = $owner;
                    152:                             }
                    153:                         }
                    154:                     }
                    155:                 } elsif ($owner !~ /:/) {
                    156:                     if ($owner =~ /^$match_username$/) {
                    157:                         my $ownerhome=&Apache::lonnet::homeserver($owner,$cdom);
                    158:                         unless (($ownerhome eq '') || ($ownerhome eq 'no_host')) {
                    159:                             $owner .= ':'.$cdom;
                    160:                             $changes{'internal.courseowner'} = $owner;
                    161:                         }
                    162:                     }
                    163:                 }
                    164:                 my $created = $courseinfo{'internal.created'};
                    165:                 my $creator = $courseinfo{'internal.creator'};
                    166:                 my $creationcontext = $courseinfo{'internal.creationcontext'};
                    167:                 my $inst_code = $courseinfo{'internal.coursecode'};
                    168:                 $inst_code = '' if (!defined($inst_code));
                    169:                 $owner = '' if (!defined($owner));
                    170:                 if ($created eq '') {
1.2       raeburn   171:                     if (ref($currhash->{$cid}) eq 'HASH') {
                    172:                         $created = $currhash->{$cid}{'created'};
                    173:                         $creator = $currhash->{$cid}{'creator'};
                    174:                         $creationcontext = $currhash->{$cid}{'context'};
1.1       raeburn   175:                         unless ($created eq '') {
                    176:                             $changes{'internal.created'} = $created;
                    177:                         }
                    178:                         if ($creator =~ /^($LONCAPA::match_username):($LONCAPA::match_domain)$/) {
                    179:                              $changes{'internal.creator'} = $creator;
                    180:                         }
                    181:                         unless ($creationcontext eq '') {
                    182:                             $changes{'internal.creationcontext'} = $creationcontext;
                    183:                         }
                    184:                     }
                    185:                     if ($created eq '') {
                    186:                         if (-e "$dir/$cnum/passwd") {
                    187:                             my @stats = stat("$dir/$cnum/passwd");
                    188:                             $created = $stats[9];
                    189:                         }
                    190:                         my %lastaccess = 
                    191:                             &Apache::lonnet::courselastaccess($cdom,$cnum);
                    192:                         if ($lastaccess{$cid}) {
                    193:                             if ($created eq '') {
                    194:                                 $created = $lastaccess{$cid};
                    195:                             } elsif ($lastaccess{$cid} < $created) {
                    196:                                 $created = $lastaccess{$cid};
                    197:                             }
                    198:                         }
                    199:                         unless ($created eq '') {
                    200:                             $changes{'internal.created'} = $created;
                    201:                         }
                    202:                     }
                    203:                 }
                    204:                 unless ($chome eq 'no_host') {
                    205:                     $courseshash->{$chome}{$cid} = {
                    206:                         description => $courseinfo{'description'},
                    207:                         inst_code   => $inst_code,
                    208:                         owner       => $owner,
                    209:                         type        => $crstype,
                    210:                     };
                    211:                     if ($creator ne '') {
                    212:                         $courseshash->{$chome}{$cid}{'creator'} = $creator;
                    213:                     }
                    214:                     if ($created ne '') {
                    215:                         $courseshash->{$chome}{$cid}{'created'} = $created;
                    216:                     }
                    217:                     if ($creationcontext ne '') {
                    218:                         $courseshash->{$chome}{$cid}{'context'} = $creationcontext;
                    219:                     }
1.2       raeburn   220:                     if (($inst_code ne '') && ($autoassign)) {
                    221:                         unless ($gotcc) {
                    222:                             %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
                    223:                         }
                    224:                         my @currcoowners;
                    225:                         my @newcoowners;
                    226:                         if ($courseinfo{'internal.co-owners'} ne '') {
                    227:                             @currcoowners = split(',',$courseinfo{'internal.co-owners'});
                    228:                         }
                    229:                         foreach my $key (keys(%roleshash)) {
                    230:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
                    231:                                 my $cc = $1;
                    232:                                 unless ($cc eq $owner) {
                    233:                                     my ($result,$desc) = &Apache::lonnet::auto_validate_instcode($cnum,$cdom,$inst_code,$cc);
                    234:                                     if ($result eq 'valid') {
                    235:                                         if (@newcoowners > 0) {
                    236:                                             unless (grep(/^\Q$cc\E$/,@newcoowners)) { 
                    237:                                                 push(@newcoowners,$cc);
                    238:                                             }
                    239:                                         } else {
                    240:                                             push(@newcoowners,$cc);
                    241:                                         }
                    242:                                     }
                    243:                                 }
                    244:                             }
                    245:                         }
                    246:                         my @diffs = &Apache::loncommon::compare_arrays(\@currcoowners,\@newcoowners);
                    247:                         if (@diffs > 0) {
                    248:                             if (@newcoowners > 0) {
                    249:                                 $changes{'internal.co-owners'} = join(',',@newcoowners);
                    250:                                 $courseshash->{$chome}{$cid}{'co-owners'} = $changes{'internal.co-owners'};
                    251:                             } else {
                    252:                                 if ($courseinfo{'internal.co-owners'} ne '') {
                    253:                                     if (&Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum) eq 'ok') {
                    254:                                         print $fh "Former co-owner(s): $courseinfo{'internal.co-owners'} for official course: $inst_code (".$cdom."_".$cnum.") no longer active CCs, co-ownership status deleted.\n";
                    255:                                     }
                    256:                                 } else {
                    257:                                     print $fh "Error occurred when updating co-ownership in course's environment.db for ".$cdom."_".$cnum."\n";
                    258:                                 }
                    259:                             }
                    260:                         } elsif (@currcoowners > 0) {
                    261:                             $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
                    262:                         }
                    263:                     } elsif ($courseinfo{'internal.co-owners'} ne '') {
                    264:                         $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
                    265:                     }
1.3     ! raeburn   266:                     foreach my $item ('categories','cloners','hidefromcat') {
        !           267:                         if ($courseinfo{$item} ne '') {
        !           268:                             $courseshash->{$chome}{$cid}{$item} = $courseinfo{$item}; 
        !           269:                         }
        !           270:                     }
        !           271:                     foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
        !           272:                         if ($courseinfo{'internal.'.$item} ne '') {
        !           273:                             $courseshash->{$chome}{$cid}{$item} =
        !           274:                                 $courseinfo{'internal.'.$item};
        !           275:                         }
        !           276:                     }
1.1       raeburn   277:                     if (keys(%changes)) {
1.2       raeburn   278:                         if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') {
                    279:                             print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: ";
                    280:                             foreach my $key (sort(keys(%changes))) {
                    281:                                 print $fh "$key => $changes{$key} ";
                    282:                             }
                    283:                             print $fh "\n";
                    284:                         } else {
                    285:                             print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n";
                    286:                         }
1.1       raeburn   287:                     }
                    288:                 }
                    289:             }
                    290:         }
                    291:     }
                    292:     return;
                    293: }
                    294: 
1.2       raeburn   295: 
                    296: 

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