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