Skip to content

Commit c2c3d3d

Browse files
committed
Add functionality
- Add lib::get_course_content_items() - Add unit tests - Add privacy class to satisfy unit test # Conflicts: # classes/privacy/provider.php # lib.php
1 parent 248e358 commit c2c3d3d

File tree

5 files changed

+187
-20
lines changed

5 files changed

+187
-20
lines changed

classes/local/lib.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace mod_cms\local;
18+
19+
use core_course\local\entity\content_item;
20+
21+
/**
22+
* Generic library functions for mod_cms.
23+
*
24+
* @package mod_cms
25+
* @author Jason den Dulk <[email protected]>
26+
* @copyright 2023, Catalyst IT
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class lib {
30+
/**
31+
* Obtains a list of defined content types to be included in the activity chooser panel.
32+
*
33+
* @param content_item $defaultmodulecontentitem
34+
* @param \stdClass $user Not used.
35+
* @param \stdClass $course Not used.
36+
* @return array
37+
*/
38+
public static function get_course_content_items(content_item $defaultmodulecontentitem, \stdClass $user,
39+
\stdClass $course) : array {
40+
$items = [];
41+
42+
$types = model\cms_types::get_records();
43+
foreach ($types as $type) {
44+
$items[] = new \core_course\local\entity\content_item(
45+
$type->get('id'),
46+
$defaultmodulecontentitem->get_name(),
47+
new \core_course\local\entity\string_title($type->get('name')),
48+
$defaultmodulecontentitem->get_link(),
49+
$defaultmodulecontentitem->get_icon(),
50+
$type->get('description'),
51+
$defaultmodulecontentitem->get_archetype(),
52+
$defaultmodulecontentitem->get_component_name(),
53+
$defaultmodulecontentitem->get_purpose()
54+
);
55+
}
56+
57+
return $items;
58+
}
59+
}

classes/privacy/provider.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17-
/**
18-
* Privacy subsystem implementation for mod_cms
19-
*
20-
* @package mod_cms
21-
* @author Marcus Boon<[email protected]>
22-
* @copyright Catalyst IT
23-
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24-
*/
2517
namespace mod_cms\privacy;
2618

2719
use context;
@@ -31,6 +23,14 @@
3123
use core_privacy\local\request\approved_contextlist;
3224
use core_privacy\local\request\approved_userlist;
3325

26+
/**
27+
* Privacy subsystem implementation for mod_cms
28+
*
29+
* @package mod_cms
30+
* @author Marcus Boon<[email protected]>
31+
* @copyright Catalyst IT
32+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33+
*/
3434
class provider implements
3535
\core_privacy\local\metadata\provider,
3636
\core_privacy\local\request\plugin\provider,

lib.php

+9-11
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
*/
2525

2626
use core_course\local\entity\content_item;
27+
use mod_cms\local\lib;
2728

2829
/**
30+
* Returns whether a feature is supported or not.
31+
*
2932
* @uses FEATURE_IDNUMBER
3033
* @uses FEATURE_GROUPS
3134
* @uses FEATURE_GROUPINGS
@@ -60,19 +63,14 @@ function cms_supports($feature) {
6063
}
6164

6265
/**
63-
* Return the custom content types which have been predefined.
66+
* Obtains a list of defined content types to be included in the activity chooser panel.
6467
*
6568
* @param content_item $defaultmodulecontentitem
66-
* @param stdClass $user
67-
* @param stdClass $course
68-
*
69+
* @param stdClass $user
70+
* @param stdClass $course
6971
* @return array
7072
*/
71-
function cms_get_course_content_items(content_item $defaultmodulecontentitem, stdClass $user,
72-
stdClass $course) {
73-
global $CFG, $OUTPUT;
74-
75-
$types = [];
76-
77-
return $types;
73+
function cms_get_course_content_items(content_item $defaultmodulecontentitem, \stdClass $user,
74+
\stdClass $course) {
75+
return lib::get_course_content_items($defaultmodulecontentitem, $user, $course);
7876
}

settings.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
);
5252

5353
if ($ADMIN->fulltree) {
54-
// TODO: There will probably be some more interesting settings to add here in the future.
54+
;// TODO: There will probably be some more interesting settings to add here in the future.
5555
}
5656

5757
$ADMIN->add('modcmsfolder', $settings);
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace mod_cms;
18+
19+
use core_course\local\entity\content_item;
20+
use mod_cms\local\lib;
21+
use mod_cms\local\model\cms_types;
22+
23+
24+
/**
25+
* Unit tests for mod_cms
26+
*
27+
* @package mod_cms
28+
* @author Jason den Dulk <[email protected]>
29+
* @copyright 2023, Catalyst IT
30+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31+
*/
32+
class get_course_content_items_test extends \advanced_testcase {
33+
/**
34+
* Set up before each test
35+
*/
36+
protected function setUp(): void {
37+
parent::setUp();
38+
$this->resetAfterTest();
39+
}
40+
41+
/**
42+
* Creates a default content item based on the logic in content_item_readonly_repository::find_all_for_course()
43+
*
44+
* @return content_item
45+
*/
46+
public function create_default_item(): content_item {
47+
global $OUTPUT, $DB;
48+
49+
$mod = $DB->get_record('modules', ['name' => 'cms']);
50+
51+
$archetype = plugin_supports('mod', $mod->name, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
52+
$purpose = plugin_supports('mod', $mod->name, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER);
53+
54+
$icon = 'monologo';
55+
56+
return new content_item(
57+
$mod->id,
58+
$mod->name,
59+
new \core_course\local\entity\lang_string_title("modulename", $mod->name),
60+
new \moodle_url('/course/mod.php', ['id' => 0, 'add' => $mod->name]),
61+
$OUTPUT->pix_icon($icon, '', $mod->name, ['class' => "activityicon"]),
62+
'help',
63+
$archetype,
64+
'mod_' . $mod->name,
65+
$purpose,
66+
);
67+
}
68+
69+
/**
70+
* Tests the lib::get_course_content_items function
71+
* @covers \mod_cms\local\lib::get_course_content_items
72+
*/
73+
public function test_get_course_content_items() {
74+
$types = [
75+
[ 'name' => 'CMS1', 'description' => 'help1'],
76+
[ 'name' => 'CMS2', 'description' => 'help2'],
77+
[ 'name' => 'betamax', 'description' => 'some description'],
78+
];
79+
80+
foreach ($types as $type) {
81+
$ct = new cms_types(0, (object) $type);
82+
$ct->save();
83+
}
84+
85+
$user = (object) [];
86+
$course = (object) [];
87+
88+
$items = lib::get_course_content_items($this->create_default_item(), $user, $course);
89+
90+
// Make sure the two arrays have the same ordering so they can be compared by index.
91+
usort(
92+
$types,
93+
function($a, $b) {
94+
return strcmp($a['name'], $b['name']);
95+
}
96+
);
97+
usort(
98+
$items,
99+
function($a, $b) {
100+
return strcmp($a->get_title()->get_value(), $b->get_title()->get_value());
101+
}
102+
);
103+
104+
$this->assertEquals(count($types), count($items));
105+
foreach ($types as $idx => $type) {
106+
$this->assertEquals($type['name'], $items[$idx]->get_title()->get_value());
107+
$this->assertEquals($type['description'], $items[$idx]->get_help());
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)