From d2fb057b7e236dc316247a4c91faa47345ab9999 Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Thu, 21 Apr 2022 13:25:43 +0200 Subject: [PATCH 1/2] MDL-73878 core_user: Fixing social fields upgrading issue --- user/profile/field/social/upgradelib.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/user/profile/field/social/upgradelib.php b/user/profile/field/social/upgradelib.php index 37d1b1aa62b..dc3bebc2c88 100644 --- a/user/profile/field/social/upgradelib.php +++ b/user/profile/field/social/upgradelib.php @@ -28,8 +28,10 @@ require_once("$CFG->dirroot/user/profile/definelib.php"); /** * Create the default category for custom profile fields if it does not exist yet. + * + * @return int Category ID for social user profile category. */ -function user_profile_social_create_info_category() { +function user_profile_social_create_info_category(): int { global $DB; $categories = $DB->get_records('user_info_category', null, 'sortorder ASC'); @@ -39,7 +41,9 @@ function user_profile_social_create_info_category() { 'name' => get_string('profiledefaultcategory', 'admin'), 'sortorder' => 1 ]; - $DB->insert_record('user_info_category', $defaultcategory); + return $DB->insert_record('user_info_category', $defaultcategory); + } else { + return (int)$DB->get_field_sql('SELECT min(id) from {user_info_category}'); } } @@ -89,13 +93,15 @@ function user_profile_social_create_profilefield($social) { ]; $visible = (in_array($confignames[$social], $hiddenfields)) ? 3 : 2; + $categoryid = user_profile_social_create_info_category(); + $newfield = (object)[ 'shortname' => $social, 'name' => $social, 'datatype' => 'social', 'description' => '', 'descriptionformat' => 1, - 'categoryid' => 1, + 'categoryid' => $categoryid, 'required' => 0, 'locked' => 0, 'visible' => $visible, @@ -106,8 +112,6 @@ function user_profile_social_create_profilefield($social) { 'param1' => $social ]; - user_profile_social_create_info_category(); - $profilefield = $DB->get_record_sql( 'SELECT * FROM {user_info_field} WHERE datatype = :datatype AND ' . $DB->sql_compare_text('param1') . ' = ' . $DB->sql_compare_text(':social', 40), From ffc655b181cacf8b3896ad7c24889a07dd146e16 Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Thu, 21 Apr 2022 14:07:54 +0200 Subject: [PATCH 2/2] MDL-73878 core_user: Creating a new category for social fields --- lib/db/upgrade.php | 18 ++++++++++++++++++ version.php | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 73017c7bbd4..341a538dff8 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -4476,5 +4476,23 @@ privatefiles,moodle|/user/files.php'; // Automatically generated Moodle v4.0.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2022042900.01) { + // Social custom fields could had been created linked to category id = 1. Let's check category 1 exists. + if (!$DB->get_record('user_info_category', ['id' => 1])) { + // Let's check if we have any custom field linked to category id = 1. + $fields = $DB->get_records('user_info_field', ['categoryid' => 1]); + if (!empty($fields)) { + $categoryid = $DB->get_field_sql('SELECT min(id) from {user_info_category}'); + foreach ($fields as $field) { + $field->categoryid = $categoryid; + $DB->update_record('user_info_field', $field); + } + } + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2022042900.01); + } + return true; } diff --git a/version.php b/version.php index 82b480e0352..1174eb6e763 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2022042900.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2022042900.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.1dev (Build: 20220429)'; // Human-friendly version name