diff --git a/admin/tests/behat/filter_users.feature b/admin/tests/behat/filter_users.feature index b9a03e5ee7bb5..75e2b422f129f 100644 --- a/admin/tests/behat/filter_users.feature +++ b/admin/tests/behat/filter_users.feature @@ -65,3 +65,20 @@ Feature: An administrator can filter user accounts by role, cohort and other pro And I should not see "User Two" And I should not see "User Three" And I should not see "User Four" + + Scenario: Filter user accounts by enrolled in any course + When I set the following fields to these values: + | id_anycourses | Yes | + And I press "Add filter" + Then I should see "User One" + And I should see "User Two" + And I should see "User Three" + And I should not see "User Four" + And I press "Remove all filters" + And I set the following fields to these values: + | id_anycourses | No | + And I press "Add filter" + And I should not see "User One" + And I should not see "User Two" + And I should not see "User Three" + And I should see "User Four" diff --git a/lang/en/filters.php b/lang/en/filters.php index 7c1b4695007fd..f178887b4d6a9 100644 --- a/lang/en/filters.php +++ b/lang/en/filters.php @@ -26,6 +26,7 @@ $string['addfilter'] = 'Add filter'; $string['anycategory'] = 'any category'; $string['anycourse'] = 'any course'; +$string['anycourses'] = 'Enrolled in any course'; $string['anyfield'] = 'any field'; $string['anyrole'] = 'any role'; $string['anyvalue'] = 'any value'; diff --git a/user/filters/anycourses.php b/user/filters/anycourses.php new file mode 100644 index 0000000000000..39022af9b3a67 --- /dev/null +++ b/user/filters/anycourses.php @@ -0,0 +1,49 @@ +. + +/** + * This is filter is used to see which students are enroled on any courses + * + * @package core_user + * @copyright 2014 Krister Viirsaar + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * User filter to distinguish users with no or any enroled courses. + * @copyright 2014 Krister Viirsaar + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class user_filter_anycourses extends user_filter_yesno { + + /** + * Returns the condition to be used with SQL + * + * @param array $data filter settings + * @return array sql string and $params + */ + public function get_sql_filter($data) { + $value = $data['value']; + + $not = $value ? '' : 'NOT'; + + return array("EXISTS ( SELECT userid FROM {user_enrolments} ) AND " . + " id $not IN ( SELECT userid FROM {user_enrolments} )", array()); + } +} + diff --git a/user/filters/lib.php b/user/filters/lib.php index 83852a28af845..2264671a607b3 100644 --- a/user/filters/lib.php +++ b/user/filters/lib.php @@ -31,6 +31,7 @@ require_once($CFG->dirroot.'/user/filters/globalrole.php'); require_once($CFG->dirroot.'/user/filters/profilefield.php'); require_once($CFG->dirroot.'/user/filters/yesno.php'); +require_once($CFG->dirroot.'/user/filters/anycourses.php'); require_once($CFG->dirroot.'/user/filters/cohort.php'); require_once($CFG->dirroot.'/user/filters/user_filter_forms.php'); require_once($CFG->dirroot.'/user/filters/checkbox.php'); @@ -63,10 +64,11 @@ public function __construct($fieldnames = null, $baseurl = null, $extraparams = } if (empty($fieldnames)) { - $fieldnames = array('realname' => 0, 'lastname' => 1, 'firstname' => 1, 'username' => 1, 'email' => 1, 'city' => 1, 'country' => 1, - 'confirmed' => 1, 'suspended' => 1, 'profile' => 1, 'courserole' => 1, 'systemrole' => 1, - 'cohort' => 1, 'firstaccess' => 1, 'lastaccess' => 1, 'neveraccessed' => 1, 'timemodified' => 1, - 'nevermodified' => 1, 'auth' => 1, 'mnethostid' => 1, 'idnumber' => 1); + $fieldnames = array('realname' => 0, 'lastname' => 1, 'firstname' => 1, 'username' => 1, 'email' => 1, 'city' => 1, + 'country' => 1, 'confirmed' => 1, 'suspended' => 1, 'profile' => 1, 'courserole' => 1, + 'anycourses' => 1, 'systemrole' => 1, 'cohort' => 1, 'firstaccess' => 1, 'lastaccess' => 1, + 'neveraccessed' => 1, 'timemodified' => 1, 'nevermodified' => 1, 'auth' => 1, 'mnethostid' => 1, + 'idnumber' => 1); } $this->_fields = array(); @@ -142,6 +144,8 @@ public function get_field($fieldname, $advanced) { case 'suspended': return new user_filter_yesno('suspended', get_string('suspended', 'auth'), $advanced, 'suspended'); case 'profile': return new user_filter_profilefield('profile', get_string('profilefields', 'admin'), $advanced); case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced); + case 'anycourses': + return new user_filter_anycourses('anycourses', get_string('anycourses', 'filters'), $advanced, 'user_enrolments'); case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced); case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess'); case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess');