ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSurveyRaterGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
26 {
27  protected \ILIAS\Survey\Editing\EditingGUIRequest $edit_request;
28  protected \ilCtrl $ctrl;
29  protected \ilLanguage $lng;
30  protected \ILIAS\DI\UIServices $ui;
31  protected \ILIAS\Refinery\Factory $refinery;
32  protected \ilGlobalTemplateInterface $main_tpl;
34  protected ilObjSurvey $survey;
35  protected \ilObjUser $user;
36  protected \ilAccessHandler $access;
37  protected ilTabsGUI $tabs;
38 
39  public function __construct(
41  ilObjSurvey $survey
42  ) {
43  global $DIC;
44 
45  $this->ctrl = $DIC->ctrl();
46  $this->lng = $DIC->language();
47  $this->ui = $DIC->ui();
48  $this->refinery = $DIC->refinery();
49  $this->main_tpl = $DIC->ui()->mainTemplate();
50  $this->parent = $parent;
51  $this->survey = $survey;
52  $this->user = $DIC->user();
53  $this->access = $DIC->access();
54  $this->tabs = $DIC->tabs();
55 
56  $this->ctrl->saveParameter($this, "appr_id");
57  $this->edit_request = $DIC->survey()
58  ->internal()
59  ->gui()
60  ->editing()
61  ->request();
62  }
63 
64  public function executeCommand(): void
65  {
66  $ctrl = $this->ctrl;
67 
68  $next_class = $ctrl->getNextClass($this);
69  $cmd = $ctrl->getCmd("add");
70 
71  switch ($next_class) {
72  case 'ilrepositorysearchgui':
73  $rep_search = new ilRepositorySearchGUI();
74 
75  $this->ctrl->setParameter($this, "rate360", 1);
76  $this->ctrl->saveParameter($this, "appr_id");
77 
78  $rep_search->setCallback(
79  $this,
80  'addFromSearch',
81  array()
82  );
83 
84  // Set tabs
85  $this->ctrl->setReturn($this, 'add');
86  $this->ctrl->forwardCommand($rep_search);
87  break;
88 
89  default:
90  if (in_array($cmd, ["add",
91  "doAutoComplete",
92  "continue",
93  "mailRaters",
94  "mailRatersAction",
95  "cancel"
96  ])) {
97  $this->$cmd();
98  }
99  break;
100  }
101  }
102 
103  protected function cancel(): void
104  {
105  $this->ctrl->redirect($this->parent, "editRaters");
106  }
107 
108  protected function add(
109  ilPropertyFormGUI $form = null
110  ): void {
111  $form_html = (!is_null($form))
112  ? $form->getHTML()
113  : $this->initOptionSelectForm()->getHTML();
114  $main_tpl = $this->main_tpl;
115  $main_tpl->setContent($form_html);
116  }
117 
119  {
120  $ctrl = $this->ctrl;
121  $lng = $this->lng;
122 
123  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
124  $form = new ilPropertyFormGUI();
125 
126  //
127  $radg = new ilRadioGroupInputGUI($lng->txt("svy_type_of_rater"), "type");
128  //$radg->setInfo($lng->txt(""));
129  //$radg->setValue();
130  $op1 = new ilRadioOption($lng->txt("svy_add_internal_user"), "direct", $lng->txt("svy_add_internal_user_info"));
131  $radg->addOption($op1);
132  $radg->setValue("direct");
133 
134  $user = new \ilTextInputGUI($lng->txt("obj_user"), "user");
135  $user->setDataSource(
136  $ctrl->getLinkTargetByClass(
137  "ilsurveyratergui",
138  "doAutoComplete",
139  "",
140  true
141  )
142  );
143  $user->setRequired(true);
144  $user->setMulti(false);
145  $op1->addSubItem($user);
146 
147 
148  $op2 = new ilRadioOption($lng->txt("svy_search_user"), "search", $lng->txt("svy_search_user_info"));
149  $radg->addOption($op2);
150 
151  $op3 = new ilRadioOption($lng->txt("svy_external_rater"), "external", $lng->txt("svy_external_rater_info"));
152  $radg->addOption($op3);
153 
154  $email = new ilEMailInputGUI($this->lng->txt("email"), "email");
155  $email->setRequired(true);
156  $op3->addSubItem($email);
157 
158  $lname = new ilTextInputGUI($this->lng->txt("lastname"), "lname");
159  $lname->setSize(30);
160  $op3->addSubItem($lname);
161 
162  $fname = new ilTextInputGUI($this->lng->txt("firstname"), "fname");
163  $fname->setSize(30);
164  $op3->addSubItem($fname);
165 
166 
167  $form->addItem($radg);
168 
169  // save and cancel commands
170  $form->addCommandButton("continue", $lng->txt("svy_save_and_continue"));
171  $form->addCommandButton("cancel", $lng->txt("cancel"));
172 
173  $form->setTitle($lng->txt("svy_add_rater"));
174  $form->setFormAction($ctrl->getFormAction($this));
175 
176  return $form;
177  }
178 
179  public function doAutoComplete(): void
180  {
181  $fields = array('login','firstname','lastname','email');
182 
183  $auto = new ilUserAutoComplete();
184  $auto->setSearchFields($fields);
185  $auto->setResultField('login');
186  $auto->enableFieldSearchableCheck(true);
187  $auto->setMoreLinkAvailable(true);
189 
190  if ($this->edit_request->getFetchAll()) {
191  $auto->setLimit(ilUserAutoComplete::MAX_ENTRIES);
192  }
193 
194  echo $auto->getList($this->edit_request->getTerm());
195  exit();
196  }
197 
198  protected function continue(): void
199  {
200  $form = $this->initOptionSelectForm();
201  if ($form->checkInput()) {
202  switch ($form->getInput("type")) {
203  case "direct":
204  $this->addRater($form);
205  break;
206 
207  case "external":
208  $this->addExternalRater($form);
209  break;
210 
211  case "search":
212  $this->ctrl->redirectByClass("ilrepositorysearchgui", "");
213  break;
214  }
215  } else {
216  $form->setValuesByPost();
217  $this->add($form);
218  }
219  }
220 
221  public function addRater(ilPropertyFormGUI $form): void
222  {
223  // check access
224  $ilAccess = $this->access;
225  $ilUser = $this->user;
226 
227  $appr_id = $this->parent->handleRatersAccess();
228  $user = $form->getInput("user");
229  $user_id = ilObjUser::_lookupId($user);
230  if ($user_id > 0) {
231  if ($ilAccess->checkAccess("write", "", $this->survey->getRefId()) ||
232  $this->survey->get360SelfEvaluation() ||
233  $user_id !== $ilUser->getId()) {
234  if ($appr_id !== $user_id) {
235  $this->survey->addRater($appr_id, $user_id);
236  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt("settings_saved"), true);
237  } else {
238  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt("svy_appraisses_cannot_be_raters"), true);
239  $user_id = 0;
240  }
241  }
242  } else {
243  $this->main_tpl->setOnScreenMessage(
244  'failure',
245  $this->lng->txt("svy_user_not_found") . " (" . $user . ")",
246  true
247  );
248  }
249 
250  $this->ctrl->setParameter($this->parent, "appr_id", $appr_id);
251  if ($user_id > 0) {
252  $this->ctrl->setParameter($this, "rater_id", "u" . $user_id);
253  $this->ctrl->redirect($this, "mailRaters");
254  }
255  $this->ctrl->redirect($this->parent, "editRaters");
256  }
257 
258 
259  public function mailRaters(ilPropertyFormGUI $a_form = null): void
260  {
261  $appr_id = $this->parent->handleRatersAccess();
262  $this->ctrl->setParameterByClass("ilSurveyParticipantsGUI", "appr_id", $appr_id);
263  $this->ctrl->setParameterByClass("ilSurveyParticipantsGUI", "rater_id", $this->edit_request->getRaterId());
264  $this->ctrl->redirectByClass("ilSurveyParticipantsGUI", "mailRaters");
265  }
266 
267  public function initMailRatersForm(
268  int $appr_id,
269  array $rec_ids
270  ): ilPropertyFormGUI {
271  $form = new ilPropertyFormGUI();
272  $form->setFormAction($this->ctrl->getFormAction($this, "mailRatersAction"));
273  $form->setTitle($this->lng->txt('compose'));
274 
275  $all_data = $this->survey->getRatersData($appr_id);
276 
277  $rec_data = array();
278  foreach ($rec_ids as $rec_id) {
279  if (isset($all_data[$rec_id])) {
280  $rec_data[] = $all_data[$rec_id]["lastname"] . ", " .
281  $all_data[$rec_id]["firstname"] .
282  " (" . $all_data[$rec_id]["email"] . ")";
283  }
284  }
285  sort($rec_data);
286  $rec = new ilCustomInputGUI($this->lng->txt('recipients'));
287  $rec->setHtml(implode("<br />", $rec_data));
288  $form->addItem($rec);
289 
290  $subject = new ilTextInputGUI($this->lng->txt('subject'), 'subject');
291  $subject->setSize(50);
292  $subject->setRequired(true);
293  $form->addItem($subject);
294 
295  $existingdata = $this->survey->getExternalCodeRecipients();
296  $existingcolumns = array();
297  if (count($existingdata)) {
298  $first = array_shift($existingdata);
299  foreach ($first as $key => $value) {
300  if (strcmp($key, 'code') !== 0 && strcmp($key, 'email') !== 0 && strcmp($key, 'sent') !== 0) {
301  $existingcolumns[] = '[' . $key . ']';
302  }
303  }
304  }
305 
306  $mailmessage_u = new ilTextAreaInputGUI($this->lng->txt('survey_360_rater_message_content_registered'), 'message_u');
307  $mailmessage_u->setRequired(true);
308  $mailmessage_u->setCols(80);
309  $mailmessage_u->setRows(10);
310  $form->addItem($mailmessage_u);
311 
312  $mailmessage_a = new ilTextAreaInputGUI($this->lng->txt('survey_360_rater_message_content_anonymous'), 'message_a');
313  $mailmessage_a->setRequired(true);
314  $mailmessage_a->setCols(80);
315  $mailmessage_a->setRows(10);
316  $mailmessage_a->setInfo(sprintf($this->lng->txt('message_content_info'), implode(', ', $existingcolumns)));
317  $form->addItem($mailmessage_a);
318 
319  $recf = new ilHiddenInputGUI("rtr_id");
320  $recf->setValue(implode(";", $rec_ids));
321  $form->addItem($recf);
322 
323  $form->addCommandButton("mailRatersAction", $this->lng->txt("send"));
324  $form->addCommandButton("cancel", $this->lng->txt("svy_dont_send"));
325 
326  $subject->setValue(sprintf($this->lng->txt('survey_360_rater_subject_default'), $this->survey->getTitle()));
327  $mailmessage_u->setValue($this->lng->txt('survey_360_rater_message_content_registered_default'));
328  $mailmessage_a->setValue($this->lng->txt('survey_360_rater_message_content_anonymous_default'));
329 
330  return $form;
331  }
332 
333 
334  public function mailRatersAction(): void
335  {
336  $ilUser = $this->user;
337 
338  $appr_id = $this->parent->handleRatersAccess();
339  $this->ctrl->setParameter($this, "appr_id", $appr_id);
340 
341  $rec_ids = $this->edit_request->getRaterIds();
342  if (count($rec_ids) === 0) {
343  $this->ctrl->redirect($this, "editRaters");
344  }
345 
346  $form = $this->initMailRatersForm($appr_id, $rec_ids);
347  if ($form->checkInput()) {
348  $txt_u = $form->getInput("message_u");
349  $txt_a = $form->getInput("message_a");
350  $subj = $form->getInput("subject");
351 
352  // #12743
353  $sender_id = (trim($ilUser->getEmail()))
354  ? $ilUser->getId()
356 
357  $all_data = $this->survey->getRatersData($appr_id);
358  foreach ($rec_ids as $rec_id) {
359  if (isset($all_data[$rec_id])) {
360  $user = $all_data[$rec_id];
361 
362  // anonymous
363  if (strpos($rec_id, "a") === 0) {
364  $mytxt = $txt_a;
365  $url = $user["href"];
366  $rcp = $user["email"];
367  }
368  // reg
369  else {
370  $mytxt = $txt_u;
371  $user["code"] = $this->lng->txt("survey_code_mail_on_demand");
372  $url = ilLink::_getStaticLink($this->survey->getRefId());
373  $rcp = $user["login"]; // #15141
374  }
375 
376  $mytxt = str_replace(
377  ["[lastname]", "[firstname]", "[url]", "[code]"],
378  [$user["lastname"], $user["firstname"], $url, $user["code"]],
379  $mytxt
380  );
381 
382  $mail = new ilMail($sender_id);
383  $mail->enqueue(
384  $rcp, // to
385  "", // cc
386  "", // bcc
387  $subj, // subject
388  $mytxt, // message
389  array() // attachments
390  );
391 
392  $this->survey->set360RaterSent(
393  $appr_id,
394  (strpos($rec_id, "a") === 0) ? 0 : (int) substr($rec_id, 1),
395  (strpos($rec_id, "u") === 0) ? 0 : (int) substr($rec_id, 1)
396  );
397  }
398  }
399 
400  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt("mail_sent"), true);
401  $this->ctrl->redirect($this->parent, "editRaters");
402  }
403 
404  $form->setValuesByPost();
405 // $this->mailRatersObject($form);
406  }
407 
408  public function addExternalRater(ilPropertyFormGUI $form): void
409  {
410  $appr_id = $this->edit_request->getAppraiseeId();
411 
412  if (!$appr_id) {
413  $this->ctrl->redirect($this, "listAppraisees");
414  }
415 
416  $this->ctrl->setParameter($this, "appr_id", $appr_id);
417 
418  $code_id = $this->parent->addCodeForExternal(
419  $form->getInput("email"),
420  $form->getInput("lname"),
421  $form->getInput("fname")
422  );
423 
424  $this->survey->addRater($appr_id, 0, $code_id);
425 
426  $this->ctrl->setParameter($this->parent, "appr_id", $appr_id);
427  if ($code_id > 0) {
428  $this->ctrl->setParameter($this, "rater_id", "a" . $code_id);
429  $this->ctrl->redirect($this, "mailRaters");
430  }
431  $this->ctrl->redirect($this->parent, "editRaters");
432  }
433 
434  public function addFromSearch(
435  array $user_ids
436  ): void {
437  // check access
438  $ilAccess = $this->access;
439  $ilUser = $this->user;
440 
441  $user_id = 0;
442 
443  $appr_id = $this->parent->handleRatersAccess();
444 
445  foreach ($user_ids as $user_id) {
446  if ($user_id > 0) {
447  if ($ilAccess->checkAccess("write", "", $this->survey->getRefId()) ||
448  $this->survey->get360SelfEvaluation() ||
449  $user_id != $ilUser->getId()) {
450  if ($appr_id != $user_id) {
451  $this->survey->addRater($appr_id, $user_id);
452  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt("settings_saved"), true);
453  } else {
454  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt("svy_appraisses_cannot_be_raters"), true);
455  $user_id = 0;
456  }
457  }
458  }
459  }
460 
461  $user_str = implode(";", array_map(static function ($u): string {
462  return "u" . $u;
463  }, $user_ids));
464 
465  $this->ctrl->setParameter($this->parent, "appr_id", $appr_id);
466  if ($user_id > 0) {
467  $this->ctrl->setParameter($this, "rater_id", $user_str);
468  $this->ctrl->redirect($this, "mailRaters");
469  }
470  $this->ctrl->redirect($this->parent, "editRaters");
471  }
472 }
Class ilSurveyParticipantsGUI.
ILIAS Survey Editing EditingGUIRequest $edit_request
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
exit
Definition: login.php:29
addExternalRater(ilPropertyFormGUI $form)
const ANONYMOUS_USER_ID
Definition: constants.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS Refinery Factory $refinery
static _lookupId($a_user_str)
addFromSearch(array $user_ids)
__construct(ilSurveyParticipantsGUI $parent, ilObjSurvey $survey)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
mailRaters(ilPropertyFormGUI $a_form=null)
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS DI UIServices $ui
getNextClass($a_gui_class=null)
This class represents a property in a property form.
initMailRatersForm(int $appr_id, array $rec_ids)
setContent(string $a_html)
Sets content for standard template.
setValue(string $a_value)
string $key
Consumer key/client ID value.
Definition: System.php:193
$url
Definition: ltiregstart.php:35
addRater(ilPropertyFormGUI $form)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setRequired(bool $a_required)
ilSurveyParticipantsGUI $parent
ilGlobalTemplateInterface $main_tpl
This class represents a text area property in a property form.
add(ilPropertyFormGUI $form=null)