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

1.7       albertel    1: # Automated Enrollment manager
1.36    ! albertel    2: # $Id: Enrollment.pm,v 1.35 2007/07/02 03:37:19 raeburn Exp $
1.7       albertel    3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
1.1       raeburn    26: package LONCAPA::Enrollment;
                     27: 
                     28: use Apache::loncoursedata;
                     29: use Apache::lonnet;
1.32      albertel   30: use Apache::loncommon();
1.8       raeburn    31: use Apache::lonmsg;
1.28      raeburn    32: use Apache::lonlocal;
1.1       raeburn    33: use HTML::Entities;
                     34: use LONCAPA::Configuration;
1.8       raeburn    35: use Time::Local;
                     36: use lib '/home/httpd/lib/perl';
1.1       raeburn    37: 
                     38: use strict;
                     39: 
                     40: sub update_LC {
1.28      raeburn    41:     my ($dom,$crs,$adds,$drops,$startdate,$enddate,$authtype,$autharg,$classesref,$groupref,$logmsg,$newusermsg,$context,$phototypes) = @_; 
1.19      raeburn    42: # Get institutional code and title of this class
                     43:     my %courseinfo = ();
                     44:     &get_courseinfo($dom,$crs,\%courseinfo);
1.1       raeburn    45: # Get current LON-CAPA student enrollment for this class
                     46:     my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
                     47:     my $cid = $dom."_".$crs;
1.26      raeburn    48:     my $roster = &Apache::loncoursedata::get_classlist($dom,$crs);
1.1       raeburn    49:     my $cend = &Apache::loncoursedata::CL_END;
                     50:     my $cstart = &Apache::loncoursedata::CL_START; 
                     51:     my $stuid=&Apache::loncoursedata::CL_ID;
                     52:     my $sec=&Apache::loncoursedata::CL_SECTION;
                     53:     my $status=&Apache::loncoursedata::CL_STATUS;
                     54:     my $type=&Apache::loncoursedata::CL_TYPE;
1.16      raeburn    55:     my $lockedtype=&Apache::loncoursedata::CL_LOCKEDTYPE;
1.1       raeburn    56:     my @localstudents = ();
1.15      raeburn    57:     my @futurestudents = ();
                     58:     my @activestudents = ();
1.18      raeburn    59:     my @excludedstudents = ();
1.1       raeburn    60:     my $currlist;
                     61:     foreach my $uname (keys %{$roster} ) {
                     62:         if ($uname =~ m/^(.+):$dom$/) {
                     63:             if ($$roster{$uname}[$status] eq "Active") {
1.15      raeburn    64:                 push @activestudents, $1;
                     65:                 @{$$currlist{$1}} = @{$$roster{$uname}};
1.1       raeburn    66:                 push @localstudents, $1;
1.15      raeburn    67:             } elsif ( ($$roster{$uname}[$cstart] > time)  && ($$roster{$uname}[$cend] > time || $$roster{$uname}[$cend] == 0 || $$roster{$uname}[$cend] eq '') ) {
                     68:                 push @futurestudents, $1;
1.1       raeburn    69:                 @{$$currlist{$1}} = @{$$roster{$uname}};
1.15      raeburn    70:                 push @localstudents, $1;
1.18      raeburn    71:             } elsif ($$roster{$uname}[$lockedtype] == 1) {
                     72:                 push @excludedstudents, $1;
1.1       raeburn    73:             }
                     74:         }
                     75:     }
                     76:     my $linefeed = '';
                     77:     my $addresult = '';
                     78:     my $dropresult = '';
1.21      raeburn    79:     my $switchresult = '';
1.28      raeburn    80:     my $photoresult = '';
1.1       raeburn    81:     if ($context eq "updatenow") {
                     82:         $linefeed = "</li>\n<li>"; 
                     83:     } elsif ($context eq "automated") {
                     84:         $linefeed = "\n";
                     85:     }
                     86:     my $enrollcount = 0;
                     87:     my $dropcount = 0;
1.21      raeburn    88:     my $switchcount = 0;
1.1       raeburn    89: 
1.19      raeburn    90: # Get role names
                     91:     my %longroles = ();
                     92:     open(FILE,"<$$configvars{'lonTabDir'}.'/rolesplain.tab");
                     93:     my @rolesplain = <FILE>;
                     94:     close(FILE);
                     95:     foreach (@rolesplain) {
                     96:         if ($_ =~ /^(st|ta|ex|ad|in|cc):([\w\s]+)$/) {
                     97:             $longroles{$1} = $2;
                     98:         }
                     99:     }
                    100: 
1.8       raeburn   101:     srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand in case initial passwords have to be generated for new users.
                    102: 
1.1       raeburn   103: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class 
                    104:     my @LCids = ();
                    105:     my %unameFromLCid = ();
                    106:     foreach my $uname (sort keys %{$currlist}) {
                    107:         my $stuID = $$currlist{$uname}[$stuid];
                    108:         if (!grep/^$stuID$/,@LCids) {
                    109:             push @LCids, $stuID;
                    110:             @{$unameFromLCid{$stuID}} = ();
                    111:         }
                    112:         push @{$unameFromLCid{$stuID}},$uname;
                    113:     }
                    114:  
                    115: # Get latest institutional enrollment for this class.
                    116:     my %allenrolled = ();
                    117:     my @reg_students = ();
                    118:     my %place = ();
                    119:     $place{'autharg'} = &CL_autharg();
                    120:     $place{'authtype'} = &CL_authtype();
                    121:     $place{'email'} = &CL_email();
                    122:     $place{'enddate'} = &CL_enddate();
                    123:     $place{'firstname'} = &CL_firstname();
                    124:     $place{'generation'} = &CL_generation();
                    125:     $place{'groupID'} = &CL_groupID();
                    126:     $place{'lastname'} = &CL_lastname();
                    127:     $place{'middlename'} = &CL_middlename();
                    128:     $place{'startdate'} = &CL_startdate();
                    129:     $place{'studentID'} = &CL_studentID();
                    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:     }
                    185: # Get mapping of student IDs to usernames for users in institutional data for this class  
                    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.15      raeburn   209:             my $access = '';
