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