Skip to content

Commit 07d13e6

Browse files
authored
Merge pull request #18 from creecros/notifcations
Adds Notifcations
2 parents a72f9b4 + 8fdd2b4 commit 07d13e6

File tree

3 files changed

+223
-3
lines changed

3 files changed

+223
-3
lines changed
+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\Group_assign\Model;
4+
5+
use Kanboard\Model\UserModel;
6+
use Kanboard\Model\GroupMemberModel;
7+
use Kanboard\Plugin\Group_assign\Model\MultiselectMemberModel;
8+
use Kanboard\Core\Base;
9+
10+
/**
11+
* User Notification Filter
12+
*
13+
* @package Kanboard\Plugin\Group_assign
14+
* @author Craig Crosby
15+
*/
16+
class NewUserNotificationFilterModel extends Base
17+
{
18+
/**
19+
* SQL table name
20+
*
21+
* @var string
22+
*/
23+
const PROJECT_TABLE = 'user_has_notifications';
24+
25+
/**
26+
* User filters
27+
*
28+
* @var integer
29+
*/
30+
const FILTER_NONE = 1;
31+
const FILTER_ASSIGNEE = 2;
32+
const FILTER_CREATOR = 3;
33+
const FILTER_BOTH = 4;
34+
35+
/**
36+
* Get the list of filters
37+
*
38+
* @access public
39+
* @return array
40+
*/
41+
public function getFilters()
42+
{
43+
return array(
44+
self::FILTER_NONE => t('All tasks'),
45+
self::FILTER_ASSIGNEE => t('Only for tasks assigned to me'),
46+
self::FILTER_CREATOR => t('Only for tasks created by me'),
47+
self::FILTER_BOTH => t('Only for tasks created by me and tasks assigned to me'),
48+
);
49+
}
50+
51+
/**
52+
* Get user selected filter
53+
*
54+
* @access public
55+
* @param integer $user_id
56+
* @return integer
57+
*/
58+
public function getSelectedFilter($user_id)
59+
{
60+
return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->findOneColumn('notifications_filter');
61+
}
62+
63+
/**
64+
* Save selected filter for a user
65+
*
66+
* @access public
67+
* @param integer $user_id
68+
* @param string $filter
69+
* @return boolean
70+
*/
71+
public function saveFilter($user_id, $filter)
72+
{
73+
return $this->db->table(UserModel::TABLE)->eq('id', $user_id)->update(array(
74+
'notifications_filter' => $filter,
75+
));
76+
}
77+
78+
/**
79+
* Get user selected projects
80+
*
81+
* @access public
82+
* @param integer $user_id
83+
* @return array
84+
*/
85+
public function getSelectedProjects($user_id)
86+
{
87+
return $this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
88+
}
89+
90+
/**
91+
* Save selected projects for a user
92+
*
93+
* @access public
94+
* @param integer $user_id
95+
* @param integer[] $project_ids
96+
* @return boolean
97+
*/
98+
public function saveSelectedProjects($user_id, array $project_ids)
99+
{
100+
$results = array();
101+
$this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->remove();
102+
103+
foreach ($project_ids as $project_id) {
104+
$results[] = $this->db->table(self::PROJECT_TABLE)->insert(array(
105+
'user_id' => $user_id,
106+
'project_id' => $project_id,
107+
));
108+
}
109+
110+
return !in_array(false, $results, true);
111+
}
112+
113+
/**
114+
* Return true if the user should receive notification
115+
*
116+
* @access public
117+
* @param array $user
118+
* @param array $event_data
119+
* @return boolean
120+
*/
121+
public function shouldReceiveNotification(array $user, array $event_data)
122+
{
123+
$filters = array(
124+
'filterNone',
125+
'filterAssignee',
126+
'filterCreator',
127+
'filterBoth',
128+
);
129+
130+
foreach ($filters as $filter) {
131+
if ($this->$filter($user, $event_data)) {
132+
return $this->filterProject($user, $event_data);
133+
}
134+
}
135+
136+
return false;
137+
}
138+
139+
/**
140+
* Return true if the user will receive all notifications
141+
*
142+
* @access public
143+
* @param array $user
144+
* @return boolean
145+
*/
146+
public function filterNone(array $user)
147+
{
148+
return $user['notifications_filter'] == self::FILTER_NONE;
149+
}
150+
151+
/**
152+
* Return true if the user is the assignee and selected the filter "assignee"
153+
*
154+
* @access public
155+
* @param array $user
156+
* @param array $event_data
157+
* @return boolean
158+
*/
159+
public function filterAssignee(array $user, array $event_data)
160+
{
161+
return $user['notifications_filter'] == self::FILTER_ASSIGNEE &&
162+
($event_data['task']['owner_id'] == $user['id'] ||
163+
$this->multiselectMemberModel->isMember($event_data['task']['owner_ms'], $user['id']) ||
164+
$this->groupMemberModel->isMember($event_data['task']['owner_gp'], $user['id']));
165+
}
166+
167+
/**
168+
* Return true if the user is the creator and enabled the filter "creator"
169+
*
170+
* @access public
171+
* @param array $user
172+
* @param array $event_data
173+
* @return boolean
174+
*/
175+
public function filterCreator(array $user, array $event_data)
176+
{
177+
return $user['notifications_filter'] == self::FILTER_CREATOR && $event_data['task']['creator_id'] == $user['id'];
178+
}
179+
180+
/**
181+
* Return true if the user is the assignee or the creator and selected the filter "both"
182+
*
183+
* @access public
184+
* @param array $user
185+
* @param array $event_data
186+
* @return boolean
187+
*/
188+
public function filterBoth(array $user, array $event_data)
189+
{
190+
return $user['notifications_filter'] == self::FILTER_BOTH &&
191+
($event_data['task']['creator_id'] == $user['id'] || $event_data['task']['owner_id'] == $user['id'] ||
192+
$this->multiselectMemberModel->isMember($event_data['task']['owner_ms'], $user['id']) ||
193+
$this->groupMemberModel->isMember($event_data['task']['owner_gp'], $user['id']));
194+
}
195+
196+
/**
197+
* Return true if the user want to receive notification for the selected project
198+
*
199+
* @access public
200+
* @param array $user
201+
* @param array $event_data
202+
* @return boolean
203+
*/
204+
public function filterProject(array $user, array $event_data)
205+
{
206+
$projects = $this->getSelectedProjects($user['id']);
207+
208+
if (! empty($projects)) {
209+
return in_array($event_data['task']['project_id'], $projects);
210+
}
211+
212+
return true;
213+
}
214+
}