1.5       raeburn   210:             if (grep/^$uname$/,@localstudents) {
1.1       raeburn   211: # Check for studentID changes
1.5       raeburn   212:                 if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) )  {
                    213:                     unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
1.33      raeburn   214:                         $$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   215:                     }
1.1       raeburn   216:                 }
1.16      raeburn   217: # Check for switch from manual to auto
                    218:                 unless (($$currlist{$uname}[$type] eq "auto") || ($$currlist{$uname}[$lockedtype] eq "1") || (!$adds) ) {
                    219: # drop manually added student
                    220:                     my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid);
                    221: # re-enroll as auto student
                    222:                     if ($drop_reply !~ /^ok/) {
1.33      raeburn   223:                             $$logmsg .= &mt('An error occured during the attempt to convert [_1] from a manual type to an auto type student - [_2].',$uname,$drop_reply).$linefeed;
1.16      raeburn   224:                     } else {
                    225: # re-enroll as auto student
                    226:                         my ($auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc);
                    227:                         &prepare_add($authtype,$autharg,$enddate,$startdate,\@stuinfo,\%place,\$dom,\$uname,\$auth,\$authparam,\$first,\$middle,\$last,\$gene,\$usec,\$end,\$start,\$emailaddr,\$pid,\$emailenc);
                    228:                         if ($$currlist{$uname}[$sec] ne $usec) {
1.33      raeburn   229:                             my $showoldsec = $$currlist{$uname}[$sec];
                    230:                             if ($$currlist{$uname}[$sec] eq '') {
                    231:                                 $showoldsec = &mt('none');
                    232:                             }
                    233:                             my $showsec = $usec;
                    234:                             if ($usec eq '') {
                    235:                                 $showsec = &mt('none');
                    236:                             }
                    237:                             $switchresult .= &mt("Section for [_1] switched from '[_2]' to '[_3]'.",$uname,$showoldsec,$showsec).$linefeed;
1.21      raeburn   238:                             if ($context eq 'automated') {
1.33      raeburn   239:                                 $$logmsg .= &mt("Section switch for [_1] from '[_2]' to '[_3]'.",$uname,$showoldsec,$usec).$linefeed;
1.21      raeburn   240:                             }
                    241:                             $switchcount ++;
1.16      raeburn   242:                         }
                    243:                         &execute_add($context,'switchtype',$uname,$dom,$auth,$authparam,$first,$middle,$last,$gene,$pid,$usec,$end,$start,$emailenc,$cid,\$addresult,\$enrollcount,$linefeed,$logmsg);
                    244:                     }
                    245:                 } 
1.1       raeburn   246: # Check for section changes
1.15      raeburn   247:                 if ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
                    248: # Check for access date changes for students with access starting in the future.
                    249:                     if ( (grep/^$uname$/,@futurestudents) && ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
1.16      raeburn   250:                         my $datechange = &datechange_check($$currlist{$uname}[$cstart],$$currlist{$uname}[$cend],$startdate,$enddate);
1.15      raeburn   251:                         if ($datechange) {
1.16      raeburn   252:                             my $modify_access_result = &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid);
1.15      raeburn   253:                             $access = &showaccess($enddate,$startdate);
                    254:                             if ($modify_access_result =~ /^ok/) {
1.33      raeburn   255:                                 $$logmsg .= &mt('Change in access dates for [_1].',$uname).$access.$linefeed;
1.15      raeburn   256:                             } else {
1.33      raeburn   257:                                 $$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   258:                             }
                    259:                         }
                    260:                     }
                    261:                 } else {
1.5       raeburn   262:                     if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
1.10      raeburn   263: # Delete from roles.db for current section
                    264:                         my $expiretime = time;
                    265:                         my $uurl='/'.$cid;
                    266:                         $uurl=~s/\_/\//g;
                    267:                         if ($$currlist{$uname}[$sec]) {
                    268:                             $uurl.='/'.$$currlist{$uname}[$sec];
                    269:                         }
                    270:                         my $expire_role_result = &Apache::lonnet::assignrole($dom,$uname,$uurl,'st',$expiretime);
                    271:                         if ($expire_role_result eq 'ok') {
1.15      raeburn   272:                             my $modify_section_result;
                    273:                             if (grep/^$uname$/,@activestudents) {
1.16      raeburn   274:                                 $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);
1.15      raeburn   275:                             } else {
1.16      raeburn   276:                                 $modify_section_result =  &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid);
1.15      raeburn   277:                                 $access =  &showaccess($enddate,$startdate);
                    278:                             }
