ILIAS  release_8 Revision v8.24
class.ilCalendarUserNotification.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5
10{
11 public const TYPE_USER = 1;
12 public const TYPE_EMAIL = 2;
13
14 private int $cal_id = 0;
15 private array $rcps = array();
16
17 protected ilLanguage $lng;
18 protected ilDBInterface $db;
20
21 public function __construct(int $a_cal_id = 0)
22 {
23 global $DIC;
24
25 $this->lng = $DIC->language();
26 $this->db = $DIC->database();
27 $this->error = $DIC['ilErr'];
28
29 $this->cal_id = $a_cal_id;
30 $this->read();
31 }
32
33 public static function deleteUser(int $a_usr_id): void
34 {
35 global $DIC;
36
37 $ilDB = $DIC['ilDB'];
38 $query = 'DELETE FROM cal_notification ' .
39 'WHERE user_id = ' . $ilDB->quote($a_usr_id, 'integer');
40 $res = $ilDB->manipulate($query);
41 }
42
43 public static function deleteCalendarEntry(int $a_cal_id): void
44 {
45 global $DIC;
46
47 $ilDB = $DIC['ilDB'];
48 $query = 'DELETE FROM cal_notification ' .
49 'WHERE cal_id = ' . $ilDB->quote($a_cal_id, 'integer');
50 $res = $ilDB->manipulate($query);
51 }
52
53 public function setEntryId(int $a_id): void
54 {
55 $this->cal_id = $a_id;
56 }
57
58 public function getEntryId(): int
59 {
60 return $this->cal_id;
61 }
62
63 public function getRecipients(): array
64 {
65 return $this->rcps;
66 }
67
68 public function validate(): bool
69 {
70 if (!count($this->getRecipients())) {
71 return true;
72 }
73 foreach ($this->getRecipients() as $rcp_data) {
74 if ($rcp_data['type'] == self::TYPE_USER) {
75 continue;
76 } elseif (!ilUtil::is_email($rcp_data['email'])) {
77 $this->error->appendMessage($this->lng->txt('cal_err_invalid_notification_rcps'));
78 return false;
79 }
80 }
81 return true;
82 }
83
84 public function save(): bool
85 {
86 $this->deleteRecipients();
87 foreach ($this->getRecipients() as $rcp) {
88 $query = 'INSERT INTO cal_notification ' .
89 '(notification_id,cal_id, user_type, user_id, email) ' .
90 'VALUES ( ' .
91 $this->db->quote($this->db->nextId('cal_notification'), 'integer') . ', ' .
92 $this->db->quote($this->getEntryId(), 'integer') . ', ' .
93 $this->db->quote((int) $rcp['type'], 'integer') . ', ' .
94 $this->db->quote((int) $rcp['usr_id'], 'integer') . ', ' .
95 $this->db->quote($rcp['email'], 'text') .
96 ')';
97 $this->db->manipulate($query);
98 }
99 return true;
100 }
101
102 public function addRecipient(int $a_type, int $a_usr_id = 0, string $a_email = ''): void
103 {
104 $this->rcps[] = array(
105 'type' => $a_type,
106 'usr_id' => $a_usr_id,
107 'email' => $a_email
108 );
109 }
110
111 public function setRecipients(array $a_rcps): void
112 {
113 $this->rcps = array();
114 }
115
116 public function deleteRecipients(): void
117 {
118 $query = 'DELETE FROM cal_notification ' .
119 'WHERE cal_id = ' . $this->db->quote($this->getEntryId(), 'integer');
120 $res = $this->db->manipulate($query);
121 }
122
123 protected function read(): void
124 {
125 if (!$this->getEntryId()) {
126 return;
127 }
128
129 $query = 'SELECT * FROM cal_notification ' .
130 'WHERE cal_id = ' . $this->db->quote($this->getEntryId(), 'integer');
131 $res = $this->db->query($query);
132
133 $this->rcps = array();
134 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
135 $this->addRecipient(
136 (int) $row->user_type,
137 (int) $row->user_id,
138 $row->email
139 );
140 }
141 }
142
143 public static function createTable(): void
144 {
145 global $DIC;
146
147 $ilDB = $DIC['ilDB'];
148 if ($ilDB->tableExists('cal_notification')) {
149 return;
150 }
151
152 // Create notification table
153 $ilDB->createTable(
154 'cal_notification',
155 array(
156 'notification_id' => array('type' => 'integer', 'length' => 4, 'notnull' => true),
157 'cal_id' => array('type' => 'integer', 'length' => 4, 'notnull' => true, 'default' => 0),
158 'user_type' => array('type' => 'integer', 'length' => 1, 'notnull' => true, 'default' => 0),
159 'user_id' => array('type' => 'integer', 'length' => 4, 'notnull' => true, 'default' => 0),
160 'email' => array('type' => 'text', 'length' => 64, 'notnull' => false)
161 )
162 );
163 $ilDB->addPrimaryKey(
164 'cal_notification',
165 array(
166 'notification_id'
167 )
168 );
169 $ilDB->createSequence('cal_notification');
170 $ilDB->addIndex('cal_notification', array('cal_id'), 'i1');
171 }
172}
error(string $a_errmsg)
addRecipient(int $a_type, int $a_usr_id=0, string $a_email='')
Error Handling & global info handling uses PEAR error class.
language handling
static is_email(string $a_email, ilMailRfc822AddressParserFactory $mailAddressParserFactory=null)
This preg-based function checks whether an e-mail address is formally valid.
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query