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

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

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