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

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

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