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

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

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