1: #!/usr/bin/perl
2: #
3: #Automated Enrollment script
4: # $Id: Autoenroll.pl,v 1.5 2003/12/11 15:39:37 albertel 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: use strict;
30: use lib '/home/httpd/lib/perl';
31: use localenroll;
32: use LONCAPA::Configuration;
33: use LONCAPA::Enrollment;
34: use Apache::lonnet;
35: use Apache::loncoursedata;
36: use Apache::lonmsg;
37: use HTML::Entities;
38:
39: # Determine the library server's domain
40: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
41: my $dom = $$perlvarref{'lonDefDomain'};
42: my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
43: $ENV{'user.domain'} = $dom;
44:
45: # Determine the present time;
46: my $timenow = time();
47:
48: # Determine the courses
49: my %courses = &Apache::lonnet::courseiddump($dom,'.',1);
50: my %affiliates = ();
51: my %enrollvar = ();
52: my %reply = ();
53: my %LC_code = ();
54: foreach my $key (sort keys %courses) {
55: my $crs;
56: if ($key =~ m/^($dom)_(\w+)$/) {
57: $crs = $2;
58: }
59:
60: # Get course settings
61: my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
62: %{$enrollvar{$crs}} = ();
63: @{$affiliates{$crs}} = ();
64: %{$LC_code{$crs}} = ();
65: foreach my $item (keys %settings) {
66: if ($item =~ m/^internal\.(.+)$/) {
67: $enrollvar{$crs}{$1} = $settings{$item};
68: } elsif ($item eq 'description') {
69: $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});
70: }
71: }
72: if (($enrollvar{$crs}{autostart} <= $timenow) && ($enrollvar{$crs}{autoend} > $timenow)) {
73: if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
74: # Add to list of classes for retrieval
75: $enrollvar{$crs}{sectionnums} =~ s/ //g;
76: $enrollvar{$crs}{crosslistings} =~ s/ //g;
77: my @sections = ();
78: my @crosslistings = ();
79: if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
80: @sections = split/,/,$enrollvar{$crs}{sectionnums};
81: } else {
82: $sections[0] = $enrollvar{$crs}{sectionnums};
83: }
84: if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
85: @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
86: } else {
87: @crosslistings = $enrollvar{$crs}{crosslistings};
88: }
89: foreach my $sec (@sections) {
90: if ($sec =~ m/^(\w+):(\w*)$/ ) {
91: my $course_id = $enrollvar{$crs}{coursecode}.$1;
92: my $gp = $2;
93: if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
94: push @{$affiliates{$crs}}, $course_id;
95: $LC_code{$crs}{$course_id} = $gp;
96: }
97: }
98: }
99: foreach my $xlist (@crosslistings) {
100: if ($xlist =~ m/^(\w+):(\w*)$/) {
101: my $course_id = $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: }
110: }
111: }
112: &localenroll::fetch_enrollment($dom,\%affiliates,\%reply);
113:
114: # Now go through classes and perform required enrollment changes.
115: open (my $fh,">>$logfile");
116: print $fh "********************\n".localtime(time)." Enrollment messages start --\n";
117: foreach my $crs (sort keys %enrollvar) {
118: my $logmsg = '';
119: my $newusermsg = '';
120: if ($reply{$crs} > 0) {
121: if (($enrollvar{$crs}{autostart} < $timenow) && ($enrollvar{$crs}{autoend} > $timenow)) {
122: if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
123: 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');
124: print $fh "Messages start for $crs\n";
125: print $fh "$logmsg\n";
126: print $fh "Messages end for $crs\n";
127: if ($changecount > 0) {
128: unless ($enrollvar{$crs}{notifylist} eq '') {
129: # Send message about enrollment changes to notifylist.
130: # Set $ENV{'user.name'}, $ENV{'user.home'} for use by logging in lonmsg
131: unless ( ($enrollvar{$crs}{'courseowner'} eq '') || (!defined($enrollvar{$crs}{'courseowner'}) ) ) {
132: $ENV{'user.name'} = $enrollvar{$crs}{'courseowner'};
133: $ENV{'user.home'} = &Apache::lonnet::homeserver($ENV{'user.name'},$dom);
134:
135: my $subject = "Student enrollment changes in $enrollvar{$crs}{coursecode}";
136: 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;
137: unless ($newusermsg eq '')
138: {
139: $message .= "\n".$newusermsg;
140: }
141: my @to_notify = ();
142: if ($enrollvar{$crs}{notifylist} =~ m/,/) {
143: @to_notify = split/,/,$enrollvar{$crs}{notifylist};
144: } else {
145: $to_notify[0] = $enrollvar{$crs}{notifylist};
146: }
147: foreach my $cc (@to_notify) {
148: my ($ccname,$ccdom) = split/@/,$cc;
149: my $status = &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message);
150: }
151: if ( ($enrollvar{$crs}{notifylist} eq '') && ($newusermsg ne '') ) {
152: my $subject = "New user accounts in $enrollvar{$crs}{'coursecode'}";
153: my $status = &Apache::lonmsg::user_normal_msg($ENV{'user.name'},$dom,$subject,$newusermsg);
154: }
155: delete($ENV{'user.name'});
156: delete($ENV{'user.home'});
157: }
158: }
159: }
160: }
161: }
162: } else {
163: print $fh "No institutional classlist data could be retrieved for $crs\n";
164: }
165: }
166: print $fh "-- ".localtime(time)." Enrollment messages end\n*******************\n\n";
167: close($fh);
168: delete($ENV{'user.domain'});
169:
170: # Check for photos
171:
172: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>