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 action_menu;
|
|
|
20 |
use action_menu_link;
|
|
|
21 |
use context;
|
|
|
22 |
use core\plugininfo\qbank;
|
|
|
23 |
use core_question\category_manager;
|
|
|
24 |
use moodle_url;
|
|
|
25 |
use pix_icon;
|
|
|
26 |
use qbank_managecategories\helper;
|
|
|
27 |
use renderable;
|
|
|
28 |
use renderer_base;
|
|
|
29 |
use stdClass;
|
|
|
30 |
use templatable;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Output component for a single category
|
|
|
34 |
*
|
|
|
35 |
* @package qbank_managecategories
|
|
|
36 |
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
|
|
|
37 |
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
*/
|
|
|
40 |
class category implements renderable, templatable {
|
|
|
41 |
/**
|
|
|
42 |
* @var bool Can this category be reordered?
|
|
|
43 |
*/
|
|
|
44 |
protected bool $canreorder;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Constructor
|
|
|
48 |
*
|
|
|
49 |
* @param stdClass $category The record of category we are rendering
|
|
|
50 |
* @param context $context The context the category belongs to.
|
|
|
51 |
* @param int $cmid The cmid of the course module context the category belongs to (optional).
|
|
|
52 |
* @param int $courseid The course ID of the course context the category belongs to (optional).
|
|
|
53 |
*/
|
|
|
54 |
public function __construct(
|
|
|
55 |
/** @var stdClass $category The record of category we are rendering */
|
|
|
56 |
protected stdClass $category,
|
|
|
57 |
/** @var context $context The context the category belongs to. */
|
|
|
58 |
protected context $context,
|
|
|
59 |
/** @var int $cmid The cmid of the course module context the category belongs to. */
|
|
|
60 |
protected int $cmid = 0,
|
|
|
61 |
/** @var int $courseid The course ID of the course context the category belongs to. */
|
|
|
62 |
protected int $courseid = 0,
|
|
|
63 |
) {
|
|
|
64 |
$manager = new category_manager();
|
|
|
65 |
$this->canreorder = !$manager->is_only_child_of_top_category_in_context($this->category->id);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Get the canreorder flag.
|
|
|
70 |
*
|
|
|
71 |
* @return bool
|
|
|
72 |
*/
|
|
|
73 |
public function get_canreorder(): bool {
|
|
|
74 |
return $this->canreorder;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Create the template data for a category, and call recursively for child categories.
|
|
|
79 |
*
|
|
|
80 |
* @param renderer_base $output
|
|
|
81 |
* @return array
|
|
|
82 |
*/
|
|
|
83 |
public function export_for_template(renderer_base $output): array {
|
|
|
84 |
global $PAGE;
|
|
|
85 |
$canmanagecategory = has_capability('moodle/question:managecategory', $this->context);
|
|
|
86 |
$params = $PAGE->url->params();
|
|
|
87 |
$cmid = $params['cmid'] ?? $this->cmid;
|
|
|
88 |
$courseid = $params['courseid'] ?? $this->courseid;
|
|
|
89 |
|
|
|
90 |
// Each section adds html to be displayed as part of this list item.
|
|
|
91 |
$questionbankurl = new moodle_url('/question/edit.php', $params);
|
|
|
92 |
$questionbankurl->param('cat', helper::combine_id_context($this->category));
|
|
|
93 |
$categoryname = format_string($this->category->name, true, ['context' => $this->context, 'escape' => false]);
|
|
|
94 |
$idnumber = null;
|
|
|
95 |
if ($this->category->idnumber !== null && $this->category->idnumber !== '') {
|
|
|
96 |
$idnumber = $this->category->idnumber;
|
|
|
97 |
}
|
|
|
98 |
$categorydesc = format_text(
|
|
|
99 |
$this->category->info,
|
|
|
100 |
$this->category->infoformat,
|
|
|
101 |
['context' => $this->context, 'noclean' => true],
|
|
|
102 |
);
|
|
|
103 |
$menu = new action_menu();
|
|
|
104 |
$menu->attributes['draggable'] = 'false';
|
|
|
105 |
$menu->set_kebab_trigger();
|
|
|
106 |
$menu->prioritise = true;
|
|
|
107 |
|
|
|
108 |
// Don't allow movement if only subcat.
|
|
|
109 |
if ($canmanagecategory) {
|
|
|
110 |
// This item display a modal for moving a category.
|
|
|
111 |
// Move category modal.
|
|
|
112 |
$menu->add(new action_menu_link(
|
|
|
113 |
new \moodle_url('#'),
|
|
|
114 |
new pix_icon(
|
|
|
115 |
't/move',
|
|
|
116 |
get_string('move'),
|
|
|
117 |
'moodle',
|
|
|
118 |
[
|
|
|
119 |
'class' => 'iconsmall',
|
|
|
120 |
]
|
|
|
121 |
),
|
|
|
122 |
get_string('move'),
|
|
|
123 |
false,
|
|
|
124 |
[
|
|
|
125 |
'class' => 'show-when-movable', // Don't allow moving when this is the only category in the context.
|
|
|
126 |
'data-categoryid' => $this->category->id,
|
|
|
127 |
'data-actiontype' => 'move',
|
|
|
128 |
'data-contextid' => (int) $this->category->contextid,
|
|
|
129 |
'data-categoryname' => $categoryname,
|
|
|
130 |
'title' => get_string('movecategory', 'qbank_managecategories', $categoryname),
|
|
|
131 |
]
|
|
|
132 |
));
|
|
|
133 |
|
|
|
134 |
$thiscontext = (int) $this->category->contextid;
|
|
|
135 |
$editurl = new moodle_url('#');
|
|
|
136 |
$menu->add(new action_menu_link(
|
|
|
137 |
$editurl,
|
|
|
138 |
new pix_icon('t/edit', 'edit'),
|
|
|
139 |
get_string('editsettings'),
|
|
|
140 |
false,
|
|
|
141 |
[
|
|
|
142 |
'data-action' => 'addeditcategory',
|
|
|
143 |
'data-actiontype' => 'edit',
|
|
|
144 |
'data-contextid' => $thiscontext,
|
|
|
145 |
'data-categoryid' => $this->category->id,
|
|
|
146 |
'data-cmid' => $cmid,
|
|
|
147 |
'data-courseid' => $courseid,
|
|
|
148 |
'data-questioncount' => $this->category->questioncount,
|
|
|
149 |
]
|
|
|
150 |
));
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
// Sets up export to XML link.
|
|
|
154 |
if (qbank::is_plugin_enabled('qbank_exportquestions')) {
|
|
|
155 |
$exporturl = new moodle_url(
|
|
|
156 |
'/question/bank/exportquestions/export.php',
|
|
|
157 |
['cat' => helper::combine_id_context($this->category)]
|
|
|
158 |
);
|
|
|
159 |
if ($courseid !== 0) {
|
|
|
160 |
$exporturl->param('courseid', $courseid);
|
|
|
161 |
} else {
|
|
|
162 |
$exporturl->param('cmid', $cmid);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
$menu->add(new action_menu_link(
|
|
|
166 |
$exporturl,
|
|
|
167 |
new pix_icon('t/download', 'download'),
|
|
|
168 |
get_string('exportasxml', 'question'),
|
|
|
169 |
false,
|
|
|
170 |
));
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
// The delete action which should be last.
|
|
|
174 |
if ($canmanagecategory) {
|
|
|
175 |
// Sets up delete link.
|
|
|
176 |
$deleteurl = new moodle_url(
|
|
|
177 |
'/question/bank/managecategories/category.php',
|
|
|
178 |
['delete' => $this->category->id, 'sesskey' => sesskey()]
|
|
|
179 |
);
|
|
|
180 |
if ($courseid !== 0) {
|
|
|
181 |
$deleteurl->param('courseid', $courseid);
|
|
|
182 |
} else {
|
|
|
183 |
$deleteurl->param('cmid', $cmid);
|
|
|
184 |
}
|
|
|
185 |
$menu->add(new action_menu_link(
|
|
|
186 |
$deleteurl,
|
|
|
187 |
new pix_icon('t/delete', 'delete'),
|
|
|
188 |
get_string('delete'),
|
|
|
189 |
false,
|
|
|
190 |
[
|
|
|
191 |
'class' => 'text-danger show-when-movable', // Don't allow deletion when this is the only category in context.
|
|
|
192 |
'data-confirmation' => 'modal',
|
|
|
193 |
'data-confirmation-type' => 'delete',
|
|
|
194 |
'data-confirmation-title-str' => json_encode(['delete', 'core']),
|
|
|
195 |
'data-confirmation-yes-button-str' => json_encode(['delete', 'core']),
|
|
|
196 |
'data-confirmation-content-str' => json_encode([
|
|
|
197 |
'confirmdelete',
|
|
|
198 |
'qbank_managecategories',
|
|
|
199 |
$this->category->name,
|
|
|
200 |
]),
|
|
|
201 |
],
|
|
|
202 |
));
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
$children = [];
|
|
|
206 |
if (!empty($this->category->children)) {
|
|
|
207 |
foreach ($this->category->children as $child) {
|
|
|
208 |
$childcategory = new category($child, $this->context);
|
|
|
209 |
$children[] = $childcategory->export_for_template($output);
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
$itemdata = [
|
|
|
213 |
'categoryid' => $this->category->id,
|
|
|
214 |
'contextid' => $this->category->contextid,
|
|
|
215 |
'questionbankurl' => $questionbankurl->out(false),
|
|
|
216 |
'categoryname' => $categoryname,
|
|
|
217 |
'idnumber' => $idnumber,
|
|
|
218 |
'questioncount' => $this->category->questioncount,
|
|
|
219 |
'categorydesc' => $categorydesc,
|
|
|
220 |
'editactionmenu' => $menu->export_for_template($output),
|
|
|
221 |
'draghandle' => $canmanagecategory && $this->canreorder,
|
|
|
222 |
'haschildren' => !empty($children),
|
|
|
223 |
'children' => $children,
|
|
|
224 |
'parent' => $this->category->parent,
|
|
|
225 |
'sortorder' => $this->category->sortorder,
|
|
|
226 |
'newchildtooltip' => get_string('newchild', 'qbank_managecategories', $categoryname),
|
|
|
227 |
];
|
|
|
228 |
return $itemdata;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
}
|