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

1.7       albertel    1: # Automated Enrollment manager
1.8     ! raeburn     2: # $Id: Enrollment.pm,v 1.7 2003/12/09 20:06:37 albertel 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';
        !            35: use localenroll;
1.1       raeburn    36: 
                     37: use strict;
                     38: 
                     39: sub update_LC {
1.8     ! raeburn    40:     my ($dom,$crs,$adds,$drops,$startdate,$enddate,$authtype,$autharg,$classesref,$groupref,$logmsg,$newusermsg,$context) = @_; 
1.1       raeburn    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 @localstudents = ();
                     52:     my $currlist;
                     53:     foreach my $uname (keys %{$roster} ) {
                     54:         if ($uname =~ m/^(.+):$dom$/) {
                     55:             if ($$roster{$uname}[$status] eq "Active") {
                     56:                 push @localstudents, $1;
                     57:                 @{$$currlist{$1}} = @{$$roster{$uname}};
                     58:             }
                     59:         }
                     60:     }
                     61:     my $linefeed = '';
                     62:     my $addresult = '';
                     63:     my $dropresult = '';
                     64:     if ($context eq "updatenow") {
                     65:         $linefeed = "</li>\n<li>"; 
                     66:     } elsif ($context eq "automated") {
                     67:         $linefeed = "\n";
                     68:     }
                     69:     my $enrollcount = 0;
                     70:     my $dropcount = 0;
                     71: 
1.8     ! raeburn    72:     srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand in case initial passwords have to be generated for new users.
        !            73: 
1.1       raeburn    74: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class 
                     75:     my @LCids = ();
                     76:     my %unameFromLCid = ();
                     77:     foreach my $uname (sort keys %{$currlist}) {
                     78:         my $stuID = $$currlist{$uname}[$stuid];
                     79:         if (!grep/^$stuID$/,@LCids) {
                     80:             push @LCids, $stuID;
                     81:             @{$unameFromLCid{$stuID}} = ();
                     82:         }
                     83:         push @{$unameFromLCid{$stuID}},$uname;
                     84:     }
                     85:  
                     86: # Get latest institutional enrollment for this class.
                     87:     my %allenrolled = ();
                     88:     my @reg_students = ();
                     89:     my %place = ();
                     90:     $place{'autharg'} = &CL_autharg();
                     91:     $place{'authtype'} = &CL_authtype();
                     92:     $place{'email'} = &CL_email();
                     93:     $place{'enddate'} = &CL_enddate();
                     94:     $place{'firstname'} = &CL_firstname();
                     95:     $place{'generation'} = &CL_generation();
                     96:     $place{'groupID'} = &CL_groupID();
                     97:     $place{'lastname'} = &CL_lastname();
                     98:     $place{'middlename'} = &CL_middlename();
                     99:     $place{'startdate'} = &CL_startdate();
                    100:     $place{'studentID'} = &CL_studentID();
                    101:     my %ucount = ();
                    102:     my %enrollinfo = ();
                    103:     foreach my $class (@{$classesref}) {
                    104:         my %enrolled = ();
                    105:         &parse_classlist($$configvars{'lonDaemons'},$dom,$crs,$class,\%place,$$groupref{$class},\%enrolled);
                    106:         foreach my $uname (sort keys %enrolled ) {
                    107:             if (!grep/^$uname$/,@reg_students) {
                    108:                 push @reg_students,$uname;
                    109:                 $ucount{$uname} = 0;
                    110:                 @{$allenrolled{$uname}} = ();
                    111:             }
                    112:             @{$allenrolled{$uname}[$ucount{$uname}]} = @{$enrolled{$uname}};
                    113:             $ucount{$uname} ++;
                    114:         }
                    115:     }
                    116: 
                    117: # Check for multiple sections for a single student 
                    118:     my @okusers = ();
                    119:     foreach my $uname (@reg_students)  {
                    120:         if (@{$allenrolled{$uname}} > 1) {
                    121:             my @sections = ();
                    122:             my $saved;
                    123:             for (my $i=0; $i<@{$allenrolled{$uname}}; $i++) {
                    124:                 my @stuinfo = @{$allenrolled{$uname}[$i]};
                    125:                 my $secnum = $stuinfo[ $place{'groupID'} ];
                    126:                 unless ($secnum eq '') {
                    127:                     unless (grep/^$secnum$/,@sections) {
                    128:                         $saved = $i; 
                    129:                         push @sections,$secnum;
                    130:                     }
                    131:                 }
                    132:             }
                    133:             if (@sections == 0) {
                    134:                 @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
                    135:                 push @okusers, $uname;
                    136:             }
                    137:             elsif (@sections == 1) {
                    138:                 @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[$saved]};
                    139:                 push @okusers, $uname;
                    140:             }
                    141:             elsif (@sections > 1) {
1.5       raeburn   142:                 $$logmsg =  "$uname appears in classlists for the more than one section of this course, i.e. in sections: ";
1.1       raeburn   143:                 foreach (@sections) {
1.5       raeburn   144:                     $$logmsg .= " $_,";
1.1       raeburn   145:                 }
1.5       raeburn   146:                 chop($$logmsg);
1.6       raeburn   147:                 $$logmsg .= ". Because of this ambiguity, no enrollment action was taken for this student.".$linefeed;
1.1       raeburn   148:             }
                    149:         } else {
                    150:             @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
                    151:             push @okusers, $uname;
                    152:         }
                    153:     }
                    154: # Get mapping of student IDs to usernames for users in institutional data for this class  
                    155:     my @allINids = ();
