File:  [LON-CAPA] / loncom / enrollment / Enrollment.pm
Revision 1.3: download - view: text, annotated - select for diffs
Fri Dec 5 18:56:27 2003 UTC (20 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Fixed bugs caught by use strict.  Modified authentication check.  Replaced call to local version of modify_student_enrollment with call to lonnet::modify_student_enrollment.

    1: package LONCAPA::Enrollment;
    2: 
    3: use Apache::loncoursedata;
    4: use Apache::lonnet;
    5: use HTML::Entities;
    6: use XML::Simple;
    7: use LONCAPA::Configuration;
    8: 
    9: use strict;
   10: 
   11: sub update_LC {
   12:     my ($dom,$crs,$adds,$drops,$startdate,$enddate,$authtype,$autharg,$classesref,$groupref,$logmsg,$context) = @_; 
   13: # Get current LON-CAPA student enrollment for this class
   14:     my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
   15:     my $cid = $dom."_".$crs;
   16:     my $roster = &Apache::loncoursedata::get_classlist($cid,$dom,$crs);
   17:     my $cend = &Apache::loncoursedata::CL_END;
   18:     my $cstart = &Apache::loncoursedata::CL_START; 
   19:     my $stuid=&Apache::loncoursedata::CL_ID;
   20:     my $sec=&Apache::loncoursedata::CL_SECTION;
   21:     my $status=&Apache::loncoursedata::CL_STATUS;
   22:     my $type=&Apache::loncoursedata::CL_TYPE;
   23:     my @localstudents = ();
   24:     my $currlist;
   25:     foreach my $uname (keys %{$roster} ) {
   26:         if ($uname =~ m/^(.+):$dom$/) {
   27:             if ($$roster{$uname}[$status] eq "Active") {
   28:                 push @localstudents, $1;
   29:                 @{$$currlist{$1}} = @{$$roster{$uname}};
   30:             }
   31:         }
   32:     }
   33:     my $linefeed = '';
   34:     my $addresult = '';
   35:     my $dropresult = '';
   36:     if ($context eq "updatenow") {
   37:         $linefeed = "</li>\n<li>"; 
   38:     } elsif ($context eq "automated") {
   39:         $linefeed = "\n";
   40:     }
   41:     my $enrollcount = 0;
   42:     my $dropcount = 0;
   43: 
   44: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class 
   45:     my @LCids = ();
   46:     my %unameFromLCid = ();
   47:     foreach my $uname (sort keys %{$currlist}) {
   48:         my $stuID = $$currlist{$uname}[$stuid];
   49:         if (!grep/^$stuID$/,@LCids) {
   50:             push @LCids, $stuID;
   51:             @{$unameFromLCid{$stuID}} = ();
   52:         }
   53:         push @{$unameFromLCid{$stuID}},$uname;
   54:     }
   55:  
   56: # Get latest institutional enrollment for this class.
   57:     my %allenrolled = ();
   58:     my @reg_students = ();
   59:     my %place = ();
   60:     $place{'autharg'} = &CL_autharg();
   61:     $place{'authtype'} = &CL_authtype();
   62:     $place{'email'} = &CL_email();
   63:     $place{'enddate'} = &CL_enddate();
   64:     $place{'firstname'} = &CL_firstname();
   65:     $place{'generation'} = &CL_generation();
   66:     $place{'groupID'} = &CL_groupID();
   67:     $place{'lastname'} = &CL_lastname();
   68:     $place{'middlename'} = &CL_middlename();
   69:     $place{'startdate'} = &CL_startdate();
   70:     $place{'studentID'} = &CL_studentID();
   71:     my %ucount = ();
   72:     my %enrollinfo = ();
   73:     foreach my $class (@{$classesref}) {
   74:         my %enrolled = ();
   75:         &parse_classlist($$configvars{'lonDaemons'},$dom,$crs,$class,\%place,$$groupref{$class},\%enrolled);
   76:         foreach my $uname (sort keys %enrolled ) {
   77:             if (!grep/^$uname$/,@reg_students) {
   78:                 push @reg_students,$uname;
   79:                 $ucount{$uname} = 0;
   80:                 @{$allenrolled{$uname}} = ();
   81:             }
   82:             @{$allenrolled{$uname}[$ucount{$uname}]} = @{$enrolled{$uname}};
   83:             $ucount{$uname} ++;
   84:         }
   85:     }
   86: 
   87: # Check for multiple sections for a single student 
   88:     my @okusers = ();
   89:     foreach my $uname (@reg_students)  {
   90:         if (@{$allenrolled{$uname}} > 1) {
   91:             my @sections = ();
   92:             my $saved;
   93:             for (my $i=0; $i<@{$allenrolled{$uname}}; $i++) {
   94:                 my @stuinfo = @{$allenrolled{$uname}[$i]};
   95:                 my $secnum = $stuinfo[ $place{'groupID'} ];
   96:                 unless ($secnum eq '') {
   97:                     unless (grep/^$secnum$/,@sections) {
   98:                         $saved = $i; 
   99:                         push @sections,$secnum;
  100:                     }
  101:                 }
  102:             }
  103:             if (@sections == 0) {
  104:                 @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
  105:                 push @okusers, $uname;
  106:             }
  107:             elsif (@sections == 1) {
  108:                 @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[$saved]};
  109:                 push @okusers, $uname;
  110:             }
  111:             elsif (@sections > 1) {
  112:                 $logmsg =  "$uname appears in classlists for multiple sections of $crs -";
  113:                 foreach (@sections) {
  114:                     $logmsg .= " $_,";
  115:                 }
  116:                 chop($logmsg);
  117:                 $logmsg .= " No automated enrollment action taken for this student.\n";
  118:             }
  119:         } else {
  120:             @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
  121:             push @okusers, $uname;
  122:         }
  123:     }
  124: # Get mapping of student IDs to usernames for users in institutional data for this class  
  125:     my @allINids = ();
  126:     my %unameFromINid = ();
  127:     foreach my $uname (@okusers) {
  128:         $enrollinfo{$uname}[ $place{'studentID'} ] =~ tr/A-Z/a-z/;
  129:         my $stuID = $enrollinfo{$uname}[ $place{'studentID'} ];
  130:         if (grep/^$stuID$/,@allINids)  {
  131:             push @{$unameFromINid{$stuID}},$uname;
  132:         } else {
  133:             push @allINids, $stuID;
  134:             @{$unameFromINid{$stuID}} = $uname; 
  135:         }
  136:     }
  137: # Compare IDs with existing LON-CAPA enrollment for this class
  138:     foreach my $uname (@okusers) {
  139:         my %uidhash=&Apache::lonnet::idrget($dom,$uname);
  140:         my @stuinfo = @{$enrollinfo{$uname}};
  141:         if (grep/^$uname$/,@localstudents) {
  142: # Check for studentID changes
  143:             if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) )  {
  144:                 unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
  145:                     $logmsg .= "Change in ID for $uname in class: $crs. StudentID in LON-CAPA system is $uidhash{$uname}, StudentID in institutional data is $stuinfo[ $place{studentID} ]\n"; 
  146:                 }
  147:             }
  148: 
  149: # Check for section changes
  150:             unless ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
  151:                 $logmsg .= "Found a section difference for $uname - ".$$currlist{$uname}[$sec] ."versus ".$stuinfo[ $place{groupID} ]." in class $crs\n";
  152:                 if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
  153:                     my $modify_section_result = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto',$cid);
  154:                     if ($modify_section_result !~ /^ok/) {
  155:                         $logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $modify_section_result\n";
  156:                     }
  157: 
  158: # Assign the role of student in the new section
  159:                     my $uurl='/'.$cid;
  160:                     $uurl=~s/\_/\//g;
  161:                     if ($stuinfo[ $place{groupID} ]) {
  162:                         $uurl.='/'.$stuinfo[ $place{groupID} ];
  163:                     }
  164:                     my $newend = $stuinfo[ $place{enddate} ];
  165:                     my $newstart = $stuinfo[ $place{startdate} ];
  166:                     if ($newend eq '') {
  167:                         $newend = $enddate;
  168:                     }
  169:                     if ($newstart eq '') {
  170:                         $newstart = $startdate;
  171:                     }
  172:                     &Apache::lonnet::assignrole($dom,$uname,$uurl,"st",$newend,$newstart); 
  173:                 }
  174:             }
  175:         }
  176:         elsif ($uname ne '') {
  177: # Check for changed usernames by checking studentIDs
  178:             if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
  179:                 if (grep/^$$currlist{$uname}[ $place{'studentID'} ]$/,@allINids) {
  180:                     foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } }  ) {
  181:                         if (grep/^$match$/,@okusers) {
  182:                             $logmsg .= "A possible change in username has been detected for a student enrolled in $crs. 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\n";
  183:                         }
  184:                     }
  185:                 }
  186:             } elsif ($adds == 1) {
  187: # Add student to LON-CAPA classlist
  188:                 my $auth = $stuinfo[ $place{'authtype'} ];
  189:                 my $authparam = $stuinfo[ $place{'autharg'} ];
  190:                 my $first = $stuinfo[ $place{'firstname'} ];
  191:                 my $middle = $stuinfo[ $place{'middlename'} ];
  192:                 my $last = $stuinfo[ $place{'lastname'} ];
  193:                 my $gene = $stuinfo[ $place{'generation'} ];
  194:                 my $usec = $stuinfo[ $place{'groupID'} ];
  195:                 my $end = $stuinfo[ $place{'enddate'} ];
  196:                 my $start = $stuinfo[ $place{'startdate'} ];
  197:                 my $emailaddr = $stuinfo[ $place{'email'} ];
  198:                 my $pid = $stuinfo[ $place{'studentID'} ];
  199: 
  200: # remove non alphanumeric values from section
  201:                 $usec =~ s/\W//g;
  202: 
  203:                 unless ($emailaddr =~/^[^\@]+\@[^\@]+$/) { $emailaddr =''; }
  204:                 my $emailenc = &HTML::Entities::encode($emailaddr); 
  205: 
  206: # Use course defaults where entry is absent
  207:                 if ($auth eq '') {
  208:                     $auth =  $authtype;
  209:                 }
  210:                 if ($authparam eq '') {
  211:                     $authparam = $autharg;
  212:                 }
  213:                 if ($auth =~ m/^krb/) {
  214:                     $auth .= ":".$authparam;
  215:                 }
  216:                 if ($end eq '') {
  217:                     $end = $enddate;
  218:                 }
  219:                 if ($start eq '') {
  220:                     $start = $startdate;
  221:                 }
  222: # Clean up whitespace
  223:                 foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$usec) {
  224:                     $$_ =~ s/(\s+$|^\s+)//g;
  225:                 }
  226: 
  227: # Check for existing account in this LON-CAPA domain for this username
  228:                 my $uhome=&Apache::lonnet::homeserver($uname,$dom);
  229:                 if ($uhome eq 'no_host') { # User does not exist
  230:                     my $create_passwd = 0;
  231:                     my $authchk = '';
  232:                     unless ($authparam eq '') { $authchk = 'ok'; };
  233: # If no account exists and passwords should be generated
  234:                     if ($authtype eq "int") {
  235:                         if ($authparam eq '') {
  236:                             ($authparam,$create_passwd,$authchk) = &create_password();
  237:                         }
  238:                     } elsif ($authtype eq "local") {
  239:                         if ($authparam eq '') {
  240:                             ($authparam,$create_passwd,$authchk) = &create_password();
  241:                         }
  242:                     } elsif ($authtype =~ m/^krb/) {
  243:                         if ($authparam eq '') {
  244:                             $logmsg .= "No Kerberos domain available for the new user - $uname in course $crs - no enrollment occurred.\n";
  245:                             $authchk = 'invalid';
  246:                         }
  247:                     } else {
  248:                         $authchk = 'invalid';
  249:                         $logmsg .= "Invalid authentication type for new user - $uname in course $crs - no enrollment occurred.\n";
  250:                     }  
  251:                     unless ($authchk eq 'ok') { 
  252: # Now create user.
  253:                         my $reply=&Apache::lonnet::modifystudent($dom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto',$cid);
  254:                         if ($reply eq 'ok') {
  255:                             $enrollcount ++;
  256:                             $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
  257:                             $logmsg .= "New user $uname added successfully. ";
  258:                             unless ($emailenc eq '') {
  259:                                 my %emailHash;
  260:                                 $emailHash{'critnotification'}  = $emailenc;
  261:                                 $emailHash{'notification'} = $emailenc;
  262:                                 my $putresult = &Apache::lonnet::put('environment',\%emailHash,$dom,$uname);
  263:                             }
  264:                             if ($create_passwd) {
  265: # Send e-mail with inital password to new user at $emailaddr
  266:                                 $logmsg .= "Initial password -  - sent to $emailaddr\n";
  267:                             } else {
  268:                                 $logmsg .= "\n";
  269:                             }
  270:                         } else {
  271:                             $logmsg .= "An error occurred adding new user $uname - $reply\n";
  272:                         }
  273:                     }
  274:                 } else {
  275: # Get the user's information and authentication
  276:                     my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification'],$dom,$uname);
  277:                     my ($tmp) = keys(%userenv);
  278:                     if ($tmp =~ /^(con_lost|error)/i) {
  279:                         %userenv = ();
  280:                     }
  281: # Get the user's e-mail address
  282:                     if ($userenv{critnotification} =~ m/%40/) {
  283:                         unless ($emailenc eq $userenv{critnotification}) {
  284:                             $logmsg .= "Current critical notification e-mail - ".$userenv{critnotification}." for $uname is different to e-mail address in Institutional classlist - $emailenc\n";
  285:                         }
  286:                     }
  287:                     if ($userenv{notification} =~ m/%40/) {
  288:                         unless ($emailenc eq $userenv{critnotification}) {
  289:                             $logmsg .= "Current standard notification e-mail - ".$userenv{notification}." for $uname is different to e-mail address in Institutional classlist - $emailenc\n";
  290:                         }
  291:                     }                            
  292:                     my $krbdefdom = '';
  293:                     my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
  294:                     if ($currentauth=~/^krb(4|5):/) {
  295:                         $currentauth=~/^krb(4|5):(.*)/;
  296:                         $krbdefdom=$1;
  297:                     }
  298:                     if ($currentauth=~/^krb(4|5):/ || 
  299:                         $currentauth=~/^unix:/ ||
  300:                         $currentauth=~/^internal:/ ||
  301:                         $currentauth=~/^localauth:/) {
  302:                                
  303:                     } else {
  304:                         $logmsg .= "Invalid authentication method $currentauth for $uname.\n";  
  305:                     }
  306: # Report if authentication methods are different.
  307:                     if ($currentauth ne $auth ) {
  308:                          $logmsg .= "Authentication mismatch for $uname - $currentauth in system, $auth for class $crs\n";
  309:                     }
  310: # Check user data
  311:                     if ($first  ne $userenv{'firstname'}  ||
  312:                         $middle ne $userenv{'middlename'} ||
  313:                         $last   ne $userenv{'lastname'}   ||
  314:                         $gene   ne $userenv{'generation'} ||
  315:                         $pid    ne $userenv{'id'} ) {         
  316: # Make the change(s)
  317:                         my %changeHash;
  318:                         $changeHash{'firstname'}  = $first;
  319:                         $changeHash{'middlename'} = $middle;
  320:                         $changeHash{'lastname'}   = $last;
  321:                         $changeHash{'generation'} = $gene;
  322:                         $changeHash{'id'} = $pid;
  323:                         my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
  324:                         if ($putresult eq 'ok') {
  325:                             $logmsg .= "User information updated for user: $uname prior to enrollment in $crs\n";
  326:                         } else {
  327:                             $logmsg .= "There was a problem modifying user data for existing user - $uname, enrollment will still be attempted for user in $crs.\n";
  328:                         }
  329:                     }
  330:  
  331: # Assign the role of student in the course.
  332:                     my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto',$cid);
  333:                     if ($classlist_reply eq 'ok') {
  334:                         $enrollcount ++;
  335:                         $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
  336:                         $logmsg .= "Existing user $uname enrolled successfully in $crs\n";
  337: 
  338:                     } else {
  339:                         $logmsg .= "There was a problem updating the classlist db file for user $uname to show the new enrollment, so no enrollment occurred for this user in $crs\n";
  340:                     }
  341:                 }
  342:             }
  343:         }
  344:     }
  345: # Do drops
  346:     if ( ($drops == 1) && (@reg_students > 0) ) {
  347:         foreach my $uname (@localstudents) {
  348:             if ($$currlist{$uname}[$type] eq "auto") {
  349:                 my @saved = ();
  350:                 if (!grep/^$uname$/,@reg_students) {
  351: # Check for changed usernames by checking studentIDs
  352:                     if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
  353:                         foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
  354:                             $logmsg .= "A possible change in username has been detected for a student enrolled in $crs. 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\n";
  355:                             push @saved,$uname;
  356:                         }
  357:                     } elsif (@saved == 0) {
  358:                         my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,undef,$cid);
  359:                         if ($drop_reply !~ /^ok/) {
  360:                             $logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $drop_reply\n";
  361:                         } else {
  362:                             $dropcount ++;
  363:                             my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
  364:                             $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname." dropped from section/group ".$$currlist{$uname}[$sec].$linefeed; 
  365:                         }
  366:                     }
  367:                 }
  368:             }
  369:         }
  370:     }
  371:     if ($enrollcount > 0) {
  372:         if ($context eq "updatenow") {
  373:             $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:<br/><ul><li>".$addresult."</li></ul><br/><br/>";
  374:         } else {
  375:             $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:\n\n".$addresult."\n\n";    
  376:         }      
  377:     }
  378:     if ($dropcount > 0) {
  379:         if ($context eq "updatenow") {
  380:             $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:<br/><ul><li>".$dropresult."</li></ul><br/><br/>";
  381:         } else {
  382:             $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:\n\n".$dropresult."\n\n";
  383:         }
  384:     }
  385:     if ( ($adds) && ($enrollcount == 0) ) {
  386:         $addresult = "There were no new students to add to the course.";
  387:         if ($context eq "updatenow") {
  388:             $addresult .="<br/><br/>";
  389:         } else {
  390:             $addresult .="\n";
  391:         }
  392:     }
  393:     if ( ($drops) && ($dropcount == 0) ) {
  394:         $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.";
  395:         if ($context eq "updatenow") {
  396:             $dropresult .="<br/>";
  397:         } else {
  398:             $dropresult .="\n";
  399:         }
  400:     }
  401:     print STDERR $logmsg;
  402:     return $addresult.$dropresult; 
  403: } 
  404: 
  405: sub parse_classlist {
  406:     my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;            
  407:     my $configvars = &LONCAPA::Configuration::read_conf();
  408:     my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_classlist.xml";
  409:     my $enrolled = XMLin( $xmlfile, KeyAttr => ['username'] );
  410:     foreach my $uname ( sort keys %{$$enrolled{'student'}} ) {
  411:         @{ $$studentsref{$uname} } = ();
  412:         foreach my $key (sort keys %{$$enrolled{'student'}{$uname}} ) {
  413:             my $value = $$enrolled{'student'}{$uname}{$key};
  414:             if (ref($value)) {
  415:                 $$studentsref{$uname}[ $$placeref{$key} ] = '';
  416:             } else {
  417:                 if ($key eq 'groupID') {
  418:                     $$studentsref{$uname}[ $$placeref{$key} ] = $groupID;
  419:                 } else {
  420:                     $$studentsref{$uname}[ $$placeref{$key} ] = $value;
  421:                 }
  422:             }
  423:         }
  424:     }
  425: #    if (-e "$xmlfile") {
  426: #        unlink $xmlfile;
  427: #    }
  428:     return;
  429: }
  430: 
  431: sub create_password {
  432:     my ($authparam,$create_passwd,$authreply);
  433:     return ($authparam,$create_passwd,$authreply);
  434: }
  435: 
  436: sub CL_autharg { return 0; }
  437: sub CL_authtype { return 1;}
  438: sub CL_email { return 2;}
  439: sub CL_enddate { return 3;}
  440: sub CL_firstname { return 4;}
  441: sub CL_generation { return 5;}
  442: sub CL_groupID { return 6;}
  443: sub CL_lastname { return 7;}
  444: sub CL_middlename { return 8;}
  445: sub CL_startdate { return 9; }
  446: sub CL_studentID { return 10; }
  447: 
  448: 1;

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