forked from EsupPortail/esup-mod_rocketchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
225 lines (211 loc) · 9.46 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?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/>.
/**
* Library of interface functions and constants.
*
* @package mod_rocketchat
* @copyright 2020 ESUP-Portail {@link https://www.esup-portail.org/}
* @author Céline Pervès<cperves@unistra.fr>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot. '/mod/rocketchat/locallib.php');
use \mod_rocketchat\api\manager\rocket_chat_api_manager;
/**
* Return if the plugin supports $feature.
*
* @param string $feature Constant representing theadd feature.
* @return true | null True if the feature is supported, null otherwise.
*/
function rocketchat_supports($feature) {
if (!$feature) {
return null;
}
$features = array(
(string) FEATURE_IDNUMBER => true,
(string) FEATURE_GROUPS => true,
(string) FEATURE_GROUPINGS => true,
(string) FEATURE_MOD_INTRO => true,
(string) FEATURE_BACKUP_MOODLE2 => true,
(string) FEATURE_COMPLETION_TRACKS_VIEWS => true,
(string) FEATURE_GRADE_HAS_GRADE => false,
(string) FEATURE_GRADE_OUTCOMES => false,
(string) FEATURE_SHOW_DESCRIPTION => true,
);
if (isset($features[(string) $feature])) {
return $features[$feature];
}
return null;
}
/**
* Saves a new instance of the mod_rocketchat into the database.
*
* Given an object containing all the necessary data, (defined by the form
* in mod_form.php) this function will create a new instance and return the id
* number of the instance.
*
* @param object $moduleinstance An object from the form.
* @param mod_rocketchat_mod_form $mform The form.
* @return int The id of the newly inserted record.
*/
function rocketchat_add_instance($moduleinstance, $mform = null) {
global $DB, $USER;
$moduleinstance->timecreated = time();
$moduleinstance->timemodified = $moduleinstance->timecreated;
$cmid = $moduleinstance->coursemodule;
$course = $DB->get_record('course', array('id' => $moduleinstance->course));
$groupname = mod_rocketchat_tools::rocketchat_group_name($cmid, $course);
$rocketchatapimanager = new rocket_chat_api_manager();
$moduleinstance->rocketchatid = $rocketchatapimanager->create_rocketchat_group($groupname);
if (is_null($moduleinstance->rocketchatid)) {
print_error('an error occured while creating Rocket.Chat group');
}
if ((boolean)get_config('mod_rocketchat', 'retentionfeature')) {
$retentionsettings = array(
'retentionenabled' =>
property_exists($moduleinstance, 'retentionenabled') ? $moduleinstance->retentionenabled : false,
'overrideglobal' =>
property_exists($moduleinstance, 'overrideglobal') ? $moduleinstance->overrideglobal : false,
'maxage' => $moduleinstance->maxage,
'filesonly' => property_exists($moduleinstance, 'filesonly') ? $moduleinstance->filesonly : false,
'excludepinned' => property_exists($moduleinstance, 'excludepinned') ? $moduleinstance->excludepinned : false
);
$rocketchatapimanager->save_rocketchat_group_settings($moduleinstance->rocketchatid, $retentionsettings);
}
if (!$moduleinstance->visible || !$moduleinstance->visibleoncoursepage) {
$group = $rocketchatapimanager->get_rocketchat_group_object($moduleinstance->rocketchatid);
$group->archive();
}
$id = $DB->insert_record('rocketchat', $moduleinstance);
// Force creator if current user has a role for this instance.
$moderatorrolesids = array_filter(explode(',', $moduleinstance->moderatorroles));
$userrolesids = array_filter(explode(',', $moduleinstance->userroles));
$forcecreator = mod_rocketchat_tools::has_rocket_chat_user_role($userrolesids, $USER, context_course::instance($course->id))
|| mod_rocketchat_tools::has_rocket_chat_moderator_role($moderatorrolesids, $USER, context_course::instance($course->id));
mod_rocketchat_tools::enrol_all_concerned_users_to_rocketchat_group(
$moduleinstance,
get_config('mod_rocketchat', 'background_add_instance'),
$forcecreator);
return $id;
}
/**
* Updates an instance of the mod_rocketchat in the database.
*
* Given an object containing all the necessary data (defined in mod_form.php),
* this function will update an existing instance with new data.
*
* @param object $moduleinstance An object from the form in mod_form.php.
* @param mod_rocketchat_mod_form $mform The form.
* @return bool True if successful, false otherwise.
*/
function rocketchat_update_instance($moduleinstance, $mform = null) {
global $DB;
$moduleinstance->timemodified = time();
$moduleinstance->id = property_exists($moduleinstance, 'id') ? $moduleinstance->id : $moduleinstance->instance;
$moduleinstance->retentionenabled =
property_exists($moduleinstance, 'retentionenabled') ? $moduleinstance->retentionenabled : false;
$moduleinstance->overrideglobal =
property_exists($moduleinstance, 'overrideglobal') ? $moduleinstance->overrideglobal : false;
$moduleinstance->filesonly =
property_exists($moduleinstance, 'filesonly') ? $moduleinstance->filesonly : false;
$moduleinstance->excludepinned =
property_exists($moduleinstance, 'excludepinned') ? $moduleinstance->excludepinned : false;
$rocketchat = $DB->get_record('rocketchat', array('id' => $moduleinstance->id));
$return = $DB->update_record('rocketchat', $moduleinstance);
if ($return) {
$rocketchatapimanager = new rocket_chat_api_manager();
if ((boolean)get_config('mod_rocketchat', 'retentionfeature')) {
$retentionsettings = array(
'retentionenabled' => $moduleinstance->retentionenabled,
'overrideglobal' => $moduleinstance->overrideglobal,
'maxage' => $moduleinstance->maxage,
'filesonly' => $moduleinstance->filesonly,
'excludepinned' => $moduleinstance->excludepinned
);
$rocketchatapimanager->save_rocketchat_group_settings($rocketchat->rocketchatid, $retentionsettings);
}
\mod_rocketchat_tools::synchronize_group_members($rocketchat->rocketchatid,
get_config('mod_rocketchat', 'background_synchronize'));
}
return $return;
}
/**
* Removes an instance of the mod_rocketchat from the database.
*
* @param int $id Id of the module instance.
* @return bool True if successful, false on failure.
*/
function rocketchat_delete_instance($id) {
global $DB;
$rocketchat = $DB->get_record('rocketchat', array('id' => $id));
if (!$rocketchat) {
return false;
}
// Treat remote Rocket.Chat remote private group depending of.
$rocketchatapimanager = new rocket_chat_api_manager();
list(, $caller) = debug_backtrace(false);
if ((\tool_recyclebin\course_bin::is_enabled() && $caller['function'] == 'course_delete_module')
|| (\tool_recyclebin\category_bin::is_enabled() && $caller['function'] == 'remove_course_contents')) {
$rocketchatapimanager->archive_rocketchat_group($rocketchat->rocketchatid);
} else {
$rocketchatapimanager->delete_rocketchat_group($rocketchat->rocketchatid);
}
$DB->delete_records('rocketchat', array('id' => $id));
return true;
}
/**
* Course reset form definition
* @param $mform
* @throws coding_exception
*/
function rocketchat_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'rocketchatheader', get_string('modulenameplural', 'mod_rocketchat'));
$mform->addElement('advcheckbox', 'reset_rocketchat', get_string('removemessages', 'mod_rocketchat'));
}
/**
* Course reset form defaults.
*
* @param object $course
* @return array
*/
function rocketchat_reset_course_form_defaults($course) {
return array('reset_rocketchat' => 1);
}
/**Remove all messages
* @param $data
*/
function rocketchat_reset_userdata($data) {
global $DB;
$status = [];
// Delete remote Rocket.Chat messages.
if (!empty($data->reset_rocketchat)) {
$sql = 'select cm.id, r.id as instanceid, r.rocketchatid from {course_modules} cm inner join {modules} m on m.id=cm.module '
.'inner join {rocketchat} r on r.id=cm.instance'
.' where cm.course=:courseid and m.name=:modname';
$rocketchats = $DB->get_records_sql($sql,
array('courseid' => $data->courseid, 'modname' => 'rocketchat'));
if ($rocketchats) {
$rocketchatapimanager = new rocket_chat_api_manager();
foreach ($rocketchats as $rocketchat) {
$rocketchatapimanager->clean_history($rocketchat->rocketchatid);
$status[] = array('component' => get_string('modulenameplural', 'rocketchat')
, 'item' => get_string("removeditem", 'mod_rocketchat', $rocketchat)
, 'error' => false);
}
}
}
return $status;
}