1: package LONCAPA::Enrollment;
2:
3: use Apache::loncoursedata;
4: use Apache::lonnet;
5: use HTML::Entities;
6: use XML::Simple;
7: use LONCAPA::Configuration;
8:
9: use strict;
10:
11: sub update_LC {
12: my ($dom,$crs,$adds,$drops,$startdate,$enddate,$authtype,$autharg,$classesref,$groupref,$logmsg,$context) = @_;
13: # Get current LON-CAPA student enrollment for this class
14: my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
15: my $cid = $dom."_".$crs;
16: my $roster = &Apache::loncoursedata::get_classlist($cid,$dom,$crs);
17: my $cend = &Apache::loncoursedata::CL_END;
18: my $cstart = &Apache::loncoursedata::CL_START;
19: my $stuid=&Apache::loncoursedata::CL_ID;
20: my $sec=&Apache::loncoursedata::CL_SECTION;
21: my $status=&Apache::loncoursedata::CL_STATUS;
22: my $type=&Apache::loncoursedata::CL_TYPE;
23: my @localstudents = ();
24: my $currlist;
25: foreach my $uname (keys %{$roster} ) {
26: if ($uname =~ m/^(.+):$dom$/) {
27: if ($$roster{$uname}[$status] eq "Active") {
28: push @localstudents, $1;
29: @{$$currlist{$1}} = @{$$roster{$uname}};
30: }
31: }
32: }
33: my $linefeed = '';
34: my $addresult = '';
35: my $dropresult = '';
36: if ($context eq "updatenow") {
37: $linefeed = "</li>\n<li>";
38: } elsif ($context eq "automated") {
39: $linefeed = "\n";
40: }
41: my $enrollcount = 0;
42: my $dropcount = 0;
43:
44: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class
45: my @LCids = ();
46: my %unameFromLCid = ();
47: foreach my $uname (sort keys %{$currlist}) {
48: my $stuID = $$currlist{$uname}[$stuid];
49: if (!grep/^$stuID$/,@LCids) {
50: push @LCids, $stuID;
51: @{$unameFromLCid{$stuID}} = ();
52: }
53: push @{$unameFromLCid{$stuID}},$uname;
54: }
55:
56: # Get latest institutional enrollment for this class.
57: my %allenrolled = ();
58: my @reg_students = ();
59: my %place = ();
60: $place{'autharg'} = &CL_autharg();
61: $place{'authtype'} = &CL_authtype();
62: $place{'email'} = &CL_email();
63: $place{'enddate'} = &CL_enddate();
64: $place{'firstname'} = &CL_firstname();
65: $place{'generation'} = &CL_generation();
66: $place{'groupID'} = &CL_groupID();
67: $place{'lastname'} = &CL_lastname();
68: $place{'middlename'} = &CL_middlename();
69: $place{'startdate'} = &CL_startdate();
70: $place{'studentID'} = &CL_studentID();
71: my %ucount = ();
72: my %enrollinfo = ();
73: foreach my $class (@{$classesref}) {
74: my %enrolled = ();
75: &parse_classlist($$configvars{'lonDaemons'},$dom,$crs,$class,\%place,$$groupref{$class},\%enrolled);
76: foreach my $uname (sort keys %enrolled ) {
77: if (!grep/^$uname$/,@reg_students) {
78: push @reg_students,$uname;
79: $ucount{$uname} = 0;
80: @{$allenrolled{$uname}} = ();
81: }
82: @{$allenrolled{$uname}[$ucount{$uname}]} = @{$enrolled{$uname}};
83: $ucount{$uname} ++;
84: }
85: }
86:
87: # Check for multiple sections for a single student
88: my @okusers = ();
89: foreach my $uname (@reg_students) {
90: if (@{$allenrolled{$uname}} > 1) {
91: my @sections = ();
92: my $saved;
93: for (my $i=0; $i<@{$allenrolled{$uname}}; $i++) {
94: my @stuinfo = @{$allenrolled{$uname}[$i]};
95: my $secnum = $stuinfo[ $place{'groupID'} ];
96: unless ($secnum eq '') {
97: unless (grep/^$secnum$/,@sections) {
98: $saved = $i;
99: push @sections,$secnum;
100: }
101: }
102: }
103: if (@sections == 0) {
104: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
105: push @okusers, $uname;
106: }
107: elsif (@sections == 1) {
108: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[$saved]};
109: push @okusers, $uname;
110: }
111: elsif (@sections > 1) {
112: $logmsg = "$uname appears in classlists for multiple sections of $crs -";
113: foreach (@sections) {
114: $logmsg .= " $_,";
115: }
116: chop($logmsg);
117: $logmsg .= " No automated enrollment action taken for this student.\n";
118: }
119: } else {
120: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
121: push @okusers, $uname;
122: }
123: }
124: # Get mapping of student IDs to usernames for users in institutional data for this class
125: my @allINids = ();
126: my %unameFromINid = ();
127: foreach my $uname (@okusers) {
128: $enrollinfo{$uname}[ $place{'studentID'} ] =~ tr/A-Z/a-z/;
129: my $stuID = $enrollinfo{$uname}[ $place{'studentID'} ];
130: if (grep/^$stuID$/,@allINids) {
131: push @{$unameFromINid{$stuID}},$uname;
132: } else {
133: push @allINids, $stuID;
134: @{$unameFromINid{$stuID}} = $uname;
135: }
136: }
137: # Compare IDs with existing LON-CAPA enrollment for this class
138: foreach my $uname (@okusers) {
139: my %uidhash=&Apache::lonnet::idrget($dom,$uname);
140: my @stuinfo = @{$enrollinfo{$uname}};
141: if (grep/^$uname$/,@localstudents) {
142: # Check for studentID changes
143: if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) ) {
144: unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
145: $logmsg .= "Change in ID for $uname in class: $crs. StudentID in LON-CAPA system is $uidhash{$uname}, StudentID in institutional data is $stuinfo[ $place{studentID} ]\n";
146: }
147: }
148:
149: # Check for section changes
150: unless ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
151: $logmsg .= "Found a section difference for $uname - ".$$currlist{$uname}[$sec] ."versus ".$stuinfo[ $place{groupID} ]." in class $crs\n";
152: if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
153: my $modify_section_result = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto',$cid);
154: if ($modify_section_result !~ /^ok/) {
155: $logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $modify_section_result\n";
156: }
157:
158: # Assign the role of student in the new section
159: my $uurl='/'.$cid;
160: $uurl=~s/\_/\//g;
161: if ($stuinfo[ $place{groupID} ]) {
162: $uurl.='/'.$stuinfo[ $place{groupID} ];
163: }
164: my $newend = $stuinfo[ $place{enddate} ];
165: my $newstart = $stuinfo[ $place{startdate} ];
166: if ($newend eq '') {
167: $newend = $enddate;
168: }
169: if ($newstart eq '') {
170: $newstart = $startdate;
171: }
172: #explicitly allow acces to creating students
173: $ENV{'allowed.cst'}='F';
174: &Apache::lonnet::assignrole($dom,$uname,$uurl,"st",$newend,$newstart);
175: delete($ENV{'allowed.cst'});
176: }
177: }
178: }
179: elsif ($uname ne '') {
180: # Check for changed usernames by checking studentIDs
181: if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
182: if (grep/^$$currlist{$uname}[ $place{'studentID'} ]$/,@allINids) {
183: foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } } ) {
184: if (grep/^$match$/,@okusers) {
185: $logmsg .= "A possible change in username has been detected for a student enrolled in $crs. The existing LON-CAPA classlist contains user: $uname and student ID: ".$$currlist{$uname}[ $place{studentID} ].". This username has been dropped from the institutional classlist, but the same student ID is used for user: $match who still appears in the institutional classlist. You may need to move the student data files for user: $uname to $match\n";
186: }
187: }
188: }
189: } elsif ($adds == 1) {
190: # Add student to LON-CAPA classlist
191: my $auth = $stuinfo[ $place{'authtype'} ];
192: my $authparam = $stuinfo[ $place{'autharg'} ];
193: my $first = $stuinfo[ $place{'firstname'} ];
194: my $middle = $stuinfo[ $place{'middlename'} ];
195: my $last = $stuinfo[ $place{'lastname'} ];
196: my $gene = $stuinfo[ $place{'generation'} ];
197: my $usec = $stuinfo[ $place{'groupID'} ];
198: my $end = $stuinfo[ $place{'enddate'} ];
199: my $start = $stuinfo[ $place{'startdate'} ];
200: my $emailaddr = $stuinfo[ $place{'email'} ];
201: my $pid = $stuinfo[ $place{'studentID'} ];
202:
203: # remove non alphanumeric values from section
204: $usec =~ s/\W//g;
205:
206: unless ($emailaddr =~/^[^\@]+\@[^\@]+$/) { $emailaddr =''; }
207: my $emailenc = &HTML::Entities::encode($emailaddr);
208:
209: # Use course defaults where entry is absent
210: if ($auth eq '') {
211: $auth = $authtype;
212: }
213: if ($authparam eq '') {
214: $authparam = $autharg;
215: }
216: if ($auth =~ m/^krb/) {
217: $auth .= ":".$authparam;
218: }
219: if ($end eq '') {
220: $end = $enddate;
221: }
222: if ($start eq '') {
223: $start = $startdate;
224: }
225: # Clean up whitespace
226: foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$usec) {
227: $$_ =~ s/(\s+$|^\s+)//g;
228: }
229:
230: # Check for existing account in this LON-CAPA domain for this username
231: my $uhome=&Apache::lonnet::homeserver($uname,$dom);
232: if ($uhome eq 'no_host') { # User does not exist
233: my $create_passwd = 0;
234: my $authchk = '';
235: unless ($authparam eq '') { $authchk = 'ok'; };
236: # If no account exists and passwords should be generated
237: if ($authtype eq "int") {
238: if ($authparam eq '') {
239: ($authparam,$create_passwd,$authchk) = &create_password();
240: }
241: } elsif ($authtype eq "local") {
242: if ($authparam eq '') {
243: ($authparam,$create_passwd,$authchk) = &create_password();
244: }
245: } elsif ($authtype =~ m/^krb/) {
246: if ($authparam eq '') {
247: $logmsg .= "No Kerberos domain available for the new user - $uname in course $crs - no enrollment occurred.\n";
248: $authchk = 'invalid';
249: }
250: } else {
251: $authchk = 'invalid';
252: $logmsg .= "Invalid authentication type for new user - $uname in course $crs - no enrollment occurred.\n";
253: }
254: unless ($authchk eq 'ok') {
255: # Now create user.
256: my $reply=&Apache::lonnet::modifystudent($dom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto',$cid);
257: if ($reply eq 'ok') {
258: $enrollcount ++;
259: $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
260: $logmsg .= "New user $uname added successfully. ";
261: unless ($emailenc eq '') {
262: my %emailHash;
263: $emailHash{'critnotification'} = $emailenc;
264: $emailHash{'notification'} = $emailenc;
265: my $putresult = &Apache::lonnet::put('environment',\%emailHash,$dom,$uname);
266: }
267: if ($create_passwd) {
268: # Send e-mail with inital password to new user at $emailaddr
269: $logmsg .= "Initial password - - sent to $emailaddr\n";
270: } else {
271: $logmsg .= "\n";
272: }
273: } else {
274: $logmsg .= "An error occurred adding new user $uname - $reply\n";
275: }
276: }
277: } else {
278: # Get the user's information and authentication
279: my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification'],$dom,$uname);
280: my ($tmp) = keys(%userenv);
281: if ($tmp =~ /^(con_lost|error)/i) {
282: %userenv = ();
283: }
284: # Get the user's e-mail address
285: if ($userenv{critnotification} =~ m/%40/) {
286: unless ($emailenc eq $userenv{critnotification}) {
287: $logmsg .= "Current critical notification e-mail - ".$userenv{critnotification}." for $uname is different to e-mail address in Institutional classlist - $emailenc\n";
288: }
289: }
290: if ($userenv{notification} =~ m/%40/) {
291: unless ($emailenc eq $userenv{critnotification}) {
292: $logmsg .= "Current standard notification e-mail - ".$userenv{notification}." for $uname is different to e-mail address in Institutional classlist - $emailenc\n";
293: }
294: }
295: my $krbdefdom = '';
296: my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
297: if ($currentauth=~/^krb(4|5):/) {
298: $currentauth=~/^krb(4|5):(.*)/;
299: $krbdefdom=$1;
300: }
301: if ($currentauth=~/^krb(4|5):/ ||
302: $currentauth=~/^unix:/ ||
303: $currentauth=~/^internal:/ ||
304: $currentauth=~/^localauth:/) {
305:
306: } else {
307: $logmsg .= "Invalid authentication method $currentauth for $uname.\n";
308: }
309: # Report if authentication methods are different.
310: if ($currentauth ne $auth ) {
311: $logmsg .= "Authentication mismatch for $uname - $currentauth in system, $auth for class $crs\n";
312: }
313: # Check user data
314: if ($first ne $userenv{'firstname'} ||
315: $middle ne $userenv{'middlename'} ||
316: $last ne $userenv{'lastname'} ||
317: $gene ne $userenv{'generation'} ||
318: $pid ne $userenv{'id'} ) {
319: # Make the change(s)
320: my %changeHash;
321: $changeHash{'firstname'} = $first;
322: $changeHash{'middlename'} = $middle;
323: $changeHash{'lastname'} = $last;
324: $changeHash{'generation'} = $gene;
325: $changeHash{'id'} = $pid;
326: my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
327: if ($putresult eq 'ok') {
328: $logmsg .= "User information updated for user: $uname prior to enrollment in $crs\n";
329: } else {
330: $logmsg .= "There was a problem modifying user data for existing user - $uname, enrollment will still be attempted for user in $crs.\n";
331: }
332: }
333:
334: # Assign the role of student in the course.
335: my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto',$cid);
336: if ($classlist_reply eq 'ok') {
337: $enrollcount ++;
338: $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
339: $logmsg .= "Existing user $uname enrolled successfully in $crs\n";
340:
341: } else {
342: $logmsg .= "There was a problem updating the classlist db file for user $uname to show the new enrollment, so no enrollment occurred for this user in $crs\n";
343: }
344: }
345: }
346: }
347: }
348: # Do drops
349: if ( ($drops == 1) && (@reg_students > 0) ) {
350: foreach my $uname (@localstudents) {
351: if ($$currlist{$uname}[$type] eq "auto") {
352: my @saved = ();
353: if (!grep/^$uname$/,@reg_students) {
354: # Check for changed usernames by checking studentIDs
355: if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
356: foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
357: $logmsg .= "A possible change in username has been detected for a student enrolled in $crs. The existing LON-CAPA classlist contains user: $uname and student ID: $$currlist{$uname}[ $place{studentID} ]. This username has been dropped from the institutional classlist, but the same student ID is used for user: $match who still appears in the institutional classlist. You may need to move the student data files for user: $uname to $match\n";
358: push @saved,$uname;
359: }
360: } elsif (@saved == 0) {
361: my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,undef,$cid);
362: if ($drop_reply !~ /^ok/) {
363: $logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $drop_reply\n";
364: } else {
365: $dropcount ++;
366: my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
367: $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname." dropped from section/group ".$$currlist{$uname}[$sec].$linefeed;
368: }
369: }
370: }
371: }
372: }
373: }
374: if ($enrollcount > 0) {
375: if ($context eq "updatenow") {
376: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:<br/><ul><li>".$addresult."</li></ul><br/><br/>";
377: } else {
378: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:\n\n".$addresult."\n\n";
379: }
380: }
381: if ($dropcount > 0) {
382: if ($context eq "updatenow") {
383: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:<br/><ul><li>".$dropresult."</li></ul><br/><br/>";
384: } else {
385: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:\n\n".$dropresult."\n\n";
386: }
387: }
388: if ( ($adds) && ($enrollcount == 0) ) {
389: $addresult = "There were no new students to add to the course.";
390: if ($context eq "updatenow") {
391: $addresult .="<br/><br/>";
392: } else {
393: $addresult .="\n";
394: }
395: }
396: if ( ($drops) && ($dropcount == 0) ) {
397: $dropresult = "There were no students with roles to expire because all active students previously added to the course from institutional classlist(s) are still officially registered.";
398: if ($context eq "updatenow") {
399: $dropresult .="<br/>";
400: } else {
401: $dropresult .="\n";
402: }
403: }
404: print STDERR $logmsg;
405: return $addresult.$dropresult;
406: }
407:
408: sub parse_classlist {
409: my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
410: my $configvars = &LONCAPA::Configuration::read_conf();
411: my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_classlist.xml";
412: my $enrolled = XMLin( $xmlfile, KeyAttr => ['username'] );
413: foreach my $uname ( sort keys %{$$enrolled{'student'}} ) {
414: @{ $$studentsref{$uname} } = ();
415: foreach my $key (sort keys %{$$enrolled{'student'}{$uname}} ) {
416: my $value = $$enrolled{'student'}{$uname}{$key};
417: if (ref($value)) {
418: $$studentsref{$uname}[ $$placeref{$key} ] = '';
419: } else {
420: if ($key eq 'groupID') {
421: $$studentsref{$uname}[ $$placeref{$key} ] = $groupID;
422: } else {
423: $$studentsref{$uname}[ $$placeref{$key} ] = $value;
424: }
425: }
426: }
427: }
428: # if (-e "$xmlfile") {
429: # unlink $xmlfile;
430: # }
431: return;
432: }
433:
434: sub create_password {
435: my ($authparam,$create_passwd,$authreply);
436: return ($authparam,$create_passwd,$authreply);
437: }
438:
439: sub CL_autharg { return 0; }
440: sub CL_authtype { return 1;}
441: sub CL_email { return 2;}
442: sub CL_enddate { return 3;}
443: sub CL_firstname { return 4;}
444: sub CL_generation { return 5;}
445: sub CL_groupID { return 6;}
446: sub CL_lastname { return 7;}
447: sub CL_middlename { return 8;}
448: sub CL_startdate { return 9; }
449: sub CL_studentID { return 10; }
450:
451: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>