diff --git a/lang/en/hub.php b/lang/en/hub.php index a82ad1af2fc..24fd764aba4 100644 --- a/lang/en/hub.php +++ b/lang/en/hub.php @@ -89,6 +89,7 @@ $string['nohubselected'] = 'No hub selected'; $string['none'] = 'None'; $string['operation'] = 'Actions'; $string['participantnumberaverage'] = 'Average number of participants ({$a})'; +$string['pluginusagedata'] = 'Plugin usage data collected from the following pages: Plugin overview, Activities, and Blocks'; $string['policyagreed'] = 'Privacy notice and data processing agreement'; $string['policyagreeddesc'] = 'I agree to the Privacy notice and data processing agreement'; $string['postaladdress'] = 'Postal address'; diff --git a/lib/classes/hub/registration.php b/lib/classes/hub/registration.php index 0b1cc88037d..16f0fb8e03e 100644 --- a/lib/classes/hub/registration.php +++ b/lib/classes/hub/registration.php @@ -30,6 +30,7 @@ use moodle_url; use context_system; use stdClass; use html_writer; +use core_plugin_manager; /** * Methods to use when registering the site at the moodle sites directory. @@ -62,6 +63,8 @@ class registration { 2020022600 => ['activeusers', 'activeparticipantnumberaverage'], // Database type, course date info, site theme, primary auth type added in Moodle 4.2. 2023021700 => ['dbtype', 'coursesnodates', 'sitetheme', 'primaryauthtype'], + // Plugin usage added in Moodle 4.5. + 2023072300 => ['pluginusage'], ]; /** @var string Site privacy: not displayed */ @@ -185,6 +188,7 @@ class registration { $siteinfo['dbtype'] = $CFG->dbtype; $siteinfo['coursesnodates'] = $DB->count_records_select('course', 'enddate = ?', [0]) - 1; $siteinfo['sitetheme'] = get_config('core', 'theme'); + $siteinfo['pluginusage'] = json_encode(self::get_plugin_usage_data()); // Primary auth type. $primaryauthsql = 'SELECT auth, count(auth) as tc FROM {user} GROUP BY auth ORDER BY tc DESC'; @@ -238,6 +242,11 @@ class registration { if (preg_match('/^(\d+\.\d.*?)[\. ]/', $moodlerelease, $matches)) { $moodlerelease = $matches[1]; } + $pluginusagelinks = [ + 'overview' => new moodle_url('/admin/plugins.php'), + 'activities' => new moodle_url('/admin/modules.php'), + 'blocks' => new moodle_url('/admin/blocks.php'), + ]; $senddata = [ 'moodlerelease' => get_string('sitereleasenum', 'hub', $moodlerelease), 'courses' => get_string('coursesnumber', 'hub', $siteinfo['courses']), @@ -267,6 +276,7 @@ class registration { 'coursesnodates' => get_string('coursesnodates', 'hub', $siteinfo['coursesnodates']), 'sitetheme' => get_string('sitetheme', 'hub', $siteinfo['sitetheme']), 'primaryauthtype' => get_string('primaryauthtype', 'hub', $siteinfo['primaryauthtype']), + 'pluginusage' => get_string('pluginusagedata', 'hub', $pluginusagelinks), ]; foreach ($senddata as $key => $str) { @@ -608,4 +618,40 @@ class registration { redirect(new moodle_url('/admin/registration/index.php', ['returnurl' => $returnurl->out_as_local_url(false)])); } } + + /** + * Return a list of plugins. + * + * Only blocks and activities will include instance counts. + * + * @return array + */ + public static function get_plugin_usage_data(): array { + global $DB; + + $pluginman = core_plugin_manager::instance(); + $plugininfo = $pluginman->get_plugins(); + $data = []; + + foreach ($plugininfo as $plugins) { + foreach ($plugins as $plugin) { + // Plugins are considered enabled if $plugin->is_enabled() returns true or null. + // Plugins that return null cannot be disabled. + $enabled = ($plugin->is_enabled() || is_null($plugin->is_enabled())); + $data[$plugin->type][$plugin->name]['enabled'] = $enabled ? 1 : 0; + + if ($plugin->type === 'mod') { + $mid = $DB->get_field('modules', 'id', ['name' => $plugin->name]); + $count = $DB->count_records('course_modules', ['module' => $mid]); + $data[$plugin->type][$plugin->name]['count'] = $count; + + } else if ($plugin->type === 'block') { + $count = $DB->count_records('block_instances', ['blockname' => $plugin->name]); + $data[$plugin->type][$plugin->name]['count'] = $count; + } + } + } + + return $data; + } } diff --git a/lib/tests/hub/registration_test.php b/lib/tests/hub/registration_test.php index d0ab3885d8a..e7acddcfdaf 100644 --- a/lib/tests/hub/registration_test.php +++ b/lib/tests/hub/registration_test.php @@ -22,15 +22,12 @@ namespace core\hub; * @package core * @copyright 2023 Matt Porritt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * + * @covers \core\hub\registration */ class registration_test extends \advanced_testcase { /** * Test getting site registration information. - * - * return void - * @covers \core\hub\registration */ public function test_get_site_info(): void { global $CFG; @@ -50,4 +47,42 @@ class registration_test extends \advanced_testcase { $this->assertEquals('manual', $siteinfo['primaryauthtype']); $this->assertEquals(1, $siteinfo['coursesnodates']); } + + /** + * Test getting the plugin usage data. + */ + public function test_get_plugin_usage(): void { + global $DB; + $this->resetAfterTest(); + + // Create some courses with end dates. + $generator = $this->getDataGenerator(); + $course = $generator->create_course(); + + // Create some assignments. + $generator->create_module('assign', ['course' => $course->id]); + $generator->create_module('assign', ['course' => $course->id]); + $generator->create_module('assign', ['course' => $course->id]); + + // Create some quizzes. + $generator->create_module('quiz', ['course' => $course->id]); + $generator->create_module('quiz', ['course' => $course->id]); + + // Add some blocks. + $generator->create_block('online_users'); + $generator->create_block('online_users'); + $generator->create_block('online_users'); + $generator->create_block('online_users'); + + // Disabled a plugin. + $DB->set_field('modules', 'visible', 0, ['name' => 'feedback']); + + // Check our plugin usage counts and enabled states are correct. + $pluginusage = registration::get_plugin_usage_data(); + $this->assertEquals(3, $pluginusage['mod']['assign']['count']); + $this->assertEquals(2, $pluginusage['mod']['quiz']['count']); + $this->assertEquals(4, $pluginusage['block']['online_users']['count']); + $this->assertEquals(0, $pluginusage['mod']['feedback']['enabled']); + $this->assertEquals(1, $pluginusage['mod']['assign']['enabled']); + } }