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

1.7       albertel    1: # Automated Enrollment manager
1.58    ! raeburn     2: # $Id: Enrollment.pm,v 1.57 2021/12/28 02:04:35 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: 
1.56      raeburn    28: use lib '/home/httpd/lib/perl';
1.1       raeburn    29: use Apache::loncoursedata;
                     30: use Apache::lonnet;
1.32      albertel   31: use Apache::loncommon();
1.8       raeburn    32: use Apache::lonmsg;
1.28      raeburn    33: use Apache::lonlocal;
1.1       raeburn    34: use HTML::Entities;
1.53      raeburn    35: use HTML::Parser;
1.1       raeburn    36: use LONCAPA::Configuration;
1.53      raeburn    37: use Math::Random;
1.8       raeburn    38: use Time::Local;
1.1       raeburn    39: 
                     40: use strict;
                     41: 
                     42: sub update_LC {
1.44      raeburn    43:     my ($dom,$crs,$adds,$drops,$startdate,$enddate,$authtype,$autharg,
1.57      raeburn    44:         $showcredits,$defaultcredits,$autofailsafe,$failsafe,$classesref,
                     45:         $groupref,$logmsg,$newusermsg,$context,$phototypes) = @_;
1.19      raeburn    46: # Get institutional code and title of this class
                     47:     my %courseinfo = ();
                     48:     &get_courseinfo($dom,$crs,\%courseinfo);
1.1       raeburn    49: # Get current LON-CAPA student enrollment for this class
                     50:     my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
                     51:     my $cid = $dom."_".$crs;
1.26      raeburn    52:     my $roster = &Apache::loncoursedata::get_classlist($dom,$crs);
1.1       raeburn    53:     my $cend = &Apache::loncoursedata::CL_END;
                     54:     my $cstart = &Apache::loncoursedata::CL_START; 
                     55:     my $stuid=&Apache::loncoursedata::CL_ID;
                     56:     my $sec=&Apache::loncoursedata::CL_SECTION;
                     57:     my $status=&Apache::loncoursedata::CL_STATUS;
                     58:     my $type=&Apache::loncoursedata::CL_TYPE;
1.16      raeburn    59:     my $lockedtype=&Apache::loncoursedata::CL_LOCKEDTYPE;
1.44      raeburn    60:     my $credidx=&Apache::loncoursedata::CL_CREDITS;
1.49      raeburn    61:     my $instidx = &Apache::loncoursedata::CL_INSTSEC;
1.1       raeburn    62:     my @localstudents = ();
1.15      raeburn    63:     my @futurestudents = ();
                     64:     my @activestudents = ();
1.18      raeburn    65:     my @excludedstudents = ();
1.1       raeburn    66:     my $currlist;
1.47      raeburn    67:     my $now = time;
1.1       raeburn    68:     foreach my $uname (keys %{$roster} ) {
                     69:         if ($uname =~ m/^(.+):$dom$/) {
                     70:             if ($$roster{$uname}[$status] eq "Active") {
1.15      raeburn    71:                 push @activestudents, $1;
                     72:                 @{$$currlist{$1}} = @{$$roster{$uname}};
1.1       raeburn    73:                 push @localstudents, $1;
1.15      raeburn    74:             } elsif ( ($$roster{$uname}[$cstart] > time)  && ($$roster{$uname}[$cend] > time || $$roster{$uname}[$cend] == 0 || $$roster{$uname}[$cend] eq '') ) {
                     75:                 push @futurestudents, $1;
1.1       raeburn    76:                 @{$$currlist{$1}} = @{$$roster{$uname}};
1.15      raeburn    77:                 push @localstudents, $1;
1.18      raeburn    78:             } elsif ($$roster{$uname}[$lockedtype] == 1) {
                     79:                 push @excludedstudents, $1;
1.1       raeburn    80:             }
                     81:         }
                     82:     }
                     83:     my $linefeed = '';
                     84:     my $addresult = '';
                     85:     my $dropresult = '';
1.21      raeburn    86:     my $switchresult = '';
1.28      raeburn    87:     my $photoresult = '';
1.1       raeburn    88:     if ($context eq "updatenow") {
                     89:         $linefeed = "</li>\n<li>"; 
                     90:     } elsif ($context eq "automated") {
                     91:         $linefeed = "\n";
                     92:     }
                     93:     my $enrollcount = 0;
                     94:     my $dropcount = 0;
1.21      raeburn    95:     my $switchcount = 0;
1.1       raeburn    96: 
1.19      raeburn    97: # Get role names
                     98:     my %longroles = ();
                     99:     open(FILE,"<$$configvars{'lonTabDir'}.'/rolesplain.tab");
                    100:     my @rolesplain = <FILE>;
                    101:     close(FILE);
1.42      raeburn   102:     foreach my $item (@rolesplain) {
                    103:         if ($_ =~ /^(st|ta|ex|ad|in|cc|co):([\w\s]+):?([\w\s]*)/) {
                    104:             if ($courseinfo{'type'} eq 'Community') {
                    105:                 unless($1 eq 'cc') {
                    106:                     $longroles{$1} = $3;
                    107:                 }
                    108:             } else {
                    109:                 unless($1 eq 'co') { 
                    110:                     $longroles{$1} = $2;
                    111:                 }
                    112:             }
1.19      raeburn   113:         }
                    114:     }
                    115: 
1.8       raeburn   116:     srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand in case initial passwords have to be generated for new users.
                    117: 
1.1       raeburn   118: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class 
                    119:     my @LCids = ();
                    120:     my %unameFromLCid = ();
                    121:     foreach my $uname (sort keys %{$currlist}) {
                    122:         my $stuID = $$currlist{$uname}[$stuid];
                    123:         if (!grep/^$stuID$/,@LCids) {
                    124:             push @LCids, $stuID;
                    125:             @{$unameFromLCid{$stuID}} = ();
                    126:         }
                    127:         push @{$unameFromLCid{$stuID}},$uname;
                    128:     }
                    129:  
                    130: # Get latest institutional enrollment for this class.
                    131:     my %allenrolled = ();
                    132:     my @reg_students = ();
1.38      raeburn   133:     my %place = &place_hash(); 
1.1       raeburn   134:     my %ucount = ();
                    135:     my %enrollinfo = ();
1.49      raeburn   136:     my %classcount;
1.1       raeburn   137:     foreach my $class (@{$classesref}) {
                    138:         my %enrolled = ();
                    139:         &parse_classlist($$configvars{'lonDaemons'},$dom,$crs,$class,\%place,$$groupref{$class},\%enrolled);
1.49      raeburn   140:         $classcount{$class} = scalar(keys(%enrolled));
1.1       raeburn   141:         foreach my $uname (sort keys %enrolled ) {
                    142:             if (!grep/^$uname$/,@reg_students) {
                    143:                 push @reg_students,$uname;
                    144:                 $ucount{$uname} = 0;
                    145:                 @{$allenrolled{$uname}} = ();
                    146:             }
                    147:             @{$allenrolled{$uname}[$ucount{$uname}]} = @{$enrolled{$uname}};
                    148:             $ucount{$uname} ++;
                    149:         }
                    150:     }
                    151: 
                    152: # Check for multiple sections for a single student 
                    153:     my @okusers = ();
                    154:     foreach my $uname (@reg_students)  {
1.18      raeburn   155:         if (grep/^$uname$/,@excludedstudents) {
1.33      raeburn   156:             $$logmsg .= &mt('No re-enrollment for [_1] - user was previously manually unenrolled and locked.',$uname).$linefeed;
1.18      raeburn   157:         } elsif (@{$allenrolled{$uname}} > 1) {
1.1       raeburn   158:             my @sections = ();
                    159:             my $saved;
                    160:             for (my $i=0; $i<@{$allenrolled{$uname}}; $i++) {
                    161:                 my @stuinfo = @{$allenrolled{$uname}[$i]};
                    162:                 my $secnum = $stuinfo[ $place{'groupID'} ];
                    163:                 unless ($secnum eq '') {
                    164:                     unless (grep/^$secnum$/,@sections) {
                    165:                         $saved = $i; 
                    166:                         push @sections,$secnum;
                    167:                     }
                    168:                 }
                    169:             }
                    170:             if (@sections == 0) {
                    171:                 @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
                    172:                 push @okusers, $uname;
                    173:             }
                    174:             elsif (@sections == 1) {
                    175:                 @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[$saved]};
                    176:                 push @okusers, $uname;
                    177:             }
                    178:             elsif (@sections > 1) {
1.33      raeburn   179:                 $$logmsg .=  &mt('[_1] appears in classlists for more than one section of this course, i.e. in sections: ',$uname);
1.1       raeburn   180:                 foreach (@sections) {
1.5       raeburn   181:                     $$logmsg .= " $_,";
1.1       raeburn   182:                 }
1.5       raeburn   183:                 chop($$logmsg);
1.33      raeburn   184:                 $$logmsg .= '. '.&mt('Because of this ambiguity, no enrollment action was taken for this student.').$linefeed;
1.1       raeburn   185:             }
                    186:         } else {
                    187:             @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
                    188:             push @okusers, $uname;
                    189:         }
                    190:     }
