Merge branch 'MDL-85841-main' of https://github.com/sarjona/moodle

This commit is contained in:
Amaia Anabitarte
2025-08-06 16:14:11 +02:00
2 changed files with 173 additions and 13 deletions
@@ -16,6 +16,11 @@
namespace core_courseformat\local\overview;
use core\url;
use action_link;
use core\output\local\properties\button;
use core\output\local\properties\text_align;
/**
* Class resourceoverview
*
@@ -24,6 +29,36 @@ namespace core_courseformat\local\overview;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class resourceoverview extends \core_courseformat\activityoverviewbase {
#[\Override]
public function get_actions_overview(): ?overviewitem {
if (!$this->is_resource()) {
// Only resource activities show the actions overview
// because they are aggregated in one table.
return null;
}
if (!has_capability('report/log:view', $this->context)) {
return null;
}
$content = new action_link(
url: new url(
'/report/log/index.php?',
['id' => $this->cm->course, 'modid' => $this->cm->id, 'chooselog' => 1, 'modaction' => 'r']
),
text: get_string('view'),
attributes: ['class' => button::BODY_OUTLINE->classes()],
);
return new overviewitem(
name: get_string('actions'),
value: '',
content: $content,
textalign: text_align::CENTER,
);
}
#[\Override]
public function get_extra_overview_items(): array {
return [
@@ -34,18 +69,11 @@ class resourceoverview extends \core_courseformat\activityoverviewbase {
/**
* Retrieves an overview item for the extra type of the resource.
*
* @return overviewitem|null
* @return overviewitem|null The overview item for the resource type.
*/
private function get_extra_type_overview(): ?overviewitem {
// Only resource activities shows the type overview
// because they are aggregated in one table.
$archetype = plugin_supports(
type: 'mod',
name: $this->cm->modname,
feature: FEATURE_MOD_ARCHETYPE,
default: MOD_ARCHETYPE_OTHER
);
if ($archetype != MOD_ARCHETYPE_RESOURCE) {
if (!$this->is_resource()) {
// Only resource activities show the type.
return null;
}
@@ -55,4 +83,20 @@ class resourceoverview extends \core_courseformat\activityoverviewbase {
content: $this->cm->modfullname,
);
}
/**
* Checks if the current activity is a resource type.
*
* @return bool True if the activity is a resource type, false otherwise.
*/
protected function is_resource(): bool {
// Check if the activity is a resource type.
$archetype = plugin_supports(
type: 'mod',
name: $this->cm->modname,
feature: FEATURE_MOD_ARCHETYPE,
default: MOD_ARCHETYPE_OTHER
);
return $archetype === MOD_ARCHETYPE_RESOURCE;
}
}
@@ -26,6 +26,111 @@ namespace core_courseformat\local\overview;
* @covers \core_courseformat\local\overview\resourceoverview
*/
final class resourceoverview_test extends \advanced_testcase {
/**
* Test get_actions_overview.
*
* @covers ::get_actions_overview
* @dataProvider provider_test_get_actions_overview
*
* @param string $role The role of the user to test.
* @param string $resourcetype The type of resource to create.
* @param array|null $expected Expected overview item data.
*/
public function test_get_actions_overview(
string $role,
string $resourcetype,
?array $expected
): void {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$currentuser = $this->getDataGenerator()->create_and_enrol($course, $role);
$resource = $this->getDataGenerator()->create_module($resourcetype, ['course' => $course->id]);
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($resource->cmid);
$this->setUser($currentuser);
$cm = get_fast_modinfo($course)->get_cm($resource->cmid);
$item = overviewfactory::create($cm)->get_actions_overview();
if ($expected === null) {
$this->assertNull($item);
return;
}
$this->assertEquals($expected['name'], $item->get_name());
$this->assertEquals($expected['value'], $item->get_value());
$this->assertStringContainsString($expected['content'], $item->get_content()->text);
}
/**
* Data provider for test_get_actions_overview.
*
* @return array
*/
public static function provider_test_get_actions_overview(): array {
return [
'Student' => [
'role' => 'student',
'resourcetype' => 'url',
'expected' => null,
],
'Teacher - Book' => [
'role' => 'editingteacher',
'resourcetype' => 'book',
'expected' => [
'name' => get_string('actions'),
'value' => '',
'content' => get_string('view'),
],
],
'Teacher - Folder' => [
'role' => 'editingteacher',
'resourcetype' => 'folder',
'expected' => [
'name' => get_string('actions'),
'value' => '',
'content' => get_string('view'),
],
],
'Teacher - Page' => [
'role' => 'editingteacher',
'resourcetype' => 'page',
'expected' => [
'name' => get_string('actions'),
'value' => '',
'content' => get_string('view'),
],
],
'Teacher - Resource' => [
'role' => 'editingteacher',
'resourcetype' => 'resource',
'expected' => [
'name' => get_string('actions'),
'value' => '',
'content' => get_string('view'),
],
],
'Teacher - URL' => [
'role' => 'editingteacher',
'resourcetype' => 'url',
'expected' => [
'name' => get_string('actions'),
'value' => '',
'content' => get_string('view'),
],
],
'Teacher - Non resource' => [
'role' => 'editingteacher',
'resourcetype' => 'lti',
'expected' => null,
],
];
}
/**
* Test get_extra_overview_items method.
*
@@ -47,6 +152,17 @@ final class resourceoverview_test extends \advanced_testcase {
$this->assertCount(1, $result);
$this->assertArrayHasKey('type', $result);
$this->assertInstanceOf(\core_courseformat\local\overview\overviewitem::class, $result['type']);
$activity = $this->getDataGenerator()->create_module('lti', ['course' => $course->id]);
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($activity->cmid);
$overview = overviewfactory::create($cm);
$result = $overview->get_extra_overview_items();
$this->assertCount(1, $result);
$this->assertArrayHasKey('type', $result);
$this->assertNull($result['type']);
}
/**
@@ -74,13 +190,13 @@ final class resourceoverview_test extends \advanced_testcase {
$overview = overviewfactory::create($cm);
$items = $overview->get_extra_overview_items();
$result = $items['type'];
if ($expected === null) {
$this->assertNull($result);
$this->assertTrue(!array_key_exists('type', $items) || $items['type'] === null);
return;
}
$result = $items['type'];
$this->assertEquals(get_string('resource_type'), $result->get_name());
$this->assertEquals($expected, $result->get_value());
$this->assertEquals($expected, $result->get_content());
@@ -113,7 +229,7 @@ final class resourceoverview_test extends \advanced_testcase {
'resourcetype' => 'url',
'expected' => 'URL',
],
// Activities without integration.
// Non-resource activities.
'bigbluebuttonbn' => [
'resourcetype' => 'bigbluebuttonbn',
'expected' => null,