1.3       raeburn   156:     my %unameFromINid = ();
1.1       raeburn   157:     foreach my $uname (@okusers) {
                    158:         $enrollinfo{$uname}[ $place{'studentID'} ] =~ tr/A-Z/a-z/;
                    159:         my $stuID = $enrollinfo{$uname}[ $place{'studentID'} ];
                    160:         if (grep/^$stuID$/,@allINids)  {
                    161:             push @{$unameFromINid{$stuID}},$uname;
                    162:         } else {
                    163:             push @allINids, $stuID;
                    164:             @{$unameFromINid{$stuID}} = $uname; 
                    165:         }
                    166:     }
1.5       raeburn   167: # Explicitly allow access to creation/modification of students if called as an automated process.
                    168:     if ($context eq 'automated') {
                    169:         $ENV{'allowed.cst'}='F';
                    170:     }
                    171: 
1.1       raeburn   172: # Compare IDs with existing LON-CAPA enrollment for this class
                    173:     foreach my $uname (@okusers) {
1.5       raeburn   174:         unless ($uname eq '') {
                    175:             my %uidhash=&Apache::lonnet::idrget($dom,$uname);
                    176:             my @stuinfo = @{$enrollinfo{$uname}};
                    177:             if (grep/^$uname$/,@localstudents) {
1.1       raeburn   178: # Check for studentID changes
1.5       raeburn   179:                 if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) )  {
                    180:                     unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
1.6       raeburn   181:                         $$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   182:                     }
1.1       raeburn   183:                 }
                    184: 
                    185: # Check for section changes
1.5       raeburn   186:                 unless ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
                    187:                     if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
                    188:                         my $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);
                    189:                         if ($modify_section_result =~ /^ok/) {
1.6       raeburn   190:                             $$logmsg .= "Section for $uname switched from old section: ".$$currlist{$uname}[$sec] ." to new section: ".$stuinfo[ $place{groupID} ].".".$linefeed;
1.5       raeburn   191:                         } else {
                    192:                             $$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;
                    193:                         }
1.1       raeburn   194:                     }
                    195:                 }
1.5       raeburn   196:             } else {
1.1       raeburn   197: # Check for changed usernames by checking studentIDs
1.5       raeburn   198:                 if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
                    199:                     if (grep/^$$currlist{$uname}[ $place{'studentID'} ]$/,@allINids) {
                    200:                         foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } }  ) {
                    201:                             if (grep/^$match$/,@okusers) {
1.6       raeburn   202:                                 $$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   203:                             }
1.1       raeburn   204:                         }
                    205:                     }
