Annotation of loncom/enrollment/Autoenroll.pl, revision 1.34
1.2 raeburn 1: #!/usr/bin/perl
1.5 albertel 2: #
3: #Automated Enrollment script
1.34 ! raeburn 4: # $Id: Autoenroll.pl,v 1.33 2013/03/01 05:20:01 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.27 raeburn 40: use LONCAPA qw(:match);
1.6 albertel 41:
1.9 raeburn 42: # Determine the library server's domain and hostID
1.2 raeburn 43: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
1.4 raeburn 44: my $logfile = $$perlvarref{'lonDaemons'}.'/logs/autoenroll.log';
1.11 raeburn 45: my @domains = &Apache::lonnet::current_machine_domains();
46: my @hostids = &Apache::lonnet::current_machine_ids();
1.1 raeburn 47:
48: # Determine the present time;
1.2 raeburn 49: my $timenow = time();
1.1 raeburn 50:
1.11 raeburn 51: # For each domain ......
52: foreach my $dom (@domains) {
1.26 raeburn 53: #only run if configured to
54: my $run_enroll = 0;
1.34 ! raeburn 55: my ($domsettings,$autofailsafe);
1.26 raeburn 56: my %domconfig =
1.33 raeburn 57: &Apache::lonnet::get_dom('configuration',['autoenroll','coursedefaults'],$dom);
1.26 raeburn 58: if (ref($domconfig{'autoenroll'}) eq 'HASH') {
1.30 raeburn 59: $domsettings = $domconfig{'autoenroll'};
60: if ($domsettings->{'run'} eq '1') {
1.26 raeburn 61: $run_enroll = 1;
62: }
1.34 ! raeburn 63: if ($domsettings->{'autofailsafe'} ne '') {
! 64: $autofailsafe = $domsettings->{'autofailsafe'};
! 65: }
1.26 raeburn 66: } else {
67: $run_enroll = &localenroll::run($dom);
68: }
69: next if ($run_enroll != 1);
70:
1.13 albertel 71: $env{'user.domain'} = $dom;
1.24 raeburn 72: # Initialize language handler
73: &Apache::lonlocal::get_language_handle();
74: # Determine the courses
1.21 raeburn 75: my %courses = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@hostids,'Course');
1.11 raeburn 76: my %affiliates = ();
77: my %enrollvar = ();
78: my %reply = ();
79: my %LC_code = ();
1.33 raeburn 80: my ($showcredits,$domdefcredits);
81: if ((ref($domconfig{'coursedefaults'}) eq 'HASH') &&
82: (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH')) {
83: if ($domconfig{'coursedefaults'}{'coursecredits'}{'official'}) {
84: $showcredits = 1;
85: $domdefcredits = $domconfig{'coursedefaults'}{'coursecredits'}{'official'};
86: }
87: }
1.11 raeburn 88: foreach my $key (sort keys %courses) {
1.28 raeburn 89: my %args = (
90: one_time => 1,
91: );
1.1 raeburn 92: # Get course settings
1.32 raeburn 93: my %settings = &Apache::lonnet::coursedescription($key,\%args);
94: my $crs = $settings{'num'};
95: next if ($settings{'domain'} ne $dom);
1.11 raeburn 96: %{$enrollvar{$crs}} = ();
97: @{$affiliates{$crs}} = ();
98: %{$LC_code{$crs}} = ();
99: foreach my $item (keys %settings) {
100: if ($item =~ m/^internal\.(.+)$/) {
101: $enrollvar{$crs}{$1} = $settings{$item};
102: } elsif ($item eq 'description') {
103: $enrollvar{$crs}{$item} = &HTML::Entities::decode($settings{$item});
104: } elsif ($item eq 'default_enrollment_start_date') {
105: $enrollvar{$crs}{startdate} = $settings{$item};
1.33 raeburn 106: } elsif ($item eq 'default_enrollment_end_date') {
1.11 raeburn 107: $enrollvar{$crs}{enddate} = $settings{$item};
108: }
1.2 raeburn 109: }
1.11 raeburn 110: if (($enrollvar{$crs}{autostart} <= $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
111: if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
1.1 raeburn 112: # Add to list of classes for retrieval
1.11 raeburn 113: $enrollvar{$crs}{sectionnums} =~ s/ //g;
114: $enrollvar{$crs}{crosslistings} =~ s/ //g;
115: my @sections = ();
116: my @crosslistings = ();
117: if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
118: @sections = split/,/,$enrollvar{$crs}{sectionnums};
119: } else {
120: $sections[0] = $enrollvar{$crs}{sectionnums};
121: }
122: if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
123: @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
124: } else {
125: @crosslistings = $enrollvar{$crs}{crosslistings};
126: }
127: foreach my $sec (@sections) {
128: if ($sec =~ m/^(\w+):(\w*)$/ ) {
129: my $course_id = $enrollvar{$crs}{coursecode}.$1;
130: my $gp = $2;
1.32 raeburn 131: if (!grep/^\Q$course_id\E$/,@{$affiliates{$crs}}) {
1.11 raeburn 132: push @{$affiliates{$crs}}, $course_id;
133: $LC_code{$crs}{$course_id} = $gp;
134: }
1.2 raeburn 135: }
136: }
1.11 raeburn 137: foreach my $xlist (@crosslistings) {
1.15 raeburn 138: if ($xlist =~ m/^([^:]+):(\w*)$/) {
1.11 raeburn 139: my $course_id = $1;
140: my $gp = $2;
1.32 raeburn 141: if (!grep/^\Q$course_id\E$/,@{$affiliates{$crs}}) {
1.11 raeburn 142: push @{$affiliates{$crs}}, $course_id;
143: $LC_code{$crs}{$course_id} = $gp;
144: }
1.2 raeburn 145: }
146: }
147: }
148: }
149: }
1.11 raeburn 150: my $outcome = &Apache::lonnet::fetch_enrollment_query('automated',\%affiliates,\%reply,$dom);
1.1 raeburn 151:
152: # Now go through classes and perform required enrollment changes.
1.11 raeburn 153: open (my $fh,">>$logfile");
1.25 raeburn 154: print $fh "********************\n".localtime(time).' '.&mt('Enrollment messages start').' --'."\n";
155: print $fh &mt("Response from [_1] was [_2]",'fetch_enrollment_query',$outcome)."\n";
1.11 raeburn 156: foreach my $crs (sort keys %enrollvar) {
157: my $logmsg = '';
158: my $newusermsg = '';
159: if ($reply{$crs} > 0) {
160: if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
161: if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
1.33 raeburn 162: my $defaultcredits = $domdefcredits;
163: if ($enrollvar{$crs}{defaultcredits}) {
164: $defaultcredits = $enrollvar{$crs}{defaultcredits};
165: }
1.34 ! raeburn 166: my $failsafe = $autofailsafe;
! 167: if ($enrollvar{$crs}{'autodropfailsafe'}) {
! 168: $failsafe = $enrollvar{$crs}{'autodropfailsafe'};
! 169: }
! 170: 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},$showcredits,$defaultcredits,$failsafe,$affiliates{$crs},$LC_code{$crs},\$logmsg,\$newusermsg,'automated');
1.25 raeburn 171: print $fh &mt('Messages start for [_1]',$crs)."\n";
1.11 raeburn 172: print $fh "$logmsg\n";
1.25 raeburn 173: print $fh &mt('Messages end for [_1]',$crs)."\n";
1.11 raeburn 174: if ($changecount > 0) {
1.23 raeburn 175: # Set $env{'user.name'}, $env{'user.domain'}, $env{'user.home'}
1.27 raeburn 176: # and $env{'request.course.id'} for use by logging in lonmsg
177: $env{'request.course.id'} = $dom.'_'.$crs;
178: my ($ownername,$ownerdom);
179: if ($enrollvar{$crs}{'courseowner'} ne '') {
180: if ($enrollvar{$crs}{'courseowner'} =~ /:/) {
181: ($ownername,$ownerdom) =
182: split(/:/,$enrollvar{$crs}{'courseowner'});
183: } else {
184: $ownername = $enrollvar{$crs}{'courseowner'};
185: $ownerdom = $dom;
186: }
187: }
1.30 raeburn 188: $env{'user.name'} = $ownername;
189: $env{'user.domain'} = $ownerdom;
190: if (ref($domsettings) eq 'HASH') {
191: if (($domsettings->{'sender_uname'} ne '') &&
192: ($domsettings->{'sender_domain'} ne '')) {
1.31 raeburn 193: $env{'user.name'} = $domsettings->{'sender_uname'};
194: $env{'user.domain'} = $domsettings->{'sender_domain'};
1.30 raeburn 195: }
1.27 raeburn 196: }
197: if ($enrollvar{$crs}{notifylist} eq '') {
198: if ($newusermsg ne '') {
199: if (($ownername =~ /^$match_username$/) &&
200: ($ownerdom =~ /^$match_domain$/)) {
201: my $subject = &mt('New user accounts in [_1]',$enrollvar{$crs}{'coursecode'});
202: my $status = &Apache::lonmsg::user_normal_msg($ownername,$ownerdom,$subject,$newusermsg);
1.23 raeburn 203: }
1.27 raeburn 204: }
205: } else {
1.30 raeburn 206: $env{'form.can_reply'} = 'N';
1.27 raeburn 207: my $msgcc;
208: # Send message about enrollment changes to notifylist.
209: $env{'user.home'} = &Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
210: my $subject = &mt('Student enrollment changes in [_1]',$enrollvar{$crs}{coursecode});
211: 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;
212: unless ($newusermsg eq '') {
213: $message .= "\n".$newusermsg;
214: }
215: my @to_notify = split(/,/,$enrollvar{$crs}{notifylist});
216: my $numsent = 0;
217: my @recusers;
218: my @recudoms;
219: foreach my $cc (@to_notify) {
220: my ($ccname,$ccdom);
221: if ($cc =~ /:/) {
222: ($ccname,$ccdom) = split(/:/,$cc);
223: } elsif ($cc =~ /\@/) {
224: ($ccname,$ccdom) = split(/\@/,$cc);
1.11 raeburn 225: }
1.27 raeburn 226: push(@recusers,$ccname);
227: push(@recudoms,$ccdom);
228: $msgcc->{$ccname.':'.$ccdom}='';
229: $numsent ++;
230: }
231: my %reciphash = (
232: cc => $msgcc,
233: );
234: my %sentmessage;
235: my $stamp = time;
236: my $msgcount = &Apache::lonmsg::get_uniq();
237: &Apache::lonmsg::process_sent_mail($subject,'',$numsent,$stamp,$env{'user.name'},$env{'user.domain'},$msgcount,$crs,$$,$message,\@recusers,\@recudoms);
238: my ($recipid,$recipstatus) =
239: &Apache::lonmsg::store_recipients($subject,
240: $env{'user.name'},$env{'user.domain'},\%reciphash);
241: foreach my $recip (sort(keys(%{$msgcc}))) {
242: my ($ccname,$ccdom) = split(/:/,$recip);
243: my $status =
244: &Apache::lonmsg::user_normal_msg($ccname,$ccdom,$subject,$message,undef,undef,undef,undef,\%sentmessage,undef,undef,undef,1,$recipid);
1.2 raeburn 245: }
1.30 raeburn 246: delete($env{'form.can_reply'});
1.2 raeburn 247: }
1.27 raeburn 248: delete($env{'user.name'});
249: delete($env{'user.home'});
250: delete($env{'request.course.id'});
251: $env{'user.domain'} = $dom;
1.2 raeburn 252: }
253: }
254: }
1.11 raeburn 255: } else {
256: if ( ($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) ) {
257: if ( ($enrollvar{$crs}{autostart} < $timenow) && ( ($enrollvar{$crs}{autoend} > $timenow) || ($enrollvar{$crs}{autoend} == 0) ) ) {
1.25 raeburn 258: print $fh &mt('No institutional classlist data could be retrieved for [_1]',$crs)."\n";
1.11 raeburn 259: } else {
1.29 raeburn 260: print $fh &mt('Not within time window for auto-enrollment in [_1]',$crs)."\n";
1.11 raeburn 261: }
1.8 raeburn 262: } else {
1.25 raeburn 263: print $fh &mt('Auto-enrollment not currently enabled for [_1]',$crs)."\n";
1.8 raeburn 264: }
265: }
1.2 raeburn 266: }
1.25 raeburn 267: print $fh "-- ".localtime(time).' '.&mt('Enrollment messages end')."\n*******************\n\n";
1.11 raeburn 268: close($fh);
1.13 albertel 269: delete($env{'user.domain'});
1.2 raeburn 270: }
1.1 raeburn 271:
272: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>