ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAccountCodesGUI.php
Go to the documentation of this file.
1 <?php
2 
24 {
26  protected \ILIAS\User\StandardGUIRequest $request;
27  protected int $ref_id;
28  protected array $filter; // obsolete?
29  private \ilGlobalTemplateInterface $main_tpl;
30 
31  public function __construct(int $a_ref_id)
32  {
33  global $DIC;
34  $this->main_tpl = $DIC->ui()->mainTemplate();
35 
36  $lng = $DIC['lng'];
37 
38  $this->ref_id = $a_ref_id;
39  $lng->loadLanguageModule("user");
40  $this->request = new \ILIAS\User\StandardGUIRequest(
41  $DIC->http(),
42  $DIC->refinery()
43  );
44  }
45 
46  public function executeCommand(): void
47  {
48  global $DIC;
49 
50  $ilCtrl = $DIC['ilCtrl'];
51 
52  $next_class = $ilCtrl->getNextClass($this);
53  $cmd = $ilCtrl->getCmd();
54 
55  switch ($next_class) {
56  default:
57  if (!$cmd) {
58  $cmd = "listCodes";
59  }
60  $this->$cmd();
61  break;
62  }
63  }
64 
65  public function listCodes(): void
66  {
67  global $DIC;
68 
69  $ilAccess = $DIC['ilAccess'];
70  $ilErr = $DIC['ilErr'];
71  $ilCtrl = $DIC['ilCtrl'];
72  $ilToolbar = $DIC['ilToolbar'];
73  $lng = $DIC['lng'];
74  $tpl = $DIC['tpl'];
75 
76  if (!$ilAccess->checkAccess('read', '', $this->ref_id)) {
77  $ilErr->raiseError($lng->txt("msg_no_perm_read"), $ilErr->MESSAGE);
78  }
79 
80  $button = ilLinkButton::getInstance();
81  $button->setCaption("user_account_codes_add");
82  $button->setUrl($ilCtrl->getLinkTarget($this, "addCodes"));
83  $ilToolbar->addButtonInstance($button);
84 
85  $ctab = new ilAccountCodesTableGUI($this, "listCodes");
86  $tpl->setContent($ctab->getHTML());
87  }
88 
89  public function initAddCodesForm(): void
90  {
91  global $DIC;
92 
93  $ilCtrl = $DIC['ilCtrl'];
94  $lng = $DIC['lng'];
95 
96  $this->form_gui = new ilPropertyFormGUI();
97  $this->form_gui->setFormAction($ilCtrl->getFormAction($this, 'createCodes'));
98  $this->form_gui->setTitle($lng->txt('user_account_codes_edit_header'));
99 
100  $count = new ilNumberInputGUI($lng->txt('user_account_codes_number'), 'acc_codes_number');
101  $count->setSize(4);
102  $count->setMaxLength(4);
103  $count->setMinValue(1);
104  $count->setMaxValue(1000);
105  $count->setRequired(true);
106  $this->form_gui->addItem($count);
107 
108  $valid = new ilRadioGroupInputGUI($lng->txt('user_account_code_valid_until'), 'valid_type');
109  $valid->setRequired(true);
110 
111  $unl = new ilRadioOption($lng->txt('user_account_code_valid_until_unlimited'), 'valid_unlimited');
112  $valid->addOption($unl);
113 
114  $st = new ilRadioOption($lng->txt('user_account_code_valid_until_static'), 'valid_static');
115  $valid->addOption($st);
116 
117  $dt = new ilDateTimeInputGUI($lng->txt('date'), 'valid_date');
118  $dt->setRequired(true);
119  $st->addSubItem($dt);
120 
121  $dyn = new ilRadioOption($lng->txt('user_account_code_valid_until_dynamic'), 'valid_dynamic');
122  $valid->addOption($dyn);
123 
124  $ds = new ilNumberInputGUI($lng->txt('days'), 'valid_days');
125  $ds->setSize(5);
126  $ds->setRequired(true);
127  $dyn->addSubItem($ds);
128 
129  $this->form_gui->addItem($valid);
130 
131  $this->form_gui->addCommandButton('createCodes', $lng->txt('create'));
132  $this->form_gui->addCommandButton('listCodes', $lng->txt('cancel'));
133  }
134 
135  public function addCodes(): void
136  {
137  global $DIC;
138 
139  $ilAccess = $DIC['ilAccess'];
140  $ilErr = $DIC['ilErr'];
141  $tpl = $DIC['tpl'];
142  $lng = $DIC['lng'];
143 
144  if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
145  $ilErr->raiseError($lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
146  }
147 
148  $this->initAddCodesForm();
149  $tpl->setContent($this->form_gui->getHTML());
150  }
151 
152  public function createCodes(): void
153  {
154  global $DIC;
155 
156  $ilAccess = $DIC['ilAccess'];
157  $ilErr = $DIC['ilErr'];
158  $lng = $DIC['lng'];
159  $tpl = $DIC['tpl'];
160  $ilCtrl = $DIC['ilCtrl'];
161 
162  $valid = "";
163 
164  if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
165  $ilErr->raiseError($lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
166  }
167 
168  $this->initAddCodesForm();
169  if ($this->form_gui->checkInput()) {
170  $number = $this->form_gui->getInput('acc_codes_number');
171  switch ($this->form_gui->getInput('valid_type')) {
172  case 'valid_unlimited':
173  $valid = 0;
174  break;
175 
176  case 'valid_static':
177  $valid = $this->form_gui->getItemByPostVar('valid_date')->getDate()->get(IL_CAL_DATE);
178  break;
179 
180  case 'valid_dynamic':
181  $valid = $this->form_gui->getInput('valid_days');
182  break;
183  }
184 
185  $stamp = time();
186  for ($loop = 1; $loop <= $number; $loop++) {
187  ilAccountCode::create($valid, $stamp);
188  }
189 
190  $this->main_tpl->setOnScreenMessage('success', $lng->txt('saved_successfully'), true);
191  $ilCtrl->redirect($this, "listCodes");
192  } else {
193  $this->form_gui->setValuesByPost();
194  $tpl->setContent($this->form_gui->getHTML());
195  }
196  }
197 
198  public function deleteCodes(): void
199  {
200  global $DIC;
201 
202  $lng = $DIC['lng'];
203  $ilCtrl = $DIC['ilCtrl'];
204 
205  $ids = $this->request->getIds();
207 
208  $this->main_tpl->setOnScreenMessage('success', $lng->txt('info_deleted'), true);
209  $ilCtrl->redirect($this, "listCodes");
210  }
211 
212  public function deleteConfirmation(): void
213  {
214  global $DIC;
215 
216  $lng = $DIC['lng'];
217  $ilCtrl = $DIC['ilCtrl'];
218  $tpl = $DIC['tpl'];
219 
220  $ids = $this->request->getIds();
221  if (count($ids) == 0) {
222  $this->main_tpl->setOnScreenMessage('failure', $lng->txt("no_checkbox"), true);
223  $this->listCodes();
224  }
225 
226  $gui = new ilConfirmationGUI();
227  $gui->setHeaderText($lng->txt("info_delete_sure"));
228  $gui->setCancel($lng->txt("cancel"), "listCodes");
229  $gui->setConfirm($lng->txt("confirm"), "deleteCodes");
230  $gui->setFormAction($ilCtrl->getFormAction($this, "deleteCodes"));
231 
233  foreach ($data as $code) {
234  $gui->addItem("id[]", $code["code_id"], $code["code"]);
235  }
236 
237  $tpl->setContent($gui->getHTML());
238  }
239 
240  public function resetCodesFilter(): void
241  {
242  $utab = new ilAccountCodesTableGUI($this, "listCodes");
243  $utab->resetOffset();
244  $utab->resetFilter();
245 
246  $this->listCodes();
247  }
248 
249  public function applyCodesFilter(): void
250  {
251  $utab = new ilAccountCodesTableGUI($this, "listCodes");
252  $utab->resetOffset();
253  $utab->writeFilterToSession();
254 
255  $this->listCodes();
256  }
257 
258  public function exportCodes(): void
259  {
260  global $DIC;
261 
262  $ilAccess = $DIC['ilAccess'];
263  $ilErr = $DIC['ilErr'];
264  $lng = $DIC['lng'];
265 
266  if (!$ilAccess->checkAccess('read', '', $this->ref_id)) {
267  $ilErr->raiseError($lng->txt("msg_no_perm_read"), $ilErr->MESSAGE);
268  }
269 
270  $utab = new ilAccountCodesTableGUI($this, "listCodes");
271  $codes = ilAccountCode::getCodesForExport($utab->filter["code"], $utab->filter["valid_until"], $utab->filter["generated"]);
272 
273  if (count($codes)) {
274  // #13497
275  ilUtil::deliverData(implode("\r\n", $codes), "ilias_account_codes_" . date("d-m-Y") . ".txt", "text/plain");
276  } else {
277  $this->main_tpl->setOnScreenMessage('failure', $lng->txt("account_export_codes_no_data"));
278  $this->listCodes();
279  }
280  }
281 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$lng
$valid
static getCodesForExport(string $filter_code, string $filter_valid_until, string $filter_generated)
static deliverData(string $a_data, string $a_filename, string $mime="application/octet-stream")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ilErr
Definition: raiseError.php:17
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
This class represents a property in a property form.
static loadCodesByIds(array $ids)
This class represents a number property in a property form.
static deleteCodes(array $ids)
setRequired(bool $a_required)
ilGlobalTemplateInterface $main_tpl
static create(string $valid_until, int $stamp)
ILIAS User StandardGUIRequest $request
const IL_CAL_DATE
ilPropertyFormGUI $form_gui
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...