forked from learnweb/moodle-mod_collabora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
261 lines (217 loc) · 8.41 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
<?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');
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;
default:
return null;
}
}
/**
* @param $collabora
* @param mod_collabora_mod_form $mform
* @return int
*/
function collabora_add_instance($collabora, $mform) {
global $DB;
$collabora->timecreated = time();
$collabora->timemodified = time();
$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);
// Save the 'initial file'.
$context = context_module::instance($collabora->coursemodule);
file_postupdate_standard_filemanager($collabora, 'initialfile', mod_collabora_mod_form::get_filemanager_opts(),
$context, 'mod_collabora', \mod_collabora\collabora::FILEAREA_INITIAL, 0);
return $collabora->id;
}
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;
}
function collabora_delete_instance($id) {
global $DB;
if (!$collabora = $DB->get_record('collabora', ['id' => $id])) {
return false;
}
$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', array('name' => 'collabora'))) {
return false;
}
$extensions = \mod_collabora\collabora::get_accepted_types();
$strdnd = get_string('dnduploadcollabora', 'mod_collabora');
$files = array();
foreach ($extensions as $extn) {
$extn = trim($extn, '.');
$files[] = array('extension' => $extn, 'message' => $strdnd);
}
return array('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', array('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\collabora::FORMAT_UPLOAD;
// Set the display options to the site defaults.
$config = get_config('mod_collabora');
$data->display = $config->defaultdisplay;
$data->width = 0;
$data->height = 0;
$data->displayname = $config->defaultdisplayname;
$data->displaydescription = $config->defaultdisplaydescription;
return collabora_add_instance($data, null);
}
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();
if ($collabora->display === \mod_collabora\collabora::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);
}
$instance = new \mod_collabora\collabora($collabora, context_module::instance($coursemodule->id), 0, $USER->id);
if ($specificicon = $instance->get_module_icon()) {
$info->icon = $specificicon;
}
return $info;
}
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\collabora::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 $settings The settings navigation object
* @param navigation_node $collaboranode The node to add module settings to
*/
function collabora_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $collaboranode) {
global $USER, $PAGE, $CFG;
if (empty($PAGE->cm->context)) {
$PAGE->cm->context = context_module::instance($PAGE->cm->instance);
}
if (has_capability('mod/collabora:repair', $PAGE->cm->context)) {
$repairurl = new \moodle_url('/mod/collabora/repair.php', array('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 = array(
'mod_collabora:repair' => 'fa-medkit',
);
return $iconmap;
}