Skip to content

Commit

Permalink
Merge branch 'content-hub' of github.com:h5p/moodle-mod_hvp into cont…
Browse files Browse the repository at this point in the history
…ent-hub
  • Loading branch information
icc committed Dec 1, 2020
2 parents 9dfb36c + cf0758a commit 2a64196
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 122 deletions.
32 changes: 15 additions & 17 deletions ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@
}
$logo = isset($_FILES['logo']) ? $_FILES['logo'] : null;

$formData = [
$formdata = [
'name' => required_param('name', PARAM_TEXT),
'email' => required_param('email', PARAM_EMAIL),
'description' => optional_param('description', '', PARAM_TEXT),
Expand All @@ -379,7 +379,7 @@
];

$core = framework::instance();
$result = $core->hubRegisterAccount($formData, $logo);
$result = $core->hubRegisterAccount($formdata, $logo);

if ($result['success'] === false) {
$core->h5pF->setErrorMessage($result['message']);
Expand Down Expand Up @@ -439,7 +439,7 @@

// Update Hub status for content before proceeding
$newstate = hvp_update_hub_status($content);
$synced = $newstate ? $newstate : intval($content['synced']);
$synced = $newstate !== false ? $newstate : intval($content['synced']);

if ($synced === \H5PContentHubSyncStatus::WAITING) {
\H5PCore::ajaxError(get_string('contentissyncing', 'hvp'));
Expand All @@ -462,37 +462,35 @@

// Add the icon and any screenshots
$files = array(
'icon' => !empty($_FILES['icon']) ? $_FILES['icon'] : NULL,
'screenshots' => !empty($_FILES['screenshots']) ? $_FILES['screenshots'] : NULL,
'icon' => !empty($_FILES['icon']) ? $_FILES['icon'] : null,
'screenshots' => !empty($_FILES['screenshots']) ? $_FILES['screenshots'] : null,
);

try {
$isedit = !empty($content['contentHubId']);
$updatecontent = $synced === \H5PContentHubSyncStatus::NOT_SYNCED && $isedit;
if ($updatecontent) {
// node has been edited since the last time it was published
$data['resync'] = 1;
// node has been edited since the last time it was published
$data['resync'] = 1;
}
$result = $core->hubPublishContent($data, $files, $isedit ? $content['contentHubId'] : NULL);
$result = $core->hubPublishContent($data, $files, $isedit ? $content['contentHubId'] : null);

$fields = array(
'shared' => 1, // Content is always shared after sharing or editing
'shared' => 1, // Content is always shared after sharing or editing
);
if (!$isedit) {
$fields['hub_id'] = $result->content->id;
// Sync will not happen on 'edit info', only for 'publish' or 'sync'.
$fields['synced'] = \H5PContentHubSyncStatus::WAITING;
}
else if ($updatecontent) {
$fields['synced'] = \H5PContentHubSyncStatus::WAITING;
$fields['hub_id'] = $result->content->id;
// Sync will not happen on 'edit info', only for 'publish' or 'sync'.
$fields['synced'] = \H5PContentHubSyncStatus::WAITING;
} else if ($updatecontent) {
$fields['synced'] = \H5PContentHubSyncStatus::WAITING;
}

// Store the content hub id
$core->h5pF->updateContentFields($cm->instance, $fields);

H5PCore::ajaxSuccess();
}
catch (Exception $e) {
} catch (Exception $e) {
H5PCore::ajaxError(!empty($e->errors) ? $e->errors : $e->getMessage());
}

Expand Down
10 changes: 6 additions & 4 deletions classes/admin_setting_html.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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

/**
* No setting - just html
* Note: since admin_setting is not namespaced, this can not be namespaced and put into a class
*/
class admin_setting_html extends admin_setting {

private $hubInfo;
private $hubinfo;

/**
* not a setting, just html
*
* @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
*/
public function __construct($name, $translation, $hubInfo) {
public function __construct($name, $translation, $hubinfo) {
$this->nosave = true;
$this->hubInfo = $hubInfo;
$this->hubinfo = $hubinfo;
parent::__construct($name, $translation, '', '');
}

Expand Down Expand Up @@ -64,6 +66,6 @@ public function write_setting($data) {
*/
public function output_html($data, $query = '') {
global $OUTPUT;
return $OUTPUT->render_from_template('mod_hvp/content_hub_registration_box', $this->hubInfo);
return $OUTPUT->render_from_template('mod_hvp/content_hub_registration_box', $this->hubinfo);
}
}
24 changes: 12 additions & 12 deletions classes/content_hub_service.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ class content_hub_service {
* @return array
* @throws Exception
*/
public static function getRegistrationUISettings() {
$core = framework::instance();
$registrationUrl = new moodle_url('/mod/hvp/ajax.php', [
public static function get_registration_ui_settings() {
$core = framework::instance();
$registrationurl = new moodle_url('/mod/hvp/ajax.php', [
'action' => 'contenthubregistration',
]);
$accountSettingsUrl = new moodle_url('/admin/settings.php?section=modsettinghvp');
$accountsettingsurl = new moodle_url('/admin/settings.php?section=modsettinghvp');

return [
'registrationURL' => $registrationUrl->out(true),
'accountSettingsUrl' => $accountSettingsUrl->out(true),
'token' => $core::createToken('contentHubRegistration'),
'l10n' => $core->getLocalization(),
'licenseAgreementTitle' => get_string('contenthub:licenseagreementtitle', 'hvp',),
'licenseAgreementDescription' => get_string('contenthub:licenseagreementdescription', 'hvp',),
'licenseAgreementMainText' => get_string('contenthub:licenseagreementmaintext', 'hvp',),
'accountInfo' => $core->hubAccountInfo(),
'registrationURL' => $registrationurl->out(true),
'accountSettingsUrl' => $accountsettingsurl->out(true),
'token' => $core::createToken('contentHubRegistration'),
'l10n' => $core->getLocalization(),
'licenseAgreementTitle' => get_string('contenthub:licenseagreementtitle', 'hvp'),
'licenseAgreementDescription' => get_string('contenthub:licenseagreementdescription', 'hvp'),
'licenseAgreementMainText' => get_string('contenthub:licenseagreementmaintext', 'hvp'),
'accountInfo' => $core->hubAccountInfo(),
];
}
}
81 changes: 37 additions & 44 deletions classes/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function getPlatformInfo() {
* @inheritdoc
*/
// @codingStandardsIgnoreLine
public function fetchExternalData($url, $data = null, $blocking = true, $stream = null, $allData = false, $headers = array(), $files = array(), $method = 'POST') {
public function fetchExternalData($url, $data = null, $blocking = true, $stream = null, $alldata = false, $headers = array(), $files = array(), $method = 'POST') {
global $CFG;

if (!empty($files)) {
Expand All @@ -177,29 +177,25 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream
foreach ($value as $subvalue) {
$curldata[$key . '[]'] = $subvalue;
}
}
else {
} else {
$curldata[$key] = $value;
}
}

foreach ($files as $name => $file) {
if ($file === NULL) {
if ($file === null) {
continue;
}
elseif (is_array($file['name'])) {
} else if (is_array($file['name'])) {
// Array of files uploaded (multiple)
for ($i = 0; $i < count($file['name']); $i++) {
for ($i = 0; $i < count($file['name']); $i ++) {
$curldata[$name . '[]'] = new \CurlFile($file['tmp_name'][$i], $file['type'][$i], $file['name'][$i]);
}
}
else {
} else {
// Single file
$curldata[$name] = new \CurlFile($file['tmp_name'], $file['type'], $file['name']);
}
}
}
elseif (!empty($data)) {
} else if (!empty($data)) {
// application/x-www-form-urlencoded
$curldata = format_postdata_for_curlcall($data);
}
Expand All @@ -208,10 +204,10 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream
'CURLOPT_SSL_VERIFYPEER' => true,
'CURLOPT_CONNECTTIMEOUT' => 20,
'CURLOPT_FOLLOWLOCATION' => 1,
'CURLOPT_MAXREDIRS' => 5,
'CURLOPT_MAXREDIRS' => 5,
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_NOBODY' => false,
'CURLOPT_TIMEOUT' => 300,
'CURLOPT_NOBODY' => false,
'CURLOPT_TIMEOUT' => 300,
);

if ($stream !== null) {
Expand All @@ -220,14 +216,14 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream

// Generate local tmp file path.
$localfolder = $CFG->tempdir . uniqid('/hvp-');
$stream = $localfolder . '.h5p';
$stream = $localfolder . '.h5p';

// Add folder and file paths to H5P Core.
$interface = self::instance('interface');
$interface->getUploadedH5pFolderPath($localfolder);
$interface->getUploadedH5pPath($stream);

$stream = fopen($stream, 'w');
$stream = fopen($stream, 'w');
$options['CURLOPT_FILE'] = $stream;
}

Expand All @@ -240,11 +236,9 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream

if (empty($data) || $method === 'GET') {
$response = $curl->get($url, array(), $options);
}
elseif ($method === 'POST') {
} else if ($method === 'POST') {
$response = $curl->post($url, $curldata, $options);
}
elseif ($method === 'PUT') {
} else if ($method === 'PUT') {
$response = $curl->put($url, $curldata, $options);
}

Expand All @@ -253,27 +247,27 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream
@chmod($stream, $CFG->filepermissions);
}

$error_no = $curl->get_errno();
$errorno = $curl->get_errno();
// Error handling
if ($error_no) {
if ($allData) {
if ($errorno) {
if ($alldata) {
$response = null;
}
else {
} else {
$this->setErrorMessage($response, 'failed-fetching-external-data');
return FALSE;

return false;
}
}

if ($allData) {
if ($alldata) {
$info = $curl->get_info();

return [
'status' => intval($info['http_code']),
'data' => empty($response) ? null : $response,
'headers' => $curl->get_raw_response(),
'status' => intval($info['http_code']),
'data' => empty($response) ? null : $response,
'headers' => $curl->get_raw_response(),
];
}
else {
} else {
return $response;
}
}
Expand Down Expand Up @@ -1765,22 +1759,21 @@ public function replaceContentHubMetadataCache($metadata, $lang = 'en') {

// Check if exist in database
$cache = $DB->get_record_sql(
'SELECT id
'SELECT id
FROM {hvp_content_hub_cache}
WHERE language = ?',
array($lang)
array($lang)
);
if ($cache) {
// Update
$DB->execute("UPDATE {hvp_content_hub_cache} SET json = ? WHERE id = ?", array($metadata, $cache->id));
}
else {
// Insert
$DB->insert_record('hvp_content_hub_cache', (object) array(
'json' => $metadata,
'language' => $lang,
'last_checked' => time(),
));
// Update
$DB->execute("UPDATE {hvp_content_hub_cache} SET json = ? WHERE id = ?", array($metadata, $cache->id));
} else {
// Insert
$DB->insert_record('hvp_content_hub_cache', (object) array(
'json' => $metadata,
'language' => $lang,
'last_checked' => time(),
));
}
}

Expand Down
6 changes: 3 additions & 3 deletions content_hub_registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
*/
use mod_hvp\content_hub_service;

global $PAGE, $SITE, $OUTPUT, $CFG;

require_once("../../config.php");
require_once($CFG->libdir.'/adminlib.php');
require_once("locallib.php");

global $PAGE, $SITE, $OUTPUT, $CFG;

// No guest autologin.
require_login(0, false);

Expand All @@ -40,7 +40,7 @@
$PAGE->set_title("{$SITE->shortname}: " . get_string('upgrade', 'hvp'));
$PAGE->set_heading(get_string('contenthub:settings:heading', 'mod_hvp'));

$settings = content_hub_service::getRegistrationUISettings();
$settings = content_hub_service::get_registration_ui_settings();
$PAGE->requires->data_for_js('H5PSettings', $settings, true);
$PAGE->requires->css(new moodle_url('library/styles/h5p.css'));
$PAGE->requires->css(new moodle_url('library/styles/h5p-hub-registration.css'));
Expand Down
9 changes: 3 additions & 6 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@
);

// Content Hub
try {
$hubInfo = $core->hubAccountInfo();
$hubinfo = $core->hubAccountInfo();

if ($hubinfo) {
$settings->add(new admin_setting_heading(
'mod_hvp/content_hub_settings',
get_string('contenthub:settings:heading', 'hvp'),
Expand All @@ -125,12 +125,9 @@
$settings->add(new admin_setting_html(
'mod_hvp/content_hub_settings_box',
get_string('contenthub:settings:box', 'hvp'),
$hubInfo
$hubinfo
));
}
catch (Exception $e) {
// Not showing account form
}

// Load js for disable hub confirmation dialog functionality.
$PAGE->requires->js('/mod/hvp/library/js/jquery.js', true);
Expand Down
Loading

0 comments on commit 2a64196

Please sign in to comment.