1.40      weissno   191: # Get mapping of student/employee IDs to usernames for users in institutional data for this class  
1.1       raeburn   192:     my @allINids = ();
1.3       raeburn   193:     my %unameFromINid = ();
1.1       raeburn   194:     foreach my $uname (@okusers) {
                    195:         $enrollinfo{$uname}[ $place{'studentID'} ] =~ tr/A-Z/a-z/;
                    196:         my $stuID = $enrollinfo{$uname}[ $place{'studentID'} ];
                    197:         if (grep/^$stuID$/,@allINids)  {
                    198:             push @{$unameFromINid{$stuID}},$uname;
                    199:         } else {
                    200:             push @allINids, $stuID;
                    201:             @{$unameFromINid{$stuID}} = $uname; 
                    202:         }
                    203:     }
1.28      raeburn   204: 
1.48      raeburn   205: # Explicitly allow access to creation/modification of students and group membership changes 
                    206: # when called as an automated process.
1.5       raeburn   207:     if ($context eq 'automated') {
1.22      albertel  208:         $env{'allowed.cst'}='F';
1.48      raeburn   209:         $env{'allowed.mdg'}='F';
1.5       raeburn   210:     }
                    211: 
1.1       raeburn   212: # Compare IDs with existing LON-CAPA enrollment for this class
                    213:     foreach my $uname (@okusers) {
1.5       raeburn   214:         unless ($uname eq '') {
                    215:             my %uidhash=&Apache::lonnet::idrget($dom,$uname);
                    216:             my @stuinfo = @{$enrollinfo{$uname}};
1.49      raeburn   217:             my ($access,$added,$inststatus,$instsec);
1.44      raeburn   218:             my $credits;
                    219:             if ($showcredits) {
                    220:                 $credits = $stuinfo[$place{'credits'}];
                    221:                 $credits =~ s/[^\d\.]//g;
                    222:                 if ($credits eq $defaultcredits) {
                    223:                     undef($credits);
                    224:                 }
                    225:             }
                    226:             $inststatus = $stuinfo[$place{inststatus}];
1.49      raeburn   227:             $instsec = $stuinfo[$place{instsec}];
1.5       raeburn   228:             if (grep/^$uname$/,@localstudents) {
1.1       raeburn   229: # Check for studentID changes
1.5       raeburn   230:                 if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) )  {
                    231:                     unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
1.33      raeburn   232:                         $$logmsg .= &mt('Change in ID for [_1]. StudentID in LON-CAPA system is [_2]; StudentID in institutional data is [_3].',$uname,$uidhash{$uname},$stuinfo[ $place{studentID} ]).$linefeed; 
1.5       raeburn   233:                     }
1.1       raeburn   234:                 }
1.16      raeburn   235: # Check for switch from manual to auto
                    236:                 unless (($$currlist{$uname}[$type] eq "auto") || ($$currlist{$uname}[$lockedtype] eq "1") || (!$adds) ) {
                    237: # drop manually added student
1.38      raeburn   238:                     my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid,'',$context);
1.16      raeburn   239: # re-enroll as auto student
                    240:                     if ($drop_reply !~ /^ok/) {
1.46      raeburn   241:                             $$logmsg .= &mt('An error occurred during the attempt to convert [_1] from a manual type to an auto type student - [_2].',$uname,$drop_reply).$linefeed;
1.16      raeburn   242:                     } else {
                    243: # re-enroll as auto student
                    244:                         my ($auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc);
                    245:                         &prepare_add($authtype,$autharg,$enddate,$startdate,\@stuinfo,\%place,\$dom,\$uname,\$auth,\$authparam,\$first,\$middle,\$last,\$gene,\$usec,\$end,\$start,\$emailaddr,\$pid,\$emailenc);
                    246:                         if ($$currlist{$uname}[$sec] ne $usec) {
1.33      raeburn   247:                             my $showoldsec = $$currlist{$uname}[$sec];
                    248:                             if ($$currlist{$uname}[$sec] eq '') {
                    249:                                 $showoldsec = &mt('none');
                    250:                             }
                    251:                             my $showsec = $usec;
                    252:                             if ($usec eq '') {
                    253:                                 $showsec = &mt('none');
                    254:                             }
1.44      raeburn   255:                             $switchresult .= &mt('Section for [_1] switched from [_2] to [_3].',$uname,$showoldsec,$showsec).$linefeed;
1.21      raeburn   256:                             if ($context eq 'automated') {
1.44      raeburn   257:                                 $$logmsg .= &mt('Section switch for [_1] from [_2] to [_3].',$uname,$showoldsec,$usec).$linefeed;
1.21      raeburn   258:                             }
                    259:                             $switchcount ++;
1.16      raeburn   260:                         }
1.44      raeburn   261:                         &execute_add($context,'switchtype',$uname,$dom,$auth,
                    262:                                      $authparam,$first,$middle,$last,$gene,
                    263:                                      $pid,$usec,$end,$start,$emailenc,
1.49      raeburn   264:                                      $credits,$instsec,$cid,\$addresult,\$enrollcount,
1.44      raeburn   265:                                      $linefeed,$logmsg);
                    266:                         $added = 1;
1.16      raeburn   267:                     }
1.44      raeburn   268:                 }
1.1       raeburn   269: # Check for section changes
1.15      raeburn   270:                 if ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
                    271: # Check for access date changes for students with access starting in the future.
                    272:                     if ( (grep/^$uname$/,@futurestudents) && ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
1.16      raeburn   273:                         my $datechange = &datechange_check($$currlist{$uname}[$cstart],$$currlist{$uname}[$cend],$startdate,$enddate);
1.15      raeburn   274:                         if ($datechange) {
1.49      raeburn   275:                             my $modify_access_result = &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid,'',$context,$credits,$instsec);
1.15      raeburn   276:                             $access = &showaccess($enddate,$startdate);
                    277:                             if ($modify_access_result =~ /^ok/) {
1.33      raeburn   278:                                 $$logmsg .= &mt('Change in access dates for [_1].',$uname).$access.$linefeed;
1.44      raeburn   279:                                 $added = 1;
1.15      raeburn   280:                             } else {
1.33      raeburn   281:                                 $$logmsg .= &mt('Error when attempting to change start and/or end access dates for [_1] in section: [_2] -error [_3].',$uname,$stuinfo[$place{groupID}],$modify_access_result).$linefeed;
1.15      raeburn   282:                             }
                    283:                         }
                    284:                     }
                    285:                 } else {
1.5       raeburn   286:                     if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
1.10      raeburn   287: # Delete from roles.db for current section
                    288:                         my $expiretime = time;
                    289:                         my $uurl='/'.$cid;
                    290:                         $uurl=~s/\_/\//g;
                    291:                         if ($$currlist{$uname}[$sec]) {
                    292:                             $uurl.='/'.$$currlist{$uname}[$sec];
                    293:                         }
1.38      raeburn   294:                         my $expire_role_result = &Apache::lonnet::assignrole($dom,$uname,$uurl,'st',$expiretime,'','','',$context);
1.10      raeburn   295:                         if ($expire_role_result eq 'ok') {
1.15      raeburn   296:                             my $modify_section_result;
                    297:                             if (grep/^$uname$/,@activestudents) {
1.49      raeburn   298:                                 $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,'',$context,$credits,$instsec);
1.15      raeburn   299:                             } else {
1.49      raeburn   300:                                 $modify_section_result =  &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid,'',$context,$credits,$instsec);
1.15      raeburn   301:                                 $access =  &showaccess($enddate,$startdate);
                    302:                             }
