From 9d9e785160541095488b6184223a16b4697745f2 Mon Sep 17 00:00:00 2001 From: Thomas Marstrander Date: Wed, 14 Apr 2021 17:28:53 +0200 Subject: [PATCH] Fix complaints from moodle ci linter --- ajax.php | 18 +++++------ classes/admin_setting_html.php | 2 +- classes/framework.php | 16 +++++----- db/access.php | 2 +- db/upgrade.php | 20 ++++++------- lang/en/hvp.php | 4 +-- lib.php | 9 +++--- locallib.php | 55 ++++++++++++++++++---------------- mod_form.php | 6 ++-- settings.php | 2 +- share.php | 16 +++++----- view.php | 2 +- 12 files changed, 77 insertions(+), 75 deletions(-) diff --git a/ajax.php b/ajax.php index e0651400..f3cc2988 100644 --- a/ajax.php +++ b/ajax.php @@ -350,7 +350,7 @@ break; case 'contenthubregistration': - // Check permission + // Check permission. $context = \context_system::instance(); if (!has_capability('mod/hvp:contenthubregistration', $context)) { \H5PCore::ajaxError(get_string('contenthub:nopermissions', 'hvp'), 'NO_PERMISSION', 403); @@ -433,12 +433,12 @@ 'age' => optional_param('age', null, PARAM_RAW), ); - // Load content + // Load content. $core = \mod_hvp\framework::instance(); $cm = get_coursemodule_from_id('hvp', $id); $content = $core->loadContent($cm->instance); - // Update Hub status for content before proceeding + // Update Hub status for content before proceeding. $newstate = hvp_update_hub_status($content); $synced = $newstate !== false ? $newstate : intval($content['synced']); @@ -447,10 +447,10 @@ break; } - // Create URL + // Create URL. $data['download_url'] = hvp_create_hub_export_url($cm->id, $content); - // Get file size + // Get file size. $slug = $content['slug'] ? $content['slug'] . '-' : ''; $filecontext = context_course::instance($cm->course); $file = get_file_storage()->get_file($filecontext->id, 'mod_hvp', 'exports', 0, '/', "{$slug}{$content['id']}.h5p"); @@ -461,7 +461,7 @@ $size = $file->get_filesize(); $data['size'] = empty($size) ? -1 : $size; - // Add the icon and any screenshots + // Add the icon and any screenshots. $files = array( 'icon' => !empty($_FILES['icon']) ? $_FILES['icon'] : null, 'screenshots' => !empty($_FILES['screenshots']) ? $_FILES['screenshots'] : null, @@ -471,13 +471,13 @@ $isedit = !empty($content['contentHubId']); $updatecontent = $synced === \H5PContentHubSyncStatus::NOT_SYNCED && $isedit; if ($updatecontent) { - // node has been edited since the last time it was published + // Node has been edited since the last time it was published. $data['resync'] = 1; } $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; @@ -487,7 +487,7 @@ $fields['synced'] = \H5PContentHubSyncStatus::WAITING; } - // Store the content hub id + // Store the content hub id. $core->h5pF->updateContentFields($cm->instance, $fields); H5PCore::ajaxSuccess(); diff --git a/classes/admin_setting_html.php b/classes/admin_setting_html.php index 48fe941e..b789e8ab 100644 --- a/classes/admin_setting_html.php +++ b/classes/admin_setting_html.php @@ -56,7 +56,7 @@ public function get_defaultsetting() { * @return string Always returns an empty string */ public function write_setting($data) { - // do not write any setting + // Do not write any setting. return ''; } diff --git a/classes/framework.php b/classes/framework.php index 44204df2..7584827e 100644 --- a/classes/framework.php +++ b/classes/framework.php @@ -171,7 +171,7 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream $curldata = array(); foreach ($data as $key => $value) { if (empty($value)) { - continue; // Skip empty values + continue; // Skip empty values. } if (is_array($value)) { foreach ($value as $i => $subvalue) { @@ -186,17 +186,17 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream if ($file === null) { continue; } else if (is_array($file['name'])) { - // Array of files uploaded (multiple) + // Array of files uploaded (multiple). for ($i = 0; $i < count($file['name']); $i ++) { $curldata["{$name}[{$i}]"] = new \CurlFile($file['tmp_name'][$i], $file['type'][$i], $file['name'][$i]); } } else { - // Single file + // Single file. $curldata[$name] = new \CurlFile($file['tmp_name'], $file['type'], $file['name']); } } } else if (!empty($data)) { - // application/x-www-form-urlencoded + // Application/x-www-form-urlencoded. $curldata = format_postdata_for_curlcall($data); } @@ -248,7 +248,7 @@ public function fetchExternalData($url, $data = null, $blocking = true, $stream } $errorno = $curl->get_errno(); - // Error handling + // Error handling. if ($errorno) { if ($alldata) { $response = null; @@ -1854,7 +1854,7 @@ public function libraryHasUpgrade($library) { public function replaceContentHubMetadataCache($metadata, $lang = 'en') { global $DB; - // Check if exist in database + // Check if exist in database. $cache = $DB->get_record_sql( 'SELECT id FROM {hvp_content_hub_cache} @@ -1862,10 +1862,10 @@ public function replaceContentHubMetadataCache($metadata, $lang = 'en') { array($lang) ); if ($cache) { - // Update + // Update. $DB->execute("UPDATE {hvp_content_hub_cache} SET json = ? WHERE id = ?", array($metadata, $cache->id)); } else { - // Insert + // Insert. $DB->insert_record('hvp_content_hub_cache', (object) array( 'json' => $metadata, 'language' => $lang, diff --git a/db/access.php b/db/access.php index e017aeac..397bd926 100644 --- a/db/access.php +++ b/db/access.php @@ -201,7 +201,7 @@ 'mod/hvp:contenthubregistration' => [ 'captype' => 'write', 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => [], // only admins by default + 'archetypes' => [], // Only admins by default. ], ); diff --git a/db/upgrade.php b/db/upgrade.php index d0791300..dee0096d 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -524,21 +524,21 @@ function hvp_upgrade_2020082800() { * Drop old unused unique index, add nonunique index. */ function hvp_upgrade_2020091500() { - global $DB; - $dbman = $DB->get_manager(); - $table = new xmldb_table('hvp_xapi_results'); - $index = new xmldb_index('results', XMLDB_INDEX_NOTUNIQUE, ['content_id', 'user_id']); - $dbman->add_index($table, $index); - - $oldindex = new xmldb_index('result', XMLDB_INDEX_UNIQUE, ['id', 'content_id', 'user_id']); - $dbman->drop_index($table, $oldindex); + global $DB; + $dbman = $DB->get_manager(); + $table = new xmldb_table('hvp_xapi_results'); + $index = new xmldb_index('results', XMLDB_INDEX_NOTUNIQUE, ['content_id', 'user_id']); + $dbman->add_index($table, $index); + + $oldindex = new xmldb_index('result', XMLDB_INDEX_UNIQUE, ['id', 'content_id', 'user_id']); + $dbman->drop_index($table, $oldindex); } function hvp_upgrade_2020112600() { global $DB; $dbman = $DB->get_manager(); - // Add Content Hub fields to main content table + // Add Content Hub fields to main content table. $table = new xmldb_table('hvp'); if (!$dbman->field_exists($table, 'shared')) { $dbman->add_field($table, new xmldb_field('shared', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'completionpass')); @@ -550,7 +550,7 @@ function hvp_upgrade_2020112600() { $dbman->add_field($table, new xmldb_field('hub_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'synced')); } - // Create table for caching content hub metadata + // Create table for caching content hub metadata. $table = new xmldb_table('hvp_content_hub_cache'); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('language', XMLDB_TYPE_CHAR, '31', null, XMLDB_NOTNULL, null, null); diff --git a/lang/en/hvp.php b/lang/en/hvp.php index da28414a..5eed2a10 100644 --- a/lang/en/hvp.php +++ b/lang/en/hvp.php @@ -483,7 +483,7 @@ $string['gradeheading'] = 'Results for {$a}'; $string['assistivetechnologieslabel'] = 'Assistive Technologies label'; -// Content hub +// Content hub. $string['contenthub:settings:heading'] = 'H5P Hub Account Settings'; $string['contenthub:settings:box'] = 'Content Hub Account'; $string['contenthub:licenseagreementtitle'] = 'End User License Agreement (EULA)'; @@ -588,7 +588,7 @@ $string['updateregistrationonhub'] = 'Save account settings'; $string['successfullyupdated'] = 'Your H5P Hub account settings have successfully been changed'; -# Content hub +// Content hub. $string['age'] = 'Typical age'; $string['agedescription'] = 'The target audience of this content. Possible input formats separated by commas: "1,34-45,-50,59-".'; $string['invalidage'] = 'Invalid input format for Typical age. Possible input formats separated by commas: "1, 34-45, -50, -59-".'; diff --git a/lib.php b/lib.php index 3752cdf6..c16286db 100644 --- a/lib.php +++ b/lib.php @@ -213,7 +213,7 @@ function hvp_delete_instance($id) { array($hvp->main_library_id) ); - // Only log event if we found library + // Only log event if we found library. if ($library) { // Log content delete. new \mod_hvp\event( @@ -293,7 +293,7 @@ function hvp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload return false; // Invalid context. } - // Allow download if valid temporary hash + // Allow download if valid temporary hash. $ishub = false; $hub = optional_param('hub', null, PARAM_RAW); if ($hub) { @@ -305,12 +305,11 @@ function hvp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload $signature = hash_hmac('SHA512', $data, get_config('mod_hvp', 'hub_secret'), true); if ($time < (time() - 43200) || !hash_equals($signature, $hash)) { - // No valid hash + // No valid hash. return false; } $ishub = true; - } - elseif (!has_capability('mod/hvp:view', $context)) { + } else if (!has_capability('mod/hvp:view', $context)) { // No permission. return false; } diff --git a/locallib.php b/locallib.php index 59ba6e1f..874d1aed 100644 --- a/locallib.php +++ b/locallib.php @@ -670,21 +670,22 @@ function hvp_attempt_submitted_handler($event) { * @param $content */ function hvp_update_hub_status($content) { - $synced = intval($content['synced']); + $synced = intval($content['synced']); - // Only check sync status when waiting - if (empty($content['contentHubId']) || $synced !== H5PContentHubSyncStatus::WAITING) { - return false; - } + // Only check sync status when waiting. + if (empty($content['contentHubId']) || $synced !== H5PContentHubSyncStatus::WAITING) { + return false; + } - $core = \mod_hvp\framework::instance(); - $newstate = $core->getHubContentStatus($content['contentHubId'], $synced); - if ($newstate !== false) { - $core->h5pF->updateContentFields($content['id'], array('synced' => $newstate)); - return $newstate; - } + $core = \mod_hvp\framework::instance(); + $newstate = $core->getHubContentStatus($content['contentHubId'], $synced); + if ($newstate !== false) { + $core->h5pF->updateContentFields($content['id'], array('synced' => $newstate)); - return false; + return $newstate; + } + + return false; } /** @@ -693,25 +694,27 @@ function hvp_update_hub_status($content) { * @param $content */ function hvp_create_hub_export_url($cmid, $content) { - // Create URL - $modulecontext = \context_module::instance($cmid); - $slug = $content['slug'] ? $content['slug'] . '-' : ''; - $filename = "{$slug}{$content['id']}.h5p"; - $exporturl = \moodle_url::make_pluginfile_url($modulecontext->id, 'mod_hvp', 'exports', '', '', $filename)->out(false); - - // To prevent anyone else from downloading we add an extra token - $time = time(); - $data = $time . ':' . get_config('mod_hvp', 'site_uuid'); - $hash = hash_hmac('SHA512', $data, get_config('mod_hvp', 'hub_secret'), true); - $token = hvp_base64_encode($time) . '.' . hvp_base64_encode($hash); - - return "$exporturl?hub=$token"; + // Create URL. + $modulecontext = \context_module::instance($cmid); + $slug = $content['slug'] ? $content['slug'] . '-' : ''; + $filename = "{$slug}{$content['id']}.h5p"; + $exporturl = \moodle_url::make_pluginfile_url($modulecontext->id, 'mod_hvp', 'exports', '', '', $filename) + ->out(false); + + // To prevent anyone else from downloading we add an extra token. + $time = time(); + $data = $time . ':' . get_config('mod_hvp', 'site_uuid'); + $hash = hash_hmac('SHA512', $data, get_config('mod_hvp', 'hub_secret'), true); + $token = hvp_base64_encode($time) . '.' . hvp_base64_encode($hash); + + return "$exporturl?hub=$token"; } /** * URL compatible base64 encoding. * - * @param string $string + * @param string $string + * * @return string */ function hvp_base64_encode($string) { diff --git a/mod_form.php b/mod_form.php index b21a428e..a7d89767 100644 --- a/mod_form.php +++ b/mod_form.php @@ -361,13 +361,13 @@ public function validation($data, $files) { public function data_postprocessing($data) { // Determine disabled content features. - // mod_form may be loaded without the H5P editor, so we have to check if + // Mod_form may be loaded without the H5P editor, so we have to check if // data is present if we want to process them. - $hasDisplayOptions = isset($data->frame) + $hasdisplayoptions = isset($data->frame) || isset($data->export) || isset($data->embed) || isset($data->copyright); - if ($hasDisplayOptions) { + if ($hasdisplayoptions) { $options = array( \H5PCore::DISPLAY_OPTION_FRAME => isset($data->frame) ? $data->frame : 0, \H5PCore::DISPLAY_OPTION_DOWNLOAD => isset($data->export) ? $data->export : 0, diff --git a/settings.php b/settings.php index 04c4a449..00e76479 100644 --- a/settings.php +++ b/settings.php @@ -112,7 +112,7 @@ ) ); - // Content Hub + // Content Hub. $hubinfo = $core->hubAccountInfo(); $settings->add(new admin_setting_heading( 'mod_hvp/content_hub_settings', diff --git a/share.php b/share.php index ce23bc0b..186b3786 100644 --- a/share.php +++ b/share.php @@ -40,7 +40,7 @@ $context = context_module::instance($cm->id); require_capability('mod/hvp:share', $context); -// Check if Hub registered, if not redirect to hub registration +// Check if Hub registered, if not redirect to hub registration. if (empty(get_config('mod_hvp', 'site_uuid')) || empty(get_config('mod_hvp', 'hub_secret'))) { if (!has_capability('mod/hvp:contenthubregistration', \context_system::instance())) { print_error('nohubregistration'); @@ -48,13 +48,13 @@ redirect(new moodle_url('/mod/hvp/content_hub_registration.php')); } -// Try to load existing content from the Hub +// Try to load existing content from the Hub. $core = \mod_hvp\framework::instance(); $content = $core->loadContent($cm->instance); $action = optional_param('action', '', PARAM_TEXT); if ($action) { - // Prepare to do a Hub Action + // Prepare to do a Hub Action. if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); } @@ -68,13 +68,13 @@ $core = \mod_hvp\framework::instance(); if ($action === 'sync') { - // Sync content already shared on the Hub + // Sync content already shared on the Hub. $exporturl = hvp_create_hub_export_url($cm->id, $content); if ($core->hubSyncContent($content['contentHubId'], $exporturl)) { $core->h5pF->updateContentFields($content['id'], array('synced' => \H5PContentHubSyncStatus::WAITING)); } } else if ($action === 'unpublish') { - // Unpublish content already shared on the Hub + // Unpublish content already shared on the Hub. if ($core->hubUnpublishContent($content['contentHubId'])) { $core->h5pF->updateContentFields($content['id'], array('shared' => 0)); } @@ -85,7 +85,7 @@ $hubcontent = !empty($content['contentHubId']) ? $core->hubRetrieveContent($content['contentHubId']) : null; if (empty($content['contentHubId'])) { - // Try to populate with license from content + // Try to populate with license from content. $license = !empty($content['metadata']['license']) ? $content['metadata']['license'] : null; $licenseversion = !empty($license) && !empty($content['metadata']['licenseVersion']) ? $content['metadata']['licenseVersion'] : null; $showcopyrightwarning = false; @@ -106,7 +106,7 @@ ]; } -// Prepare settings for the UI +// Prepare settings for the UI. $locale = \mod_hvp\framework::get_language(); $settings = [ 'token' => \H5PCore::createToken('share_' . $id), @@ -126,7 +126,7 @@ $PAGE->set_title(format_string($cm->name)); $PAGE->set_heading($course->fullname); -// Load JavaScript and styles +// Load JavaScript and styles. $PAGE->requires->js(new \moodle_url(\mod_hvp\view_assets::getsiteroot() . '/mod/hvp/library/js/h5p-hub-sharing.js'), true); foreach (\H5PCore::$styles as $style) { $PAGE->requires->css(new \moodle_url(\mod_hvp\view_assets::getsiteroot() . "/mod/hvp/library/{$style}")); diff --git a/view.php b/view.php index 09b8c116..cadb75ef 100644 --- a/view.php +++ b/view.php @@ -78,7 +78,7 @@ 'isshared' => $isshared ); -// Update Hub status for content before printing out messages +// Update Hub status for content before printing out messages. if ($hashub && $isshared) { $newstate = hvp_update_hub_status($content); $synced = $newstate !== false ? $newstate : intval($content['synced']);