1.10      raeburn   279:                             if ($modify_section_result =~ /^ok/) {
1.33      raeburn   280:                                 $switchresult .= &mt("Section for [_1] switched from old section: '[_2]' to new section: '[_3]'.",$uname,$$currlist{$uname}[$sec],$stuinfo[ $place{groupID} ]).$access.$linefeed;
1.21      raeburn   281:                                 if ($context eq 'automated') {
1.33      raeburn   282:                                     $$logmsg .= &mt('Section switch for [_1] from [_2] to [_3]',$uname,$$currlist{$uname}[$sec],$stuinfo[ $place{groupID} ]).$linefeed;
1.21      raeburn   283:                                 }
                    284:                                 $switchcount ++;
1.10      raeburn   285:                             } else {
1.33      raeburn   286:                                 $$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   287:                             }
1.5       raeburn   288:                         } else {
1.33      raeburn   289:                             $$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   290:                         }
1.1       raeburn   291:                     }
                    292:                 }
1.5       raeburn   293:             } else {
1.1       raeburn   294: # Check for changed usernames by checking studentIDs
1.5       raeburn   295:                 if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
1.27      raeburn   296:                     foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } }  ) {
1.33      raeburn   297:                         $$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 ID: [_2].',$match,$stuinfo[ $place{studentID} ]);
1.27      raeburn   298:                         if (grep/^$match$/,@okusers) {
1.33      raeburn   299:                             $$logmsg .= &mt('The username [_1] remains in the institutional classlist, but the same student 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   300:                         } else {
                    301:                             unless ($drops == 1) {
1.33      raeburn   302:                                 $$logmsg .= &mt('This username - [_1] - has been dropped from the institutional classlist, but the student 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   303:                             }
1.1       raeburn   304:                         }
1.33      raeburn   305:                         $$logmsg .= &mt('Because of this student ID conflict, the new username - [_1] - has not been added to the LON-CAPA classlist',$uname).$linefeed;
1.1       raeburn   306:                     }
1.5       raeburn   307:                 } elsif ($adds == 1) {
1.16      raeburn   308:                     my ($auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc);
                    309:                     &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   310: # Check for existing account in this LON-CAPA domain for this username
1.5       raeburn   311:                     my $uhome=&Apache::lonnet::homeserver($uname,$dom);
                    312:                     if ($uhome eq 'no_host') { # User does not exist
1.19      raeburn   313:                         my $args = {'auth' => $auth,
                    314:                                     'authparam' => $authparam,
                    315:                                     'emailenc' => $emailenc,
                    316:                                     'udom' => $dom,
                    317:                                     'uname' => $uname,
                    318:                                     'pid' => $pid,
                    319:                                     'first' => $first,
                    320:                                     'middle' => $middle,
                    321:                                     'last' => $last,
                    322:                                     'gene' => $gene,
                    323:                                     'usec' => $usec,
                    324:                                     'end' => $end,
                    325:                                     'start' => $start,
                    326:                                     'emailaddr' => $emailaddr,
                    327:                                     'cid' => $cid,
                    328:                                     'crs' => $crs,
                    329:                                     'cdom' => $dom,
                    330:                                     'context' => $context,
                    331:                                     'linefeed' => $linefeed,
                    332:                                     'role' => 'st'
                    333:                                    };
1.20      raeburn   334:                         my $outcome = &create_newuser($args,$logmsg,$newusermsg,\$enrollcount,\$addresult,\%longroles,\%courseinfo);
1.5       raeburn   335:                     } else {
1.16      raeburn   336:                         &execute_add($context,'newstudent',$uname,$dom,$auth,$authparam,$first,$middle,$last,$gene,$pid,$usec,$end,$start,$emailenc,$cid,\$addresult,\$enrollcount,$linefeed,$logmsg);
1.3       raeburn   337:                     }
1.31      raeburn   338:                     if ($courseinfo{'showphoto'}) {
1.28      raeburn   339:                         my ($result,$resulttype) = 
                    340:                            &Apache::lonnet::auto_checkphotos($uname,$dom,$pid);
                    341:                         if ($resulttype) {
                    342:                             push(@{$$phototypes{$resulttype}},$uname);
                    343:                         }
                    344:                     }
1.1       raeburn   345:                 }
                    346:             }
                    347:         }
                    348:     }
1.31      raeburn   349:     if ($courseinfo{'showphoto'}) {
1.28      raeburn   350:         if (keys(%{$phototypes})>0) {
                    351:             my %lt = &photo_response_types();
                    352:             foreach my $type (sort(keys(%{$phototypes}))) {
                    353:                 my $numphoto = @{$$phototypes{$type}};
                    354:                 if ($numphoto > 0) {
                    355:                     if ($context eq 'updatenow') {
                    356:                         $photoresult .=  '<br /><b>'.
1.29      albertel  357: 			    &mt('For [_1] students, photos ',$numphoto).
                    358: 			    $lt{$type}.'</b><ul><li>';
1.28      raeburn   359:                     } else {
1.33      raeburn   360:                         $photoresult .=  "\n".&mt("For [quant,_1,student], photos ",$numphoto).
1.29      albertel  361: 			    $lt{$type}."\n";
1.28      raeburn   362:                     }
                    363:                     foreach my $user (@{$$phototypes{$type}}) { 
                    364:                         $photoresult .= $user.$linefeed;
                    365:                     }
                    366:                     if ($context eq 'updatenow') {
                    367:                         $photoresult = substr($photoresult,0,
1.29      albertel  368: 					      rindex($photoresult,"<li>"));
1.28      raeburn   369:                         $photoresult .= '</ul><br />';
                    370:                     } else {
                    371:                         $photoresult .= "\n";
                    372:                     }
                    373:                 }
                    374:             }
                    375:         }
                    376:     }
                    377: 
