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 functions.
|
|
|
19 |
*
|
|
|
20 |
* @package filter_amanote
|
|
|
21 |
* @copyright 2020 Amaplex Software
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require_once($CFG->dirroot . '/config.php');
|
|
|
26 |
require_once($CFG->libdir . '/externallib.php');
|
|
|
27 |
require_once(__DIR__ . '/helpers/filehelper.php');
|
|
|
28 |
require_once(__DIR__ . '/models/annotatable.php');
|
|
|
29 |
|
|
|
30 |
// Verify that the current user is logged in.
|
|
|
31 |
require_login();
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Validate an annotatable id.
|
|
|
35 |
*
|
|
|
36 |
* @param string $id The annotatable id.
|
|
|
37 |
*
|
|
|
38 |
* @return boolean True if the id is valid.
|
|
|
39 |
*/
|
|
|
40 |
function validate_annotatableid($id) {
|
|
|
41 |
return $id && preg_match('/\d+\.\d+(\.\d+)?/', $id);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Generate the amanote URL from an annotatable id.
|
|
|
46 |
*
|
|
|
47 |
* @param string $annotatableid The annotatable id.
|
|
|
48 |
* @param file $file The corresponding file if in any.
|
|
|
49 |
* @param int $pagenumber The page number to open if any.
|
|
|
50 |
* @param string $route The route in the Amanote app.
|
|
|
51 |
*
|
|
|
52 |
* @return string The generated url.
|
|
|
53 |
*/
|
|
|
54 |
function generate_amanote_url($annotatableid, $file = null, $pagenumber = null, $route = 'note-taking') {
|
|
|
55 |
global $DB, $CFG, $USER;
|
|
|
56 |
|
|
|
57 |
if (!validate_annotatableid($annotatableid)) {
|
|
|
58 |
throw new Exception('Invalid annotatable id.');
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$explodedid = explode('.', $annotatableid);
|
|
|
62 |
$notefilename = $annotatableid . '.ama';
|
|
|
63 |
$annotatable = null;
|
|
|
64 |
|
|
|
65 |
if ($file) {
|
|
|
66 |
$annotatable = get_annotatable_for_file($file, $explodedid[0], null, $explodedid[1]);
|
|
|
67 |
} else {
|
|
|
68 |
$annotatable = get_annotatable_by_id($annotatableid);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if ($annotatable === null) {
|
|
|
72 |
$annotatable = new annotatable();
|
|
|
73 |
$annotatable->id = $annotatableid;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
if ($annotatable->legacyid) {
|
|
|
77 |
$savednotes = get_user_notes_for_course($USER->id, $explodedid[0]);
|
|
|
78 |
if (array_key_exists($annotatable->legacyid . '.ama', $savednotes)) {
|
|
|
79 |
$notefilename = $annotatable->legacyid . '.ama';
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// Get the moodle mobile service.
|
|
|
84 |
$serviceparams = ['shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE, 'enabled' => 1];
|
|
|
85 |
$service = $DB->get_record('external_services', $serviceparams);
|
|
|
86 |
|
|
|
87 |
if (empty($service)) {
|
|
|
88 |
throw new Exception('Moodle mobile service not found.');
|
|
|
89 |
exit();
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Generate the URL.
|
|
|
93 |
$config = get_config('filter_amanote');
|
|
|
94 |
$siteurl = $CFG->wwwroot;
|
|
|
95 |
$language = substr($USER->lang, 0, 2);
|
|
|
96 |
$usercontext = context_user::instance($USER->id);
|
|
|
97 |
$privatefilepath = '/' . $usercontext->id . '/user/private/Amanote/';
|
|
|
98 |
$moodleversion = preg_replace('/(\d+\.\d+(\.\d+)?) .*$/', '$1', $CFG->release);
|
|
|
99 |
$token = external_generate_token_for_current_user($service);
|
|
|
100 |
$sslenabled = !(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off' || $_SERVER['SERVER_PORT'] != 443);
|
|
|
101 |
$protocol = $sslenabled ? 'https' : 'http';
|
|
|
102 |
|
|
|
103 |
if ($route === 'note-taking' && $annotatable->kind === 'video') {
|
|
|
104 |
$route = '/note-taking/moodle/video/' . $annotatable->id;
|
|
|
105 |
} else {
|
|
|
106 |
$route = '/moodle/' . $route;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
$url = $protocol . '://app.amanote.com/' . $language . $route . '?' .
|
|
|
110 |
'siteURL='. $siteurl . '&' .
|
|
|
111 |
'accessToken=' . $token->token . '&' .
|
|
|
112 |
'tokenExpDate=' . $token->validuntil . '&' .
|
|
|
113 |
'userId=' . $USER->id . '&' .
|
|
|
114 |
'filePath=' . ($annotatable->internal ? $annotatable->url : rawurlencode($annotatable->url)) . '&' .
|
|
|
115 |
'amaPath=' . $privatefilepath . $notefilename . '&' .
|
|
|
116 |
'resourceId=' . $annotatable->id . '&' .
|
|
|
117 |
'legacyResourceId='. ($annotatable->legacyid ? $annotatable->legacyid : $annotatable->id) . '&' .
|
|
|
118 |
'saveInProvider=' . $config->saveinprivate . '&' .
|
|
|
119 |
'providerVersion=' . $moodleversion . '&' .
|
|
|
120 |
'pluginVersion=' . $config->version . '&' .
|
|
|
121 |
'key=' . $config->key . '&' .
|
|
|
122 |
'worksheet=' . $config->worksheet . '&' .
|
|
|
123 |
'mimeType=' . $annotatable->mimetype . '&' .
|
|
|
124 |
'anonymous=' . $config->anonymous;
|
|
|
125 |
|
|
|
126 |
if ($pagenumber != null) {
|
|
|
127 |
$url .= '&pageNumber=' . $pagenumber;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
if ($config->target == 2) {
|
|
|
131 |
$url .= '&embedded=1';
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
return $url;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Generate the amanote login URL that allows to login from Moodle to Amanote.
|
|
|
139 |
*
|
|
|
140 |
* @param string|null $redirecturl Optional. The URL to redirect the user to after login. Default is null.
|
|
|
141 |
* @param boolean $embedded Optional. Whether to open Amanote in embedded mode. Default is false.
|
|
|
142 |
*
|
|
|
143 |
* @return string The generated URL.
|
|
|
144 |
*/
|
|
|
145 |
function generate_amanote_login_url($redirecturl = null, $embedded = false) {
|
|
|
146 |
global $DB, $CFG, $USER;
|
|
|
147 |
|
|
|
148 |
// Get the moodle mobile service.
|
|
|
149 |
$serviceparams = ['shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE, 'enabled' => 1];
|
|
|
150 |
$service = $DB->get_record('external_services', $serviceparams);
|
|
|
151 |
|
|
|
152 |
if (empty($service)) {
|
|
|
153 |
throw new Exception('Moodle mobile service not found.');
|
|
|
154 |
exit();
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// Generate the URL.
|
|
|
158 |
$config = get_config('filter_amanote');
|
|
|
159 |
$siteurl = $CFG->wwwroot;
|
|
|
160 |
$language = substr($USER->lang, 0, 2);
|
|
|
161 |
$usercontext = context_user::instance($USER->id);
|
|
|
162 |
$token = external_generate_token_for_current_user($service);
|
|
|
163 |
$sslenabled = !(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off' || $_SERVER['SERVER_PORT'] != 443);
|
|
|
164 |
$protocol = $sslenabled ? 'https' : 'http';
|
|
|
165 |
$route = '/moodle/auth-redirect';
|
|
|
166 |
|
|
|
167 |
$url = $protocol . '://app.amanote.com/' . $language . $route . '?' .
|
|
|
168 |
'siteURL='. $siteurl . '&' .
|
|
|
169 |
'accessToken=' . $token->token . '&' .
|
|
|
170 |
'tokenExpDate=' . $token->validuntil . '&' .
|
|
|
171 |
'userId=' . $USER->id;
|
|
|
172 |
|
|
|
173 |
if (!empty($redirecturl)) {
|
|
|
174 |
$url .= '&redirectURL=' . rawurlencode($redirecturl);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
if ($embedded) {
|
|
|
178 |
$url .= '&embedded=1';
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
return $url;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Return the annotatable for a given id.
|
|
|
186 |
*
|
|
|
187 |
* @param string $annotatableid The annotatable id to get.
|
|
|
188 |
*
|
|
|
189 |
* @return annotatable The annotatable.
|
|
|
190 |
*/
|
|
|
191 |
function get_annotatable_by_id($annotatableid) {
|
|
|
192 |
global $DB, $USER;
|
|
|
193 |
|
|
|
194 |
$explodedid = explode('.', $annotatableid);
|
|
|
195 |
|
|
|
196 |
// Get the file from annotatable id.
|
|
|
197 |
if (count($explodedid) >= 3) {
|
|
|
198 |
$fileid = $explodedid[2];
|
|
|
199 |
$file = $DB->get_record('files', ['id' => $fileid]);
|
|
|
200 |
|
|
|
201 |
return get_annotatable_for_file($file, $explodedid[0], null, $explodedid[1]);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
// Get the instance from the cmid.
|
|
|
205 |
$sql = "SELECT instance, name FROM {course_modules}
|
|
|
206 |
INNER JOIN {modules} ON {modules}.id = {course_modules}.module
|
|
|
207 |
WHERE {course_modules}.id = :cmid";
|
|
|
208 |
|
|
|
209 |
$cmid = $explodedid[1];
|
|
|
210 |
$cm = $DB->get_record_sql($sql, ['cmid' => $cmid]);
|
|
|
211 |
|
|
|
212 |
if (empty($cm)) {
|
|
|
213 |
return null;
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
if (in_array($cm->name, ['resource', 'folder', 'label'])) {
|
|
|
217 |
// Get annotatable for file.
|
|
|
218 |
$sql = "SELECT
|
|
|
219 |
{files}.id as id,
|
|
|
220 |
{files}.contextid,
|
|
|
221 |
{files}.mimetype,
|
|
|
222 |
{files}.component,
|
|
|
223 |
{files}.filearea,
|
|
|
224 |
{files}.filename
|
|
|
225 |
FROM {course_modules}
|
|
|
226 |
LEFT JOIN {context} ON {context}.instanceid = {course_modules}.id
|
|
|
227 |
LEFT JOIN {files} ON {files}.contextid = {context}.id
|
|
|
228 |
AND {course_modules}.id = :cmid
|
|
|
229 |
WHERE {files}.component in ('mod_resource', 'mod_label', 'mod_folder')
|
|
|
230 |
AND {files}.source IS NOT NULL AND {files}.filename != '.'";
|
|
|
231 |
|
|
|
232 |
$files = $DB->get_records_sql($sql, ['cmid' => $cmid]);
|
|
|
233 |
|
|
|
234 |
if (!$files || count($files) <= 0) {
|
|
|
235 |
throw new Exception('File not found.');
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
$file = reset($files);
|
|
|
239 |
|
|
|
240 |
return get_annotatable_for_file($file, $explodedid[0], $cmid, $cm->instance);
|
|
|
241 |
} else if ($cm->name === 'url') {
|
|
|
242 |
// Get annotatable for url.
|
|
|
243 |
$url = $DB->get_record($cm->name, ['id' => $cm->instance]);
|
|
|
244 |
|
|
|
245 |
if (empty($url)) {
|
|
|
246 |
throw new Exception('Course module instance not found.');
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
return get_annotatable_for_url($url, $explodedid[0], $cmid);
|
|
|
250 |
} else {
|
|
|
251 |
throw new Exception('Module not supported.');
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
return null;
|
|
|
255 |
}
|