Merge branch 'MDL-80942-main_link_meta_enrolments_to_meta_page' of https://github.com/sharpchi/moodle

This commit is contained in:
Huong Nguyen
2025-09-12 09:01:43 +07:00
2 changed files with 72 additions and 0 deletions
+19
View File
@@ -22,6 +22,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\output\html_writer;
use core\url;
defined('MOODLE_INTERNAL') || die();
/**
@@ -63,6 +66,22 @@ class enrol_meta_plugin extends enrol_plugin {
}
}
/**
* Display meta course name with link if user has permissions.
*
* @param stdClass $instance
* @return string
*/
public function get_instance_name_for_management_page(stdClass $instance): string {
$displayname = $this->get_instance_name($instance);
$metacoursecontext = \core\context\course::instance($instance->customint1);
$canviewmetacourse = course_can_view_participants($metacoursecontext);
if ($canviewmetacourse) {
$displayname = html_writer::link(new url('/user/index.php', ['id' => $instance->customint1]), $displayname);
}
return $displayname;
}
/**
* Returns true if we can add a new instance to this course.
*
+53
View File
@@ -1258,4 +1258,57 @@ final class plugin_test extends \advanced_testcase {
$instance = $metaplugin->find_instance($enrolmentdata, $course1->id);
$this->assertEquals($instance2->id, $instance->id);
}
/**
* Test the display name is linked to the meta course depending on permissions.
*
* @covers ::get_instance_name_for_management_page
* @return void
*/
public function test_get_instance_name_for_management_page(): void {
global $DB;
$this->resetAfterTest();
$cat = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course1']);
$course2 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course2']);
$course3 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course3']);
$metaplugin = enrol_get_plugin('meta');
$teacher1 = $this->getDataGenerator()->create_and_enrol($course1, 'editingteacher');
$teacher2 = $this->getDataGenerator()->create_and_enrol($course2, 'editingteacher');
$this->getDataGenerator()->enrol_user($teacher1->id, $course3->id, 'editingteacher');
$this->getDataGenerator()->enrol_user($teacher2->id, $course3->id, 'editingteacher');
// Add two meta enrol instances.
$instanceid1 = $metaplugin->add_instance($course3, ['customint1' => $course1->id]);
$instanceid2 = $metaplugin->add_instance($course3, ['customint1' => $course2->id]);
$instance1 = $DB->get_record('enrol', ['id' => $instanceid1]);
$instance2 = $DB->get_record('enrol', ['id' => $instanceid2]);
$this->setUser($teacher1);
$this->assertMatchesRegularExpression(
'#<a href="[^"]+user/index.php\?id=' . $course1->id . '">Course meta link \(Test course 1\)<\/a>#',
$metaplugin->get_instance_name_for_management_page($instance1),
'Teacher1 has permissions to view Course 1 so should have a link'
);
$this->assertEquals(
'Course meta link (Test course 2)',
$metaplugin->get_instance_name_for_management_page($instance2),
"Teacher1 doesn't have permissions to view Course 2 so no link"
);
$this->setUser($teacher2);
$this->assertEquals(
'Course meta link (Test course 1)',
$metaplugin->get_instance_name_for_management_page($instance1),
"Teacher2 doesn't have permissions to view Course 1 so no link"
);
$this->assertMatchesRegularExpression(
'#<a href="[^"]+user/index.php\?id=' . $course2->id . '">Course meta link \(Test course 2\)<\/a>#',
$metaplugin->get_instance_name_for_management_page($instance2),
'Teacher2 has permissions to view Course 2 so should have a link'
);
}
}