Add user filter/condition for notifications

This commit is contained in:
Frederic Guillot
2015-06-07 20:06:31 -04:00
parent 9d9e3afba2
commit 4f32352fe6
15 changed files with 582 additions and 249 deletions

View File

@@ -143,4 +143,23 @@ abstract class Base extends \Core\Base
'url' => $this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])),
);
}
/**
* Group a collection of records by a column
*
* @access public
* @param array $collection
* @param string $column
* @return array
*/
public function groupByColumn(array $collection, $column)
{
$result = array();
foreach ($collection as $item) {
$result[$item[$column]][] = $item;
}
return $result;
}
}

View File

@@ -20,104 +20,314 @@ class Notification extends Base
*/
const TABLE = 'user_has_notifications';
/**
* User filters
*
* @var integer
*/
const FILTER_NONE = 1;
const FILTER_ASSIGNEE = 2;
const FILTER_CREATOR = 3;
const FILTER_BOTH = 4;
/**
* Send overdue tasks
*
* @access public
*/
public function sendOverdueTaskNotifications()
{
$tasks = $this->taskFinder->getOverdueTasks();
$projects = array();
foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) {
// Get the list of users that should receive notifications for each projects
$users = $this->notification->getUsersWithNotificationEnabled($project_id);
foreach ($users as $user) {
$this->sendUserOverdueTaskNotifications($user, $project_tasks);
}
}
return $tasks;
}
/**
* Send overdue tasks for a given user
*
* @access public
* @param array $user
* @param array $tasks
*/
public function sendUserOverdueTaskNotifications(array $user, array $tasks)
{
$user_tasks = array();
foreach ($tasks as $task) {
if ($this->notification->shouldReceiveNotification($user, array('task' => $task))) {
$user_tasks[] = $task;
}
}
if (! empty($user_tasks)) {
$this->sendEmailNotification(
$user,
Task::EVENT_OVERDUE,
array('tasks' => $user_tasks, 'project_name' => $tasks[0]['project_name'])
);
}
}
/**
* Send notifications to people
*
* @access public
* @param string $event_name
* @param array $event_data
*/
public function sendNotifications($event_name, array $event_data)
{
$logged_user_id = $this->userSession->isLogged() ? $this->userSession->getId() : 0;
$users = $this->notification->getUsersWithNotificationEnabled($event_data['task']['project_id'], $logged_user_id);
foreach ($users as $user) {
if ($this->shouldReceiveNotification($user, $event_data)) {
$this->sendEmailNotification($user, $event_name, $event_data);
}
}
// Restore locales
$this->config->setupTranslations();
}
/**
* Send email notification to someone
*
* @access public
* @param array $user User
* @param string $event_name
* @param array $event_data
*/
public function sendEmailNotification(array $user, $event_name, array $event_data)
{
// Use the user language otherwise use the application language (do not use the session language)
if (! empty($user['language'])) {
Translator::load($user['language']);
}
else {
Translator::load($this->config->get('application_language', 'en_US'));
}
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$this->getMailSubject($event_name, $event_data),
$this->getMailContent($event_name, $event_data)
);
}
/**
* Return true if the user should receive notification
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function shouldReceiveNotification(array $user, array $event_data)
{
$filters = array(
'filterNone',
'filterAssignee',
'filterCreator',
'filterBoth',
);
foreach ($filters as $filter) {
if ($this->$filter($user, $event_data)) {
return $this->filterProject($user, $event_data);
}
}
return false;
}
/**
* Return true if the user will receive all notifications
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterNone(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_NONE;
}
/**
* Return true if the user is the assignee and selected the filter "assignee"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterAssignee(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_ASSIGNEE && $event_data['task']['owner_id'] == $user['id'];
}
/**
* Return true if the user is the creator and enabled the filter "creator"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterCreator(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_CREATOR && $event_data['task']['creator_id'] == $user['id'];
}
/**
* Return true if the user is the assignee or the creator and selected the filter "both"
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterBoth(array $user, array $event_data)
{
return $user['notifications_filter'] == self::FILTER_BOTH &&
($event_data['task']['creator_id'] == $user['id'] || $event_data['task']['owner_id'] == $user['id']);
}
/**
* Return true if the user want to receive notification for the selected project
*
* @access public
* @param array $user
* @param array $event_data
* @return boolean
*/
public function filterProject(array $user, array $event_data)
{
$projects = $this->db->table(self::TABLE)->eq('user_id', $user['id'])->findAllByColumn('project_id');
if (! empty($projects)) {
return in_array($event_data['task']['project_id'], $projects);
}
return true;
}
/**
* Get a list of people with notifications enabled
*
* @access public
* @param integer $project_id Project id
* @param array $exclude_users List of user_id to exclude
* @param integer $project_id Project id
* @param array $exclude_user_id User id to exclude
* @return array
*/
public function getUsersWithNotification($project_id, array $exclude_users = array())
public function getUsersWithNotificationEnabled($project_id, $exclude_user_id = 0)
{
if ($this->projectPermission->isEverybodyAllowed($project_id)) {
return $this->db
->table(User::TABLE)
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language')
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
->eq('notifications_enabled', '1')
->neq('email', '')
->notin(User::TABLE.'.id', $exclude_users)
->neq(User::TABLE.'.id', $exclude_user_id)
->findAll();
}
return $this->db
->table(ProjectPermission::TABLE)
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language')
->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name', User::TABLE.'.email', User::TABLE.'.language', User::TABLE.'.notifications_filter')
->join(User::TABLE, 'id', 'user_id')
->eq('project_id', $project_id)
->eq('notifications_enabled', '1')
->neq('email', '')
->notin(User::TABLE.'.id', $exclude_users)
->neq(User::TABLE.'.id', $exclude_user_id)
->findAll();
}
/**
* Get the list of users to send the notification for a given project
* Get the mail content for a given template name
*
* @access public
* @param integer $project_id Project id
* @param array $exclude_users List of user_id to exclude
* @return array
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
*/
public function getUsersList($project_id, array $exclude_users = array())
public function getMailContent($event_name, array $event_data)
{
// Exclude the connected user
if (Session::isOpen() && $this->userSession->isLogged()) {
$exclude_users[] = $this->userSession->getId();
}
$users = $this->getUsersWithNotification($project_id, $exclude_users);
foreach ($users as $index => $user) {
$projects = $this->db->table(self::TABLE)
->eq('user_id', $user['id'])
->findAllByColumn('project_id');
// The user have selected only some projects
if (! empty($projects)) {
// If the user didn't select this project we remove that guy from the list
if (! in_array($project_id, $projects)) {
unset($users[$index]);
}
}
}
return $users;
return $this->template->render(
'notification/'.str_replace('.', '_', $event_name),
$event_data + array('application_url' => $this->config->get('application_url'))
);
}
/**
* Send the email notifications
* Get the mail subject for a given template name
*
* @access public
* @param string $template Template name
* @param array $users List of users
* @param array $data Template data
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
*/
public function sendEmails($template, array $users, array $data)
public function getMailSubject($template, array $data)
{
foreach ($users as $user) {
// Use the user language otherwise use the application language (do not use the session language)
if (! empty($user['language'])) {
Translator::load($user['language']);
}
else {
Translator::load($this->config->get('application_language', 'en_US'));
}
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$this->getMailSubject($template, $data),
$this->getMailContent($template, $data)
);
switch ($template) {
case Task::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New attachment'), $data);
break;
case Comment::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New comment'), $data);
break;
case Comment::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Comment updated'), $data);
break;
case Subtask::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New subtask'), $data);
break;
case Subtask::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Subtask updated'), $data);
break;
case Task::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New task'), $data);
break;
case Task::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Task updated'), $data);
break;
case Task::EVENT_CLOSE:
$subject = $this->getStandardMailSubject(e('Task closed'), $data);
break;
case Task::EVENT_OPEN:
$subject = $this->getStandardMailSubject(e('Task opened'), $data);
break;
case Task::EVENT_MOVE_COLUMN:
$subject = $this->getStandardMailSubject(e('Column Change'), $data);
break;
case Task::EVENT_MOVE_POSITION:
$subject = $this->getStandardMailSubject(e('Position Change'), $data);
break;
case Task::EVENT_ASSIGNEE_CHANGE:
$subject = $this->getStandardMailSubject(e('Assignee Change'), $data);
break;
case Task::EVENT_OVERDUE:
$subject = e('[%s] Overdue tasks', $data['project_name']);
break;
default:
$subject = e('Notification');
}
// Restore locales
$this->config->setupTranslations();
return $subject;
}
/**
@@ -126,83 +336,13 @@ class Notification extends Base
* @access private
* @param string $label Label
* @param array $data Template data
* @return string
*/
private function getStandardMailSubject($label, array $data)
{
return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']);
}
/**
* Get the mail subject for a given template name
*
* @access public
* @param string $template Template name
* @param array $data Template data
*/
public function getMailSubject($template, array $data)
{
switch ($template) {
case 'file_creation':
$subject = $this->getStandardMailSubject(e('New attachment'), $data);
break;
case 'comment_creation':
$subject = $this->getStandardMailSubject(e('New comment'), $data);
break;
case 'comment_update':
$subject = $this->getStandardMailSubject(e('Comment updated'), $data);
break;
case 'subtask_creation':
$subject = $this->getStandardMailSubject(e('New subtask'), $data);
break;
case 'subtask_update':
$subject = $this->getStandardMailSubject(e('Subtask updated'), $data);
break;
case 'task_creation':
$subject = $this->getStandardMailSubject(e('New task'), $data);
break;
case 'task_update':
$subject = $this->getStandardMailSubject(e('Task updated'), $data);
break;
case 'task_close':
$subject = $this->getStandardMailSubject(e('Task closed'), $data);
break;
case 'task_open':
$subject = $this->getStandardMailSubject(e('Task opened'), $data);
break;
case 'task_move_column':
$subject = $this->getStandardMailSubject(e('Column Change'), $data);
break;
case 'task_move_position':
$subject = $this->getStandardMailSubject(e('Position Change'), $data);
break;
case 'task_assignee_change':
$subject = $this->getStandardMailSubject(e('Assignee Change'), $data);
break;
case 'task_due':
$subject = e('[%s][Due tasks]', $data['project']);
break;
default:
$subject = e('[Kanboard] Notification');
}
return $subject;
}
/**
* Get the mail content for a given template name
*
* @access public
* @param string $template Template name
* @param array $data Template data
*/
public function getMailContent($template, array $data)
{
return $this->template->render(
'notification/'.$template,
$data + array('application_url' => $this->config->get('application_url'))
);
}
/**
* Save settings for the given user
*
@@ -219,7 +359,8 @@ class Notification extends Base
// Activate notifications
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
'notifications_enabled' => '1'
'notifications_enabled' => '1',
'notifications_filter' => empty($values['notifications_filter']) ? self::FILTER_BOTH : $values['notifications_filter'],
));
// Save selected projects
@@ -251,9 +392,7 @@ class Notification extends Base
*/
public function readSettings($user_id)
{
$values = array();
$values['notifications_enabled'] = $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('notifications_enabled');
$values = $this->db->table(User::TABLE)->eq('id', $user_id)->columns('notifications_enabled', 'notifications_filter')->findOne();
$projects = $this->db->table(self::TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
foreach ($projects as $project_id) {

View File

@@ -105,7 +105,8 @@ class SubtaskTimeTracking extends Base
->join(Subtask::TABLE, 'id', 'subtask_id')
->join(Task::TABLE, 'id', 'task_id', Subtask::TABLE)
->join(User::TABLE, 'id', 'user_id', self::TABLE)
->eq(Task::TABLE.'.project_id', $project_id);
->eq(Task::TABLE.'.project_id', $project_id)
->asc(self::TABLE.'.id');
}
/**

View File

@@ -40,6 +40,7 @@ class Task extends Base
const EVENT_OPEN = 'task.open';
const EVENT_CREATE_UPDATE = 'task.create_update';
const EVENT_ASSIGNEE_CHANGE = 'task.assignee_change';
const EVENT_OVERDUE = 'task.overdue';
/**
* Recurrence: status

View File

@@ -168,6 +168,8 @@ class TaskFinder extends Base
Task::TABLE.'.title',
Task::TABLE.'.date_due',
Task::TABLE.'.project_id',
Task::TABLE.'.creator_id',
Task::TABLE.'.owner_id',
Project::TABLE.'.name AS project_name',
User::TABLE.'.username AS assignee_username',
User::TABLE.'.name AS assignee_name'