MDL-87708 tool_moodlenet: Update adhoc task to create profile fields
Part of MDL-87361
This commit is contained in:
@@ -169,6 +169,33 @@ class profile_manager {
|
||||
return 'moodlenetprofile';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create MoodleNet user profile fields (category and text field).
|
||||
* This method is called during plugin installation to ensure the required
|
||||
* profile field exists from the start.
|
||||
*/
|
||||
public static function create_moodlenet_user_profile_fields(): void {
|
||||
global $DB;
|
||||
|
||||
// This checks if the category and field already exist before creating them.
|
||||
if (!self::check_profile_category()) {
|
||||
$categoryid = self::create_user_profile_category();
|
||||
self::create_user_profile_text_field($categoryid);
|
||||
} else {
|
||||
// Category exists, get its ID and check if field exists.
|
||||
$category = $DB->get_record('user_info_category', [
|
||||
'name' => self::get_category_name(),
|
||||
]);
|
||||
|
||||
$profilefield = $DB->record_exists('user_info_field', [
|
||||
'shortname' => self::get_profile_field_name(),
|
||||
'categoryid' => $category->id,
|
||||
]);
|
||||
if (!$profilefield) {
|
||||
self::create_user_profile_text_field($category->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a user profile field to hold the moodlenet profile information.
|
||||
@@ -194,7 +221,7 @@ class profile_manager {
|
||||
'forceunique' => 1,
|
||||
'visible' => 2,
|
||||
'param1' => 30,
|
||||
'param2' => 2048
|
||||
'param2' => 2048,
|
||||
];
|
||||
$profileclass->define_save($data);
|
||||
}
|
||||
|
||||
@@ -31,5 +31,11 @@ namespace tool_moodlenet\task;
|
||||
class post_install extends \core\task\adhoc_task {
|
||||
public function execute() {
|
||||
set_config('activitychooseractivefooter', 'tool_moodlenet');
|
||||
|
||||
// Create the MoodleNet custom profile field for new installations.
|
||||
// This checks if the category and field already exist before creating them.
|
||||
if (!PHPUNIT_TEST) {
|
||||
\tool_moodlenet\profile_manager::create_moodlenet_user_profile_fields();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,8 +53,8 @@ $string['instanceplaceholder'] = '[email protected]';
|
||||
$string['invalidmoodlenetprofile'] = '$userprofile is not correctly formatted';
|
||||
$string['missinginvalidpostdata'] = 'The resource information from MoodleNet is either missing, or is in an incorrect format.
|
||||
If this happens repeatedly, please contact the site administrator.';
|
||||
$string['mnetprofile'] = 'MoodleNet profile';
|
||||
$string['mnetprofiledesc'] = '<p>Enter your MoodleNet profile details here to be redirected to your profile while visiting MoodleNet.</p>';
|
||||
$string['mnetprofile'] = 'MoodleNet profile ID';
|
||||
$string['mnetprofiledesc'] = 'Your MoodleNet profile ID links your MoodleNet profile with this site.';
|
||||
$string['moodlenetnotenabled'] = 'The MoodleNet integration must be enabled in Site administration / MoodleNet before resource imports can be processed.';
|
||||
$string['moodlenetsettings'] = 'MoodleNet inbound settings';
|
||||
$string['notification'] = 'You are about to import the content "{$a->name} ({$a->type})" into your site. Select the course in which it should be added, or <a href="{$a->cancellink}">cancel</a>.';
|
||||
|
||||
@@ -136,4 +136,48 @@ final class profile_manager_test extends \advanced_testcase {
|
||||
$fieldname = \tool_moodlenet\profile_manager::get_profile_field_name();
|
||||
$this->assertEquals($profilename, $userdata->{'profile_field_' . $fieldname});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creation of MoodleNet user profile fields.
|
||||
*
|
||||
* @covers ::create_moodlenet_user_profile_fields
|
||||
*/
|
||||
public function test_create_moodlenet_user_profile_fields(): void {
|
||||
global $CFG, $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
|
||||
$categoryname = \tool_moodlenet\profile_manager::get_category_name();
|
||||
$fieldname = \tool_moodlenet\profile_manager::get_profile_field_name();
|
||||
|
||||
// Verify category and field don't exist initially.
|
||||
$categorycount = $DB->count_records('user_info_category', ['name' => $categoryname]);
|
||||
$this->assertEquals(0, $categorycount);
|
||||
$fieldcount = $DB->count_records('user_info_field', ['shortname' => $fieldname]);
|
||||
$this->assertEquals(0, $fieldcount);
|
||||
|
||||
// Call the method to create category and field.
|
||||
\tool_moodlenet\profile_manager::create_moodlenet_user_profile_fields();
|
||||
|
||||
// Verify category was created.
|
||||
$categorycount = $DB->count_records('user_info_category', ['name' => $categoryname]);
|
||||
$this->assertEquals(1, $categorycount);
|
||||
$category = $DB->get_record('user_info_category', ['name' => $categoryname]);
|
||||
$categoryid = $category->id;
|
||||
|
||||
// Verify field was created in the correct category.
|
||||
$fieldcount = $DB->count_records('user_info_field', [
|
||||
'shortname' => $fieldname,
|
||||
'categoryid' => $categoryid,
|
||||
]);
|
||||
$this->assertEquals(1, $fieldcount);
|
||||
|
||||
// Verify the method is idempotent (can be called multiple times).
|
||||
\tool_moodlenet\profile_manager::create_moodlenet_user_profile_fields();
|
||||
$categorycount = $DB->count_records('user_info_category', ['name' => $categoryname]);
|
||||
$this->assertEquals(1, $categorycount);
|
||||
$fieldcount = $DB->count_records('user_info_field', ['shortname' => $fieldname]);
|
||||
$this->assertEquals(1, $fieldcount);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user