-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_moochub_courses_v2.php
338 lines (299 loc) · 14.1 KB
/
get_moochub_courses_v2.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
330
331
332
333
334
335
336
337
338
<?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/>.
/**
* Script that generates moochub course data in json format and saves it to a file.
*
* @package local_ildmeta
* @author Pascal Hürten <[email protected]>
* @copyright 2022 ILD TH Lübeck <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use local_ildmeta\manager;
use Opis\JsonSchema\{
Validator,
ValidationResult,
Helper,
Errors\ErrorFormatter,
};
require_once(__DIR__ . '/../../config.php');
global $CFG;
require_once($CFG->libdir . '/filelib.php');
// Check if file exists.
if (file_exists('./vendor/autoload.php')) {
require_once('./vendor/autoload.php');
}
$download = optional_param('download', false, PARAM_BOOL);
$metas = [];
$metarecords = $DB->get_records('ildmeta');
// Create Links for pagination as proposed by the JSON:API schema.
$jsonlink = $CFG->httpswwwroot . '/local/ildmeta/get_moochub_courses.php';
$metaslinks = ['self' => $jsonlink, 'first' => $jsonlink, 'last' => $jsonlink];
$metas['links'] = $metaslinks;
if (!isset($_GET['idn']) && !isset($_GET['id'])) {
$metarecords = $DB->get_records('ildmeta');
}
if (isset($_GET['idn'])) {
$idnumbers = $_GET['idn'];
$idnumbers = pproc_input($idnumbers);
if (!empty($idnumbers)) {
// Get all meta records for the given idnumers.
[$insql, $inparams] = $DB->get_in_or_equal($idnumbers);
$sql = "SELECT * FROM {ildmeta} meta
LEFT JOIN {course} course ON meta.courseid = course.id
WHERE course.idnumber $insql";
$metarecords = $DB->get_records_sql($sql, $inparams);
}
}
if (isset($_GET['id'])) {
$idnumbers = $_GET['id'];
$idnumbers = pproc_input($idnumbers);
if (!empty($idnumbers)) {
// Get all meta records for the given course ids.
[$insql, $inparams] = $DB->get_in_or_equal($idnumbers);
$sql = "SELECT * FROM {ildmeta} meta
LEFT JOIN {course} course ON meta.courseid = course.id
WHERE course.id $insql";
$metarecords = $DB->get_records_sql($sql, $inparams);
}
}
// Return empty data, if there was no course.
if (!isset($metarecords) or empty($metarecords)) {
$metas['data'] = [];
} else {
// Get vocabularies from ildmeta_vocabulary for dropdown selection fields.
$records = $DB->get_records('ildmeta_vocabulary');
$vocabularies = new \stdClass();
foreach ($records as $vocabulary) {
$vocabularies->{$vocabulary->title} = manager::filter_vocabulary_lang($vocabulary, 'de');
}
// Get list of providers.
$providers = manager::get_providers('de');
// Create a json entry for every course, that is supposed to be shared.
foreach ($metarecords as $meta) {
// Skip courses that are not supposed to be exported or a course record does not exist.
if ($meta->noindexcourse != 0 || !$DB->record_exists('course', array('id' => $meta->courseid))) {
continue;
}
$metaentry = [];
$metaentry['type'] = 'courses';
// Create an ID by adding the moodle course id to the host name of the current moodle site.
$metaentry['id'] = parse_url($CFG->wwwroot, PHP_URL_HOST) . $meta->courseid;
$metaentry['attributes'] = [];
$metaentry['attributes']['name'] = $meta->coursetitle;
$metaentry['attributes']['courseCode'] = null;
$metaentry['attributes']['courseMode'] = 'MOOC';
$metaentry['attributes']['url'] = manager::get_external_course_link($meta->courseid);
if (isset($meta->teasertext) && !empty($meta->teasertext)) {
$metaentry['attributes']['description'] = $meta->teasertext;
} else {
$metaentry['attributes']['description'] = null;
}
$metaentry['attributes']['abstract'] = null;
// Set teaching language.
// TODO: Enable selection of multiple teaching languages.
// TODO: Add more languages and manage them like the other vocabularies.
$langlist = [
'de',
'en',
'uk',
'ru'
];
$metaentry['attributes']['languages'] = [$langlist[$meta->courselanguage]];
$metaentry['attributes']['startDate'] = date('c', $meta->starttime);
$metaentry['attributes']['endDate'] = null;
// Get overview image from filestorage.
// If no custom image ist set in ildmeta, then use the course image instead.
$fs = get_file_storage();
$context = context_course::instance($meta->courseid);
$fileurl = '';
$imagefile = null;
if (isset($meta->overviewimage)) {
$files = $fs->get_area_files($context->id, 'local_ildmeta', 'overviewimage', 0);
} else {
$files = $fs->get_area_files($context->id, 'course', 'overviewfiles', 0);
}
foreach ($files as $file) {
if ($file->is_valid_image()) {
$imagefile = $file;
$fileurl = moodle_url::make_pluginfile_url(
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
isset($meta->overviewimage) ? $file->get_itemid() : null,
$file->get_filepath(),
$file->get_filename(),
false
);
break;
}
}
// Set image metadata.
if (!isset($fileurl) || empty((string)$fileurl) || !isset($imagefile)) {
$metaentry['attributes']['image'] = null;
} else {
$metaentry['attributes']['image'] = array();
$metaentry['attributes']['image']['url'] = trim((string)$fileurl);
// Get License of image and convert to an SPDX license.
$license = $DB->get_record('license', array('shortname' => $imagefile->get_license()), '*', MUST_EXIST);
if (isset($license) && !empty($license) && $license->shortname != 'unknown') {
$spdxlicense = $DB->get_record('ildmeta_spdx_licenses', array('moodle_license' => $license->id), '*', MUST_EXIST);
$spdxurl = !empty($spdxlicense->spdx_url) ? $spdxlicense->spdx_url : null;
$metaentry['attributes']['image']['licenses'] = array();
$metaentry['attributes']['image']['licenses'][0]['id'] = $spdxlicense->spdx_shortname;
$metaentry['attributes']['image']['licenses'][0]['url'] = $spdxurl;
$metaentry['attributes']['image']['licenses'][0]['name'] = $spdxlicense->spdx_fullname;
$metaentry['attributes']['image']['licenses'][0]['author'] = $imagefile->get_author();
} else {
$metaentry['attributes']['image'] = null;
}
}
// TODO: Add option to describe learning objectives for a course.
// $metaentry['attributes']['learningObjectives'] = [];
// Set duration by converting amount of hours to ISO 8601 duration.
$duration = null;
if (isset($meta->processingtime) && !empty($meta->processingtime)) {
$duration = 'PT' . $meta->processingtime . 'H';
$metaentry['attributes']['duration'] = $duration;
} else {
$metaentry['attributes']['duration'] = null;
}
if (isset($meta->availableuntil) && !empty($meta->availableuntil)) {
$metaentry['attributes']['availableUntil'] = date('c', $meta->availableuntil);
} else {
$metaentry['attributes']['availableUntil'] = null;
}
// Set instructors.
$lecturer = explode(', ', $meta->lecturer);
$metaentry['attributes']['instructors'] = array();
for ($i = 0; $i < count($lecturer); $i++) {
if ($lecturer[$i] != '') {
$metaentry['attributes']['instructors'][$i] = new \stdClass;
$metaentry['attributes']['instructors'][$i]->name = $lecturer[$i];
}
}
// Set video.
$metaentry['attributes']['video'] = null;
if (isset($meta->videocode) && !empty(trim($meta->videolicense))) {
$license = $DB->get_record('license', array('id' => $meta->videolicense), '*', IGNORE_MISSING);
// Only set video if video license is known.
if (isset($license) && !empty($license) && $license->shortname != 'unknown') {
$metaentry['attributes']['video'] = array();
$metaentry['attributes']['video']['url'] = trim($meta->videocode);
$spdxlicense = $DB->get_record('ildmeta_spdx_licenses', array('moodle_license' => $license->id), '*', MUST_EXIST);
$spdxurl = !empty($spdxlicense->spdx_url) ? $spdxlicense->spdx_url : null;
$metaentry['attributes']['video']['licenses'] = array();
$metaentry['attributes']['video']['licenses'][0]['id'] = $spdxlicense->spdx_shortname;
$metaentry['attributes']['video']['licenses'][0]['url'] = $spdxurl;
$metaentry['attributes']['video']['licenses'][0]['name'] = $spdxlicense->spdx_fullname;
}
}
// Set course license.
$license = $DB->get_record('license', array('id' => $meta->license), '*', IGNORE_MISSING);
if (isset($license) && !empty($license) && $license->shortname != 'unknown') {
$spdxlicense = $DB->get_record('ildmeta_spdx_licenses', array('moodle_license' => $license->id), '*', MUST_EXIST);
$spdxurl = !empty($spdxlicense->spdx_url) ? $spdxlicense->spdx_url : null;
$metaentry['attributes']['courseLicenses'] = array();
$metaentry['attributes']['courseLicenses'][0]['id'] = $spdxlicense->spdx_shortname;
$metaentry['attributes']['courseLicenses'][0]['url'] = $spdxurl;
$metaentry['attributes']['courseLicenses'][0]['name'] = $spdxlicense->spdx_fullname;
} else {
$metaentry['attributes']['courseLicenses'] = [];
$metaentry['attributes']['courseLicenses'][0]['id'] = 'Proprietary';
$metaentry['attributes']['courseLicenses'][0]['url'] = null;
}
// Set course provider.
$provider = $providers[$meta->provider];
$metaentry['attributes']['moocProvider']['name'] = $provider['name'];
$urlwithprotocol = $provider['url'];
// Make sure the provider url includes a protocol, add https if missing.
if (strpos($provider['url'], 'http') === false) {
$urlwithprotocol = 'https://' . $provider['url'];
}
$metaentry['attributes']['moocProvider']['url'] = $urlwithprotocol;
$metaentry['attributes']['moocProvider']['logo'] = $provider['logo'];
// Set access to "free". Currently there is no option to track paid courses.
$metaentry['attributes']['access'] = ['free'];
$metas['data'][] = $metaentry;
}
}
// Check if opis/json-schema is installed.
if (class_exists('Opis\JsonSchema\Validator')) {
// Schema Validation.
$validator = new Validator();
$schemaurl = "https://raw.githubusercontent.com/MOOChub/schema/d32a0476c8a8ef48af54439f329bffaaf088bf1c/moochub-schema.json";
// Get schema from github.
$schemajson = file_get_contents($schemaurl);
$schema = Helper::toJSON(json_decode($schemajson));
$data = Helper::toJSON($metas);
// Get validation result.
/** @var ValidationResult $result */
$result = $validator->validate($data, $schema);
// Checking if $data is valid.
if ($result->isValid()) {
// Send Json response.
$json = json_encode($metas, JSON_UNESCAPED_SLASHES);
// If download flag is set trigger download on client browser.
if ($download) {
send_file($json, 'courses_moochub.json', 0, 0, true, $download);
} else {
header('Content-Type: application/vnd.api+json; moochub-version=2.3');
echo $json;
}
}
// Checking if there is an error.
if ($result->hasError()) {
// Get the error.
$error = $result->error();
// Create an error formatter.
$formatter = new ErrorFormatter();
$formattederror = $formatter->format($error, true);
$errormessage = json_encode(
$formattederror,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);
// Notify moodle admin per email.
$PAGE->set_context(context_system::instance());
$admin = get_admin();
$adminuser = $DB->get_record('user', ['id' => $admin->id]);
$adminemail = $adminuser->email;
$subject = 'ILD Meta Data: JSON Schema Validation Error';
$message = 'There was an error while validating the JSON Schema for the moochub courses. Please check the error message below.';
$message .= '<br><br>';
$message .= $errormessage;
// Send email.
email_to_user($adminuser, $adminuser, $subject, $message, '', '', '', true);
// Send error 500 response.
$error = [
'errors' => $formattederror,
'source' => $metas,
'schema' => $schema,
];
header('Content-Type: application/vnd.api+json; moochub-version=2.3');
$json = json_encode($error, JSON_UNESCAPED_SLASHES);
http_response_code(500);
echo $json;
}
} else {
// Send Json response.
$json = json_encode($metas, JSON_UNESCAPED_SLASHES);
// If download flag is set trigger download on client browser.
if ($download) {
send_file($json, 'courses_moochub.json', 0, 0, true, $download);
} else {
header('Content-Type: application/vnd.api+json; moochub-version=2.3');
echo $json;
}
}