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

1.7       albertel    1: # Automated Enrollment manager
1.15    ! raeburn     2: # $Id: Enrollment.pm,v 1.14 2004/03/31 19:31:56 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 = ();
1.15    ! raeburn    52:     my @futurestudents = ();
        !            53:     my @activestudents = ();
1.1       raeburn    54:     my $currlist;
                     55:     foreach my $uname (keys %{$roster} ) {
                     56:         if ($uname =~ m/^(.+):$dom$/) {
                     57:             if ($$roster{$uname}[$status] eq "Active") {
1.15    ! raeburn    58:                 push @activestudents, $1;
        !            59:                 @{$$currlist{$1}} = @{$$roster{$uname}};
1.1       raeburn    60:                 push @localstudents, $1;
1.15    ! raeburn    61:             } elsif ( ($$roster{$uname}[$cstart] > time)  && ($$roster{$uname}[$cend] > time || $$roster{$uname}[$cend] == 0 || $$roster{$uname}[$cend] eq '') ) {
        !            62:                 push @futurestudents, $1;
1.1       raeburn    63:                 @{$$currlist{$1}} = @{$$roster{$uname}};
1.15    ! raeburn    64:                 push @localstudents, $1;
1.1       raeburn    65:             }
                     66:         }
                     67:     }
                     68:     my $linefeed = '';
                     69:     my $addresult = '';
                     70:     my $dropresult = '';
                     71:     if ($context eq "updatenow") {
                     72:         $linefeed = "</li>\n<li>"; 
                     73:     } elsif ($context eq "automated") {
                     74:         $linefeed = "\n";
                     75:     }
                     76:     my $enrollcount = 0;
                     77:     my $dropcount = 0;
                     78: 
1.8       raeburn    79:     srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand in case initial passwords have to be generated for new users.
                     80: 
1.1       raeburn    81: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class 
                     82:     my @LCids = ();
                     83:     my %unameFromLCid = ();
                     84:     foreach my $uname (sort keys %{$currlist}) {
                     85:         my $stuID = $$currlist{$uname}[$stuid];
                     86:         if (!grep/^$stuID$/,@LCids) {
                     87:             push @LCids, $stuID;
                     88:             @{$unameFromLCid{$stuID}} = ();
                     89:         }
                     90:         push @{$unameFromLCid{$stuID}},$uname;
                     91:     }
                     92:  
                     93: # Get latest institutional enrollment for this class.
                     94:     my %allenrolled = ();
                     95:     my @reg_students = ();
                     96:     my %place = ();
                     97:     $place{'autharg'} = &CL_autharg();
                     98:     $place{'authtype'} = &CL_authtype();
                     99:     $place{'email'} = &CL_email();
                    100:     $place{'enddate'} = &CL_enddate();
                    101:     $place{'firstname'} = &CL_firstname();
                    102:     $place{'generation'} = &CL_generation();
                    103:     $place{'groupID'} = &CL_groupID();
                    104:     $place{'lastname'} = &CL_lastname();
                    105:     $place{'middlename'} = &CL_middlename();
                    106:     $place{'startdate'} = &CL_startdate();
                    107:     $place{'studentID'} = &CL_studentID();
                    108:     my %ucount = ();
                    109:     my %enrollinfo = ();
                    110:     foreach my $class (@{$classesref}) {
                    111:         my %enrolled = ();
                    112:         &parse_classlist($$configvars{'lonDaemons'},$dom,$crs,$class,\%place,$$groupref{$class},\%enrolled);
                    113:         foreach my $uname (sort keys %enrolled ) {
                    114:             if (!grep/^$uname$/,@reg_students) {
                    115:                 push @reg_students,$uname;
                    116:                 $ucount{$uname} = 0;
                    117:                 @{$allenrolled{$uname}} = ();
                    118:             }
                    119:             @{$allenrolled{$uname}[$ucount{$uname}]} = @{$enrolled{$uname}};
                    120:             $ucount{$uname} ++;
                    121:         }
                    122:     }
                    123: 
                    124: # Check for multiple sections for a single student 
                    125:     my @okusers = ();
                    126:     foreach my $uname (@reg_students)  {
                    127:         if (@{$allenrolled{$uname}} > 1) {
                    128:             my @sections = ();
                    129:             my $saved;
                    130:             for (my $i=0; $i<@{$allenrolled{$uname}}; $i++) {
                    131:                 my @stuinfo = @{$allenrolled{$uname}[$i]};
                    132:                 my $secnum = $stuinfo[ $place{'groupID'} ];
                    133:                 unless ($secnum eq '') {
                    134:                     unless (grep/^$secnum$/,@sections) {
                    135:                         $saved = $i; 
                    136:                         push @sections,$secnum;
                    137:                     }
                    138:                 }
                    139:             }
                    140:             if (@sections == 0) {
                    141:                 @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
                    142:                 push @okusers, $uname;
                    143:             }
                    144:             elsif (@sections == 1) {
                    145:                 @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[$saved]};
                    146:                 push @okusers, $uname;
                    147:             }
                    148:             elsif (@sections > 1) {
1.5       raeburn   149:                 $$logmsg =  "$uname appears in classlists for the more than one section of this course, i.e. in sections: ";
1.1       raeburn   150:                 foreach (@sections) {
1.5       raeburn   151:                     $$logmsg .= " $_,";
1.1       raeburn   152:                 }
1.5       raeburn   153:                 chop($$logmsg);
1.6       raeburn   154:                 $$logmsg .= ". Because of this ambiguity, no enrollment action was taken for this student.".$linefeed;
1.1       raeburn   155:             }
                    156:         } else {
                    157:             @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
                    158:             push @okusers, $uname;
                    159:         }
                    160:     }
                    161: # Get mapping of student IDs to usernames for users in institutional data for this class  
                    162:     my @allINids = ();
