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 qbank_managecategories;
18
 
19
use context;
20
use moodle_url;
21
 
22
/**
23
 * Builds a tree for categories for rendering the category management page.
24
 *
25
 * @package    qbank_managecategories
26
 * @copyright  2024 Catalyst IT Europe Ltd.
27
 * @author     Mark Johnson <mark.johnson@catalyst-eu.net>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class question_categories {
31
    /**
32
     * @var moodle_url Object representing url for this page
33
     */
34
    public moodle_url $pageurl;
35
 
36
    /**
37
     * @var ?int cmid.
38
     */
39
    public ?int $cmid;
40
 
41
    /**
42
     * @var ?int courseid.
43
     */
44
    public ?int $courseid;
45
 
46
    /**
47
     * @var ?int The context ID of the current page.
48
     */
49
    public ?int $contextid;
50
 
51
    /**
52
     * @var array An array containing a tree of categories for each context.
53
     */
54
    public array $editlists;
55
 
56
    /**
57
     * Constructor.
58
     *
59
     * @param moodle_url $pageurl base URL of the display categories page. Used for redirects.
60
     * @param context[] $contexts contexts where the current user can edit categories.
61
     * @param ?int $cmid course module id for the current page.
62
     * @param ?int $courseid course id for the current page.
63
     * @param ?int $thiscontext The context ID of the current page.
64
     */
65
    public function __construct(
66
        moodle_url $pageurl,
67
        array $contexts,
68
        ?int $cmid = null,
69
        ?int $courseid = null,
70
        ?int $thiscontext = null,
71
    ) {
72
        global $DB;
73
 
74
        $this->cmid = $cmid;
75
        $this->courseid = $courseid;
76
 
77
        $this->pageurl = $pageurl;
78
        $this->contextid = $thiscontext;
79
 
80
        $contextids = array_map(fn($context) => $context->id, $contexts);
81
        [$insql, $params] = $DB->get_in_or_equal($contextids);
82
        $topcategories = $DB->get_records_select_menu(
83
            'question_categories',
84
            'parent = 0 AND contextid ' . $insql,
85
            $params,
86
            fields: 'contextid, id'
87
        );
88
        foreach ($contexts as $context) {
89
            // We can only have categories in module context.
90
            if ($context->contextlevel !== CONTEXT_MODULE) {
91
                continue;
92
            }
93
            $items = helper::get_categories_for_contexts($context->id);
94
            // Create an ordered tree with children correctly nested under parents.
95
            foreach ($items as $item) {
96
                if (array_key_exists((int) $item->parent, $items)) {
97
                    $item->parentitem = $items[$item->parent];
98
                    $items[$item->parent]->children[$item->id] = $item;
99
                }
100
            }
101
            foreach ($items as $item) {
102
                if (isset($item->children)) {
103
                    foreach ($item->children as $children) {
104
                        unset($items[$children->id]);
105
                    }
106
                }
107
            }
108
            $this->editlists[$context->id] = (object) [
109
                'items' => $items,
110
                'context' => $context,
111
                'categoryid' => $topcategories[$context->id],
112
            ];
113
        }
114
    }
115
}