]> git.mjollnir.org Git - moodle.git/commitdiff
course requests: MDL-6160 clean up admin option for choosing who is notified. Merged...
authortjhunt <tjhunt>
Thu, 8 Jan 2009 07:07:00 +0000 (07:07 +0000)
committertjhunt <tjhunt>
Thu, 8 Jan 2009 07:07:00 +0000 (07:07 +0000)
Also, change the 'notifyloginfailures' setting to use the same sort of admin setting type.

admin/settings/courses.php
admin/settings/security.php
course/request.php
lang/en_utf8/admin.php
lib/adminlib.php
lib/db/upgrade.php
lib/moodlelib.php
version.php

index c8e545564cb5aea93cdebf352e37879b74ea8c1f..e95bb448eae146bee43a626094d8a8c6894f83b6 100644 (file)
@@ -122,7 +122,7 @@ if ($hassiteconfig
     $temp = new admin_settingpage('courserequest', get_string('courserequest'));
     $temp->add(new admin_setting_configcheckbox('enablecourserequests', get_string('enablecourserequests', 'admin'), get_string('configenablecourserequests', 'admin'), 0));
     $temp->add(new admin_settings_coursecat_select('defaultrequestcategory', get_string('defaultrequestcategory', 'admin'), get_string('configdefaultrequestcategory', 'admin'), 1));
-    $temp->add(new admin_setting_configtext('courserequestnotify', get_string('courserequestnotify', 'admin'), get_string('configcourserequestnotify', 'admin'), ''));
+    $temp->add(new admin_setting_users_with_capability('courserequestnotify', get_string('courserequestnotify', 'admin'), get_string('configcourserequestnotify2', 'admin'), array(), 'moodle/site:approvecourse'));
     $ADMIN->add('courses', $temp);
 
 /// Pending course requests.
index c603abe98365afd03967444346269aab30cba0d5..77eba67fe461852db5df8d6b2b84c03eceb7f99f 100644 (file)
@@ -90,9 +90,7 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page
                                                                                                                                                                                 'admin' => get_string('administrators'),
                                                                                                                                                                                 'teacher' => get_string('administratorsandteachers'),
                                                                                                                                                                                 'everybody' => get_string('everybody'))));
