diff --git a/public/admin/tool/moodlenet/classes/profile_manager.php b/public/admin/tool/moodlenet/classes/profile_manager.php index f6715a008ef..0f541f9a7ad 100644 --- a/public/admin/tool/moodlenet/classes/profile_manager.php +++ b/public/admin/tool/moodlenet/classes/profile_manager.php @@ -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); } diff --git a/public/admin/tool/moodlenet/classes/task/post_install.php b/public/admin/tool/moodlenet/classes/task/post_install.php index 55f2ca13881..4b735011b3e 100644 --- a/public/admin/tool/moodlenet/classes/task/post_install.php +++ b/public/admin/tool/moodlenet/classes/task/post_install.php @@ -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(); + } } } diff --git a/public/admin/tool/moodlenet/lang/en/tool_moodlenet.php b/public/admin/tool/moodlenet/lang/en/tool_moodlenet.php index dcb01c5184a..e00f07df79c 100644 --- a/public/admin/tool/moodlenet/lang/en/tool_moodlenet.php +++ b/public/admin/tool/moodlenet/lang/en/tool_moodlenet.php @@ -53,8 +53,8 @@ $string['instanceplaceholder'] = 'a1b2c3d4e5f6-example@moodle.net'; $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'] = '
Enter your MoodleNet profile details here to be redirected to your profile while visiting MoodleNet.
'; +$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 cancel.'; diff --git a/public/admin/tool/moodlenet/tests/profile_manager_test.php b/public/admin/tool/moodlenet/tests/profile_manager_test.php index 8561bbb16ae..d7b0285fbb5 100644 --- a/public/admin/tool/moodlenet/tests/profile_manager_test.php +++ b/public/admin/tool/moodlenet/tests/profile_manager_test.php @@ -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); + } }