MDL-67415 environment: 3.10 base information
[moodle.git] / h5p / lib.php
blobdeccba44862942190d2031591a1a317702aa2b5f
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.
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/>.
17 /**
18 * Callbacks.
20 * @package core_h5p
21 * @copyright 2019 Bas Brands <bas@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
26 /**
27 * Serve the files from the core_h5p file areas.
29 * @package core_h5p
30 * @category files
32 * @param stdClass $course the course object
33 * @param stdClass $cm the course module object
34 * @param stdClass $context the newmodule's context
35 * @param string $filearea the name of the file area
36 * @param array $args extra arguments (itemid, path)
37 * @param bool $forcedownload whether or not force download
38 * @param array $options additional options affecting the file serving
40 * @return bool Returns false if we don't find a file.
42 function core_h5p_pluginfile($course, $cm, $context, string $filearea, array $args, bool $forcedownload,
43 array $options = []) : bool {
44 global $DB;
46 // Require classes from H5P third party library
47 \core_h5p\autoloader::register();
49 $filesettingsset = false;
51 switch ($filearea) {
52 default:
53 return false; // Invalid file area.
55 case \core_h5p\file_storage::LIBRARY_FILEAREA:
56 if ($context->contextlevel != CONTEXT_SYSTEM) {
57 return false; // Invalid context because the libraries are loaded always in the context system.
60 $itemid = null;
62 // The files that can be delivered to this function are unfortunately out of our control. Some of the
63 // references are embedded into the JavaScript of the files and we have no ability to inject an item id.
64 // We also don't know the location of the item id when we do include it, so we look for the first numeric
65 // value and try to serve that file.
66 foreach ($args as $key => $value) {
67 if (is_numeric($value)) {
68 $itemid = $value;
69 unset($args[$key]);
70 break;
74 if (!isset($itemid)) {
75 // We didn't find an item id to use, so we fall back to retrieving the record using all the other
76 // fields. The combination of component, filearea, filepath, and filename is enough for a unique
77 // record.
78 $filename = array_pop($args);
79 $filepath = '/' . implode('/', $args) . '/';
80 $itemid = $DB->get_field('files', 'itemid', [
81 'component' => \core_h5p\file_storage::COMPONENT,
82 'filearea' => \core_h5p\file_storage::LIBRARY_FILEAREA,
83 'filepath' => $filepath,
84 'filename' => $filename
85 ]);
86 $filesettingsset = true;
88 break;
89 case \core_h5p\file_storage::CONTENT_FILEAREA:
90 if ($context->contextlevel != CONTEXT_SYSTEM) {
91 return false; // Invalid context because the content files are loaded always in the context system.
93 $itemid = array_shift($args);
94 break;
95 case \core_h5p\file_storage::CACHED_ASSETS_FILEAREA:
96 case \core_h5p\file_storage::EXPORT_FILEAREA:
97 $itemid = 0;
98 break;
101 if (!$filesettingsset) {
102 $filename = array_pop($args);
103 $filepath = (!$args ? '/' : '/' . implode('/', $args) . '/');
106 $fs = get_file_storage();
107 $file = $fs->get_file($context->id, \core_h5p\file_storage::COMPONENT, $filearea, $itemid, $filepath, $filename);
108 if (!$file) {
109 return false; // No such file.
112 send_stored_file($file, null, 0, $forcedownload, $options);
114 return true;