ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilRatingCategoryGUI.php
Go to the documentation of this file.
1<?php
2
19use ILIAS\UI\Factory as UIFactory;
20use ILIAS\UI\Renderer as UIRenderer;
21use ILIAS\Data\Factory as DataFactory;
22use Psr\Http\Message\RequestInterface;
23use ILIAS\Refinery\Factory as Refinery;
24use ILIAS\HTTP\Services as HttpServices;
26
33{
34 protected readonly ilLanguage $lng;
35 private readonly UIFactory $ui_factory;
36 private readonly UIRenderer $ui_renderer;
37 private readonly Refinery $refinery;
38 private readonly HttpServices $http;
39 protected ilCtrl $ctrl;
42 protected int $parent_id; // [int]
43 protected $export_callback; // [string|array]
44 protected ?string $export_subobj_title = null;
45 protected int $requested_cat_id;
46 protected RequestInterface $request;
47 protected int $cat_id;
48
54 public function __construct(
55 int $a_parent_id,
56 $a_export_callback = null,
57 ?string $a_export_subobj_title = null
58 ) {
59 global $DIC;
60
61 $this->tpl = $DIC["tpl"];
62 $this->http = $DIC->http();
63 $this->ctrl = $DIC->ctrl();
64 $this->lng = $DIC->language();
65 $this->toolbar = $DIC->toolbar();
66 $this->refinery = $DIC->refinery();
67 $this->request = $DIC->http()->request();
68 $this->ui_factory = $DIC->ui()->factory();
69 $this->ui_renderer = $DIC->ui()->renderer();
70
71 $lng = $DIC->language();
72
73 $this->parent_id = $a_parent_id;
74 $this->export_callback = $a_export_callback;
75 $this->export_subobj_title = $a_export_subobj_title;
76
77 $lng->loadLanguageModule("rating");
78
79 $params = $this->request->getQueryParams();
80 $body = $this->request->getParsedBody();
81 $this->requested_cat_id = (int) ($body["cat_id"] ?? ($params["cat_id"] ?? 0));
82
83 if ($this->requested_cat_id) {
84 $cat = new ilRatingCategory($this->requested_cat_id);
85 if ($cat->getParentId() === $this->parent_id) {
86 $this->cat_id = $cat->getId();
87 }
88 }
89 }
90
94 public function executeCommand(): void
95 {
96 $ilCtrl = $this->ctrl;
97
98 $next_class = $ilCtrl->getNextClass($this);
99 $cmd = $ilCtrl->getCmd('listCategories');
100
101 switch ($next_class) {
102 default:
103 $this->$cmd();
104 break;
105 }
106 }
107
108 public function handleTableActions(): void
109 {
110 $cmd = $this->http->wrapper()->query()->retrieve(
111 'rating_category_ordering_table_action',
112 $this->refinery->byTrying(
113 [
114 $this->refinery->kindlyTo()->string(),
115 $this->refinery->always('')
116 ]
117 )
118 );
119
120 if ($cmd !== '') {
121 $this->$cmd();
122 }
123 }
124
125 protected function listCategories(): void
126 {
128 $ilToolbar = $this->toolbar;
130 $ilCtrl = $this->ctrl;
131
132 $ilToolbar->addButton(
133 $lng->txt("rating_add_category"),
134 $ilCtrl->getLinkTarget($this, "add")
135 );
136
137 $ilToolbar->addSeparator();
138
139 $ilToolbar->addButton(
140 $lng->txt("export"),
141 $ilCtrl->getLinkTarget($this, "export")
142 );
143
144 $tpl->setContent($this->ui_renderer->render($this->getRatingCategoryOrderingTable()->getComponent()));
145 }
146
148 {
150 $this->parent_id,
151 $this->ui_factory,
152 $this->lng,
153 $this->ctrl,
154 new DataFactory(),
155 $this->http->request()
156 )
157 ;
158 }
159
160 protected function initCategoryForm(?int $a_id = null): ilPropertyFormGUI
161 {
163 $ilCtrl = $this->ctrl;
164
165 $form = new ilPropertyFormGUI();
166 $form->setTarget("_top");
167 $form->setFormAction($ilCtrl->getFormAction($this, "save"));
168 $form->setTitle($lng->txt("rating_category_" . ($a_id ? "edit" : "create")));
169
170 // title
171 $ti = new ilTextInputGUI($lng->txt("title"), "title");
172 $ti->setMaxLength(128);
173 $ti->setSize(40);
174 $ti->setRequired(true);
175 $form->addItem($ti);
176
177 // description
178 $ta = new ilTextAreaInputGUI($lng->txt("description"), "desc");
179 $ta->setCols(40);
180 $ta->setRows(2);
181 $ta->setMaxNumOfChars(ilObject::LONG_DESC_LENGTH);
182 $form->addItem($ta);
183
184 if (!$a_id) {
185 $form->addCommandButton("save", $lng->txt("rating_category_add"));
186 } else {
187 $cat = new ilRatingCategory($a_id);
188 $ti->setValue($cat->getTitle());
189 $ta->setValue($cat->getDescription());
190
191 $form->addCommandButton("update", $lng->txt("rating_category_update"));
192 }
193 $form->addCommandButton("listCategories", $lng->txt("cancel"));
194
195 return $form;
196 }
197
198 protected function add(?ilPropertyFormGUI $a_form = null): void
199 {
201
202 if (!$a_form) {
203 $a_form = $this->initCategoryForm();
204 }
205
206 $tpl->setContent($a_form->getHTML());
207 }
208
209 protected function save(): void
210 {
211 $ilCtrl = $this->ctrl;
213
214 $form = $this->initCategoryForm();
215 if ($form->checkInput()) {
216 $cat = new ilRatingCategory();
217 $cat->setParentId($this->parent_id);
218 $cat->setTitle($form->getInput("title"));
219 $cat->setDescription($form->getInput("desc"));
220 $cat->save();
221
222 $this->tpl->setOnScreenMessage('success', $lng->txt("rating_category_created"));
223 $ilCtrl->redirect($this, "listCategories");
224 }
225
226 $form->setValuesByPost();
227 $this->add($form);
228 }
229
230 protected function edit(?ilPropertyFormGUI $a_form = null): void
231 {
233 $ilCtrl = $this->ctrl;
234
235 $cat_id = $this->http->wrapper()->query()->has('rating_category_ordering_rating_ids') ?
236 $this->http->wrapper()->query()->retrieve(
237 'rating_category_ordering_rating_ids',
238 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
239 ) : 0;
240 $cat_id = $cat_id[0] ?? 0;
241
242 $ilCtrl->setParameter($this, 'cat_id', $cat_id);
243
244 if (!$a_form) {
245 $a_form = $this->initCategoryForm($cat_id);
246 }
247
248 $tpl->setContent($a_form->getHTML());
249 }
250
251 protected function update(): void
252 {
253 $ilCtrl = $this->ctrl;
255
256 $form = $this->initCategoryForm($this->cat_id);
257 if ($form->checkInput()) {
258 $cat = new ilRatingCategory($this->cat_id);
259 $cat->setTitle($form->getInput("title"));
260 $cat->setDescription($form->getInput("desc"));
261 $cat->update();
262
263 $this->tpl->setOnScreenMessage('success', $lng->txt("rating_category_updated"));
264 $ilCtrl->redirect($this, "listCategories");
265 }
266
267 $form->setValuesByPost();
268 $this->add($form);
269 }
270
271 protected function updateOrder(): void
272 {
273 $ilCtrl = $this->ctrl;
275
276 $table = $this->getRatingCategoryOrderingTable();
277 $data = $table->getComponent()->getData();
278
279 $cnt = 0;
280 foreach ($data as $id) {
281 $cat = new ilRatingCategory($id);
282 if ($cat->getParentId() === $this->parent_id) {
283 $cnt += 10;
284 $cat->setPosition($cnt);
285 $cat->update();
286 }
287 }
288
289 $this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
290 $ilCtrl->redirect($this, 'listCategories');
291 }
292
293 protected function confirmDelete(): void
294 {
296 $ilCtrl = $this->ctrl;
298
299 $cat_id = $this->http->wrapper()->query()->has('rating_category_ordering_rating_ids') ?
300 $this->http->wrapper()->query()->retrieve(
301 'rating_category_ordering_rating_ids',
302 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
303 ) : 0;
304 $cat_id = $cat_id[0] ?? 0;
305
306 if (!$cat_id) {
307 $this->listCategories();
308
309 return;
310 }
311
312 $cgui = new ilConfirmationGUI();
313 $cgui->setHeaderText($lng->txt('rating_category_delete_sure') . '<br/>' .
314 $lng->txt('info_delete_warning_no_trash'));
315
316 $cgui->setFormAction($ilCtrl->getFormAction($this));
317 $cgui->setCancel($lng->txt('cancel'), 'listCategories');
318 $cgui->setConfirm($lng->txt('confirm'), 'delete');
319
320 $cat = new ilRatingCategory($cat_id);
321 $cgui->addItem('cat_id', $cat_id, $cat->getTitle());
322
323 $tpl->setContent($cgui->getHTML());
324 }
325
326 protected function delete(): void
327 {
328 $ilCtrl = $this->ctrl;
330
331 if ($this->cat_id) {
332 ilRatingCategory::delete($this->cat_id);
333 $this->tpl->setOnScreenMessage('success', $lng->txt("rating_category_deleted"), true);
334 }
335
336 // fix order
337 $cnt = 0;
338 foreach (ilRatingCategory::getAllForObject($this->parent_id) as $item) {
339 $cnt += 10;
340
341 $cat = new ilRatingCategory($item["id"]);
342 $cat->setPosition($cnt);
343 $cat->update();
344 }
345
346 $ilCtrl->redirect($this, "listCategories");
347 }
348
349 protected function export(): void
350 {
352
353 $excel = new ilExcel();
354 $excel->addSheet($lng->txt("rating_categories"));
355
356 // restrict to currently active (probably not needed - see delete())
357 $active = array();
358 foreach (ilRatingCategory::getAllForObject($this->parent_id) as $item) {
359 $active[$item["id"]] = $item["title"];
360 }
361
362 // title row
363 $row = 1;
364 $excel->setCell($row, 0, $this->export_subobj_title . " (" . $lng->txt("id") . ")");
365 $excel->setCell($row, 1, $this->export_subobj_title);
366 $excel->setCell($row, 2, $lng->txt("rating_export_category") . " (" . $lng->txt("id") . ")");
367 $excel->setCell($row, 3, $lng->txt("rating_export_category"));
368 $excel->setCell($row, 4, $lng->txt("rating_export_date"));
369 $excel->setCell($row, 5, $lng->txt("rating_export_rating"));
370 $excel->setBold("A1:F1");
371
372 // content rows
373 foreach (ilRating::getExportData($this->parent_id, ilObject::_lookupType($this->parent_id), array_keys($active)) as $item) {
374 // overall rating?
375 if (!$item["sub_obj_id"]) {
376 continue;
377 }
378
379 $row++;
380
381 $sub_obj_title = $item["sub_obj_type"];
382 if ($this->export_callback) {
383 $sub_obj_title = call_user_func($this->export_callback, $item["sub_obj_id"], $item["sub_obj_type"]);
384 }
385
386 $excel->setCell($row, 0, (int) $item["sub_obj_id"]);
387 $excel->setCell($row, 1, $sub_obj_title);
388 $excel->setCell($row, 2, (int) $item["category_id"]);
389 $excel->setCell($row, 3, $active[$item["category_id"]] ?? "");
390 $excel->setCell($row, 4, new ilDateTime($item["tstamp"] ?? null, IL_CAL_UNIX));
391 $excel->setCell($row, 5, $item["rating"] ?? "");
392 }
393
394 $excel->sendToClient(ilObject::_lookupTitle($this->parent_id));
395 }
396
397 public function getUnsafeGetCommands(): array
398 {
399 return ['handleTableActions'];
400 }
401
402 public function getSafePostCommands(): array
403 {
404 return [];
405 }
406}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
const IL_CAL_UNIX
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilCtrl provides processing control methods.
getNextClass($a_gui_class=null)
@inheritDoc
@classDescription Date and time handling
language handling
loadLanguageModule(string $a_module)
Load language module.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static _lookupType(int $id, bool $reference=false)
const LONG_DESC_LENGTH
static _lookupTitle(int $obj_id)
This class represents a property form user interface.
Class ilRatingCategoryGUI.
getSafePostCommands()
This method must return a list of safe POST commands.
add(?ilPropertyFormGUI $a_form=null)
readonly HttpServices $http
readonly UIRenderer $ui_renderer
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
readonly UIFactory $ui_factory
__construct(int $a_parent_id, $a_export_callback=null, ?string $a_export_subobj_title=null)
ilRatingCategoryGUI constructor.
ilGlobalTemplateInterface $tpl
edit(?ilPropertyFormGUI $a_form=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getAllForObject(int $a_parent_obj_id)
static delete(int $a_id)
static getExportData(int $a_obj_id, string $a_obj_type, ?array $a_category_ids=null)
Get export data.
This class represents a text area property in a property form.
This class represents a text property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setContent(string $a_html)
Sets content for standard template.
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26