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_question\output;
18
 
19
use core\context;
20
use core\output\renderable;
21
use core\output\renderer_base;
22
use core\output\templatable;
23
use core_question\local\bank\question_version_status;
24
 
25
/**
26
 * A select menu of question categories.
27
 *
28
 *
29
 * @package   core_question
30
 * @copyright 2025 onwards Catalyst IT EU {@link https://catalyst-eu.net}
31
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
32
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class question_category_selector implements renderable, templatable {
35
 
36
    /**
37
     * Constructor.
38
     *
39
     * @param array $contexts
40
     * @param bool $top
41
     * @param string $currentcat
42
     * @param string $selected
43
     * @param int $nochildrenof
44
     * @param bool $autocomplete
45
     */
46
    public function __construct(
47
        /** @var array The module contexts for the question banks to show category options for. */
48
        protected array $contexts = [],
49
        /** @var bool If true, include top categories for each context in the options. */
50
        protected bool $top = false,
51
        /** @var string The current category, to exclude from the list. */
52
        protected $currentcat = 0,
53
        /** @var string The value of the initially selected option. */
54
        protected string $selected = "",
55
        /** @var int If this matches the ID of a category in the list, don't include its children. */
56
        protected int $nochildrenof = -1,
57
        /** @var bool If true, return options as a flattened array suitable for a list of autocomplete suggestions. */
58
        protected bool $autocomplete = false,
59
    ) {
60
    }
61
 
62
    /**
63
     * Get all the category objects, including a count of the number of questions in that category,
64
     * for all the categories in the lists $contexts.
65
     *
66
     * @param string $contexts comma separated list of contextids
67
     * @param string $sortorder used as the ORDER BY clause in the select statement.
68
     * @param bool $top Whether to return the top categories or not.
69
     * @param int $showallversions 1 to show all versions not only the latest.
70
     * @return array of category objects.
71
     * @throws \dml_exception
72
     */
73
    public function get_categories_for_contexts(
74
        string $contexts,
75
        string $sortorder = 'parent, sortorder, name ASC',
76
        bool $top = false,
77
        int $showallversions = 0,
78
    ): array {
79
        global $DB;
80
 
81
        $contextids = explode(',', $contexts);
82
        foreach ($contextids as $contextid) {
83
            $context = context::instance_by_id($contextid);
84
            if ($context->contextlevel === CONTEXT_MODULE) {
85
                $validcontexts[] = $contextid;
86
            }
87
        }
88
        if (empty($validcontexts)) {
89
            return [];
90
        }
91
 
92
        [$insql, $inparams] = $DB->get_in_or_equal($validcontexts);
93
 
94
        $topwhere = $top ? '' : 'AND c.parent <> 0';
95
        $statuscondition = "AND (qv.status = '" . question_version_status::QUESTION_STATUS_READY . "' " .
96
            " OR qv.status = '" . question_version_status::QUESTION_STATUS_DRAFT . "' )";
97
        $substatuscondition = "AND v.status <> '"  . question_version_status::QUESTION_STATUS_HIDDEN . "' ";
98
        $sql = "SELECT c.*,
99
                    (SELECT COUNT(1)
100
                       FROM {question} q
101
                       JOIN {question_versions} qv ON qv.questionid = q.id
102
                       JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
103
                      WHERE q.parent = '0'
104
                        $statuscondition
105
                            AND c.id = qbe.questioncategoryid
106
                            AND ({$showallversions} = 1
107
                                OR (qv.version = (SELECT MAX(v.version)
108
                                                    FROM {question_versions} v
109
                                                    JOIN {question_bank_entries} be ON be.id = v.questionbankentryid
110
                                                   WHERE be.id = qbe.id $substatuscondition)
111
                                   )
112
                                )
113
                            ) AS questioncount
114
                  FROM {question_categories} c
115
                 WHERE c.contextid {$insql} {$topwhere}
116
              ORDER BY {$sortorder}";
117
 
118
        return $DB->get_records_sql($sql, $inparams);
119
    }
120
 
121
    /**
122
     * Output an array of question categories.
123
     *
124
     * @param array $contexts The list of contexts.
125
     * @param bool $top Whether to return the top categories or not.
126
     * @param int $currentcat The current category, to exclude from the list.
127
     * @param bool $popupform Return each question bank's group in an additional nested array.
128
     * @param int $nochildrenof Don't include children of this category
129
     * @param bool $escapecontextnames Whether the returned name of the thing is to be HTML escaped or not.
130
     * @return array
131
     * @throws \coding_exception|\dml_exception
132
     */
