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

1.1     ! raeburn     1: #!/usr/bin/perl
        !             2: # The LearningOnline Network
        !             3: #
        !             4: # $Id: refresh_courseids_db.pl,v 1.1 2010/03/14 19:00:40 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: #################################################
        !            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;
        !            81:     &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,$fh);
        !            82:     foreach my $lonhost (keys(%courseshash)) {
        !            83:         if (ref($courseshash{$lonhost}) eq 'HASH') {
        !            84:             if (&Apache::lonnet::courseidput($dom,$courseshash{$lonhost},$lonhost,'notime') eq 'ok') {
        !            85:                 print $fh "nohist_courseids.db updated successfully for domain $dom on lonHostID $lonhost\n";
        !            86:             } else {
        !            87:                 print $fh "Error occurred when updating nohist_courseids.db for domain $dom on lonHostID $lonhost\n";
        !            88:             }
        !            89:         }
        !            90:     }
        !            91: }
        !            92: 
        !            93: ## Finished!
        !            94: print $fh "==== refresh_courseids.db completed ".localtime()." ====\n";
        !            95: close($fh);
        !            96: 
        !            97: sub recurse_courses {
        !            98:     my ($cdom,$dir,$depth,$courseshash,$currhash,$fh) = @_;
        !            99:     next unless (ref($currhash) eq 'HASH');
        !           100:     if (-d $dir) {
        !           101:         opendir(DIR,$dir);
        !           102:         my @contents = grep(!/^\./,readdir(DIR));
        !           103:         closedir(DIR);
        !           104:         $depth ++;
        !           105:         foreach my $item (@contents) {
        !           106:             if ($depth < 4) {
        !           107:                 &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash,$currhash,$fh);
        !           108:             } elsif ($item =~ /^$match_courseid$/) {
        !           109:                 my $cnum = $item;
        !           110:                 my $cid = $cdom.'_'.$cnum;
        !           111:                 unless (ref($currhash->{$cid}) eq 'HASH') {
        !           112:                     my $is_course = 0;
        !           113:                     if (-e "$dir/$cnum/passwd") {
        !           114:                         if (open(my $pwfh,"<$dir/$cnum/passwd")) {
        !           115:                             while (<$pwfh>) {
        !           116:                                 if (/^none:/) {
        !           117:                                     $is_course = 1;
        !           118:                                     last;
        !           119:                                 }
        !           120:                             } 
        !           121:                         }
        !           122:                     }
        !           123:                     next unless ($is_course);
        !           124:                     my @stats = stat("$dir/$cnum/passwd");
        !           125:                     print $fh "Course missing from nohist_courseids.db: $cid, created:".localtime($stats[9])."\n";
        !           126:                 }
        !           127:                 my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
        !           128:                 my %changes = ();
        !           129:                 my $crstype = $courseinfo{'type'};
        !           130:                 if ($crstype eq '') {
        !           131:                     if ($cnum =~ /^$match_community$/) {
        !           132:                         $crstype = 'Community';
        !           133:                     } else {
        !           134:                         $crstype = 'Course';
        !           135:                     }
        !           136:                     $changes{'type'} = $crstype;
        !           137:                 }
        !           138:                 my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
        !           139:                 my $owner = $courseinfo{'internal.courseowner'};
        !           140:                 if ($owner eq '') {
        !           141:                     my %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
        !           142:                     if (keys(%roleshash) == 1) {
        !           143:                         foreach my $key (keys(%roleshash)) {
        !           144:                             if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
        !           145:                                 $owner = $1;
        !           146:                                 $changes{'internal.courseowner'} = $owner;
        !           147:                             }
        !           148:                         }
        !           149:                     }
        !           150:                 } elsif ($owner !~ /:/) {
        !           151:                     if ($owner =~ /^$match_username$/) {
        !           152:                         my $ownerhome=&Apache::lonnet::homeserver($owner,$cdom);
        !           153:                         unless (($ownerhome eq '') || ($ownerhome eq 'no_host')) {
        !           154:                             $owner .= ':'.$cdom;
        !           155:                             $changes{'internal.courseowner'} = $owner;
        !           156:                         }
        !           157:                     }
        !           158:                 }
        !           159:                 my $created = $courseinfo{'internal.created'};
        !           160:                 my $creator = $courseinfo{'internal.creator'};
        !           161:                 my $creationcontext = $courseinfo{'internal.creationcontext'};
        !           162:                 my $inst_code = $courseinfo{'internal.coursecode'};
        !           163:                 $inst_code = '' if (!defined($inst_code));
        !           164:                 $owner = '' if (!defined($owner));
        !           165:                 if ($created eq '') {
        !           166:                     my %currdump = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
        !           167:                     if (ref($currdump{$cid}) eq 'HASH') {
        !           168:                         $created = $currdump{$cid}{'created'};
        !           169:                         $creator = $currdump{$cid}{'creator'};
        !           170:                         $creationcontext = $currdump{$cid}{'context'};
        !           171:                         unless ($created eq '') {
        !           172:                             $changes{'internal.created'} = $created;
        !           173:                         }
        !           174:                         if ($creator =~ /^($LONCAPA::match_username):($LONCAPA::match_domain)$/) {
        !           175:                              $changes{'internal.creator'} = $creator;
        !           176:                         }
        !           177:                         unless ($creationcontext eq '') {
        !           178:                             $changes{'internal.creationcontext'} = $creationcontext;
        !           179:                         }
        !           180:                     }
        !           181:                     if ($created eq '') {
        !           182:                         if (-e "$dir/$cnum/passwd") {
        !           183:                             my @stats = stat("$dir/$cnum/passwd");
        !           184:                             $created = $stats[9];
        !           185:                         }
        !           186:                         my %lastaccess = 
        !           187:                             &Apache::lonnet::courselastaccess($cdom,$cnum);
        !           188:                         if ($lastaccess{$cid}) {
        !           189:                             if ($created eq '') {
        !           190:                                 $created = $lastaccess{$cid};
        !           191:                             } elsif ($lastaccess{$cid} < $created) {
        !           192:                                 $created = $lastaccess{$cid};
        !           193:                             }
        !           194:                         }
        !           195:                         unless ($created eq '') {
        !           196:                             $changes{'internal.created'} = $created;
        !           197:                         }
        !           198:                     }
        !           199:                 }
        !           200:                 unless ($chome eq 'no_host') {
        !           201:                     $courseshash->{$chome}{$cid} = {
        !           202:                         description => $courseinfo{'description'},
        !           203:                         inst_code   => $inst_code,
        !           204:                         owner       => $owner,
        !           205:                         type        => $crstype,
        !           206:                     };
        !           207:                     if ($courseinfo{'internal.co-owners'} ne '') {
        !           208:                         $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
        !           209:                     }
        !           210:                     if ($creator ne '') {
        !           211:                         $courseshash->{$chome}{$cid}{'creator'} = $creator;
        !           212:                     }
        !           213:                     if ($created ne '') {
        !           214:                         $courseshash->{$chome}{$cid}{'created'} = $created;
        !           215:                     }
        !           216:                     if ($creationcontext ne '') {
        !           217:                         $courseshash->{$chome}{$cid}{'context'} = $creationcontext;
        !           218:                     }
        !           219:                     if (keys(%changes)) {
        !           220:                        if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') {
        !           221:                            print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: ";
        !           222:                            foreach my $key (sort(keys(%changes))) {
        !           223:                                print $fh "$key => $changes{$key} "; 
        !           224:                            }
        !           225:                            print $fh "\n";
        !           226:                        } else {
        !           227:                            print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n";
        !           228:                        }
        !           229:                     }
        !           230:                 }
        !           231:             }
        !           232:         }
        !           233:     }
        !           234:     return;
        !           235: }
        !           236: 

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