Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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.
1441 ariadna 54
                // The link is stored in a language file but should not be translated, use value for English.
55
                $link = $sm->get_string('modulename_link', $modname, null, 'en');
56
                // The text 'More help' and other strings should be in the current language.
1 efrain 57
                $linktext = get_string('morehelp');
58
                $arialabel = get_string('morehelpaboutmodule', '', get_string('modulename', $modname));
59
                $doclink = $OUTPUT->doc_link($link, $linktext, true, ['aria-label' => $arialabel]);
60
                $help .= \html_writer::tag('div', $doclink, ['class' => 'helpdoclink']);
61
            }
62
        }
63
        return $help;
64
    }
65
 
66
    /**
67
     * Helper to get the contentitems from all subplugin hooks for a given module plugin.
68
     *
69
     * @param string $parentpluginname the name of the module plugin to check subplugins for.
70
     * @param content_item $modulecontentitem the content item of the module plugin, to pass to the hooks.
71
     * @param \stdClass $user the user object to pass to subplugins.
72
     * @return array the array of content items.
73
     */
74
    private function get_subplugin_course_content_items(string $parentpluginname, content_item $modulecontentitem,
75
            \stdClass $user): array {
76
 
77
        $contentitems = [];
78
        $pluginmanager = \core_plugin_manager::instance();
79
        foreach ($pluginmanager->get_subplugins_of_plugin($parentpluginname) as $subpluginname => $subplugin) {
80
            // Call the hook, but with a copy of the module content item data.
81
            $spcontentitems = component_callback($subpluginname, 'get_course_content_items', [$modulecontentitem, $user], null);
82
            if (!is_null($spcontentitems)) {
83
                foreach ($spcontentitems as $spcontentitem) {
84
                    $contentitems[] = $spcontentitem;
85
                }
86
            }
87
        }
88
        return $contentitems;
89
    }
90
 
91
    /**
92
     * Get all the content items for a subplugin.
93
     *
94
     * @param string $parentpluginname
95
     * @param content_item $modulecontentitem
96
     * @return array
97
     */
98
    private function get_subplugin_all_content_items(string $parentpluginname, content_item $modulecontentitem): array {
99
        $contentitems = [];
100
        $pluginmanager = \core_plugin_manager::instance();
101
        foreach ($pluginmanager->get_subplugins_of_plugin($parentpluginname) as $subpluginname => $subplugin) {
102
            // Call the hook, but with a copy of the module content item data.
103
            $spcontentitems = component_callback($subpluginname, 'get_all_content_items', [$modulecontentitem], null);
104
            if (!is_null($spcontentitems)) {
105
                foreach ($spcontentitems as $spcontentitem) {
106
                    $contentitems[] = $spcontentitem;
107
                }
108
            }
109
        }
110
        return $contentitems;
111
    }
112
 
113
    /**
1441 ariadna 114
     * Filter out any modules that are not to be rendered or managed on the course page.
115
     *
116
     * @param content_item[] $contentitems items to be filtered
117
     * @return content_item[] filtered items
118
     */
119
    private static function filter_out_items_not_to_be_displayed(array $contentitems): array {
120
        return array_filter($contentitems, static function ($module) {
121
            [, $name] = core_component::normalize_component($module->get_component_name());
122
            return \course_modinfo::is_mod_type_visible_on_course($name);
123
        });
124
    }
125
 
126
    /**
1 efrain 127
     * Find all the available content items, not restricted to course or user.
128
     *
129
     * @return array the array of content items.
130
     */
