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