ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilSystemNotification.php
Go to the documentation of this file.
1<?php
2
34{
35 protected string $subject_lang_id = "";
36 protected string $subject_direct = "";
37 protected string $introduction = "";
38 protected string $introduction_direct = "";
39 protected string $task = "";
40 protected string $reason = "";
42 protected array $additional = [];
43 protected string $goto_caption = "";
44 protected int $changed_by = 0;
45 protected ?array $all_ref_ids = [];
46
47 public function __construct(protected bool $is_in_wsp = false)
48 {
49 parent::__construct($is_in_wsp);
50 $this->ref_id = 0;
51 }
52
53 public function setSubjectLangId(string $a_lang_id): void
54 {
55 $this->subject_lang_id = $a_lang_id;
56 }
57
58 public function setSubjectDirect(string $a_text): void
59 {
60 $this->subject_direct = trim($a_text);
61 }
62
63 public function setIntroductionLangId(string $a_lang_id): void
64 {
65 $this->introduction = $a_lang_id;
66 }
67
68 public function setIntroductionDirect(string $a_text): void
69 {
70 $this->introduction_direct = trim($a_text);
71 }
72
73 public function setTaskLangId(string $a_lang_id): void
74 {
75 $this->task = $a_lang_id;
76 }
77
78 public function setReasonLangId(string $a_lang_id): void
79 {
80 $this->reason = $a_lang_id;
81 }
82
83 public function setGotoLangId(string $a_lang_id): void
84 {
85 $this->goto_caption = $a_lang_id;
86 }
87
88 public function setChangedByUserId(int $a_id): void
89 {
90 $this->changed_by = $a_id;
91 }
92
96 public function addAdditionalInfo(
97 string $a_lang_id,
98 string $a_value,
99 bool $a_multiline = false,
100 bool $a_lang_direct = false
101 ): void {
102 $this->additional[] = [
103 'caption' => $a_lang_id,
104 'content' => trim($a_value),
105 'is_direct_translation' => $a_lang_direct,
106 'is_multiline' => $a_multiline
107 ];
108 }
109
119 array $a_user_ids,
120 ?string $a_goto_additional = null,
121 string $a_permission = "read"
122 ): array {
123 $this->all_ref_ids = null;
124
125 // prepare object related info
126 if ($this->getObjId()) {
127 if (!$this->getRefId()) {
128 // try to find ref_id(s)
129 if (!$this->is_in_wsp) {
130 $ref_ids = ilObject::_getAllReferences($this->getObjId());
131 if (count($ref_ids) === 1) {
132 $this->ref_id = array_shift($ref_ids);
133 } else {
134 $this->all_ref_ids = $ref_ids;
135 }
136 }
137 } elseif ($this->is_in_wsp) { // #11680
138 $this->ref_id = $this->wsp_tree->lookupNodeId($this->getObjId());
139 }
140
141 // default values
142 if (!$this->goto_caption) {
143 $this->goto_caption = "url";
144 }
145 }
146
147 $recipient_ids = array();
148 foreach (array_unique($a_user_ids) as $user_id) {
149 // author of change should not get notification
150 if ($this->changed_by === $user_id) {
151 continue;
152 }
153 if ($this->composeAndSendMail($user_id, $a_goto_additional, $a_permission)) {
154 $recipient_ids[] = $user_id;
155 }
156 }
157
158 return $recipient_ids;
159 }
160
164 public function compose(
165 int $a_user_id,
166 ?string $a_goto_additional = null,
167 string $a_permission = "read",
168 bool $a_append_signature_direct = false
169 ): bool {
170 $find_ref_id = false;
171 $this->initLanguage($a_user_id);
172 $this->initMail();
173 if ($this->subject_direct) {
174 $this->setSubject($this->subject_direct);
175 } else {
176 $this->setSubject(
177 sprintf($this->getLanguageText($this->subject_lang_id), $this->getObjectTitle(true))
178 );
179 }
180
181 $this->setBody(ilMail::getSalutation($a_user_id, $this->getLanguage()));
182 $this->appendBody("\n\n");
183
184 if ($this->introduction) {
185 $this->appendBody($this->getLanguageText($this->introduction));
186 $this->appendBody("\n\n");
187 }
188
189 if ($this->introduction_direct) {
190 $this->appendBody($this->introduction_direct);
191 $this->appendBody("\n\n");
192 }
193
194 if ($this->task) {
195 $this->appendBody($this->getLanguageText($this->task));
196 $this->appendBody("\n\n");
197 }
198
199 // details table
200 if ($this->getObjId()) {
201 $this->appendBody($this->getLanguageText("obj_" . $this->getObjType()) . ": " .
202 $this->getObjectTitle() . "\n");
203 }
204
205 if (!empty($this->additional)) {
206 $num_addtional_sections = 0;
207 $render_opening_block_border = true;
208
210 foreach ($this->additional as $item) {
211 if ($num_addtional_sections > 0) {
212 $this->appendBody("\n");
213 }
214
215 $caption = $item['caption'];
216 if ($caption !== '') {
217 $caption = $item['is_direct_translation']
218 ? $caption
219 : $this->getLanguageText($caption);
220 }
221
222 if ($item['is_multiline']) {
223 if ($caption !== '') {
224 $this->appendBody($caption);
225 $this->appendBody("\n");
226 // a) If we render a new caption, we ensure a new opening block border is rendered
227 $render_opening_block_border = true;
228 }
229
230 if ($render_opening_block_border) {
231 $this->appendBody($this->getBlockBorder());
232 }
233
234 $this->appendBody("\n");
235 $this->appendBody($item['content']);
236 $this->appendBody("\n");
237 $this->appendBody($this->getBlockBorder());
238
239 // b) We rendered a closing block border, the next block does not need an opening block border
240 $render_opening_block_border = false;
241 } else {
242 if ($caption) {
243 $caption .= ': ';
244 }
245
246 $this->appendBody($caption . $item['content']);
247
248 // c) If a single line was rendered, the next block needs an opening border
249 $render_opening_block_border = true;
250 }
251
252 ++$num_addtional_sections;
253 }
254 }
255 $this->body = trim($this->body);
256 $this->appendBody("\n\n");
257
258 if ($this->changed_by) {
259 $this->appendBody($this->getLanguageText("system_notification_installation_changed_by") . ": " .
260 ilUserUtil::getNamePresentation($this->changed_by));
261 $this->appendBody("\n\n");
262 }
263
264 if ($this->getObjId()) {
265 // try to find accessible ref_id
266 if (!$this->getRefId() && $this->all_ref_ids) {
267 $find_ref_id = true;
268 foreach ($this->all_ref_ids as $ref_id) {
269 if ($this->isRefIdAccessible($a_user_id, $ref_id, $a_permission)) {
270 $this->ref_id = $ref_id;
271 break;
272 }
273 }
274 }
275
276 // check if initially given ref_id is accessible for current recipient
277 if ($this->getRefId() &&
278 !$find_ref_id &&
279 !$this->isRefIdAccessible($a_user_id, $this->getRefId(), $a_permission)) {
280 return false;
281 }
282
283 $goto = $this->createPermanentLink(array(), (string) $a_goto_additional);
284 if ($goto) {
285 $this->appendBody($this->getLanguageText($this->goto_caption) . ": " .
286 $goto);
287 $this->appendBody("\n\n");
288 }
289
290 if ($find_ref_id) {
291 $this->ref_id = 0;
292 }
293 }
294
295 if ($this->reason) {
296 $this->appendBody($this->getLanguageText($this->reason));
297 $this->appendBody("\n\n");
298 }
299
300 $this->appendBody(ilMail::_getAutoGeneratedMessageString($this->language));
301
302 // signature will append new lines
303 $this->body = trim($this->body);
304
305 if (!$a_append_signature_direct) {
306 $this->getMail()->appendInstallationSignature(true);
307 } else {
308 $this->appendBody(ilMail::_getInstallationSignature());
309 }
310
311 return true;
312 }
313
317 protected function composeAndSendMail(
318 int $a_user_id,
319 ?string $a_goto_additional = null,
320 string $a_permission = "read"
321 ): bool {
322 if ($this->compose($a_user_id, $a_goto_additional, $a_permission)) {
323 $this->sendMail(array($a_user_id), is_numeric($a_user_id));
324 return true;
325 }
326 return false;
327 }
328
332 public function composeAndGetMessage(
333 int $a_user_id,
334 ?string $a_goto_additional = null,
335 string $a_permission = "read",
336 bool $a_append_signature_direct = false
337 ): string {
338 if ($this->compose($a_user_id, $a_goto_additional, $a_permission, $a_append_signature_direct)) {
339 return $this->body;
340 }
341 return "";
342 }
343}
static getSalutation(int $a_usr_id, ?ilLanguage $a_language=null)
static _getInstallationSignature()
static _getAutoGeneratedMessageString(?ilLanguage $lang=null)
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setIntroductionLangId(string $a_lang_id)
setSubjectLangId(string $a_lang_id)
sendMailAndReturnRecipients(array $a_user_ids, ?string $a_goto_additional=null, string $a_permission="read")
Send notification(s)
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.
composeAndSendMail(int $a_user_id, ?string $a_goto_additional=null, string $a_permission="read")
Send notification to single recipient.
setReasonLangId(string $a_lang_id)
addAdditionalInfo(string $a_lang_id, string $a_value, bool $a_multiline=false, bool $a_lang_direct=false)
Add additional information.
__construct(protected bool $is_in_wsp=false)
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=null)
Default behaviour is:
$ref_id
Definition: ltiauth.php:66
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
if(!file_exists('../ilias.ini.php'))
getLanguage()