[LON-CAPA-cvs] cvs: loncom /misc refresh_courseids_db.pl

raeburn raeburn@source.lon-capa.org
Sun, 14 Mar 2010 23:17:54 -0000


This is a MIME encoded message

--raeburn1268608674
Content-Type: text/plain

raeburn		Sun Mar 14 23:17:54 2010 EDT

  Added files:                 
    /loncom/misc	refresh_courseids_db.pl 
  Log:
  - Bug 5967.
   - Contents of nohist_courseids.db (excluding lastaccess) rebuilt from permanent data in
     course environment.db files.
   - Will determine course owner and/or creation date for old courses created before these items
     were in use, and update course's environment.db file accordingly.
  
  
--raeburn1268608674
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20100314231754.txt"


Index: loncom/misc/refresh_courseids_db.pl
+++ loncom/misc/refresh_courseids_db.pl
#!/usr/bin/perl
# The LearningOnline Network
#
# $Id: refresh_courseids_db.pl,v 1.1 2010/03/14 23:17:54 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
#################################################

=pod

=head1 NAME

refresh_courseids_db.pl

=head1 SYNOPSIS

refresh_courseids_db.pl is run on a library server and gathers 
course information for each course for which the current server is
the home server.  Entries (excluding last access time) for each course 
in nohist_courseids.db are updated.   

=head1 DESCRIPTION

refresh_courseids_db.pl will update course information, apart 
from last access time, in nohist_courseids.db, using course data   
from each course's environment.db file.

=cut

#################################################

use strict;
use lib '/home/httpd/lib/perl/';
use Apache::lonnet;
use Apache::loncommon;
use LONCAPA qw(:DEFAULT :match);

exit if ($Apache::lonnet::perlvar{'lonRole'} ne 'library');

#  Make sure this process is running from user=www
my $wwwid=getpwnam('www');
if ($wwwid!=$<) {
    my $emailto="$Apache::lonnet::perlvar{'lonAdmEMail'},$Apache::lonnet::perlvar{'lonSysEMail'}";
    my $subj="LON: $Apache::lonnet::perlvar{'lonHostID'} User ID mismatch";
    system("echo 'User ID mismatch. refresh_courseids_db.pl must be run as user www.' |\
 mail -s '$subj' $emailto > /dev/null");
    exit 1;
}
#
# Let people know we are running
open(my $fh,'>>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/logs/refreshcourseids_db.log');
print $fh "==== refresh_courseids_db.pl Run ".localtime()."====\n";

my @domains = sort(&Apache::lonnet::current_machine_domains());
foreach my $dom (@domains) {
    my %courseshash;
    my @ids=&Apache::lonnet::current_machine_ids();
    my %currhash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@ids,'.');
    my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
    &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,$fh);
    foreach my $lonhost (keys(%courseshash)) {
        if (ref($courseshash{$lonhost}) eq 'HASH') {
            if (&Apache::lonnet::courseidput($dom,$courseshash{$lonhost},$lonhost,'notime') eq 'ok') {
                print $fh "nohist_courseids.db updated successfully for domain $dom on lonHostID $lonhost\n";
            } else {
                print $fh "Error occurred when updating nohist_courseids.db for domain $dom on lonHostID $lonhost\n";
            }
        }
    }
}

## Finished!
print $fh "==== refresh_courseids.db completed ".localtime()." ====\n";
close($fh);

