Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_courseformat\output\local\overview;
18
 
19
use core\context\course as context_course;
20
use core\output\named_templatable;
21
use core\output\renderable;
22
use core\url;
23
use core_course\output\activity_icon;
24
use core_collator;
25
use stdClass;
26
 
27
/**
28
 * Class overview page
29
 *
30
 * @package    core_course
31
 * @copyright  2025 Ferran Recio <ferran@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class overviewpage implements renderable, named_templatable {
35
    /** @var context_course the context. */
36
    protected context_course $context;
37
 
38
    /**
39
     * Constructor.
40
     *
41
     * @param stdClass $course the course object.
42
     * @param string[] $expanded the sections to be expanded on load.
43
     */
44
    public function __construct(
45
        /** @var stdClass the course object  */
46
        protected stdClass $course,
47
        /** @var string[] the sections to be expanded on load  */
48
        protected array $expanded = [],
49
    ) {
50
        $this->context = context_course::instance($this->course->id);
51
    }
52
 
53
    /**
54
     * Gets the URL to the course overview page for a given course and module name.
55
     *
56
     * @param int $courseid
57
     * @param string $modname
58
     * @return url
59
     */
60
    public static function get_modname_url(int $courseid, string $modname): url {
61
        return new url(
62
            url: '/course/overview.php',
63
            params: ['id' => $courseid, 'expand[]' => $modname],
64
            anchor: "{$modname}_overview_collapsible",
65
        );
66
    }
67
 
68
    #[\Override]
69
    public function export_for_template(\renderer_base $output): stdClass {
70
        $modfullnames = $this->get_course_activities_overview_list();
71
 
72
        $elements = [];
73
        foreach ($modfullnames as $modname => $modfullname) {
74
            $elements[] = $this->export_activity_overview_section_data($output, $modname, $modfullname);
75
        }
76
 
77
        return (object) [
78
            'elements' => $elements,
79
            'courseid' => $this->course->id,
80
            'contextid' => $this->context->id,
81
        ];
82
    }
83
 
84
    /**
85
     * Retrieves a list of course activities overview.
86
     *
87
     * @return string[] An associative array module name => module plural name.
88
     */
89
    private function get_course_activities_overview_list(): array {
90
        $modinfo = get_fast_modinfo($this->course);
91
        $modfullnames = [];
92
        $archetypes = [];
93
 
94
        foreach ($modinfo->cms as $cm) {
95
            // Exclude activities that aren't visible or have no view link (e.g. label).
96
            // Account for folder being displayed inline.
97
            if (!$cm->uservisible || (!$cm->has_view() && strcmp($cm->modname, 'folder') !== 0)) {
98
                continue;
99
            }
100
            if (array_key_exists($cm->modname, $modfullnames)) {
101
                continue;
102
            }
103
            if (!array_key_exists($cm->modname, $archetypes)) {
104
                $archetypes[$cm->modname] = plugin_supports(
105
                    type: 'mod',
106
                    name: $cm->modname,
107
                    feature: FEATURE_MOD_ARCHETYPE,
108
                    default: MOD_ARCHETYPE_OTHER
109
                );
110
            }
111
            if ($archetypes[$cm->modname] == MOD_ARCHETYPE_RESOURCE) {
112
                if (!array_key_exists('resource', $modfullnames)) {
113
                    $modfullnames['resource'] = get_string('resources');
114
                }
115
            } else {
116
                $modfullnames[$cm->modname] = $cm->modplural;
117
            }
118
        }
119
 
120
        core_collator::asort($modfullnames);
121
        return $modfullnames;
122
    }
123
 
124
    /**
125
     * Exports the data for the activity overview section.
126
     *
127
     * This function checks if the activity has an overview integration,
128
     * and return the data accordingly.
129
     *
130
     * @param \renderer_base $output
131
     * @param string $modname The name of the module.
132
     * @param string $modfullname The full name of the module.
133
     * @return stdClass The exported data for the activity overview section.
134
     */
135
    private function export_activity_overview_section_data(
136
        \renderer_base $output,
137
        string $modname,
138
        string $modfullname
139
    ): stdClass {
140
        return (object) [
141
            'fragment' => $this->export_overview_fragment($modname),
142
            'icon' => $this->get_activity_overview_icon($output, $modname),
143
            'name' => $modfullname,
144
            'shortname' => $modname,
145
            'open' => in_array($modname, $this->expanded),
146
        ];
147
    }
148
 
149
    /**
150
     * Generates the activity overview icon for a given module.
151
     *
152
     * @param \renderer_base $output
153
     * @param string $modname The name of the module for which the icon is being generated.
154
     * @return string The HTML string for the activity overview icon.
155
     */
156
    private function get_activity_overview_icon(\renderer_base $output, string $modname): string {
157
        // Resource is a generic term for all modules with MOD_ARCHETYPE_RESOURCE.
158
        // We group all of them under the mod_page icon.
159
        if ($modname === 'resource') {
160
            $modname = 'page';
161
        }
162
        return $output->render(activity_icon::from_modname($modname));
163
    }
164
 
165
    /**
166
     * Exports an overview fragment for a given module name.
167
     *
168
     * This function creates and returns an object containing details
169
     * about the course overview fragment for the specified module.
170
     *
171
     * @param string $modname
172
     * @return stdClass The exported overview fragment data.
173
     */
174
    private function export_overview_fragment(string $modname): stdClass {
175
        // If the element is expanded, we don't need to load the fragment.
176
        if (in_array($modname, $this->expanded)) {
177
            return (object) [
178
                'preloadedcontent' => course_output_fragment_course_overview([
179
                    'courseid' => $this->course->id,
180
                    'modname' => $modname,
181
                ]),
182
            ];
183
        }
184
        return (object) [
185
            'component' => 'core_course',
186
            'method' => 'overview_table',
187
            'course' => $this->course,
188
            'modname' => $modname,
189
        ];
190
    }
191
 
192
    /**
193
     * Get the name of the template to use for this templatable.
194
     *
195
     * @param \renderer_base $renderer The renderer requesting the template name
196
     * @return string
197
     */
198
    public function get_template_name(\renderer_base $renderer): string {
199
        return 'core_courseformat/local/overview/overviewpage';
200
    }
201
}