1.10      raeburn   303:                             if ($modify_section_result =~ /^ok/) {
1.44      raeburn   304:                                 $switchresult .= &mt('Section for [_1] switched from old section: [_2] to new section: [_3].',$uname,$$currlist{$uname}[$sec],$stuinfo[ $place{groupID} ]).$access.$linefeed;
                    305:                                 $added = 1;
1.21      raeburn   306:                                 if ($context eq 'automated') {
1.44      raeburn   307:                                     $$logmsg .= &mt('Section switch for [_1] from [_2] to [_3].',$uname,$$currlist{$uname}[$sec],$stuinfo[ $place{groupID} ]).$linefeed;
1.21      raeburn   308:                                 }
                    309:                                 $switchcount ++;
1.10      raeburn   310:                             } else {
1.33      raeburn   311:                                 $$logmsg .= &mt("Error when attempting section change for [_1], from old section: '[_2]' to new section: '[_3]' -error: [_4]",$uname,$$currlist{$uname}[$sec],$stuinfo[ $place{groupID} ],$modify_section_result).$linefeed;
1.10      raeburn   312:                             }
1.5       raeburn   313:                         } else {
1.33      raeburn   314:                             $$logmsg .= &mt("Error when attempting to expire role for [_1] in old section: '[_2]' -error: '[_3]'.",$uname,$$currlist{$uname}[$sec],$expire_role_result).$linefeed;
1.5       raeburn   315:                         }
1.1       raeburn   316:                     }
                    317:                 }
1.44      raeburn   318: # Check for credits changes
                    319:                 if (($showcredits) && 
                    320:                     ($$currlist{$uname}[$credidx] ne $credits) && (!$added)) {
                    321:                     my $modify_credits_result =
1.49      raeburn   322:                         &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid,'',$context,$credits,$instsec);
1.44      raeburn   323:                     if ($modify_credits_result =~ /^ok/) {
                    324:                         if ($credits ne '') {
                    325:                             $$logmsg .= &mt('Credits change for [_1] from [_2] to [_3].',$uname,$$currlist{$uname}[$credidx],$credits).$linefeed;
                    326:                         } else {
                    327:                             $$logmsg .= &mt('Credits change for [_1] from [_2] to course default [_3].',$uname,$$currlist{$uname}[$credidx],$defaultcredits).$linefeed;
                    328:                         }
                    329:                     } else {
                    330:                         $$logmsg .= &mt('Error when attempting to change credits for [_1] in section: [_2] -error [_3].',$uname,$stuinfo[$place{groupID}],$modify_credits_result).$linefeed;
                    331:                     }
                    332:                 }
1.49      raeburn   333: # Check for institutional section change
1.55      raeburn   334:                 if (($$currlist{$uname}[$instidx] ne $instsec) && (!$added) && ($$currlist{$uname}[$type] eq "auto")) {
1.49      raeburn   335:                     my $modify_instsec_result =
                    336:                         &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid,'',$context,$credits,$instsec);
                    337:                     if ($modify_instsec_result =~ /^ok/) {
                    338:                         $$logmsg .= &mt('Institutional section change for [_1] from [_2] to [_3].',$uname,$$currlist{$uname}[$instidx],$instsec).$linefeed;
                    339:                     } else {
                    340:                         $$logmsg .= &mt('Error when attempting to change institutional section for [_1] in section: [_2] -error [_3].',$uname,$stuinfo[$place{groupID}],$modify_instsec_result).$linefeed;
                    341:                     }
                    342:                 }
1.5       raeburn   343:             } else {
1.1       raeburn   344: # Check for changed usernames by checking studentIDs
1.5       raeburn   345:                 if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
1.27      raeburn   346:                     foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } }  ) {
1.40      weissno   347:                         $$logmsg .= &mt('A possible change in username has been detected for a student enrolled in this course.').' '.&mt('The existing LON-CAPA classlist contains user: [_1] and student/employee ID: [_2].',$match,$stuinfo[ $place{studentID} ]);
1.27      raeburn   348:                         if (grep/^$match$/,@okusers) {
1.40      weissno   349:                             $$logmsg .= &mt('The username [_1] remains in the institutional classlist, but the same student/employee ID is used for new user: [_2] now found in the institutional classlist.',$match,$uname).' '.&mt('You may need to contact your Domain Coordinator to determine how to resolve this issue and whether to move student data files for user: [_1] to [_2].',$match,$uname).' ';
1.27      raeburn   350:                         } else {
                    351:                             unless ($drops == 1) {
1.40      weissno   352:                                 $$logmsg .= &mt('This username - [_1] - has been dropped from the institutional classlist, but the student/employee ID of this user is also used by [_2] who now appears in the institutional classlist.',$match,$uname).' '.&mt('You may need to contact your Domain Coordinator to request a move of the student data files for user: [_1] to [_2].',$match,$uname).' ';
1.5       raeburn   353:                             }
1.1       raeburn   354:                         }
1.40      weissno   355:                         $$logmsg .= &mt('Because of this student/employee ID conflict, the new username - [_1] - has not been added to the LON-CAPA classlist',$uname).$linefeed;
1.1       raeburn   356:                     }
1.5       raeburn   357:                 } elsif ($adds == 1) {
1.44      raeburn   358:                     my ($auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc,$credithours);
1.16      raeburn   359:                     &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   360: # Check for existing account in this LON-CAPA domain for this username
1.47      raeburn   361:                     next if (($end) && ($end < $now));
1.5       raeburn   362:                     my $uhome=&Apache::lonnet::homeserver($uname,$dom);
                    363:                     if ($uhome eq 'no_host') { # User does not exist
1.19      raeburn   364:                         my $args = {'auth' => $auth,
                    365:                                     'authparam' => $authparam,
                    366:                                     'emailenc' => $emailenc,
                    367:                                     'udom' => $dom,
                    368:                                     'uname' => $uname,
                    369:                                     'pid' => $pid,
                    370:                                     'first' => $first,
                    371:                                     'middle' => $middle,
                    372:                                     'last' => $last,
                    373:                                     'gene' => $gene,
                    374:                                     'usec' => $usec,
                    375:                                     'end' => $end,
                    376:                                     'start' => $start,
                    377:                                     'emailaddr' => $emailaddr,
                    378:                                     'cid' => $cid,
                    379:                                     'crs' => $crs,
                    380:                                     'cdom' => $dom,
                    381:                                     'context' => $context,
                    382:                                     'linefeed' => $linefeed,
1.44      raeburn   383:                                     'inststatus' => $inststatus,
1.49      raeburn   384:                                     'instsec'  => $instsec,
1.44      raeburn   385:                                     'role' => 'st',
1.19      raeburn   386:                                    };
1.44      raeburn   387:                         if ($credits) {
                    388:                             $args->{'credits'} = $credits;
                    389:                         }
1.38      raeburn   390:                         my $outcome = &create_newuser($args,$logmsg,$newusermsg,\$enrollcount,\$addresult,\%longroles,\%courseinfo,$context);
1.5       raeburn   391:                     } else {
1.44      raeburn   392:                         &execute_add($context,'newstudent',$uname,$dom,$auth,
                    393:                                      $authparam,$first,$middle,$last,$gene,$pid,
1.49      raeburn   394:                                      $usec,$end,$start,$emailenc,$credits,$instsec,
1.44      raeburn   395:                                      $cid,\$addresult,\$enrollcount,$linefeed,
                    396:                                      $logmsg);
1.3       raeburn   397:                     }
1.31      raeburn   398:                     if ($courseinfo{'showphoto'}) {
1.28      raeburn   399:                         my ($result,$resulttype) = 
                    400:                            &Apache::lonnet::auto_checkphotos($uname,$dom,$pid);
                    401:                         if ($resulttype) {
                    402:                             push(@{$$phototypes{$resulttype}},$uname);
                    403:                         }
                    404:                     }
1.1       raeburn   405:                 }
                    406:             }
                    407:         }
                    408:     }
