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

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

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