1.3       raeburn   163:     my %unameFromINid = ();
1.1       raeburn   164:     foreach my $uname (@okusers) {
                    165:         $enrollinfo{$uname}[ $place{'studentID'} ] =~ tr/A-Z/a-z/;
                    166:         my $stuID = $enrollinfo{$uname}[ $place{'studentID'} ];
                    167:         if (grep/^$stuID$/,@allINids)  {
                    168:             push @{$unameFromINid{$stuID}},$uname;
                    169:         } else {
                    170:             push @allINids, $stuID;
                    171:             @{$unameFromINid{$stuID}} = $uname; 
                    172:         }
                    173:     }
1.5       raeburn   174: # Explicitly allow access to creation/modification of students if called as an automated process.
                    175:     if ($context eq 'automated') {
                    176:         $ENV{'allowed.cst'}='F';
                    177:     }
                    178: 
1.1       raeburn   179: # Compare IDs with existing LON-CAPA enrollment for this class
                    180:     foreach my $uname (@okusers) {
1.5       raeburn   181:         unless ($uname eq '') {
                    182:             my %uidhash=&Apache::lonnet::idrget($dom,$uname);
                    183:             my @stuinfo = @{$enrollinfo{$uname}};
1.15    ! raeburn   184:             my $access = '';
1.5       raeburn   185:             if (grep/^$uname$/,@localstudents) {
1.1       raeburn   186: # Check for studentID changes
1.5       raeburn   187:                 if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) )  {
                    188:                     unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
1.6       raeburn   189:                         $$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   190:                     }
1.1       raeburn   191:                 }
                    192: # Check for section changes
