MDL-73878 core_user: Fixing social fields upgrading issue

This commit is contained in:
Amaia Anabitarte 2022-04-21 13:25:43 +02:00
parent 1a744030d6
commit d2fb057b7e

View file

@ -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. * 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; global $DB;
$categories = $DB->get_records('user_info_category', null, 'sortorder ASC'); $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'), 'name' => get_string('profiledefaultcategory', 'admin'),
'sortorder' => 1 '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; $visible = (in_array($confignames[$social], $hiddenfields)) ? 3 : 2;
$categoryid = user_profile_social_create_info_category();
$newfield = (object)[ $newfield = (object)[
'shortname' => $social, 'shortname' => $social,
'name' => $social, 'name' => $social,
'datatype' => 'social', 'datatype' => 'social',
'description' => '', 'description' => '',
'descriptionformat' => 1, 'descriptionformat' => 1,
'categoryid' => 1, 'categoryid' => $categoryid,
'required' => 0, 'required' => 0,
'locked' => 0, 'locked' => 0,
'visible' => $visible, 'visible' => $visible,
@ -106,8 +112,6 @@ function user_profile_social_create_profilefield($social) {
'param1' => $social 'param1' => $social
]; ];
user_profile_social_create_info_category();
$profilefield = $DB->get_record_sql( $profilefield = $DB->get_record_sql(
'SELECT * FROM {user_info_field} WHERE datatype = :datatype AND ' . 'SELECT * FROM {user_info_field} WHERE datatype = :datatype AND ' .
$DB->sql_compare_text('param1') . ' = ' . $DB->sql_compare_text(':social', 40), $DB->sql_compare_text('param1') . ' = ' . $DB->sql_compare_text(':social', 40),