1.31      raeburn   409:     if ($courseinfo{'showphoto'}) {
1.28      raeburn   410:         if (keys(%{$phototypes})>0) {
                    411:             my %lt = &photo_response_types();
                    412:             foreach my $type (sort(keys(%{$phototypes}))) {
                    413:                 my $numphoto = @{$$phototypes{$type}};
                    414:                 if ($numphoto > 0) {
                    415:                     if ($context eq 'updatenow') {
                    416:                         $photoresult .=  '<br /><b>'.
1.29      albertel  417: 			    &mt('For [_1] students, photos ',$numphoto).
                    418: 			    $lt{$type}.'</b><ul><li>';
1.28      raeburn   419:                     } else {
1.33      raeburn   420:                         $photoresult .=  "\n".&mt("For [quant,_1,student], photos ",$numphoto).
1.29      albertel  421: 			    $lt{$type}."\n";
1.28      raeburn   422:                     }
                    423:                     foreach my $user (@{$$phototypes{$type}}) { 
                    424:                         $photoresult .= $user.$linefeed;
                    425:                     }
                    426:                     if ($context eq 'updatenow') {
                    427:                         $photoresult = substr($photoresult,0,
1.29      albertel  428: 					      rindex($photoresult,"<li>"));
1.28      raeburn   429:                         $photoresult .= '</ul><br />';
                    430:                     } else {
                    431:                         $photoresult .= "\n";
                    432:                     }
                    433:                 }
                    434:             }
                    435:         }
                    436:     }
                    437: 
1.1       raeburn   438: # Do drops
                    439:     if ( ($drops == 1) && (@reg_students > 0) ) {
1.49      raeburn   440:         my %delaydrops;
1.1       raeburn   441:         foreach my $uname (@localstudents) {
                    442:             if ($$currlist{$uname}[$type] eq "auto") {
                    443:                 my @saved = ();
                    444:                 if (!grep/^$uname$/,@reg_students) {
                    445: # Check for changed usernames by checking studentIDs
                    446:                     if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
                    447:                         foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
1.52      raeburn   448:                             $$logmsg .= &mt('A possible change in username has been detected for a student enrolled in this course.').' '.&mt('The existing LON-CAPA classlist contains user: [_1] and student/employee ID: [_2].',$uname,$$currlist{$uname}[ $stuid ]).' '.&mt('This username has been dropped from the institutional classlist, but the same student/employee ID is used for user: [_1] who still appears in the institutional classlist.',$match).' '.&mt('You may need to move the student data files for user: [_1] to [_2]',$uname,$match).' '.&mt('Because of this, user [_1] has not been dropped from the course.',$uname).$linefeed;
1.1       raeburn   449:                             push @saved,$uname;
                    450:                         }
                    451:                     } elsif (@saved == 0) {
1.49      raeburn   452: # Check enrollment count for institutional section of student to be dropped 
                    453:                         if ($$currlist{$uname}[$instidx]) {
                    454:                             if (exists($classcount{$$currlist{$uname}[$instidx]})) {
1.57      raeburn   455:                                 if ($failsafe eq 'any') { 
1.49      raeburn   456:                                     if ($autofailsafe) {
1.57      raeburn   457:                                         push(@{$delaydrops{$$currlist{$uname}[$instidx]}},$uname);
1.49      raeburn   458:                                         next;
                    459:                                     }
1.57      raeburn   460:                                 } else {
                    461:                                     unless ($failsafe eq 'off') {
                    462:                                         if ($classcount{$$currlist{$uname}[$instidx]} == 0) {
                    463:                                             if ($autofailsafe) {
                    464:                                                 push(@{$delaydrops{$$currlist{$uname}[$instidx]}},$uname);
                    465:                                                 next;
                    466:                                             }
                    467:                                         }
                    468:                                     }
1.49      raeburn   469:                                 }
                    470:                             }
                    471:                         }
1.38      raeburn   472:                         my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid,'',$context);
1.1       raeburn   473:                         if ($drop_reply !~ /^ok/) {
1.46      raeburn   474:                             $$logmsg .= &mt('An error occurred during the attempt to expire the [_1] from the old section [_2] - [_3].',$uname,$$currlist{$uname}[$sec],$drop_reply).$linefeed;
1.1       raeburn   475:                         } else {
                    476:                             $dropcount ++;
                    477:                             my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
1.38      raeburn   478:                             $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname.' '.&mt("dropped from section: '[_1]'.",$$currlist{$uname}[$sec]).$linefeed; 
1.8       raeburn   479:                             if ($context eq 'automated') {
1.33      raeburn   480:                                 $$logmsg .= &mt('User [_1] student role expired from course.',$uname).$linefeed;
1.8       raeburn   481:                             }
1.1       raeburn   482:                         }
                    483:                     }
                    484:                 }
                    485:             }
                    486:         }
1.49      raeburn   487:         if (scalar(keys(%delaydrops)) > 0) {
                    488:             foreach my $class (keys(%delaydrops)) {
                    489:                 if (ref($delaydrops{$class}) eq 'ARRAY') {
                    490:                     if ($autofailsafe < scalar(@{$delaydrops{$class}})) {
1.58    ! raeburn   491:                         if ($failsafe eq 'any') {
        !           492:                             $$logmsg .= &mt('The following students were not expired from the old section [_1] because the number of students with roles to expire exceeded the failsafe threshold of [_2], set to apply when the enrollment retrieved for an institutional section is zero or greater:',$class,$autofailsafe);
        !           493:                         } else {
        !           494:                             $$logmsg .= &mt('The following students were not expired from the old section [_1] because the enrollment count retrieved for that institutional section was zero, and the number of students with roles to expire exceeded the failsafe threshold of [_2]:',$class,$autofailsafe);
        !           495:                         }
1.49      raeburn   496:                         if ($context eq "updatenow") {
1.50      raeburn   497:                             $$logmsg .= '<br />'.join('<br />',@{$delaydrops{$class}}).$linefeed; 
1.49      raeburn   498:                         } elsif ($context eq "automated") {
1.50      raeburn   499:                             $$logmsg .= $linefeed.join($linefeed,@{$delaydrops{$class}}).$linefeed;
1.49      raeburn   500:                         }
                    501:                     } else {
                    502:                         foreach my $uname (@{$delaydrops{$class}}) {
                    503:                             my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid,'',$context);
                    504:                             if ($drop_reply !~ /^ok/) {
                    505:                                 $$logmsg .= &mt('An error occurred during the attempt to expire the [_1] from the old section [_2] - [_3].',$uname,$$currlist{$uname}[$sec],$drop_reply).$linefeed;
                    506:                             } else {
                    507:                                 $dropcount ++;
                    508:                                 my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
                    509:                                 $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname.' '.&mt("dropped from section: '[_1]'.",$$currlist{$uname}[$sec]).$linefeed;
                    510:                                 if ($context eq 'automated') {
                    511:                                    $$logmsg .= &mt('User [_1] student role expired from course.',$uname).$linefeed;
                    512:                                 }
                    513:                             }
                    514:                         }
                    515:                     }
                    516:                 }
                    517:             }
                    518:         }
1.1       raeburn   519:     }
1.5       raeburn   520: 
1.48      raeburn   521: # Terminated explictly allowed access to student creation/modification 
                    522: # and group membership changes