1.15    ! raeburn   193:                 if ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
        !           194: # Check for access date changes for students with access starting in the future.
        !           195:                     if ( (grep/^$uname$/,@futurestudents) && ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
        !           196:                         my $datechange = 0;
        !           197:                         unless ($$currlist{$uname}[$cstart] eq $startdate) {
        !           198:                             $datechange = 1;
        !           199:                         }
        !           200:                         if (!$datechange) {
        !           201:                             if (!$$currlist{$uname}[$cend]) {
        !           202:                                 if ($enddate) {
        !           203:                                     $datechange = 1;
        !           204:                                 }
        !           205:                             } elsif ($$currlist{$uname}[$cend] ne $enddate) {
        !           206:                                 $datechange = 1;
        !           207:                             } 
        !           208:                         }
        !           209:                         if ($datechange) {
        !           210:                             my $modify_access_result = &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto',$cid);
        !           211:                             $access = &showaccess($enddate,$startdate);
        !           212:                             if ($modify_access_result =~ /^ok/) {
        !           213:                                 $$logmsg .= "Change in access dates for $uname.".$access.$linefeed;
        !           214:                             } else {
        !           215:                                 $$logmsg .= "Error when attempting to change start and/or end access dates for $uname in section: ".$stuinfo[ $place{groupID} ]." -error $modify_access_result".$linefeed;
        !           216:                             }
        !           217:                         }
        !           218:                     }
        !           219:                 } else {
1.5       raeburn   220:                     if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
1.10      raeburn   221: # Delete from roles.db for current section
                    222:                         my $expiretime = time;
                    223:                         my $uurl='/'.$cid;
                    224:                         $uurl=~s/\_/\//g;
                    225:                         if ($$currlist{$uname}[$sec]) {
                    226:                             $uurl.='/'.$$currlist{$uname}[$sec];
                    227:                         }
                    228:                         my $expire_role_result = &Apache::lonnet::assignrole($dom,$uname,$uurl,'st',$expiretime);
                    229:                         if ($expire_role_result eq 'ok') {
1.15    ! raeburn   230:                             my $modify_section_result;
        !           231:                             if (grep/^$uname$/,@activestudents) {
        !           232:                                 $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);
        !           233:                             } else {
        !           234:                                 $modify_section_result =  &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto',$cid);
        !           235:                                 $access =  &showaccess($enddate,$startdate);
        !           236:                             }
1.10      raeburn   237:                             if ($modify_section_result =~ /^ok/) {
1.15    ! raeburn   238:                                 $$logmsg .= "Section for $uname switched from old section: ".$$currlist{$uname}[$sec] ." to new section: ".$stuinfo[ $place{groupID} ].".".$access.$linefeed;
1.10      raeburn   239:                             } else {
                    240:                                 $$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;
                    241:                             }
1.5       raeburn   242:                         } else {
1.10      raeburn   243:                             $$logmsg .= "Error when attempting to expire role for $uname in old section" .$$currlist{$uname}[$sec]." -error: $expire_role_result".$linefeed;
1.5       raeburn   244:                         }
1.1       raeburn   245:                     }
                    246:                 }
1.5       raeburn   247:             } else {
1.1       raeburn   248: # Check for changed usernames by checking studentIDs
1.5       raeburn   249:                 if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
                    250:                     if (grep/^$$currlist{$uname}[ $place{'studentID'} ]$/,@allINids) {
                    251:                         foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } }  ) {
                    252:                             if (grep/^$match$/,@okusers) {
1.6       raeburn   253:                                 $$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   254:                             }
1.1       raeburn   255:                         }
                    256:                     }
