Permalink
Browse files

MDL-27123 adds a new admin_setting_configmultiselect_modules class

and uses it on the module security page. This saves one DB query per
page for users who can see the admin tree in the settings nav.
  • Loading branch information...
1 parent 91787c3 commit 7351b54e86a408354e66f885c1561b8cb904032b John Beedell committed with timhunt Apr 11, 2011
Showing with 38 additions and 8 deletions.
  1. +3 −8 admin/settings/security.php
  2. +35 −0 lib/adminlib.php
@@ -91,14 +91,9 @@
'all' => get_string('fulllistofcourses'),
'requested' => get_string('requestedcourses'))));
$temp->add(new admin_setting_configcheckbox('restrictbydefault', get_string('restrictbydefault', 'admin'), get_string('configrestrictbydefault', 'admin'), 0));
- if (!$options = $DB->get_records('modules')) {
- $options = array();
- }
- $options2 = array();
- foreach ($options as $option) {
- $options2[$option->id] = $option->name;
- }
- $temp->add(new admin_setting_configmultiselect('defaultallowedmodules', get_string('defaultallowedmodules', 'admin'), get_string('configdefaultallowedmodules', 'admin'), array(), $options2));
+ $temp->add(new admin_setting_configmultiselect_modules('defaultallowedmodules',
+ get_string('defaultallowedmodules', 'admin'),
+ get_string('configdefaultallowedmodules', 'admin')));
$ADMIN->add('security', $temp);
View
@@ -7309,3 +7309,38 @@ public function output_html($data, $query = '') {
}
}
+
+/**
+ * Multiselect for current modules
+ *
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class admin_setting_configmultiselect_modules extends admin_setting_configmultiselect {
+/**
+ * Calls parent::__construct - note array $choices is not required
+ */
+ public function __construct($name, $visiblename, $description) {
+ parent::__construct($name, $visiblename, $description, array(), null);
+ }
+
+ /**
+ * Loads an array of current module choices
+ *
+ * @return bool always return true
+ */
+ public function load_choices() {
+ if (is_array($this->choices)) {
+ return true;
+ }
+ $this->choices = array();
+
+ global $CFG, $DB;
+ $records = $DB->get_records('modules', array('visible'=>1), 'name');
+ foreach ($records as $record) {
+ if (file_exists("$CFG->dirroot/mod/$record->name/lib.php")) {
+ $this->choices[$record->id] = $record->name;
+ }
+ }
+ return true;
+ }
+}

0 comments on commit 7351b54

Please sign in to comment.