133
    public function question_category_options(
134
        array $contexts,
135
        bool $top = false,
136
        int $currentcat = 0,
137
        bool $popupform = false,
138
        int $nochildrenof = -1,
139
        bool $escapecontextnames = true,
140
    ): array {
141
        global $CFG;
142
        $pcontexts = [];
143
        foreach ($contexts as $context) {
144
            if ($context->contextlevel !== CONTEXT_MODULE) {
145
                continue;
146
            }
147
            $pcontexts[] = $context->id;
148
        }
149
        $contextslist = join(', ', $pcontexts);
150
 
151
        $categories = $this->get_categories_for_contexts($contextslist, 'parent, sortorder, name ASC', $top);
152
 
153
        if ($top) {
154
            $categories = $this->question_fix_top_names($categories);
155
        }
156
 
157
        $categories = $this->question_add_context_in_key($categories);
158
        $categories = $this->add_indented_names($categories, $nochildrenof);
159
 
160
        // Sort cats out into different contexts.
161
        $categoriesarray = [];
162
        foreach ($pcontexts as $contextid) {
163
            $context = context::instance_by_id($contextid);
164
            $contextstring = $context->get_context_name(true, true, $escapecontextnames);
165
            foreach ($categories as $category) {
166
                if ($category->contextid == $contextid) {
167
                    $cid = $category->id;
168
                    if ("{$currentcat},{$contextid}" != $cid || $currentcat == 0) {
169
                        $a = new \stdClass();
170
                        $a->name = format_string(
171
                            $category->indentedname,
172
                            true,
173
                            ['context' => $context]
174
                        );
175
                        if ($category->idnumber !== null && $category->idnumber !== '') {
176
                            $a->idnumber = s($category->idnumber);
177
                        }
178
                        if (!empty($category->questioncount)) {
179
                            $a->questioncount = $category->questioncount;
180
                        }
181
                        if (isset($a->idnumber) && isset($a->questioncount)) {
182
                            $formattedname = get_string('categorynamewithidnumberandcount', 'question', $a);
183
                        } else if (isset($a->idnumber)) {
184
                            $formattedname = get_string('categorynamewithidnumber', 'question', $a);
185
                        } else if (isset($a->questioncount)) {
186
                            $formattedname = get_string('categorynamewithcount', 'question', $a);
187
                        } else {
188
                            $formattedname = $a->name;
189
                        }
190
                        $categoriesarray[$contextstring][$cid] = $formattedname;
191
                    }
192
                }
193
            }
194
        }
195
        if ($popupform) {
196
            $popupcats = [];
197
            foreach ($categoriesarray as $contextstring => $optgroup) {
198
                $group = [];
199
                foreach ($optgroup as $key => $value) {
200
                    $key = str_replace($CFG->wwwroot, '', $key);
201
                    $group[$key] = $value;
202
                }
203
                $popupcats[] = [$contextstring => $group];
204
            }
205
            return $popupcats;
206
        } else {
207
            return $categoriesarray;
208
        }
209
    }
210
 
211
    /**
212
     * Add context in categories key.
213
     *
214
     * @param array $categories The list of categories, keyed by ID.
215
     * @return array The list with the context id added to each key, id, and parent attribute.
216
     */
217
    public function question_add_context_in_key(array $categories): array {
218
        $newcatarray = [];
219
        foreach ($categories as $id => $category) {
220
            $category->parent = "$category->parent,$category->contextid";
221
            $category->id = "$category->id,$category->contextid";
222
            $newcatarray["$id,$category->contextid"] = $category;
223
        }
224
        return $newcatarray;
225
    }
226
 
227
    /**
228
     * Finds top categories in the given categories hierarchy and replace their name with a proper localised string.
229
     *
230
     * @param array $categories An array of question categories.
231
     * @param bool $escape Whether the returned name of the thing is to be HTML escaped or not.
232
     * @return array The same question category list given to the function, with the top category names being translated.
233
     * @throws \coding_exception
234
     */
235
    public function question_fix_top_names(array $categories, bool $escape = true): array {
236
 
237
        foreach ($categories as $id => $category) {
238
            if ($category->parent == 0) {
239
                $context = context::instance_by_id($category->contextid);
240
                $categories[$id]->name = get_string('topfor', 'question', $context->get_context_name(false, false, $escape));
241
            }
242
        }
243
 
244
        return $categories;
245
    }
246
 
247
    /**
248
     * Format categories into an indented list reflecting the tree structure.
249
     *
250
     * @param array $categories An array of category objects, keyed by ID.
251
     * @param int $nochildrenof If the category with this ID is in the list, don't include its children.
252
     * @return array The formatted list of categories.
253
     */
