Skip to content

Commit

Permalink
HFP-2858 Change deprecated httpswwwroot to use wwwroot when > Moodle 3.4
Browse files Browse the repository at this point in the history
Added missing string and mapping for when siteee is missing mbstring
  • Loading branch information
thomasmars committed Oct 8, 2019
1 parent f9c661f commit 9a433dc
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 45 deletions.
9 changes: 5 additions & 4 deletions classes/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static function instance($type = null) {
$fs = new \mod_hvp\file_storage();

$context = \context_system::instance();
$url = "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp";
$root = view_assets::getSiteRoot();
$url = "{$root}/pluginfile.php/{$context->id}/mod_hvp";

$language = self::get_language();

Expand Down Expand Up @@ -478,7 +479,8 @@ public function t($message, $replacements = array()) {
'The total size of the unpacked files exceeds the maximum size allowed. (%used > %max)' => 'unpackedfilesexceedsmaxsize',
'Unable to read file from the package: %fileName' => 'couldnotreadfilefromzip',
'Unable to parse JSON from the package: %fileName' => 'couldnotparsejsonfromzip',
'Could not parse post data.' => 'couldnotparsepostdata'
'Could not parse post data.' => 'couldnotparsepostdata',
'The mbstring PHP extension is not loaded. H5P needs this to function properly' => 'nombstringexteension',
];
// @codingStandardsIgnoreEnd
}
Expand All @@ -501,9 +503,8 @@ public function getH5pPath() {
*/
// @codingStandardsIgnoreLine
public function getLibraryFileUrl($libraryfoldername, $fileName) {
global $CFG;
$context = \context_system::instance();
$basepath = $CFG->httpswwwroot . '/';
$basepath = view_assets::getSiteRoot() . '/';
return "{$basepath}pluginfile.php/{$context->id}/mod_hvp/libraries/{$libraryfoldername}/{$fileName}";
}

Expand Down
41 changes: 25 additions & 16 deletions classes/view_assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class view_assets {
protected $files;

public function __construct($cm, $course, $options = []) {
global $CFG;

$this->cm = $cm;
$this->course = $course;
$this->core = framework::instance();
Expand All @@ -67,6 +65,7 @@ public function __construct($cm, $course, $options = []) {

// Add JavaScript settings for this content.
$cid = 'cid-' . $this->content['id'];
$root = self::getSiteRoot();
$this->settings['contents'][ $cid ] = array(
'library' => \H5PCore::libraryToString($this->content['library']),
'jsonContent' => $this->getfilteredparameters(),
Expand All @@ -76,8 +75,8 @@ public function __construct($cm, $course, $options = []) {
'resizeCode' => $this->getresizecode($displayoptions[ \H5PCore::DISPLAY_OPTION_EMBED ]),
'title' => $this->content['title'],
'displayOptions' => $displayoptions,
'url' => "{$CFG->httpswwwroot}/mod/hvp/view.php?id={$this->cm->id}",
'contentUrl' => "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp/content/{$this->content['id']}",
'url' => "{$root}/mod/hvp/view.php?id={$this->cm->id}",
'contentUrl' => "{$root}/pluginfile.php/{$context->id}/mod_hvp/content/{$this->content['id']}",
'metadata' => $this->content['metadata'],
'contentUserData' => array(
0 => content_user_data::load_pre_loaded_user_data($this->content['id'])
Expand Down Expand Up @@ -149,13 +148,12 @@ private function getexportsettings($downloadenabled) {
* @return string
*/
private function getembedcode($embedenabled) {
global $CFG;

if ( ! $embedenabled) {
return '';
}

$embedurl = new \moodle_url("{$CFG->httpswwwroot}/mod/hvp/embed.php?id={$this->cm->id}");
$root = self::getSiteRoot();
$embedurl = new \moodle_url("{$root}/mod/hvp/embed.php?id={$this->cm->id}");

return "<iframe src=\"{$embedurl->out()}\" width=\":w\" height=\":h\" frameborder=\"0\" " .
"allowfullscreen=\"allowfullscreen\"></iframe>";
Expand All @@ -169,13 +167,11 @@ private function getembedcode($embedenabled) {
* @return string
*/
private function getresizecode($embedenabled) {
global $CFG;

if ( ! $embedenabled) {
return '';
}

$resizeurl = new \moodle_url($CFG->httpswwwroot . '/mod/hvp/library/js/h5p-resizer.js');
$resizeurl = new \moodle_url(self::getSiteRoot() . '/mod/hvp/library/js/h5p-resizer.js');

return "<script src=\"{$resizeurl->out()}\" charset=\"UTF-8\"></script>";
}
Expand Down Expand Up @@ -203,8 +199,6 @@ private function getdependencyfiles() {
* Generates assets depending on embed type
*/
private function generateassets() {
global $CFG;

if ($this->embedtype === 'div') {
$context = \context_system::instance();
$hvppath = "/pluginfile.php/{$context->id}/mod_hvp";
Expand All @@ -219,7 +213,7 @@ private function generateassets() {
$url = $hvppath . $url;
}
$this->settings['loadedJs'][] = $url;
$this->jsrequires[] = new \moodle_url($isexternal ? $url : $CFG->httpswwwroot . $url);
$this->jsrequires[] = new \moodle_url($isexternal ? $url : self::getSiteRoot() . $url);
}

// Schedule stylesheets for loading through Moodle.
Expand All @@ -232,7 +226,7 @@ private function generateassets() {
$url = $hvppath . $url;
}
$this->settings['loadedCss'][] = $url;
$this->cssrequires[] = new \moodle_url($isexternal ? $url : $CFG->httpswwwroot . $url);
$this->cssrequires[] = new \moodle_url($isexternal ? $url : self::getSiteRoot() . $url);
}
} else {
// JavaScripts and stylesheets will be loaded through h5p.js.
Expand Down Expand Up @@ -292,7 +286,7 @@ public function triggermoduleviewedevent() {
* Adds js assets to current page
*/
public function addassetstopage() {
global $PAGE, $CFG;
global $PAGE;

foreach ($this->jsrequires as $script) {
$PAGE->requires->js($script, true);
Expand All @@ -306,7 +300,7 @@ public function addassetstopage() {
$PAGE->requires->data_for_js('H5PIntegration', $this->settings, true);

// Add xAPI collector script.
$PAGE->requires->js(new \moodle_url($CFG->httpswwwroot . '/mod/hvp/xapi-collector.js'), true);
$PAGE->requires->js(new \moodle_url(self::getSiteRoot() . '/mod/hvp/xapi-collector.js'), true);
}

/**
Expand Down Expand Up @@ -337,4 +331,19 @@ public function validatecontent() {
print_error('invalidhvp');
}
}

/**
* Gets the site root absolute address
*
* @return string Root address for the site
*/
public static function getSiteRoot() {
global $CFG;
// In Moodle 3.4 version wwwroot is always the same as httpswwwroot
if ($CFG->version < 2017111300) {
return $CFG->httpswwwroot;
}

return $CFG->wwwroot;
}
}
8 changes: 5 additions & 3 deletions embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
require_course_login($course, true, $cm, true, true);
} catch (Exception $e) {
$PAGE->set_pagelayout('embedded');
$embedfailedsvg = new \moodle_url("{$CFG->httpswwwroot}/mod/hvp/library/images/h5p.svg");
$root = \mod_hvp\view_assets::getSiteRoot();
$embedfailedsvg = new \moodle_url("{$root}/mod/hvp/library/images/h5p.svg");
echo '<body style="margin:0">' .
'<div style="background: #fafafa ' .
'url(' . $embedfailedsvg->out() . ') no-repeat center;' .
Expand Down Expand Up @@ -83,8 +84,9 @@
// Embed specific page setup.
$PAGE->add_body_class('h5p-embed');
$PAGE->set_pagelayout('embedded');
$PAGE->requires->css(new \moodle_url("{$CFG->httpswwwroot}/mod/hvp/embed.css"));
$PAGE->requires->js(new \moodle_url("{$CFG->httpswwwroot}/mod/hvp/embed.js"));
$root = \mod_hvp\view_assets::getSiteRoot();
$PAGE->requires->css(new \moodle_url("{$root}/mod/hvp/embed.css"));
$PAGE->requires->js(new \moodle_url("{$root}/mod/hvp/embed.js"));

// Add H5P assets to page.
$view->addassetstopage();
Expand Down
13 changes: 7 additions & 6 deletions grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,18 @@
$dataviewid = 'h5p-results';

// Add required assets for data views.
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/library/js/jquery.js'), true);
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/library/js/h5p-utils.js'), true);
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/library/js/h5p-data-view.js'), true);
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/dataviews.js'), true);
$PAGE->requires->css(new moodle_url($CFG->httpswwwroot . '/mod/hvp/styles.css'));
$root = \mod_hvp\view_assets::getSiteRoot();
$PAGE->requires->js(new moodle_url($root . '/mod/hvp/library/js/jquery.js'), true);
$PAGE->requires->js(new moodle_url($root . '/mod/hvp/library/js/h5p-utils.js'), true);
$PAGE->requires->js(new moodle_url($root . '/mod/hvp/library/js/h5p-data-view.js'), true);
$PAGE->requires->js(new moodle_url($root . '/mod/hvp/dataviews.js'), true);
$PAGE->requires->css(new moodle_url($root . '/mod/hvp/styles.css'));

// Add JavaScript settings to data views.
$settings = array(
'dataViews' => array(
"{$dataviewid}" => array(
'source' => "{$CFG->httpswwwroot}/mod/hvp/ajax.php?action=results&content_id={$hvp->id}",
'source' => "{$root}/mod/hvp/ajax.php?action=results&content_id={$hvp->id}",
'headers' => array(
(object) array(
'text' => get_string('user', 'hvp'),
Expand Down
1 change: 1 addition & 0 deletions lang/en/hvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,4 @@
$string['couldnotreadfilefromzip'] = 'Unable to read file from the package: %fileName';
$string['couldnotparsejsonfromzip'] = 'Unable to parse JSON from the package: %fileName';
$string['couldnotparsepostdata'] = 'Could not parse post data.';
$string['nombstringexteension'] = 'The mbstring PHP extension is not loaded. H5P needs this to function properly';
2 changes: 1 addition & 1 deletion library_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
);

// Add js.
$liburl = $CFG->httpswwwroot . '/mod/hvp/library/';
$liburl = \mod_hvp\view_assets::getSiteRoot() . '/mod/hvp/library/';

hvp_admin_add_generic_css_and_js($PAGE, $liburl, $settings);
$PAGE->requires->js(new moodle_url($liburl . 'js/h5p-library-list.js' . hvp_get_cache_buster()), true);
Expand Down
17 changes: 8 additions & 9 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function hvp_get_core_settings($context) {
global $USER, $CFG;

$systemcontext = \context_system::instance();
$basepath = $CFG->httpswwwroot . '/';
$basepath = \mod_hvp\view_assets::getSiteRoot() . '/';

// Check permissions and generate ajax paths.
$ajaxpaths = array();
Expand Down Expand Up @@ -91,7 +91,7 @@ function hvp_get_core_settings($context) {
* @return array
*/
function hvp_get_core_assets($context) {
global $CFG, $PAGE;
global $PAGE;

// Get core settings.
$settings = \hvp_get_core_settings($context);
Expand All @@ -106,7 +106,7 @@ function hvp_get_core_assets($context) {
$cachebuster = \hvp_get_cache_buster();

// Use relative URL to support both http and https.
$liburl = $CFG->httpswwwroot . '/mod/hvp/library/';
$liburl = \mod_hvp\view_assets::getSiteRoot() . '/mod/hvp/library/';
$relpath = '/' . preg_replace('/^[^:]+:\/\/[^\/]+\//', '', $liburl);

// Add core stylesheets.
Expand Down Expand Up @@ -154,7 +154,7 @@ function hvp_add_editor_assets($id = null, $mformid = null) {
);

// Use relative URL to support both http and https.
$url = $CFG->httpswwwroot . '/mod/hvp/';
$url = \mod_hvp\view_assets::getSiteRoot() . '/mod/hvp/';
$url = '/' . preg_replace('/^[^:]+:\/\/[^\/]+\//', '', $url);

// Make sure files are reloaded for each plugin update.
Expand Down Expand Up @@ -187,7 +187,8 @@ function hvp_add_editor_assets($id = null, $mformid = null) {
$PAGE->requires->js(new moodle_url('/mod/hvp/' . $languagescript . $cachebuster), true);

// Add JavaScript settings.
$filespathbase = "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp/";
$root = \mod_hvp\view_assets::getSiteRoot();
$filespathbase = "{$root}/pluginfile.php/{$context->id}/mod_hvp/";
$contentvalidator = \mod_hvp\framework::instance('contentvalidator');
$editorajaxtoken = \H5PCore::createToken('editorajax');
$settings['editor'] = array(
Expand Down Expand Up @@ -216,7 +217,7 @@ function hvp_add_editor_assets($id = null, $mformid = null) {
$context = \context_module::instance($cm->id);

// Override content URL.
$contenturl = "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp/content/{$id}";
$contenturl = "{$root}/pluginfile.php/{$context->id}/mod_hvp/content/{$id}";
$settings['contents']['cid-' . $id]['contentUrl'] = $contenturl;
}

Expand Down Expand Up @@ -395,8 +396,6 @@ function hvp_content_upgrade_progress($libraryid) {
* to upgrade script
*/
function hvp_get_library_upgrade_info($name, $major, $minor) {
global $CFG;

$library = (object) array(
'name' => $name,
'version' => (object) array(
Expand All @@ -412,7 +411,7 @@ function hvp_get_library_upgrade_info($name, $major, $minor) {
$context = \context_system::instance();
$libraryfoldername = "{$library->name}-{$library->version->major}.{$library->version->minor}";
if (\mod_hvp\file_storage::fileExists($context->id, 'libraries', '/' . $libraryfoldername . '/', 'upgrades.js')) {
$basepath = $CFG->httpswwwroot . '/';
$basepath = \mod_hvp\view_assets::getSiteRoot() . '/';
$library->upgradesScript = "{$basepath}pluginfile.php/{$context->id}/mod_hvp/libraries/{$libraryfoldername}/upgrades.js";
}

Expand Down
10 changes: 5 additions & 5 deletions review.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@
$pageurl = new moodle_url('/mod/hvp/review.php', [
'id' => $hvp->id,
]);
$basepath = \mod_hvp\view_assets::getSiteRoot();
$PAGE->set_url($pageurl);
$PAGE->set_title($hvp->title);
$PAGE->set_heading($COURSE->fullname);
$PAGE->requires->css(new moodle_url($CFG->httpswwwroot . '/mod/hvp/xapi-custom-report.css'));
$PAGE->requires->css(new moodle_url($basepath . '/mod/hvp/xapi-custom-report.css'));

// We have to get grades from gradebook as well.
$xapiresults = $DB->get_records_sql("
Expand Down Expand Up @@ -145,16 +146,15 @@
$styles = $reporter->getStylesUsed();
$scripts = $reporter->getScriptsUsed();
foreach ($styles as $style) {
$PAGE->requires->css(new moodle_url($CFG->httpswwwroot . '/mod/hvp/reporting/' . $style));
$PAGE->requires->css(new moodle_url($basepath . '/mod/hvp/reporting/' . $style));
}
foreach ($scripts as $script) {
$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/reporting/' . $script));
$PAGE->requires->js(new moodle_url($basepath . '/mod/hvp/reporting/' . $script));
}

$PAGE->requires->js(new moodle_url($CFG->httpswwwroot . '/mod/hvp/library/js/jquery.js'), true);
$PAGE->requires->js(new moodle_url($basepath . '/mod/hvp/library/js/jquery.js'), true);

// Send the enpoints necessary for dynamic grading to the view.
$basepath = $CFG->httpswwwroot;
$setsubcontentendpoint = "{$basepath}/mod/hvp/ajax.php?contextId={$context->instanceid}&token=" .
\H5PCore::createToken('result') . '&action=updatesubcontentscore';
$getsubcontentendpoint = "{$basepath}/mod/hvp/ajax.php?contextId={$context->instanceid}&token=" .
Expand Down
2 changes: 1 addition & 1 deletion upgrade_content_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
);

// Add JavaScripts.
$liburl = $CFG->httpswwwroot . '/mod/hvp/library/';
$liburl = \mod_hvp\view_assets::getSiteRoot() . '/mod/hvp/library/';
hvp_admin_add_generic_css_and_js($PAGE, $liburl, $settings);
$PAGE->requires->js(new moodle_url($liburl . 'js/h5p-version.js' . hvp_get_cache_buster()), true);
$PAGE->requires->js(new moodle_url($liburl . 'js/h5p-content-upgrade.js' . hvp_get_cache_buster()), true);
Expand Down

0 comments on commit 9a433dc

Please sign in to comment.