ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilRatingCategoryGUI.php
Go to the documentation of this file.
1 <?php
2 
20 
27 {
28  protected ilLanguage $lng;
29  protected ilCtrl $ctrl;
32  protected int $parent_id; // [int]
33  protected $export_callback; // [string|array]
34  protected ?string $export_subobj_title = null;
35  protected int $requested_cat_id;
37  protected int $cat_id;
38 
45  public function __construct(
46  int $a_parent_id,
47  $a_export_callback = null,
48  string $a_export_subobj_title = null
49  ) {
50  global $DIC;
51 
52  $this->lng = $DIC->language();
53  $this->ctrl = $DIC->ctrl();
54  $this->tpl = $DIC["tpl"];
55  $this->toolbar = $DIC->toolbar();
56  $this->request = $DIC->http()->request();
57  $lng = $DIC->language();
58 
59  $this->parent_id = $a_parent_id;
60  $this->export_callback = $a_export_callback;
61  $this->export_subobj_title = $a_export_subobj_title;
62 
63  $lng->loadLanguageModule("rating");
64 
65  $params = $this->request->getQueryParams();
66  $body = $this->request->getParsedBody();
67  $this->requested_cat_id = (int) ($body["cat_id"] ?? ($params["cat_id"] ?? 0));
68 
69  if ($this->requested_cat_id) {
70  $cat = new ilRatingCategory($this->requested_cat_id);
71  if ($cat->getParentId() == $this->parent_id) {
72  $this->cat_id = $cat->getId();
73  }
74  }
75  }
76 
80  public function executeCommand(): void
81  {
82  $ilCtrl = $this->ctrl;
83 
84  $next_class = $ilCtrl->getNextClass($this);
85  $cmd = $ilCtrl->getCmd("listCategories");
86 
87  switch ($next_class) {
88  default:
89  $this->$cmd();
90  break;
91  }
92  }
93 
94  protected function listCategories(): void
95  {
96  $tpl = $this->tpl;
97  $ilToolbar = $this->toolbar;
98  $lng = $this->lng;
99  $ilCtrl = $this->ctrl;
100 
101  $ilToolbar->addButton(
102  $lng->txt("rating_add_category"),
103  $ilCtrl->getLinkTarget($this, "add")
104  );
105 
106  $ilToolbar->addSeparator();
107 
108  $ilToolbar->addButton(
109  $lng->txt("export"),
110  $ilCtrl->getLinkTarget($this, "export")
111  );
112 
113  $table = new ilRatingCategoryTableGUI($this, "listCategories", $this->parent_id);
114  $tpl->setContent($table->getHTML());
115  }
116 
117 
118  protected function initCategoryForm(int $a_id = null): ilPropertyFormGUI
119  {
120  $lng = $this->lng;
121  $ilCtrl = $this->ctrl;
122 
123  $form = new ilPropertyFormGUI();
124  $form->setTarget("_top");
125  $form->setFormAction($ilCtrl->getFormAction($this, "save"));
126  $form->setTitle($lng->txt("rating_category_" . ($a_id ? "edit" : "create")));
127 
128  // title
129  $ti = new ilTextInputGUI($lng->txt("title"), "title");
130  $ti->setMaxLength(128);
131  $ti->setSize(40);
132  $ti->setRequired(true);
133  $form->addItem($ti);
134 
135  // description
136  $ta = new ilTextAreaInputGUI($lng->txt("description"), "desc");
137  $ta->setCols(40);
138  $ta->setRows(2);
139  $ta->setMaxNumOfChars(ilObject::LONG_DESC_LENGTH);
140  $form->addItem($ta);
141 
142  if (!$a_id) {
143  $form->addCommandButton("save", $lng->txt("rating_category_add"));
144  } else {
145  $cat = new ilRatingCategory($a_id);
146  $ti->setValue($cat->getTitle());
147  $ta->setValue($cat->getDescription());
148 
149  $form->addCommandButton("update", $lng->txt("rating_category_update"));
150  }
151  $form->addCommandButton("listCategories", $lng->txt("cancel"));
152 
153  return $form;
154  }
155 
156  protected function add(ilPropertyFormGUI $a_form = null): void
157  {
158  $tpl = $this->tpl;
159 
160  if (!$a_form) {
161  $a_form = $this->initCategoryForm();
162  }
163 
164  $tpl->setContent($a_form->getHTML());
165  }
166 
167  protected function save(): void
168  {
169  $ilCtrl = $this->ctrl;
170  $lng = $this->lng;
171 
172  $form = $this->initCategoryForm();
173  if ($form->checkInput()) {
174  $cat = new ilRatingCategory();
175  $cat->setParentId($this->parent_id);
176  $cat->setTitle($form->getInput("title"));
177  $cat->setDescription($form->getInput("desc"));
178  $cat->save();
179 
180  $this->tpl->setOnScreenMessage('success', $lng->txt("rating_category_created"));
181  $ilCtrl->redirect($this, "listCategories");
182  }
183 
184  $form->setValuesByPost();
185  $this->add($form);
186  }
187 
188  protected function edit(ilPropertyFormGUI $a_form = null): void
189  {
190  $tpl = $this->tpl;
191  $ilCtrl = $this->ctrl;
192 
193  $ilCtrl->setParameter($this, "cat_id", $this->cat_id);
194 
195  if (!$a_form) {
196  $a_form = $this->initCategoryForm($this->cat_id);
197  }
198 
199  $tpl->setContent($a_form->getHTML());
200  }
201 
202  protected function update(): void
203  {
204  $ilCtrl = $this->ctrl;
205  $lng = $this->lng;
206 
207  $form = $this->initCategoryForm($this->cat_id);
208  if ($form->checkInput()) {
209  $cat = new ilRatingCategory($this->cat_id);
210  $cat->setTitle($form->getInput("title"));
211  $cat->setDescription($form->getInput("desc"));
212  $cat->update();
213 
214  $this->tpl->setOnScreenMessage('success', $lng->txt("rating_category_updated"));
215  $ilCtrl->redirect($this, "listCategories");
216  }
217 
218  $form->setValuesByPost();
219  $this->add($form);
220  }
221 
222  protected function updateOrder(): void
223  {
224  $ilCtrl = $this->ctrl;
225  $lng = $this->lng;
226 
227  $body = $this->request->getParsedBody();
228  $order = $body["pos"];
229  asort($order);
230 
231  $cnt = 0;
232  foreach ($order as $id => $pos) {
233  $cat = new ilRatingCategory($id);
234  if ($cat->getParentId() == $this->parent_id) {
235  $cnt += 10;
236  $cat->setPosition($cnt);
237  $cat->update();
238  }
239  }
240 
241  $this->tpl->setOnScreenMessage('success', $lng->txt("settings_saved"), true);
242  $ilCtrl->redirect($this, "listCategories");
243  }
244 
245  protected function confirmDelete(): void
246  {
247  $tpl = $this->tpl;
248  $ilCtrl = $this->ctrl;
249  $lng = $this->lng;
250 
251  if (!$this->cat_id) {
252  $this->listCategories();
253  return;
254  }
255 
256  $cgui = new ilConfirmationGUI();
257  $cgui->setHeaderText($lng->txt("rating_category_delete_sure") . "<br/>" .
258  $lng->txt("info_delete_warning_no_trash"));
259 
260  $cgui->setFormAction($ilCtrl->getFormAction($this));
261  $cgui->setCancel($lng->txt("cancel"), "listCategories");
262  $cgui->setConfirm($lng->txt("confirm"), "delete");
263 
264  $cat = new ilRatingCategory($this->cat_id);
265  $cgui->addItem("cat_id", $this->cat_id, $cat->getTitle());
266 
267  $tpl->setContent($cgui->getHTML());
268  }
269 
270  protected function delete(): void
271  {
272  $ilCtrl = $this->ctrl;
273  $lng = $this->lng;
274 
275  if ($this->cat_id) {
276  ilRatingCategory::delete($this->cat_id);
277  $this->tpl->setOnScreenMessage('success', $lng->txt("rating_category_deleted"), true);
278  }
279 
280  // fix order
281  $cnt = 0;
282  foreach (ilRatingCategory::getAllForObject($this->parent_id) as $item) {
283  $cnt += 10;
284 
285  $cat = new ilRatingCategory($item["id"]);
286  $cat->setPosition($cnt);
287  $cat->update();
288  }
289 
290  $ilCtrl->redirect($this, "listCategories");
291  }
292 
293  protected function export(): void
294  {
295  $lng = $this->lng;
296 
297  $excel = new ilExcel();
298  $excel->addSheet($lng->txt("rating_categories"));
299 
300  // restrict to currently active (probably not needed - see delete())
301  $active = array();
302  foreach (ilRatingCategory::getAllForObject($this->parent_id) as $item) {
303  $active[$item["id"]] = $item["title"];
304  }
305 
306  // title row
307  $row = 1;
308  $excel->setCell($row, 0, $this->export_subobj_title . " (" . $lng->txt("id") . ")");
309  $excel->setCell($row, 1, $this->export_subobj_title);
310  $excel->setCell($row, 2, $lng->txt("rating_export_category") . " (" . $lng->txt("id") . ")");
311  $excel->setCell($row, 3, $lng->txt("rating_export_category"));
312  $excel->setCell($row, 4, $lng->txt("rating_export_date"));
313  $excel->setCell($row, 5, $lng->txt("rating_export_rating"));
314  $excel->setBold("A1:F1");
315 
316  // content rows
317  foreach (ilRating::getExportData($this->parent_id, ilObject::_lookupType($this->parent_id), array_keys($active)) as $item) {
318  // overall rating?
319  if (!$item["sub_obj_id"]) {
320  continue;
321  }
322 
323  $row++;
324 
325  $sub_obj_title = $item["sub_obj_type"];
326  if ($this->export_callback) {
327  $sub_obj_title = call_user_func($this->export_callback, $item["sub_obj_id"], $item["sub_obj_type"]);
328  }
329 
330  $excel->setCell($row, 0, (int) $item["sub_obj_id"]);
331  $excel->setCell($row, 1, $sub_obj_title);
332  $excel->setCell($row, 2, (int) $item["category_id"]);
333  $excel->setCell($row, 3, $active[$item["category_id"]] ?? "");
334  $excel->setCell($row, 4, new ilDateTime($item["tstamp"] ?? null, IL_CAL_UNIX));
335  $excel->setCell($row, 5, $item["rating"] ?? "");
336  }
337 
338  $excel->sendToClient(ilObject::_lookupTitle($this->parent_id));
339  }
340 }
executeCommand()
execute command
static delete(int $a_id)
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...
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
static getExportData(int $a_obj_id, string $a_obj_type, array $a_category_ids=null)
Get export data.
loadLanguageModule(string $a_module)
Load language module.
edit(ilPropertyFormGUI $a_form=null)
const IL_CAL_UNIX
const LONG_DESC_LENGTH
global $DIC
Definition: feed.php:28
ilGlobalTemplateInterface $tpl
getNextClass($a_gui_class=null)
Class ilRatingCategoryGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupTitle(int $obj_id)
setContent(string $a_html)
Sets content for standard template.
static getAllForObject(int $a_parent_obj_id)
add(ilPropertyFormGUI $a_form=null)
__construct(int $a_parent_id, $a_export_callback=null, string $a_export_subobj_title=null)
ilRatingCategoryGUI constructor.
This class represents a text area property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setParameter(object $a_gui_obj, string $a_parameter, $a_value)
static _lookupType(int $id, bool $reference=false)