1.5       raeburn   523:     if ($context eq 'automated') {
1.22      albertel  524:         delete($env{'allowed.cst'});
1.48      raeburn   525:         delete($env{'allowed.mdg'});
1.5       raeburn   526:     }
1.1       raeburn   527:     if ($enrollcount > 0) {
                    528:         if ($context eq "updatenow") {
1.6       raeburn   529:             $addresult = substr($addresult,0,rindex($addresult,"<li>"));
1.33      raeburn   530:             $addresult = &mt("The following [quant,_1,student was,students were] added to this LON-CAPA course:",$enrollcount).'<br/><ul><li>'.$addresult.'</ul><br/><br/>';
1.1       raeburn   531:         } else {
1.33      raeburn   532:             $addresult = &mt("The following [quant,_1,student was,students were] added to this LON-CAPA course:",$enrollcount)."\n\n".$addresult."\n\n";
1.21      raeburn   533:         }
1.1       raeburn   534:     }
                    535:     if ($dropcount > 0) {
                    536:         if ($context eq "updatenow") {
1.6       raeburn   537:             $dropresult = substr($dropresult,0,rindex($dropresult,"<li>"));
1.33      raeburn   538:             $dropresult = &mt("The following [quant,_1,student was,students were] expired from this LON-CAPA course:",$dropcount).'<br/><ul><li>'.$dropresult.'</ul><br/><br/>';
1.1       raeburn   539:         } else {
1.33      raeburn   540:             $dropresult = &mt("The following [quant,_1,student was,students were] expired from this LON-CAPA course:",$dropcount)."\n\n".$dropresult."\n\n";
1.1       raeburn   541:         }
                    542:     }
1.21      raeburn   543:     if ($switchcount > 0) {
                    544:         if ($context eq "updatenow") {
                    545:             $switchresult = substr($switchresult,0,rindex($switchresult,"<li>"));
1.33      raeburn   546:             $switchresult = &mt("The following [quant,_1,student] switched sections in this LON-CAPA course:",$switchcount).'<br/><ul><li>'.$switchresult.'</ul><br/><br/>';
1.21      raeburn   547:         } else {
1.33      raeburn   548:             $switchresult = &mt("The following [quant,_1,student] switched sections in this LON-CAPA course:",$switchcount)."\n\n".$switchresult."\n\n";
1.21      raeburn   549:         }
                    550:     }
1.1       raeburn   551:     if ( ($adds) && ($enrollcount == 0) ) {
1.33      raeburn   552:         $addresult = &mt('There were no new students to add to the course.');
1.1       raeburn   553:         if ($context eq "updatenow") {
                    554:             $addresult .="<br/><br/>";
                    555:         } else {
                    556:             $addresult .="\n";
                    557:         }
                    558:     }
                    559:     if ( ($drops) && ($dropcount == 0) ) {
1.33      raeburn   560:         $dropresult = &mt('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.');
1.1       raeburn   561:         if ($context eq "updatenow") {
                    562:             $dropresult .="<br/>";
                    563:         } else {
                    564:             $dropresult .="\n";
                    565:         }
                    566:     }
