From 5813c7f7dcf74aec72a355fb7de918a124b5bb28 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Tue, 22 Nov 2011 16:16:23 +0000 Subject: [PATCH] MDL-30408 Allow modules the choice to not appear on 'Add a...' lists This is useful for modules which are created by the system and are not supposed to be created by users. There are various reasons for doing this in third-party modules. --- course/edit_form.php | 10 +++++++++- course/lib.php | 2 ++ lib/adminlib.php | 15 +++++++++++++-- lib/moodlelib.php | 2 ++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/course/edit_form.php b/course/edit_form.php index a5bad505cb8..d87c9d13275 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -270,7 +270,15 @@ class course_edit_form extends moodleform { } $mods = array(0=>get_string('allownone')); - $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name'); + $allmods = $DB->get_records_menu('modules', array('visible' => 1), + 'name', 'id, name'); + foreach ($allmods as $key => $value) { + // Add module to list unless it cannot be added by users anyway + if (plugin_supports('mod', $value, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER) !== + MOD_ARCHETYPE_SYSTEM) { + $mods[$key] = get_string('pluginname', $value); + } + } $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10')); $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0); // defaults are already in $course diff --git a/course/lib.php b/course/lib.php index c1864229d64..383eb53a0c5 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1853,6 +1853,8 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false, $archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER); if ($archetype == MOD_ARCHETYPE_RESOURCE) { $resources[$urlbase.$modname] = $modnamestr; + } else if ($archetype === MOD_ARCHETYPE_SYSTEM) { + // System modules cannot be added by user, do not add to dropdown } else { // all other archetypes are considered activity $activities[$urlbase.$modname] = $modnamestr; diff --git a/lib/adminlib.php b/lib/adminlib.php index 4eaaecb4ad7..a071fdf4eda 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -7896,6 +7896,8 @@ class admin_setting_devicedetectregex extends admin_setting { * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class admin_setting_configmultiselect_modules extends admin_setting_configmultiselect { + private $excludesystem; + /** * Calls parent::__construct - note array $choices is not required * @@ -7903,9 +7905,12 @@ class admin_setting_configmultiselect_modules extends admin_setting_configmultis * @param string $visiblename localised setting name * @param string $description setting description * @param array $defaultsetting a plain array of default module ids + * @param bool $excludesystem If true, excludes modules with 'system' archetype */ - public function __construct($name, $visiblename, $description, $defaultsetting = array()) { + public function __construct($name, $visiblename, $description, $defaultsetting = array(), + $excludesystem = true) { parent::__construct($name, $visiblename, $description, $defaultsetting, null); + $this->excludesystem = $excludesystem; } /** @@ -7922,8 +7927,14 @@ class admin_setting_configmultiselect_modules extends admin_setting_configmultis global $CFG, $DB; $records = $DB->get_records('modules', array('visible'=>1), 'name'); foreach ($records as $record) { + // Exclude modules if the code doesn't exist if (file_exists("$CFG->dirroot/mod/$record->name/lib.php")) { - $this->choices[$record->id] = $record->name; + // Also exclude system modules (if specified) + if (!($this->excludesystem && + plugin_supports('mod', $record->name, FEATURE_MOD_ARCHETYPE) === + MOD_ARCHETYPE_SYSTEM)) { + $this->choices[$record->id] = $record->name; + } } } return true; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 29142057ae1..2b62236e374 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -417,6 +417,8 @@ define('MOD_ARCHETYPE_OTHER', 0); define('MOD_ARCHETYPE_RESOURCE', 1); /** Assignment module archetype */ define('MOD_ARCHETYPE_ASSIGNMENT', 2); +/** System (not user-addable) module archetype */ +define('MOD_ARCHETYPE_SYSTEM', 3); /** * Security token used for allowing access