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\output;
18
 
19
use context;
20
use qbank_managecategories\question_categories;
21
use renderable;
22
use renderer_base;
23
use templatable;
24
 
25
/**
26
 * Output component for the Manage category page.
27
 *
28
 * This will return the template context for a page containing a tree of categories for each context in the provided
29
 * question_categories object, with editing controls.
30
 *
31
 * @package   qbank_managecategories
32
 * @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
33
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class categories implements renderable, templatable {
37
    /**
38
     * Constructor.
39
     *
40
     * @param question_categories $categories Question categories for display.
41
     */
42
    public function __construct(
43
        /** @var question_categories $categories Question categories for display. */
44
        protected question_categories $categories,
45
    ) {
46
    }
47
 
48
    #[\Override]
49
    public function export_for_template(renderer_base $output): array {
50
        $categories = [];
51
        foreach ($this->categories->editlists as $contextid => $list) {
52
            // Get list elements.
53
            $context = context::instance_by_id($contextid);
54
            $itemstab = [];
55
            if (count($list->items)) {
56
                foreach ($list->items as $item) {
57
                    $category = new category($item, $context);
58
                    $itemstab['items'][] = $category->export_for_template($output);
59
                }
60
            }
61
            if (isset($itemstab['items'])) {
62
                $ctxlvl = "contextlevel" . $list->context->contextlevel;
63
                $contextname = $list->context->get_context_name();
64
                $heading = get_string('questioncatsfor', 'question', $contextname);
65
 
66
                // Get categories context.
67
                $categories[] = [
68
                    'ctxlvl' => $ctxlvl,
69
                    'contextid' => $list->context->id,
70
                    'contextname' => $contextname,
71
                    'heading' => $heading,
72
                    'items' => $itemstab['items'],
73
                    'categoryid' => $list->categoryid,
74
                ];
75
            }
76
        }
77
        $data = [
78
            'categoriesrendered' => $categories,
79
            'contextid' => $this->categories->contextid,
80
            'cmid' => $this->categories->cmid,
81
            'courseid' => $this->categories->courseid,
82
            'showdescriptions' => get_user_preferences('qbank_managecategories_showdescriptions'),
83
        ];
84
        return $data;
85
    }
86
}