1.5       raeburn   257:                 } elsif ($adds == 1) {
1.1       raeburn   258: # Add student to LON-CAPA classlist
1.5       raeburn   259:                     my $auth = $stuinfo[ $place{'authtype'} ];
                    260:                     my $authparam = $stuinfo[ $place{'autharg'} ];
                    261:                     my $first = $stuinfo[ $place{'firstname'} ];
                    262:                     my $middle = $stuinfo[ $place{'middlename'} ];
                    263:                     my $last = $stuinfo[ $place{'lastname'} ];
                    264:                     my $gene = $stuinfo[ $place{'generation'} ];
                    265:                     my $usec = $stuinfo[ $place{'groupID'} ];
                    266:                     my $end = $stuinfo[ $place{'enddate'} ];
                    267:                     my $start = $stuinfo[ $place{'startdate'} ];
                    268:                     my $emailaddr = $stuinfo[ $place{'email'} ];
                    269:                     my $pid = $stuinfo[ $place{'studentID'} ];
1.1       raeburn   270: 
                    271: # remove non alphanumeric values from section
1.5       raeburn   272:                     $usec =~ s/\W//g;
1.1       raeburn   273: 
1.5       raeburn   274:                     unless ($emailaddr =~/^[^\@]+\@[^\@]+$/) { $emailaddr =''; }
1.13      albertel  275:                     my $emailenc = &HTML::Entities::encode($emailaddr,'<>&"'); 
1.1       raeburn   276: 
                    277: # Use course defaults where entry is absent
1.5       raeburn   278:                     if ( ($auth eq '') || (!defined($auth)) ) {
                    279:                         $auth =  $authtype;
                    280:                     }
                    281:                     if ( ($authparam eq '')  || (!defined($authparam)) )  {
                    282:                         $authparam = $autharg;
                    283:                     }
                    284:                     if ( ($end eq '') || (!defined($end)) )  {
                    285:                          $end = $enddate;
                    286:                     }
                    287:                     if ( ($start eq '')  || (!defined($start)) )  {
                    288:                          $start = $startdate;
                    289:                     }
1.1       raeburn   290: # Clean up whitespace
1.5       raeburn   291:                     foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$usec) {
                    292:                          $$_ =~ s/(\s+$|^\s+)//g;
                    293:                     }
1.1       raeburn   294: 
                    295: # Check for existing account in this LON-CAPA domain for this username
