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 |
* H5P Content manager class
|
|
|
19 |
*
|
|
|
20 |
* @package contenttype_h5p
|
|
|
21 |
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace contenttype_h5p;
|
|
|
26 |
|
|
|
27 |
use core\notification;
|
|
|
28 |
use core_h5p\factory;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* H5P Content manager class
|
|
|
32 |
*
|
|
|
33 |
* @package contenttype_h5p
|
|
|
34 |
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class content extends \core_contentbank\content {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Returns user has access permission for the content itself.
|
|
|
41 |
* If the H5P content-type library is disabled, the user won't have access to it.
|
|
|
42 |
*
|
|
|
43 |
* @return bool True if content could be accessed. False otherwise.
|
|
|
44 |
*/
|
|
|
45 |
public function is_view_allowed(): bool {
|
|
|
46 |
// Force H5P content to be deployed.
|
|
|
47 |
$fileurl = $this->get_file_url();
|
|
|
48 |
if (empty($fileurl)) {
|
|
|
49 |
// This should never happen because H5P contents should have always a file. However, this extra-checked has been added
|
|
|
50 |
// to avoid the contentbank stop working if, for any unkonwn/weird reason, the file doesn't exist.
|
|
|
51 |
return false;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
// Skip capability check when creating the H5P content (because it has been created by trusted users).
|
|
|
55 |
$h5pplayer = new \core_h5p\player($fileurl, new \stdClass(), true, '', true);
|
|
|
56 |
// Flush error messages.
|
|
|
57 |
$h5pplayer->get_messages();
|
|
|
58 |
|
|
|
59 |
// Check if the H5P entry has been created and if the main library is enabled.
|
|
|
60 |
$file = $this->get_file();
|
|
|
61 |
if (!empty($file)) {
|
|
|
62 |
$h5p = \core_h5p\api::get_content_from_pathnamehash($file->get_pathnamehash());
|
|
|
63 |
if (empty($h5p)) {
|
|
|
64 |
// If there is no H5P entry for this content, it won't be displayed unless the user has the manageanycontent
|
|
|
65 |
// capability. Reasons for contents without a proper H5P entry in DB:
|
|
|
66 |
// - Invalid H5P package (it won't be never deployed).
|
|
|
67 |
// - Disabled content-type library (it can't be deployed so there is no way to know the mainlibraryid).
|
|
|
68 |
$context = \context::instance_by_id($this->content->contextid);
|
|
|
69 |
if (!has_capability('moodle/contentbank:manageanycontent', $context)) {
|
|
|
70 |
return false;
|
|
|
71 |
}
|
|
|
72 |
} else if (!\core_h5p\api::is_library_enabled((object) ['id' => $h5p->mainlibraryid])) {
|
|
|
73 |
// If the main library is disabled, it won't be displayed.
|
|
|
74 |
return false;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return parent::is_view_allowed();
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Import a file as a valid content.
|
|
|
83 |
* Before importing the file, this method will check if the file is a valid H5P package. If it's not valid, it will thrown
|
|
|
84 |
* an exception.
|
|
|
85 |
*
|
|
|
86 |
* @throws \moodle_exception If file operations fail
|
|
|
87 |
* @param \stored_file $file File to store in the content file area.
|
|
|
88 |
* @return \stored_file|null the stored content file or null if the file is discarted.
|
|
|
89 |
*/
|
|
|
90 |
public function import_file(\stored_file $file): ?\stored_file {
|
|
|
91 |
// The H5P content can be only deployed if the author of the .h5p file can update libraries or if all the
|
|
|
92 |
// content-type libraries exist, to avoid users without the h5p:updatelibraries capability upload malicious content.
|
|
|
93 |
$onlyupdatelibs = !\core_h5p\helper::can_update_library($file);
|
|
|
94 |
|
|
|
95 |
if (!\core_h5p\api::is_valid_package($file, $onlyupdatelibs)) {
|
|
|
96 |
$factory = new factory();
|
|
|
97 |
$errors = $factory->get_framework()->getMessages('error');
|
|
|
98 |
foreach ($errors as $error) {
|
|
|
99 |
notification::error($error->message);
|
|
|
100 |
}
|
|
|
101 |
if (empty($errors) || count($errors) > 1) {
|
|
|
102 |
throw new \moodle_exception('notvalidpackage', 'h5p');
|
|
|
103 |
}
|
|
|
104 |
throw new \moodle_exception($errors[0]->code, 'h5p');
|
|
|
105 |
}
|
|
|
106 |
return parent::import_file($file);
|
|
|
107 |
}
|
|
|
108 |
}
|