131
    public function find_all(): array {
132
        global $OUTPUT, $DB, $CFG;
133
 
134
        // Get all modules so we know which plugins are enabled and able to add content.
135
        // Only module plugins may add content items.
136
        $modules = $DB->get_records('modules', ['visible' => 1]);
137
        $return = [];
138
 
139
        // Now, generate the content_items.
140
        foreach ($modules as $modid => $mod) {
141
            // Exclude modules if the code doesn't exist.
142
            if (!file_exists("$CFG->dirroot/mod/$mod->name/lib.php")) {
143
                continue;
144
            }
145
            // Create the content item for the module itself.
146
            // If the module chooses to implement the hook, this may be thrown away.
147
            $help = $this->get_core_module_help_string($mod->name);
148
            $archetype = plugin_supports('mod', $mod->name, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
149
            $purpose = plugin_supports('mod', $mod->name, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER);
150
            $isbranded = component_callback('mod_' . $mod->name, 'is_branded', [], false);
151
 
152
            $contentitem = new content_item(
153
                $mod->id,
154
                $mod->name,
155
                new lang_string_title("modulename", $mod->name),
156
                new \moodle_url(''), // No course scope, so just an empty link.
157
                $OUTPUT->pix_icon('monologo', '', $mod->name, ['class' => 'icon activityicon']),
158
                $help,
159
                $archetype,
160
                'mod_' . $mod->name,
161
                $purpose,
162
                $isbranded,
163
            );
164
 
165
            $modcontentitemreference = clone($contentitem);
166
 
167
            if (component_callback_exists('mod_' . $mod->name, 'get_all_content_items')) {
168
                // Call the module hooks for this module.
169
                $plugincontentitems = component_callback('mod_' . $mod->name, 'get_all_content_items',
170
                    [$modcontentitemreference], []);
171
                if (!empty($plugincontentitems)) {
172
                    array_push($return, ...$plugincontentitems);
173
                }
174
 
175
                // Now, get those for subplugins of the module.
176
                $subplugincontentitems = $this->get_subplugin_all_content_items('mod_' . $mod->name, $modcontentitemreference);
177
                if (!empty($subplugincontentitems)) {
178
                    array_push($return, ...$subplugincontentitems);
179
                }
180
            } else {
181
                // Neither callback was found, so just use the default module content item.
182
                $return[] = $contentitem;
183
            }
184
        }
1441 ariadna 185
 
186
        // These module types are not to be rendered to the course page.
187
        return self::filter_out_items_not_to_be_displayed($return);
1 efrain 188
    }
189
 
190
    /**
191
     * Get the list of potential content items for the given course.
192
     *
193
     * @param \stdClass $course the course
194
     * @param \stdClass $user the user, to pass to plugins implementing callbacks.
195
     * @return array the array of content_item objects
196
     */
197
    public function find_all_for_course(\stdClass $course, \stdClass $user): array {
198
        global $OUTPUT, $DB, $CFG;
199
 
200
        // Get all modules so we know which plugins are enabled and able to add content.
201
        // Only module plugins may add content items.
202
        $modules = $DB->get_records('modules', ['visible' => 1]);
203
        $return = [];
204
 
205
        // Now, generate the content_items.
206
        foreach ($modules as $modid => $mod) {
207
            // Exclude modules if the code doesn't exist.
208
            if (!file_exists("$CFG->dirroot/mod/$mod->name/lib.php")) {
209
                continue;
210
            }
211
            // Create the content item for the module itself.
212
            // If the module chooses to implement the hook, this may be thrown away.
213
            $help = $this->get_core_module_help_string($mod->name);
214
            $archetype = plugin_supports('mod', $mod->name, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
215
            $purpose = plugin_supports('mod', $mod->name, FEATURE_MOD_PURPOSE, MOD_PURPOSE_OTHER);
216
            $isbranded = component_callback('mod_' . $mod->name, 'is_branded', [], false);
217
 
218
            $icon = 'monologo';
219
            // Quick check for monologo icons.
220
            // Plugins that don't have monologo icons will be displayed as is and CSS filter will not be applied.
221
            $hasmonologoicons = core_component::has_monologo_icon('mod', $mod->name);
222
            $iconclass = '';
223
            if (!$hasmonologoicons) {
224
                $iconclass = 'nofilter';
225
            }
226
            $contentitem = new content_item(
227
                $mod->id,
228
                $mod->name,
229
                new lang_string_title("modulename", $mod->name),
230
                new \moodle_url('/course/mod.php', ['id' => $course->id, 'add' => $mod->name]),
231
                $OUTPUT->pix_icon($icon, '', $mod->name, ['class' => "activityicon $iconclass"]),
232
                $help,
233
                $archetype,
234
                'mod_' . $mod->name,
235
                $purpose,
236
                $isbranded,
237
            );
238
 
239
            $modcontentitemreference = clone($contentitem);
240
 
241
            if (component_callback_exists('mod_' . $mod->name, 'get_course_content_items')) {
242
                // Call the module hooks for this module.
243
                $plugincontentitems = component_callback('mod_' . $mod->name, 'get_course_content_items',
244
                    [$modcontentitemreference, $user, $course], []);
245
                if (!empty($plugincontentitems)) {
246
                    array_push($return, ...$plugincontentitems);
247
                }
248
 
249
                // Now, get those for subplugins of the module.
250
                $subpluginitems = $this->get_subplugin_course_content_items('mod_' . $mod->name, $modcontentitemreference, $user);
251
                if (!empty($subpluginitems)) {
252
                    array_push($return, ...$subpluginitems);
253
                }
254
 
255
            } else {
256
                // Callback was not found, so just use the default module content item.
257
                $return[] = $contentitem;
258
            }
259
        }
260
 
1441 ariadna 261
        // These module types are not to be rendered to the course page.
262
        return self::filter_out_items_not_to_be_displayed($return);
1 efrain 263
    }
264
}