1.5       raeburn   296:                     my $uhome=&Apache::lonnet::homeserver($uname,$dom);
                    297:                     if ($uhome eq 'no_host') { # User does not exist
                    298:                         my $create_passwd = 0;
                    299:                         my $authchk = '';
                    300:                         unless ($authparam eq '') { $authchk = 'ok'; };
1.1       raeburn   301: # If no account exists and passwords should be generated
1.11      raeburn   302:                         if ($auth eq "internal") {
1.5       raeburn   303:                             if ($authparam eq '') {
1.8       raeburn   304:                                 ($authparam) = &create_password();
                    305:                                 if ($authparam eq '') {
1.11      raeburn   306:                                     $authchk = '';
1.8       raeburn   307:                                 } else {
                    308:                                     $create_passwd = 1;
1.11      raeburn   309:                                     $authchk = 'ok';
1.8       raeburn   310:                                 }        
1.5       raeburn   311:                             }
1.11      raeburn   312:                         } elsif ($auth eq "localauth") {
                    313:                             ($authparam,$create_passwd,$authchk) = &localenroll::create_password($authparam);
                    314:                         } elsif ($auth =~ m/^krb/) {
1.5       raeburn   315:                             if ($authparam eq '') {
1.6       raeburn   316:                                 $$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   317:                                 $authchk = 'invalid';
                    318:                             }
                    319:                         } else {
1.3       raeburn   320:                             $authchk = 'invalid';
1.6       raeburn   321:                             $$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   322:                         }
1.5       raeburn   323:                         if ($authchk eq 'ok') { 
1.1       raeburn   324: # Now create user.
1.5       raeburn   325:                             my $reply=&Apache::lonnet::modifystudent($dom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto',$cid);
                    326:                             if ($reply eq 'ok') {
1.15    ! raeburn   327:                                 $access = &showaccess($end,$start);
1.5       raeburn   328:                                 $enrollcount ++;
1.15    ! raeburn   329:                                 $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$access.$linefeed;
1.6       raeburn   330:                                 if ($context eq 'automated') {
1.8       raeburn   331:                                     $$logmsg .= "New $dom user $uname added successfully.";
1.6       raeburn   332:                                 }
1.5       raeburn   333:                                 unless ($emailenc eq '') {
                    334:                                     my %emailHash;
                    335:                                     $emailHash{'critnotification'}  = $emailenc;
                    336:                                     $emailHash{'notification'} = $emailenc;
                    337:                                     my $putresult = &Apache::lonnet::put('environment',\%emailHash,$dom,$uname);
                    338:                                 }
                    339:                                 if ($create_passwd) {
1.8       raeburn   340: # Send e-mail with initial password to new user at $emailaddr. 
                    341: # If e-mail address is invalid, send password via message to courseowner i
                    342: # (if automated call) or to user if roster update. 
                    343:                                     if ($emailaddr eq '') {
                    344:                                         $$newusermsg .= " username: $uname, password: ".$authparam.$linefeed."\n";
                    345:                                     } else {
                    346:                                         my $subject = "New LON-CAPA account";
                    347:                                         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'};
                    348:                                        &Apache::lonmsg::sendemail($emailaddr,$subject,$body); 
                    349:                                     }                                 
                    350:                                     if ($context eq 'automated') {
                    351:                                         $$logmsg .= " Initial password -  - sent to ".$emailaddr.$linefeed;
                    352:                                     }
1.5       raeburn   353:                                 } else {
1.8       raeburn   354:                                     if ($context eq 'automated') {
                    355:                                         $$logmsg .= $linefeed;
                    356:                                     }
1.5       raeburn   357:                                 }
1.3       raeburn   358:                             } else {
1.5       raeburn   359:                                 $$logmsg .= "An error occurred adding new user $uname - ".$reply.$linefeed;
1.3       raeburn   360:                             }
1.1       raeburn   361:                         }
1.5       raeburn   362:                     } else {
1.1       raeburn   363: # Get the user's information and authentication
1.5       raeburn   364:                         my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification'],$dom,$uname);
                    365:                         my ($tmp) = keys(%userenv);
                    366:                         if ($tmp =~ /^(con_lost|error)/i) {
                    367:                             %userenv = ();
                    368:                         }
1.1       raeburn   369: # Get the user's e-mail address
1.5       raeburn   370:                         if ($userenv{critnotification} =~ m/%40/) {
                    371:                             unless ($emailenc eq $userenv{critnotification}) {
                    372:                                 $$logmsg .= "Current critical notification e-mail - ".$userenv{critnotification}." for $uname is different to e-mail address in Institutional classlist - ".$emailenc.$linefeed;
                    373:                             }
                    374:                         }
                    375:                         if ($userenv{notification} =~ m/%40/) {
                    376:                             unless ($emailenc eq $userenv{critnotification}) {
                    377:                                 $$logmsg .= "Current standard notification e-mail - ".$userenv{notification}." for $uname is different to e-mail address in institutional classlist - ".$emailenc.$linefeed;
                    378:                             }
                    379:                         }                            
                    380:                         my $krbdefdom = '';
                    381:                         my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
1.12      raeburn   382:                         if ($currentauth=~/^(krb[45]):(.*)/) {
                    383:                             $currentauth = $1;
                    384:                             $krbdefdom = $2;
1.11      raeburn   385:                         } elsif ($currentauth=~ /^(unix|internal|localauth):/) {
                    386:                             $currentauth = $1;
1.5       raeburn   387:                         } else {
                    388:                             $$logmsg .= "Invalid authentication method $currentauth for $uname.".$linefeed;  
                    389:                         }
1.1       raeburn   390: # Report if authentication methods are different.
1.12      raeburn   391:                         if ($currentauth ne $auth) {
                    392:                             $$logmsg .= "Authentication type mismatch for $uname - '$currentauth' in system, '$auth' based on information in classlist or default for this course.".$linefeed;
                    393:                         } elsif ($auth =~ m/^krb/) {
                    394:                             if ($krbdefdom ne $authparam) {
                    395:                                 $$logmsg .= "Kerberos domain mismatch for $uname - '$krbdefdom' in system, '$authparam' based on information in classlist or default for this course.".$linefeed;
                    396:                             }
1.14      raeburn   397:                         }
1.12      raeburn   398: 
1.1       raeburn   399: # Check user data
1.5       raeburn   400:                         if ($first  ne $userenv{'firstname'}  ||
                    401:                             $middle ne $userenv{'middlename'} ||
                    402:                             $last   ne $userenv{'lastname'}   ||
                    403:                             $gene   ne $userenv{'generation'} ||
                    404:                             $pid    ne $userenv{'id'} ) {         
1.1       raeburn   405: # Make the change(s)
1.5       raeburn   406:                             my %changeHash;
                    407:                             $changeHash{'firstname'}  = $first;
                    408:                             $changeHash{'middlename'} = $middle;
                    409:                             $changeHash{'lastname'}   = $last;
                    410:                             $changeHash{'generation'} = $gene;
                    411:                             $changeHash{'id'} = $pid;
                    412:                             my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
                    413:                             if ($putresult eq 'ok') {
                    414:                                 $$logmsg .= "User information updated for user: $uname prior to enrollment.".$linefeed;
                    415:                             } else {
                    416:                                 $$logmsg .= "There was a problem modifying user data for existing user - $uname -error: $putresult, enrollment will still be attempted.".$linefeed;
                    417:                             }
1.1       raeburn   418:                         }
1.3       raeburn   419:  
                    420: # Assign the role of student in the course.
1.5       raeburn   421:                         my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto',$cid);
                    422:                         if ($classlist_reply eq 'ok') {
1.15    ! raeburn   423:                             $access = &showaccess($end,$start);
1.5       raeburn   424:                             $enrollcount ++;
1.15    ! raeburn   425:                             $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$access.$linefeed;
1.6       raeburn   426:                             if ($context eq 'automated') {
                    427:                                 $$logmsg .= "Existing $dom user $uname enrolled successfully.".$linefeed;
                    428:                             }
1.5       raeburn   429:                         } else {
                    430:                             $$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;
                    431:                         }
1.3       raeburn   432:                     }
1.1       raeburn   433:                 }
                    434:             }
                    435:         }
                    436:     }
                    437: # Do drops
                    438:     if ( ($drops == 1) && (@reg_students > 0) ) {
                    439:         foreach my $uname (@localstudents) {
                    440:             if ($$currlist{$uname}[$type] eq "auto") {
                    441:                 my @saved = ();
                    442:                 if (!grep/^$uname$/,@reg_students) {
                    443: # Check for changed usernames by checking studentIDs
                    444:                     if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
                    445:                         foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
1.6       raeburn   446:                             $$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   447:                             push @saved,$uname;
                    448:                         }
                    449:                     } elsif (@saved == 0) {
1.15    ! raeburn   450:                         my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto',$cid);
1.1       raeburn   451:                         if ($drop_reply !~ /^ok/) {
1.5       raeburn   452:                             $$logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $drop_reply.".$linefeed;
1.1       raeburn   453:                         } else {
                    454:                             $dropcount ++;
                    455:                             my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
                    456:                             $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname." dropped from section/group ".$$currlist{$uname}[$sec].$linefeed; 
1.8       raeburn   457:                             if ($context eq 'automated') {
                    458:                                 $$logmsg .= "User $uname student role expired from course.".$linefeed;
                    459:                             }
1.1       raeburn   460:                         }
                    461:                     }
                    462:                 }
                    463:             }
                    464:         }
                    465:     }