1.5       raeburn   206:                 } elsif ($adds == 1) {
1.1       raeburn   207: # Add student to LON-CAPA classlist
1.5       raeburn   208:                     my $auth = $stuinfo[ $place{'authtype'} ];
                    209:                     my $authparam = $stuinfo[ $place{'autharg'} ];
                    210:                     my $first = $stuinfo[ $place{'firstname'} ];
                    211:                     my $middle = $stuinfo[ $place{'middlename'} ];
                    212:                     my $last = $stuinfo[ $place{'lastname'} ];
                    213:                     my $gene = $stuinfo[ $place{'generation'} ];
                    214:                     my $usec = $stuinfo[ $place{'groupID'} ];
                    215:                     my $end = $stuinfo[ $place{'enddate'} ];
                    216:                     my $start = $stuinfo[ $place{'startdate'} ];
                    217:                     my $emailaddr = $stuinfo[ $place{'email'} ];
                    218:                     my $pid = $stuinfo[ $place{'studentID'} ];
1.1       raeburn   219: 
                    220: # remove non alphanumeric values from section
1.5       raeburn   221:                     $usec =~ s/\W//g;
1.1       raeburn   222: 
1.5       raeburn   223:                     unless ($emailaddr =~/^[^\@]+\@[^\@]+$/) { $emailaddr =''; }
                    224:                     my $emailenc = &HTML::Entities::encode($emailaddr); 
1.1       raeburn   225: 
                    226: # Use course defaults where entry is absent
1.5       raeburn   227:                     if ( ($auth eq '') || (!defined($auth)) ) {
                    228:                         $auth =  $authtype;
                    229:                     }
                    230:                     if ( ($authparam eq '')  || (!defined($authparam)) )  {
                    231:                         $authparam = $autharg;
                    232:                     }
                    233:                     if ($auth =~ m/^krb/) {
                    234:                         $auth .= ":".$authparam;
                    235:                     }
                    236:                     if ( ($end eq '') || (!defined($end)) )  {
                    237:                          $end = $enddate;
                    238:                     }
                    239:                     if ( ($start eq '')  || (!defined($start)) )  {
                    240:                          $start = $startdate;
                    241:                     }
1.1       raeburn   242: # Clean up whitespace
1.5       raeburn   243:                     foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$usec) {
                    244:                          $$_ =~ s/(\s+$|^\s+)//g;
                    245:                     }
1.1       raeburn   246: 
                    247: # Check for existing account in this LON-CAPA domain for this username
