1: #!/usr/bin/perl
2: #
3: #Automated Enrollment script
4: # $Id: Autoenroll.pl,v 1.23 2007/01/01 19:19:58 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 LONCAPA::Configuration;
32: use LONCAPA::Enrollment;
33: use Apache::lonnet;
34: use Apache::loncoursedata;
35: use Apache::lonmsg;
36: use Apache::longroup;
37: use Apache::loncommon;
38: use HTML::Entities;
39:
40: # Determine the library server's domain and hostID
41: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
42: my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
43: my @domains = &Apache::lonnet::current_machine_domains();
44: my @hostids = &Apache::lonnet::current_machine_ids();
45:
46: # Determine the present time;
47: my $timenow = time();
48:
49: # For each domain ......
50: foreach my $dom (@domains) {
51: #only run if configured to
52: if (! &localenroll::run($dom)) { next; }
53: $env{'user.domain'} = $dom;
54: # Determine the courses
55: my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course');
56: my %affiliates = ();
57: my %enrollvar = ();
58: my %reply = ();
59: my %LC_code = ();
60: foreach my $key (sort keys %courses) {
61: my $crs;
62: if ($key =~ m/^($dom)_(\w+)$/) {
63: $crs = $2;
64: }
65:
66: # Get course settings
67: my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
68: %{$enrollvar{$crs}} = ();
69: @{$affiliates{$crs}} = ();
70: %{$LC_code{$crs}} = ();
71: foreach my $item (keys %settings) {
72: if ($item =~ m/^internal\.(.+)$/) {
73: $enrollvar{$crs}{$1} = $settings{$item};
74: } elsif ($item eq 'description') {
75: $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});
76: } elsif ($item eq 'default_enrollment_start_date') {
77: $enrollvar{$crs}{startdate} = $settings{$item};
78: } elsif ($item eq 'default_enrollment_end_date') {
79: $enrollvar{$crs}{enddate} = $settings{$item};
80: }
81: }
82: if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
83: if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
84: # Add to list of classes for retrieval
85: $enrollvar{$crs}{sectionnums} =~ s/ //g;
86: $enrollvar{$crs}{crosslistings} =~ s/ //g;
87: my @sections = ();
88: my @crosslistings = ();
89: if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
90: @sections = split/,/,$enrollvar{$crs}{sectionnums};
91: } else {
92: $sections[0] = $enrollvar{$crs}{sectionnums};
93: }
94: if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
95: @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
96: } else {
97: @crosslistings = $enrollvar{$crs}{crosslistings};
98: }
99: foreach my $sec (@sections) {
100: if ($sec =~ m/^(\w+):(\w*)$/ ) {
101: my $course_id = $enrollvar{$crs}{coursecode}.$1;
102: my $gp = $2;
103: if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
104: push @{$affiliates{$crs}}, $course_id;
105: $LC_code{$crs}{$course_id} = $gp;
106: }
107: }
108: }
109: foreach my $xlist (@crosslistings) {
110: if ($xlist =~ m/^([^:]+):(\w*)$/) {
111: my $course_id = $1;
112: my $gp = $2;
113: if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
114: push @{$affiliates{$crs}}, $course_id;
115: $LC_code{$crs}{$course_id} = $gp;
116: }
117: }
118: }
119: }
120: }
121: }
122: my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom);
123:
124: # Now go through classes and perform required enrollment changes.
125: open (my $fh,">>$logfile");
126: print $fh "********************\n".localtime(time)." Enrollment messages start --\n";
127: print $fh "Response from fetch_enrollment_query was $outcome\n";
128: foreach my $crs (sort keys %enrollvar) {
129: my $logmsg = '';
130: my $newusermsg = '';
131: if ($reply{$crs} > 0) {
132: if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
133: if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
134: my ($changecount,$response) = &LONCAPA::Enrollment::update_LC($dom,$crs,$enrollvar{$crs}{autoadds},$enrollvar{$crs}{autodrops},$enrollvar{$crs}{startdate},$enrollvar{$crs}{enddate},$enrollvar{$crs}{authtype},$enrollvar{$crs}{autharg},\@{$affiliates{$crs}},\%{$LC_code{$crs}},\$logmsg,\$newusermsg,'automated');
135: print $fh "Messages start for $crs\n";
136: print $fh "$logmsg\n";
137: print $fh "Messages end for $crs\n";
138: if ($changecount > 0) {
139: unless ($enrollvar{$crs}{notifylist} eq '') {
140: # Send message about enrollment changes to notifylist.
141: # Set $env{'user.name'}, $env{'user.domain'}, $env{'user.home'}
142: # for use by logging in lonmsg
143: unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) ) ) {
144: if ($enrollvar{$crs}{'courseowner'} =~ /:/) {
145: ($env{'user.name'},$env{'user.domain'}) = split(/:/,$enrollvar{$crs}{'courseowner'});
146: } else {
147: $env{'user.name'} = $enrollvar{$crs}{'courseowner'};
148: $env{'user.domain'} = $dom;
149: }
150: $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
151:
152: my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}";
153: my $message = "The following $changecount change(s) occurred in $enrollvar{$crs}{description} - $enrollvar{$crs}{coursecode} as a result of the automated classlist update:\n\n".$response;
154: unless ($newusermsg eq '') {
155: $message .= "\n".$newusermsg;
156: }
157: my @to_notify = ();
158: if ($enrollvar{$crs}{notifylist} =~ m/,/) {
159: @to_notify = split/,/,$enrollvar{$crs}{notifylist};
160: } else {
161: $to_notify[0] = $enrollvar{$crs}{notifylist};
162: }
163: foreach my $cc (@to_notify) {
164: my ($ccname,$ccdom);
165: if ($cc =~ /:/) {
166: ($ccname,$ccdom) = split(/:/,$cc);
167: } elsif ($cc =~ /\@/) {
168: ($ccname,$ccdom) = split(/\@/,$cc);
169: }
170: my $status = &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);
171: }
172: if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {
173: my $subject = "New user accounts in $enrollvar{$crs}{'coursecode'}";
174: my $status = &Apache::lonmsg::user_normal_msg($env{'user.name'},$env{'user.domain'},$subject,$newusermsg);
175: }
176: delete($env{'user.name'});
177: delete($env{'user.home'});
178: $env{'user.domain'} = $dom;
179: }
180: }
181: }
182: }
183: }
184: } else {
185: if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
186: if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
187: print $fh "No institutional classlist data could be retrieved for $crs\n";
188: } else {
189: print $fh "Not within time window for auto-enrollment in $crs\n";
190: }
191: } else {
192: print $fh "Auto-enrollment not currently enabled for $crs\n";
193: }
194: }
195: }
196: print $fh "-- ".localtime(time)." Enrollment messages end\n*******************\n\n";
197: close($fh);
198: delete($env{'user.domain'});
199: # Check for photos
200: }
201:
202: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>