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 |
namespace communication_matrix;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* class matrix_user_manager to handle specific actions.
|
|
|
21 |
*
|
|
|
22 |
* @package communication_matrix
|
|
|
23 |
* @copyright 2023 Stevani Andolo <stevani.andolo@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class matrix_user_manager {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Prefix for Matrix usernames when they are detected as numeric.
|
|
|
30 |
*/
|
|
|
31 |
const MATRIX_USER_PREFIX = 'user';
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Gets matrix user id from moodle.
|
|
|
35 |
*
|
|
|
36 |
* @param int $userid Moodle user id
|
|
|
37 |
* @return string|null
|
|
|
38 |
*/
|
|
|
39 |
public static function get_matrixid_from_moodle(
|
|
|
40 |
int $userid,
|
|
|
41 |
): ?string {
|
|
|
42 |
self::load_requirements();
|
|
|
43 |
$field = profile_user_record($userid);
|
|
|
44 |
$matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
|
|
|
45 |
|
|
|
46 |
if ($matrixprofilefield === false) {
|
|
|
47 |
return null;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
return $field->{$matrixprofilefield} ?? null;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Get a qualified matrix user id based on a Moodle username.
|
|
|
55 |
*
|
|
|
56 |
* @param string $username The moodle username to turn into a Matrix username
|
|
|
57 |
* @return string
|
|
|
58 |
*/
|
|
|
59 |
public static function get_formatted_matrix_userid(
|
|
|
60 |
string $username,
|
|
|
61 |
): string {
|
|
|
62 |
$username = preg_replace('/[@#$%^&*()+{}|<>?!,]/i', '.', $username);
|
|
|
63 |
$username = ltrim(rtrim($username, '.'), '.');
|
|
|
64 |
|
|
|
65 |
// Matrix/Synapse servers will not allow numeric usernames.
|
|
|
66 |
if (is_numeric($username)) {
|
|
|
67 |
$username = self::MATRIX_USER_PREFIX . $username;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
$homeserver = self::get_formatted_matrix_home_server();
|
|
|
71 |
|
|
|
72 |
return "@{$username}:{$homeserver}";
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Add user's Matrix user id.
|
|
|
77 |
*
|
|
|
78 |
* @param int $userid Moodle user id
|
|
|
79 |
* @param string $matrixuserid Matrix user id
|
|
|
80 |
*/
|
|
|
81 |
public static function set_matrix_userid_in_moodle(
|
|
|
82 |
int $userid,
|
|
|
83 |
string $matrixuserid,
|
|
|
84 |
): void {
|
|
|
85 |
$matrixprofilefield = self::get_profile_field_name();
|
|
|
86 |
$field = profile_get_custom_field_data_by_shortname($matrixprofilefield);
|
|
|
87 |
|
|
|
88 |
if ($field === null) {
|
|
|
89 |
return;
|
|
|
90 |
}
|
|
|
91 |
$userinfodata = (object) [
|
|
|
92 |
'id' => $userid,
|
|
|
93 |
'data' => $matrixuserid,
|
|
|
94 |
'fieldid' => $field->id,
|
|
|
95 |
"profile_field_{$matrixprofilefield}" => $matrixuserid,
|
|
|
96 |
];
|
|
|
97 |
profile_save_data($userinfodata);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Sets home server for user matrix id
|
|
|
102 |
*
|
|
|
103 |
* @return string
|
|
|
104 |
*/
|
|
|
105 |
public static function get_formatted_matrix_home_server(): string {
|
|
|
106 |
$homeserver = get_config('communication_matrix', 'matrixhomeserverurl');
|
|
|
107 |
if ($homeserver === false) {
|
|
|
108 |
throw new \moodle_exception('Unknown matrix homeserver url');
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$homeserver = parse_url($homeserver)['host'];
|
|
|
112 |
|
|
|
113 |
if (str_starts_with($homeserver, 'www.')) {
|
|
|
114 |
$homeserver = str_replace('www.', '', $homeserver);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
return $homeserver;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Insert "Communication" category and "matrixuserid" field.
|
|
|
122 |
*
|
|
|
123 |
* @return string
|
|
|
124 |
*/
|
|
|
125 |
public static function create_matrix_user_profile_fields(): string {
|
|
|
126 |
global $CFG, $DB;
|
|
|
127 |
|
|
|
128 |
require_once($CFG->dirroot . '/user/profile/definelib.php');
|
|
|
129 |
require_once($CFG->dirroot . '/user/profile/field/text/define.class.php');
|
|
|
130 |
|
|
|
131 |
// Check if communication category exists.
|
|
|
132 |
$categoryname = get_string('communication', 'core_communication');
|
|
|
133 |
$category = $DB->count_records('user_info_category', ['name' => $categoryname]);
|
|
|
134 |
|
|
|
135 |
if ($category < 1) {
|
|
|
136 |
$data = new \stdClass();
|
|
|
137 |
$data->sortorder = $DB->count_records('user_info_category') + 1;
|
|
|
138 |
$data->name = $categoryname;
|
|
|
139 |
$data->id = $DB->insert_record('user_info_category', $data);
|
|
|
140 |
|
|
|
141 |
$createdcategory = $DB->get_record('user_info_category', ['id' => $data->id]);
|
|
|
142 |
$categoryid = $createdcategory->id;
|
|
|
143 |
\core\event\user_info_category_created::create_from_category($createdcategory)->trigger();
|
|
|
144 |
} else {
|
|
|
145 |
$category = $DB->get_record('user_info_category', ['name' => $categoryname]);
|
|
|
146 |
$categoryid = $category->id;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
set_config('communication_category_field', $categoryname, 'core_communication');
|
|
|
150 |
|
|
|
151 |
// Check if matrixuserid exists in user_info_field table.
|
|
|
152 |
$matrixuserid = $DB->count_records('user_info_field', [
|
|
|
153 |
'shortname' => 'matrixuserid',
|
|
|
154 |
'categoryid' => $categoryid,
|
|
|
155 |
]);
|
|
|
156 |
|
|
|
157 |
if ($matrixuserid < 1) {
|
|
|
158 |
$profileclass = new \profile_define_text();
|
|
|
159 |
|
|
|
160 |
$data = (object) [
|
|
|
161 |
'shortname' => 'matrixuserid',
|
|
|
162 |
'name' => get_string('matrixuserid', 'communication_matrix'),
|
|
|
163 |
'datatype' => 'text',
|
|
|
164 |
'categoryid' => $categoryid,
|
|
|
165 |
'forceunique' => 1,
|
|
|
166 |
'visible' => 0,
|
|
|
167 |
'locked' => 1,
|
|
|
168 |
'param1' => 30,
|
|
|
169 |
'param2' => 2048,
|
|
|
170 |
];
|
|
|
171 |
|
|
|
172 |
$profileclass->define_save($data);
|
|
|
173 |
set_config('matrixuserid_field', 'matrixuserid', 'communication_matrix');
|
|
|
174 |
return 'matrixuserid';
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Get the profile field name, creating the profiel field if it does not exist.
|
|
|
180 |
*
|
|
|
181 |
* @return string
|
|
|
182 |
*/
|
|
|
183 |
protected static function get_profile_field_name(): string {
|
|
|
184 |
self::load_requirements();
|
|
|
185 |
$matrixprofilefield = get_config('communication_matrix', 'matrixuserid_field');
|
|
|
186 |
if ($matrixprofilefield === false) {
|
|
|
187 |
$matrixprofilefield = self::create_matrix_user_profile_fields();
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
return $matrixprofilefield;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Load requirements for profile field management.
|
|
|
195 |
*
|
|
|
196 |
* This is just a helper to keep loading legacy files isolated.
|
|
|
197 |
*/
|
|
|
198 |
protected static function load_requirements(): void {
|
|
|
199 |
global $CFG;
|
|
|
200 |
|
|
|
201 |
require_once("{$CFG->dirroot}/user/profile/lib.php");
|
|
|
202 |
}
|
|
|
203 |
}
|