1.5       raeburn   248:                     my $uhome=&Apache::lonnet::homeserver($uname,$dom);
                    249:                     if ($uhome eq 'no_host') { # User does not exist
                    250:                         my $create_passwd = 0;
                    251:                         my $authchk = '';
                    252:                         unless ($authparam eq '') { $authchk = 'ok'; };
1.1       raeburn   253: # If no account exists and passwords should be generated
1.5       raeburn   254:                         if ($authtype eq "int") {
                    255:                             if ($authparam eq '') {
1.8     ! raeburn   256:                                 ($authparam) = &create_password();
        !           257:                                 if ($authparam eq '') {
        !           258:                                      $authchk = '';
        !           259:                                 } else {
        !           260:                                     $create_passwd = 1;
        !           261:                                 }        
1.5       raeburn   262:                             }
                    263:                         } elsif ($authtype eq "local") {
1.8     ! raeburn   264:                              ($authparam,$create_passwd,$authchk) = &localenroll::create_password($authparam);
1.5       raeburn   265:                         } elsif ($authtype =~ m/^krb/) {
                    266:                             if ($authparam eq '') {
1.6       raeburn   267:                                 $$logmsg .= "No Kerberos domain was provided for the new user - $uname, so the new student was not enrolled in the course.".$linefeed;
1.5       raeburn   268:                                 $authchk = 'invalid';
                    269:                             }
                    270:                         } else {
1.3       raeburn   271:                             $authchk = 'invalid';
1.6       raeburn   272:                             $$logmsg .= "An invalid authentication type was provided for the new user - $uname, so the student was not enrolled in the course.".$linefeed;
1.3       raeburn   273:                         }
1.5       raeburn   274:                         if ($authchk eq 'ok') { 
1.1       raeburn   275: # Now create user.
1.5       raeburn   276:                             my $reply=&Apache::lonnet::modifystudent($dom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto',$cid);
                    277:                             if ($reply eq 'ok') {
                    278:                                 $enrollcount ++;
                    279:                                 $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
1.6       raeburn   280:                                 if ($context eq 'automated') {
1.8     ! raeburn   281:                                     $$logmsg .= "New $dom user $uname added successfully.";
1.6       raeburn   282:                                 }
1.5       raeburn   283:                                 unless ($emailenc eq '') {
                    284:                                     my %emailHash;
                    285:                                     $emailHash{'critnotification'}  = $emailenc;
                    286:                                     $emailHash{'notification'} = $emailenc;
                    287:                                     my $putresult = &Apache::lonnet::put('environment',\%emailHash,$dom,$uname);
                    288:                                 }
                    289:                                 if ($create_passwd) {
1.8     ! raeburn   290: # Send e-mail with initial password to new user at $emailaddr. 
        !           291: # If e-mail address is invalid, send password via message to courseowner i
        !           292: # (if automated call) or to user if roster update. 
        !           293:                                     if ($emailaddr eq '') {
        !           294:                                         $$newusermsg .= " username: $uname, password: ".$authparam.$linefeed."\n";
        !           295:                                     } else {
        !           296:                                         my $subject = "New LON-CAPA account";
        !           297:                                         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'};
        !           298:                                        &Apache::lonmsg::sendemail($emailaddr,$subject,$body); 
        !           299:                                     }                                 
        !           300:                                     if ($context eq 'automated') {
        !           301:                                         $$logmsg .= " Initial password -  - sent to ".$emailaddr.$linefeed;
        !           302:                                     }
1.5       raeburn   303:                                 } else {
1.8     ! raeburn   304:                                     if ($context eq 'automated') {
        !           305:                                         $$logmsg .= $linefeed;
        !           306:                                     }
1.5       raeburn   307:                                 }
1.3       raeburn   308:                             } else {
1.5       raeburn   309:                                 $$logmsg .= "An error occurred adding new user $uname - ".$reply.$linefeed;
1.3       raeburn   310:                             }
1.1       raeburn   311:                         }
1.5       raeburn   312:                     } else {
1.1       raeburn   313: # Get the user's information and authentication
1.5       raeburn   314:                         my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification'],$dom,$uname);
                    315:                         my ($tmp) = keys(%userenv);
                    316:                         if ($tmp =~ /^(con_lost|error)/i) {
                    317:                             %userenv = ();
                    318:                         }
1.1       raeburn   319: # Get the user's e-mail address
1.5       raeburn   320:                         if ($userenv{critnotification} =~ m/%40/) {
                    321:                             unless ($emailenc eq $userenv{critnotification}) {
                    322:                                 $$logmsg .= "Current critical notification e-mail - ".$userenv{critnotification}." for $uname is different to e-mail address in Institutional classlist - ".$emailenc.$linefeed;
                    323:                             }
                    324:                         }
                    325:                         if ($userenv{notification} =~ m/%40/) {
                    326:                             unless ($emailenc eq $userenv{critnotification}) {
                    327:                                 $$logmsg .= "Current standard notification e-mail - ".$userenv{notification}." for $uname is different to e-mail address in institutional classlist - ".$emailenc.$linefeed;
                    328:                             }
                    329:                         }                            
                    330:                         my $krbdefdom = '';
                    331:                         my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
                    332:                         if ($currentauth=~/^krb(4|5):/) {
                    333:                             $currentauth=~/^krb(4|5):(.*)/;
                    334:                             $krbdefdom=$1;
                    335:                         }
                    336:                         if ($currentauth=~/^krb(4|5):/ || 
                    337:                             $currentauth=~/^unix:/ ||
                    338:                             $currentauth=~/^internal:/ ||
                    339:                             $currentauth=~/^localauth:/) {
1.1       raeburn   340:                                
1.5       raeburn   341:                         } else {
                    342:                             $$logmsg .= "Invalid authentication method $currentauth for $uname.".$linefeed;  
                    343:                         }
1.1       raeburn   344: # Report if authentication methods are different.
1.5       raeburn   345:                         if ($currentauth ne $auth ) {
1.6       raeburn   346:                             $$logmsg .= "Authentication mismatch for $uname - $currentauth in system, $auth based on information in classlist or default for this course.".$linefeed;
1.5       raeburn   347:                         }
1.1       raeburn   348: # Check user data
1.5       raeburn   349:                         if ($first  ne $userenv{'firstname'}  ||
                    350:                             $middle ne $userenv{'middlename'} ||
                    351:                             $last   ne $userenv{'lastname'}   ||
                    352:                             $gene   ne $userenv{'generation'} ||
                    353:                             $pid    ne $userenv{'id'} ) {         
1.1       raeburn   354: # Make the change(s)
1.5       raeburn   355:                             my %changeHash;
                    356:                             $changeHash{'firstname'}  = $first;
                    357:                             $changeHash{'middlename'} = $middle;
                    358:                             $changeHash{'lastname'}   = $last;
                    359:                             $changeHash{'generation'} = $gene;
                    360:                             $changeHash{'id'} = $pid;
                    361:                             my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
                    362:                             if ($putresult eq 'ok') {
                    363:                                 $$logmsg .= "User information updated for user: $uname prior to enrollment.".$linefeed;
                    364:                             } else {
                    365:                                 $$logmsg .= "There was a problem modifying user data for existing user - $uname -error: $putresult, enrollment will still be attempted.".$linefeed;
                    366:                             }
1.1       raeburn   367:                         }
1.3       raeburn   368:  
                    369: # Assign the role of student in the course.
1.5       raeburn   370:                         my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto',$cid);
                    371:                         if ($classlist_reply eq 'ok') {
                    372:                             $enrollcount ++;
                    373:                             $addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$linefeed;
1.6       raeburn   374:                             if ($context eq 'automated') {
                    375:                                 $$logmsg .= "Existing $dom user $uname enrolled successfully.".$linefeed;
                    376:                             }
1.5       raeburn   377:                         } else {
                    378:                             $$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;
                    379:                         }
1.3       raeburn   380:                     }
1.1       raeburn   381:                 }
                    382:             }
                    383:         }
                    384:     }
                    385: # Do drops
                    386:     if ( ($drops == 1) && (@reg_students > 0) ) {
                    387:         foreach my $uname (@localstudents) {
                    388:             if ($$currlist{$uname}[$type] eq "auto") {
                    389:                 my @saved = ();
                    390:                 if (!grep/^$uname$/,@reg_students) {
                    391: # Check for changed usernames by checking studentIDs
                    392:                     if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
                    393:                         foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
1.6       raeburn   394:                             $$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   395:                             push @saved,$uname;
                    396:                         }
                    397:                     } elsif (@saved == 0) {
                    398:                         my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,undef,$cid);
                    399:                         if ($drop_reply !~ /^ok/) {
1.5       raeburn   400:                             $$logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $drop_reply.".$linefeed;
1.1       raeburn   401:                         } else {
                    402:                             $dropcount ++;
                    403:                             my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
                    404:                             $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname." dropped from section/group ".$$currlist{$uname}[$sec].$linefeed; 
1.8     ! raeburn   405:                             if ($context eq 'automated') {
        !           406:                                 $$logmsg .= "User $uname student role expired from course.".$linefeed;
        !           407:                             }
1.1       raeburn   408:                         }
                    409:                     }
                    410:                 }
                    411:             }
                    412:         }
                    413:     }
