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

HFP-3615 Add support for the H5P OER Hub #150

Open
wants to merge 5 commits into
base: master
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
217 changes: 191 additions & 26 deletions admin/class-h5p-content-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,47 @@ private function current_user_can_edit($content) {
return get_current_user_id() === $author_id;
}

/**
* Check whether current user has permission to share content.
*
* @since 1.15.5
* @param array $content Content data.
* @return bool True, if current user is allowed to share content. Else false.
*/
private function current_user_can_share($content) {
if (
empty(get_option('h5p_hub_is_enabled')) ||
empty(get_option('h5p_h5p_site_uuid')) ||
empty(get_option('h5p_hub_secret'))
) {
return FALSE; // Hub is not ready to be shared with
}

// If you can't share content, neither can you share others contents
if (!current_user_can('share_h5p_contents')) {
return FALSE;
}
if (current_user_can('share_others_h5p_contents')) {
return TRUE;
}
$author_id = (int)(is_array($content) ?
$content['user_id'] :
$content->user_id);

return get_current_user_id() === $author_id;
}

/**
* Determine whether content was shared successfully.
*
* @since 1.15.5
* @return bool True if content was shared.
*/
private function is_content_shared() {
return (int)($this->content['shared']) !==
H5PContentStatus::STATUS_UNPUBLISHED;
}

/**
* Permission check. Can the current user view the given content?
*
Expand Down Expand Up @@ -170,6 +211,68 @@ private function current_user_can_view_content_results($content) {
return $this->current_user_can_edit($content);
}

/**
* Check and update hub status.
*
* @since 1.15.5
* @param array $content Content data.
*/
private function check_update_hub_status($content = []) {
if (empty($content)) {
return; // No content to check
}

if (!$this->current_user_can_share($content)) {
return; // User is not allowed to update status
}

if (
empty($content['contentHubId']) ||
(int)($content['synced']) === H5PContentHubSyncStatus::NOT_SYNCED
) {
return; // Content needs syncing
}

H5PContentSharing::update_hub_status($this->content);
H5P_Plugin_Admin::print_messages();
}

/**
* Display content.
*
* @since 1.15.5
*/
public function display_content() {
// Access restriction
if ($this->current_user_can_view($this->content) == FALSE) {
H5P_Plugin_Admin::set_error(
__('You are not allowed to view this content.', $this->plugin_slug)
);
H5P_Plugin_Admin::print_messages();
return;
}

// Admin preview of H5P content.
if (is_string($this->content)) {
H5P_Plugin_Admin::set_error($this->content);
H5P_Plugin_Admin::print_messages();
}
else {
$plugin = H5P_Plugin::get_instance();
$embed_code = $plugin->add_assets($this->content);
include_once('views/show-content.php');
H5P_Plugin::get_instance()->add_settings();

// Log view
new H5P_Event('content', NULL,
$this->content['id'],
$this->content['title'],
$this->content['library']['name'],
$this->content['library']['majorVersion'] . '.' .
$this->content['library']['minorVersion']);
}
}

/**
* Display a list of all h5p content.
*
Expand Down Expand Up @@ -233,31 +336,8 @@ public function display_contents_page() {
return;

case 'show':
// Access restriction
if ($this->current_user_can_view($this->content) == FALSE) {
H5P_Plugin_Admin::set_error(__('You are not allowed to view this content.', $this->plugin_slug));
H5P_Plugin_Admin::print_messages();
return;
}

// Admin preview of H5P content.
if (is_string($this->content)) {
H5P_Plugin_Admin::set_error($this->content);
H5P_Plugin_Admin::print_messages();
}
else {
$plugin = H5P_Plugin::get_instance();
$embed_code = $plugin->add_assets($this->content);
include_once('views/show-content.php');
H5P_Plugin::get_instance()->add_settings();

// Log view
new H5P_Event('content', NULL,
$this->content['id'],
$this->content['title'],
$this->content['library']['name'],
$this->content['library']['majorVersion'] . '.' . $this->content['library']['minorVersion']);
}
$this->check_update_hub_status($this->content);
$this->display_content();
return;

case 'results':
Expand Down Expand Up @@ -312,6 +392,45 @@ public function display_contents_page() {
$this->content['library']['majorVersion'] . '.' . $this->content['library']['minorVersion']);
}
return;

// Share content on the Hub
case 'share': {
$content_id = (int)filter_input(
INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT
);
H5PContentSharing::display_share_content_form($content_id);
return;
}

// Sync content on the Hub
case 'sync': {
$content_id = (int)filter_input(INPUT_GET, 'id',
FILTER_SANITIZE_NUMBER_INT
);
H5PContentSharing::sync($content_id);

$plugin = H5P_Plugin::get_instance();
$this->content = $plugin->get_content((int)$content_id);

$this->check_update_hub_status($this->content);
$this->display_content();
return;
}

// Share content on the Hub
case 'unshare': {
$content_id = (int)filter_input(
INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT
);
H5PContentSharing::unshare($content_id);

$plugin = H5P_Plugin::get_instance();
$this->content = $plugin->get_content((int)$content_id);

$this->check_update_hub_status($this->content);
$this->display_content();
return;
}
}

print '<div class="wrap"><h2>' . esc_html__('Unknown task.', $this->plugin_slug) . '</h2></div>';
Expand Down Expand Up @@ -1030,6 +1149,9 @@ public function add_editor_assets($id = NULL) {

// Add JavaScript settings
$content_validator = $plugin->get_h5p_instance('contentvalidator');
$content_search_url = H5PHubEndpoints::createURL(H5PHubEndpoints::CONTENT) .
'/search';

$settings['editor'] = array(
'filesPath' => $plugin->get_h5p_url() . '/editor',
'fileIcon' => array(
Expand All @@ -1044,7 +1166,12 @@ public function add_editor_assets($id = NULL) {
'assets' => $assets,
'deleteMessage' => __('Are you sure you wish to delete this content?', $this->plugin_slug),
'apiVersion' => H5PCore::$coreApi,
'language' => $language
'language' => $language,
'hub' => array(
'contentSearchUrl' => $content_search_url,
),
'enableContentHub' => !empty(get_option('h5p_h5p_site_uuid')) &&
!empty(get_option('h5p_hub_secret'))
);

if ($id !== NULL) {
Expand Down Expand Up @@ -1183,4 +1310,42 @@ public function ajax_filter() {
$editor->ajax->action(H5PEditorEndpoints::FILTER, $token, $libraryParameters);
exit;
}

/*
* Handle filtering of parameters through AJAX.
*
* @since 1.15.5
*/
public function ajax_h5p_content_hub_metadata_cache () {
$plugin = H5P_Plugin::get_instance();
$plugin->get_h5p_instance('core');

// Check capability to register
if (!current_user_can('edit_h5p_contents')) {
H5PCore::ajaxError(__('You do not have permission to view the metadata for the content hub.', $plugin->get_plugin_slug()), 'NO_PERMISSION', 403);
wp_die();
}

$editor = $this->get_h5peditor_instance();
$editor->ajax->action(
H5PEditorEndpoints::CONTENT_HUB_METADATA_CACHE, $plugin->get_language()
);
exit;
}

/**
* Get content from the H5P Hub.
*
* @since 1.15.5
*/
public function ajax_h5p_get_content() {
$token = filter_input(INPUT_GET, 'token', FILTER_SANITIZE_STRING);
$hubid = filter_input(INPUT_GET, 'hubId');

$editor = $this->get_h5peditor_instance();
$editor->ajax->action(
H5PEditorEndpoints::GET_HUB_CONTENT, $token, $hubid, NULL
);
exit;
}
}
Loading