1.5       raeburn   466: 
                    467: # Terminated explictly allowed access to student creation/modification
                    468:     if ($context eq 'automated') {
                    469:         delete($ENV{'allowed.cst'});
                    470:     }
1.1       raeburn   471:     if ($enrollcount > 0) {
                    472:         if ($context eq "updatenow") {
1.6       raeburn   473:             $addresult = substr($addresult,0,rindex($addresult,"<li>"));
1.1       raeburn   474:             $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:<br/><ul><li>".$addresult."</li></ul><br/><br/>";
                    475:         } else {
                    476:             $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:\n\n".$addresult."\n\n";    
                    477:         }      
                    478:     }
                    479:     if ($dropcount > 0) {
                    480:         if ($context eq "updatenow") {
1.6       raeburn   481:             $dropresult = substr($dropresult,0,rindex($dropresult,"<li>"));
1.1       raeburn   482:             $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:<br/><ul><li>".$dropresult."</li></ul><br/><br/>";
                    483:         } else {
                    484:             $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:\n\n".$dropresult."\n\n";
                    485:         }
                    486:     }
                    487:     if ( ($adds) && ($enrollcount == 0) ) {
                    488:         $addresult = "There were no new students to add to the course.";
                    489:         if ($context eq "updatenow") {
                    490:             $addresult .="<br/><br/>";
                    491:         } else {
                    492:             $addresult .="\n";
                    493:         }
                    494:     }
                    495:     if ( ($drops) && ($dropcount == 0) ) {
                    496:         $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.";
                    497:         if ($context eq "updatenow") {
                    498:             $dropresult .="<br/>";
                    499:         } else {
                    500:             $dropresult .="\n";
                    501:         }
                    502:     }
