Skip to content

Commit

Permalink
MDL-67059 core_h5p: UI to manually upload H5P content type libraries
Browse files Browse the repository at this point in the history
Co-authored by: Mihail Geshoski <mihail@moodle.com>
  • Loading branch information
Amaia Anabitarte authored and abgreeve committed Nov 7, 2019
1 parent cf29a48 commit b57e1c9
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 2 deletions.
29 changes: 29 additions & 0 deletions admin/settings/h5p.php
@@ -0,0 +1,29 @@
<?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/>.

/**
* H5P settings link.
*
* @package core_h5p
* @copyright 2019 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

// Settings page.
$ADMIN->add('h5p', new admin_externalpage('h5psettings', get_string('h5pmanage', 'core_h5p'),
new moodle_url('/h5p/libraries.php'), ['moodle/site:config', 'moodle/h5p:updatelibraries']));
1 change: 1 addition & 0 deletions admin/settings/top.php
Expand Up @@ -31,6 +31,7 @@
$ADMIN->add('root', new admin_category('analytics', new lang_string('analytics', 'analytics')));
$ADMIN->add('root', new admin_category('competencies', new lang_string('competencies', 'core_competency')));
$ADMIN->add('root', new admin_category('badges', new lang_string('badges'), empty($CFG->enablebadges)));
$ADMIN->add('root', new admin_category('h5p', new lang_string('h5p', 'core_h5p')));
$ADMIN->add('root', new admin_category('location', new lang_string('location','admin')));
$ADMIN->add('root', new admin_category('language', new lang_string('language')));
$ADMIN->add('root', new admin_category('messaging', new lang_string('messagingcategory', 'admin')));
Expand Down
57 changes: 57 additions & 0 deletions h5p/classes/form/uploadlibraries_form.php
@@ -0,0 +1,57 @@
<?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/>.

/**
* Upload an h5p content to update the content libraries.
*
* @package core_h5p
* @copyright 2019 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_h5p\form;

defined('MOODLE_INTERNAL') || die();

/**
* Upload a zip or h5p content to update the content libraries.
*
* @copyright 2019 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class uploadlibraries_form extends \moodleform {
/**
* Form definition.
*/
public function definition() {
$mform = $this->_form;
$mform->addElement('header', 'settingsheader', get_string('uploadlibraries', 'core_h5p'));

$filemanageroptions = array(
'accepted_types' => array('.h5p', '.zip'),
'maxbytes' => 0,
'maxfiles' => 1,
'subdirs' => 0
);

$mform->addElement('filepicker', 'h5ppackage', get_string('h5ppackage', 'core_h5p'),
null, $filemanageroptions);
$mform->addHelpButton('h5ppackage', 'h5ppackage', 'core_h5p');
$mform->addRule('h5ppackage', null, 'required');

$this->add_action_buttons(false, get_string('uploadlibraries', 'core_h5p'));
}
}
1 change: 0 additions & 1 deletion h5p/classes/helper.php
Expand Up @@ -76,7 +76,6 @@ public static function save_h5p(factory $factory, \stored_file $file, \stdClass

return $h5pstorage->contentId;
}

return false;
}

Expand Down
67 changes: 67 additions & 0 deletions h5p/libraries.php
@@ -0,0 +1,67 @@
<?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/>.

