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 |
* Amanote file helper functions.
|
|
|
19 |
*
|
|
|
20 |
* @package filter_amanote
|
|
|
21 |
* @copyright 2021 Amaplex Software
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Try to guess the mimetype from an URL.
|
|
|
27 |
*
|
|
|
28 |
* @param string $url The url.
|
|
|
29 |
*
|
|
|
30 |
* @return string The mimetype if one is found, application/unknown otherwise.
|
|
|
31 |
*/
|
|
|
32 |
function guess_url_mimetype($url) {
|
|
|
33 |
|
|
|
34 |
$mimetype = 'application/unknown';
|
|
|
35 |
|
|
|
36 |
if (!$url) {
|
|
|
37 |
return $mimetype;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
// Check if the url matches a supported video provider (youtube or vimeo).
|
|
|
41 |
preg_match("/^(https?\:\/\/)?(www\.)?(youtube|youtu|vimeo)\.(com|be)\/.+$/", $url, $matches);
|
|
|
42 |
if ($matches && count($matches) > 3) {
|
|
|
43 |
return strpos($matches[3], 'youtu') !== false ? 'video/youtube' : 'video/vimeo';
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
// Check if the url ends with a file format.
|
|
|
47 |
preg_match("/\.(\w{3,4})($|\?|\#)/", $url, $matches);
|
|
|
48 |
if (!$matches || count($matches) < 2) {
|
|
|
49 |
return $mimetype;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
$extension = $matches[1];
|
|
|
53 |
$types = core_filetypes::get_types();
|
|
|
54 |
|
|
|
55 |
if (isset($types[$extension])) {
|
|
|
56 |
$mimetype = $types[$extension]['type'];
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
return $mimetype;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Get the content kind from a mimetype.
|
|
|
64 |
*
|
|
|
65 |
* @param string $mimetype The mimetype.
|
|
|
66 |
*
|
|
|
67 |
* @return string The content kind if one is found, unknown otherwise.
|
|
|
68 |
*/
|
|
|
69 |
function get_content_kind_from_mimetype($mimetype) {
|
|
|
70 |
|
|
|
71 |
if ($mimetype === 'video/youtube' || $mimetype === 'video/vimeo') {
|
|
|
72 |
return 'video';
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$types = core_filetypes::get_types();
|
|
|
76 |
|
|
|
77 |
foreach ($types as $type) {
|
|
|
78 |
if ($type['type'] === $mimetype && isset($type['groups']) && !empty($type['groups'])) {
|
|
|
79 |
return $type['groups'][0];
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
return 'unknown';
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Get the supported mimetypes for a given (optional) content kind. If no
|
|
|
88 |
* content kind is given, all the supported mimtypes are returned.
|
|
|
89 |
*
|
|
|
90 |
* @param string $contentkind The content kind if any.
|
|
|
91 |
*
|
|
|
92 |
* @return string The supported mimetypes.
|
|
|
93 |
*/
|
|
|
94 |
function get_supported_mimetypes($contentkind = null) {
|
|
|
95 |
$documentmimetypes = [
|
|
|
96 |
'application/pdf',
|
|
|
97 |
'application/vnd.ms-powerpoint',
|
|
|
98 |
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
|
99 |
'application/epub+zip',
|
|
|
100 |
'application/vnd.oasis.opendocument.text',
|
|
|
101 |
'application/msword',
|
|
|
102 |
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
|
103 |
];
|
|
|
104 |
|
|
|
105 |
$videomimetypes = [
|
|
|
106 |
'video/quicktime',
|
|
|
107 |
'video/mp4',
|
|
|
108 |
'video/mpeg',
|
|
|
109 |
'video/ogg',
|
|
|
110 |
'video/youtube',
|
|
|
111 |
'video/vimeo',
|
|
|
112 |
];
|
|
|
113 |
|
|
|
114 |
if ($contentkind === 'video') {
|
|
|
115 |
return $videomimetypes;
|
|
|
116 |
} else if ($contentkind === 'document') {
|
|
|
117 |
return $documentmimetypes;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return array_merge($documentmimetypes, $videomimetypes);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Get the user's notes for a given course.
|
|
|
125 |
*
|
|
|
126 |
* @param string $userid The user id.
|
|
|
127 |
* @param string $courseid The course id.
|
|
|
128 |
*
|
|
|
129 |
* @return array An array of filename.
|
|
|
130 |
*/
|
|
|
131 |
function get_user_notes_for_course($userid, $courseid) {
|
|
|
132 |
global $DB;
|
|
|
133 |
|
|
|
134 |
$sql = "SELECT filename FROM {files} WHERE filepath = '/Amanote/'
|
|
|
135 |
AND filearea = 'private'
|
|
|
136 |
AND userid = :userid
|
|
|
137 |
AND filename LIKE :courseid";
|
|
|
138 |
|
|
|
139 |
return $DB->get_records_sql($sql, ['userid' => $userid, 'courseid' => $courseid . '.%']);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Get the annotatable for a given file.
|
|
|
144 |
*
|
|
|
145 |
* @param file $file The file.
|
|
|
146 |
* @param string $courseid The course id.
|
|
|
147 |
* @param string $cmid The course module id.
|
|
|
148 |
* @param string $modinstance The module instance id.
|
|
|
149 |
*
|
|
|
150 |
* @return annotatable The annotatable.
|
|
|
151 |
*/
|
|
|
152 |
function get_annotatable_for_file($file, $courseid, $cmid, $modinstance) {
|
|
|
153 |
$annotatable = new annotatable();
|
|
|
154 |
|
|
|
155 |
// Generate the annotable id for the file.
|
|
|
156 |
if ($cmid) {
|
|
|
157 |
$annotatable->id = $courseid . '.' . $cmid;
|
|
|
158 |
} else {
|
|
|
159 |
$annotatable->id = $courseid . '.' . $modinstance . '.' . $file->id;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
$annotatable->cmid = $cmid;
|
|
|
163 |
|
|
|
164 |
if ($file->component === 'mod_resource' || $file->component === 'mod_folder') {
|
|
|
165 |
$annotatable->legacyid = $courseid . '.' . $modinstance . '.' . $file->id;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
// Use the legacy id for folder as all files in the folder has the same cmid.
|
|
|
169 |
if ($file->component === 'mod_folder') {
|
|
|
170 |
$annotatable->id = $annotatable->legacyid;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
// Set the content's kind.
|
|
|
174 |
$annotatable->kind = get_content_kind_from_mimetype($file->mimetype);
|
|
|
175 |
if ($annotatable->kind !== 'video') {
|
|
|
176 |
$annotatable->kind = 'document';
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
$annotatable->mimetype = $file->mimetype;
|
|
|
180 |
$annotatable->internal = true;
|
|
|
181 |
$filepath = (empty($file->filepath) ? '/' : $file->filepath) . $file->filename;
|
|
|
182 |
$annotatable->url = "/$file->contextid/$file->component/$file->filearea/1$filepath";
|
|
|
183 |
|
|
|
184 |
if ($file->filearea === 'intro') {
|
|
|
185 |
$annotatable->url = str_replace('intro/1', 'intro', $annotatable->url);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
return $annotatable;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Get the annotatable for a given url.
|
|
|
193 |
*
|
|
|
194 |
* @param string $url The url.
|
|
|
195 |
* @param string $courseid The course id.
|
|
|
196 |
* @param string $cmid The course module id.
|
|
|
197 |
*
|
|
|
198 |
* @return annotatable The annotatable.
|
|
|
199 |
*/
|
|
|
200 |
function get_annotatable_for_url($url, $courseid, $cmid) {
|
|
|
201 |
|
|
|
202 |
$annotatable = new annotatable();
|
|
|
203 |
|
|
|
204 |
// Generate the annotable id for the url.
|
|
|
205 |
$annotatable->id = $courseid . '.' . $cmid;
|
|
|
206 |
$annotatable->cmid = $cmid;
|
|
|
207 |
|
|
|
208 |
$mimetype = guess_url_mimetype($url->externalurl);
|
|
|
209 |
|
|
|
210 |
// Set the content's kind.
|
|
|
211 |
$annotatable->kind = get_content_kind_from_mimetype($mimetype);
|
|
|
212 |
if ($annotatable->kind !== 'video') {
|
|
|
213 |
$annotatable->kind = 'document';
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
$annotatable->mimetype = $mimetype;
|
|
|
217 |
$annotatable->internal = false;
|
|
|
218 |
$annotatable->url = $url->externalurl;
|
|
|
219 |
|
|
|
220 |
return $annotatable;
|
|
|
221 |
}
|