2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
25 * This class determines which emails need to be sent out based on user
26 * options and bug changes, and then it sends said emails.
28 * @author Iris Studios, Inc.
29 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
34 class NotificationCenter
44 * Global bugsys registry
51 * Role list: a list of user IDs with their relations to the bug
56 '-notapplicable-' => array(),
57 'reporter' => array(),
58 'assignee' => array(),
59 'favourite' => array(),
61 'commenter' => array()
72 * A list of notices per-user that are combined together in NotificationCenter::finalize()
76 var $notices = array();
78 // ###################################################################
80 * Constructor: set database objects
84 function __construct()
88 $this->registry
=& $bugsys;
91 // ###################################################################
97 function NotificationCenter()
102 // ###################################################################
104 * Fetches all the users who could be related to the bug and sticks
105 * their information into an array.
109 function fetch_user_cache()
111 $favourites = $this->registry
->db
->query("SELECT userid FROM " . TABLE_PREFIX
. "favourite WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
112 while ($fav = $this->registry
->db
->fetch_array($favourites))
114 $this->roles
['favourite']["$fav[userid]"] = $fav['userid'];
117 $voters = $this->registry
->db
->query_first("SELECT userids FROM " . TABLE_PREFIX
. "vote WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
118 $this->roles
['voter'] = explode(',', $voters['userids']);
120 $commenters = $this->registry
->db
->query("SELECT userid FROM " . TABLE_PREFIX
. "comment WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
121 while ($comment = $this->registry
->db
->fetch_array($commenters))
123 $this->roles
['commenter']["$comment[userid]"] = $comment['userid'];
126 $masterids = array_merge($this->roles
['-notapplicable-'], $this->roles
['reporter'], $this->roles
['assignee'], $this->roles
['favourite'], $this->roles
['voter'], $this->roles
['commenter']);
127 $masterids = array_unique($masterids);
129 $userinfo = $this->registry
->db
->query("
130 SELECT user.*, useremail.*
131 FROM " . TABLE_PREFIX
. "useremail AS useremail
132 LEFT JOIN " . TABLE_PREFIX
. "user AS user
133 ON (user.userid = useremail.userid)
134 WHERE useremail.userid IN (" . implode(',', $masterids) . ")
136 while ($user = $this->registry
->db
->fetch_array($userinfo))
138 if (!is_array($this->users
["$user[userid]"]))
140 $this->users
["$user[userid]"] = $user;
141 unset($this->users
["$user[userid]"]['mask'], $this->users
["$user[userid]"]['relation']);
143 $this->users
["$user[userid]"]['options']["$user[relation]"] = $user['mask'];
147 // ###################################################################
149 * Sends the appropriate emails for changes to bugs. This function
150 * works a lot like the Logging class by taking BugAPI->objdata and
151 * BugAPI->values and then comparing the two arries and sending emails
152 * with the differences.
156 * @param array Original data (BugAPI->objdata)
157 * @param array Modified data (BugAPI->values)
159 function send_bug_changes_notice($original, $modified)
161 $this->bug
= $modified;
163 $this->roles
['-notapplicable-'] = array($original['assignedto'], $modified['assignedto']);
164 $this->roles
['reporter'] = array($original['userid']);
165 $this->roles
['assignee'] = array($modified['assignedto']);
167 $this->fetch_user_cache();
169 if ($original['assignedto'] != $modified['assignedto'])
171 if ($original['assignedto'] != '')
173 $this->notice_no_longer_assigned($original['assignedto']);
175 if ($modified['assignedto'] != '')
177 $this->notice_now_assigned($modified['assignedto']);
181 if ($original['status'] != $modified['status'])
183 $this->notice_status_change($original['status'], $modified['status']);
185 if ($original['resolution'] != $modified['resoultion'])
187 $this->notice_resolution_change($original['resolution'], $modified['resolution']);
190 if ($original['duplicates'] != $modified['duplicates'])
192 $this->notice_duplicates_change($original['duplicates'], $modified['duplicates']);
196 // ###################################################################
198 * Sends an email to the specified user ID that they are no longer the
199 * person assigned to the bug.
203 * @param integer User ID to send to
205 function notice_no_longer_assigned($userid)
207 if ($this->users
["$userid"]['options'][0] & $this->registry
->emailoptions
['notifications']['assignedto'] AND in_array($userid, $this->roles
['-notapplicable-']))
209 $this->notices
["$userid"][] = sprintf(
210 $this->registry
->lang
->string('You are no longer assigned to this bug, per %1$s\'s changes.'),
212 construct_user_display($this->registry
->userinfo
, false
)
217 // ###################################################################
219 * Informs the user that they have been made the assignee of the bug.
223 * @param integer User ID
225 function notice_now_assigned($userid)
227 if ($this->users
["$userid"]['options'][0] & $this->registry
->emailoptions
['notifications']['assignedto'] AND in_array($userid, $this->roles
['-notapplicable-']))
229 $this->notices
["$userid"][] = sprintf(
230 $this->registry
->lang
->string('You have been assigned to this bug by %1$s.'),
232 construct_user_display($this->registry
->userinfo
, false
)
237 // ###################################################################
239 * Sends a message to inform users that the status has changed.
243 * @param integer Old status
244 * @param integer New status
246 function notice_status_change($old, $new)
248 $userlist = $this->fetch_users_with_on_bit('statusresolve');
249 foreach ($userlist AS $userid => $user)
251 $this->notices
["$user[userid]"][] = sprintf(
252 $this->registry
->lang
->string('The status field has changed from "%1$s" to "%2$s".'),
254 $this->registry
->datastore
['status']["$old"]['status'],
255 $this->registry
->datastore
['status']["$new"]['status']
260 // ###################################################################
262 * Sends an email to inform users that the resolution has changed.
266 * @param integer Old resolution
267 * @param integer New resolution
269 function notice_resolution_change($old, $new)
271 $userlist = $this->fetch_users_with_on_bit('statusresolve');
272 foreach ($userlist AS $userid => $user)
274 $this->notices
["$user[userid]"][] = sprintf(
275 $this->registry
->lang
->string('The resolution field has changed from "%1$s" to "%2$s".'),
277 $this->registry
->datastore
['resolution']["$old"]['resolution'],
278 $this->registry
->datastore
['resolution']["$new"]['resolution']
283 // ###################################################################
285 * Informs users that the duplicates list has changed.
289 * @param string Old duplicates list
290 * @param string New duplicates list
292 function notice_duplicates_change($old, $new)
297 // ###################################################################
299 * Sends the appropriate users information about a new comment being
300 * posted to the bug report.
304 * @param array CommentAPI->values array
306 function send_new_comment_notice($comment)
311 // ###################################################################
313 * Generates an array of users who have a given email notification flag
314 * turned on in their bitfields.
318 * @param string Notification bitfield name
320 * @return array Array of users and their data
322 function fetch_users_with_on_bit($bitname)
326 foreach ($this->users
AS $user)
328 foreach ($this->registry
->emailoptions
['relations'] AS $name => $bit)
330 if (in_array($user['userid'], $this->roles
["$name"]) AND $user['options']["$bit"] & $this->registry
->emailoptions
['notifications']["$bitname"])
332 $idlist[] = $user['userid'];
337 $masters = array_unique($idlist);
340 foreach ($masters AS $userid)
342 $return["$userid"] =& $this->users
["$userid"];
349 /*=====================================================================*\
350 || ###################################################################
353 || ###################################################################
354 \*=====================================================================*/