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

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

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