1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://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 |
/**
|
|
|
18 |
* Contains class content_item_repository, for fetching content_items.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @subpackage course
|
|
|
22 |
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
namespace core_course\local\repository;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
use core_component;
|
|
|
30 |
use core_course\local\entity\content_item;
|
|
|
31 |
use core_course\local\entity\lang_string_title;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* The class content_item_repository, for reading content_items.
|
|
|
35 |
*
|
|
|
36 |
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class content_item_readonly_repository implements content_item_readonly_repository_interface {
|
|
|
40 |
/**
|
|
|
41 |
* Get the help string for content items representing core modules.
|
|
|
42 |
*
|
|
|
43 |
* @param string $modname the module name.
|
|
|
44 |
* @return string the help string, including help link.
|
|
|
45 |
*/
|
|
|
46 |
private function get_core_module_help_string(string $modname): string {
|
|
|
47 |
global $OUTPUT;
|
|
|
48 |
|
|
|
49 |
$help = '';
|
|
|
50 |
$sm = get_string_manager();
|
|
|
51 |
if ($sm->string_exists('modulename_help', $modname)) {
|
|
|
52 |
$help = get_string('modulename_help', $modname);
|
|
|
53 |
if ($sm->string_exists('modulename_link', $modname)) { // Link to further info in Moodle docs.
|
|
|
54 |
$link = get_string('modulename_link', $modname);
|
|
|
55 |
$linktext = get_string('morehelp');
|
|
|
56 |
$arialabel = get_string('morehelpaboutmodule', '', get_string('modulename', $modname));
|
|
|
57 |
$doclink = $OUTPUT->doc_link($link, $linktext, true, ['aria-label' => $arialabel]);
|
|
|
58 |
$help .= \html_writer::tag('div', $doclink, ['class' => 'helpdoclink']);
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
return $help;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Helper to get the contentitems from all subplugin hooks for a given module plugin.
|
|
|
66 |
*
|
|
|
67 |
* @param string $parentpluginname the name of the module plugin to check subplugins for.
|
|
|
68 |
* @param content_item $modulecontentitem the content item of the module plugin, to pass to the hooks.
|
|
|
69 |
* @param \stdClass $user the user object to pass to subplugins.
|
|
|
70 |
* @return array the array of content items.
|
|
|
71 |
*/
|
|
|
72 |
private function get_subplugin_course_content_items(string $parentpluginname, content_item $modulecontentitem,
|
|
|
73 |
\stdClass $user): array {
|
|
|
74 |
|
|
|
75 |
$contentitems = [];
|
|
|
76 |
$pluginmanager = \core_plugin_manager::instance();
|
|
|
77 |
foreach ($pluginmanager->get_subplugins_of_plugin($parentpluginname) as $subpluginname => $subplugin) {
|
|
|
78 |
// Call the hook, but with a copy of the module content item data.
|
|
|
79 |
$spcontentitems = component_callback($subpluginname, 'get_course_content_items', [$modulecontentitem, $user], null);
|
|
|
80 |
if (!is_null($spcontentitems)) {
|
|
|
81 |
foreach ($spcontentitems as $spcontentitem) {
|
|
|
82 |
$contentitems[] = $spcontentitem;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
return $contentitems;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Get all the content items for a subplugin.
|
|
|
91 |
*
|
|
|
92 |
* @param string $parentpluginname
|
|
|
93 |
* @param content_item $modulecontentitem
|
|
|
94 |
* @return array
|
|
|
95 |
*/
|
|
|
96 |
private function get_subplugin_all_content_items(string $parentpluginname, content_item $modulecontentitem): array {
|
|
|
97 |
$contentitems = [];
|
|
|
98 |
$pluginmanager = \core_plugin_manager::instance();
|
|
|
99 |
foreach ($pluginmanager->get_subplugins_of_plugin($parentpluginname) as $subpluginname => $subplugin) {
|
|
|
100 |
// Call the hook, but with a copy of the module content item data.
|
|
|
101 |
$spcontentitems = component_callback($subpluginname, 'get_all_content_items', [$modulecontentitem], null);
|
|
|
102 |
if (!is_null($spcontentitems)) {
|
|
|
103 |
foreach ($spcontentitems as $spcontentitem) {
|
|
|
104 |
$contentitems[] = $spcontentitem;
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
return $contentitems;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Find all the available content items, not restricted to course or user.
|
|
|
113 |
*
|
|
|
114 |
* @return array the array of content items.
|
|
|
115 |
*/
|
|
|
116 |
public function find_all(): array {
|
|
|
117 |
global $OUTPUT, $DB, $CFG;
|
|
|
118 |
|
|
|
119 |
// Get all modules so we know which plugins are enabled and able to add content.
|
|
|
120 |
// Only module plugins may add content items.
|
|
|
121 |
$modules = $DB->get_records('modules', ['visible' => 1]);
|
|
|
122 |
$return = [];
|
|
|
123 |
|
|
|
124 |
// Now, generate the content_items.
|
|
|
125 |
foreach ($modules as $modid => $mod) {
|
|
|
126 |
// Exclude modules if the code doesn't exist.
|
|
|
127 |
if (!file_exists("$CFG->dirroot/mod/$mod->name/lib.php")) {
|
|
|
128 |
continue;
|
|
|
129 |
}
|
|
|
130 |
// Create the content item for the module itself.
|
|
|
131 |
// If the module chooses to implement the hook, this may be thrown away.
|
|
|
132 |
$help = $this->get_core_module_help_string($mod->name);
|
|
|
133 |
$archetype = plugin_supports('mod', $mod->name, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
|
|
|
134 |
$purpose = plugin_supports('mod', $mod->name, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER);
|
|
|
135 |
$isbranded = component_callback('mod_' . $mod->name, 'is_branded', [], false);
|
|
|
136 |
|
|
|
137 |
$contentitem = new content_item(
|
|
|
138 |
$mod->id,
|
|
|
139 |
$mod->name,
|
|
|
140 |
new lang_string_title("modulename", $mod->name),
|
|
|
141 |
new \moodle_url(''), // No course scope, so just an empty link.
|
|
|
142 |
$OUTPUT->pix_icon('monologo', '', $mod->name, ['class' => 'icon activityicon']),
|
|
|
143 |
$help,
|
|
|
144 |
$archetype,
|
|
|
145 |
'mod_' . $mod->name,
|
|
|
146 |
$purpose,
|
|
|
147 |
$isbranded,
|
|
|
148 |
);
|
|
|
149 |
|
|
|
150 |
$modcontentitemreference = clone($contentitem);
|
|
|
151 |
|
|
|
152 |
if (component_callback_exists('mod_' . $mod->name, 'get_all_content_items')) {
|
|
|
153 |
// Call the module hooks for this module.
|
|
|
154 |
$plugincontentitems = component_callback('mod_' . $mod->name, 'get_all_content_items',
|
|
|
155 |
[$modcontentitemreference], []);
|
|
|
156 |
if (!empty($plugincontentitems)) {
|
|
|
157 |
array_push($return, ...$plugincontentitems);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
// Now, get those for subplugins of the module.
|
|
|
161 |
$subplugincontentitems = $this->get_subplugin_all_content_items('mod_' . $mod->name, $modcontentitemreference);
|
|
|
162 |
if (!empty($subplugincontentitems)) {
|
|
|
163 |
array_push($return, ...$subplugincontentitems);
|
|
|
164 |
}
|
|
|
165 |
} else {
|
|
|
166 |
// Neither callback was found, so just use the default module content item.
|
|
|
167 |
$return[] = $contentitem;
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
return $return;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Get the list of potential content items for the given course.
|
|
|
175 |
*
|
|
|
176 |
* @param \stdClass $course the course
|
|
|
177 |
* @param \stdClass $user the user, to pass to plugins implementing callbacks.
|
|
|
178 |
* @return array the array of content_item objects
|
|
|
179 |
*/
|
|
|
180 |
public function find_all_for_course(\stdClass $course, \stdClass $user): array {
|
|
|
181 |
global $OUTPUT, $DB, $CFG;
|
|
|
182 |
|
|
|
183 |
// Get all modules so we know which plugins are enabled and able to add content.
|
|
|
184 |
// Only module plugins may add content items.
|
|
|
185 |
$modules = $DB->get_records('modules', ['visible' => 1]);
|
|
|
186 |
$return = [];
|
|
|
187 |
|
|
|
188 |
// Now, generate the content_items.
|
|
|
189 |
foreach ($modules as $modid => $mod) {
|
|
|
190 |
// Exclude modules if the code doesn't exist.
|
|
|
191 |
if (!file_exists("$CFG->dirroot/mod/$mod->name/lib.php")) {
|
|
|
192 |
continue;
|
|
|
193 |
}
|
|
|
194 |
// Create the content item for the module itself.
|
|
|
195 |
// If the module chooses to implement the hook, this may be thrown away.
|
|
|
196 |
$help = $this->get_core_module_help_string($mod->name);
|
|
|
197 |
$archetype = plugin_supports('mod', $mod->name, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
|
|
|
198 |
$purpose = plugin_supports('mod', $mod->name, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER);
|
|
|
199 |
$isbranded = component_callback('mod_' . $mod->name, 'is_branded', [], false);
|
|
|
200 |
|
|
|
201 |
$icon = 'monologo';
|
|
|
202 |
// Quick check for monologo icons.
|
|
|
203 |
// Plugins that don't have monologo icons will be displayed as is and CSS filter will not be applied.
|
|
|
204 |
$hasmonologoicons = core_component::has_monologo_icon('mod', $mod->name);
|
|
|
205 |
$iconclass = '';
|
|
|
206 |
if (!$hasmonologoicons) {
|
|
|
207 |
$iconclass = 'nofilter';
|
|
|
208 |
}
|
|
|
209 |
$contentitem = new content_item(
|
|
|
210 |
$mod->id,
|
|
|
211 |
$mod->name,
|
|
|
212 |
new lang_string_title("modulename", $mod->name),
|
|
|
213 |
new \moodle_url('/course/mod.php', ['id' => $course->id, 'add' => $mod->name]),
|
|
|
214 |
$OUTPUT->pix_icon($icon, '', $mod->name, ['class' => "activityicon $iconclass"]),
|
|
|
215 |
$help,
|
|
|
216 |
$archetype,
|
|
|
217 |
'mod_' . $mod->name,
|
|
|
218 |
$purpose,
|
|
|
219 |
$isbranded,
|
|
|
220 |
);
|
|
|
221 |
|
|
|
222 |
$modcontentitemreference = clone($contentitem);
|
|
|
223 |
|
|
|
224 |
if (component_callback_exists('mod_' . $mod->name, 'get_course_content_items')) {
|
|
|
225 |
// Call the module hooks for this module.
|
|
|
226 |
$plugincontentitems = component_callback('mod_' . $mod->name, 'get_course_content_items',
|
|
|
227 |
[$modcontentitemreference, $user, $course], []);
|
|
|
228 |
if (!empty($plugincontentitems)) {
|
|
|
229 |
array_push($return, ...$plugincontentitems);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
// Now, get those for subplugins of the module.
|
|
|
233 |
$subpluginitems = $this->get_subplugin_course_content_items('mod_' . $mod->name, $modcontentitemreference, $user);
|
|
|
234 |
if (!empty($subpluginitems)) {
|
|
|
235 |
array_push($return, ...$subpluginitems);
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
} else {
|
|
|
239 |
// Callback was not found, so just use the default module content item.
|
|
|
240 |
$return[] = $contentitem;
|
|
|
241 |
}
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
return $return;
|
|
|
245 |
}
|
|
|
246 |
}
|