[LON-CAPA-cvs] cvs: modules /raeburn Autoenroll.pl

raeburn lon-capa-cvs@mail.lon-capa.org
Thu, 20 Nov 2003 00:19:41 -0000


raeburn		Wed Nov 19 19:19:41 2003 EDT

  Modified files:              
    /modules/raeburn	Autoenroll.pl 
  Log:
  Script to run as cron to update classlists - 11/19 version
  
  
Index: modules/raeburn/Autoenroll.pl
diff -u modules/raeburn/Autoenroll.pl:1.1 modules/raeburn/Autoenroll.pl:1.2
--- modules/raeburn/Autoenroll.pl:1.1	Wed Nov 19 01:44:14 2003
+++ modules/raeburn/Autoenroll.pl	Wed Nov 19 19:19:41 2003
@@ -237,20 +237,62 @@
          elsif ($uname ne '')
          {
 # Check for changed usernames by checking studentIDs
-           if (grep/^$$currlist{$uname}[$pid]$/,@allPids)
+           if (grep/^$$currlist{$uname}[$id]$/,@allINids)
            {
-             foreach my $match @{$unameFromPid{$$currlist{$student}[$pid]}}
+             foreach my $match @{$unameFromINid{$$currlist{$student}[$id]}}
              {
                if (grep/^$match$/,@okusers)
                {
-                 print "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}[$pid].  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";
+                 print "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}[$id].  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";
                }
              }
            }
-# add student to LON-CAPA classlist
+# Add student to LON-CAPA classlist
            elsif ($enrollvar{$crs}{autoadds} == 1)
            {
-             my $reply=&Apache::lonnet::modifystudent($dom,$uname,$cid,$stuinfo[$amode],$stuinfo[$password],$stuinfo[$fname],$stuinfo[$mname],$studinfo[$lname],$stuinfo[$gen],$stuinfo[$sec],$stuinfo[$enddate],$stuinfo[$startdate],'',undef,$stuinfo[$email]);
+             my $authtype = $stuinfo[$amode];
+             my $passwd = $stuinfo[$password];
+             my $first = $stuinfo[$fname];
+             my $middle = $stuinfo[$mname];
+             my $last = $stuinfo[$lname];
+             my $gene = $stuinfo[$gen];
+             my $section = $stuinfo[$sect];
+             my $enddate = $stuinfo[$end];
+             my $startdate = $stuinfo[$start];
+             my $address = $stuinfo[$email];
+             my $pid = $stuinfo[$id];
+
+             # remove non alphanumeric values from section
+             $section =~ s/\W//g;
+
+             unless ($address=~/^[^\@]+\@[^\@]+$/) { $address=''; }
+
+ # Use course defaults where entry is absent
+             if ($authtype eq '')
+             {
+               $authtype =  $enrollvar{$crs}{authtype};
+             }
+             if ($enddate eq '')
+             {
+               $enddate = $enrollvar{$crs}{enddate};
+             }
+             if ($startdate eq '')
+             {
+               $startdate = $enrollvar{$crs}{startdate};
+             }
+             if ($passwd eq '')
+             {
+# If no account exists and passwords should be generated
+               $passwd = &LONCAPA::Enrollment::create_password();
+             }
+
+             # Clean up whitespace
+             foreach (\$dom,\$uname,\$pid,\$first,\$middle,\$last,\$gene,\$section)
+             {
+               $$_ =~ s/(\s+$|^\s+)//g;
+             }
+
+             my $reply=&modifystudent($dom,$cid,$uname,$pid,$authtype,$passwd,$first,$middle,$last,$gene,$section,$enddate,$startdate,'',undef,$stuinfo[$email]);
            }
          }
        }
@@ -264,3 +306,68 @@
      }
    }
  }
+
+
+sub modifystudent {
+    my ($udom,$cid,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
+        $end,$start,$forceid,$desiredhome,$email)=@_;
+# --------------------------------------------------------------- Make the user
+    my $reply=&modifyuser
+        ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
+         $desiredhome,$email);
+    unless ($reply eq 'ok') { return $reply; }
+    # This will cause &modify_student_enrollment to get the uid from the
+    # students environment
+    $uid = undef if (!$forceid);
+    $reply = &modify_student_enrollment($udom,$cid,$uname,$uid,$first,$middle,
+                                        $last,$gene,$usec,$end,$start);
+    return $reply;
+}
+                                                                                       
+sub modify_student_enrollment {
+    my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start) = @_;
+    # Make sure the user exists
+    my $uhome=&homeserver($uname,$udom);
+    if (($uhome eq '') || ($uhome eq 'no_host')) {
+        return 'error: no such user';
+    }
+    #
+    # Get student data if we were not given enough information
+    if (!defined($first)  || $first  eq '' ||
+        !defined($last)   || $last   eq '' ||
+        !defined($uid)    || $uid    eq '' ||
+        !defined($middle) || $middle eq '' ||
+        !defined($gene)   || $gene   eq '') {
+        # They did not supply us with enough data to enroll the student, so
+        # we need to pick up more information.
+        my %tmp = &get('environment',
+                       ['firstname','middlename','lastname', 'generation','id']
+                       ,$udom,$uname);
+        foreach (keys(%tmp)) {
+            &logthis("key $_ = ".$tmp{$_});
+        }
+        $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
+        $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
+        $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
+        $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
+        $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
+    }
+    my $fullname = &Apache::loncoursedata::ProcessFullName($last,$gene,
+                                                           $first,$middle);
+    my $reply=critical('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
+                      $ENV{'course.'.$cid.'.num'}.':classlist:'.
+                      &escape($uname.':'.$udom).'='.
+                      &escape(join(':',$end,$start,$uid,$usec,$fullname)),
+                      $ENV{'course.'.$cid.'.home'});
+    unless (($reply eq 'ok') || ($reply eq 'delayed')) {
+        return 'error: '.$reply;
+    }
+    # Add student role to user
+    my $uurl='/'.$cid;
+    $uurl=~s/\_/\//g;
+    if ($usec) {
+        $uurl.='/'.$usec;
+    }
+    return &assignrole($udom,$uname,$uurl,'st',$end,$start);
+}
+