Skip to content

Instantly share code, notes, and snippets.

@nadavkav
Created April 8, 2016 18:32
Show Gist options
  • Save nadavkav/155cc21df9cd19e9ee1f6760462b62cb to your computer and use it in GitHub Desktop.
Save nadavkav/155cc21df9cd19e9ee1f6760462b62cb to your computer and use it in GitHub Desktop.
<?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/>.
/**
* Display a H5P Interactive video embed.
*
* @package filter
* @subpackage hvp
* @copyright 2016 onwards Nadav kavalerchik
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// todo: add syntax instruction (or README)
class filter_hvp extends moodle_text_filter {
protected $settings = null;
protected $core = null;
public function setup($page, $context) {
global $CFG;
if ($page->requires->should_create_one_time_item_now('filter_hvp')) {
require_once($CFG->dirroot.'/mod/hvp/locallib.php');
//require_course_login($course, false, $cm);
// Load H5P Core
$this->core = \mod_hvp\framework::instance();
//$this->settings = hvp_get_core_assets($page);
// Get core settings
// todo: fix me
//$this->settings = hvp_get_core_settings();
$this->settings['core'] = array(
'styles' => array(),
'scripts' => array()
);
$this->settings['loadedJs'] = array();
$this->settings['loadedCss'] = array();
// Make sure files are reloaded for each plugin update
$cache_buster = hvp_get_cache_buster();
// Use relative URL to support both http and https.
$lib_url = $CFG->httpswwwroot . '/mod/hvp/library/';
$rel_path = '/' . preg_replace('/^[^:]+:\/\/[^\/]+\//', '', $lib_url);
// Add core stylesheets
foreach (H5PCore::$styles as $style) {
$this->settings['core']['styles'][] = $rel_path . $style . $cache_buster;
//$page->requires->css(new moodle_url($lib_url . $style . $cache_buster));
}
// Add core JavaScript
foreach (H5PCore::$scripts as $script) {
$this->settings['core']['scripts'][] = $rel_path . $script . $cache_buster;
$page->requires->js(new moodle_url($lib_url . $script . $cache_buster), true);
}
}
}
function filter($text, array $options = array()) {
global $USER, $DB, $PAGE, $CFG;
$CFG->cachetext = false; // very cpu intensive !!!
// Do a quick check to avoid unnecessary work : - Is there instance ? - Are we on the first page ?
if (strpos($text, '[[hvp(') === false) {
return $text;
}
// There is job to do.... so let's do it !
$pattern = '/\[\[hvp\(([0-9]+)\)\]\]/';
// If there is an instance again...
while (preg_match($pattern, $text, $regs)) {
// For each instance
if ($regs[1] > 0) {
$hvpid = $regs[1];
if (! $cm = get_coursemodule_from_id('hvp', $hvpid)) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
print_error('coursemisconf');
}
require_once($CFG->dirroot.'/mod/hvp/locallib.php');
//require_course_login($course, false, $cm);
// Load H5P Core
//$core = \mod_hvp\framework::instance();
$core = $this->core;
// Load H5P Content
$content = $core->loadContent($cm->instance);
if ($content === NULL) {
print_error('invalidhvp');
}
// Mark viewed by user (if required)
$completion = new completion_info($course);
//$completion->set_module_viewed($cm);
// Attach scripts, styles, etc. from core
// todo: fix
//$settings = hvp_get_core_assets();
$settings = $this->settings;
// Add global disable settings
if (!isset($content['disable'])) {
$content['disable'] = $core->getGlobalDisable();
}
else {
$content['disable'] |= $core->getGlobalDisable();
}
// Embed is not supported in Moodle
$content['disable'] |= \H5PCore::DISABLE_EMBED;
// Filter content parameters
$safe_parameters = $core->filterParameters($content);
$export = '';
if (!isset($CFG->mod_hvp_export) || $CFG->mod_hvp_export === TRUE) {
// Find course context
$context = \context_course::instance($course->id);
$hvp_path = "{$CFG->sessioncookiepath}pluginfile.php/{$context->id}/mod_hvp";
$export_filename = ($content['slug'] ? $content['slug'] . '-' : '') . $content['id'] . '.h5p';
$export = "{$hvp_path}/exports/{$export_filename}";
}
// Add JavaScript settings for this content
$cid = 'cid-' . $content['id'];
$settings['contents'][$cid] = array(
'library' => \H5PCore::libraryToString($content['library']),
'jsonContent' => $safe_parameters,
'fullScreen' => $content['library']['fullscreen'],
'exportUrl' => $export,
'title' => $content['title'],
'disable' => $content['disable'],
'url' => "{$CFG->httpswwwroot}/mod/hvp/view.php?id={$hvpid}",
'contentUserData' => array(
0 => array(
'state' => \mod_hvp\content_user_data::load_user_data($content['id'])
)
)
);
// Get assets for this content
$preloaded_dependencies = $core->loadContentDependencies($content['id'], 'preloaded');
$files = $core->getDependenciesFiles($preloaded_dependencies);
// Determine embed type
$embedtype = H5PCore::determineEmbedType($content['embedType'], $content['library']['embedTypes']);
if (true || $embedtype === 'div') {
$context = \context_system::instance();
$hvp_path = "/pluginfile.php/{$context->id}/mod_hvp";
// Schedule JavaScripts for loading through Moodle
foreach ($files['scripts'] as $script) {
$url = $hvp_path . $script->path . $script->version;
$settings['loadedJs'][] = $url;
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . $url), true);
}
// Schedule stylesheets for loading through Moodle
foreach ($files['styles'] as $style) {
$url = $hvp_path . $style->path . $style->version;
$settings['loadedCss'][] = $url;
//$PAGE->requires->css(new moodle_url($CFG->httpswwwroot . $url));
}
}
else {
// JavaScripts and stylesheets will be loaded through h5p.js
$settings['contents'][$cid]['scripts'] = $core->getAssetsUrls($files['scripts']);
$settings['contents'][$cid]['styles'] = $core->getAssetsUrls($files['styles']);
}
// xAPI (ADL) js wrapper.
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/xapi-jswrapper/dist/xapiwrapper.min.js'), true);
// todo: fix
//$PAGE->requires->jquery();
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/js/xapi-stmt-dispatcher.js'), true);
// TODO: move code to use AMD (https://docs.moodle.org/dev/Javascript_Modules & Install grunt to compile AMD)
//$PAGE->requires->js_call_amd('mod_hvp/xapi-stmt-dispatcher', 'initialise');
// Print JavaScript settings to page
$PAGE->requires->data_for_js('H5PIntegration', $settings, true);
// Print page HTML
$hvpembed = '<div class="clearer"></div>';
// Print any messages
//\mod_hvp\framework::printMessages('info', \mod_hvp\framework::messages('info'));
//\mod_hvp\framework::printMessages('error', \mod_hvp\framework::messages('error'));
// Print H5P Content
if ($embedtype === 'div') {
$hvpembed .= '<div class="h5p-content" data-content-id="' . $content['id'] . '"></div>';
}
else {
$hvpembed .= '<div class="h5p-iframe-wrapper"><iframe id="h5p-iframe-' . $content['id'] . '" class="h5p-iframe" data-content-id="' .
$content['id'] . '" style="height:1px" src="about:blank" frameBorder="0" scrolling="no"></iframe></div>';
}
$text = str_replace('[[hvp('.$hvpid.')]]', $hvpembed, $text);
} else {
break;
}
}
return $text;
}
}
@alanpt
Copy link

alanpt commented Sep 7, 2016

I'm very keen to know when you have this working reliably. I have a department of educators really wanting to embed their h5p activities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment