MDL-87708 tool_moodlenet: Remove user table moodlenetprofile support
Part of MDL-87361
This commit is contained in:
@@ -41,23 +41,10 @@ class profile_manager {
|
||||
*/
|
||||
public static function get_moodlenet_user_profile(int $userid): ?moodlenet_user_profile {
|
||||
global $CFG;
|
||||
// Check for official profile.
|
||||
if (self::official_profile_exists()) {
|
||||
$user = \core_user::get_user($userid, 'moodlenetprofile');
|
||||
try {
|
||||
$userprofile = $user->moodlenetprofile ? $user->moodlenetprofile : '';
|
||||
return (isset($user)) ? new moodlenet_user_profile(s($userprofile), $userid) : null;
|
||||
} catch (\moodle_exception $e) {
|
||||
// If an exception is thrown, means there isn't a valid profile set. No need to log exception.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// Otherwise get hacked in user profile field.
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
$profilefields = profile_get_user_fields_with_data($userid);
|
||||
foreach ($profilefields as $key => $field) {
|
||||
if ($field->get_category_name() == self::get_category_name()
|
||||
&& $field->inputname == 'profile_field_mnetprofile') {
|
||||
foreach ($profilefields as $field) {
|
||||
if ($field->inputname == 'profile_field_moodlenetprofile') {
|
||||
try {
|
||||
return new moodlenet_user_profile(s($field->display_data()), $userid);
|
||||
} catch (\moodle_exception $e) {
|
||||
@@ -75,18 +62,7 @@ class profile_manager {
|
||||
* @param moodlenet_user_profile $moodlenetprofile The moodlenet profile to save.
|
||||
*/
|
||||
public static function save_moodlenet_user_profile(moodlenet_user_profile $moodlenetprofile): void {
|
||||
global $CFG, $DB;
|
||||
// Do some cursory checks first to see if saving is possible.
|
||||
if (self::official_profile_exists()) {
|
||||
// All good. Let's save.
|
||||
$user = \core_user::get_user($moodlenetprofile->get_userid());
|
||||
$user->moodlenetprofile = $moodlenetprofile->get_profile_name();
|
||||
|
||||
require_once($CFG->dirroot . '/user/lib.php');
|
||||
|
||||
\user_update_user($user, false, true);
|
||||
return;
|
||||
}
|
||||
global $CFG;
|
||||
$fielddata = self::get_user_profile_field();
|
||||
$fielddata = self::validate_and_fix_missing_profile_items($fielddata);
|
||||
// Everything should be back to normal. Let's save.
|
||||
@@ -157,55 +133,13 @@ class profile_manager {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Are we using the proper user profile field to hold the mnet profile?
|
||||
*
|
||||
* @return bool True if we are using a user table field for the mnet profile. False means we are using costom profile fields.
|
||||
*/
|
||||
public static function official_profile_exists(): bool {
|
||||
global $DB;
|
||||
|
||||
$usertablecolumns = $DB->get_columns('user', false);
|
||||
if (isset($usertablecolumns['moodlenetprofile'])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the category name that is set for this site.
|
||||
*
|
||||
* @return string The category used to hold the moodle net profile field.
|
||||
*/
|
||||
public static function get_category_name(): string {
|
||||
return get_config('tool_moodlenet', 'profile_category');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the a unique category to hold the moodle net user profile.
|
||||
*
|
||||
* @param string $categoryname The base category name to use.
|
||||
* @return string The actual name of the category to use.
|
||||
*/
|
||||
private static function set_category_name(string $categoryname): string {
|
||||
global $DB;
|
||||
|
||||
$attemptname = $categoryname;
|
||||
|
||||
// Check if this category already exists.
|
||||
$foundcategoryname = false;
|
||||
$i = 0;
|
||||
do {
|
||||
$category = $DB->count_records('user_info_category', ['name' => $attemptname]);
|
||||
if ($category > 0) {
|
||||
$i++;
|
||||
$attemptname = $categoryname . $i;
|
||||
} else {
|
||||
set_config('profile_category', $attemptname, 'tool_moodlenet');
|
||||
$foundcategoryname = true;
|
||||
}
|
||||
} while (!$foundcategoryname);
|
||||
return $attemptname;
|
||||
return 'MoodleNet';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,7 +152,7 @@ class profile_manager {
|
||||
// No nice API to do this, so direct DB calls it is.
|
||||
$data = new \stdClass();
|
||||
$data->sortorder = $DB->count_records('user_info_category') + 1;
|
||||
$data->name = self::set_category_name(get_string('pluginname', 'tool_moodlenet'));
|
||||
$data->name = self::get_category_name();
|
||||
$data->id = $DB->insert_record('user_info_category', $data, true);
|
||||
|
||||
$createdcategory = $DB->get_record('user_info_category', array('id' => $data->id));
|
||||
@@ -227,39 +161,12 @@ class profile_manager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a unique name to be used for the moodle net profile.
|
||||
* Gets the profile field used to hold the moodle net profile.
|
||||
*
|
||||
* @param string $fieldname The base fieldname to use.
|
||||
* @return string The actual profile field name.
|
||||
*/
|
||||
private static function set_profile_field_name(string $fieldname): string {
|
||||
global $DB;
|
||||
|
||||
$attemptname = $fieldname;
|
||||
|
||||
// Check if this profilefield already exists.
|
||||
$foundfieldname = false;
|
||||
$i = 0;
|
||||
do {
|
||||
$profilefield = $DB->count_records('user_info_field', ['shortname' => $attemptname]);
|
||||
if ($profilefield > 0) {
|
||||
$i++;
|
||||
$attemptname = $fieldname . $i;
|
||||
} else {
|
||||
set_config('profile_field_name', $attemptname, 'tool_moodlenet');
|
||||
$foundfieldname = true;
|
||||
}
|
||||
} while (!$foundfieldname);
|
||||
return $attemptname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the unique profile field used to hold the moodle net profile.
|
||||
*
|
||||
* @return string The profile field name being used on this site.
|
||||
* @return string The profile field shortname.
|
||||
*/
|
||||
public static function get_profile_field_name(): string {
|
||||
return get_config('tool_moodlenet', 'profile_field_name');
|
||||
return 'moodlenetprofile';
|
||||
}
|
||||
|
||||
|
||||
@@ -277,7 +184,7 @@ class profile_manager {
|
||||
// Add our moodlenet profile field.
|
||||
$profileclass = new \profile_define_text();
|
||||
$data = (object) [
|
||||
'shortname' => self::set_profile_field_name('mnetprofile'),
|
||||
'shortname' => 'moodlenetprofile',
|
||||
'name' => get_string('mnetprofile', 'tool_moodlenet'),
|
||||
'datatype' => 'text',
|
||||
'description' => get_string('mnetprofiledesc', 'tool_moodlenet'),
|
||||
|
||||
@@ -26,13 +26,6 @@ namespace tool_moodlenet;
|
||||
*/
|
||||
final class profile_manager_test extends \advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test that on this site we use the user table to hold moodle net profile information.
|
||||
*/
|
||||
public function test_official_profile_exists(): void {
|
||||
$this->assertTrue(\tool_moodlenet\profile_manager::official_profile_exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a null is returned when the user's mnet profile field is not set.
|
||||
*/
|
||||
@@ -60,11 +53,24 @@ final class profile_manager_test extends \advanced_testcase {
|
||||
* Test the return of a moodle net profile.
|
||||
*/
|
||||
public function test_get_moodlenet_user_profile(): void {
|
||||
global $CFG;
|
||||
$this->resetAfterTest();
|
||||
$user = $this->getDataGenerator()->create_user(['moodlenetprofile' => '@[email protected]']);
|
||||
|
||||
// Create the custom profile category and field.
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
$categoryid = \tool_moodlenet\profile_manager::create_user_profile_category();
|
||||
\tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$profilename = '@[email protected]';
|
||||
|
||||
// Save the profile using the profile manager.
|
||||
$moodlenetprofile = new \tool_moodlenet\moodlenet_user_profile($profilename, $user->id);
|
||||
\tool_moodlenet\profile_manager::save_moodlenet_user_profile($moodlenetprofile);
|
||||
|
||||
// Get the profile back.
|
||||
$result = \tool_moodlenet\profile_manager::get_moodlenet_user_profile($user->id);
|
||||
$this->assertEquals($user->moodlenetprofile, $result->get_profile_name());
|
||||
$this->assertEquals($profilename, $result->get_profile_name());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,22 +80,14 @@ final class profile_manager_test extends \advanced_testcase {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$basecategoryname = get_string('pluginname', 'tool_moodlenet');
|
||||
|
||||
\tool_moodlenet\profile_manager::create_user_profile_category();
|
||||
$categoryname = \tool_moodlenet\profile_manager::get_category_name();
|
||||
$this->assertEquals($basecategoryname, $categoryname);
|
||||
$expectedname = get_string('pluginname', 'tool_moodlenet');
|
||||
$this->assertEquals($expectedname, $categoryname);
|
||||
|
||||
\tool_moodlenet\profile_manager::create_user_profile_category();
|
||||
|
||||
$recordcount = $DB->count_records('user_info_category', ['name' => $basecategoryname]);
|
||||
$recordcount = $DB->count_records('user_info_category', ['name' => $categoryname]);
|
||||
$this->assertEquals(1, $recordcount);
|
||||
|
||||
// Test the duplication of categories to ensure a unique name is always used.
|
||||
$categoryname = \tool_moodlenet\profile_manager::get_category_name();
|
||||
$this->assertEquals($basecategoryname . 1, $categoryname);
|
||||
\tool_moodlenet\profile_manager::create_user_profile_category();
|
||||
$categoryname = \tool_moodlenet\profile_manager::get_category_name();
|
||||
$this->assertEquals($basecategoryname . 2, $categoryname);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +97,7 @@ final class profile_manager_test extends \advanced_testcase {
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$shortname = 'mnetprofile';
|
||||
$shortname = 'moodlenetprofile';
|
||||
|
||||
$categoryid = \tool_moodlenet\profile_manager::create_user_profile_category();
|
||||
\tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
|
||||
@@ -108,21 +106,23 @@ final class profile_manager_test extends \advanced_testcase {
|
||||
$this->assertEquals($shortname, $record->shortname);
|
||||
$this->assertEquals($categoryid, $record->categoryid);
|
||||
|
||||
// Test for a unique name if 'mnetprofile' is already in use.
|
||||
\tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
|
||||
// Verify the field shortname is always 'moodlenetprofile'.
|
||||
$profilename = \tool_moodlenet\profile_manager::get_profile_field_name();
|
||||
$this->assertEquals($shortname . 1, $profilename);
|
||||
\tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
|
||||
$profilename = \tool_moodlenet\profile_manager::get_profile_field_name();
|
||||
$this->assertEquals($shortname . 2, $profilename);
|
||||
$this->assertEquals($shortname, $profilename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the user moodlenet profile is saved.
|
||||
*/
|
||||
public function test_save_moodlenet_user_profile(): void {
|
||||
global $CFG;
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create the custom profile category and field.
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
$categoryid = \tool_moodlenet\profile_manager::create_user_profile_category();
|
||||
\tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$profilename = '@[email protected]';
|
||||
|
||||
@@ -130,7 +130,10 @@ final class profile_manager_test extends \advanced_testcase {
|
||||
|
||||
\tool_moodlenet\profile_manager::save_moodlenet_user_profile($moodlenetprofile);
|
||||
|
||||
// Load the user with profile data to verify.
|
||||
$userdata = \core_user::get_user($user->id);
|
||||
$this->assertEquals($profilename, $userdata->moodlenetprofile);
|
||||
profile_load_data($userdata);
|
||||
$fieldname = \tool_moodlenet\profile_manager::get_profile_field_name();
|
||||
$this->assertEquals($profilename, $userdata->{'profile_field_' . $fieldname});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user