Plugin.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Kanboard\Model\TaskModel;
77
use Kanboard\Model\ProjectGroupRoleModel;
88
use Kanboard\Plugin\Group_assign\Model\NewTaskFinderModel;
9+
use Kanboard\Plugin\Group_assign\Model\NewUserNotificationFilterModel;
910
use Kanboard\Plugin\Group_assign\Model\MultiselectModel;
1011
use Kanboard\Plugin\Group_assign\Model\MultiselectMemberModel;
1112
use Kanboard\Plugin\Group_assign\Model\OldTaskFinderModel;
@@ -27,7 +28,11 @@ public function initialize()
2728
{
2829
//Events & Changes
2930
$this->template->setTemplateOverride('task/changes', 'group_assign:task/changes');
30-
31+
32+
//Notifications
33+
$this->container['userNotificationFilterModel'] = $this->container->factory(function ($c) {
34+
return new NewUserNotificationFilterModel($c);
35+
});
3136

3237
//Helpers
3338
$this->helper->register('newTaskHelper', '\Kanboard\Plugin\Group_assign\Helper\NewTaskHelper');
@@ -118,7 +123,7 @@ public function getPluginAuthor()
118123
}
119124
public function getPluginVersion()
120125
{
121-
return '1.2.2';
126+
return '1.3.0';
122127
}
123128
public function getPluginHomepage()
124129
{

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ Kanboard v1.1.0 or Higher
2828
* using ``assignee:GroupName`` in filter will find tasks assigned to a group by NAME of the group.
2929
* using ``assignee:GroupID`` in filter will find tasks assigned to a group by ID number of group.
3030
* using ``assignee:Username`` or ``assignee:Name`` will NOW find a task assigned to a group with that UserName or Name in the group or in Other Assignees.
31+
* user assigneed via a group or multiselect will now recieve notifications
3132

3233
# Future enhancments
3334
Find bugs or missing functionality, please report it.
3435

3536
- [x] Add a few basic automatic actions that utilize Groups assigned
3637
- [x] Add relationship for ``assignee:Username`` or ``assignee:Name`` in the table lookup
3738
- [ ] Add an event for assigned group change.
38-
- [ ] Incorporate into notifications
39+
- [x] Incorporate into notifications
3940

4041
# Screenshots
4142

0 commit comments

Comments
 (0)