From 1c4f27d4bc358b0f817d9368c6347bdcad7e9cb1 Mon Sep 17 00:00:00 2001 From: yusufwib01 Date: Thu, 22 Jan 2026 00:20:33 +0700 Subject: [PATCH] MDL-87543 hub: Registration collect defaulthomepage data --- public/lang/en/hub.php | 1 + public/lib/classes/hub/registration.php | 37 ++++++++++++++++++++ public/lib/tests/hub/registration_test.php | 40 ++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/public/lang/en/hub.php b/public/lang/en/hub.php index d803fa6484b..c9a52d0f8bd 100644 --- a/public/lang/en/hub.php +++ b/public/lang/en/hub.php @@ -45,6 +45,7 @@ $string['courseshortname'] = 'Shortname'; $string['courseshortname_help'] = 'Enter a short name for your course. It does not need to be unique.'; $string['coursesnumber'] = 'Number of courses ({$a})'; $string['dbtype'] = 'Database type ({$a})'; +$string['defaulthomepage'] = 'Start page for users ({$a})'; $string['demourl'] = 'Demo URL'; $string['demourl_help'] = 'Enter the demo URL of your course. By default it is the URL of your course. The demo URL is displayed as a link in a search result.'; $string['diskusagesize'] = 'Disk usage size ({$a} MB)'; diff --git a/public/lib/classes/hub/registration.php b/public/lib/classes/hub/registration.php index b99c8b912fc..c7816ab7c13 100644 --- a/public/lib/classes/hub/registration.php +++ b/public/lib/classes/hub/registration.php @@ -70,6 +70,8 @@ class registration { 2023081200 => ['aiusage'], // Disk usage added in Moodle 5.2. 2025100900 => ['diskusage'], + // Default homepage added in Moodle 5.2. + 2026012200 => ['defaulthomepage'], ]; /** @var string Site privacy: not displayed */ @@ -214,6 +216,9 @@ class registration { // Disk usage size. $siteinfo['diskusage'] = self::get_dataroot_size() ?? 0; + // Default homepage. + $siteinfo['defaulthomepage'] = get_config('moodle', 'defaulthomepage'); + // Primary auth type. $primaryauthsql = 'SELECT auth, count(auth) as tc FROM {user} GROUP BY auth ORDER BY tc DESC'; $siteinfo['primaryauthtype'] = $DB->get_field_sql($primaryauthsql, null, IGNORE_MULTIPLE); @@ -305,6 +310,7 @@ class registration { 'pluginusage' => get_string('pluginusagedata', 'hub', $pluginusagelinks), 'aiusage' => $OUTPUT->render_from_template('core/ai_usage_data', ['aiusagedata' => self::show_ai_usage()]), 'diskusage' => get_string('diskusagesize', 'hub', $siteinfo['diskusage']), + 'defaulthomepage' => get_string('defaulthomepage', 'hub', self::get_defaulthomepage_name($siteinfo['defaulthomepage'])), ]; foreach ($senddata as $key => $str) { @@ -968,4 +974,35 @@ class registration { public static function reset_caches(): void { self::$registration = null; } + + /** + * Returns the title for the defaulthomepage setting value. + * + * @param int|string $value The defaulthomepage value (constant or URL) + * @return string The title + */ + public static function get_defaulthomepage_name($value): string { + // Check for standard homepages. + $options = [ + HOMEPAGE_SITE => get_string('home'), + HOMEPAGE_MY => get_string('mymoodle', 'admin'), + HOMEPAGE_USER => get_string('userpreference', 'admin'), + HOMEPAGE_MYCOURSES => get_string('mycourses', 'admin'), + ]; + + if (isset($options[$value])) { + return $options[$value]; + } + + // Check for custom homepage. + $hook = new \core_user\hook\extend_default_homepage(); + \core\di::get(\core\hook\manager::class)->dispatch($hook); + $customoptions = $hook->get_options(); + + if (isset($customoptions[$value])) { + return (string)$customoptions[$value]; + } + + return $value; + } } diff --git a/public/lib/tests/hub/registration_test.php b/public/lib/tests/hub/registration_test.php index c3ddab8f569..3a7fc9b4a2e 100644 --- a/public/lib/tests/hub/registration_test.php +++ b/public/lib/tests/hub/registration_test.php @@ -197,4 +197,44 @@ final class registration_test extends \advanced_testcase { $this->assertEquals(get_string('time_range', 'hub'), $timerange['label']); $this->assertTrue(!empty($timerange['values'])); } + + /** + * Test getting the title for the defaulthomepage setting value. + * + * @covers \core\hub\registration::get_defaulthomepage_name + */ + public function test_get_defaulthomepage_name(): void { + $this->resetAfterTest(); + + // Test HOMEPAGE_SITE constant. + $result = registration::get_defaulthomepage_name(HOMEPAGE_SITE); + $this->assertEquals(get_string('home'), $result); + + // Test HOMEPAGE_MY constant. + $result = registration::get_defaulthomepage_name(HOMEPAGE_MY); + $this->assertEquals(get_string('mymoodle', 'admin'), $result); + + // Test HOMEPAGE_USER constant. + $result = registration::get_defaulthomepage_name(HOMEPAGE_USER); + $this->assertEquals(get_string('userpreference', 'admin'), $result); + + // Test HOMEPAGE_MYCOURSES constant. + $result = registration::get_defaulthomepage_name(HOMEPAGE_MYCOURSES); + $this->assertEquals(get_string('mycourses', 'admin'), $result); + + // Test custom homepage option via hook. + $customurl = '/local/customhomepage/landing.php'; + $customtitle = 'Custom landing page'; + $callback = function (\core_user\hook\extend_default_homepage $hook) use ($customurl, $customtitle) { + $hook->add_option(new \core\url($customurl), $customtitle); + }; + $this->redirectHook(\core_user\hook\extend_default_homepage::class, $callback); + + $result = registration::get_defaulthomepage_name($customurl); + $this->assertEquals($customtitle, $result); + + // Test unknown URL. + $result = registration::get_defaulthomepage_name('/unknown/page'); + $this->assertEquals('/unknown/page', $result); + } }