ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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 setSubjectLangId(string $a_lang_id): void
37  {
38  $this->subject_lang_id = $a_lang_id;
39  }
40 
41  public function setSubjectDirect(string $a_text): void
42  {
43  $this->subject_direct = trim($a_text);
44  }
45 
46  public function setIntroductionLangId(string $a_lang_id): void
47  {
48  $this->introduction = $a_lang_id;
49  }
50 
51  public function setIntroductionDirect(string $a_text): void
52  {
53  $this->introduction_direct = trim($a_text);
54  }
55 
56  public function setTaskLangId(string $a_lang_id): void
57  {
58  $this->task = $a_lang_id;
59  }
60 
61  public function setReasonLangId(string $a_lang_id): void
62  {
63  $this->reason = $a_lang_id;
64  }
65 
66  public function setGotoLangId(string $a_lang_id): void
67  {
68  $this->goto_caption = $a_lang_id;
69  }
70 
71  public function setChangedByUserId(int $a_id): void
72  {
73  $this->changed_by = $a_id;
74  }
75 
79  public function addAdditionalInfo(
80  string $a_lang_id,
81  string $a_value,
82  bool $a_multiline = false,
83  bool $a_lang_direct = false
84  ): void {
85  $this->additional[$a_lang_id] = array(trim($a_value), $a_multiline, $a_lang_direct);
86  }
87 
96  public function sendMailAndReturnRecipients(
97  array $a_user_ids,
98  ?string $a_goto_additional = null,
99  string $a_permission = "read"
100  ): array {
101  $this->all_ref_ids = null;
102 
103  // prepare object related info
104  if ($this->getObjId()) {
105  if (!$this->getRefId()) {
106  // try to find ref_id(s)
107  if (!$this->is_in_wsp) {
108  $ref_ids = ilObject::_getAllReferences($this->getObjId());
109  if (count($ref_ids) === 1) {
110  $this->ref_id = array_shift($ref_ids);
111  } else {
112  $this->all_ref_ids = $ref_ids;
113  }
114  }
115  } elseif ($this->is_in_wsp) { // #11680
116  $this->ref_id = $this->wsp_tree->lookupNodeId($this->getObjId());
117  }
118 
119  // default values
120  if (!$this->goto_caption) {
121  $this->goto_caption = "url";
122  }
123  }
124 
125  $recipient_ids = array();
126  foreach (array_unique($a_user_ids) as $user_id) {
127  // author of change should not get notification
128  if ($this->changed_by === $user_id) {
129  continue;
130  }
131  if ($this->composeAndSendMail($user_id, $a_goto_additional, $a_permission)) {
132  $recipient_ids[] = $user_id;
133  }
134  }
135 
136  return $recipient_ids;
137  }
138 
142  public function compose(
143  int $a_user_id,
144  ?string $a_goto_additional = null,
145  string $a_permission = "read",
146  bool $a_append_signature_direct = false
147  ): bool {
148  $find_ref_id = false;
149  $this->initLanguage($a_user_id);
150  $this->initMail();
151 
152  if ($this->subject_direct) {
153  $this->setSubject($this->subject_direct);
154  } else {
155  $this->setSubject(
156  sprintf($this->getLanguageText($this->subject_lang_id), $this->getObjectTitle(true))
157  );
158  }
159 
160  $this->setBody(ilMail::getSalutation($a_user_id, $this->getLanguage()));
161  $this->appendBody("\n\n");
162 
163  if ($this->introduction) {
164  $this->appendBody($this->getLanguageText($this->introduction));
165  $this->appendBody("\n\n");
166  }
167 
168  if ($this->introduction_direct) {
169  $this->appendBody($this->introduction_direct);
170  $this->appendBody("\n\n");
171  }
172 
173  if ($this->task) {
174  $this->appendBody($this->getLanguageText($this->task));
175  $this->appendBody("\n\n");
176  }
177 
178  // details table
179  if ($this->getObjId()) {
180  $this->appendBody($this->getLanguageText("obj_" . $this->getObjType()) . ": " .
181  $this->getObjectTitle() . "\n");
182  }
183  if (count($this->additional) > 0) {
184  foreach ($this->additional as $lang_id => $item) {
185  $caption = "";
186  if ($lang_id) {
187  $caption = (!$item[2])
188  ? $this->getLanguageText($lang_id)
189  : $lang_id;
190  }
191  if (!$item[1]) {
192  if ($caption) {
193  $caption .= ": ";
194  }
195  $this->appendBody($caption . $item[0] . "\n");
196  } else {
197  if ($caption) {
198  $caption .= "\n";
199  }
200  $this->appendBody("\n" . $caption .
201  $this->getBlockBorder() .
202  $item[0] . "\n" .
203  $this->getBlockBorder() . "\n");
204  }
205  }
206  }
207  $this->body = trim($this->body);
208  $this->appendBody("\n\n");
209 
210  if ($this->changed_by) {
211  $this->appendBody($this->getLanguageText("system_notification_installation_changed_by") . ": " .
212  ilUserUtil::getNamePresentation($this->changed_by));
213  $this->appendBody("\n\n");
214  }
215 
216  if ($this->getObjId()) {
217  // try to find accessible ref_id
218  if (!$this->getRefId() && $this->all_ref_ids) {
219  $find_ref_id = true;
220  foreach ($this->all_ref_ids as $ref_id) {
221  if ($this->isRefIdAccessible($a_user_id, $ref_id, $a_permission)) {
222  $this->ref_id = $ref_id;
223  break;
224  }
225  }
226  }
227 
228  // check if initially given ref_id is accessible for current recipient
229  if ($this->getRefId() &&
230  !$find_ref_id &&
231  !$this->isRefIdAccessible($a_user_id, $this->getRefId(), $a_permission)) {
232  return false;
233  }
234 
235  $goto = $this->createPermanentLink(array(), (string) $a_goto_additional);
236  if ($goto) {
237  $this->appendBody($this->getLanguageText($this->goto_caption) . ": " .
238  $goto);
239  $this->appendBody("\n\n");
240  }
241 
242  if ($find_ref_id) {
243  $this->ref_id = 0;
244  }
245  }
246 
247  if ($this->reason) {
248  $this->appendBody($this->getLanguageText($this->reason));
249  $this->appendBody("\n\n");
250  }
251 
253 
254  // signature will append new lines
255  $this->body = trim($this->body);
256 
257  if (!$a_append_signature_direct) {
258  $this->getMail()->appendInstallationSignature(true);
259  } else {
261  }
262 
263  return true;
264  }
265 
269  protected function composeAndSendMail(
270  int $a_user_id,
271  ?string $a_goto_additional = null,
272  string $a_permission = "read"
273  ): bool {
274  if ($this->compose($a_user_id, $a_goto_additional, $a_permission)) {
275  $this->sendMail(array($a_user_id), is_numeric($a_user_id));
276  return true;
277  }
278  return false;
279  }
280 
284  public function composeAndGetMessage(
285  int $a_user_id,
286  ?string $a_goto_additional = null,
287  string $a_permission = "read",
288  bool $a_append_signature_direct = false
289  ): string {
290  if ($this->compose($a_user_id, $a_goto_additional, $a_permission, $a_append_signature_direct)) {
291  return $this->body;
292  }
293  return "";
294  }
295 }
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:
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)
Base class for course/group mail notifications.
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.
getObjectTitle(bool $a_shorten=false)
createPermanentLink(array $a_params=[], string $a_append='')
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.
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.