From 764db39e095d9ffedd834ba278e6446dcdc1c923 Mon Sep 17 00:00:00 2001
From: Alexander Bias <alexander.bias@uni-ulm.de>
Date: Fri, 2 Feb 2018 15:55:21 +0100
Subject: [PATCH] Adopt code changes in Moodle 3.3 core auth_ldap: Sync user
 profile fields

---
 CHANGES.md |  1 +
 auth.php   | 43 +++----------------------------------------
 2 files changed, 4 insertions(+), 40 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 567185a..6a24f2e 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,7 @@ Changes
 
 ### Unreleased
 
+* 2018-02-02 - Adopt code changes in Moodle 3.3 core auth_ldap: Sync user profile fields
 * 2018-02-02 - Adopt code changes in Moodle 3.3 core auth_ldap: Convert auth plugins to use settings.php. Please double-check your plugin settings after upgrading to this version.
 * 2017-12-12 - Prepare compatibility for Moodle 3.3, no functionality change.
 * 2017-12-05 - Added Workaround to travis.yml for fixing Behat tests with TravisCI.
diff --git a/auth.php b/auth.php
index 9bde324..c825894 100644
--- a/auth.php
+++ b/auth.php
@@ -331,23 +331,7 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
         /// User Updates - time-consuming (optional)
         if ($do_updates) {
             // Narrow down what fields we need to update
-            $all_keys = array_keys(get_object_vars($this->config));
-            $updatekeys = array();
-            foreach ($all_keys as $key) {
-                if (preg_match('/^field_updatelocal_(.+)$/', $key, $match)) {
-                    // If we have a field to update it from
-                    // and it must be updated 'onlogin' we
-                    // update it on cron
-                    if (!empty($this->config->{'field_map_'.$match[1]})
-                         and $this->config->{$match[0]} === 'onlogin') {
-                        array_push($updatekeys, $match[1]); // the actual key name
-                    }
-                }
-            }
-            if ($this->config->suspended_attribute && $this->config->sync_suspended) {
-                $updatekeys[] = 'suspended';
-            }
-            unset($all_keys); unset($key);
+            $updatekeys = $this->get_profile_keys();
         } else {
             mtrace(get_string('noupdatestobedone', 'auth_ldap'));
         }
@@ -458,29 +442,8 @@ class auth_plugin_ldap_syncplus extends auth_plugin_ldap {
                     }
 
                     // Save custom profile fields.
-                    $profilefields = array();
-                    foreach ($user as $key => $value) {
-                        if (preg_match('/^profile_field_(.*)$/', $key, $match)) {
-                            $field = $match[1];
-                            $profilefields[$field] = $user->$key;
-                        }
-                    }
-                    // Now, save the profile fields if the user has any.
-                    if ($fields = $DB->get_records('user_info_field')) {
-                        foreach ($fields as $field) {
-                            if (isset($profilefields[$field->shortname])) {
-                                $conditions = array('fieldid' => $field->id, 'userid' => $euser->id);
-                                $id = $DB->get_field('user_info_data', 'id', $conditions);
-                                $data = $profilefields[$field->shortname];
-                                if ($id) {
-                                    $DB->set_field('user_info_data', 'data', $data, array('id' => $id));
-                                } else {
-                                    $record = array('fieldid' => $field->id, 'userid' => $euser->id, 'data' => $data);
-                                    $DB->insert_record('user_info_data', $record);
-                                }
-                            }
-                        }
-                    }
+                    $updatekeys = $this->get_profile_keys(true);
+                    $this->update_user_record($user->username, $updatekeys, false);
                 }
                 $transaction->allow_commit();
                 unset($add_users); // free mem
-- 
GitLab