1.1       raeburn   378: # Do drops
                    379:     if ( ($drops == 1) && (@reg_students > 0) ) {
                    380:         foreach my $uname (@localstudents) {
                    381:             if ($$currlist{$uname}[$type] eq "auto") {
                    382:                 my @saved = ();
                    383:                 if (!grep/^$uname$/,@reg_students) {
                    384: # Check for changed usernames by checking studentIDs
                    385:                     if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
                    386:                         foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
1.33      raeburn   387:                             $$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 ID: [_2].',$uname,$$currlist{$uname}[ $place{studentID} ]).' '.&mt('This username has been dropped from the institutional classlist, but the same student 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   388:                             push @saved,$uname;
                    389:                         }
                    390:                     } elsif (@saved == 0) {
1.16      raeburn   391:                         my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid);
1.1       raeburn   392:                         if ($drop_reply !~ /^ok/) {
1.33      raeburn   393:                             $$logmsg .= &mt('An error occured during the attempt to expire the [_1] from the old section [_2] - [_3].',$uname,$$currlist{$uname}[$sec],$drop_reply).$linefeed;
1.1       raeburn   394:                         } else {
                    395:                             $dropcount ++;
                    396:                             my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
1.33      raeburn   397:                             $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname.' '.&mt("dropped from section/group: '[_1]'.",$$currlist{$uname}[$sec]).$linefeed; 
1.8       raeburn   398:                             if ($context eq 'automated') {
1.33      raeburn   399:                                 $$logmsg .= &mt('User [_1] student role expired from course.',$uname).$linefeed;
1.8       raeburn   400:                             }
1.1       raeburn   401:                         }
                    402:                     }
                    403:                 }
                    404:             }
                    405:         }
                    406:     }
1.5       raeburn   407: 
                    408: # Terminated explictly allowed access to student creation/modification
                    409:     if ($context eq 'automated') {
1.22      albertel  410:         delete($env{'allowed.cst'});
1.5       raeburn   411:     }
1.1       raeburn   412:     if ($enrollcount > 0) {
                    413:         if ($context eq "updatenow") {
1.6       raeburn   414:             $addresult = substr($addresult,0,rindex($addresult,"<li>"));
1.33      raeburn   415:             $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   416:         } else {
1.33      raeburn   417:             $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   418:         }
1.1       raeburn   419:     }
                    420:     if ($dropcount > 0) {
                    421:         if ($context eq "updatenow") {
1.6       raeburn   422:             $dropresult = substr($dropresult,0,rindex($dropresult,"<li>"));
1.33      raeburn   423:             $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   424:         } else {
1.33      raeburn   425:             $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   426:         }
                    427:     }
1.21      raeburn   428:     if ($switchcount > 0) {
                    429:         if ($context eq "updatenow") {
                    430:             $switchresult = substr($switchresult,0,rindex($switchresult,"<li>"));
1.33      raeburn   431:             $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   432:         } else {
1.33      raeburn   433:             $switchresult = &mt("The following [quant,_1,student] switched sections in this LON-CAPA course:",$switchcount)."\n\n".$switchresult."\n\n";
1.21      raeburn   434:         }
                    435:     }
1.1       raeburn   436:     if ( ($adds) && ($enrollcount == 0) ) {
1.33      raeburn   437:         $addresult = &mt('There were no new students to add to the course.');
1.1       raeburn   438:         if ($context eq "updatenow") {
                    439:             $addresult .="<br/><br/>";
                    440:         } else {
                    441:             $addresult .="\n";
                    442:         }
                    443:     }
                    444:     if ( ($drops) && ($dropcount == 0) ) {
1.33      raeburn   445:         $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   446:         if ($context eq "updatenow") {
                    447:             $dropresult .="<br/>";
                    448:         } else {
                    449:             $dropresult .="\n";
                    450:         }
                    451:     }