sub recurse_courses {
    my ($cdom,$dir,$depth,$courseshash,$currhash,$fh) = @_;
    next unless (ref($currhash) eq 'HASH');
    if (-d $dir) {
        opendir(DIR,$dir);
        my @contents = grep(!/^\./,readdir(DIR));
        closedir(DIR);
        $depth ++;
        foreach my $item (@contents) {
            if ($depth < 4) {
                &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash,$currhash,$fh);
            } elsif ($item =~ /^$match_courseid$/) {
                my $cnum = $item;
                my $cid = $cdom.'_'.$cnum;
                unless (ref($currhash->{$cid}) eq 'HASH') {
                    my $is_course = 0;
                    if (-e "$dir/$cnum/passwd") {
                        if (open(my $pwfh,"<$dir/$cnum/passwd")) {
                            while (<$pwfh>) {
                                if (/^none:/) {
                                    $is_course = 1;
                                    last;
                                }
                            } 
                        }
                    }
                    next unless ($is_course);
                    my @stats = stat("$dir/$cnum/passwd");
                    print $fh "Course missing from nohist_courseids.db: $cid, created:".localtime($stats[9])."\n";
                }
                my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
                my %changes = ();
                my $crstype = $courseinfo{'type'};
                if ($crstype eq '') {
                    if ($cnum =~ /^$match_community$/) {
                        $crstype = 'Community';
                    } else {
                        $crstype = 'Course';
                    }
                    $changes{'type'} = $crstype;
                }
                my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                my $owner = $courseinfo{'internal.courseowner'};
                if ($owner eq '') {
                    my %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
                    if (keys(%roleshash) == 1) {
                        foreach my $key (keys(%roleshash)) {
                            if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
                                $owner = $1;
                                $changes{'internal.courseowner'} = $owner;
                            }
                        }
                    }
                } elsif ($owner !~ /:/) {
                    if ($owner =~ /^$match_username$/) {
                        my $ownerhome=&Apache::lonnet::homeserver($owner,$cdom);
                        unless (($ownerhome eq '') || ($ownerhome eq 'no_host')) {
                            $owner .= ':'.$cdom;
                            $changes{'internal.courseowner'} = $owner;
                        }
                    }
                }
                my $created = $courseinfo{'internal.created'};
                my $creator = $courseinfo{'internal.creator'};
                my $creationcontext = $courseinfo{'internal.creationcontext'};
                my $inst_code = $courseinfo{'internal.coursecode'};
                $inst_code = '' if (!defined($inst_code));
                $owner = '' if (!defined($owner));
                if ($created eq '') {
                    my %currdump = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                    if (ref($currdump{$cid}) eq 'HASH') {
                        $created = $currdump{$cid}{'created'};
                        $creator = $currdump{$cid}{'creator'};
                        $creationcontext = $currdump{$cid}{'context'};
                        unless ($created eq '') {
                            $changes{'internal.created'} = $created;
                        }
                        if ($creator =~ /^($LONCAPA::match_username):($LONCAPA::match_domain)$/) {
                             $changes{'internal.creator'} = $creator;
                        }
                        unless ($creationcontext eq '') {
                            $changes{'internal.creationcontext'} = $creationcontext;
                        }
                    }
                    if ($created eq '') {
                        if (-e "$dir/$cnum/passwd") {
                            my @stats = stat("$dir/$cnum/passwd");
                            $created = $stats[9];
                        }
                        my %lastaccess = 
                            &Apache::lonnet::courselastaccess($cdom,$cnum);
                        if ($lastaccess{$cid}) {
                            if ($created eq '') {
                                $created = $lastaccess{$cid};
                            } elsif ($lastaccess{$cid} < $created) {
                                $created = $lastaccess{$cid};
                            }
                        }
                        unless ($created eq '') {
                            $changes{'internal.created'} = $created;
                        }
                    }
                }
                unless ($chome eq 'no_host') {
                    $courseshash->{$chome}{$cid} = {
                        description => $courseinfo{'description'},
                        inst_code   => $inst_code,
                        owner       => $owner,
                        type        => $crstype,
                    };
                    if ($courseinfo{'internal.co-owners'} ne '') {
                        $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
                    }
                    if ($creator ne '') {
                        $courseshash->{$chome}{$cid}{'creator'} = $creator;
                    }
                    if ($created ne '') {
                        $courseshash->{$chome}{$cid}{'created'} = $created;
                    }
                    if ($creationcontext ne '') {
                        $courseshash->{$chome}{$cid}{'context'} = $creationcontext;
                    }
                    if (keys(%changes)) {
                       if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') {
                           print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: ";
                           foreach my $key (sort(keys(%changes))) {
                               print $fh "$key => $changes{$key} "; 
                           }
                           print $fh "\n";
                       } else {
                           print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n";
                       }
                    }
                }
            }
        }
    }
    return;
}


--raeburn1268608674--