Annotation of loncom/enrollment/Enrollment.pm, revision 1.10
1.7 albertel 1: # Automated Enrollment manager
1.10 ! raeburn 2: # $Id: Enrollment.pm,v 1.9 2003/12/15 05:39:40 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/) {
246: $auth .= ":".$authparam;
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.5 raeburn 266: if ($authtype eq "int") {
267: if ($authparam eq '') {
1.8 raeburn 268: ($authparam) = &create_password();
269: if ($authparam eq '') {
270: $authchk = '';
271: } else {
272: $create_passwd = 1;
273: }
1.5 raeburn 274: }
275: } elsif ($authtype eq "local") {
1.8 raeburn 276: ($authparam,$create_passwd,$authchk) = &localenroll::create_password($authparam);
1.5 raeburn 277: } elsif ($authtype =~ m/^krb/) {
278: if ($authparam eq '') {
1.6 raeburn 279: $$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 280: $authchk = 'invalid';
281: }
282: } else {
1.3 raeburn 283: $authchk = 'invalid';
1.6 raeburn 284: $$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 285: }
1.5 raeburn 286: if ($authchk eq 'ok') {
1.1 raeburn 287: # Now create user.
1.5 raeburn 288: my $reply=&Apache::lonnet::modifystudent($dom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto',$cid);
289: if ($reply eq 'ok') {
290: $enrollcount ++;
291: $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
1.6 raeburn 292: if ($context eq 'automated') {
1.8 raeburn 293: $$logmsg .= "New $dom user $uname added successfully.";
1.6 raeburn 294: }
1.5 raeburn 295: unless ($emailenc eq '') {
296: my %emailHash;
297: $emailHash{'critnotification'} = $emailenc;
298: $emailHash{'notification'} = $emailenc;
299: my $putresult = &Apache::lonnet::put('environment',\%emailHash,$dom,$uname);
300: }
301: if ($create_passwd) {
1.8 raeburn 302: # Send e-mail with initial password to new user at $emailaddr.
303: # If e-mail address is invalid, send password via message to courseowner i
304: # (if automated call) or to user if roster update.
305: if ($emailaddr eq '') {
306: $$newusermsg .= " username: $uname, password: ".$authparam.$linefeed."\n";
307: } else {
308: my $subject = "New LON-CAPA account";
309: 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'};
310: &Apache::lonmsg::sendemail($emailaddr,$subject,$body);
311: }
312: if ($context eq 'automated') {
313: $$logmsg .= " Initial password - - sent to ".$emailaddr.$linefeed;
314: }
1.5 raeburn 315: } else {
1.8 raeburn 316: if ($context eq 'automated') {
317: $$logmsg .= $linefeed;
318: }
1.5 raeburn 319: }
1.3 raeburn 320: } else {
1.5 raeburn 321: $$logmsg .= "An error occurred adding new user $uname - ".$reply.$linefeed;
1.3 raeburn 322: }
1.1 raeburn 323: }
1.5 raeburn 324: } else {
1.1 raeburn 325: # Get the user's information and authentication
1.5 raeburn 326: my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification'],$dom,$uname);
327: my ($tmp) = keys(%userenv);
328: if ($tmp =~ /^(con_lost|error)/i) {
329: %userenv = ();
330: }
1.1 raeburn 331: # Get the user's e-mail address
1.5 raeburn 332: if ($userenv{critnotification} =~ m/%40/) {
333: unless ($emailenc eq $userenv{critnotification}) {
334: $$logmsg .= "Current critical notification e-mail - ".$userenv{critnotification}." for $uname is different to e-mail address in Institutional classlist - ".$emailenc.$linefeed;
335: }
336: }
337: if ($userenv{notification} =~ m/%40/) {
338: unless ($emailenc eq $userenv{critnotification}) {
339: $$logmsg .= "Current standard notification e-mail - ".$userenv{notification}." for $uname is different to e-mail address in institutional classlist - ".$emailenc.$linefeed;
340: }
341: }
342: my $krbdefdom = '';
343: my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
344: if ($currentauth=~/^krb(4|5):/) {
345: $currentauth=~/^krb(4|5):(.*)/;
346: $krbdefdom=$1;
347: }
348: if ($currentauth=~/^krb(4|5):/ ||
349: $currentauth=~/^unix:/ ||
350: $currentauth=~/^internal:/ ||
351: $currentauth=~/^localauth:/) {
1.1 raeburn 352:
1.5 raeburn 353: } else {
354: $$logmsg .= "Invalid authentication method $currentauth for $uname.".$linefeed;
355: }
1.1 raeburn 356: # Report if authentication methods are different.
1.5 raeburn 357: if ($currentauth ne $auth ) {
1.6 raeburn 358: $$logmsg .= "Authentication mismatch for $uname - $currentauth in system, $auth based on information in classlist or default for this course.".$linefeed;
1.5 raeburn 359: }
1.1 raeburn 360: # Check user data
1.5 raeburn 361: if ($first ne $userenv{'firstname'} ||
362: $middle ne $userenv{'middlename'} ||
363: $last ne $userenv{'lastname'} ||
364: $gene ne $userenv{'generation'} ||
365: $pid ne $userenv{'id'} ) {
1.1 raeburn 366: # Make the change(s)
1.5 raeburn 367: my %changeHash;
368: $changeHash{'firstname'} = $first;
369: $changeHash{'middlename'} = $middle;
370: $changeHash{'lastname'} = $last;
371: $changeHash{'generation'} = $gene;
372: $changeHash{'id'} = $pid;
373: my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
374: if ($putresult eq 'ok') {
375: $$logmsg .= "User information updated for user: $uname prior to enrollment.".$linefeed;
376: } else {
377: $$logmsg .= "There was a problem modifying user data for existing user - $uname -error: $putresult, enrollment will still be attempted.".$linefeed;
378: }
1.1 raeburn 379: }
1.3 raeburn 380:
381: # Assign the role of student in the course.
1.5 raeburn 382: my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto',$cid);
383: if ($classlist_reply eq 'ok') {
384: $enrollcount ++;
385: $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
1.6 raeburn 386: if ($context eq 'automated') {
387: $$logmsg .= "Existing $dom user $uname enrolled successfully.".$linefeed;
388: }
1.5 raeburn 389: } else {
390: $$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;
391: }
1.3 raeburn 392: }
1.1 raeburn 393: }
394: }
395: }
396: }
397: # Do drops
398: if ( ($drops == 1) && (@reg_students > 0) ) {
399: foreach my $uname (@localstudents) {
400: if ($$currlist{$uname}[$type] eq "auto") {
401: my @saved = ();
402: if (!grep/^$uname$/,@reg_students) {
403: # Check for changed usernames by checking studentIDs
404: if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
405: foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
1.6 raeburn 406: $$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 407: push @saved,$uname;
408: }
409: } elsif (@saved == 0) {
410: my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,undef,$cid);
411: if ($drop_reply !~ /^ok/) {
1.5 raeburn 412: $$logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $drop_reply.".$linefeed;
1.1 raeburn 413: } else {
414: $dropcount ++;
415: my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
416: $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname." dropped from section/group ".$$currlist{$uname}[$sec].$linefeed;
1.8 raeburn 417: if ($context eq 'automated') {
418: $$logmsg .= "User $uname student role expired from course.".$linefeed;
419: }
1.1 raeburn 420: }
421: }
422: }
423: }
424: }
425: }
1.5 raeburn 426:
427: # Terminated explictly allowed access to student creation/modification
428: if ($context eq 'automated') {
429: delete($ENV{'allowed.cst'});
430: }
1.1 raeburn 431: if ($enrollcount > 0) {
432: if ($context eq "updatenow") {
1.6 raeburn 433: $addresult = substr($addresult,0,rindex($addresult,"<li>"));
1.1 raeburn 434: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:<br/><ul><li>".$addresult."</li></ul><br/><br/>";
435: } else {
436: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:\n\n".$addresult."\n\n";
437: }
438: }
439: if ($dropcount > 0) {
440: if ($context eq "updatenow") {
1.6 raeburn 441: $dropresult = substr($dropresult,0,rindex($dropresult,"<li>"));
1.1 raeburn 442: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:<br/><ul><li>".$dropresult."</li></ul><br/><br/>";
443: } else {
444: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:\n\n".$dropresult."\n\n";
445: }
446: }
447: if ( ($adds) && ($enrollcount == 0) ) {
448: $addresult = "There were no new students to add to the course.";
449: if ($context eq "updatenow") {
450: $addresult .="<br/><br/>";
451: } else {
452: $addresult .="\n";
453: }
454: }
455: if ( ($drops) && ($dropcount == 0) ) {
456: $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.";
457: if ($context eq "updatenow") {
458: $dropresult .="<br/>";
459: } else {
460: $dropresult .="\n";
461: }
462: }
1.5 raeburn 463: my $changecount = $enrollcount + $dropcount;
464: return ($changecount,$addresult.$dropresult);
1.6 raeburn 465: }
1.1 raeburn 466:
467: sub parse_classlist {
1.6 raeburn 468: my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
1.5 raeburn 469: my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
1.6 raeburn 470: my $uname = '';
471: my @state;
1.8 raeburn 472: my @items = ('autharg','authtype','email','firstname','generation','lastname','middlename','studentID');
1.6 raeburn 473: my $p = HTML::Parser->new
474: (
475: xml_mode => 1,
476: start_h =>
477: [sub {
478: my ($tagname, $attr) = @_;
479: push @state, $tagname;
480: if ("@state" eq "students student") {
481: $uname = $attr->{username};
482: }
483: }, "tagname, attr"],
484: text_h =>
485: [sub {
486: my ($text) = @_;
487: if ("@state" eq "students student groupID") {
488: $$studentsref{$uname}[ $$placeref{'groupID'} ] = $groupID;
1.8 raeburn 489: } elsif ("@state" eq "students student startdate") {
490: my $start = $text;
491: unless ($text eq '') {
492: $start = &process_date($text);
493: }
494: $$studentsref{$uname}[ $$placeref{'startdate'} ] = $start;
495: } elsif ("@state" eq "students student enddate") {
496: my $end = $text;
497: unless ($text eq '') {
498: $end = &process_date($text);
499: }
500: $$studentsref{$uname}[ $$placeref{'enddate'} ] = $end;
1.6 raeburn 501: } else {
502: foreach my $item (@items) {
503: if ("@state" eq "students student $item") {
504: $$studentsref{$uname}[ $$placeref{$item} ] = $text;
505: }
506: }
507: }
508: }, "dtext"],
509: end_h =>
510: [sub {
511: my ($tagname) = @_;
512: pop @state;
513: }, "tagname"],
514: );
515:
516: $p->parse_file($xmlfile);
517: $p->eof;
1.8 raeburn 518: if (-e "$xmlfile") {
519: unlink $xmlfile;
520: }
1.3 raeburn 521: return;
1.1 raeburn 522: }
523:
1.8 raeburn 524: sub process_date {
525: my $timestr = shift;
526: my $timestamp = '';
527: if ($timestr =~ m/^\d{4}:\d{2}:\d{2}/) {
528: my @entries = split/:/,$timestr;
529: for (my $j=0; $j<@entries; $j++) {
530: if ( length($entries[$j]) > 1 ) {
531: $entries[$j] =~ s/^0//;
532: }
533: }
534: $entries[1] = $entries[1] - 1;
535: $timestamp = timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
536: }
537: return $timestamp;
538: }
539:
1.1 raeburn 540: sub create_password {
1.8 raeburn 541: my $passwd = '';
542: 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";
543: for (my $i=0; $i<8; $i++) {
544: my $lettnum = int (rand 2);
545: my $item = '';
546: if ($lettnum) {
547: $item = $letts[int( rand(26) )];
548: my $uppercase = int(rand 2);
549: if ($uppercase) {
550: $item =~ tr/a-z/A-Z/;
551: }
552: } else {
553: $item = int( rand(10) );
554: }
555: $passwd .= $item;
556: }
557: return ($passwd);
1.9 raeburn 558: }
559:
560: sub check_user_status {
561: my ($udom,$uname,$cdom,$crs,$role,$secgrp) = @_;
562: my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
563: my @uroles = keys %userinfo;
564: my $srchstr;
565: my $active_chk = 'none';
566: if (@uroles > 0) {
567: if ( ($role eq 'cc') || ($secgrp eq '') || ( !defined($secgrp) ) ) {
568: $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
569: } else {
570: $srchstr = '/'.$cdom.'/'.$crs.'/'.$secgrp.'_'.$role;
571: }
572: if (grep/^$srchstr$/,@uroles) {
573: my $role_end = 0;
574: my $role_start = 0;
575: $active_chk = 'ok';
576: if ( $userinfo{$srchstr} =~ m/^($role)_(\d+)/ ) {
577: $role_end = $2;
578: if ( $userinfo{$srchstr} =~ m/^($role)_($role_end)_(\d+)$/ )
579: {
580: $role_start = $3;
581: }
582: }
583: if ($role_start > 0) {
584: if (time < $role_start) {
585: $active_chk = 'expired';
586: }
587: }
588: if ($role_end > 0) {
589: if (time > $role_end) {
590: $active_chk = 'expired';
591: }
592: }
593: }
594: }
595: return $active_chk;
1.1 raeburn 596: }
597:
598: sub CL_autharg { return 0; }
599: sub CL_authtype { return 1;}
600: sub CL_email { return 2;}
601: sub CL_enddate { return 3;}
602: sub CL_firstname { return 4;}
603: sub CL_generation { return 5;}
604: sub CL_groupID { return 6;}
605: sub CL_lastname { return 7;}
606: sub CL_middlename { return 8;}
607: sub CL_startdate { return 9; }
608: sub CL_studentID { return 10; }
609:
610: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>