1.21      raeburn   452:     my $changecount = $enrollcount + $dropcount + $switchcount;
1.28      raeburn   453:     return ($changecount,$addresult.$photoresult.$dropresult.$switchresult);
1.6       raeburn   454: }
1.1       raeburn   455: 
1.19      raeburn   456: sub create_newuser {
1.36    ! albertel  457:     my ($args,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,
        !           458: 	$courseinfo,$called_context) = @_;
1.19      raeburn   459:     my $auth = $args->{'auth'};
                    460:     my $authparam = $args->{'authparam'};
                    461:     my $emailenc = $args->{'emailenc'};
                    462:     my $udom = $args->{'udom'};
                    463:     my $uname = $args->{'uname'};
                    464:     my $pid = $args->{'pid'};
                    465:     my $first = $args->{'first'};
                    466:     my $middle = $args->{'middle'};
                    467:     my $last = $args->{'last'} ;
                    468:     my $gene = $args->{'gene'};
                    469:     my $usec = $args->{'usec'};
                    470:     my $end = $args->{'end'};
                    471:     my $start = $args->{'start'};
                    472:     my $emailaddr = $args->{'emailaddr'};
                    473:     my $cid = $args->{'cid'};
                    474:     my $crs = $args->{'crs'};
                    475:     my $cdom = $args->{'cdom'};
                    476:     my $context = $args->{'context'};
                    477:     my $linefeed = $args->{'linefeed'};
                    478:     my $role = $args->{'role'};
                    479:     my $create_passwd = 0;
                    480:     my $authchk = '';
                    481:     my $outcome;
                    482:     unless ($authparam eq '') { $authchk = 'ok'; };
                    483: # If no account exists and passwords should be generated
                    484:     if ($auth eq "internal") {
                    485:         if ($authparam eq '') {
                    486:             $authparam = &create_password();
                    487:             if ($authparam eq '') {
                    488:                 $authchk = '';
                    489:             } else {
                    490:                 $create_passwd = 1;
                    491:                 $authchk = 'ok';
                    492:             }
                    493:         }
                    494:     } elsif ($auth eq "localauth") {
1.34      raeburn   495:         ($authparam,$create_passwd,$authchk) = &Apache::lonnet::auto_create_password($crs,$cdom,$authparam,$udom);
1.19      raeburn   496:     } elsif ($auth =~ m/^krb/) {
                    497:         if ($authparam eq '') {
1.33      raeburn   498:             $$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   499:             $authchk = 'invalid';
                    500:         }
                    501:     } else {
                    502:         $authchk = 'invalid';
1.33      raeburn   503:         $$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   504:     }
1.19      raeburn   505:     if ($authchk eq 'ok') {
                    506: # Now create user.
                    507:         my $type = 'auto';
                    508:         my $userurl = '/'.$cdom.'/'.$crs;
                    509:         if ($usec ne '') {
                    510:             $userurl .= '/'.$usec;
                    511:         }
                    512:         if ($context eq 'createowner' || $context eq 'createcourse') {
                    513:             my $result = &Apache::lonnet::modifyuser($udom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,'1',undef,$emailaddr);
                    514:             if ($result eq 'ok' && $context eq 'createcourse') {
1.36    ! albertel  515:                 $outcome = &Apache::loncommon::commit_standardrole($udom,$uname,$userurl,$role,$start,$end,$cdom,$crs,$usec,$called_context);
1.19      raeburn   516:                 unless ($outcome =~ /^Error:/) {
                    517:                     $outcome = 'ok';
                    518:                 }
                    519:             } else {
                    520:                 $outcome = $result;
                    521:             }
                    522:         } else {
                    523:             $outcome=&Apache::lonnet::modifystudent($udom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto','',$cid);
                    524:         }
                    525:         if ($outcome eq 'ok') {
                    526:             my $access = &showaccess($end,$start);
1.33      raeburn   527:             my $showsec = $usec;
                    528:             if ($usec eq '') {
                    529:                 $showsec = &mt('none');
                    530:             }
                    531:             $$addresult .= "$first $last ($pid) - $uname ".&mt("enrolled in section/group: '[_1]'.",$showsec).$access.$linefeed;
1.19      raeburn   532:             unless ($context eq 'createowner' || $context eq 'createcourse') {
                    533:                 $$enrollcount ++;
                    534:             }
1.36    ! albertel  535:             if ($called_context eq 'automated') {
1.33      raeburn   536:                 $$logmsg .= &mt('New [_1] user [_2] added successfully.',$udom,$uname);
1.19      raeburn   537:             }
                    538:             unless ($emailenc eq '' || $context eq 'createowner' || $context eq 'createcourse') {
                    539:                 my %emailHash;
                    540:                 $emailHash{'critnotification'}  = $emailenc;
                    541:                 $emailHash{'notification'} = $emailenc;
1.23      raeburn   542:                 $emailHash{'permanentemail'} = $emailenc;
1.19      raeburn   543:                 my $putresult = &Apache::lonnet::put('environment',\%emailHash,$udom,$uname);
                    544:             }
                    545:             if ($create_passwd) {
                    546: # Send e-mail with initial password to new user at $emailaddr.
                    547: # If e-mail address is invalid, send password via message to courseowner i
                    548: # (if automated call) or to user if roster update.
                    549:                 if ($emailaddr eq '') {
1.33      raeburn   550:                     $$newusermsg .= &mt(' username: [_1], password: [_2]',$uname,$authparam).$linefeed."\n";
1.19      raeburn   551:                 } else {
1.33      raeburn   552:                     my $subject = &mt('New LON-CAPA account');
1.19      raeburn   553:                     my $body;
                    554:                     if ($context eq 'createowner') {
1.33      raeburn   555:                         $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 school is: ').'http://'.$ENV{'SERVER_NAME'}."\n\n";
1.19      raeburn   556:                     } elsif ($context eq 'createcourse') {
1.33      raeburn   557:                         $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 school is: '),'http://'.$ENV{'SERVER_NAME'}."\n\n"; 
1.19      raeburn   558:                     } else {
                    559:                         my $access_start = 'immediately';
                    560:                         if ($start > 0) {
                    561:                             $access_start = localtime($start)
                    562:                         }
1.33      raeburn   563:                         $body = &mt('You have been enrolled in the LON-CAPA system at your school, because you are a registered student in a class that is using the LON-CAPA couse management and online homework system.')."\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 school is: ').'http://'.$ENV{'SERVER_NAME'}."\n\n".&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   564:                     }
                    565:                     &Apache::lonmsg::sendemail($emailaddr,$subject,$body);
                    566:                 }
1.36    ! albertel  567:                 if ($called_context eq 'automated') {
1.33      raeburn   568:                     $$logmsg .= &mt(' Initial password - sent to ').$emailaddr.$linefeed;
1.19      raeburn   569:                 }
                    570:             } else {
1.36    ! albertel  571:                 if ($called_context eq 'automated') {
1.19      raeburn   572:                     $$logmsg .= $linefeed;
                    573:                 }
                    574:             }
                    575:         } else {
1.33      raeburn   576:             $$logmsg .= &mt('An error occurred adding new user [_1] - [_2].',$uname,$outcome).$linefeed;
1.19      raeburn   577:         }
1.34      raeburn   578:     } else {
                    579:         $$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   580:     }
                    581:     return $outcome;
                    582: }
                    583: 
1.16      raeburn   584: sub prepare_add {
                    585:     my ($authtype,$autharg,$enddate,$startdate,$stuinfo,$place,$dom,$uname,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc) = @_;
                    586:     $$auth = $$stuinfo[ $$place{'authtype'} ];
                    587:     $$authparam = $$stuinfo[ $$place{'autharg'} ];
                    588:     $$first = $$stuinfo[ $$place{'firstname'} ];
                    589:     $$middle = $$stuinfo[ $$place{'middlename'} ];
                    590:     $$last = $$stuinfo[ $$place{'lastname'} ];
                    591:     $$gene = $$stuinfo[ $$place{'generation'} ];
                    592:     $$usec = $$stuinfo[ $$place{'groupID'} ];
                    593:     $$end = $$stuinfo[ $$place{'enddate'} ];
                    594:     $$start = $$stuinfo[ $$place{'startdate'} ];
                    595:     $$emailaddr = $$stuinfo[ $$place{'email'} ];
                    596:     $$pid = $$stuinfo[ $$place{'studentID'} ];
                    597:                                                                                   
                    598: # remove non alphanumeric values from section
                    599:     $$usec =~ s/\W//g;
                    600:                                                                                   
                    601:     unless ($$emailaddr =~/^[^\@]+\@[^\@]+$/) { $$emailaddr =''; }
                    602:     $$emailenc = &HTML::Entities::encode($$emailaddr,'<>&"');
                    603:                                                                                   
                    604: # Use course defaults where entry is absent
                    605:     if ( ($$auth eq '') || (!defined($$auth)) ) {
                    606:         $$auth =  $authtype;
                    607:     }
                    608:     if ( ($$authparam eq '')  || (!defined($$authparam)) )  {
                    609:         $$authparam = $autharg;
                    610:     }
                    611:     if ( ($$end eq '') || (!defined($$end)) )  {
                    612:         $$end = $enddate;
                    613:     }
                    614:     if ( ($$start eq '')  || (!defined($$start)) )  {
                    615:         $$start = $startdate;
                    616:     }
                    617: # Clean up whitespace
                    618:     foreach ($dom,$uname,$pid,$first,$middle,$last,$gene,$usec) {
                    619:         $$_ =~ s/(\s+$|^\s+)//g;
                    620:     }
                    621:     return;
                    622: }
                    623: 
                    624: sub execute_add {
                    625:     my ($context,$caller,$uname,$dom,$auth,$authparam,$first,$middle,$last,$gene,$pid,$usec,$end,$start,$emailenc,$cid,$addresult,$enrollcount,$linefeed,$logmsg) = @_;
                    626: # Get the user's information and authentication
1.23      raeburn   627:     my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification','permanentemail'],$dom,$uname);
1.16      raeburn   628:     my ($tmp) = keys(%userenv);
                    629:     if ($tmp =~ /^(con_lost|error)/i) {
                    630:         %userenv = ();
                    631:     }
                    632: # Get the user's e-mail address
                    633:     if ($userenv{critnotification} =~ m/%40/) {
                    634:         unless ($emailenc eq $userenv{critnotification}) {
1.33      raeburn   635:             $$logmsg .= &mt('Current critical notification e-mail
                    636: - [_1] for [_2] is different to e-mail address in institutional classlist - [_3].',$userenv{critnotification},$uname,$emailenc).$linefeed;
1.16      raeburn   637:         }
                    638:     }
                    639:     if ($userenv{notification} =~ m/%40/) {
1.23      raeburn   640:         unless ($emailenc eq $userenv{notification}) {
1.33      raeburn   641:             $$logmsg .= &mt('Current standard notification e-mail
                    642: - [_1] for [_2] is different to e-mail address in institutional classlist - [_3]',$userenv{notification},$uname,$emailenc).$linefeed;
1.16      raeburn   643:         }
                    644:     }
1.23      raeburn   645:     if ($userenv{permanentemail} =~ m/%40/) {
                    646:         unless ($emailenc eq $userenv{permanentemail}) {
1.33      raeburn   647:             $$logmsg .= &mt('Current permanent e-mail
                    648: - [_1] for [_2] is different to e-mail address in institutional classlist - [_3]',$userenv{permanentemail},$uname,$emailenc).$linefeed;
1.23      raeburn   649:         }
                    650:     }
1.16      raeburn   651:     my $krbdefdom = '';
                    652:     my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
                    653:     if ($currentauth=~/^(krb[45]):(.*)/) {
                    654:         $currentauth = $1;
                    655:         $krbdefdom = $2;
                    656:     } elsif ($currentauth=~ /^(unix|internal|localauth):/) {
                    657:         $currentauth = $1;
                    658:     } else {
1.33      raeburn   659:         $$logmsg .= &mt('Invalid authentication method [_1] for [_2].',$currentauth,$uname).$linefeed;
1.16      raeburn   660:     }
                    661: # Report if authentication methods are different.
                    662:     if ($currentauth ne $auth) {
1.33      raeburn   663:         $$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   664:     } elsif ($auth =~ m/^krb/) {
                    665:         if ($krbdefdom ne $authparam) {
1.33      raeburn   666:             $$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   667:         }
                    668:     }
                    669:                                                                                   
                    670: # Check user data
                    671:     if ($first  ne $userenv{'firstname'}  ||
                    672:         $middle ne $userenv{'middlename'} ||
                    673:         $last   ne $userenv{'lastname'}   ||
                    674:         $gene   ne $userenv{'generation'} ||
1.23      raeburn   675:         $pid    ne $userenv{'id'} ||
                    676:         $emailenc ne $userenv{'permanentemail'} ) {
1.16      raeburn   677: # Make the change(s)
                    678:         my %changeHash;
                    679:         $changeHash{'firstname'}  = $first;
                    680:         $changeHash{'middlename'} = $middle;
                    681:         $changeHash{'lastname'}   = $last;
                    682:         $changeHash{'generation'} = $gene;
                    683:         $changeHash{'id'} = $pid;
1.23      raeburn   684:         $changeHash{'permanentemail'} = $emailenc;
1.16      raeburn   685:         my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
                    686:         if ($putresult eq 'ok') {
1.33      raeburn   687:             $$logmsg .= &mt('User information updated for user: [_1]$uname prior to enrollment.',$uname).$linefeed;
1.16      raeburn   688:         } else {
1.33      raeburn   689:             $$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   690:         }
                    691:     }
                    692:                                                                                   
                    693: # Assign the role of student in the course.
                    694:     my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto','',$cid);
                    695:     if ($classlist_reply eq 'ok') {
                    696:         my $access = &showaccess($end,$start);
1.33      raeburn   697:         my $showsec = $usec;
                    698:         if ($usec eq '') {
                    699:             $showsec = &mt('none');
                    700:         }
1.16      raeburn   701:         if ($caller eq 'switchtype') {
1.33      raeburn   702:             $$logmsg .= &mt("Existing user [_1] detected in institutional classlist - switched from 'manual' to 'auto' enrollment in section/group [_2].",$uname,$showsec).$access.$linefeed;
1.16      raeburn   703:         } elsif ($caller eq 'newstudent') {
                    704:             $$enrollcount ++;
1.33      raeburn   705:             $$addresult .= "$first $last ($pid) - $uname ".&mt("enrolled in section/group '[_1]'.",$showsec).$access.$linefeed;
1.16      raeburn   706:         }
                    707:         if ($context eq 'automated') {
1.33      raeburn   708:             $$logmsg .= &mt('Existing [_1] user [_2] enrolled successfully.',$dom,$uname).$linefeed;
1.16      raeburn   709:         }
                    710:     } else {
1.33      raeburn   711:            $$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   712:     }
                    713:     return;
                    714: }
                    715: 
                    716: sub datechange_check {
                    717:     my ($oldstart,$oldend,$startdate,$enddate) = @_;
                    718:     my $datechange = 0;
                    719:     unless ($oldstart eq $startdate) {
                    720:         $datechange = 1;
                    721:     }
                    722:     if (!$datechange) {
                    723:         if (!$oldend) {
                    724:             if ($enddate) {
                    725:                 $datechange = 1;
                    726:             }
                    727:         } elsif ($oldend ne $enddate) {
                    728:             $datechange = 1;
                    729:         }
                    730:     }
                    731:     return $datechange;
                    732: }
                    733: 
1.15      raeburn   734: sub showaccess {
                    735:     my ($end,$start) = @_;
                    736:     my $showstart;
                    737:     my $showend;
                    738:     if ( (!$start) || ($start <= time) ) {
                    739:         $showstart = 'immediately';
                    740:     } else {
                    741:         $showstart = &Apache::lonlocal::locallocaltime($start);
                    742:     }
                    743:     if (!$end) {
                    744:         $showend = 'no end date';
                    745:     } else {
                    746:         $showend = &Apache::lonlocal::locallocaltime($end);
                    747:     }
1.33      raeburn   748:     my $access_msg = ' '.&mt('Access starts: [_1], ends: [_2].',$showstart,$showend);
1.15      raeburn   749:     return $access_msg;
                    750: }
                    751: 
1.1       raeburn   752: sub parse_classlist {
1.6       raeburn   753:     my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
1.5       raeburn   754:     my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
1.6       raeburn   755:     my $uname = '';
                    756:     my @state;
1.8       raeburn   757:     my @items = ('autharg','authtype','email','firstname','generation','lastname','middlename','studentID');
1.6       raeburn   758:     my $p = HTML::Parser->new
                    759:     (
                    760:         xml_mode => 1,
                    761:         start_h =>
                    762:             [sub {
                    763:                  my ($tagname, $attr) = @_;
                    764:                  push @state, $tagname;
                    765:                  if ("@state" eq "students student") {
                    766:                      $uname = $attr->{username};
                    767:                  }
                    768:             }, "tagname, attr"],
                    769:          text_h =>
                    770:              [sub {
                    771:                  my ($text) = @_;
                    772:                  if ("@state" eq "students student groupID") {
                    773:                      $$studentsref{$uname}[ $$placeref{'groupID'} ] = $groupID;
1.8       raeburn   774:                  } elsif ("@state" eq "students student startdate") {
                    775:                      my $start = $text;
                    776:                      unless ($text eq '') {
                    777:                          $start = &process_date($text);
                    778:                      }
                    779:                      $$studentsref{$uname}[ $$placeref{'startdate'} ] = $start; 
                    780:                  } elsif ("@state" eq "students student enddate") {
                    781:                      my $end = $text;
                    782:                      unless ($text eq '') {
                    783:                          $end = &process_date($text);
                    784:                      }
                    785:                      $$studentsref{$uname}[ $$placeref{'enddate'} ] = $end;
1.6       raeburn   786:                  } else {
                    787:                      foreach my $item (@items) {
                    788:                          if ("@state" eq "students student $item") {
                    789:                              $$studentsref{$uname}[ $$placeref{$item} ] = $text;
                    790:                          }
                    791:                      }
                    792:                  }
                    793:                }, "dtext"],
                    794:          end_h =>
                    795:                [sub {
                    796:                    my ($tagname) = @_;
                    797:                    pop @state;
                    798:                 }, "tagname"],
                    799:     );
                    800:                                                                                                              
                    801:     $p->parse_file($xmlfile);
                    802:     $p->eof;
1.8       raeburn   803:     if (-e "$xmlfile") {
                    804:         unlink $xmlfile;
                    805:     }
1.3       raeburn   806:     return;
1.1       raeburn   807: }
                    808: 
1.8       raeburn   809: sub process_date {
                    810:     my $timestr = shift;
                    811:     my $timestamp = '';
                    812:     if ($timestr =~ m/^\d{4}:\d{2}:\d{2}/) {
                    813:         my @entries = split/:/,$timestr;
                    814:         for (my $j=0; $j<@entries; $j++) {
                    815:             if ( length($entries[$j]) > 1 ) {
                    816:                 $entries[$j] =~ s/^0//;
                    817:             }
                    818:         }
                    819:         $entries[1] = $entries[1] - 1;
                    820:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    821:     }
                    822:     return $timestamp;
                    823: }
                    824: 
1.1       raeburn   825: sub create_password {
1.8       raeburn   826:     my $passwd = '';
1.11      raeburn   827:     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   828:     for (my $i=0; $i<8; $i++) {
                    829:         my $lettnum = int (rand 2);
                    830:         my $item = '';
                    831:         if ($lettnum) {
                    832:             $item = $letts[int( rand(26) )];
                    833:             my $uppercase = int(rand 2);
                    834:             if ($uppercase) {
                    835:                 $item =~ tr/a-z/A-Z/;
                    836:             }
                    837:         } else {
                    838:             $item = int( rand(10) );
                    839:         } 
                    840:         $passwd .= $item;
                    841:     }
                    842:     return ($passwd);
1.9       raeburn   843: }
                    844: 
1.19      raeburn   845: sub get_courseinfo {
                    846:     my ($dom,$crs,$courseinfo) = @_;
                    847:     my $owner;
                    848:     if (defined($dom) && defined($crs)) {
1.31      raeburn   849:         my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.showphoto','description'],$dom,$crs);
1.19      raeburn   850:         if ( defined($settings{'internal.coursecode'}) ) {
                    851:             $$courseinfo{'inst_code'} = $settings{'internal.coursecode'};
                    852:         }
                    853:         if ( defined($settings{'description'}) ) {
                    854:             $$courseinfo{'description'} = $settings{'description'};
                    855:         }
1.31      raeburn   856:         if ( defined($settings{'internal.showphoto'}) ) {
                    857:             $$courseinfo{'showphoto'} = $settings{'internal.showphoto'};
1.28      raeburn   858:         }
1.19      raeburn   859:     }
                    860:     return;
                    861: }
                    862: 
1.1       raeburn   863: sub CL_autharg { return 0; }
                    864: sub CL_authtype { return 1;}
                    865: sub CL_email { return 2;}
                    866: sub CL_enddate { return 3;}
                    867: sub CL_firstname { return 4;}
                    868: sub CL_generation { return 5;}
                    869: sub CL_groupID { return 6;}
                    870: sub CL_lastname { return 7;}
                    871: sub CL_middlename { return 8;}
                    872: sub CL_startdate { return 9; }
                    873: sub CL_studentID { return 10; }
                    874: 
1.28      raeburn   875: sub photo_response_types {
1.29      albertel  876:     my %lt = &Apache::lonlocal::texthash(
1.28      raeburn   877:                       'same' => 'remained unchanged',
                    878:                       'update' => 'were updated',
                    879:                       'new' => 'were added',
                    880:                       'missing' => 'were missing',
                    881:                       'error' => 'were not imported because an error occurred',
                    882:                       'nouser' => 'were for users without accounts',
                    883:                       'noid' => 'were for users without student IDs',
1.29      albertel  884: 					 );
1.28      raeburn   885:     return %lt;
                    886: }
                    887: 
                    888: 
1.1       raeburn   889: 1;

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