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_report\output;
18
 
19
use core\output\local\properties\iconsize;
20
use core_course\output\activity_icon;
21
 
22
/**
23
 * Course sections, subsections and activities structure for reports.
24
 *
25
 * @package    core_report
26
 * @copyright  2024 Amaia Anabitarte <amaia@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class coursestructure implements \renderable, \templatable {
30
 
31
    /**
32
     * Constructor
33
     *
34
     * @param \course_modinfo $modinfo
35
     */
36
    public function __construct(
37
        /** @var \course_modinfo $modinfo */
38
        protected \course_modinfo $modinfo
39
    ) {
40
    }
41
 
42
    /**
43
     * Exports the data.
44
     *
45
     * @param \renderer_base $output
46
     * @return array|\stdClass
47
     */
48
    public function export_for_template(\renderer_base $output) {
49
 
50
        $headers = $this->export_headers($output);
51
        return [
52
                'class' => 'generaltable boxaligncenter',
53
                'headers' => $headers,
54
                'headerscount' => count($headers),
55
                'activities' => $this->export_activities($output),
56
        ];
57
    }
58
 
59
    /**
60
     * Exports activities data.
61
     *
62
     * @param \renderer_base $output
63
     * @return array|\stdClass
64
     */
65
    public function export_activities(\renderer_base $output) {
66
 
67
        $activities = [];
68
 
69
        $delegatedsections = $this->modinfo->get_sections_delegated_by_cm();
70
        $allsections = $this->modinfo->get_sections();
71
        foreach ($allsections as $sectionnum => $sectionmodules) {
72
            // Add the section row.
73
            if ($sectionnum > 0) {
74
                $sectioninfo = $this->modinfo->get_section_info($sectionnum);
75
 
76
                // Don't show subsections here. We are showing them in the corresponding module.
77
                if ($sectioninfo->get_component_instance()) {
78
                    continue;
79
                }
80
 
81
                if (!$sectioninfo->uservisible) {
82
                    continue;
83
                }
84
 
85
                $activities[] = $this->export_section_data($output, $sectioninfo, false);
86
            }
87
 
88
            // Add section modules and possibly subsections.
89
            foreach ($sectionmodules as $cmid) {
90
                $cm = $this->modinfo->cms[$cmid];
91
 
92
                // Check if the module is delegating a section.
93
                if (array_key_exists($cm->id, $delegatedsections)) {
94
                    $subsectioninfo = $delegatedsections[$cm->id];
95
                    // Only non-empty are listed in allsections. We don't show empty sections.
96
                    if (!array_key_exists($subsectioninfo->sectionnum, $allsections)) {
97
                        continue;
98
                    }
99
 
100
                    $activities[] = $this->export_section_data($output, $subsectioninfo, true);
101
 
102
                    // Show activities inside the section.
103
                    $subsectionmodules = $allsections[$subsectioninfo->sectionnum];
104
                    foreach ($subsectionmodules as $subsectioncmid) {
105
                        $cm = $this->modinfo->cms[$subsectioncmid];
106
                        $activities[] = $this->export_activity_data($output, $cm, true);
107
                    }
108
                } else {
109
                    // It's simply a module.
110
                    $activities[] = $this->export_activity_data($output, $cm);
111
                }
112
            }
113
        }
114
 
115
        return $activities;
116
    }
117
 
118
    /**
119
     * Exports course sections, sections delegated by modules and modules data in a hierarchical format.
120
     *
121
     * @param \renderer_base $output
122
     * @return array|\stdClass
123
     */
124
    public function export_hierarchy(\renderer_base $output) {
125
 
126
        $sections = [];
127
 
128
        $allsections = $this->modinfo->get_sections();
129
        foreach ($allsections as $sectionnum => $sectionmodules) {
130
            // Add the section row.
131
            $sectioninfo = $this->modinfo->get_section_info($sectionnum);
132
 
133
            // Don't show subsections here. We are showing them in the corresponding module.
134
            if ($sectioninfo->get_component_instance()) {
135
                continue;
136
            }
137
 
138
            if (!$sectioninfo->uservisible) {
139
                continue;
140
            }
141
 
142
            $section = $this->export_section_data($output, $sectioninfo, false);
143
            if (empty($sectioninfo) || empty($sectioninfo->sequence)) {
144
                continue;
145
            }
146
 
147
            $activities = $this->export_hierarchy_section_activities_data($output, $sectioninfo, $allsections);
148
            if (!empty($activities)) {
149
                $section['activities'] = $activities;
150
            }
151
 
152
            $sections[] = $section;
153
        }
154
 
155
        return $sections;
156
    }
157
 
