diff --git a/h5p/classes/form/editcontent_form.php b/h5p/classes/form/editcontent_form.php new file mode 100644 index 00000000000..f9a1dca1993 --- /dev/null +++ b/h5p/classes/form/editcontent_form.php @@ -0,0 +1,83 @@ +<?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/>. + +namespace core_h5p\form; + +use core_h5p\editor; + +/** + * Form to edit an existing H5P content. + * + * @package core_h5p + * @copyright 2020 Victor Deniz <victor@moodle.com> + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class editcontent_form extends \moodleform { + + /** @var editor H5P editor object */ + private $editor; + + /** + * The form definition. + */ + public function definition() { + + $mform = $this->_form; + $id = $this->_customdata['id'] ?? null; + $contenturl = $this->_customdata['contenturl'] ?? null; + $returnurl = $this->_customdata['returnurl'] ?? null; + + $editor = new editor(); + + if ($id) { + $mform->addElement('hidden', 'id', $id); + $mform->setType('id', PARAM_INT); + + $editor->set_content($id); + } + + if ($contenturl) { + $mform->addElement('hidden', 'url', $contenturl); + $mform->setType('url', PARAM_LOCALURL); + } + + if ($returnurl) { + $mform->addElement('hidden', 'returnurl', $returnurl); + $mform->setType('returnurl', PARAM_LOCALURL); + } + + $this->editor = $editor; + $mformid = 'h5peditor'; + $mform->setAttributes(array('id' => $mformid) + $mform->getAttributes()); + + $this->add_action_buttons(); + + $editor->add_editor_to_form($mform); + + $this->add_action_buttons(); + } + + /** + * Updates an H5P content. + * + * @param \stdClass $data Object with all the H5P data. + * + * @return void + */ + public function save_h5p(\stdClass $data): void { + $this->editor->save_content($data); + } +} diff --git a/h5p/edit.php b/h5p/edit.php new file mode 100644 index 00000000000..a524967736e --- /dev/null +++ b/h5p/edit.php @@ -0,0 +1,114 @@ +<?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/>. + +/** + * Open the editor to modify an H5P content from a given H5P URL. + * + * @package core_h5p + * @copyright 2021 Sara Arjona <sara@moodle.com> + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../config.php'); +require_once("$CFG->libdir/formslib.php"); +require_once("$CFG->libdir/filestorage/file_storage.php"); + +require_login(null, false); + +$contenturl = required_param('url', PARAM_LOCALURL); +$returnurl = optional_param('returnurl', null, PARAM_LOCALURL); + +// If no returnurl is defined, use local_referer. +if (empty($returnurl)) { + $returnurl = get_local_referer(false); + if (empty($returnurl)) { + // If local referer is empty, returnurl will be set to default site page. + $returnurl = new \moodle_url('/'); + } +} + +$contentid = null; +$isreferenced = false; +$context = \context_system::instance(); +if (!empty($contenturl)) { + list($originalfile, $h5p, $file) = \core_h5p\api::get_original_content_from_pluginfile_url($contenturl); + $isreferenced = ($file !== false); + if ($originalfile) { + // Check if the user can edit the content behind the given URL. + if (\core_h5p\api::can_edit_content($originalfile)) { + if (!$h5p) { + // This H5P file hasn't been deployed yet, so it should be saved to create the entry into the H5P DB. + \core_h5p\local\library\autoloader::register(); + $factory = new \core_h5p\factory(); + $config = new \stdClass(); + $onlyupdatelibs = !\core_h5p\helper::can_update_library($originalfile); + $contentid = \core_h5p\helper::save_h5p($factory, $originalfile, $config, $onlyupdatelibs, false); + } else { + // The H5P content exists. Update the contentid value. + $contentid = $h5p->id; + } + } + if ($file) { + list($context, $course, $cm) = get_context_info_array($file->get_contextid()); + if ($course) { + $context = \context_course::instance($course->id); + } + } else { + list($context, $course, $cm) = get_context_info_array($originalfile->get_contextid()); + if ($course) { + $context = \context_course::instance($course->id); + } + } + } +} + +if (empty($contentid)) { + throw new \moodle_exception('error:emptycontentid', 'core_h5p', $returnurl); +} + +$pagetitle = get_string('h5peditor', 'core_h5p'); +$url = new \moodle_url("/h5p/edit.php"); + +$PAGE->set_context($context); +$PAGE->set_url($url); +$PAGE->set_title($pagetitle); +$PAGE->set_heading($pagetitle); + +$values = [ + 'id' => $contentid, + 'contenturl' => $contenturl, + 'returnurl' => $returnurl, +]; + +$form = new \core_h5p\form\editcontent_form(null, $values); +if ($form->is_cancelled()) { + redirect($returnurl); +} else if ($data = $form->get_data()) { + $form->save_h5p($data); + if (!empty($returnurl)) { + redirect($returnurl); + } +} + +echo $OUTPUT->header(); + +if ($isreferenced) { + echo $OUTPUT->notification(get_string('contentinuse', 'core_h5p'), 'info'); +} + +$form->display(); + +echo $OUTPUT->footer(); diff --git a/h5p/upgrade.txt b/h5p/upgrade.txt index 4e7fbf614de..03828711b38 100644 --- a/h5p/upgrade.txt +++ b/h5p/upgrade.txt @@ -3,6 +3,7 @@ information provided here is intended especially for developers. === 4.0 === * Added new methods to api: get_original_content_from_pluginfile_url and can_edit_content. +* Added edit.php and editcontent_form class, for modifying H5P content given an H5P identifier (from the h5p table). === 3.11 === * Added $skipcapcheck parameter to H5P constructor, api::create_content_from_pluginfile_url() and diff --git a/lang/en/h5p.php b/lang/en/h5p.php index 73c67156e05..84a0fefda11 100644 --- a/lang/en/h5p.php +++ b/lang/en/h5p.php @@ -59,6 +59,7 @@ $string['connectionLost'] = 'Connection lost. Results will be stored and sent wh $string['connectionReestablished'] = 'Connection reestablished.'; $string['contentCopied'] = 'Content is copied to the clipboard'; $string['contentchanged'] = 'This content has changed since you last used it.'; +$string['contentinuse'] = 'This content may be in use in other places.'; $string['contenttype'] = 'Content type'; $string['copyright'] = 'Rights of use'; $string['copyrightinfo'] = 'Copyright information'; @@ -78,6 +79,7 @@ $string['downloadtitle'] = 'Download this content as a H5P file.'; $string['editor'] = 'Editor'; $string['embed'] = 'Embed'; $string['embedtitle'] = 'View the embed code for this content.'; +$string['error:emptycontentid'] = 'The given URL is incorrect or you cannot edit this file.'; $string['eventh5pviewed'] = 'H5P content viewed'; $string['eventh5pdeleted'] = 'H5P deleted'; $string['feature'] = 'Feature'; @@ -88,6 +90,7 @@ $string['filter_displayh5p_description'] = 'The Display H5P filter converts URLs $string['fullscreen'] = 'Fullscreen'; $string['gpl'] = 'General Public License v3'; $string['h5p'] = 'H5P'; +$string['h5peditor'] = 'H5P Editor'; $string['h5ptitle'] = 'Visit h5p.org to check out more content.'; $string['h5pfilenotfound'] = 'H5P file not found'; $string['h5pinvalidurl'] = 'Invalid H5P content URL.';