1.5       raeburn   503:     my $changecount = $enrollcount + $dropcount;
                    504:     return ($changecount,$addresult.$dropresult); 
1.6       raeburn   505: }
1.1       raeburn   506: 
1.15    ! raeburn   507: sub showaccess {
        !           508:     my ($end,$start) = @_;
        !           509:     my $showstart;
        !           510:     my $showend;
        !           511:     if ( (!$start) || ($start <= time) ) {
        !           512:         $showstart = 'immediately';
        !           513:     } else {
        !           514:         $showstart = &Apache::lonlocal::locallocaltime($start);
        !           515:     }
        !           516:     if (!$end) {
        !           517:         $showend = 'no end date';
        !           518:     } else {
        !           519:         $showend = &Apache::lonlocal::locallocaltime($end);
        !           520:     }
        !           521:     my $access_msg = " Access starts: ".$showstart.", ends: ".$showend.".";
        !           522:     return $access_msg;
        !           523: }
        !           524: 
1.1       raeburn   525: sub parse_classlist {
1.6       raeburn   526:     my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
1.5       raeburn   527:     my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
1.6       raeburn   528:     my $uname = '';
                    529:     my @state;
1.8       raeburn   530:     my @items = ('autharg','authtype','email','firstname','generation','lastname','middlename','studentID');
1.6       raeburn   531:     my $p = HTML::Parser->new
                    532:     (
                    533:         xml_mode => 1,
                    534:         start_h =>
                    535:             [sub {
                    536:                  my ($tagname, $attr) = @_;
                    537:                  push @state, $tagname;
                    538:                  if ("@state" eq "students student") {
                    539:                      $uname = $attr->{username};
                    540:                  }
                    541:             }, "tagname, attr"],
                    542:          text_h =>
                    543:              [sub {
                    544:                  my ($text) = @_;
                    545:                  if ("@state" eq "students student groupID") {
                    546:                      $$studentsref{$uname}[ $$placeref{'groupID'} ] = $groupID;
1.8       raeburn   547:                  } elsif ("@state" eq "students student startdate") {
                    548:                      my $start = $text;
                    549:                      unless ($text eq '') {
                    550:                          $start = &process_date($text);
                    551:                      }
                    552:                      $$studentsref{$uname}[ $$placeref{'startdate'} ] = $start; 
                    553:                  } elsif ("@state" eq "students student enddate") {
                    554:                      my $end = $text;
                    555:                      unless ($text eq '') {
                    556:                          $end = &process_date($text);
                    557:                      }
                    558:                      $$studentsref{$uname}[ $$placeref{'enddate'} ] = $end;
1.6       raeburn   559:                  } else {
                    560:                      foreach my $item (@items) {
                    561:                          if ("@state" eq "students student $item") {
                    562:                              $$studentsref{$uname}[ $$placeref{$item} ] = $text;
                    563:                          }
                    564:                      }
                    565:                  }
                    566:                }, "dtext"],
                    567:          end_h =>
                    568:                [sub {
                    569:                    my ($tagname) = @_;
                    570:                    pop @state;
                    571:                 }, "tagname"],
                    572:     );
                    573:                                                                                                              
                    574:     $p->parse_file($xmlfile);
                    575:     $p->eof;
1.8       raeburn   576:     if (-e "$xmlfile") {
                    577:         unlink $xmlfile;
                    578:     }
1.3       raeburn   579:     return;
1.1       raeburn   580: }
                    581: 
