ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilMailSummaryNotification.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  protected ilLanguage $lng;
24  protected ilDBInterface $db;
25  protected ilSetting $settings;
27 
28  public function __construct(bool $a_is_personal_workspace = false)
29  {
30  global $DIC;
31  $this->db = $DIC->database();
32  $this->lng = $DIC->language();
33  $this->settings = $DIC->settings();
34  $this->sender_factory = $DIC->mail()->mime()->senderFactory();
35 
36  parent::__construct($a_is_personal_workspace);
37  }
38 
39  public function send(): void
40  {
41  $is_message_enabled = (bool) $this->settings->get('mail_notification_message', '0');
42 
43  $res = $this->db->queryF(
44  'SELECT mail.* FROM mail_options
45  INNER JOIN mail ON mail.user_id = mail_options.user_id
46  INNER JOIN mail_obj_data ON mail_obj_data.obj_id = mail.folder_id
47  INNER JOIN usr_data ud ON ud.usr_id = mail.user_id
48  WHERE mail_options.cronjob_notification = %s
49  AND mail.send_time >= %s
50  AND mail.m_status = %s
51  AND ud.active = %s',
52  ['integer', 'timestamp', 'text', 'integer'],
53  [1, date('Y-m-d H:i:s', time() - 60 * 60 * 24), 'unread', 1]
54  );
55 
56  $users = [];
57  $user_id = 0;
58 
59  while ($row = $this->db->fetchAssoc($res)) {
60  if ($user_id === 0 || (int) $row['user_id'] !== $user_id) {
61  $user_id = (int) $row['user_id'];
62  }
63  $users[$user_id][] = $row;
64  }
65  $sender = $this->sender_factory->system();
66 
67  foreach ($users as $user_id => $mail_data) {
68  $this->initLanguage($user_id);
69  $user_lang = $this->getLanguage();
70 
71  $this->initMail();
72 
73  $this->setRecipients([$user_id]);
74  $this->setSubject($this->getLanguageText('mail_notification_subject'));
75 
76  $this->setBody(ilMail::getSalutation($user_id, $user_lang));
77  $this->appendBody("\n\n");
78  if (count($mail_data) === 1) {
79  $this->appendBody(sprintf(
80  $user_lang->txt('mail_at_the_ilias_installation'),
81  count($mail_data),
83  ));
84  } else {
85  $this->appendBody(sprintf(
86  $user_lang->txt('mails_at_the_ilias_installation'),
87  count($mail_data),
89  ));
90  }
91  $this->appendBody("\n\n");
92 
93  $counter = 1;
94  foreach ($mail_data as $mail) {
95  $this->appendBody(
96  '----------------------------------------------------------------------------------------------'
97  );
98  $this->appendBody("\n\n");
99  $this->appendBody('#' . $counter . "\n\n");
100  $this->appendBody($user_lang->txt('date') . ': ' . $mail['send_time']);
101  $this->appendBody("\n");
102  if ((int) $mail['sender_id'] === ANONYMOUS_USER_ID) {
103  $sender_name = ilMail::_getIliasMailerName();
104  } else {
105  $sender_name = ilObjUser::_lookupLogin((int) $mail['sender_id']);
106  }
107  $this->appendBody($user_lang->txt('sender') . ': ' . $sender_name);
108  $this->appendBody("\n");
109  $this->appendBody($user_lang->txt('subject') . ': ' . $mail['m_subject']);
110  $this->appendBody("\n\n");
111 
112  if ($is_message_enabled) {
113  $this->appendBody($user_lang->txt('message') . ': ' . $mail['m_message']);
114  $this->appendBody("\n\n");
115  }
116  ++$counter;
117  }
118  $this->appendBody(
119  '----------------------------------------------------------------------------------------------'
120  );
121  $this->appendBody("\n\n");
122  $this->appendBody($user_lang->txt('follow_link_to_read_mails') . ' ');
123  $this->appendBody("\n");
124  $mailbox_link = rtrim(ilUtil::_getHttpPath(), '/');
125  $mailbox_link .= '/goto.php?target=mail&client_id=' . CLIENT_ID;
126 
127  $this->appendBody($mailbox_link);
128  $this->appendBody("\n\n");
131 
132  $mmail = new ilMimeMail();
133  $mmail->From($sender);
134 
135  $mail_options = new ilMailOptions($user_id);
136  $mmail->To($mail_options->getExternalEmailAddresses());
137 
138  $mmail->Subject($this->getSubject());
139  $mmail->Body($this->getBody());
140  $mmail->Send();
141  }
142  }
143 }
$res
Definition: ltiservices.php:66
const ANONYMOUS_USER_ID
Definition: constants.php:27
static _getIliasMailerName()
static _getAutoGeneratedMessageString(?ilLanguage $lang=null)
__construct(bool $a_is_personal_workspace=false)
static getSalutation(int $a_usr_id, ?ilLanguage $a_language=null)
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:26
getLanguageText(string $a_keyword)
static _getHttpPath()
__construct(Container $dic, ilPlugin $plugin)
setSubject(string $a_subject)
static _getInstallationSignature()
static _lookupLogin(int $a_user_id)