Annotation of loncom/enrollment/Enrollment.pm, revision 1.11
1.7 albertel 1: # Automated Enrollment manager
1.11 ! raeburn 2: # $Id: Enrollment.pm,v 1.10 2004/01/14 21:03:31 raeburn Exp $
1.7 albertel 3: #
4: # Copyright Michigan State University Board of Trustees
5: #
6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
7: #
8: # LON-CAPA is free software; you can redistribute it and/or modify
9: # it under the terms of the GNU General Public License as published by
10: # the Free Software Foundation; either version 2 of the License, or
11: # (at your option) any later version.
12: #
13: # LON-CAPA is distributed in the hope that it will be useful,
14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: # GNU General Public License for more details.
17: #
18: # You should have received a copy of the GNU General Public License
19: # along with LON-CAPA; if not, write to the Free Software
20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: #
22: # /home/httpd/html/adm/gpl.txt
23: #
24: # http://www.lon-capa.org/
25: #
1.1 raeburn 26: package LONCAPA::Enrollment;
27:
28: use Apache::loncoursedata;
29: use Apache::lonnet;
1.8 raeburn 30: use Apache::lonmsg;
1.1 raeburn 31: use HTML::Entities;
32: use LONCAPA::Configuration;
1.8 raeburn 33: use Time::Local;
34: use lib '/home/httpd/lib/perl';
35: use localenroll;
1.1 raeburn 36:
37: use strict;
38:
39: sub update_LC {
1.8 raeburn 40: my ($dom,$crs,$adds,$drops,$startdate,$enddate,$authtype,$autharg,$classesref,$groupref,$logmsg,$newusermsg,$context) = @_;
1.1 raeburn 41: # Get current LON-CAPA student enrollment for this class
42: my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
43: my $cid = $dom."_".$crs;
44: my $roster = &Apache::loncoursedata::get_classlist($cid,$dom,$crs);
45: my $cend = &Apache::loncoursedata::CL_END;
46: my $cstart = &Apache::loncoursedata::CL_START;
47: my $stuid=&Apache::loncoursedata::CL_ID;
48: my $sec=&Apache::loncoursedata::CL_SECTION;
49: my $status=&Apache::loncoursedata::CL_STATUS;
50: my $type=&Apache::loncoursedata::CL_TYPE;
51: my @localstudents = ();
52: my $currlist;
53: foreach my $uname (keys %{$roster} ) {
54: if ($uname =~ m/^(.+):$dom$/) {
55: if ($$roster{$uname}[$status] eq "Active") {
56: push @localstudents, $1;
57: @{$$currlist{$1}} = @{$$roster{$uname}};
58: }
59: }
60: }
61: my $linefeed = '';
62: my $addresult = '';
63: my $dropresult = '';
64: if ($context eq "updatenow") {
65: $linefeed = "</li>\n<li>";
66: } elsif ($context eq "automated") {
67: $linefeed = "\n";
68: }
69: my $enrollcount = 0;
70: my $dropcount = 0;
71:
1.8 raeburn 72: srand( time() ^ ($$ + ($$ << 15)) ); # Seed rand in case initial passwords have to be generated for new users.
73:
1.1 raeburn 74: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class
75: my @LCids = ();
76: my %unameFromLCid = ();
77: foreach my $uname (sort keys %{$currlist}) {
78: my $stuID = $$currlist{$uname}[$stuid];
79: if (!grep/^$stuID$/,@LCids) {
80: push @LCids, $stuID;
81: @{$unameFromLCid{$stuID}} = ();
82: }
83: push @{$unameFromLCid{$stuID}},$uname;
84: }
85:
86: # Get latest institutional enrollment for this class.
87: my %allenrolled = ();
88: my @reg_students = ();
89: my %place = ();
90: $place{'autharg'} = &CL_autharg();
91: $place{'authtype'} = &CL_authtype();
92: $place{'email'} = &CL_email();
93: $place{'enddate'} = &CL_enddate();
94: $place{'firstname'} = &CL_firstname();
95: $place{'generation'} = &CL_generation();
96: $place{'groupID'} = &CL_groupID();
97: $place{'lastname'} = &CL_lastname();
98: $place{'middlename'} = &CL_middlename();
99: $place{'startdate'} = &CL_startdate();
100: $place{'studentID'} = &CL_studentID();
101: my %ucount = ();
102: my %enrollinfo = ();
103: foreach my $class (@{$classesref}) {
104: my %enrolled = ();
105: &parse_classlist($$configvars{'lonDaemons'},$dom,$crs,$class,\%place,$$groupref{$class},\%enrolled);
106: foreach my $uname (sort keys %enrolled ) {
107: if (!grep/^$uname$/,@reg_students) {
108: push @reg_students,$uname;
109: $ucount{$uname} = 0;
110: @{$allenrolled{$uname}} = ();
111: }
112: @{$allenrolled{$uname}[$ucount{$uname}]} = @{$enrolled{$uname}};
113: $ucount{$uname} ++;
114: }
115: }
116:
117: # Check for multiple sections for a single student
118: my @okusers = ();
119: foreach my $uname (@reg_students) {
120: if (@{$allenrolled{$uname}} > 1) {
121: my @sections = ();
122: my $saved;
123: for (my $i=0; $i<@{$allenrolled{$uname}}; $i++) {
124: my @stuinfo = @{$allenrolled{$uname}[$i]};
125: my $secnum = $stuinfo[ $place{'groupID'} ];
126: unless ($secnum eq '') {
127: unless (grep/^$secnum$/,@sections) {
128: $saved = $i;
129: push @sections,$secnum;
130: }
131: }
132: }
133: if (@sections == 0) {
134: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
135: push @okusers, $uname;
136: }
137: elsif (@sections == 1) {
138: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[$saved]};
139: push @okusers, $uname;
140: }
141: elsif (@sections > 1) {
1.5 raeburn 142: $$logmsg = "$uname appears in classlists for the more than one section of this course, i.e. in sections: ";
1.1 raeburn 143: foreach (@sections) {
1.5 raeburn 144: $$logmsg .= " $_,";
1.1 raeburn 145: }
1.5 raeburn 146: chop($$logmsg);
1.6 raeburn 147: $$logmsg .= ". Because of this ambiguity, no enrollment action was taken for this student.".$linefeed;
1.1 raeburn 148: }
149: } else {
150: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
151: push @okusers, $uname;
152: }
153: }
154: # Get mapping of student IDs to usernames for users in institutional data for this class
155: my @allINids = ();
1.3 raeburn 156: my %unameFromINid = ();
1.1 raeburn 157: foreach my $uname (@okusers) {
158: $enrollinfo{$uname}[ $place{'studentID'} ] =~ tr/A-Z/a-z/;
159: my $stuID = $enrollinfo{$uname}[ $place{'studentID'} ];
160: if (grep/^$stuID$/,@allINids) {
161: push @{$unameFromINid{$stuID}},$uname;
162: } else {
163: push @allINids, $stuID;
164: @{$unameFromINid{$stuID}} = $uname;
165: }
166: }
1.5 raeburn 167: # Explicitly allow access to creation/modification of students if called as an automated process.
168: if ($context eq 'automated') {
169: $ENV{'allowed.cst'}='F';
170: }
171:
1.1 raeburn 172: # Compare IDs with existing LON-CAPA enrollment for this class
173: foreach my $uname (@okusers) {
1.5 raeburn 174: unless ($uname eq '') {
175: my %uidhash=&Apache::lonnet::idrget($dom,$uname);
176: my @stuinfo = @{$enrollinfo{$uname}};
177: if (grep/^$uname$/,@localstudents) {
1.1 raeburn 178: # Check for studentID changes
1.5 raeburn 179: if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) ) {
180: unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
1.6 raeburn 181: $$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 182: }
1.1 raeburn 183: }
184:
185: # Check for section changes
1.5 raeburn 186: unless ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
187: if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
1.10 raeburn 188: # Delete from roles.db for current section
189: my $expiretime = time;
190: my $uurl='/'.$cid;
191: $uurl=~s/\_/\//g;
192: if ($$currlist{$uname}[$sec]) {
193: $uurl.='/'.$$currlist{$uname}[$sec];
194: }
195: my $expire_role_result = &Apache::lonnet::assignrole($dom,$uname,$uurl,'st',$expiretime);
196: if ($expire_role_result eq 'ok') {
197: 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);
198: if ($modify_section_result =~ /^ok/) {
199: $$logmsg .= "Section for $uname switched from old section: ".$$currlist{$uname}[$sec] ." to new section: ".$stuinfo[ $place{groupID} ].".".$linefeed;
200: } else {
201: $$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;
202: }
1.5 raeburn 203: } else {
1.10 raeburn 204: $$logmsg .= "Error when attempting to expire role for $uname in old section" .$$currlist{$uname}[$sec]." -error: $expire_role_result".$linefeed;
1.5 raeburn 205: }
1.1 raeburn 206: }
207: }
1.5 raeburn 208: } else {
1.1 raeburn 209: # Check for changed usernames by checking studentIDs
1.5 raeburn 210: if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
211: if (grep/^$$currlist{$uname}[ $place{'studentID'} ]$/,@allINids) {
212: foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } } ) {
213: if (grep/^$match$/,@okusers) {
1.6 raeburn 214: $$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 215: }
1.1 raeburn 216: }
217: }
1.5 raeburn 218: } elsif ($adds == 1) {
1.1 raeburn 219: # Add student to LON-CAPA classlist
1.5 raeburn 220: my $auth = $stuinfo[ $place{'authtype'} ];
221: my $authparam = $stuinfo[ $place{'autharg'} ];
222: my $first = $stuinfo[ $place{'firstname'} ];
223: my $middle = $stuinfo[ $place{'middlename'} ];
224: my $last = $stuinfo[ $place{'lastname'} ];
225: my $gene = $stuinfo[ $place{'generation'} ];
226: my $usec = $stuinfo[ $place{'groupID'} ];
227: my $end = $stuinfo[ $place{'enddate'} ];
228: my $start = $stuinfo[ $place{'startdate'} ];
229: my $emailaddr = $stuinfo[ $place{'email'} ];
230: my $pid = $stuinfo[ $place{'studentID'} ];
1.1 raeburn 231:
232: # remove non alphanumeric values from section
1.5 raeburn 233: $usec =~ s/\W//g;
1.1 raeburn 234:
1.5 raeburn 235: unless ($emailaddr =~/^[^\@]+\@[^\@]+$/) { $emailaddr =''; }
236: my $emailenc = &HTML::Entities::encode($emailaddr);
1.1 raeburn 237:
238: # Use course defaults where entry is absent
1.5 raeburn 239: if ( ($auth eq '') || (!defined($auth)) ) {
240: $auth = $authtype;
241: }
242: if ( ($authparam eq '') || (!defined($authparam)) ) {
243: $authparam = $autharg;
244: }
245: if ($auth =~ m/^krb/) {
1.11 ! raeburn 246: $auth .= ':'.$authparam;
1.5 raeburn 247: }
248: if ( ($end eq '') || (!defined($end)) ) {
249: $end = $enddate;
250: }
251: if ( ($start eq '') || (!defined($start)) ) {
252: $start = $startdate;
253: }
1.1 raeburn 254: # Clean up whitespace
1.5 raeburn 255: foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$usec) {
256: $$_ =~ s/(\s+$|^\s+)//g;
257: }
1.1 raeburn 258:
259: # Check for existing account in this LON-CAPA domain for this username
1.5 raeburn 260: my $uhome=&Apache::lonnet::homeserver($uname,$dom);
261: if ($uhome eq 'no_host') { # User does not exist
262: my $create_passwd = 0;
263: my $authchk = '';
264: unless ($authparam eq '') { $authchk = 'ok'; };
1.1 raeburn 265: # If no account exists and passwords should be generated
1.11 ! raeburn 266: if ($auth eq "internal") {
1.5 raeburn 267: if ($authparam eq '') {
1.8 raeburn 268: ($authparam) = &create_password();
269: if ($authparam eq '') {
1.11 ! raeburn 270: $authchk = '';
1.8 raeburn 271: } else {
272: $create_passwd = 1;
1.11 ! raeburn 273: $authchk = 'ok';
1.8 raeburn 274: }
1.5 raeburn 275: }
1.11 ! raeburn 276: } elsif ($auth eq "localauth") {
! 277: ($authparam,$create_passwd,$authchk) = &localenroll::create_password($authparam);
! 278: } elsif ($auth =~ m/^krb/) {
1.5 raeburn 279: if ($authparam eq '') {
1.6 raeburn 280: $$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 281: $authchk = 'invalid';
282: }
283: } else {
1.3 raeburn 284: $authchk = 'invalid';
1.6 raeburn 285: $$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 286: }
1.5 raeburn 287: if ($authchk eq 'ok') {
1.1 raeburn 288: # Now create user.
1.5 raeburn 289: my $reply=&Apache::lonnet::modifystudent($dom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto',$cid);
290: if ($reply eq 'ok') {
291: $enrollcount ++;
292: $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
1.6 raeburn 293: if ($context eq 'automated') {
1.8 raeburn 294: $$logmsg .= "New $dom user $uname added successfully.";
1.6 raeburn 295: }
1.5 raeburn 296: unless ($emailenc eq '') {
297: my %emailHash;
298: $emailHash{'critnotification'} = $emailenc;
299: $emailHash{'notification'} = $emailenc;
300: my $putresult = &Apache::lonnet::put('environment',\%emailHash,$dom,$uname);
301: }
302: if ($create_passwd) {
1.8 raeburn 303: # Send e-mail with initial password to new user at $emailaddr.
304: # If e-mail address is invalid, send password via message to courseowner i
305: # (if automated call) or to user if roster update.
306: if ($emailaddr eq '') {
307: $$newusermsg .= " username: $uname, password: ".$authparam.$linefeed."\n";
308: } else {
309: my $subject = "New LON-CAPA account";
310: my $body = "You have been enrolled in the LON-CAPA system at your school, because you are a registered student in a class that is using the LON-CAPA course management and online homework system.\n\nYou should log-in to the system using the following credentials:\nusername: $uname\npassword: $authparam\n\nThe URL you should use to access the LON-CAPA system at your school is: http://".$ENV{'SERVER_NAME'};
311: &Apache::lonmsg::sendemail($emailaddr,$subject,$body);
312: }
313: if ($context eq 'automated') {
314: $$logmsg .= " Initial password - - sent to ".$emailaddr.$linefeed;
315: }
1.5 raeburn 316: } else {
1.8 raeburn 317: if ($context eq 'automated') {
318: $$logmsg .= $linefeed;
319: }
1.5 raeburn 320: }
1.3 raeburn 321: } else {
1.5 raeburn 322: $$logmsg .= "An error occurred adding new user $uname - ".$reply.$linefeed;
1.3 raeburn 323: }
1.1 raeburn 324: }
1.5 raeburn 325: } else {
1.1 raeburn 326: # Get the user's information and authentication
1.5 raeburn 327: my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification'],$dom,$uname);
328: my ($tmp) = keys(%userenv);
329: if ($tmp =~ /^(con_lost|error)/i) {
330: %userenv = ();
331: }
1.1 raeburn 332: # Get the user's e-mail address
1.5 raeburn 333: if ($userenv{critnotification} =~ m/%40/) {
334: unless ($emailenc eq $userenv{critnotification}) {
335: $$logmsg .= "Current critical notification e-mail - ".$userenv{critnotification}." for $uname is different to e-mail address in Institutional classlist - ".$emailenc.$linefeed;
336: }
337: }
338: if ($userenv{notification} =~ m/%40/) {
339: unless ($emailenc eq $userenv{critnotification}) {
340: $$logmsg .= "Current standard notification e-mail - ".$userenv{notification}." for $uname is different to e-mail address in institutional classlist - ".$emailenc.$linefeed;
341: }
342: }
343: my $krbdefdom = '';
344: my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
345: if ($currentauth=~/^krb(4|5):/) {
346: $currentauth=~/^krb(4|5):(.*)/;
347: $krbdefdom=$1;
1.11 ! raeburn 348: } elsif ($currentauth=~ /^(unix|internal|localauth):/) {
! 349: $currentauth = $1;
1.5 raeburn 350: } else {
351: $$logmsg .= "Invalid authentication method $currentauth for $uname.".$linefeed;
352: }
1.1 raeburn 353: # Report if authentication methods are different.
1.5 raeburn 354: if ($currentauth ne $auth ) {
1.6 raeburn 355: $$logmsg .= "Authentication mismatch for $uname - $currentauth in system, $auth based on information in classlist or default for this course.".$linefeed;
1.5 raeburn 356: }
1.1 raeburn 357: # Check user data
1.5 raeburn 358: if ($first ne $userenv{'firstname'} ||
359: $middle ne $userenv{'middlename'} ||
360: $last ne $userenv{'lastname'} ||
361: $gene ne $userenv{'generation'} ||
362: $pid ne $userenv{'id'} ) {
1.1 raeburn 363: # Make the change(s)
1.5 raeburn 364: my %changeHash;
365: $changeHash{'firstname'} = $first;
366: $changeHash{'middlename'} = $middle;
367: $changeHash{'lastname'} = $last;
368: $changeHash{'generation'} = $gene;
369: $changeHash{'id'} = $pid;
370: my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
371: if ($putresult eq 'ok') {
372: $$logmsg .= "User information updated for user: $uname prior to enrollment.".$linefeed;
373: } else {
374: $$logmsg .= "There was a problem modifying user data for existing user - $uname -error: $putresult, enrollment will still be attempted.".$linefeed;
375: }
1.1 raeburn 376: }
1.3 raeburn 377:
378: # Assign the role of student in the course.
1.5 raeburn 379: my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto',$cid);
380: if ($classlist_reply eq 'ok') {
381: $enrollcount ++;
382: $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
1.6 raeburn 383: if ($context eq 'automated') {
384: $$logmsg .= "Existing $dom user $uname enrolled successfully.".$linefeed;
385: }
1.5 raeburn 386: } else {
387: $$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;
388: }
1.3 raeburn 389: }
1.1 raeburn 390: }
391: }
392: }
393: }
394: # Do drops
395: if ( ($drops == 1) && (@reg_students > 0) ) {
396: foreach my $uname (@localstudents) {
397: if ($$currlist{$uname}[$type] eq "auto") {
398: my @saved = ();
399: if (!grep/^$uname$/,@reg_students) {
400: # Check for changed usernames by checking studentIDs
401: if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
402: foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
1.6 raeburn 403: $$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 404: push @saved,$uname;
405: }
406: } elsif (@saved == 0) {
407: my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,undef,$cid);
408: if ($drop_reply !~ /^ok/) {
1.5 raeburn 409: $$logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $drop_reply.".$linefeed;
1.1 raeburn 410: } else {
411: $dropcount ++;
412: my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
413: $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname." dropped from section/group ".$$currlist{$uname}[$sec].$linefeed;
1.8 raeburn 414: if ($context eq 'automated') {
415: $$logmsg .= "User $uname student role expired from course.".$linefeed;
416: }
1.1 raeburn 417: }
418: }
419: }
420: }
421: }
422: }
1.5 raeburn 423:
424: # Terminated explictly allowed access to student creation/modification
425: if ($context eq 'automated') {
426: delete($ENV{'allowed.cst'});
427: }
1.1 raeburn 428: if ($enrollcount > 0) {
429: if ($context eq "updatenow") {
1.6 raeburn 430: $addresult = substr($addresult,0,rindex($addresult,"<li>"));
1.1 raeburn 431: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:<br/><ul><li>".$addresult."</li></ul><br/><br/>";
432: } else {
433: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:\n\n".$addresult."\n\n";
434: }
435: }
436: if ($dropcount > 0) {
437: if ($context eq "updatenow") {
1.6 raeburn 438: $dropresult = substr($dropresult,0,rindex($dropresult,"<li>"));
1.1 raeburn 439: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:<br/><ul><li>".$dropresult."</li></ul><br/><br/>";
440: } else {
441: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:\n\n".$dropresult."\n\n";
442: }
443: }
444: if ( ($adds) && ($enrollcount == 0) ) {
445: $addresult = "There were no new students to add to the course.";
446: if ($context eq "updatenow") {
447: $addresult .="<br/><br/>";
448: } else {
449: $addresult .="\n";
450: }
451: }
452: if ( ($drops) && ($dropcount == 0) ) {
453: $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.";
454: if ($context eq "updatenow") {
455: $dropresult .="<br/>";
456: } else {
457: $dropresult .="\n";
458: }
459: }
1.5 raeburn 460: my $changecount = $enrollcount + $dropcount;
461: return ($changecount,$addresult.$dropresult);
1.6 raeburn 462: }
1.1 raeburn 463:
464: sub parse_classlist {
1.6 raeburn 465: my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
1.5 raeburn 466: my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
1.6 raeburn 467: my $uname = '';
468: my @state;
1.8 raeburn 469: my @items = ('autharg','authtype','email','firstname','generation','lastname','middlename','studentID');
1.6 raeburn 470: my $p = HTML::Parser->new
471: (
472: xml_mode => 1,
473: start_h =>
474: [sub {
475: my ($tagname, $attr) = @_;
476: push @state, $tagname;
477: if ("@state" eq "students student") {
478: $uname = $attr->{username};
479: }
480: }, "tagname, attr"],
481: text_h =>
482: [sub {
483: my ($text) = @_;
484: if ("@state" eq "students student groupID") {
485: $$studentsref{$uname}[ $$placeref{'groupID'} ] = $groupID;
1.8 raeburn 486: } elsif ("@state" eq "students student startdate") {
487: my $start = $text;
488: unless ($text eq '') {
489: $start = &process_date($text);
490: }
491: $$studentsref{$uname}[ $$placeref{'startdate'} ] = $start;
492: } elsif ("@state" eq "students student enddate") {
493: my $end = $text;
494: unless ($text eq '') {
495: $end = &process_date($text);
496: }
497: $$studentsref{$uname}[ $$placeref{'enddate'} ] = $end;
1.6 raeburn 498: } else {
499: foreach my $item (@items) {
500: if ("@state" eq "students student $item") {
501: $$studentsref{$uname}[ $$placeref{$item} ] = $text;
502: }
503: }
504: }
505: }, "dtext"],
506: end_h =>
507: [sub {
508: my ($tagname) = @_;
509: pop @state;
510: }, "tagname"],
511: );
512:
513: $p->parse_file($xmlfile);
514: $p->eof;
1.8 raeburn 515: if (-e "$xmlfile") {
516: unlink $xmlfile;
517: }
1.3 raeburn 518: return;
1.1 raeburn 519: }
520:
1.8 raeburn 521: sub process_date {
522: my $timestr = shift;
523: my $timestamp = '';
524: if ($timestr =~ m/^\d{4}:\d{2}:\d{2}/) {
525: my @entries = split/:/,$timestr;
526: for (my $j=0; $j<@entries; $j++) {
527: if ( length($entries[$j]) > 1 ) {
528: $entries[$j] =~ s/^0//;
529: }
530: }
531: $entries[1] = $entries[1] - 1;
532: $timestamp = timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
533: }
534: return $timestamp;
535: }
536:
1.1 raeburn 537: sub create_password {
1.8 raeburn 538: my $passwd = '';
1.11 ! raeburn 539: my @letts = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
1.8 raeburn 540: for (my $i=0; $i<8; $i++) {
541: my $lettnum = int (rand 2);
542: my $item = '';
543: if ($lettnum) {
544: $item = $letts[int( rand(26) )];
545: my $uppercase = int(rand 2);
546: if ($uppercase) {
547: $item =~ tr/a-z/A-Z/;
548: }
549: } else {
550: $item = int( rand(10) );
551: }
552: $passwd .= $item;
553: }
554: return ($passwd);
1.9 raeburn 555: }
556:
557: sub check_user_status {
558: my ($udom,$uname,$cdom,$crs,$role,$secgrp) = @_;
559: my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
560: my @uroles = keys %userinfo;
561: my $srchstr;
562: my $active_chk = 'none';
563: if (@uroles > 0) {
564: if ( ($role eq 'cc') || ($secgrp eq '') || ( !defined($secgrp) ) ) {
565: $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
566: } else {
567: $srchstr = '/'.$cdom.'/'.$crs.'/'.$secgrp.'_'.$role;
568: }
569: if (grep/^$srchstr$/,@uroles) {
570: my $role_end = 0;
571: my $role_start = 0;
572: $active_chk = 'ok';
573: if ( $userinfo{$srchstr} =~ m/^($role)_(\d+)/ ) {
574: $role_end = $2;
575: if ( $userinfo{$srchstr} =~ m/^($role)_($role_end)_(\d+)$/ )
576: {
577: $role_start = $3;
578: }
579: }
580: if ($role_start > 0) {
581: if (time < $role_start) {
582: $active_chk = 'expired';
583: }
584: }
585: if ($role_end > 0) {
586: if (time > $role_end) {
587: $active_chk = 'expired';
588: }
589: }
590: }
591: }
592: return $active_chk;
1.1 raeburn 593: }
594:
595: sub CL_autharg { return 0; }
596: sub CL_authtype { return 1;}
597: sub CL_email { return 2;}
598: sub CL_enddate { return 3;}
599: sub CL_firstname { return 4;}
600: sub CL_generation { return 5;}
601: sub CL_groupID { return 6;}
602: sub CL_lastname { return 7;}
603: sub CL_middlename { return 8;}
604: sub CL_startdate { return 9; }
605: sub CL_studentID { return 10; }
606:
607: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>