158
    /**
159
     * Exports activities data for a section in a hierarchical format.
160
     * @param \renderer_base $output
161
     * @param \section_info $sectioninfo
162
     * @param array $allsections
163
     * @return array
164
     */
165
    private function export_hierarchy_section_activities_data(
166
        \renderer_base $output,
167
        \section_info $sectioninfo,
168
        array $allsections
169
    ): array {
170
        $allsections = $this->modinfo->get_sections();
171
 
172
        $sectionmodules = explode(",", $sectioninfo->sequence);
173
        $activities = [];
174
 
175
        // Add section modules and possibly subsections.
176
        foreach ($sectionmodules as $cmid) {
177
            $activity = $this->export_hierarchy_activity_data($output, $this->modinfo->cms[$cmid], $allsections);
178
            if (!empty($activity)) {
179
                $activities[] = $activity;
180
            }
181
        }
182
        return $activities;
183
    }
184
 
185
    /**
186
     * Exports activity data for a section in a hierarchical format.
187
     * @param \renderer_base $output
188
     * @param \cm_info $cm
189
     * @param array $allsections
190
     * @return array|null
191
     */
192
    private function export_hierarchy_activity_data(
193
        \renderer_base $output,
194
        \cm_info $cm,
195
        array $allsections
196
    ): ?array {
197
        $delegatedsections = $this->modinfo->get_sections_delegated_by_cm();
198
 
199
        // Subsections has a special export.
200
        if (array_key_exists($cm->id, $delegatedsections)) {
201
            $subsectioninfo = $delegatedsections[$cm->id];
202
            // Only non-empty are listed in allsections. We don't show empty sections.
203
            if (!array_key_exists($subsectioninfo->sectionnum, $allsections)) {
204
                return null;
205
            }
206
 
207
            $subsection = $this->export_section_data($output, $subsectioninfo, true);
208
            if (empty($subsection)) {
209
                return null;
210
            }
211
 
212
            // Show activities inside the section.
213
            $subsectionmodules = $allsections[$subsectioninfo->sectionnum];
214
            $subactivities = [];
215
            foreach ($subsectionmodules as $subsectioncmid) {
216
                $cm = $this->modinfo->cms[$subsectioncmid];
217
                $activity = $this->export_activity_data($output, $cm, true);
218
                if (!empty($activity)) {
219
                    $subactivities[] = $activity;
220
                }
221
            }
222
            if (!empty($subactivities)) {
223
                $subsection['activities'] = $subactivities;
224
            }
225
            return $subsection;
226
        }
227
 
228
        return $this->export_activity_data($output, $cm);
229
    }
230
 
231
    /**
232
     * Exports the headers for report table.
233
     *
234
     * @param \renderer_base $output
235
     * @return array
236
     */
237
    protected function export_headers(\renderer_base $output): array {
238
        return [get_string('activity')];
239
    }
240
 
241
    /**
242
     * Exports the data for a single section.
243
     *
244
     * @param \renderer_base $output
245
     * @param \section_info $sectioninfo Section to export information from.
246
     * @param bool $isdelegated Whether the section is a delegated subsection or not.
247
     * @return array
248
     */
249
    public function export_section_data(\renderer_base $output, \section_info $sectioninfo, bool $isdelegated = false): array {
250
        $datasection = [
251
            'issection' => !$isdelegated,
252
            'isdelegated' => $isdelegated,
253
            'visible' => $sectioninfo->visible,
254
            'class' => 'section',
255
            'text' => get_section_name($sectioninfo->course, $sectioninfo->sectionnum),
256
        ];
257
 
258
        return $datasection;
259
    }
260
 
261
    /**
262
     * Exports the data for a single activity.
263
     *
264
     * @param \renderer_base $output
265
     * @param \cm_info $cm
266
     * @param bool $indelegated Whether the activity is part of a delegated section or not.
267
     * @return array
268
     */
269
    public function export_activity_data(\renderer_base $output, \cm_info $cm, bool $indelegated = false): array {
270
        global $CFG;
271
 
272
        if (!$cm->has_view()) {
273
            return [];
274
        }
275
        if (!$cm->uservisible) {
276
            return [];
277
        }
278
 
279
        $dataactivity = [
280
            'isactivity' => true,
281
            'indelegated' => $indelegated,
282
            'visible' => $cm->visible,
283
            'cells' => [],
284
        ];
285
 
286
        $activityicon = activity_icon::from_cm_info($cm)
287
            ->set_icon_size(iconsize::SIZE4);
288
 
289
        $dataactivity['activitycolumn'] = [
290
            'activityicon' => $output->render($activityicon),
291
            'link' => "$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id",
292
            'text' => $cm->get_formatted_name(),
293
        ];
294
        return $dataactivity;
295
    }
296
}