Skip to content

Commit

Permalink
Upgrade: Migrate the enrol_semco_before_standard_top_of_body_html() f…
Browse files Browse the repository at this point in the history
…unction to the new hook callback on Moodle 4.4.
  • Loading branch information
abias committed Jun 17, 2024
1 parent ee7320e commit 0e89053
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes

### Unreleased

* 2024-06-01 - Upgrade: Migrate the enrol_semco_before_standard_top_of_body_html() function to the new hook callback on Moodle 4.4.
* 2024-06-01 - Release: Let codechecker ignore some sniffs in the language pack.

### v4.3-r2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Enrolment method "SEMCO" - Hook: Allows plugins to modify the page navigation before the page output is started.
*
* @package enrol_semco
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace enrol_semco\local\hook\output;

/**
* Hook to allow plugins to modify the page navigation before the page output is started.
*
* @package enrol_semco
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class before_standard_top_of_body_html_generation {
/**
* Callback to modify the page navigation.
*
* @param \core\hook\output\before_standard_top_of_body_html_generation $hook
*/
public static function callback(\core\hook\output\before_standard_top_of_body_html_generation $hook): void {
global $CFG;

// Require local library.
require_once($CFG->dirroot . '/enrol/semco/locallib.php');

// Call callback implementation.
enrol_semco_callbackimpl_before_standard_top_of_body_html($hook);
}
}
33 changes: 33 additions & 0 deletions db/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Enrolment method "SEMCO" - Hook callbacks.
*
* @package enrol_semco
* @copyright 2024 Alexander Bias, lern.link GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

$callbacks = [
[
'hook' => \core\hook\output\before_standard_top_of_body_html_generation::class,
'callback' => 'enrol_semco\local\hook\output\before_standard_top_of_body_html_generation::callback',
'priority' => 0,
],
];
35 changes: 8 additions & 27 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,14 @@ public function has_bulk_operations(course_enrolment_manager $manager) {
}

/**
* The enrol plugin type, unfortunately, does not include settings.php for non-admins.
* So we have to use a nasty workaround to add the SEMCO enrolment report link to the site administration
* where managers will find it.
* This is done here by hooking into the page navigation manually before the page output is started.
* Callback to add head elements (for releases up to Moodle 4.3).
*/
function enrol_semco_before_standard_top_of_body_html() {
global $PAGE;

// Allow admins and users with the enrol/semco:viewreport capability to access the report.
$context = context_system::instance();
if (has_capability('moodle/site:config', $context) ||
has_capability('enrol/semco:viewreport', $context)) {

// Create new navigation node for enrolment report.
$reportnode = navigation_node::create(get_string('reportpagetitle', 'enrol_semco', null, true),
new moodle_url('/enrol/semco/enrolreport.php'),
navigation_node::TYPE_SETTING,
null,
'enrol_semco_enrolreport');

// Find the reports container in navigation.
$reports = $PAGE->settingsnav->find('reports', navigation_node::TYPE_SETTING);

// If the reports container was found.
if ($reports != false) {
// Add our report node to the list of reports.
$reports->add_node($reportnode);
}
}
global $CFG;

// Require local library.
require_once($CFG->dirroot.'/enrol/semco/locallib.php');

// Call and return callback implementation.
return enrol_semco_callbackimpl_before_standard_top_of_body_html();
}
39 changes: 39 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,42 @@ function enrol_semco_check_local_recompletion() {

return $localrecompletioninstalled;
}

/**
* Callback to modify the page navigation.
* This function is implemented here and used from two locations:
* -> function enrol_semco_before_standard_top_of_body_html in lib.php (for releases up to Moodle 4.3)
* -> class enrol_semco\local\hook\output\before_standard_top_of_body_html_generation (for releases from Moodle 4.4 on).
*
* We use this callback as the enrol plugin type, unfortunately, does not include settings.php for non-admins.
* So we have to use a nasty workaround to add the SEMCO enrolment report link to the site administration
* where managers will find it.
* This is done here by hooking into the page navigation manually before the page output is started.
*
* @param \core\hook\output\before_standard_top_of_body_html_generation $hook The hook (which is unused in this plugin).
*/
function enrol_semco_callbackimpl_before_standard_top_of_body_html(&$hook = null) {
global $PAGE;

// Allow admins and users with the enrol/semco:viewreport capability to access the report.
$context = context_system::instance();
if (has_capability('moodle/site:config', $context) ||
has_capability('enrol/semco:viewreport', $context)) {

// Create new navigation node for enrolment report.
$reportnode = navigation_node::create(get_string('reportpagetitle', 'enrol_semco', null, true),
new moodle_url('/enrol/semco/enrolreport.php'),
navigation_node::TYPE_SETTING,
null,
'enrol_semco_enrolreport');

// Find the reports container in navigation.
$reports = $PAGE->settingsnav->find('reports', navigation_node::TYPE_SETTING);

// If the reports container was found.
if ($reports != false) {
// Add our report node to the list of reports.
$reports->add_node($reportnode);
}
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'enrol_semco';
$plugin->version = 2023100906;
$plugin->version = 2023100907;
$plugin->release = 'v4.3-r2';
$plugin->requires = 2023100900;
$plugin->supported = [403, 403];
Expand Down

0 comments on commit 0e89053

Please sign in to comment.