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

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

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