File:  [LON-CAPA] / loncom / enrollment / Enrollment.pm
Revision 1.16: download - view: text, annotated - select for diffs
Tue Jun 29 04:30:00 2004 UTC (19 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
AUtomated Enrollment Manager now provides automatic switching from 'manual' to 'auto' enrollment type when a manually enrolled student is detected in the institution's classlist.  Auto switching can be prevented on a per-student basis by 'locking' the type to 'manual'. Also changes to Enrollment.pm to make it more modular.

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

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