File:  [LON-CAPA] / loncom / enrollment / Enrollment.pm
Revision 1.57: download - view: text, annotated - select for diffs
Tue Dec 28 02:04:35 2021 UTC (2 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Extend "zero enrollment failsafe" mechanism so it can also (optionally)
  protect against unwanted drops if partial data retrieved from institutional
  source for specific institutional section.

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

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