/**
* Manage H5P libraries settings page.
*
* @package core_h5p
* @copyright 2019 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once(__DIR__ . '/../config.php');

require_login(null, false);

$context = context_system::instance();
require_capability('moodle/h5p:updatelibraries', $context);

$pagetitle = get_string('h5pmanage', 'core_h5p');
$url = new \moodle_url("/h5p/libraries.php");

$PAGE->set_context($context);
$PAGE->set_url($url);
$PAGE->set_title($pagetitle);
$PAGE->set_pagelayout('admin');
$PAGE->set_heading($pagetitle);

echo $OUTPUT->header();
echo $OUTPUT->heading($pagetitle);
echo $OUTPUT->box(get_string('librariesmanagerdescription', 'core_h5p'));

$form = new \core_h5p\form\uploadlibraries_form();
if ($data = $form->get_data()) {
require_sesskey();

// Get the file from the users draft area.
$usercontext = context_user::instance($USER->id);
$fs = get_file_storage();
$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->h5ppackage, 'id',
false);
$file = reset($files);

// Validate and save the H5P package.
$h5pfactory = new \core_h5p\factory();
// Because we are passing skipcontent = true to save_h5p function, the returning value is false in an error
// is encountered, null when successfully saving the package without creating the content.
if (\core_h5p\helper::save_h5p($h5pfactory, $file, new stdClass(), false, true) === false) {
echo $OUTPUT->notification(get_string('invalidpackage', 'core_h5p'), 'error');
} else {
echo $OUTPUT->notification(get_string('uploadsuccess', 'core_h5p'), 'success');
}
}
$form->display();
echo $OUTPUT->footer();
10 changes: 10 additions & 0 deletions lang/en/h5p.php
Expand Up @@ -79,6 +79,9 @@
$string['h5pfilenotfound'] = 'H5P file not found';
$string['h5pinvalidurl'] = 'Invalid H5P content URL.';
$string['h5pprivatefile'] = 'This H5P content can\'t be displayed because you don\'t have access to the .h5p file.';
$string['h5pmanage'] = 'Manage H5P content types';
$string['h5ppackage'] = 'H5P content type';
$string['h5ppackage_help'] = 'An H5P content type is a file with an H5P or ZIP extension containing all libraries required to display the content.';
$string['hideadvanced'] = 'Hide advanced';
$string['invalidcontextid'] = 'H5P file not found (invalid contextid)';
$string['invalidfile'] = 'File "{$a->%filename}" not allowed. Only files with the following extensions are allowed: {$a->%files-allowed}.';
Expand All @@ -92,11 +95,16 @@
$string['invalidlibraryproperty'] = 'Can\'t read the property {$a->%property} in {$a->%library}';
$string['invalidmainjson'] = 'A valid main h5p.json file is missing';
$string['invalidmultiselectoption'] = 'Invalid selected option in multi-select.';
$string['invalidpackage'] = 'Invalid H5P content type';
$string['invalidselectoption'] = 'Invalid selected option in select.';
$string['invalidsemanticsjson'] = 'Invalid semantics.json file has been included in the library {$a->%name}';
$string['invalidsemanticstype'] = 'H5P internal error: unknown content type "{$a->@type}" in semantics. Removing content!';
$string['invalidstring'] = 'Provided string is not valid according to regexp in semantics. (value: "{$a->%value}", regexp: "{$a->%regexp}")';
$string['librarydirectoryerror'] = 'Library directory name must match machineName or machineName-majorVersion.minorVersion (from library.json). (Directory: {$a->%directoryName} , machineName: {$a->%machineName}, majorVersion: {$a->%majorVersion}, minorVersion: {$a->%minorVersion})';
$string['librariesmanagerdescription'] = '<p>H5P enables users to create interactive content by providing a range of content types.</p>'.
'<p>To ensure that only trusted H5P content types are used on your site, you need to <i>either</i></p>'.
'<ul><li>Upload H5P content types from h5p.org <i>or</i></li><li>Enable the scheduled task \'Download available H5P content types from h5p.org</li></ul>'.
'<p>Note that users will only be able to use the H5P content types which are installed on your site.</p>';
$string['license'] = 'License';
$string['licenseCC010'] = 'CC0 1.0 Universal (CC0 1.0) Public Domain Dedication';
$string['licenseCC010U'] = 'CC0 1.0 Universal';
Expand Down Expand Up @@ -155,6 +163,8 @@
$string['unpackedFilesExceedsMaxSize'] = 'The total size of the unpacked files exceeds the maximum size allowed. ({$a->%used} > {$a->%max})';
$string['updatedlibraries'] = 'Updated {$a->%old} H5P libraries.';
$string['updatedlibrary'] = 'Updated {$a->%old} H5P library.';
$string['uploadlibraries'] = 'Upload H5P content types';
$string['uploadsuccess'] = 'H5P content types uploaded successfully';
$string['wrongversion'] = 'The version of the H5P library {$a->%machineName} used in this content is not valid. Content contains {$a->%contentLibrary}, but it should be {$a->%semanticsLibrary}.';
$string['year'] = 'Year';
$string['years'] = 'Year(s)';
Expand Down
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2019110500.01; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2019110500.02; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit b57e1c9

Please sign in to comment.