Skip to content

Commit

Permalink
MDL-44646 administration: Users can be filtered by course enrollment
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Jun 20, 2018
1 parent 9e7c397 commit 78f6d02
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
17 changes: 17 additions & 0 deletions admin/tests/behat/filter_users.feature
Expand Up @@ -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"
1 change: 1 addition & 0 deletions lang/en/filters.php
Expand Up @@ -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';
Expand Down
49 changes: 49 additions & 0 deletions user/filters/anycourses.php
@@ -0,0 +1,49 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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());
}
}

12 changes: 8 additions & 4 deletions user/filters/lib.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 78f6d02

Please sign in to comment.