mirror of
https://github.com/moodle/moodle.git
synced 2025-04-15 05:25:08 +02:00
Merge branch 'MDL-83473_main' of https://github.com/dasistwas/moodle
This commit is contained in:
commit
c77fb0e6e0
11
.upgradenotes/MDL-83473-2024111407511128.yml
Normal file
11
.upgradenotes/MDL-83473-2024111407511128.yml
Normal file
@ -0,0 +1,11 @@
|
||||
issueNumber: MDL-83473
|
||||
notes:
|
||||
core:
|
||||
- message: >-
|
||||
The deprecated implementation in course/view.php, which uses the
|
||||
extern_server_course function to handle routing between internal and
|
||||
external courses, can be improved by utilizing the Hook API. This
|
||||
enhancement is essential for a project involving multiple universities,
|
||||
as the Hook API provides a more generalized and flexible approach to
|
||||
route users to external courses from within other plugins.
|
||||
type: improved
|
66
course/classes/hook/before_course_viewed.php
Normal file
66
course/classes/hook/before_course_viewed.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Contains class before_course_viewed responsible for external course routing
|
||||
*
|
||||
* @package core_course
|
||||
* @copyright 2024 Jacob Viertel
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core_course\hook;
|
||||
|
||||
use stdClass;
|
||||
use core\hook\described_hook;
|
||||
|
||||
/**
|
||||
* External course redirect hook before_course_viewed
|
||||
*
|
||||
*/
|
||||
final class before_course_viewed implements described_hook {
|
||||
/**
|
||||
* The course being viewed
|
||||
* @var stdClass
|
||||
*/
|
||||
public $course;
|
||||
|
||||
/**
|
||||
* Constructor for the hook.
|
||||
* @param stdClass $course The course instance.
|
||||
*/
|
||||
public function __construct(stdClass $course) {
|
||||
$this->course = $course;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_hook_description(): string {
|
||||
return 'Hook dispatched just before viewing a course in course/view.php.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks tags
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_hook_tags(): array {
|
||||
return ['course', 'view', 'routing', 'navigation'];
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@
|
||||
|
||||
namespace core_courseformat;
|
||||
|
||||
use core_course\hook\before_course_viewed;
|
||||
use core_group\hook\after_group_membership_added;
|
||||
use core_group\hook\after_group_membership_removed;
|
||||
|
||||
@ -52,4 +53,21 @@ class hook_listener {
|
||||
$course = get_course($group->courseid);
|
||||
base::invalidate_all_session_caches_for_course($course);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect to external course url using the before_course_viewed hook.
|
||||
*
|
||||
* @param before_course_viewed $hook The hook object containing course data.
|
||||
*/
|
||||
public static function before_course_viewed(before_course_viewed $hook): void {
|
||||
global $CFG;
|
||||
if (file_exists($CFG->dirroot . '/course/externservercourse.php')) {
|
||||
include($CFG->dirroot . '/course/externservercourse.php');
|
||||
if (function_exists('extern_server_course')) {
|
||||
if ($externurl = extern_server_course($hook->course)) {
|
||||
redirect($externurl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -107,14 +107,8 @@ if ($switchrole > 0 && confirm_sesskey() &&
|
||||
|
||||
// If course is hosted on an external server, redirect to corresponding
|
||||
// url with appropriate authentication attached as parameter.
|
||||
if (file_exists($CFG->dirroot . '/course/externservercourse.php')) {
|
||||
include($CFG->dirroot . '/course/externservercourse.php');
|
||||
if (function_exists('extern_server_course')) {
|
||||
if ($externurl = extern_server_course($course)) {
|
||||
redirect($externurl);
|
||||
}
|
||||
}
|
||||
}
|
||||
$hook = new \core_course\hook\before_course_viewed($course);
|
||||
\core\hook\manager::get_instance()->dispatch($hook);
|
||||
|
||||
require_once($CFG->dirroot.'/calendar/lib.php'); // This is after login because it needs $USER.
|
||||
|
||||
|
@ -122,4 +122,9 @@ $callbacks = [
|
||||
'hook' => \core_files\hook\before_file_created::class,
|
||||
'callback' => [\core_files\redactor\hook_listener::class, 'file_redaction_handler'],
|
||||
],
|
||||
[
|
||||
'hook' => \core_course\hook\before_course_viewed::class,
|
||||
'callback' => [\core_courseformat\hook_listener::class, 'before_course_viewed'],
|
||||
'priority' => 999,
|
||||
],
|
||||
];
|
||||
|
91
lib/tests/hook/before_course_viewed_test.php
Normal file
91
lib/tests/hook/before_course_viewed_test.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core\hook;
|
||||
|
||||
/**
|
||||
* Test hook for external routing
|
||||
*
|
||||
* @coversDefaultClass \core_course\hook\before_course_viewed
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2024 Jacob Viertel
|
||||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class before_course_viewed_test extends \advanced_testcase {
|
||||
/**
|
||||
* Test hook description.
|
||||
* @covers ::get_hook_description
|
||||
*/
|
||||
public function test_hook_description(): void {
|
||||
$description = \core_course\hook\before_course_viewed::get_hook_description();
|
||||
$this->assertIsString($description);
|
||||
$this->assertSame('Hook dispatched just before viewing a course in course/view.php.', $description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test hook tags.
|
||||
* @covers ::get_hook_tags
|
||||
*/
|
||||
public function test_hook_tags(): void {
|
||||
$tags = \core_course\hook\before_course_viewed::get_hook_tags();
|
||||
$this->assertIsArray($tags);
|
||||
$this->assertContains('course', $tags);
|
||||
$this->assertContains('view', $tags);
|
||||
$this->assertContains('routing', $tags);
|
||||
$this->assertContains('navigation', $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test hook initialization with course data.
|
||||
* @covers ::__construct
|
||||
*/
|
||||
public function test_hook_initialization(): void {
|
||||
$course = new \stdClass();
|
||||
$course->id = 1;
|
||||
$course->fullname = 'Test Course';
|
||||
|
||||
$hook = new \core_course\hook\before_course_viewed($course);
|
||||
|
||||
$this->assertInstanceOf(\core_course\hook\before_course_viewed::class, $hook);
|
||||
$this->assertSame($course, $hook->course);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test hook dispatch and propagation.
|
||||
* @covers \core\hook\manager::dispatch
|
||||
*/
|
||||
public function test_hook_dispatch(): void {
|
||||
$course = new \stdClass();
|
||||
$course->id = 1;
|
||||
$course->fullname = 'Test Course';
|
||||
|
||||
$hook = new \core_course\hook\before_course_viewed($course);
|
||||
|
||||
$count = 0;
|
||||
$receivedhook = null;
|
||||
$testcallback = function (\core_course\hook\before_course_viewed $hook) use (&$receivedhook, &$count): void {
|
||||
$count++;
|
||||
$receivedhook = $hook;
|
||||
};
|
||||
$this->redirectHook(\core_course\hook\before_course_viewed::class, $testcallback);
|
||||
|
||||
\core\hook\manager::get_instance()->dispatch($hook);
|
||||
|
||||
$this->assertSame(1, $count);
|
||||
$this->assertSame($hook, $receivedhook);
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2024112200.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2024112200.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
$release = '5.0dev (Build: 20241122)'; // Human-friendly version name
|
||||
|
Loading…
x
Reference in New Issue
Block a user