1.21      raeburn   567:     my $changecount = $enrollcount + $dropcount + $switchcount;
1.28      raeburn   568:     return ($changecount,$addresult.$photoresult.$dropresult.$switchresult);
1.6       raeburn   569: }
1.1       raeburn   570: 
1.19      raeburn   571: sub create_newuser {
1.36      albertel  572:     my ($args,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,
                    573: 	$courseinfo,$called_context) = @_;
1.19      raeburn   574:     my $auth = $args->{'auth'};
                    575:     my $authparam = $args->{'authparam'};
                    576:     my $emailenc = $args->{'emailenc'};
                    577:     my $udom = $args->{'udom'};
                    578:     my $uname = $args->{'uname'};
                    579:     my $pid = $args->{'pid'};
                    580:     my $first = $args->{'first'};
                    581:     my $middle = $args->{'middle'};
1.58    ! raeburn   582:     my $last = $args->{'last'};
1.19      raeburn   583:     my $gene = $args->{'gene'};
                    584:     my $usec = $args->{'usec'};
                    585:     my $end = $args->{'end'};
                    586:     my $start = $args->{'start'};
                    587:     my $emailaddr = $args->{'emailaddr'};
                    588:     my $cid = $args->{'cid'};
                    589:     my $crs = $args->{'crs'};
                    590:     my $cdom = $args->{'cdom'};
                    591:     my $context = $args->{'context'};
                    592:     my $linefeed = $args->{'linefeed'};
                    593:     my $role = $args->{'role'};
1.44      raeburn   594:     my $inststatus = $args->{'inststatus'};
                    595:     my $credits = $args->{'credits'};
1.49      raeburn   596:     my $instsec = $args->{'instsec'};
1.19      raeburn   597:     my $create_passwd = 0;
                    598:     my $authchk = '';
                    599:     my $outcome;
                    600:     unless ($authparam eq '') { $authchk = 'ok'; };
                    601: # If no account exists and passwords should be generated
                    602:     if ($auth eq "internal") {
                    603:         if ($authparam eq '') {
1.53      raeburn   604:             $authparam = &create_password($udom);
1.19      raeburn   605:             if ($authparam eq '') {
                    606:                 $authchk = '';
                    607:             } else {
                    608:                 $create_passwd = 1;
                    609:                 $authchk = 'ok';
                    610:             }
                    611:         }
                    612:     } elsif ($auth eq "localauth") {
1.34      raeburn   613:         ($authparam,$create_passwd,$authchk) = &Apache::lonnet::auto_create_password($crs,$cdom,$authparam,$udom);
1.19      raeburn   614:     } elsif ($auth =~ m/^krb/) {
                    615:         if ($authparam eq '') {
1.33      raeburn   616:             $$logmsg .= &mt('No Kerberos domain was provided for the new user - [_1], so the new user was not enrolled in the course',$uname).$linefeed;
1.19      raeburn   617:             $authchk = 'invalid';
                    618:         }
                    619:     } else {
                    620:         $authchk = 'invalid';
1.33      raeburn   621:         $$logmsg .= &mt('An invalid authentication type was provided for the new user - [_1], so the user was not enrolled in the course.',$uname).$linefeed;
1.34      raeburn   622:     }
1.19      raeburn   623:     if ($authchk eq 'ok') {
                    624: # Now create user.
                    625:         my $type = 'auto';
                    626:         my $userurl = '/'.$cdom.'/'.$crs;
                    627:         if ($usec ne '') {
                    628:             $userurl .= '/'.$usec;
                    629:         }
                    630:         if ($context eq 'createowner' || $context eq 'createcourse') {
                    631:             my $result = &Apache::lonnet::modifyuser($udom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,'1',undef,$emailaddr);
                    632:             if ($result eq 'ok' && $context eq 'createcourse') {
1.36      albertel  633:                 $outcome = &Apache::loncommon::commit_standardrole($udom,$uname,$userurl,$role,$start,$end,$cdom,$crs,$usec,$called_context);
1.19      raeburn   634:                 unless ($outcome =~ /^Error:/) {
                    635:                     $outcome = 'ok';
                    636:                 }
                    637:             } else {
                    638:                 $outcome = $result;
                    639:             }
                    640:         } else {
1.49      raeburn   641:             $outcome=&Apache::lonnet::modifystudent($udom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto','',$cid,'',$called_context,$inststatus,$credits,$instsec);
1.19      raeburn   642:         }
                    643:         if ($outcome eq 'ok') {
                    644:             my $access = &showaccess($end,$start);
1.33      raeburn   645:             my $showsec = $usec;
                    646:             if ($usec eq '') {
                    647:                 $showsec = &mt('none');
                    648:             }
1.38      raeburn   649:             $$addresult .= "$first $last ($pid) - $uname ".&mt("enrolled in section: '[_1]'.",$showsec).$access.$linefeed;
1.19      raeburn   650:             unless ($context eq 'createowner' || $context eq 'createcourse') {
                    651:                 $$enrollcount ++;
                    652:             }
1.36      albertel  653:             if ($called_context eq 'automated') {
1.33      raeburn   654:                 $$logmsg .= &mt('New [_1] user [_2] added successfully.',$udom,$uname);
1.19      raeburn   655:             }
                    656:             unless ($emailenc eq '' || $context eq 'createowner' || $context eq 'createcourse') {
                    657:                 my %emailHash;
                    658:                 $emailHash{'critnotification'}  = $emailenc;
                    659:                 $emailHash{'notification'} = $emailenc;
1.23      raeburn   660:                 $emailHash{'permanentemail'} = $emailenc;
1.19      raeburn   661:                 my $putresult = &Apache::lonnet::put('environment',\%emailHash,$udom,$uname);
                    662:             }
                    663:             if ($create_passwd) {
                    664: # Send e-mail with initial password to new user at $emailaddr.
                    665: # If e-mail address is invalid, send password via message to courseowner i
                    666: # (if automated call) or to user if roster update.
                    667:                 if ($emailaddr eq '') {
1.33      raeburn   668:                     $$newusermsg .= &mt(' username: [_1], password: [_2]',$uname,$authparam).$linefeed."\n";
1.19      raeburn   669:                 } else {
1.33      raeburn   670:                     my $subject = &mt('New LON-CAPA account');
1.19      raeburn   671:                     my $body;
1.41      raeburn   672:                     my $portalurl = 'http://'.$ENV{'SERVER_NAME'};
                    673:                     my $protocol = 'http';
                    674:                     my $lonhost=&Apache::lonnet::domain($udom,'primary');
                    675:                     if ($lonhost ne '') {
                    676:                         my $ip = &Apache::lonnet::get_host_ip($lonhost);
                    677:                         if ($Apache::lonnet::protocol{$lonhost} eq 'https') {
                    678:                             $protocol = 'https';
                    679:                         }
                    680:                         if ($ip ne '') {
                    681:                             $portalurl = $protocol.'://'.$ip
                    682:                         }
                    683:                     }
1.19      raeburn   684:                     if ($context eq 'createowner') {
1.41      raeburn   685:                         $body = &mt('A user account has been created for you while creating your new course in the LON-CAPA course management and online homework system.')."\n\n".&mt('You should log-in to the system using the following credentials:')."\n".&mt('username: ').$uname."\n".&mt('password: ').$authparam."\n\n".&mt('The URL you should use to access the LON-CAPA system at your institution is: ').$portalurl."\n\n";
1.19      raeburn   686:                     } elsif ($context eq 'createcourse') {
1.41      raeburn   687:                         $body = &mt('You have been assigned the role of [_1] in a new course: [_2] - [_3] in the LON-CAPA course management and online homework system.',$$longroles{$role},$$courseinfo{'description'},$$courseinfo{'inst_code'}).' '.&mt('As you did not have an existing user account in the system, one has been created for you.')."\n\n".&mt("You should log-in to the system using the following credentials:\nusername: [_1]\npassword: [_2]",$uname,$authparam)."\n\n".&mt('The URL you should use to access the LON-CAPA system at your institution is: ').$portalurl."\n\n"; 
1.19      raeburn   688:                     } else {
                    689:                         my $access_start = 'immediately';
                    690:                         if ($start > 0) {
                    691:                             $access_start = localtime($start)
                    692:                         }
1.45      bisitz    693:                         $body =
                    694:                             &mt('You have been enrolled in the LON-CAPA system at your institution, because you are a registered student in a class which is using the LON-CAPA course management and online homework system.')."\n\n"
                    695:                            .&mt("You should log-in to the system using the following credentials:\nusername: [_1]\npassword: [_2]",$uname,$authparam)."\n\n"
                    696:                            .&mt('The URL you should use to access the LON-CAPA system at your institution is: ').$portalurl."\n\n"
                    697:                            .&mt('When you log-in you will be able to access the LON-CAPA course for [_1] - [_2] starting [_3].',$$courseinfo{'description'},$$courseinfo{'inst_code'},$access_start)."\n";
1.19      raeburn   698:                     }
                    699:                     &Apache::lonmsg::sendemail($emailaddr,$subject,$body);
                    700:                 }
1.36      albertel  701:                 if ($called_context eq 'automated') {
1.33      raeburn   702:                     $$logmsg .= &mt(' Initial password - sent to ').$emailaddr.$linefeed;
1.19      raeburn   703:                 }
                    704:             } else {
1.36      albertel  705:                 if ($called_context eq 'automated') {
1.19      raeburn   706:                     $$logmsg .= $linefeed;
                    707:                 }
                    708:             }
                    709:         } else {
1.33      raeburn   710:             $$logmsg .= &mt('An error occurred adding new user [_1] - [_2].',$uname,$outcome).$linefeed;
1.19      raeburn   711:         }
1.34      raeburn   712:     } else {
                    713:         $$logmsg .= &mt('An error occurred adding the new user [_1] because the authcheck failed for authtype [_2] and parameter [_3].',$uname,$auth,$authparam).' '.&mt('The authcheck response was [_1].',$authchk).$linefeed;
1.19      raeburn   714:     }
                    715:     return $outcome;
                    716: }
                    717: 
1.16      raeburn   718: sub prepare_add {
                    719:     my ($authtype,$autharg,$enddate,$startdate,$stuinfo,$place,$dom,$uname,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc) = @_;
                    720:     $$auth = $$stuinfo[ $$place{'authtype'} ];
                    721:     $$authparam = $$stuinfo[ $$place{'autharg'} ];
                    722:     $$first = $$stuinfo[ $$place{'firstname'} ];
                    723:     $$middle = $$stuinfo[ $$place{'middlename'} ];
                    724:     $$last = $$stuinfo[ $$place{'lastname'} ];
                    725:     $$gene = $$stuinfo[ $$place{'generation'} ];
                    726:     $$usec = $$stuinfo[ $$place{'groupID'} ];
                    727:     $$end = $$stuinfo[ $$place{'enddate'} ];
                    728:     $$start = $$stuinfo[ $$place{'startdate'} ];
                    729:     $$emailaddr = $$stuinfo[ $$place{'email'} ];
                    730:     $$pid = $$stuinfo[ $$place{'studentID'} ];
1.47      raeburn   731: 
1.16      raeburn   732: # remove non alphanumeric values from section
                    733:     $$usec =~ s/\W//g;
                    734:                                                                                   
                    735:     unless ($$emailaddr =~/^[^\@]+\@[^\@]+$/) { $$emailaddr =''; }
                    736:     $$emailenc = &HTML::Entities::encode($$emailaddr,'<>&"');
                    737:                                                                                   
                    738: # Use course defaults where entry is absent
                    739:     if ( ($$auth eq '') || (!defined($$auth)) ) {
                    740:         $$auth =  $authtype;
                    741:     }
                    742:     if ( ($$authparam eq '')  || (!defined($$authparam)) )  {
                    743:         $$authparam = $autharg;
                    744:     }
                    745:     if ( ($$end eq '') || (!defined($$end)) )  {
                    746:         $$end = $enddate;
                    747:     }
                    748:     if ( ($$start eq '')  || (!defined($$start)) )  {
                    749:         $$start = $startdate;
                    750:     }
                    751: # Clean up whitespace
                    752:     foreach ($dom,$uname,$pid,$first,$middle,$last,$gene,$usec) {
                    753:         $$_ =~ s/(\s+$|^\s+)//g;
                    754:     }
                    755:     return;
                    756: }
                    757: 
                    758: sub execute_add {
1.44      raeburn   759:     my ($context,$caller,$uname,$dom,$auth,$authparam,$first,$middle,$last,
1.49      raeburn   760:         $gene,$pid,$usec,$end,$start,$emailenc,$credits,$instsec,$cid,$addresult,
1.44      raeburn   761:         $enrollcount,$linefeed,$logmsg) = @_;
1.16      raeburn   762: # Get the user's information and authentication
1.44      raeburn   763:     my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification','permanentemail','inststatus'],$dom,$uname);
1.16      raeburn   764:     my ($tmp) = keys(%userenv);
                    765:     if ($tmp =~ /^(con_lost|error)/i) {
                    766:         %userenv = ();
                    767:     }
                    768: # Get the user's e-mail address
                    769:     if ($userenv{critnotification} =~ m/%40/) {
                    770:         unless ($emailenc eq $userenv{critnotification}) {
1.41      raeburn   771:             $$logmsg .= &mt('Current critical notification e-mail - [_1] for [_2] is different to e-mail address in institutional classlist - [_3].',
                    772:                            $userenv{critnotification},$uname,$emailenc).
                    773:                         $linefeed;
1.16      raeburn   774:         }
                    775:     }
                    776:     if ($userenv{notification} =~ m/%40/) {
1.23      raeburn   777:         unless ($emailenc eq $userenv{notification}) {
1.41      raeburn   778:             $$logmsg .= &mt('Current standard notification e-mail - [_1] for [_2] is different to e-mail address in institutional classlist - [_3].',
                    779:                             $userenv{notification},$uname,$emailenc).
                    780:                         $linefeed;
1.16      raeburn   781:         }
                    782:     }
1.23      raeburn   783:     if ($userenv{permanentemail} =~ m/%40/) {
                    784:         unless ($emailenc eq $userenv{permanentemail}) {
1.33      raeburn   785:             $$logmsg .= &mt('Current permanent e-mail
                    786: - [_1] for [_2] is different to e-mail address in institutional classlist - [_3]',$userenv{permanentemail},$uname,$emailenc).$linefeed;
1.23      raeburn   787:         }
                    788:     }
1.16      raeburn   789:     my $krbdefdom = '';
                    790:     my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
                    791:     if ($currentauth=~/^(krb[45]):(.*)/) {
                    792:         $currentauth = $1;
                    793:         $krbdefdom = $2;
                    794:     } elsif ($currentauth=~ /^(unix|internal|localauth):/) {
                    795:         $currentauth = $1;
                    796:     } else {
1.33      raeburn   797:         $$logmsg .= &mt('Invalid authentication method [_1] for [_2].',$currentauth,$uname).$linefeed;
1.16      raeburn   798:     }
                    799: # Report if authentication methods are different.
                    800:     if ($currentauth ne $auth) {
1.33      raeburn   801:         $$logmsg .= &mt("Authentication type mismatch for [_1] - '[_2]' in system, '[_3]' based on information in classlist or default for this course.",$uname,$currentauth,$auth).$linefeed;
1.16      raeburn   802:     } elsif ($auth =~ m/^krb/) {
                    803:         if ($krbdefdom ne $authparam) {
1.33      raeburn   804:             $$logmsg .= &mt("Kerberos domain mismatch for [_1] - '[_2]' in system, '[_3]' based on information in classlist or default for this course.",$uname,$krbdefdom,$authparam).$linefeed;
1.16      raeburn   805:         }
                    806:     }
                    807:                                                                                   
                    808: # Check user data
                    809:     if ($first  ne $userenv{'firstname'}  ||
                    810:         $middle ne $userenv{'middlename'} ||
                    811:         $last   ne $userenv{'lastname'}   ||
                    812:         $gene   ne $userenv{'generation'} ||
1.23      raeburn   813:         $pid    ne $userenv{'id'} ||
                    814:         $emailenc ne $userenv{'permanentemail'} ) {
1.16      raeburn   815: # Make the change(s)
                    816:         my %changeHash;
                    817:         $changeHash{'firstname'}  = $first;
                    818:         $changeHash{'middlename'} = $middle;
                    819:         $changeHash{'lastname'}   = $last;
                    820:         $changeHash{'generation'} = $gene;
                    821:         $changeHash{'id'} = $pid;
1.23      raeburn   822:         $changeHash{'permanentemail'} = $emailenc;
1.16      raeburn   823:         my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
                    824:         if ($putresult eq 'ok') {
1.37      raeburn   825:             $$logmsg .= &mt('User information updated for user: [_1] prior to enrollment.',$uname).$linefeed;
1.16      raeburn   826:         } else {
1.33      raeburn   827:             $$logmsg .= &mt('There was a problem modifying user data for existing user - [_1] -error: [_2], enrollment will still be attempted.',$uname,$putresult).$linefeed;
1.16      raeburn   828:         }
                    829:     }
                    830:                                                                                   
                    831: # Assign the role of student in the course.
1.44      raeburn   832:     my $classlist_reply = 
                    833:         &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,
                    834:                                                    $last,$gene,$usec,$end,$start,
                    835:                                                    'auto','',$cid,'',$context,
1.49      raeburn   836:                                                    $credits,$instsec);
1.16      raeburn   837:     if ($classlist_reply eq 'ok') {
                    838:         my $access = &showaccess($end,$start);
1.33      raeburn   839:         my $showsec = $usec;
                    840:         if ($usec eq '') {
                    841:             $showsec = &mt('none');
                    842:         }
1.16      raeburn   843:         if ($caller eq 'switchtype') {
1.38      raeburn   844:             $$logmsg .= &mt("Existing user [_1] detected in institutional classlist - switched from 'manual' to 'auto' enrollment in section [_2].",$uname,$showsec).$access.$linefeed;
1.16      raeburn   845:         } elsif ($caller eq 'newstudent') {
                    846:             $$enrollcount ++;
1.38      raeburn   847:             $$addresult .= "$first $last ($pid) - $uname ".&mt("enrolled in section '[_1]'.",$showsec).$access.$linefeed;
1.16      raeburn   848:         }
                    849:         if ($context eq 'automated') {
1.33      raeburn   850:             $$logmsg .= &mt('Existing [_1] user [_2] enrolled successfully.',$dom,$uname).$linefeed;
1.16      raeburn   851:         }
                    852:     } else {
1.33      raeburn   853:            $$logmsg .= &mt('There was a problem updating the classlist db file for user [_1] to show the new enrollment -error: [_2], so no enrollment occurred for this user.',$uname,$classlist_reply).$linefeed;
1.16      raeburn   854:     }
                    855:     return;
                    856: }
                    857: 
                    858: sub datechange_check {
                    859:     my ($oldstart,$oldend,$startdate,$enddate) = @_;
                    860:     my $datechange = 0;
                    861:     unless ($oldstart eq $startdate) {
                    862:         $datechange = 1;
                    863:     }
                    864:     if (!$datechange) {
                    865:         if (!$oldend) {
                    866:             if ($enddate) {
                    867:                 $datechange = 1;
                    868:             }
                    869:         } elsif ($oldend ne $enddate) {
                    870:             $datechange = 1;
                    871:         }
                    872:     }
                    873:     return $datechange;
                    874: }
                    875: 
1.15      raeburn   876: sub showaccess {
                    877:     my ($end,$start) = @_;
                    878:     my $showstart;
                    879:     my $showend;
                    880:     if ( (!$start) || ($start <= time) ) {
                    881:         $showstart = 'immediately';
                    882:     } else {
                    883:         $showstart = &Apache::lonlocal::locallocaltime($start);
                    884:     }
                    885:     if (!$end) {
                    886:         $showend = 'no end date';
                    887:     } else {
                    888:         $showend = &Apache::lonlocal::locallocaltime($end);
                    889:     }
1.33      raeburn   890:     my $access_msg = ' '.&mt('Access starts: [_1], ends: [_2].',$showstart,$showend);
1.15      raeburn   891:     return $access_msg;
                    892: }
                    893: 
1.1       raeburn   894: sub parse_classlist {
1.6       raeburn   895:     my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
1.5       raeburn   896:     my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
1.6       raeburn   897:     my $uname = '';
                    898:     my @state;
1.44      raeburn   899:     my @items = ('autharg','authtype','email','firstname','generation','lastname','middlename','studentID','credits','inststatus');
1.6       raeburn   900:     my $p = HTML::Parser->new
                    901:     (
                    902:         xml_mode => 1,
                    903:         start_h =>
                    904:             [sub {
                    905:                  my ($tagname, $attr) = @_;
                    906:                  push @state, $tagname;
                    907:                  if ("@state" eq "students student") {
                    908:                      $uname = $attr->{username};
1.43      raeburn   909:                      $$studentsref{$uname}[ $$placeref{'groupID'} ] = $groupID;
1.49      raeburn   910:                      $$studentsref{$uname}[ $$placeref{'instsec'} ] = $class;
1.6       raeburn   911:                  }
                    912:             }, "tagname, attr"],
                    913:          text_h =>
                    914:              [sub {
                    915:                  my ($text) = @_;
1.43      raeburn   916:                  if ("@state" eq "students student startdate") {
1.8       raeburn   917:                      my $start = $text;
                    918:                      unless ($text eq '') {
                    919:                          $start = &process_date($text);
                    920:                      }
                    921:                      $$studentsref{$uname}[ $$placeref{'startdate'} ] = $start; 
                    922:                  } elsif ("@state" eq "students student enddate") {
                    923:                      my $end = $text;
                    924:                      unless ($text eq '') {
                    925:                          $end = &process_date($text);
                    926:                      }
                    927:                      $$studentsref{$uname}[ $$placeref{'enddate'} ] = $end;
1.6       raeburn   928:                  } else {
                    929:                      foreach my $item (@items) {
                    930:                          if ("@state" eq "students student $item") {
                    931:                              $$studentsref{$uname}[ $$placeref{$item} ] = $text;
                    932:                          }
                    933:                      }
                    934:                  }
                    935:                }, "dtext"],
                    936:          end_h =>
                    937:                [sub {
                    938:                    my ($tagname) = @_;
                    939:                    pop @state;
                    940:                 }, "tagname"],
                    941:     );
                    942:                                                                                                              
                    943:     $p->parse_file($xmlfile);
                    944:     $p->eof;
1.8       raeburn   945:     if (-e "$xmlfile") {
                    946:         unlink $xmlfile;
                    947:     }
1.3       raeburn   948:     return;
1.1       raeburn   949: }
                    950: 
1.8       raeburn   951: sub process_date {
                    952:     my $timestr = shift;
                    953:     my $timestamp = '';
                    954:     if ($timestr =~ m/^\d{4}:\d{2}:\d{2}/) {
                    955:         my @entries = split/:/,$timestr;
                    956:         for (my $j=0; $j<@entries; $j++) {
                    957:             if ( length($entries[$j]) > 1 ) {
                    958:                 $entries[$j] =~ s/^0//;
                    959:             }
                    960:         }
                    961:         $entries[1] = $entries[1] - 1;
                    962:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    963:     }
                    964:     return $timestamp;
                    965: }
                    966: 
1.1       raeburn   967: sub create_password {
1.53      raeburn   968:     my ($udom) = @_;
                    969:     my %passwdconf = &Apache::lonnet::get_passwdconf($udom);
                    970:     my ($min,$max,@chars);
1.54      raeburn   971:     $min = $Apache::lonnet::passwdmin;
1.53      raeburn   972:     if (ref($passwdconf{'chars'}) eq 'ARRAY') {
                    973:         if ($passwdconf{'min'} =~ /^\d+$/) {
1.54      raeburn   974:             if ($passwdconf{'min'} > $min) {
                    975:                 $min = $passwdconf{'min'};
                    976:             }
1.53      raeburn   977:         }
                    978:         if ($passwdconf{'max'} =~ /^\d+$/) {
                    979:             $max = $passwdconf{'max'};
                    980:         }
                    981:         @chars = @{$passwdconf{'chars'}};
                    982:     }
                    983:     my @letts = qw(b c d f g h j k l m n p q r s t v w x y z);
                    984:     my (@included,%reqd);
                    985:     if (@chars) {
                    986:         map { $reqd{$_} = 1; } @chars;
                    987:     }
                    988:     if ($reqd{'uc'}) {
                    989:         my $letter = $letts[int( rand(21) )];   
                    990:         $letter =~ tr/a-z/A-Z/;
                    991:         if ($letter ne '') {
                    992:             push(@included,$letter); 
                    993:         }
                    994:     }
                    995:     if ($reqd{'lc'}) {
                    996:         my $letter = $letts[int( rand(21) )];
                    997:         if ($letter ne '') {
                    998:             push(@included,$letter);
                    999:         } 
                   1000:     }
                   1001:     if ($reqd{'num'}) {
                   1002:         my $number = int( rand(10) );
                   1003:         if ($number ne '') {
                   1004:             push(@included,$number);
                   1005:         }
                   1006:     }
                   1007:     if ($reqd{'spec'}) {
                   1008:         my @specs = qw(! # * & _ - + $);
                   1009:         my $special = $specs[int( rand(8) )];
                   1010:         if ($special ne '') {
                   1011:             push(@included,$special);
                   1012:         }
                   1013:     }
                   1014:     my $start = 0;
                   1015:     if (scalar(@included) > 0) {
                   1016:         $start = scalar(@included);
                   1017:     }
                   1018:     my $end = 8;
                   1019:     if ($min =~ /^\d+$/) {
                   1020:         if ($min > $end) {
                   1021:             $end = $min;
                   1022:         } 
                   1023:     }
                   1024:     for (my $i=$start; $i<$end; $i++) {
1.8       raeburn  1025:         my $lettnum = int (rand 2);
                   1026:         my $item = '';
                   1027:         if ($lettnum) {
1.53      raeburn  1028:             $item = $letts[int( rand(21) )];
1.8       raeburn  1029:             my $uppercase = int(rand 2);
                   1030:             if ($uppercase) {
                   1031:                 $item =~ tr/a-z/A-Z/;
                   1032:             }
                   1033:         } else {
                   1034:             $item = int( rand(10) );
1.53      raeburn  1035:         }
                   1036:         if ($item ne '') {
                   1037:             push(@included,$item);
                   1038:         }
1.8       raeburn  1039:     }
1.53      raeburn  1040:     my $passwd = join('',&Math::Random::random_permutation(@included));
                   1041:     return $passwd;
1.9       raeburn  1042: }
                   1043: 
1.19      raeburn  1044: sub get_courseinfo {
                   1045:     my ($dom,$crs,$courseinfo) = @_;
                   1046:     my $owner;
                   1047:     if (defined($dom) && defined($crs)) {
1.44      raeburn  1048:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.showphoto','description','internal.defaultcredits'],$dom,$crs);
1.19      raeburn  1049:         if ( defined($settings{'internal.coursecode'}) ) {
                   1050:             $$courseinfo{'inst_code'} = $settings{'internal.coursecode'};
                   1051:         }
                   1052:         if ( defined($settings{'description'}) ) {
                   1053:             $$courseinfo{'description'} = $settings{'description'};
                   1054:         }
1.31      raeburn  1055:         if ( defined($settings{'internal.showphoto'}) ) {
                   1056:             $$courseinfo{'showphoto'} = $settings{'internal.showphoto'};
1.28      raeburn  1057:         }
1.44      raeburn  1058:         if ( defined($settings{'internal.credithours'}) ) {
                   1059:             $$courseinfo{'defaultcredits'} = $settings{'internal.defaultcredits'};
                   1060:         }
1.19      raeburn  1061:     }
                   1062:     return;
                   1063: }
                   1064: 
1.38      raeburn  1065: sub place_hash {
                   1066:     my %place = (
                   1067:                   autharg   => 0,
                   1068:                   authtype  => 1,
                   1069:                   email     => 2,
                   1070:                   enddate   => 3,
                   1071:                   firstname => 4,
                   1072:                   generation => 5,
                   1073:                   groupID    => 6,
                   1074:                   lastname   => 7,
                   1075:                   middlename => 8,
                   1076:                   startdate  => 9,
                   1077:                   studentID  => 10,
1.44      raeburn  1078:                   credits    => 11,
                   1079:                   inststatus => 12,
1.49      raeburn  1080:                   instsec    => 13,
1.38      raeburn  1081:                 );
                   1082:     return %place;
                   1083: }
1.1       raeburn  1084: 
1.28      raeburn  1085: sub photo_response_types {
1.29      albertel 1086:     my %lt = &Apache::lonlocal::texthash(
1.28      raeburn  1087:                       'same' => 'remained unchanged',
                   1088:                       'update' => 'were updated',
                   1089:                       'new' => 'were added',
                   1090:                       'missing' => 'were missing',
                   1091:                       'error' => 'were not imported because an error occurred',
                   1092:                       'nouser' => 'were for users without accounts',
1.40      weissno  1093:                       'noid' => 'were for users without student/employee IDs',
1.29      albertel 1094: 					 );
1.28      raeburn  1095:     return %lt;
                   1096: }
                   1097: 
                   1098: 
1.1       raeburn  1099: 1;

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