Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to allow H5P items to be embedded onto the course page as an … #232

Open
wants to merge 3 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion classes/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,9 @@ public function updateContent($content, $contentmainid = null) {
'filtered' => '',
'disable' => $content['disable'],
'timemodified' => time(),
'autoembed' => intval($content['autoembed']),
'mobiledelay' => intval($content['mobiledelay']),
'embedmaxwidth' => intval($content['embedmaxwidth'])
));

if (!isset($content['id'])) {
Expand Down Expand Up @@ -1075,7 +1078,10 @@ public function loadContent($id) {
hc.year_from,
hc.year_to,
hc.changes,
hc.author_comments
hc.author_comments,
hc.autoembed,
hc.mobiledelay,
hc.embedmaxwidth
FROM {hvp} hc
JOIN {hvp_libraries} hl ON hl.id = hc.main_library_id
WHERE hc.id = ?", array($id)
Expand Down Expand Up @@ -1104,6 +1110,9 @@ public function loadContent($id) {
'libraryMinorVersion' => $data->minor_version,
'libraryEmbedTypes' => $data->embed_types,
'libraryFullscreen' => $data->fullscreen,
'autoembed' => $data->autoembed,
'mobiledelay' => $data->mobiledelay,
'embedmaxwidth' => $data->embedmaxwidth,
);

$metadatafields = [
Expand Down
3 changes: 3 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<FIELD NAME="slug" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Human readable content identifier that is unique"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="autoembed" TYPE="int" LENGTH="10" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="mobiledelay" TYPE="int" LENGTH="10" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="embedmaxwidth" TYPE="char" LENGTH="7" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
31 changes: 30 additions & 1 deletion db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,34 @@ function hvp_upgrade_2018090300() {
}
}

/**
* Add autoembed to hvp table
*/
function hvp_upgrade_2018112201() {
global $DB;
$dbman = $DB->get_manager();

// Add autoembed to hvp options.
$table = new xmldb_table('hvp');
if ($dbman->table_exists($table)) {

$autoembed = new xmldb_field('autoembed', XMLDB_TYPE_INTEGER, '10');
if (!$dbman->field_exists($table, $autoembed)) {
$dbman->add_field($table, $autoembed);
}

$delayembedformobile = new xmldb_field('mobiledelay', XMLDB_TYPE_INTEGER, '10');
if (!$dbman->field_exists($table, $delayembedformobile)) {
$dbman->add_field($table, $delayembedformobile);
}

$embedmaxwidth = new xmldb_field('embedmaxwidth', XMLDB_TYPE_CHAR, '50');
if (!$dbman->field_exists($table, $embedmaxwidth)) {
$dbman->add_field($table, $embedmaxwidth);
}
}
}

/**
* Hvp module upgrade function.
*
Expand All @@ -451,7 +479,8 @@ function xmldb_hvp_upgrade($oldversion) {
2017040500,
2017050900,
2017060900,
2018090300
2018090300,
2018112201
];

foreach ($upgrades as $version) {
Expand Down
20 changes: 20 additions & 0 deletions h5p-autoembed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var embedlistiner = function() {
var id = this.getAttribute('data-ref');
this.style.display = 'none';
document.getElementById('hvpe' + id ).height = '50';
document.getElementById('hvpe' + id).src = '/mod/hvp/embed.php?id=' + id;
document.getElementById('hvpe' + id).addEventListener('load', function(){
this.className = this.className.replace(/\bmobiledelay\b/g, "");
}, true);
};

var hvpembedbuttons = document.getElementsByClassName('mobileautoembed');
for (var i = 0; i < hvpembedbuttons.length; i++) {
hvpembedbuttons[i].addEventListener("click", embedlistiner);
}

var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.hvpautoembed a img{display:none} .section .activity.hvpautoembed .contentafterlink{margin-left:0}';
style.innerHTML += '.hvpautoembed .mobiledelay{background:url("/mod/hvp/library/images/throbber.gif") no-repeat center #e2e2e2';
document.getElementsByTagName('head')[0].appendChild(style);
6 changes: 6 additions & 0 deletions lang/en/hvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,9 @@
$string['privacy:metadata:hvp_xapi_results:additionals'] = 'Additional information that the H5P can send in.';
$string['privacy:metadata:hvp_xapi_results:raw_score'] = 'Achieved score for the event.';
$string['privacy:metadata:hvp_xapi_results:max_score'] = 'Max achievable score for the event.';

// Autoembed.
$string['autoembed'] ='Automatically embed this H5P';
$string['mobiledelay'] = 'Don\'t auto embed for Mobile devices';
$string['mobilebutton'] = 'Show {$a}';
$string['embedmaxwidth'] = 'Set the max width (in pixels) for Auto Embedded content';
55 changes: 55 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,58 @@ function hvp_update_grades($hvp=null, $userid=0, $nullifnone=true) {
hvp_grade_item_update($hvp);
}
}

/**
* @param cm_info $cm
* @throws dml_exception
*/
function mod_hvp_cm_info_view(cm_info $cm) {
global $DB, $PAGE;

// No Embed if page is being edited.
if (!$PAGE->user_is_editing()) {
$extrascript = '';
if ($DB->record_exists('hvp', ['id' => $cm->instance])) {
$hvp = $DB->get_record('hvp', ['id' => $cm->instance], "autoembed, mobiledelay, embedmaxwidth");
if ($hvp->autoembed) {
$extrascript = autoembedhvp($cm->id, 50);
$cm->set_extra_classes('hvpautoembed');
}
if ($hvp->mobiledelay && (core_useragent::is_ios() || core_useragent::is_webkit_android())) {
$extrascript = autoembedhvp($cm->id, 0);
$extrascript .= '<button class="btn btn-primary mobileautoembed" data-ref="' . $cm->id . '" '
. '>' . get_string('mobilebutton', 'hvp', $cm->get_formatted_name()) . '</button>';
$cm->set_extra_classes('hvpautoembed');
}
if (intval($hvp->embedmaxwidth) > 0) {
$extrascript = '<div style="max-width:' . intval($hvp->embedmaxwidth) . 'px"> '
. $extrascript
. '</div>';
}
}

// No Embed if the item is hidden.
if ($cm->uservisible) {
$cm->set_content($cm->content . $extrascript);
}
}
}

/**
* @param $id
* @param int $height
* @return string
*/
function autoembedhvp($id, $height = 0) {
global $PAGE;
$class = $height ? '' : 'mobiledelay';
$src = $height ? 'src="/mod/hvp/embed.php?id=' . $id . '"' : '';

$PAGE->requires->js('/mod/hvp/library/js/h5p-resizer.js');
$PAGE->requires->js('/mod/hvp/h5p-autoembed.js');

// Embed the H5P.
return '<iframe class="hvpautoembed ' . $class .'" '
. 'id="hvpe' . $id . '" ' . $src
. ' width="100%" height="' . $height . '" frameborder="0" allowfullscreen="allowfullscreen"></iframe>';
}
13 changes: 13 additions & 0 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ public function definition() {
}
}

// Autoembed checkbox.
$mform->addElement('checkbox', 'autoembed', get_string('autoembed', 'hvp'));
$mform->setType('autoembed', PARAM_BOOL);

// Prevent mobile from autoembeding.
$mform->addElement('checkbox', 'mobiledelay', get_string('mobiledelay', 'hvp'));
$mform->setType('mobiledelay', PARAM_BOOL);

// Set a max width for embeded items.
$mform->addElement('text', 'embedmaxwidth', get_string('embedmaxwidth', 'hvp'));
$mform->setType('embedmaxwidth', PARAM_INT);
$mform->addRule('embedmaxwidth', get_string('maximumchars', '', 10), 'maxlength', 10, 'client');

// Grade settings.
$this->standard_grading_coursemodule_elements();
$mform->removeElement('grade');
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

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

$plugin->version = 2018112200;
$plugin->version = 2018112201;
$plugin->requires = 2013051403;
$plugin->cron = 0;
$plugin->component = 'mod_hvp';
Expand Down