-    $temp->add(new admin_setting_configselect('notifyloginfailures', get_string('notifyloginfailures', 'admin'), get_string('confignotifyloginfailures', 'admin'), '', array('' => get_string('nobody'),
-                                                                                                                                                                             'mainadmin' => get_string('administrator'),
-                                                                                                                                                                             'alladmins' => get_string('administratorsall'))));
+    $temp->add(new admin_setting_users_with_capability('notifyloginfailures', get_string('notifyloginfailures', 'admin'), get_string('confignotifyloginfailures', 'admin'), array(), 'moodle/site:config'));
     $options = array();
     for ($i = 1; $i <= 100; $i++) {
         $options[$i] = $i;
index 86dcfc66d8e8874ae4dc53f6ee377dfac9ecb9eb..aaa8050954d79a07882c436886d636c3d679a933 100644 (file)
@@ -68,8 +68,8 @@
 
     /// Notify the admin if required.
         if ($CFG->courserequestnotify) {
-            if ($user = $user = $DB->get_record('user', array('username' => $CFG->courserequestnotify,
-                    'mnethostid' => $CFG->mnet_localhost_id))) {
+            $users = get_users_from_config($CFG->courserequestnotify, 'moodle/site:approvecourse');
+            foreach ($users as $user) {
                 $eventdata = new object();
                 $eventdata->modulename        = 'moodle';
                 $eventdata->component         = 'course';
index 93efdcd317d3ce321fe5820e1baa8da782dc8b07..1f6d2f482ac9c5aa7bf1b47fd2f1ed18c72c9011 100644 (file)
@@ -91,6 +91,7 @@ $string['configcookiesecure'] = 'If server is accepting only https connections i
 $string['configcountry'] = 'If you set a country here, then this country will be selected by default on new user accounts.  To force users to choose a country, just leave this unset.';
 $string['configcoursemanager'] = 'This setting allows you to control who appears on the course description. Users need to have at least one of these roles in a course to be shown on the course description for that course.';
 $string['configcourserequestnotify'] = 'Type username of user to be notified when new course requested.';
+$string['configcourserequestnotify2'] = 'Users who will be notified when a course is requested. Only users who can approve course requests are listed here.';
 $string['configcoursesperpage'] = 'Enter the number of courses to be display per page in a course listing.';
 $string['configcreatornewroleid'] = 'This role is automatically assigned to creators in new courses they created. This role is not assigned if creator already has needed capabilities in parent context.';
 $string['configdbsessions'] = 'If enabled, this setting will use the database to store information about current sessions.  This is especially useful for large/busy sites or sites built on cluster of servers.  For most sites this should probably be left disabled so that the server disk is used instead.  Note that changing this setting now will log out all current users (including you). If you are using MySQL please make sure that \'max_allowed_packet\' in my.cnf (or my.ini) is at least 4M.';
@@ -387,6 +388,7 @@ $string['errordeletingconfig'] = 'An error occurred while deleting the configura
 $string['errors'] = 'Errors';
 $string['errorsetting'] = 'Could not save setting:';
 $string['errorwithsettings'] = 'Some settings were not changed due to an error.';
+$string['everyonewhocan'] = 'Everyone who can \'$a\'';
 $string['exceptions'] = 'exceptions';
 $string['experimental'] = 'Experimental';
 $string['experimentalsettings'] = 'Experimental settings';
index f62b0dac722cfd0b832585211c954d3aa38057ee..4bde5306f072ac72c496a87abf0bef1fe57e8fad 100644 (file)
@@ -3292,8 +3292,9 @@ class admin_setting_configmultiselect extends admin_setting_configselect {
         }
 
         $defaults = array();
+        $size = min(10, count($this->choices));
         $return = '<div class="form-select"><input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected
-        $return .= '<select id="'.$this->get_id().'" name="'.$this->get_full_name().'[]" size="10" multiple="multiple">';
+        $return .= '<select id="'.$this->get_id().'" name="'.$this->get_full_name().'[]" size="'.$size.'" multiple="multiple">';
         foreach ($this->choices as $key => $description) {
             if (in_array($key, $data)) {
                 $selected = 'selected="selected"';
@@ -3411,6 +3412,71 @@ class admin_setting_configiplist extends admin_setting_configtextarea {
     }
 }
 
+/**
+ * An admin setting for selecting one or more users, who have a particular capability
+ * in the system context. Warning, make sure the list will never be too long. There is
+ * no paging or searching of this list.
+ *
+ * To correctly get a list of users from this config setting, you need to call the
+ * get_users_from_config($CFG->mysetting, $capability); function in moodlelib.php.
+ */
+class admin_setting_users_with_capability extends admin_setting_configmultiselect {
+    protected $capability;
+
+    /**
+     * Constructor.
+     *
+     * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
+     * @param string $visiblename localised name
+     * @param string $description localised long description
+     * @param array $defaultsetting array of usernames
+     * @param string $capability string capability name.
+     */
+    function __construct($name, $visiblename, $description, $defaultsetting, $capability) {
+        $users = get_users_by_capability(get_context_instance(CONTEXT_SYSTEM),
+                $capability, 'username,firstname,lastname', 'lastname,firstname');
+        $choices = array(
+            '$@NONE@$' => get_string('nobody'),
+            '$@ALL@$' => get_string('everyonewhocan', 'admin', get_capability_string($capability)),
+        );
+        foreach ($users as $user) {
+            $choices[$user->username] = fullname($user);
+        }
+        parent::admin_setting_configmultiselect($name, $visiblename, $description, $defaultsetting, $choices);
+    }
+
+    function get_defaultsetting() {
+        $this->load_choices();
+        if (empty($this->defaultsetting)) {
+            return array('$@NONE@$');
+        } else if (array_key_exists($this->defaultsetting, $this->choices)) {
+            return $this->defaultsetting;
+        } else {
+            return '';
+        }
+    }
+
+    function get_setting() {
+        $result = parent::get_setting();
+        if (empty($result)) {
+            $result = array('$@NONE@$');
+        }
+        return $result;
+    }
+
+    function write_setting($data) {
+        // If all is selected, remove any explicit options.
+        if (in_array('$@ALL@$', $data)) {
+            $data = array('$@ALL@$');
+        }
+        // None never needs to be writted to the DB.
+        if (in_array('$@NONE@$', $data)) {
+            unset($data[array_search('$@NONE@$', $data)]);
+        }
+        return parent::write_setting($data);
+    }
+}
+
 /**
  * Special checkbox for calendar - resets SESSION vars.
  */
index abb89504d3dc20048dedffe832af9e425daf9fdd..a2525dfc06d3ec285d26e53e9bd2326c3f195929 100644 (file)
@@ -1295,6 +1295,20 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint($result, 2009010606);
     }
 
+    if ($result && $oldversion < 2009010800) {
+    /// Update the notifyloginfailures setting.
+        if ($CFG->notifyloginfailures == 'mainadmin') {
+            set_config('notifyloginfailures', get_admin()->username);
+        } else if ($CFG->notifyloginfailures == 'alladmins') {
+            set_config('notifyloginfailures', '$@ALL@$');
+        } else {
+            set_config('notifyloginfailures', '');
+        }
+
+    /// Main savepoint reached
+        upgrade_main_savepoint($result, 2009010800);
+    }
+
     return $result;
 }
 
index f8de272d0da543d03a10f9c6bdae1093bbeb54ef..f9e3f23e29e387933e8ae5eecf9d852666f0c151 100644 (file)
@@ -818,6 +818,24 @@ function unset_all_config_for_plugin($plugin) {
     return $success;
 }
 
+/**
+ * Use this funciton to get a list of users from a config setting of type admin_setting_users_with_capability.
+ * @param string $value the value of the config setting. 
+ * @param string $capability the capability - must match the one passed to the admin_setting_users_with_capability constructor.
+ * @return array of user objects.
+ */
+function get_users_from_config($value, $capability) {
+    global $CFG;
+    if ($value == '$@ALL@$') {
+        $users = get_users_by_capability(get_context_instance(CONTEXT_SYSTEM), $capability);
+    } else {
+        list($where, $params) = $DB->get_in_or_equal(explode(',', $CFG->courserequestnotify));
+        $params[] = $CFG->mnet_localhost_id;
+        $users = $DB->get_records_select('user', 'username ' . $where . ' AND mnethostid = ?', $params);
+    }
+    return $users;
+}
+
 /**
  * Get volatile flags
  *
@@ -6556,14 +6574,7 @@ function upgrade_set_timeout($max_execution_time=300) {
 function notify_login_failures() {
     global $CFG, $DB;
 
-    switch ($CFG->notifyloginfailures) {
-        case 'mainadmin' :
-            $recip = array(get_admin());
-            break;
-        case 'alladmins':
-            $recip = get_admins();
-            break;
-    }
+    $recip = get_users_from_config($CFG->notifyloginfailures, 'moodle/site:config');
 
     if (empty($CFG->lastnotifyfailure)) {
         $CFG->lastnotifyfailure=0;
index 83272855c49eec9e45e38212ffebe4fd0f99af05..9ea71e3aade0f4455221c70d7163d8ea30d0256c 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2009010606;  // YYYYMMDD   = date of the last version bump
+    $version = 2009010800;  // YYYYMMDD   = date of the last version bump
                             //         XX = daily increments
 
     $release = '2.0 dev (Build: 20090108)';  // Human-friendly version name