ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilObjCourseGroupingGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=0);
20 
25 use ILIAS\Course\Grouping\Table\GroupingHandler as GroupingTableHandler;
26 use ILIAS\Course\Grouping\Table\AssignmentHandler as AssignmentTableHandler;
27 
33 {
35  private int $id;
37  private string $content_type = '';
38 
41  protected ilLanguage $lng;
44  protected ilTabsGUI $tabs;
49 
50  protected GroupingTableHandler $grouping_table_handler;
51  protected AssignmentTableHandler $assignment_table_handler;
52 
53  public function __construct(ilObject $content_obj, int $a_obj_id = 0)
54  {
55  global $DIC;
56 
57  $this->tpl = $DIC->ui()->mainTemplate();
58  $this->ctrl = $DIC->ctrl();
59  $this->lng = $DIC->language();
60  $this->access = $DIC->access();
61  $this->error = $DIC['ilErr'];
62  $this->tabs = $DIC->tabs();
63  $this->toolbar = $DIC->toolbar();
64  $this->http = $DIC->http();
65  $this->ui_renderer = $DIC->ui()->renderer();
66  $this->refinery = $DIC->refinery();
67 
68  $this->content_obj = $content_obj;
69  $this->content_type = ilObject::_lookupType($this->content_obj->getId());
70 
71  $this->id = $a_obj_id;
72  $this->ctrl->saveParameter($this, 'obj_id');
73  $this->__initGroupingObject();
74 
75  $data_factory = new DataFactory();
76  $this->grouping_table_handler = new GroupingTableHandler(
77  $this->ctrl->getLinkTarget($this, 'handleGroupingTableAction'),
78  $this->content_obj->getId(),
79  $this->lng,
80  $DIC->ui()->factory(),
81  $data_factory,
84  $DIC['static_url']
85  );
86  $this->assignment_table_handler = new AssignmentTableHandler(
87  $this->ctrl->getLinkTarget($this, 'handleAssignmentTableAction'),
88  $this->content_obj->getId(),
90  $this->lng,
91  $DIC->ui()->factory(),
92  $data_factory,
95  $DIC->user(),
96  $DIC->repositoryTree()
97  );
98  }
99 
100  public function executeCommand(): void
101  {
102  $this->tabs->setTabActive('crs_groupings');
103  $cmd = $this->ctrl->getCmd();
104  if (!$cmd = $this->ctrl->getCmd()) {
105  $cmd = "edit";
106  }
107  $this->$cmd();
108  }
109 
110  public function __initGroupingObject(): void
111  {
112  $this->grp_obj = new ilObjCourseGrouping($this->id);
113  }
114 
115  public function getContentType(): string
116  {
117  return $this->content_type;
118  }
119 
120  public function listGroupings(): void
121  {
122  if (!$this->access->checkAccess('write', '', $this->content_obj->getRefId())) {
123  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
124  }
125 
126  $this->toolbar->addButton(
127  $this->lng->txt('crs_add_grouping'),
128  $this->ctrl->getLinkTarget($this, 'create')
129  );
130 
131  $table = $this->grouping_table_handler->getTable();
132  $this->tpl->setContent($this->ui_renderer->render($table));
133  }
134 
135  public function handleGroupingTableAction(): void
136  {
137  switch ($this->grouping_table_handler->getSelectedTableAction()) {
138  case GroupingTableHandler::ACTION_EDIT:
139  $selected = $this->grouping_table_handler->getSelectedGroupingIDs()[0] ?? 0;
140  $this->ctrl->setParameter($this, 'obj_id', $selected);
141  $this->ctrl->redirect($this, 'edit');
142  break;
143 
144  case GroupingTableHandler::ACTION_DELETE:
145  $this->askDeleteGrouping(...$this->grouping_table_handler->getSelectedGroupingIDs());
146  break;
147  }
148  }
149 
150  public function askDeleteGrouping(int ...$grouping_ids): void
151  {
152  if (!$this->access->checkAccess('write', '', $this->content_obj->getRefId())) {
153  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
154  }
155 
156  if (!count($grouping_ids)) {
157  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('crs_grouping_select_one'));
158  $this->listGroupings();
159  return;
160  }
161 
162  // display confirmation message
163  $cgui = new ilConfirmationGUI();
164  $cgui->setFormAction($this->ctrl->getFormAction($this));
165  $cgui->setHeaderText($this->lng->txt("crs_grouping_delete_sure"));
166  $cgui->setCancel($this->lng->txt("cancel"), "listGroupings");
167  $cgui->setConfirm($this->lng->txt("delete"), "deleteGrouping");
168 
169  // list objects that should be deleted
170  foreach ($grouping_ids as $grouping_id) {
171  $tmp_obj = new ilObjCourseGrouping($grouping_id);
172  $cgui->addItem("grouping[]", $grouping_id, $tmp_obj->getTitle());
173  }
174  $this->tpl->setContent($cgui->getHTML());
175  }
176 
177  public function deleteGrouping(): void
178  {
179  if (!$this->access->checkAccess('write', '', $this->content_obj->getRefId())) {
180  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
181  }
182  $grouping = [];
183  if ($this->http->wrapper()->post()->has('grouping')) {
184  $grouping = $this->http->wrapper()->post()->retrieve(
185  'grouping',
186  $this->refinery->kindlyTo()->listOf(
187  $this->refinery->kindlyTo()->int()
188  )
189  );
190  }
191 
192  foreach ($grouping as $grouping_id) {
193  $tmp_obj = new ilObjCourseGrouping((int) $grouping_id);
194  $tmp_obj->delete();
195  }
196 
197  $this->tpl->setOnScreenMessage('success', $this->lng->txt('crs_grouping_deleted'), true);
198  $this->ctrl->redirect($this, 'listGroupings');
199  }
200 
201  public function create(?ilPropertyFormGUI $a_form = null): void
202  {
203  if (!$this->access->checkAccess('write', '', $this->content_obj->getRefId())) {
204  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
205  }
206 
207  if (!$a_form) {
208  $a_form = $this->initForm(true);
209  }
210 
211  $this->tpl->setContent($a_form->getHTML());
212  }
213 
214  public function initForm(bool $a_create): ilPropertyFormGUI
215  {
216  $form = new ilPropertyFormGUI();
217  $form->setFormAction($this->ctrl->getFormAction($this));
218 
219  $title = new ilTextInputGUI($this->lng->txt('title'), 'title');
220  $title->setRequired(true);
221  $form->addItem($title);
222 
223  $desc = new ilTextAreaInputGUI($this->lng->txt('description'), 'description');
224  $form->addItem($desc);
225 
226  $options = array('login' => 'login',
227  'email' => 'email',
228  'matriculation' => 'matriculation'
229  );
230 
231  foreach ($options as $value => $caption) {
232  $options[$value] = $this->lng->txt($caption);
233  }
234  $uniq = new ilSelectInputGUI($this->lng->txt('unambiguousness'), 'unique');
235  $uniq->setRequired(true);
236  $uniq->setOptions($options);
237  $form->addItem($uniq);
238 
239  if ($a_create) {
240  $form->setTitle($this->lng->txt('crs_add_grouping'));
241  $form->addCommandButton('add', $this->lng->txt('btn_add'));
242  } else {
243  $grouping = new ilObjCourseGrouping($this->id);
244  $title->setValue($grouping->getTitle());
245  $desc->setValue($grouping->getDescription());
246  $uniq->setValue($grouping->getUniqueField());
247 
248  $ass = new ilCustomInputGUI($this->lng->txt('groupings_assigned_obj_' . $this->getContentType()));
249  $form->addItem($ass);
250 
251  // assignments
252  $items = array();
253  foreach ($grouping->getAssignedItems() as $cond_data) {
254  $items[] = ilObject::_lookupTitle($cond_data['target_obj_id']);
255  }
256  if ($items !== []) {
257  $ass->setHtml(implode("<br />", $items));
258  } else {
259  $ass->setHtml($this->lng->txt('crs_grp_no_courses_assigned'));
260  }
261 
262  $form->setTitle($this->lng->txt('edit_grouping'));
263  $form->addCommandButton('update', $this->lng->txt('save'));
264  $form->addCommandButton('selectCourse', $this->lng->txt('grouping_change_assignment'));
265  }
266  $form->addCommandButton('listGroupings', $this->lng->txt('cancel'));
267  return $form;
268  }
269 
270  public function add(): void
271  {
272  $form = $this->initForm(true);
273  if ($form->checkInput()) {
274  $this->grp_obj->setTitle($form->getInput('title'));
275  $this->grp_obj->setDescription($form->getInput('description'));
276  $this->grp_obj->setUniqueField($form->getInput('unique'));
277 
278  $this->grp_obj->create($this->content_obj->getRefId(), $this->content_obj->getId());
279  $this->tpl->setOnScreenMessage('success', $this->lng->txt('crs_grp_added_grouping'), true);
280  $this->ctrl->redirect($this, 'listGroupings');
281  }
282  $form->setValuesByPost();
283  $this->create($form);
284  }
285 
286  public function edit(?ilPropertyFormGUI $a_form = null): void
287  {
288  if (!$this->access->checkAccess('write', '', $this->content_obj->getRefId())) {
289  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
290  }
291  if (!$a_form) {
292  $a_form = $this->initForm(false);
293  }
294  $this->tpl->setContent($a_form->getHTML());
295  }
296 
297  public function update(): void
298  {
299  if (!$this->access->checkAccess('write', '', $this->content_obj->getRefId())) {
300  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
301  }
302 
303  $obj_id = 0;
304  if ($this->http->wrapper()->query()->has('obj_id')) {
305  $obj_id = $this->http->wrapper()->query()->retrieve(
306  'obj_id',
307  $this->refinery->kindlyTo()->int()
308  );
309  }
310  $form = $this->initForm(false);
311  if ($form->checkInput()) {
312  $tmp_grouping = new ilObjCourseGrouping($obj_id);
313  $tmp_grouping->setTitle($form->getInput('title'));
314  $tmp_grouping->setDescription($form->getInput('description'));
315  $tmp_grouping->setUniqueField($form->getInput('unique'));
316  $tmp_grouping->update();
317 
318  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
319  $this->ctrl->redirect($this, 'listGroupings');
320  }
321 
322  $form->setValuesByPost();
323  $this->edit($form);
324  }
325 
326  public function selectCourse(): void
327  {
328  if (!$this->access->checkAccess('write', '', $this->content_obj->getRefId())) {
329  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
330  }
331 
332  if (!$this->id) {
333  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('crs_grp_no_grouping_id_given'));
334  $this->listGroupings();
335  return;
336  }
337 
338  $this->tabs->clearTargets();
339  $this->tabs->setBackTarget(
340  $this->lng->txt('back'),
341  $this->ctrl->getLinkTarget($this, 'edit')
342  );
343 
344  $table = $this->assignment_table_handler->getTable();
345  $this->tpl->setContent($this->ui_renderer->render($table));
346  }
347 
348  public function handleAssignmentTableAction(): void
349  {
350  switch ($this->assignment_table_handler->getSelectedTableAction()) {
351  case AssignmentTableHandler::ACTION_TOGGLE_ASSIGNMENT:
352  $this->assignCourse(...$this->assignment_table_handler->getSelectedRefIDs());
353  break;
354  }
355  }
356 
357  public function assignCourse(int ...$ref_ids): void
358  {
359  if (!$this->access->checkAccess('write', '', $this->content_obj->getRefId())) {
360  $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->MESSAGE);
361  }
362 
363  if (!$this->id) {
364  $this->listGroupings();
365  return;
366  }
367 
368  // add assignments of not selected items that were assigned
369  // add assignments for selected items that were not assigned
370  $grouping = new ilObjCourseGrouping($this->id);
371  $assigned = $grouping->getAssignedItems();
372  $old_assigned_ref_ids = [];
373  foreach ($assigned as $item) {
374  $old_assigned_ref_ids[] = $item['target_ref_id'];
375  }
376  $new_assigned_ref_ids = array_merge(
377  array_diff($old_assigned_ref_ids, $ref_ids),
378  array_diff($ref_ids, $old_assigned_ref_ids)
379  );
380 
381  // delete all existing conditions
382  $condh = new ilConditionHandler();
383  $condh->deleteByObjId($this->id);
384 
385  foreach ($new_assigned_ref_ids as $ref_id) {
386  $tmp_crs = ilObjectFactory::getInstanceByRefId($ref_id);
387  $tmp_condh = new ilConditionHandler();
388  $tmp_condh->enableAutomaticValidation(false);
389 
390  $tmp_condh->setTargetRefId($ref_id);
391  $tmp_condh->setTargetObjId($tmp_crs->getId());
392  $tmp_condh->setTargetType($this->getContentType());
393  $tmp_condh->setTriggerRefId(0);
394  $tmp_condh->setTriggerObjId($this->id);
395  $tmp_condh->setTriggerType('crsg');
396  $tmp_condh->setOperator('not_member');
397  $tmp_condh->setValue($this->grp_obj->getUniqueField());
398 
399  if (!$tmp_condh->checkExists()) {
400  $tmp_condh->storeCondition();
401  }
402  }
403 
404  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
405  $this->ctrl->redirect($this, 'edit');
406  }
407 } // END class.ilObjCourseGrouping
edit(?ilPropertyFormGUI $a_form=null)
create(?ilPropertyFormGUI $a_form=null)
This class represents a selection list property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilGlobalTemplateInterface $tpl
__construct(ilObject $content_obj, int $a_obj_id=0)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
static http()
Fetches the global http state from ILIAS.
static _lookupTitle(int $obj_id)
askDeleteGrouping(int ... $grouping_ids)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
global $DIC
Definition: shib_login.php:26
setRequired(bool $a_required)
Class ilObjCourseGroupingGUI.
AssignmentTableHandler $assignment_table_handler
This class represents a text area property in a property form.
static _lookupType(int $id, bool $reference=false)
GroupingTableHandler $grouping_table_handler