ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilSystemNotification.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected string $subject_lang_id = "";
26  protected string $subject_direct = "";
27  protected string $introduction = "";
28  protected string $introduction_direct = "";
29  protected string $task = "";
30  protected string $reason = "";
31  protected array $additional = [];
32  protected string $goto_caption = "";
33  protected int $changed_by = 0;
34  protected ?array $all_ref_ids = [];
35 
36  public function __construct(protected bool $is_in_wsp = false)
37  {
38  parent::__construct($is_in_wsp);
39  $this->ref_id = 0;
40  }
41 
42  public function setSubjectLangId(string $a_lang_id): void
43  {
44  $this->subject_lang_id = $a_lang_id;
45  }
46 
47  public function setSubjectDirect(string $a_text): void
48  {
49  $this->subject_direct = trim($a_text);
50  }
51 
52  public function setIntroductionLangId(string $a_lang_id): void
53  {
54  $this->introduction = $a_lang_id;
55  }
56 
57  public function setIntroductionDirect(string $a_text): void
58  {
59  $this->introduction_direct = trim($a_text);
60  }
61 
62  public function setTaskLangId(string $a_lang_id): void
63  {
64  $this->task = $a_lang_id;
65  }
66 
67  public function setReasonLangId(string $a_lang_id): void
68  {
69  $this->reason = $a_lang_id;
70  }
71 
72  public function setGotoLangId(string $a_lang_id): void
73  {
74  $this->goto_caption = $a_lang_id;
75  }
76 
77  public function setChangedByUserId(int $a_id): void
78  {
79  $this->changed_by = $a_id;
80  }
81 
85  public function addAdditionalInfo(
86  string $a_lang_id,
87  string $a_value,
88  bool $a_multiline = false,
89  bool $a_lang_direct = false
90  ): void {
91  $this->additional[$a_lang_id] = array(trim($a_value), $a_multiline, $a_lang_direct);
92  }
93 
103  array $a_user_ids,
104  ?string $a_goto_additional = null,
105  string $a_permission = "read"
106  ): array {
107  $this->all_ref_ids = null;
108 
109  // prepare object related info
110  if ($this->getObjId()) {
111  if (!$this->getRefId()) {
112  // try to find ref_id(s)
113  if (!$this->is_in_wsp) {
114  $ref_ids = ilObject::_getAllReferences($this->getObjId());
115  if (count($ref_ids) === 1) {
116  $this->ref_id = array_shift($ref_ids);
117  } else {
118  $this->all_ref_ids = $ref_ids;
119  }
120  }
121  } elseif ($this->is_in_wsp) { // #11680
122  $this->ref_id = $this->wsp_tree->lookupNodeId($this->getObjId());
123  }
124 
125  // default values
126  if (!$this->goto_caption) {
127  $this->goto_caption = "url";
128  }
129  }
130 
131  $recipient_ids = array();
132  foreach (array_unique($a_user_ids) as $user_id) {
133  // author of change should not get notification
134  if ($this->changed_by === $user_id) {
135  continue;
136  }
137  if ($this->composeAndSendMail($user_id, $a_goto_additional, $a_permission)) {
138  $recipient_ids[] = $user_id;
139  }
140  }
141 
142  return $recipient_ids;
143  }
144 
148  public function compose(
149  int $a_user_id,
150  ?string $a_goto_additional = null,
151  string $a_permission = "read",
152  bool $a_append_signature_direct = false
153  ): bool {
154  $find_ref_id = false;
155  $this->initLanguage($a_user_id);
156  $this->initMail();
157  if ($this->subject_direct) {
158  $this->setSubject($this->subject_direct);
159  } else {
160  $this->setSubject(
161  sprintf($this->getLanguageText($this->subject_lang_id), $this->getObjectTitle(true))
162  );
163  }
164 
165  $this->setBody(ilMail::getSalutation($a_user_id, $this->getLanguage()));
166  $this->appendBody("\n\n");
167 
168  if ($this->introduction) {
169  $this->appendBody($this->getLanguageText($this->introduction));
170  $this->appendBody("\n\n");
171  }
172 
173  if ($this->introduction_direct) {
174  $this->appendBody($this->introduction_direct);
175  $this->appendBody("\n\n");
176  }
177 
178  if ($this->task) {
179  $this->appendBody($this->getLanguageText($this->task));
180  $this->appendBody("\n\n");
181  }
182 
183  // details table
184  if ($this->getObjId()) {
185  $this->appendBody($this->getLanguageText("obj_" . $this->getObjType()) . ": " .
186  $this->getObjectTitle() . "\n");
187  }
188  if (count($this->additional) > 0) {
189  foreach ($this->additional as $lang_id => $item) {
190  $caption = "";
191  if ($lang_id) {
192  $caption = (!$item[2])
193  ? $this->getLanguageText($lang_id)
194  : $lang_id;
195  }
196  if (!$item[1]) {
197  if ($caption) {
198  $caption .= ": ";
199  }
200  $this->appendBody($caption . $item[0] . "\n");
201  } else {
202  if ($caption) {
203  $caption .= "\n";
204  }
205  $this->appendBody("\n" . $caption .
206  $this->getBlockBorder() .
207  $item[0] . "\n" .
208  $this->getBlockBorder() . "\n");
209  }
210  }
211  }
212  $this->body = trim($this->body);
213  $this->appendBody("\n\n");
214 
215  if ($this->changed_by) {
216  $this->appendBody($this->getLanguageText("system_notification_installation_changed_by") . ": " .
217  ilUserUtil::getNamePresentation($this->changed_by));
218  $this->appendBody("\n\n");
219  }
220 
221  if ($this->getObjId()) {
222  // try to find accessible ref_id
223  if (!$this->getRefId() && $this->all_ref_ids) {
224  $find_ref_id = true;
225  foreach ($this->all_ref_ids as $ref_id) {
226  if ($this->isRefIdAccessible($a_user_id, $ref_id, $a_permission)) {
227  $this->ref_id = $ref_id;
228  break;
229  }
230  }
231  }
232 
233  // check if initially given ref_id is accessible for current recipient
234  if ($this->getRefId() &&
235  !$find_ref_id &&
236  !$this->isRefIdAccessible($a_user_id, $this->getRefId(), $a_permission)) {
237  return false;
238  }
239 
240  $goto = $this->createPermanentLink(array(), (string) $a_goto_additional);
241  if ($goto) {
242  $this->appendBody($this->getLanguageText($this->goto_caption) . ": " .
243  $goto);
244  $this->appendBody("\n\n");
245  }
246 
247  if ($find_ref_id) {
248  $this->ref_id = 0;
249  }
250  }
251 
252  if ($this->reason) {
253  $this->appendBody($this->getLanguageText($this->reason));
254  $this->appendBody("\n\n");
255  }
256 
258 
259  // signature will append new lines
260  $this->body = trim($this->body);
261 
262  if (!$a_append_signature_direct) {
263  $this->getMail()->appendInstallationSignature(true);
264  } else {
266  }
267 
268  return true;
269  }
270 
274  protected function composeAndSendMail(
275  int $a_user_id,
276  ?string $a_goto_additional = null,
277  string $a_permission = "read"
278  ): bool {
279  if ($this->compose($a_user_id, $a_goto_additional, $a_permission)) {
280  $this->sendMail(array($a_user_id), is_numeric($a_user_id));
281  return true;
282  }
283  return false;
284  }
285 
289  public function composeAndGetMessage(
290  int $a_user_id,
291  ?string $a_goto_additional = null,
292  string $a_permission = "read",
293  bool $a_append_signature_direct = false
294  ): string {
295  if ($this->compose($a_user_id, $a_goto_additional, $a_permission, $a_append_signature_direct)) {
296  return $this->body;
297  }
298  return "";
299  }
300 }
sendMail(array $a_rcp, bool $a_parse_recipients=true)
static _getAllReferences(int $id)
get all reference ids for object ID
sendMailAndReturnRecipients(array $a_user_ids, ?string $a_goto_additional=null, string $a_permission="read")
Send notification(s)
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 getSalutation(int $a_usr_id, ?ilLanguage $a_language=null)
getLanguageText(string $a_keyword)
setIntroductionLangId(string $a_lang_id)
isRefIdAccessible(int $a_user_id, int $a_ref_id, string $a_permission="read")
composeAndGetMessage(int $a_user_id, ?string $a_goto_additional=null, string $a_permission="read", bool $a_append_signature_direct=false)
Compose notification to single recipient.
__construct(protected bool $is_in_wsp=false)
getObjectTitle(bool $a_shorten=false)
createPermanentLink(array $a_params=[], string $a_append='')
__construct(Container $dic, ilPlugin $plugin)
addAdditionalInfo(string $a_lang_id, string $a_value, bool $a_multiline=false, bool $a_lang_direct=false)
Add additional information.
static _getAutoGeneratedMessageString(ilLanguage $lang=null)
setSubject(string $a_subject)
composeAndSendMail(int $a_user_id, ?string $a_goto_additional=null, string $a_permission="read")
Send notification to single recipient.
language()
description: > Example for rendring a language glyph.
Definition: language.php:25
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setReasonLangId(string $a_lang_id)
static _getInstallationSignature()
setSubjectLangId(string $a_lang_id)
compose(int $a_user_id, ?string $a_goto_additional=null, string $a_permission="read", bool $a_append_signature_direct=false)
Compose notification to single recipient.