Annotation of loncom/enrollment/Enrollment.pm, revision 1.5
1.1 raeburn 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) {
1.5 ! raeburn 112: $$logmsg = "$uname appears in classlists for the more than one section of this course, i.e. in sections: ";
1.1 raeburn 113: foreach (@sections) {
1.5 ! raeburn 114: $$logmsg .= " $_,";
1.1 raeburn 115: }
1.5 ! raeburn 116: chop($$logmsg);
! 117: $$logmsg .= " Because of this ambiguity, no enrollment action was taken for this student.".$linefeed;
1.1 raeburn 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 = ();
1.3 raeburn 126: my %unameFromINid = ();
1.1 raeburn 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: }
1.5 ! raeburn 137: # Explicitly allow access to creation/modification of students if called as an automated process.
! 138: if ($context eq 'automated') {
! 139: $ENV{'allowed.cst'}='F';
! 140: }
! 141:
1.1 raeburn 142: # Compare IDs with existing LON-CAPA enrollment for this class
143: foreach my $uname (@okusers) {
1.5 ! raeburn 144: unless ($uname eq '') {
! 145: my %uidhash=&Apache::lonnet::idrget($dom,$uname);
! 146: my @stuinfo = @{$enrollinfo{$uname}};
! 147: if (grep/^$uname$/,@localstudents) {
1.1 raeburn 148: # Check for studentID changes
1.5 ! raeburn 149: if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) ) {
! 150: unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
! 151: $$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} ]".$linefeed;
! 152: }
1.1 raeburn 153: }
154:
155: # Check for section changes
1.5 ! raeburn 156: unless ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
! 157: if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
! 158: 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);
! 159: if ($modify_section_result =~ /^ok/) {
! 160: $$logmsg .= "Section for $uname switched from old section: ".$$currlist{$uname}[$sec] ."to new section: ".$stuinfo[ $place{groupID} ]." in class $crs".$linefeed;
! 161: } else {
! 162: $$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;
! 163: }
1.1 raeburn 164: }
165: }
1.5 ! raeburn 166: } else {
1.1 raeburn 167: # Check for changed usernames by checking studentIDs
1.5 ! raeburn 168: if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
! 169: if (grep/^$$currlist{$uname}[ $place{'studentID'} ]$/,@allINids) {
! 170: foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } } ) {
! 171: if (grep/^$match$/,@okusers) {
! 172: $$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".$linefeed;
! 173: }
1.1 raeburn 174: }
175: }
1.5 ! raeburn 176: } elsif ($adds == 1) {
1.1 raeburn 177: # Add student to LON-CAPA classlist
1.5 ! raeburn 178: my $auth = $stuinfo[ $place{'authtype'} ];
! 179: my $authparam = $stuinfo[ $place{'autharg'} ];
! 180: my $first = $stuinfo[ $place{'firstname'} ];
! 181: my $middle = $stuinfo[ $place{'middlename'} ];
! 182: my $last = $stuinfo[ $place{'lastname'} ];
! 183: my $gene = $stuinfo[ $place{'generation'} ];
! 184: my $usec = $stuinfo[ $place{'groupID'} ];
! 185: my $end = $stuinfo[ $place{'enddate'} ];
! 186: my $start = $stuinfo[ $place{'startdate'} ];
! 187: my $emailaddr = $stuinfo[ $place{'email'} ];
! 188: my $pid = $stuinfo[ $place{'studentID'} ];
1.1 raeburn 189:
190: # remove non alphanumeric values from section
1.5 ! raeburn 191: $usec =~ s/\W//g;
1.1 raeburn 192:
1.5 ! raeburn 193: unless ($emailaddr =~/^[^\@]+\@[^\@]+$/) { $emailaddr =''; }
! 194: my $emailenc = &HTML::Entities::encode($emailaddr);
1.1 raeburn 195:
196: # Use course defaults where entry is absent
1.5 ! raeburn 197: if ( ($auth eq '') || (!defined($auth)) ) {
! 198: $auth = $authtype;
! 199: }
! 200: if ( ($authparam eq '') || (!defined($authparam)) ) {
! 201: $authparam = $autharg;
! 202: }
! 203: if ($auth =~ m/^krb/) {
! 204: $auth .= ":".$authparam;
! 205: }
! 206: if ( ($end eq '') || (!defined($end)) ) {
! 207: $end = $enddate;
! 208: }
! 209: if ( ($start eq '') || (!defined($start)) ) {
! 210: $start = $startdate;
! 211: }
1.1 raeburn 212: # Clean up whitespace
1.5 ! raeburn 213: foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$usec) {
! 214: $$_ =~ s/(\s+$|^\s+)//g;
! 215: }
1.1 raeburn 216:
217: # Check for existing account in this LON-CAPA domain for this username
1.5 ! raeburn 218: my $uhome=&Apache::lonnet::homeserver($uname,$dom);
! 219: if ($uhome eq 'no_host') { # User does not exist
! 220: my $create_passwd = 0;
! 221: my $authchk = '';
! 222: unless ($authparam eq '') { $authchk = 'ok'; };
1.1 raeburn 223: # If no account exists and passwords should be generated
1.5 ! raeburn 224: if ($authtype eq "int") {
! 225: if ($authparam eq '') {
! 226: ($authparam,$create_passwd,$authchk) = &create_password();
! 227: }
! 228: } elsif ($authtype eq "local") {
! 229: if ($authparam eq '') {
! 230: ($authparam,$create_passwd,$authchk) = &create_password();
! 231: }
! 232: } elsif ($authtype =~ m/^krb/) {
! 233: if ($authparam eq '') {
! 234: $$logmsg .= "No Kerberos domain available for the new user - $uname in course $crs - no enrollment occurred.".$linefeed;
! 235: $authchk = 'invalid';
! 236: }
! 237: } else {
1.3 raeburn 238: $authchk = 'invalid';
1.5 ! raeburn 239: $$logmsg .= "Invalid authentication type for new user - $uname in course $crs - no enrollment occurred.".$linefeed;
1.3 raeburn 240: }
1.5 ! raeburn 241: if ($authchk eq 'ok') {
1.1 raeburn 242: # Now create user.
1.5 ! raeburn 243: my $reply=&Apache::lonnet::modifystudent($dom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto',$cid);
! 244: if ($reply eq 'ok') {
! 245: $enrollcount ++;
! 246: $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
! 247: $$logmsg .= "New user $uname added successfully. ";
! 248: unless ($emailenc eq '') {
! 249: my %emailHash;
! 250: $emailHash{'critnotification'} = $emailenc;
! 251: $emailHash{'notification'} = $emailenc;
! 252: my $putresult = &Apache::lonnet::put('environment',\%emailHash,$dom,$uname);
! 253: }
! 254: if ($create_passwd) {
1.1 raeburn 255: # Send e-mail with inital password to new user at $emailaddr
1.5 ! raeburn 256: $$logmsg .= "Initial password - - sent to ".$emailaddr.$linefeed;
! 257: } else {
! 258: $$logmsg .= $linefeed;
! 259: }
1.3 raeburn 260: } else {
1.5 ! raeburn 261: $$logmsg .= "An error occurred adding new user $uname - ".$reply.$linefeed;
1.3 raeburn 262: }
1.1 raeburn 263: }
1.5 ! raeburn 264: } else {
1.1 raeburn 265: # Get the user's information and authentication
1.5 ! raeburn 266: my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification'],$dom,$uname);
! 267: my ($tmp) = keys(%userenv);
! 268: if ($tmp =~ /^(con_lost|error)/i) {
! 269: %userenv = ();
! 270: }
1.1 raeburn 271: # Get the user's e-mail address
1.5 ! raeburn 272: if ($userenv{critnotification} =~ m/%40/) {
! 273: unless ($emailenc eq $userenv{critnotification}) {
! 274: $$logmsg .= "Current critical notification e-mail - ".$userenv{critnotification}." for $uname is different to e-mail address in Institutional classlist - ".$emailenc.$linefeed;
! 275: }
! 276: }
! 277: if ($userenv{notification} =~ m/%40/) {
! 278: unless ($emailenc eq $userenv{critnotification}) {
! 279: $$logmsg .= "Current standard notification e-mail - ".$userenv{notification}." for $uname is different to e-mail address in institutional classlist - ".$emailenc.$linefeed;
! 280: }
! 281: }
! 282: my $krbdefdom = '';
! 283: my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
! 284: if ($currentauth=~/^krb(4|5):/) {
! 285: $currentauth=~/^krb(4|5):(.*)/;
! 286: $krbdefdom=$1;
! 287: }
! 288: if ($currentauth=~/^krb(4|5):/ ||
! 289: $currentauth=~/^unix:/ ||
! 290: $currentauth=~/^internal:/ ||
! 291: $currentauth=~/^localauth:/) {
1.1 raeburn 292:
1.5 ! raeburn 293: } else {
! 294: $$logmsg .= "Invalid authentication method $currentauth for $uname.".$linefeed;
! 295: }
1.1 raeburn 296: # Report if authentication methods are different.
1.5 ! raeburn 297: if ($currentauth ne $auth ) {
! 298: $$logmsg .= "Authentication mismatch for $uname - $currentauth in system, $auth for class $crs".$linefeed;
! 299: }
1.1 raeburn 300: # Check user data
1.5 ! raeburn 301: if ($first ne $userenv{'firstname'} ||
! 302: $middle ne $userenv{'middlename'} ||
! 303: $last ne $userenv{'lastname'} ||
! 304: $gene ne $userenv{'generation'} ||
! 305: $pid ne $userenv{'id'} ) {
1.1 raeburn 306: # Make the change(s)
1.5 ! raeburn 307: my %changeHash;
! 308: $changeHash{'firstname'} = $first;
! 309: $changeHash{'middlename'} = $middle;
! 310: $changeHash{'lastname'} = $last;
! 311: $changeHash{'generation'} = $gene;
! 312: $changeHash{'id'} = $pid;
! 313: my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
! 314: if ($putresult eq 'ok') {
! 315: $$logmsg .= "User information updated for user: $uname prior to enrollment.".$linefeed;
! 316: } else {
! 317: $$logmsg .= "There was a problem modifying user data for existing user - $uname -error: $putresult, enrollment will still be attempted.".$linefeed;
! 318: }
1.1 raeburn 319: }
1.3 raeburn 320:
321: # Assign the role of student in the course.
1.5 ! raeburn 322: my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto',$cid);
! 323: if ($classlist_reply eq 'ok') {
! 324: $enrollcount ++;
! 325: $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
! 326: $$logmsg .= "Existing user $uname enrolled successfully.".$linefeed;
1.3 raeburn 327:
1.5 ! raeburn 328: } else {
! 329: $$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;
! 330: }
1.3 raeburn 331: }
1.1 raeburn 332: }
333: }
334: }
335: }
336: # Do drops
337: if ( ($drops == 1) && (@reg_students > 0) ) {
338: foreach my $uname (@localstudents) {
339: if ($$currlist{$uname}[$type] eq "auto") {
340: my @saved = ();
341: if (!grep/^$uname$/,@reg_students) {
342: # Check for changed usernames by checking studentIDs
343: if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
344: foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
1.5 ! raeburn 345: $$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.".$linefeed;
1.1 raeburn 346: push @saved,$uname;
347: }
348: } elsif (@saved == 0) {
349: my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,undef,$cid);
350: if ($drop_reply !~ /^ok/) {
1.5 ! raeburn 351: $$logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $drop_reply.".$linefeed;
1.1 raeburn 352: } else {
353: $dropcount ++;
354: my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
355: $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname." dropped from section/group ".$$currlist{$uname}[$sec].$linefeed;
356: }
357: }
358: }
359: }
360: }
361: }
1.5 ! raeburn 362:
! 363: # Terminated explictly allowed access to student creation/modification
! 364: if ($context eq 'automated') {
! 365: delete($ENV{'allowed.cst'});
! 366: }
1.1 raeburn 367: if ($enrollcount > 0) {
368: if ($context eq "updatenow") {
369: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:<br/><ul><li>".$addresult."</li></ul><br/><br/>";
370: } else {
371: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:\n\n".$addresult."\n\n";
372: }
373: }
374: if ($dropcount > 0) {
375: if ($context eq "updatenow") {
376: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:<br/><ul><li>".$dropresult."</li></ul><br/><br/>";
377: } else {
378: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:\n\n".$dropresult."\n\n";
379: }
380: }
381: if ( ($adds) && ($enrollcount == 0) ) {
382: $addresult = "There were no new students to add to the course.";
383: if ($context eq "updatenow") {
384: $addresult .="<br/><br/>";
385: } else {
386: $addresult .="\n";
387: }
388: }
389: if ( ($drops) && ($dropcount == 0) ) {
390: $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.";
391: if ($context eq "updatenow") {
392: $dropresult .="<br/>";
393: } else {
394: $dropresult .="\n";
395: }
396: }
1.5 ! raeburn 397: my $changecount = $enrollcount + $dropcount;
! 398: return ($changecount,$addresult.$dropresult);
1.1 raeburn 399: }
400:
401: sub parse_classlist {
1.3 raeburn 402: my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
403: my $configvars = &LONCAPA::Configuration::read_conf();
1.5 ! raeburn 404: my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
1.3 raeburn 405: my $enrolled = XMLin( $xmlfile, KeyAttr => ['username'] );
406: foreach my $uname ( sort keys %{$$enrolled{'student'}} ) {
407: @{ $$studentsref{$uname} } = ();
408: foreach my $key (sort keys %{$$enrolled{'student'}{$uname}} ) {
409: my $value = $$enrolled{'student'}{$uname}{$key};
410: if (ref($value)) {
411: $$studentsref{$uname}[ $$placeref{$key} ] = '';
412: } else {
413: if ($key eq 'groupID') {
414: $$studentsref{$uname}[ $$placeref{$key} ] = $groupID;
415: } else {
416: $$studentsref{$uname}[ $$placeref{$key} ] = $value;
417: }
418: }
419: }
420: }
421: # if (-e "$xmlfile") {
422: # unlink $xmlfile;
423: # }
424: return;
1.1 raeburn 425: }
426:
427: sub create_password {
1.3 raeburn 428: my ($authparam,$create_passwd,$authreply);
429: return ($authparam,$create_passwd,$authreply);
1.1 raeburn 430: }
431:
432: sub CL_autharg { return 0; }
433: sub CL_authtype { return 1;}
434: sub CL_email { return 2;}
435: sub CL_enddate { return 3;}
436: sub CL_firstname { return 4;}
437: sub CL_generation { return 5;}
438: sub CL_groupID { return 6;}
439: sub CL_lastname { return 7;}
440: sub CL_middlename { return 8;}
441: sub CL_startdate { return 9; }
442: sub CL_studentID { return 10; }
443:
444: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>