1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Class users_data_source.
|
|
|
19 |
*
|
|
|
20 |
* @package block_dash
|
|
|
21 |
* @copyright 2019 bdecent gmbh <https://bdecent.de>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace block_dash\local\data_source;
|
|
|
26 |
|
|
|
27 |
use block_dash\local\block_builder;
|
|
|
28 |
use block_dash\local\dash_framework\query_builder\builder;
|
|
|
29 |
use block_dash\local\dash_framework\query_builder\join;
|
|
|
30 |
use block_dash\local\dash_framework\structure\table;
|
|
|
31 |
use block_dash\local\dash_framework\structure\user_table;
|
|
|
32 |
use block_dash\local\data_grid\filter\current_course_participants_condition;
|
|
|
33 |
use block_dash\local\data_grid\filter\date_filter;
|
|
|
34 |
use block_dash\local\data_grid\filter\filter;
|
|
|
35 |
use block_dash\local\data_grid\filter\group_filter;
|
|
|
36 |
use block_dash\local\data_grid\filter\logged_in_user_condition;
|
|
|
37 |
use block_dash\local\data_grid\filter\filter_collection;
|
|
|
38 |
use block_dash\local\data_grid\filter\filter_collection_interface;
|
|
|
39 |
use block_dash\local\data_grid\filter\my_groups_condition;
|
|
|
40 |
use block_dash\local\data_grid\filter\participants_condition;
|
|
|
41 |
use block_dash\local\data_grid\filter\user_field_filter;
|
|
|
42 |
use block_dash\local\data_grid\filter\user_profile_field_filter;
|
|
|
43 |
use block_dash\local\data_grid\filter\current_course_condition;
|
|
|
44 |
use block_dash\local\data_grid\filter\bool_filter;
|
|
|
45 |
use coding_exception;
|
|
|
46 |
use context;
|
|
|
47 |
/**
|
|
|
48 |
* Class users_data_source.
|
|
|
49 |
*
|
|
|
50 |
* @package block_dash
|
|
|
51 |
*/
|
|
|
52 |
class users_data_source extends abstract_data_source {
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Constructor.
|
|
|
56 |
*
|
|
|
57 |
* @param context $context
|
|
|
58 |
*/
|
|
|
59 |
public function __construct(context $context) {
|
|
|
60 |
$this->add_table(new user_table());
|
|
|
61 |
|
|
|
62 |
parent::__construct($context);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Get human readable name of data source.
|
|
|
67 |
*
|
|
|
68 |
* @return string
|
|
|
69 |
* @throws coding_exception
|
|
|
70 |
*/
|
|
|
71 |
public function get_name() {
|
|
|
72 |
return get_string('users');
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Return query template for retrieving user info.
|
|
|
77 |
*
|
|
|
78 |
* @return builder
|
|
|
79 |
* @throws coding_exception
|
|
|
80 |
*/
|
|
|
81 |
public function get_query_template(): builder {
|
|
|
82 |
global $CFG, $DB;
|
|
|
83 |
|
|
|
84 |
require_once("$CFG->dirroot/user/profile/lib.php");
|
|
|
85 |
|
|
|
86 |
$builder = new builder();
|
|
|
87 |
$builder
|
|
|
88 |
->select('u.id', 'u_id')
|
|
|
89 |
->from('user', 'u')
|
|
|
90 |
->join('user_enrolments', 'ue', 'userid', 'u.id', join::TYPE_LEFT_JOIN)
|
|
|
91 |
->join('enrol', 'e', 'id', 'ue.enrolid', join::TYPE_LEFT_JOIN)
|
|
|
92 |
->join('course', 'c', 'id', 'e.courseid', join::TYPE_LEFT_JOIN)
|
|
|
93 |
->join('groups_members', 'gm', 'userid', 'u.id', join::TYPE_LEFT_JOIN)
|
|
|
94 |
->join('groups', 'g', 'id', 'gm.groupid', join::TYPE_LEFT_JOIN);
|
|
|
95 |
|
|
|
96 |
foreach (profile_get_custom_fields() as $field) {
|
|
|
97 |
$alias = 'u_pf_' . strtolower($field->shortname);
|
|
|
98 |
|
|
|
99 |
$builder
|
|
|
100 |
->join('user_info_data', $alias, 'userid', 'u.id', join::TYPE_LEFT_JOIN)
|
|
|
101 |
->join_condition($alias, "$alias.fieldid = $field->id");
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
$builder->where('u.deleted', [0]);
|
|
|
105 |
|
|
|
106 |
return $builder;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Group by columns.
|
|
|
111 |
*
|
|
|
112 |
* @return string
|
|
|
113 |
*/
|
|
|
114 |
public function get_groupby() {
|
|
|
115 |
return false;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Build and return filter collection.
|
|
|
120 |
*
|
|
|
121 |
* @return filter_collection_interface
|
|
|
122 |
* @throws coding_exception
|
|
|
123 |
*/
|
|
|
124 |
public function build_filter_collection() {
|
|
|
125 |
global $CFG;
|
|
|
126 |
|
|
|
127 |
require_once("$CFG->dirroot/user/profile/lib.php");
|
|
|
128 |
|
|
|
129 |
$filtercollection = new filter_collection(get_class($this), $this->get_context());
|
|
|
130 |
|
|
|
131 |
$filtercollection->add_filter(new group_filter('group', 'gm100.groupid'));
|
|
|
132 |
|
|
|
133 |
$filtercollection->add_filter(new user_field_filter('u_department', 'u.department', 'department',
|
|
|
134 |
get_string('department')));
|
|
|
135 |
$filtercollection->add_filter(new user_field_filter('u_institution', 'u.institution', 'institution',
|
|
|
136 |
get_string('institution')));
|
|
|
137 |
|
|
|
138 |
$filter = new date_filter('u_lastlogin', 'u.lastlogin', date_filter::DATE_FUNCTION_FLOOR,
|
|
|
139 |
get_string('lastlogin'));
|
|
|
140 |
$filter->set_operation(filter::OPERATION_GREATER_THAN_EQUAL);
|
|
|
141 |
$filtercollection->add_filter($filter);
|
|
|
142 |
|
|
|
143 |
$filter = new date_filter('u_firstaccess', 'u.firstaccess', date_filter::DATE_FUNCTION_FLOOR,
|
|
|
144 |
get_string('firstaccess'));
|
|
|
145 |
$filter->set_operation(filter::OPERATION_GREATER_THAN_EQUAL);
|
|
|
146 |
$filtercollection->add_filter($filter);
|
|
|
147 |
|
|
|
148 |
$filtercollection->add_filter(new logged_in_user_condition('current_user', 'u.id'));
|
|
|
149 |
$filtercollection->add_filter(new participants_condition('participants', 'u.id'));
|
|
|
150 |
$filtercollection->add_filter(new my_groups_condition('my_groups', 'gm300.groupid'));
|
|
|
151 |
$filtercollection->add_filter(new current_course_condition('current_course', 'c.id'));
|
|
|
152 |
|
|
|
153 |
if (block_dash_has_pro()) {
|
|
|
154 |
$filtercollection->add_filter(new \local_dash\data_grid\filter\parent_role_condition('parentrole', 'u.id'));
|
|
|
155 |
$filtercollection->add_filter(new \local_dash\data_grid\filter\cohort_condition('cohort', 'u.id'));
|
|
|
156 |
$filtercollection->add_filter(new \local_dash\data_grid\filter\users_mycohort_condition('users_mycohort', 'u.id'));
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
foreach (profile_get_custom_fields() as $field) {
|
|
|
160 |
$alias = 'u_pf_' . strtolower($field->shortname);
|
|
|
161 |
$select = $alias . '.data';
|
|
|
162 |
switch ($field->datatype) {
|
|
|
163 |
case 'checkbox':
|
|
|
164 |
$definitions[] = new bool_filter($alias, $select, $field->name);
|
|
|
165 |
break;
|
|
|
166 |
case 'datetime':
|
|
|
167 |
$filtercollection->add_filter(new date_filter($alias, $select, date_filter::DATE_FUNCTION_FLOOR,
|
|
|
168 |
$field->name));
|
|
|
169 |
break;
|
|
|
170 |
case 'textarea':
|
|
|
171 |
break;
|
|
|
172 |
default:
|
|
|
173 |
$filter = new user_profile_field_filter($alias, $alias . '.data', $field->id, $field->name);
|
|
|
174 |
$filter->set_label(format_string($field->name));
|
|
|
175 |
$filtercollection->add_filter($filter);
|
|
|
176 |
break;
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
return $filtercollection;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Set the default preferences of the User datasource, force the set the default settings.
|
|
|
185 |
*
|
|
|
186 |
* @param array $data
|
|
|
187 |
* @return array
|
|
|
188 |
*/
|
|
|
189 |
public function set_default_preferences(&$data) {
|
|
|
190 |
$configpreferences = $data['config_preferences'];
|
|
|
191 |
$configpreferences['available_fields']['u_firstname']['visible'] = true;
|
|
|
192 |
$configpreferences['available_fields']['u_lastname']['visible'] = true;
|
|
|
193 |
$configpreferences['available_fields']['u_email']['visible'] = true;
|
|
|
194 |
$configpreferences['available_fields']['u_lastlogin']['visible'] = true;
|
|
|
195 |
$data['config_preferences'] = $configpreferences;
|
|
|
196 |
}
|
|
|
197 |
}
|