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

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

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