1.5       raeburn   414: 
                    415: # Terminated explictly allowed access to student creation/modification
                    416:     if ($context eq 'automated') {
                    417:         delete($ENV{'allowed.cst'});
                    418:     }
1.1       raeburn   419:     if ($enrollcount > 0) {
                    420:         if ($context eq "updatenow") {
1.6       raeburn   421:             $addresult = substr($addresult,0,rindex($addresult,"<li>"));
1.1       raeburn   422:             $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:<br/><ul><li>".$addresult."</li></ul><br/><br/>";
                    423:         } else {
                    424:             $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:\n\n".$addresult."\n\n";    
                    425:         }      
                    426:     }
                    427:     if ($dropcount > 0) {
                    428:         if ($context eq "updatenow") {
1.6       raeburn   429:             $dropresult = substr($dropresult,0,rindex($dropresult,"<li>"));
1.1       raeburn   430:             $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:<br/><ul><li>".$dropresult."</li></ul><br/><br/>";
                    431:         } else {
                    432:             $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:\n\n".$dropresult."\n\n";
                    433:         }
                    434:     }
                    435:     if ( ($adds) && ($enrollcount == 0) ) {
                    436:         $addresult = "There were no new students to add to the course.";
                    437:         if ($context eq "updatenow") {
                    438:             $addresult .="<br/><br/>";
                    439:         } else {
                    440:             $addresult .="\n";
                    441:         }
                    442:     }
                    443:     if ( ($drops) && ($dropcount == 0) ) {
                    444:         $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.";
                    445:         if ($context eq "updatenow") {
                    446:             $dropresult .="<br/>";
                    447:         } else {
                    448:             $dropresult .="\n";
                    449:         }
                    450:     }
1.5       raeburn   451:     my $changecount = $enrollcount + $dropcount;
                    452:     return ($changecount,$addresult.$dropresult); 
