-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlib.php
329 lines (284 loc) · 9.87 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?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/>.
/**
* API functions.
*
* @package mod_collabora
* @copyright 2019 Davo Smith, Synergy Learning
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once(__DIR__ . '/mod_form.php');
/**
* Checks whether or not a feature is supported.
*
* @param string $feature
* @return bool
*/
function collabora_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_MOD_PURPOSE:
return MOD_PURPOSE_COLLABORATION;
default:
return false;
}
}
/**
* Create a new collabora instance.
*
* @param \stdClass $collabora
* @param mod_collabora_mod_form $mform
* @return int
*/
function collabora_add_instance($collabora, $mform) {
global $DB;
$collabora->timecreated = time();
$collabora->timemodified = time();
// Save the 'initial file'.
if (!\mod_collabora\util::store_initial_file($collabora, $collabora->coursemodule)) {
return 0;
}
$collabora->id = $DB->insert_record('collabora', $collabora);
$completiontimeexpected = !empty($collabora->completionexpected) ? $collabora->completionexpected : null;
\core_completion\api::update_completion_date_event(
$collabora->coursemodule,
'collabora',
$collabora->id,
$completiontimeexpected
);
return $collabora->id;
}
/**
* Update a collabora instance.
*
* @param \stdClass $collabora
* @return bool
*/
function collabora_update_instance($collabora) {
global $DB;
$collabora->id = $collabora->instance;
$collabora->timemodified = time();
$DB->update_record('collabora', $collabora);
$completiontimeexpected = !empty($collabora->completionexpected) ? $collabora->completionexpected : null;
\core_completion\api::update_completion_date_event($collabora->coursemodule, 'collabora',
$collabora->id, $completiontimeexpected);
// Do not save the 'initial file' here, as you cannot change this after the activity has been created.
return true;
}
/**
* Delete an existing collabora instance.
*
* @param int $id
* @return bool
*/
function collabora_delete_instance($id) {
global $DB;
if (!$collabora = $DB->get_record('collabora', ['id' => $id])) {
return true; // The instance is deleted already.
}
if ($cm = get_coursemodule_from_instance('collabora', $id)) {
\core_completion\api::update_completion_date_event($cm->id, 'collabora', $id, null);
}
$DB->delete_records('collabora_document', ['collaboraid' => $collabora->id]);
$DB->delete_records('collabora', ['id' => $collabora->id]);
return true;
}
/**
* Register the ability to handle drag and drop file uploads.
*
* @return array containing details of the files / types the mod can handle
*/
function collabora_dndupload_register() {
global $DB;
// Prevent using this hook while disabled.
if (!$DB->get_field('modules', 'visible', ['name' => 'collabora'])) {
return false;
}
$extensions = \mod_collabora\api\collabora_fs::get_accepted_types();
$strdnd = get_string('dnduploadcollabora', 'mod_collabora');
$files = [];
foreach ($extensions as $extn) {
$extn = trim($extn, '.');
$files[] = ['extension' => $extn, 'message' => $strdnd];
}
return ['files' => $files];
}
/**
* Handle a file that has been uploaded.
*
* @param object $uploadinfo details of the file / content that has been uploaded
* @return int instance id of the newly created mod
*/
function collabora_dndupload_handle($uploadinfo) {
global $DB;
// Prevent using this hook while disabled.
if (!$DB->get_field('modules', 'visible', ['name' => 'collabora'])) {
return false;
}
// Gather the required info.
$data = new stdClass();
$data->course = $uploadinfo->course->id;
$data->name = $uploadinfo->displayname;
$data->intro = '';
$data->introformat = FORMAT_HTML;
$data->coursemodule = $uploadinfo->coursemodule;
$data->initialfile_filemanager = $uploadinfo->draftitemid;
$data->format = \mod_collabora\util::FORMAT_UPLOAD;
// Set the display options to the site defaults.
$config = get_config('mod_collabora');
$data->display = $config->defaultdisplay;
$data->height = 0;
$data->displayname = $config->defaultdisplayname;
$data->displaydescription = $config->defaultdisplaydescription;
return collabora_add_instance($data, null);
}
/**
* Get a coursemodule info object with infos about its presentation on the course page.
*
* @param \stdClass $coursemodule
* @return \cached_cm_info
*/
function collabora_get_coursemodule_info($coursemodule) {
global $DB, $USER;
if (!$collabora = $DB->get_record('collabora', ['id' => $coursemodule->instance])) {
return null;
}
$info = new cached_cm_info();
$info->name = $collabora->name;
if ($collabora->display === \mod_collabora\util::DISPLAY_NEW) {
// Use javascript to open the link in a new tab.
$url = new moodle_url('/mod/collabora/view.php', ['id' => $coursemodule->id]);
$info->onclick = "event.preventDefault();window.open('" . $url->out(false) . "', '_blank').focus();";
}
if ($coursemodule->showdescription) {
$info->content = format_module_intro('collabora', $collabora, $coursemodule->id, false);
}
$groupid = \mod_collabora\util::get_current_groupid_from_cm($coursemodule);
if ($groupid < 0) {
return null;
}
$collaborafs = new \mod_collabora\api\collabora_fs(
$collabora,
context_module::instance($coursemodule->id),
$groupid,
$USER->id
);
if ($specificicon = $collaborafs->get_module_icon()) {
$info->icon = 'mod/collabora/' . $specificicon;
$info->customdata['filtericon'] = 1; // Apply the monologo filter to the icon.
}
return $info;
}
/**
* Send a stored_file to the browser.
*
* @param \stdClass|int $course
* @param \stdClass|null $cm
* @param \context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @param array $options
* @return void
*/
function collabora_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
if ($context->contextlevel != CONTEXT_MODULE) {
return;
}
require_login($course, false, $cm);
// File link only occurs on the edit settings page, so restrict access to teachers.
if (!has_capability('moodle/course:manageactivities', $context)) {
return;
}
if ($filearea !== \mod_collabora\api\collabora_fs::FILEAREA_INITIAL) {
return;
}
$itemid = (int) array_shift($args);
if ($itemid !== 0) {
return;
}
$filename = array_pop($args);
$filepath = '/' . implode('/', $args);
if ($filepath !== '/') {
$filepath .= '/';
}
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'mod_collabora', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return;
}
send_stored_file($file, null, 0, $forcedownload, $options);
}
/**
* Adds module specific settings to the settings block.
*
* @param settings_navigation $settingsnav The settings navigation object
* @param navigation_node $collaboranode The node to add module settings to
* @return void
*/
function collabora_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $collaboranode) {
global $USER, $PAGE, $CFG;
if (empty($PAGE->cm->context)) {
return;
}
if (has_capability('mod/collabora:repair', $PAGE->cm->context)) {
$repairurl = new \moodle_url('/mod/collabora/repair.php', ['id' => $PAGE->cm->id]);
$repairicon = new \pix_icon('repair', get_string('repair', 'mod_collabora'), 'mod_collabora');
$collaboranode->add(
get_string('repair', 'mod_collabora'),
$repairurl,
navigation_node::TYPE_SETTING,
null,
null,
$repairicon
);
}
}
/**
* Get icon mapping for FontAwesome.
*/
function collabora_get_fontawesome_icon_map() {
// We build a map of some icons we use in pix_icon objects.
$iconmap = [
'mod_collabora:repair' => 'fa-medkit',
];
return $iconmap;
}
/**
* Get an html fragment.
*
* @param mixed $args an array or object with context and parameters needed to get the data
* @return string The html fragment we want to use by ajax
*/
function mod_collabora_output_fragment_get_html($args) {
return \mod_collabora\fragment\util::get_html($args);
}