Annotation of loncom/enrollment/Enrollment.pm, revision 1.12

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>