From 86b06a7b9a96e7515883ec1f333ba9f5137d7bc9 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Thu, 10 Jun 2021 17:07:30 +0200 Subject: [PATCH] MDL-71885 core_h5p: Add the form for editing content The editor form is based on the code that Victor Deniz prepared while he was working on the integration of the H5P editor into Moodle. The original version of this file can be found in MDL-67814. --- h5p/classes/form/editcontent_form.php | 83 +++++++++++++++++++ h5p/edit.php | 114 ++++++++++++++++++++++++++ h5p/upgrade.txt | 1 + lang/en/h5p.php | 3 + 4 files changed, 201 insertions(+) create mode 100644 h5p/classes/form/editcontent_form.php create mode 100644 h5p/edit.php 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 @@ +. + +namespace core_h5p\form; + +use core_h5p\editor; + +/** + * Form to edit an existing H5P content. + * + * @package core_h5p + * @copyright 2020 Victor Deniz + * @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 @@ +. + +/** + * Open the editor to modify an H5P content from a given H5P URL. + * + * @package core_h5p + * @copyright 2021 Sara Arjona + * @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.';