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