1.8       raeburn   582: sub process_date {
                    583:     my $timestr = shift;
                    584:     my $timestamp = '';
                    585:     if ($timestr =~ m/^\d{4}:\d{2}:\d{2}/) {
                    586:         my @entries = split/:/,$timestr;
                    587:         for (my $j=0; $j<@entries; $j++) {
                    588:             if ( length($entries[$j]) > 1 ) {
                    589:                 $entries[$j] =~ s/^0//;
                    590:             }
                    591:         }
                    592:         $entries[1] = $entries[1] - 1;
                    593:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    594:     }
                    595:     return $timestamp;
                    596: }
                    597: 
1.1       raeburn   598: sub create_password {
1.8       raeburn   599:     my $passwd = '';
1.11      raeburn   600:     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   601:     for (my $i=0; $i<8; $i++) {
                    602:         my $lettnum = int (rand 2);
                    603:         my $item = '';
                    604:         if ($lettnum) {
                    605:             $item = $letts[int( rand(26) )];
                    606:             my $uppercase = int(rand 2);
                    607:             if ($uppercase) {
                    608:                 $item =~ tr/a-z/A-Z/;
                    609:             }
                    610:         } else {
                    611:             $item = int( rand(10) );
                    612:         } 
                    613:         $passwd .= $item;
                    614:     }
                    615:     return ($passwd);
1.9       raeburn   616: }
                    617: 
                    618: sub check_user_status {
                    619:     my ($udom,$uname,$cdom,$crs,$role,$secgrp) = @_;
                    620:     my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
                    621:     my @uroles = keys %userinfo;
                    622:     my $srchstr;
                    623:     my $active_chk = 'none';
                    624:     if (@uroles > 0) {
                    625:         if ( ($role eq 'cc') || ($secgrp eq '') || ( !defined($secgrp) ) ) {
                    626:             $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
                    627:         } else {
                    628:             $srchstr = '/'.$cdom.'/'.$crs.'/'.$secgrp.'_'.$role;
                    629:         }
                    630:         if (grep/^$srchstr$/,@uroles) {
                    631:             my $role_end = 0;
                    632:             my $role_start = 0;
                    633:             $active_chk = 'ok';
                    634:             if ( $userinfo{$srchstr} =~ m/^($role)_(\d+)/ ) {
                    635:                 $role_end = $2;
                    636:                 if ( $userinfo{$srchstr} =~ m/^($role)_($role_end)_(\d+)$/ )
                    637:                 {
                    638:                     $role_start = $3;
                    639:                 }
                    640:             }   
                    641:             if ($role_start > 0) {
                    642:                 if (time < $role_start) {
                    643:                     $active_chk = 'expired';
                    644:                 }
                    645:             }
                    646:             if ($role_end > 0) {
                    647:                 if (time > $role_end) {
                    648:                     $active_chk = 'expired';
                    649:                 }
                    650:             }
                    651:         }
                    652:     }
                    653:     return $active_chk;
1.1       raeburn   654: }
                    655: 
                    656: sub CL_autharg { return 0; }
                    657: sub CL_authtype { return 1;}
                    658: sub CL_email { return 2;}
                    659: sub CL_enddate { return 3;}
                    660: sub CL_firstname { return 4;}
                    661: sub CL_generation { return 5;}
                    662: sub CL_groupID { return 6;}
                    663: sub CL_lastname { return 7;}
                    664: sub CL_middlename { return 8;}
                    665: sub CL_startdate { return 9; }
                    666: sub CL_studentID { return 10; }
                    667: 
                    668: 1;

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