1.6       raeburn   453: }
1.1       raeburn   454: 
                    455: sub parse_classlist {
1.6       raeburn   456:     my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
1.5       raeburn   457:     my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
1.6       raeburn   458:     my $uname = '';
                    459:     my @state;
1.8     ! raeburn   460:     my @items = ('autharg','authtype','email','firstname','generation','lastname','middlename','studentID');
1.6       raeburn   461:     my $p = HTML::Parser->new
                    462:     (
                    463:         xml_mode => 1,
                    464:         start_h =>
                    465:             [sub {
                    466:                  my ($tagname, $attr) = @_;
                    467:                  push @state, $tagname;
                    468:                  if ("@state" eq "students student") {
                    469:                      $uname = $attr->{username};
                    470:                  }
                    471:             }, "tagname, attr"],
                    472:          text_h =>
                    473:              [sub {
                    474:                  my ($text) = @_;
                    475:                  if ("@state" eq "students student groupID") {
                    476:                      $$studentsref{$uname}[ $$placeref{'groupID'} ] = $groupID;
1.8     ! raeburn   477:                  } elsif ("@state" eq "students student startdate") {
        !           478:                      my $start = $text;
        !           479:                      unless ($text eq '') {
        !           480:                          $start = &process_date($text);
        !           481:                      }
        !           482:                      $$studentsref{$uname}[ $$placeref{'startdate'} ] = $start; 
        !           483:                  } elsif ("@state" eq "students student enddate") {
        !           484:                      my $end = $text;
        !           485:                      unless ($text eq '') {
        !           486:                          $end = &process_date($text);
        !           487:                      }
        !           488:                      $$studentsref{$uname}[ $$placeref{'enddate'} ] = $end;
1.6       raeburn   489:                  } else {
                    490:                      foreach my $item (@items) {
                    491:                          if ("@state" eq "students student $item") {
                    492:                              $$studentsref{$uname}[ $$placeref{$item} ] = $text;
                    493:                          }
                    494:                      }
                    495:                  }
                    496:                }, "dtext"],
                    497:          end_h =>
                    498:                [sub {
                    499:                    my ($tagname) = @_;
                    500:                    pop @state;
                    501:                 }, "tagname"],
                    502:     );
                    503:                                                                                                              
                    504:     $p->parse_file($xmlfile);
                    505:     $p->eof;
1.8     ! raeburn   506:     if (-e "$xmlfile") {
        !           507:         unlink $xmlfile;
        !           508:     }
1.3       raeburn   509:     return;
1.1       raeburn   510: }
                    511: 
1.8     ! raeburn   512: sub process_date {
        !           513:     my $timestr = shift;
        !           514:     my $timestamp = '';
        !           515:     if ($timestr =~ m/^\d{4}:\d{2}:\d{2}/) {
        !           516:         my @entries = split/:/,$timestr;
        !           517:         for (my $j=0; $j<@entries; $j++) {
        !           518:             if ( length($entries[$j]) > 1 ) {
        !           519:                 $entries[$j] =~ s/^0//;
        !           520:             }
        !           521:         }
        !           522:         $entries[1] = $entries[1] - 1;
        !           523:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
        !           524:     }
        !           525:     return $timestamp;
        !           526: }
        !           527: 
1.1       raeburn   528: sub create_password {
1.8     ! raeburn   529:     my $passwd = '';
        !           530:     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";
        !           531:     for (my $i=0; $i<8; $i++) {
        !           532:         my $lettnum = int (rand 2);
        !           533:         my $item = '';
        !           534:         if ($lettnum) {
        !           535:             $item = $letts[int( rand(26) )];
        !           536:             my $uppercase = int(rand 2);
        !           537:             if ($uppercase) {
        !           538:                 $item =~ tr/a-z/A-Z/;
        !           539:             }
        !           540:         } else {
        !           541:             $item = int( rand(10) );
        !           542:         } 
        !           543:         $passwd .= $item;
        !           544:     }
        !           545:     return ($passwd);
1.1       raeburn   546: }
                    547: 
                    548: sub CL_autharg { return 0; }
                    549: sub CL_authtype { return 1;}
                    550: sub CL_email { return 2;}
                    551: sub CL_enddate { return 3;}
                    552: sub CL_firstname { return 4;}
                    553: sub CL_generation { return 5;}
                    554: sub CL_groupID { return 6;}
                    555: sub CL_lastname { return 7;}
                    556: sub CL_middlename { return 8;}
                    557: sub CL_startdate { return 9; }
                    558: sub CL_studentID { return 10; }
                    559: 
                    560: 1;

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