mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-67810 core_contentbank: contenttype_h5p editor integrated
- Loading branch information
Showing
5 changed files
with
452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Provides {@see \core_contentbank\form\edit_content} class. | ||
* | ||
* @package core_contentbank | ||
* @copyright 2020 Victor Deniz <victor@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace core_contentbank\form; | ||
|
||
use moodleform; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
require_once($CFG->libdir.'/formslib.php'); | ||
|
||
/** | ||
* Defines the form for editing a content. | ||
* | ||
* @package core_contentbank | ||
* @copyright 2020 Victor Deniz <victor@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
abstract class edit_content extends moodleform { | ||
|
||
/** @var int Context the content belongs to. */ | ||
protected $contextid; | ||
|
||
/** @var string Content type plugin name. */ | ||
protected $plugin; | ||
|
||
/** @var int Content id in the content bank. */ | ||
protected $id; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $action The action attribute for the form. | ||
* @param array $customdata Data to set during instance creation. | ||
* @param string $method Form method. | ||
*/ | ||
public function __construct(string $action = null, array $customdata = null, string $method = 'post') { | ||
parent::__construct($action, $customdata, $method); | ||
$this->contextid = $customdata['contextid']; | ||
$this->plugin = $customdata['plugin']; | ||
$this->id = $customdata['id']; | ||
|
||
$mform =& $this->_form; | ||
$mform->addElement('hidden', 'contextid', $this->contextid); | ||
$this->_form->setType('contextid', PARAM_INT); | ||
|
||
$mform->addElement('hidden', 'plugin', $this->plugin); | ||
$this->_form->setType('plugin', PARAM_PLUGIN); | ||
|
||
$mform->addElement('hidden', 'id', $this->id); | ||
$this->_form->setType('id', PARAM_INT); | ||
} | ||
|
||
/** | ||
* Overrides formslib's add_action_buttons() method. | ||
* | ||
* | ||
* @param bool $cancel | ||
* @param string|null $submitlabel | ||
* | ||
* @return void | ||
*/ | ||
public function add_action_buttons($cancel = true, $submitlabel = null): void { | ||
if (is_null($submitlabel)) { | ||
$submitlabel = get_string('save'); | ||
} | ||
parent::add_action_buttons($cancel, $submitlabel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Provides the class that defines the form for the H5P authoring tool. | ||
* | ||
* @package contenttype_h5p | ||
* @copyright 2020 Victor Deniz <victor@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace contenttype_h5p\form; | ||
|
||
use contenttype_h5p\content; | ||
use contenttype_h5p\contenttype; | ||
use core_contentbank\form\edit_content; | ||
use core_h5p\api; | ||
use core_h5p\editor as h5peditor; | ||
use core_h5p\factory; | ||
use stdClass; | ||
|
||
/** | ||
* Defines the form for editing an H5P content. | ||
* | ||
* @copyright 2020 Victor Deniz <victor@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class editor extends edit_content { | ||
|
||
/** @var $h5peditor H5P editor object */ | ||
private $h5peditor; | ||
|
||
/** @var $content The content being edited */ | ||
private $content; | ||
|
||
/** | ||
* Defines the form fields. | ||
*/ | ||
protected function definition() { | ||
global $DB; | ||
|
||
$mform = $this->_form; | ||
|
||
// Id of the content to edit. | ||
$id = $this->_customdata['id']; | ||
// H5P content type to create. | ||
$library = optional_param('library', null, PARAM_TEXT); | ||
|
||
if (empty($id) && empty($library)) { | ||
$returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $this->_customdata['contextid']]); | ||
print_error('invalidcontentid', 'error', $returnurl); | ||
} | ||
|
||
$this->h5peditor = new h5peditor(); | ||
|
||
if ($id) { | ||
// The H5P editor needs the H5P content id (h5p table). | ||
$record = $DB->get_record('contentbank_content', ['id' => $id]); | ||
$this->content = new content($record); | ||
$file = $this->content->get_file(); | ||
|
||
$h5p = api::get_content_from_pathnamehash($file->get_pathnamehash()); | ||
$mform->addElement('hidden', 'h5pid', $h5p->id); | ||
$mform->setType('h5pid', PARAM_INT); | ||
$this->h5peditor->set_content($h5p->id); | ||
} else { | ||
// The H5P editor needs the H5P content type library name for a new content. | ||
$mform->addElement('hidden', 'library', $library); | ||
$mform->setType('library', PARAM_TEXT); | ||
$this->h5peditor->set_library($library, $this->_customdata['contextid'], 'contentbank', 'public'); | ||
} | ||
|
||
$mformid = 'coolh5peditor'; | ||
$mform->setAttributes(array('id' => $mformid) + $mform->getAttributes()); | ||
|
||
$this->add_action_buttons(); | ||
|
||
$this->h5peditor->add_editor_to_form($mform); | ||
|
||
$this->add_action_buttons(); | ||
} | ||
|
||
/** | ||
* Modify or create an H5P content from the form data. | ||
* | ||
* @param stdClass $data Form data to create or modify an H5P content. | ||
* | ||
* @return int The id of the edited or created content. | ||
*/ | ||
public function save_content(stdClass $data): int { | ||
global $DB; | ||
|
||
// The H5P libraries expect data->id as the H5P content id. | ||
// The method \H5PCore::saveContent throws an error if id is set but empty. | ||
if (empty($data->id)) { | ||
unset($data->id); | ||
} else { | ||
// The H5P libraries save in $data->id the H5P content id (h5p table), so the content id is saved in another var. | ||
$contentid = $data->id; | ||
} | ||
|
||
$h5pcontentid = $this->h5peditor->save_content($data); | ||
|
||
$factory = new factory(); | ||
$h5pfs = $factory->get_framework(); | ||
|
||
// Needs the H5P file id to create or update the content bank record. | ||
$h5pcontent = $h5pfs->loadContent($h5pcontentid); | ||
$fs = get_file_storage(); | ||
$file = $fs->get_file_by_hash($h5pcontent['pathnamehash']); | ||
// Creating new content. | ||
if (!isset($data->h5pid)) { | ||
// The initial name of the content is the title of the H5P content. | ||
$cbrecord = new stdClass(); | ||
$cbrecord->name = json_decode($data->h5pparams)->metadata->title; | ||
$context = \context::instance_by_id($data->contextid, MUST_EXIST); | ||
// Create entry in content bank. | ||
$contenttype = new contenttype($context); | ||
$newcontent = $contenttype->create_content($cbrecord); | ||
if ($file && $newcontent) { | ||
$updatedfilerecord = new stdClass(); | ||
$updatedfilerecord->id = $file->get_id(); | ||
$updatedfilerecord->itemid = $newcontent->get_id(); | ||
// As itemid changed, the pathnamehash has to be updated in the file table. | ||
$pathnamehash = \file_storage::get_pathname_hash($file->get_contextid(), $file->get_component(), | ||
$file->get_filearea(), $updatedfilerecord->itemid, $file->get_filepath(), $file->get_filename()); | ||
$updatedfilerecord->pathnamehash = $pathnamehash; | ||
$DB->update_record('files', $updatedfilerecord); | ||
// The pathnamehash in the h5p table must match the file pathnamehash. | ||
$h5pfs->updateContentFields($h5pcontentid, ['pathnamehash' => $pathnamehash]); | ||
} | ||
} else { | ||
// Update content. | ||
$this->content->update_content(); | ||
} | ||
|
||
return $contentid ?? $newcontent->get_id(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Create or update contents through the specific content type editor | ||
* | ||
* @package core_contentbank | ||
* @copyright 2020 Victor Deniz <victor@moodle.com> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
require('../config.php'); | ||
|
||
require_login(); | ||
|
||
$contextid = required_param('contextid', PARAM_INT); | ||
$pluginname = required_param('plugin', PARAM_PLUGIN); | ||
$id = optional_param('id', null, PARAM_INT); | ||
$context = context::instance_by_id($contextid, MUST_EXIST); | ||
require_capability('moodle/contentbank:access', $context); | ||
|
||
$returnurl = new \moodle_url('/contentbank/view.php', ['id' => $id]); | ||
|
||
if (!empty($id)) { | ||
$record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST); | ||
$contentclass = "$record->contenttype\\content"; | ||
$content = new $contentclass($record); | ||
// Set the heading title. | ||
$heading = $content->get_name(); | ||
// The content type of the content overwrites the pluginname param value. | ||
$contenttypename = $content->get_content_type(); | ||
} else { | ||
$contenttypename = "contenttype_$pluginname"; | ||
$heading = get_string('addinganew', 'moodle', get_string('description', $contenttypename)); | ||
} | ||
|
||
// Check plugin is enabled. | ||
$plugin = core_plugin_manager::instance()->get_plugin_info($contenttypename); | ||
if (!$plugin || !$plugin->is_enabled()) { | ||
print_error('unsupported', 'core_contentbank', $returnurl); | ||
} | ||
|
||
// Create content type instance. | ||
$contenttypeclass = "$contenttypename\\contenttype"; | ||
if (class_exists($contenttypeclass)) { | ||
$contenttype = new $contenttypeclass($context); | ||
} else { | ||
print_error('unsupported', 'core_contentbank', $returnurl); | ||
} | ||
|
||
// Checks the user can edit this content type. | ||
if (!$contenttype->can_edit()) { | ||
print_error('contenttypenoedit', 'core_contentbank', $returnurl, $contenttype->get_plugin_name()); | ||
} | ||
|
||
$values = [ | ||
'contextid' => $contextid, | ||
'plugin' => $pluginname, | ||
'id' => $id | ||
]; | ||
|
||
$title = get_string('contentbank'); | ||
\core_contentbank\helper::get_page_ready($context, $title, true); | ||
if ($PAGE->course) { | ||
require_login($PAGE->course->id); | ||
} | ||
|
||
$PAGE->set_url(new \moodle_url('/contentbank/edit.php', $values)); | ||
$PAGE->set_context($context); | ||
$PAGE->navbar->add(get_string('edit')); | ||
$PAGE->set_title($title); | ||
|
||
$PAGE->set_heading($heading); | ||
|
||
// Instantiate the content type form. | ||
$editorclass = "$contenttypename\\form\\editor"; | ||
if (!class_exists($editorclass)) { | ||
print_error('noformdesc'); | ||
} | ||
|
||
$editorform = new $editorclass(null, $values); | ||
|
||
if ($editorform->is_cancelled()) { | ||
if (empty($id)) { | ||
$returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]); | ||
} | ||
redirect($returnurl); | ||
} else if ($data = $editorform->get_data()) { | ||
$id = $editorform->save_content($data); | ||
// Just in case we've created a new content. | ||
$returnurl->param('id', $id); | ||
redirect($returnurl); | ||
} | ||
|
||
echo $OUTPUT->header(); | ||
$editorform->display(); | ||
echo $OUTPUT->footer(); |
Oops, something went wrong.