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

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

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