ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilGroupAddToGroupActionGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
32 {
33  protected ilCtrl $ctrl;
35  protected UIServices $ui;
36  protected ilLanguage $lng;
37  protected ilTree $tree;
38  protected ilObjUser $user;
39 
40 
42  protected Factory $refinery;
43 
44 
45 
46  public function __construct()
47  {
48  global $DIC;
49 
50  $this->ctrl = $DIC->ctrl();
51  $this->tpl = $DIC->ui()->mainTemplate();
52  $this->ui = $DIC->ui();
53  $this->lng = $DIC->language();
54  $this->tree = $DIC->repositoryTree();
55  $this->user = $DIC->user();
56  $this->http = $DIC->http();
57  $this->refinery = $DIC->refinery();
58 
59 
60  $this->lng->loadLanguageModule("grp");
61  $this->ctrl->saveParameter($this, array("user_id", "modal_exists"));
62  }
63 
64  protected function initGroupRefIdFromQuery(): int
65  {
66  $ref_id = 0;
67  if ($this->http->wrapper()->query()->has('grp_act_ref_id')) {
68  $ref_id = $this->http->wrapper()->query()->retrieve(
69  'grp_act_ref_id',
70  $this->refinery->kindlyTo()->int()
71  );
72  }
73  return $ref_id;
74  }
75 
76  protected function initGroupParentRefIdFromQuery(): int
77  {
78  $ref_id = 0;
79  if ($this->http->wrapper()->query()->has('grp_act_par_ref_id')) {
80  $ref_id = $this->http->wrapper()->query()->retrieve(
81  'grp_act_par_ref_id',
82  $this->refinery->kindlyTo()->int()
83  );
84  }
85  return $ref_id;
86  }
87 
88  protected function initUserIdFromQuery(): int
89  {
90  $user_id = 0;
91  if ($this->http->wrapper()->query()->has('user_id')) {
92  $user_id = $this->http->wrapper()->query()->retrieve(
93  'user_id',
94  $this->refinery->kindlyTo()->int()
95  );
96  }
97  return $user_id;
98  }
99 
100  public function executeCommand(): void
101  {
102  $ctrl = $this->ctrl;
103  $user = $this->user;
104 
105  $next_class = $ctrl->getNextClass($this);
106  $cmd = $ctrl->getCmd("show");
107 
108  if ($cmd == "show") {
110  if (count($ca) == 0) {
111  return;
112  }
113  if (count($ca) == 1) {
114  switch (current($ca)) {
115  case "create_grp":
116  $cmd = "selectParent";
117  break;
118 
119  case "manage_members":
120  $cmd = "selectGroup";
121  break;
122 
123  default:
124  return;
125  }
126  }
127  }
128 
129  switch ($next_class) {
130  default:
131  if (in_array($cmd, array("show", "selectGroup", "confirmAddUser", "addUser",
132  "selectParent", "createGroup", "confirmCreateGroupAndAddUser", "createGroupAndAddUser"))) {
133  $ctrl->setParameter($this, "modal_exists", 1);
134  $this->$cmd();
135  }
136  }
137  }
138  public function show(): void
139  {
140  $lng = $this->lng;
141  $ctrl = $this->ctrl;
142 
143  $toolbar = new ilToolbarGUI();
144 
145  // button use existing group
146  $url1 = $ctrl->getLinkTarget($this, "selectGroup", "", true);
147  $button1 = $this->ui->factory()->button()->standard($lng->txt("grp_use_existing"), "#")
148  ->withOnLoadCode(function ($id) use ($url1) {
149  return "$('#$id').on('click', function() {il.repository.core.fetchReplaceInner( document.getElementById('il_grp_action_modal_content'),'$url1'); return false;})";
150  });
151  $toolbar->addComponent($button1);
152 
153  // button create new group
154  $url2 = $ctrl->getLinkTarget($this, "selectParent", "", true);
155  $button2 = $this->ui->factory()->button()->standard($lng->txt("grp_create_new"), "#")
156  ->withOnLoadCode(function ($id) use ($url2) {
157  return "$('#$id').on('click', function() {il.repository.core.fetchReplaceInner( document.getElementById('il_grp_action_modal_content'),'$url2'); return false;})";
158  });
159  $toolbar->addComponent($button2);
160 
161  $this->sendResponse(
162  ilUtil::getSystemMessageHTML($lng->txt("grp_create_or_use_existing"), "question") .
163  $toolbar->getHTML()
164  );
165  }
166 
167  public function sendResponse(string $a_content): void
168  {
169  $lng = $this->lng;
170 
171  $modal_exists = false;
172  if ($this->http->wrapper()->query()->has('modal_exists')) {
173  $modal_exists = (bool) $this->http->wrapper()->query()->retrieve(
174  'modal_exists',
175  $this->refinery->kindlyTo()->int()
176  );
177  }
178  if ($modal_exists) {
179  echo $this->ui->renderer()->renderAsync($this->ui->factory()->legacy()->content($a_content));
180  } else {
181  $mtpl = new ilTemplate("tpl.grp_add_to_grp_modal_content.html", true, true, "./components/ILIAS/Group/UserActions");
182  $mtpl->setVariable("CONTENT", $a_content);
183  $content = $this->ui->factory()->legacy()->content($mtpl->get());
184  $modal = $this->ui->factory()->modal()->roundtrip(
185  $lng->txt("grp_add_user_to_group"),
186  $content
187  )->withOnLoadCode(function ($id) {
188  return "il.UI.modal.showModal(document.getElementById('$id'), {'ajaxRenderUrl':'','keyboard':true}, {id: '$id'});";
189  });
190  echo $this->ui->renderer()->renderAsync($modal);
191  }
192  exit;
193  }
194 
195 
196  public function selectGroup(): void
197  {
198  $tree = $this->tree;
199 
200  $exp = new ilGroupActionTargetExplorerGUI($this, "selectGroup");
201 
202  $exp->setClickableType("grp");
203  $exp->setTypeWhiteList(array("root", "cat", "crs", "fold", "grp"));
204  $exp->setPathOpen($tree->readRootId());
205 
206  if (!$exp->handleCommand()) {
207  $this->sendResponse($exp->getHTML());
208  }
209  exit;
210  }
211 
212  public function confirmAddUser(): void
213  {
214  $ctrl = $this->ctrl;
215  $lng = $this->lng;
216 
217  $ref_id = $this->initGroupRefIdFromQuery();
218  $user_id = $this->initUserIdFromQuery();
220  if ($participants->isMember($user_id)) {
221  $url = $ctrl->getLinkTarget($this, "selectGroup", "", true);
222  $button = $this->ui->factory()->button()->standard($lng->txt("back"), "#")
223  ->withOnLoadCode(function ($id) use ($url) {
224  return "$('#$id').on('click', function() {il.repository.core.fetchReplaceInner( document.getElementById('il_grp_action_modal_content'),'$url'); return false;})";
225  });
226 
227  echo
228  ilUtil::getSystemMessageHTML($lng->txt("grp_user_already_in_group") . "<br>" .
229  $lng->txt("obj_user") . ": " . ilUserUtil::getNamePresentation($user_id) . "<br>" .
230  $lng->txt("obj_grp") . ": " . ilObject::_lookupTitle(ilObject::_lookupObjId($ref_id)), "failure") .
231  $this->ui->renderer()->renderAsync($button);
232  exit;
233  }
234 
235 
236  // button create new group
237  $ctrl->setParameter($this, "grp_act_ref_id", $ref_id);
238  $url = $ctrl->getLinkTarget($this, "addUser", "", true);
239  $button = $this->ui->factory()->button()->standard($lng->txt("grp_add_user"), "#")
240  ->withOnLoadCode(function ($id) use ($url) {
241  return "$('#$id').on('click', function() {il.repository.core.fetchReplaceInner( document.getElementById('il_grp_action_modal_content'),'$url'); return false;})";
242  });
243 
244  echo
245  ilUtil::getSystemMessageHTML($lng->txt("grp_sure_add_user_to_group") . "<br>" .
246  $lng->txt("obj_user") . ": " . ilUserUtil::getNamePresentation($user_id) . "<br>" .
247  $lng->txt("obj_grp") . ": " . ilObject::_lookupTitle(ilObject::_lookupObjId($ref_id)), "question") .
248  $this->ui->renderer()->renderAsync($button);
249  exit;
250  }
251 
252  public function addUser(): void
253  {
254  $lng = $this->lng;
255 
256  $ref_id = $this->initGroupRefIdFromQuery();
257  $user_id = $this->initUserIdFromQuery();
258 
259  // @todo: check permission
260 
261  $group = new ilObjGroup($ref_id);
262  $participants = ilParticipants::getInstanceByObjId($group->getId());
263 
264  $participants->add($user_id, ilParticipants::IL_GRP_MEMBER);
265 
266  $participants->sendNotification(
268  $user_id
269  );
270 
271  echo ilUtil::getSystemMessageHTML($lng->txt("grp_user_been_added"), "success");
272  echo "<script>setTimeout(function (){ il.Group.UserActions.closeModal();}, 1000);</script>";
273  exit;
274  }
275 
276  public function selectParent(): void
277  {
278  $tree = $this->tree;
279  $lng = $this->lng;
280 
281  $exp = new ilGroupActionTargetExplorerGUI($this, "selectParent", true);
282 
283  $exp->setTypeWhiteList(array("root", "cat", "crs"));
284  $exp->setPathOpen($tree->readRootId());
285 
286  if (!$exp->handleCommand()) {
287  $this->sendResponse(ilUtil::getSystemMessageHTML($lng->txt("grp_no_perm_to_add_create_first")) .
288  $exp->getHTML());
289  }
290 
291  exit;
292  }
293 
294  public function createGroup($form = null): void
295  {
296  $lng = $this->lng;
297 
299 
300  if ($form == null) {
301  $form = $this->getGroupCreationForm();
302  }
303  $this->ctrl->saveParameter($this, "grp_act_par_ref_id");
304  $form->setFormAction($this->ctrl->getLinkTarget($this, "confirmCreateGroupAndAddUser", "", true));
305 
306  echo ilUtil::getSystemMessageHTML(str_replace("%1", ilObject::_lookupTitle(ilObject::_lookupObjId($ref_id)), $lng->txt("grp_create_new_grp_in")), "info") .
307  $form->getHTML();
308  exit;
309  }
310 
311 
313  {
314  $lng = $this->lng;
315 
316  $ref_id = $this->initGroupRefIdFromQuery();
317 
318  $group_gui = new ilObjGroupGUI("", 0, true);
319  $group_gui->setCreationMode();
320  $form = $group_gui->initForm("create", true);
321  $form->clearCommandButtons();
322  $form->addCommandButton("save", $lng->txt("grp_next"));
323  $form->setShowTopButtons(false);
324  return $form;
325  }
326 
327  public function confirmCreateGroupAndAddUser(): void
328  {
329  $lng = $this->lng;
330 
331  $user_id = $this->initUserIdFromQuery();
332  $title = '';
333  if ($this->http->wrapper()->post()->has('title')) {
334  $title = $this->http->wrapper()->post()->retrieve(
335  'title',
336  $this->refinery->kindlyTo()->string()
337  );
338  }
339  $form = $this->getGroupCreationForm();
340 
341 
342 
343  if (!$form->checkInput()) {
344  $this->createGroup($form);
345  return;
346  }
347 
348  $this->ctrl->saveParameter($this, "grp_act_par_ref_id");
349  $form->setFormAction($this->ctrl->getLinkTarget($this, "createGroupAndAddUser", "", true));
350  $form->setValuesByPost();
351 
352  $button = $this->ui->factory()->button()->standard($lng->txt("grp_create_and_add_user"), "#")
353  ->withOnLoadCode(function ($id) {
354  return "$('#$id').on('click', function(e) {il.Group.UserActions.createGroup(e);})";
355  });
356 
357  echo
358  ilUtil::getSystemMessageHTML($lng->txt("grp_sure_create_group_add_user") . "<br>" .
359  $lng->txt("obj_user") . ": " . ilUserUtil::getNamePresentation($user_id) . "<br>" .
360  $lng->txt("obj_grp") . ": " . $title, "question") .
361  "<div class='ilNoDisplay'>" . $form->getHTML() . "</div>" .
362  "<div class='ilRight'>" . $this->ui->renderer()->renderAsync($button) . "</div>";
363 
364  exit;
365  }
366 
367  public function createGroupAndAddUser(): void
368  {
369  $lng = $this->lng;
370 
371  $user_id = $this->initUserIdFromQuery();
373  $form = $this->getGroupCreationForm();
374 
375  $form->checkInput();
376 
377  $group_gui = new ilObjGroupGUI("", 0, true);
378 
379  // create instance
380  $newObj = new ilObjGroup();
381  $newObj->setType("grp");
382  $newObj->setTitle($form->getInput("title"));
383  $newObj->setDescription($form->getInput("desc"));
384  $newObj->create();
385 
386  $group_gui->putObjectInTree($newObj, $ref_id);
387  $group_gui = new ilObjGroupGUI("", $group_gui->getRefId(), true);
388 
389 
390  // apply didactic template?
391  $type = 'didactic_type';
392  $dtpl = 0;
393  if ($this->http->wrapper()->post()->has('didactic_type')) {
394  $var = $this->http->wrapper()->post()->retrieve(
395  'didactic_type',
396  $this->refinery->kindlyTo()->string()
397  );
398 
399  if (substr($var, 0, strlen($type) + 1) != $type . "_") {
400  $dtpl = 0;
401  } else {
402  $dtpl = (int) substr($var, strlen($type) + 1);
403  }
404  }
405  if ($dtpl) {
406  $newObj->applyDidacticTemplate($dtpl);
407  }
408 
409  $group_gui->afterSave($newObj, false);
410 
411 
412  $participants = ilParticipants::getInstanceByObjId($newObj->getId());
413 
414  $participants->add($user_id, ilParticipants::IL_GRP_MEMBER);
415 
416  $participants->sendNotification(
418  $user_id
419  );
420 
421 
422  $url = ilLink::_getLink($newObj->getRefId());
423  echo ilUtil::getSystemMessageHTML($lng->txt("grp_created_and_user_been_added"), "success");
424  echo "<script>setTimeout(function (){ window.location.replace('$url');}, 1500);</script>";
425  exit;
426  }
427 }
static getSystemMessageHTML(string $a_txt, string $a_type="info")
Get HTML for a system message.
Class ilObjGroupGUI.
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...
Add user to group from awareness tool.
static getInstanceByObjId(int $a_obj_id)
Get instance by obj type.
getCmd(?string $fallback_command=null)
getLinkTarget(object $a_gui_obj, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$url
Definition: shib_logout.php:66
static _lookupObjId(int $ref_id)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Provides fluid interface to RBAC services.
Definition: UIServices.php:24
getNextClass($a_gui_class=null)
$ref_id
Definition: ltiauth.php:65
static http()
Fetches the global http state from ILIAS.
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path='ilpublicuserprofilegui')
Default behaviour is:
static _lookupTitle(int $obj_id)
global $DIC
Definition: shib_login.php:22
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class ilObjGroup.
setParameter(object $a_gui_obj, string $a_parameter, $a_value)
exit
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...