Annotation of loncom/misc/refresh_courseids_db.pl, revision 1.21
1.1 raeburn 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: #
1.21 ! raeburn 4: # $Id: refresh_courseids_db.pl,v 1.20 2016/01/27 22:23:09 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;
1.4 raeburn 57: use Apache::lonuserstate;
58: use Apache::loncoursedata;
59: use Apache::lonnavmaps;
1.19 raeburn 60: use Apache::lonrelrequtils;
1.1 raeburn 61: use LONCAPA qw(:DEFAULT :match);
62:
63: exit if ($Apache::lonnet::perlvar{'lonRole'} ne 'library');
64:
65: # Make sure this process is running from user=www
66: my $wwwid=getpwnam('www');
67: if ($wwwid!=$<) {
68: my $emailto="$Apache::lonnet::perlvar{'lonAdmEMail'},$Apache::lonnet::perlvar{'lonSysEMail'}";
69: my $subj="LON: $Apache::lonnet::perlvar{'lonHostID'} User ID mismatch";
70: system("echo 'User ID mismatch. refresh_courseids_db.pl must be run as user www.' |\
71: mail -s '$subj' $emailto > /dev/null");
72: exit 1;
73: }
74: #
75: # Let people know we are running
76: open(my $fh,'>>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/logs/refreshcourseids_db.log');
77: print $fh "==== refresh_courseids_db.pl Run ".localtime()."====\n";
78:
79: my @domains = sort(&Apache::lonnet::current_machine_domains());
1.8 raeburn 80: my @ids=&Apache::lonnet::current_machine_ids();
1.4 raeburn 81:
1.19 raeburn 82: &Apache::lonrelrequtils::init_global_hashes();
83: my $globals_set = 1;
84:
1.4 raeburn 85: $env{'allowed.bre'} = 'F';
86:
1.1 raeburn 87: foreach my $dom (@domains) {
1.14 raeburn 88: $env{'user.domain'} = $dom;
89: $env{'user.name'} = &Apache::lonnet::get_domainconfiguser($dom);
1.1 raeburn 90: my %courseshash;
91: my %currhash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@ids,'.');
1.8 raeburn 92: my %lastaccess = &Apache::lonnet::courselastaccess($dom,undef,\@ids);
1.1 raeburn 93: my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
1.2 raeburn 94: my %domdesign = &Apache::loncommon::get_domainconf($dom);
95: my $autoassign = $domdesign{$dom.'.autoassign.co-owners'};
1.8 raeburn 96: &recurse_courses($dom,$dir,0,\%courseshash,\%currhash,\%lastaccess,$autoassign,$fh);
1.1 raeburn 97: foreach my $lonhost (keys(%courseshash)) {
98: if (ref($courseshash{$lonhost}) eq 'HASH') {
99: if (&Apache::lonnet::courseidput($dom,$courseshash{$lonhost},$lonhost,'notime') eq 'ok') {
100: print $fh "nohist_courseids.db updated successfully for domain $dom on lonHostID $lonhost\n";
101: } else {
102: print $fh "Error occurred when updating nohist_courseids.db for domain $dom on lonHostID $lonhost\n";
103: }
104: }
105: }
1.15 bisitz 106: delete($env{'user.name'});
1.14 raeburn 107: delete($env{'user.domain'});
1.1 raeburn 108: }
109:
1.4 raeburn 110: delete($env{'allowed.bre'});
111:
1.1 raeburn 112: ## Finished!
113: print $fh "==== refresh_courseids.db completed ".localtime()." ====\n";
114: close($fh);
115:
116: sub recurse_courses {
1.8 raeburn 117: my ($cdom,$dir,$depth,$courseshash,$currhash,$lastaccess,$autoassign,$fh) = @_;
1.1 raeburn 118: next unless (ref($currhash) eq 'HASH');
119: if (-d $dir) {
120: opendir(DIR,$dir);
121: my @contents = grep(!/^\./,readdir(DIR));
122: closedir(DIR);
123: $depth ++;
124: foreach my $item (@contents) {
1.20 raeburn 125: if (($depth < 4) && (length($item) == 1)) {
1.2 raeburn 126: &recurse_courses($cdom,$dir.'/'.$item,$depth,$courseshash,
1.8 raeburn 127: $currhash,$lastaccess,$autoassign,$fh);
1.1 raeburn 128: } elsif ($item =~ /^$match_courseid$/) {
129: my $cnum = $item;
130: my $cid = $cdom.'_'.$cnum;
131: unless (ref($currhash->{$cid}) eq 'HASH') {
132: my $is_course = 0;
133: if (-e "$dir/$cnum/passwd") {
134: if (open(my $pwfh,"<$dir/$cnum/passwd")) {
135: while (<$pwfh>) {
136: if (/^none:/) {
137: $is_course = 1;
138: last;
139: }
140: }
141: }
142: }
143: next unless ($is_course);
144: my @stats = stat("$dir/$cnum/passwd");
145: print $fh "Course missing from nohist_courseids.db: $cid, created:".localtime($stats[9])."\n";
146: }
147: my %courseinfo=&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
148: my %changes = ();
149: my $crstype = $courseinfo{'type'};
150: if ($crstype eq '') {
151: if ($cnum =~ /^$match_community$/) {
152: $crstype = 'Community';
153: } else {
154: $crstype = 'Course';
155: }
156: $changes{'type'} = $crstype;
157: }
158: my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
159: my $owner = $courseinfo{'internal.courseowner'};
1.8 raeburn 160: my $twodaysago = time - 172800;
1.4 raeburn 161: my (%roleshash,$gotcc,$reqdmajor,$reqdminor);
1.1 raeburn 162: if ($owner eq '') {
1.2 raeburn 163: %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
164: $gotcc = 1;
1.1 raeburn 165: if (keys(%roleshash) == 1) {
166: foreach my $key (keys(%roleshash)) {
167: if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
168: $owner = $1;
169: $changes{'internal.courseowner'} = $owner;
170: }
171: }
172: }
173: } elsif ($owner !~ /:/) {
174: if ($owner =~ /^$match_username$/) {
175: my $ownerhome=&Apache::lonnet::homeserver($owner,$cdom);
176: unless (($ownerhome eq '') || ($ownerhome eq 'no_host')) {
177: $owner .= ':'.$cdom;
178: $changes{'internal.courseowner'} = $owner;
179: }
180: }
181: }
182: my $created = $courseinfo{'internal.created'};
183: my $creator = $courseinfo{'internal.creator'};
184: my $creationcontext = $courseinfo{'internal.creationcontext'};
185: my $inst_code = $courseinfo{'internal.coursecode'};
1.8 raeburn 186: my $releaserequired = $courseinfo{'internal.releaserequired'};
1.17 raeburn 187: my $uniquecode = $courseinfo{'internal.uniquecode'};
1.1 raeburn 188: $inst_code = '' if (!defined($inst_code));
189: $owner = '' if (!defined($owner));
1.17 raeburn 190: $uniquecode = '' if (!defined($uniquecode));
1.1 raeburn 191: if ($created eq '') {
1.2 raeburn 192: if (ref($currhash->{$cid}) eq 'HASH') {
193: $created = $currhash->{$cid}{'created'};
194: $creator = $currhash->{$cid}{'creator'};
195: $creationcontext = $currhash->{$cid}{'context'};
1.1 raeburn 196: unless ($created eq '') {
197: $changes{'internal.created'} = $created;
198: }
199: if ($creator =~ /^($LONCAPA::match_username):($LONCAPA::match_domain)$/) {
200: $changes{'internal.creator'} = $creator;
201: }
202: unless ($creationcontext eq '') {
203: $changes{'internal.creationcontext'} = $creationcontext;
204: }
205: }
206: if ($created eq '') {
207: if (-e "$dir/$cnum/passwd") {
208: my @stats = stat("$dir/$cnum/passwd");
209: $created = $stats[9];
210: }
1.8 raeburn 211: if ($lastaccess->{$cid}) {
1.1 raeburn 212: if ($created eq '') {
1.8 raeburn 213: $created = $lastaccess->{$cid};
214: } elsif ($lastaccess->{$cid} < $created) {
215: $created = $lastaccess->{$cid};
1.1 raeburn 216: }
217: }
218: unless ($created eq '') {
219: $changes{'internal.created'} = $created;
220: }
221: }
222: }
1.8 raeburn 223:
224: if (($chome ne '') && ($lastaccess->{$cid} > $twodaysago)) {
225: $env{'request.course.id'} = $cdom.'_'.$cnum;
226: $env{'request.role'} = 'cc./'.$cdom.'/'.$cnum;
227:
1.19 raeburn 228: my $readmap = 1;
229: ($reqdmajor,$reqdminor) = &Apache::lonrelrequtils::get_release_req($cnum,$cdom,
230: $crstype,$readmap,
231: $globals_set);
1.8 raeburn 232: delete($env{'request.course.id'});
233: delete($env{'request.role'});
234: } elsif ($releaserequired) {
235: ($reqdmajor,$reqdminor) = split(/\./,$releaserequired);
236: }
1.4 raeburn 237:
1.1 raeburn 238: unless ($chome eq 'no_host') {
1.13 raeburn 239: if (($lastaccess->{$cid} eq '') ||
240: ($lastaccess->{$cid} > $twodaysago)) {
241: my $contentchange;
242: if ($courseinfo{'internal.created'} eq '') {
243: $contentchange = &last_map_update($cnum,$cdom);
244: } else {
245: unless ($courseinfo{'internal.created'} > $lastaccess->{$cid}) {
246: $contentchange = &last_map_update($cnum,$cdom);
247: }
248: }
249: if (($contentchange) && ($contentchange > $courseinfo{'internal.contentchange'})) {
250: $changes{'internal.contentchange'} = $contentchange;
251: }
252: }
1.1 raeburn 253: $courseshash->{$chome}{$cid} = {
254: description => $courseinfo{'description'},
255: inst_code => $inst_code,
256: owner => $owner,
257: type => $crstype,
258: };
259: if ($creator ne '') {
260: $courseshash->{$chome}{$cid}{'creator'} = $creator;
261: }
262: if ($created ne '') {
263: $courseshash->{$chome}{$cid}{'created'} = $created;
264: }
265: if ($creationcontext ne '') {
266: $courseshash->{$chome}{$cid}{'context'} = $creationcontext;
267: }
1.2 raeburn 268: if (($inst_code ne '') && ($autoassign)) {
269: unless ($gotcc) {
270: %roleshash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,undef,['cc'],undef,undef,1);
271: }
272: my @currcoowners;
273: my @newcoowners;
274: if ($courseinfo{'internal.co-owners'} ne '') {
275: @currcoowners = split(',',$courseinfo{'internal.co-owners'});
276: }
277: foreach my $key (keys(%roleshash)) {
278: if ($key =~ /^($match_username\:$match_domain)\:cc$/) {
279: my $cc = $1;
280: unless ($cc eq $owner) {
281: my ($result,$desc) = &Apache::lonnet::auto_validate_instcode($cnum,$cdom,$inst_code,$cc);
282: if ($result eq 'valid') {
283: if (@newcoowners > 0) {
284: unless (grep(/^\Q$cc\E$/,@newcoowners)) {
285: push(@newcoowners,$cc);
286: }
287: } else {
288: push(@newcoowners,$cc);
289: }
290: }
291: }
292: }
293: }
294: my @diffs = &Apache::loncommon::compare_arrays(\@currcoowners,\@newcoowners);
295: if (@diffs > 0) {
296: if (@newcoowners > 0) {
297: $changes{'internal.co-owners'} = join(',',@newcoowners);
298: $courseshash->{$chome}{$cid}{'co-owners'} = $changes{'internal.co-owners'};
299: } else {
300: if ($courseinfo{'internal.co-owners'} ne '') {
301: if (&Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum) eq 'ok') {
302: 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";
303: }
304: } else {
305: print $fh "Error occurred when updating co-ownership in course's environment.db for ".$cdom."_".$cnum."\n";
306: }
307: }
308: } elsif (@currcoowners > 0) {
309: $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
310: }
311: } elsif ($courseinfo{'internal.co-owners'} ne '') {
312: $courseshash->{$chome}{$cid}{'co-owners'} = $courseinfo{'internal.co-owners'};
313: }
1.3 raeburn 314: foreach my $item ('categories','cloners','hidefromcat') {
315: if ($courseinfo{$item} ne '') {
316: $courseshash->{$chome}{$cid}{$item} = $courseinfo{$item};
317: }
318: }
1.17 raeburn 319: foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date','uniquecode') {
1.3 raeburn 320: if ($courseinfo{'internal.'.$item} ne '') {
321: $courseshash->{$chome}{$cid}{$item} =
322: $courseinfo{'internal.'.$item};
323: }
324: }
1.6 raeburn 325: if ($reqdmajor eq '' && $reqdminor eq '') {
326: if ($courseinfo{'internal.releaserequired'} ne '') {
327: $changes{'internal.releaserequired'} = '';
328: }
329: } else {
330: my $releasereq = $reqdmajor.'.'.$reqdminor;
331: $courseshash->{$chome}{$cid}{'releaserequired'} = $releasereq;
332: if ($courseinfo{'internal.releaserequired'} eq '') {
333: $changes{'internal.releaserequired'} = $releasereq;
334: } else {
335: if ($courseinfo{'internal.releaserequired'} ne $releasereq) {
336: $changes{'internal.releaserequired'} = $releasereq;
337: }
338: }
1.4 raeburn 339: }
1.1 raeburn 340: if (keys(%changes)) {
1.2 raeburn 341: if (&Apache::lonnet::put('environment',\%changes,$cdom,$cnum) eq 'ok') {
342: print $fh "Course's environment.db for ".$cdom."_".$cnum." successfully updated with following entries: ";
343: foreach my $key (sort(keys(%changes))) {
344: print $fh "$key => $changes{$key} ";
345: }
346: print $fh "\n";
347: } else {
348: print $fh "Error occurred when updating course's environment.db for ".$cdom."_".$cnum."\n";
349: }
1.1 raeburn 350: }
351: }
352: }
353: }
354: }
355: return;
356: }
357:
1.13 raeburn 358: sub last_map_update {
359: my ($cnum,$cdom) = @_;
360: my $lastupdate = 0;
361: my $path = &LONCAPA::propath($cdom,$cnum);
362: if (-d "$path/userfiles") {
363: if (opendir(my $dirh, "$path/userfiles")) {
364: my @maps = grep(/^default_?\d*\.(?:sequence|page)$/,readdir($dirh));
365: foreach my $map (@maps) {
366: my $mtime = (stat("$path/userfiles/$map"))[9];
367: if ($mtime > $lastupdate) {
368: $lastupdate = $mtime;
369: }
370: }
371: }
372: }
373: return $lastupdate;
374: }
375:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>