254
    public function add_indented_names(array $categories, int $nochildrenof = -1): array {
255
 
256
        // Add an array to each category to hold the child category ids. This array
257
        // will be removed again by flatten_category_tree(). It should not be used
258
        // outside these two functions.
259
        foreach (array_keys($categories) as $id) {
260
            $categories[$id]->childids = [];
261
        }
262
 
263
        // Build the tree structure, and record which categories are top-level.
264
        // We have to be careful, because the categories array may include published
265
        // categories from other courses, but not their parents.
266
        $toplevelcategoryids = [];
267
        foreach (array_keys($categories) as $id) {
268
            if (
269
                !empty($categories[$id]->parent) &&
270
                array_key_exists($categories[$id]->parent, $categories)
271
            ) {
272
                $categories[$categories[$id]->parent]->childids[] = $id;
273
            } else {
274
                $toplevelcategoryids[] = $id;
275
            }
276
        }
277
 
278
        // Flatten the tree to and add the indents.
279
        $newcategories = [];
280
        foreach ($toplevelcategoryids as $id) {
281
            $newcategories = $newcategories + $this->flatten_category_tree(
282
                    $categories,
283
                    $id,
284
                    0,
285
                    $nochildrenof,
286
                );
287
        }
288
 
289
        return $newcategories;
290
    }
291
 
292
    /**
293
     * Only for the use of add_indented_names().
294
     *
295
     * Recursively adds an indentedname field to each category, starting with the category
296
     * with id $id, and dealing with that category and all its children, and
297
     * return a new array, with those categories in the right order.
298
     *
299
     * @param array $categories an array of categories which has had childids
300
     *          fields added by flatten_category_tree(). Passed by reference for
301
     *          performance only. It is not modfied.
302
     * @param int $id the category to start the indenting process from.
303
     * @param int $depth the indent depth. Used in recursive calls.
304
     * @param int $nochildrenof If the category with this ID is in the list, don't recur to its children.
305
     * @return array a new array of categories, in the right order for the tree.
306
     */
307
    public function flatten_category_tree(array &$categories, $id, int $depth = 0, int $nochildrenof = -1): array {
308
 
309
        // Indent the name of this category.
310
        $newcategories = [];
311
        $newcategories[$id] = $categories[$id];
312
        $newcategories[$id]->indentedname = str_repeat('&nbsp;&nbsp;&nbsp;', $depth) .
313
            $categories[$id]->name;
314
 
315
        // Recursively indent the children.
316
        foreach ($categories[$id]->childids as $childid) {
317
            [$childcategory, ] = explode(',', $childid);
318
            if ($childcategory != $nochildrenof) {
319
                $newcategories = $newcategories + self::flatten_category_tree(
320
                        $categories,
321
                        $childid,
322
                        $depth + 1,
323
                        $nochildrenof,
324
                    );
325
            }
326
        }
327
 
328
        // Remove the childids array that were temporarily added.
329
        unset($newcategories[$id]->childids);
330
 
331
        return $newcategories;
332
    }
333
 
334
    /**
335
     * Context for the question_category_selector.mustache template.
336
     *
337
     * @param renderer_base $output
338
     * @return array[] [
339
     *  'banks' => a 2-D array of question banks and categories, for a plain select list with optgroups.
340
     *  'categories' => A flat list of categories, with bank names and disabled entries, for enhancing with an Autocomplete.
341
     * ]
342
     */
343
    public function export_for_template(renderer_base $output): array {
344
        $categoriesarray = $this->question_category_options(
345
            $this->contexts,
346
            $this->top,
347
            $this->currentcat,
348
            false,
349
            $this->nochildrenof,
350
            false,
351
        );
352
 
353
        $bankoptgroups = [];
354
        $categoryoptions = [];
355
        if ($this->autocomplete) {
356
            foreach ($categoriesarray as $bankname => $categories) {
357
                $categoryoptions[] = [
358
                    'label' => $bankname,
359
                    'value' => 0,
360
                    'disabled' => true,
361
                ];
362
                foreach ($categories as $idcontext => $category) {
363
                    $categoryoptions[] = [
364
                        'label' => $category,
365
                        'value' => $idcontext,
366
                        'selected' => $this->selected == $idcontext,
367
                    ];
368
                }
369
            }
370
            if (empty($selected) && isset($categoryoptions[1])) {
371
                // Default to selecting the first category option.
372
                $categoryoptions[1]['selected'] = 1;
373
            }
374
        } else {
375
            foreach ($categoriesarray as $bankname => $categories) {
376
                $bankoptgroups[] = [
377
                    'bankname' => $bankname,
378
                    'categories' => array_map(
379
                        fn($idcontext, $category) => [
380
                            'idcontext' => $idcontext,
381
                            'category' => $category,
382
                            'selected' => $this->selected == $idcontext,
383
                        ],
384
                        array_keys($categories),
385
                        $categories,
386
                    ),
387
                ];
388
            }
389
            if (empty($selected) && isset($bankoptgroups[0]['categories'][0])) {
390
                // Default to selecting the first category option.
391
                $bankoptgroups[0]['categories'][0]['selected'] = 1;
392
            }
393
        }
394
 
395
        return [
396
            'banks' => $bankoptgroups,
397
            'categories' => $categoryoptions,
398
        ];
399
    }
400
}