Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awards: Add user notification #1177

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion application/modules/awards/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Config extends \Ilch\Config\Install
{
public $config = [
'key' => 'awards',
'version' => '1.11.1',
'version' => '1.12.0',
'icon_small' => 'fa-solid fa-trophy',
'author' => 'Veldscholten, Kevin',
'link' => 'https://ilch.de',
Expand All @@ -32,12 +32,18 @@ class Config extends \Ilch\Config\Install
public function install()
{
$this->db()->queryMulti($this->getInstallSql());

$databaseConfig = new \Ilch\Config\Database($this->db());
$databaseConfig->set('awards_userNotification', 1);
}

public function uninstall()
{
$this->db()->queryMulti('DROP TABLE `[prefix]_awards_recipients` IF EXISTS;
DROP TABLE `[prefix]_awards` IF EXISTS;');

$databaseConfig = new \Ilch\Config\Database($this->db());
$databaseConfig->delete('awards_userNotification');
}

public function getInstallSql()
Expand Down Expand Up @@ -110,6 +116,10 @@ public function getUpdate($installedVersion)
$this->db()->query('ALTER TABLE `[prefix]_awards` DROP COLUMN `ut_id`, DROP COLUMN `typ`');
case '1.10.0':
case '1.10.1':
case '1.11.0':
// Enable notification of users if they receive an award by default.
$databaseConfig = new \Ilch\Config\Database($this->db());
$databaseConfig->set('awards_userNotification', 1);
}
}
}
48 changes: 48 additions & 0 deletions application/modules/awards/controllers/admin/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Modules\Awards\Mappers\Recipients as RecipientsMapper;
use Modules\Awards\Models\Recipient as RecipientModel;
use Modules\User\Mappers\User as UserMapper;
use Modules\User\Mappers\Notifications as NotificationsMapper;
use Modules\User\Models\Notification as NotificationModel;
use Modules\Teams\Mappers\Teams as TeamsMapper;
use Ilch\Validation;

Expand All @@ -30,6 +32,12 @@ public function init()
'icon' => 'fa-solid fa-circle-plus',
'url' => $this->getLayout()->getUrl(['controller' => 'index', 'action' => 'treat'])
]
],
[
'name' => 'settings',
'active' => false,
'icon' => 'fa-solid fa-gears',
'url' => $this->getLayout()->getUrl(['controller' => 'settings', 'action' => 'index'])
]
];

Expand Down Expand Up @@ -171,6 +179,46 @@ public function treatAction()
}
$recipientMapper->saveMulti($recipientModels);

// Notify the recipient of the award if this is enabled. Don't send a notification if this post is from editing an award.
if ($this->getConfig()->get('awards_userNotification') && !$this->getRequest()->getParam('id')) {
$notificationsMapper = new NotificationsMapper();
$notificationModels = [];

foreach($recipientModels as $recipientModel) {
$users = [];
if ($recipientModel->getTyp() === 2) {
// The recipient is a team.
if ($awardsMapper->existsTable('teams')) {
$teamsMapper = new TeamsMapper();
$team = $teamsMapper->getTeamById($recipientModel->getUtId());
$users = $userMapper->getUserListByGroupId($team->getGroupId(), 1);
}
} else {
// The recipient is a user.
$notificationModel = new NotificationModel();
$notificationModel->setUserId($recipientModel->getUtId())
->setModule('awards')
->setMessage($this->getTranslator()->trans('awardReceived'))
->setURL($this->getLayout()->getUrl(['module' => 'awards', 'controller' => 'index', 'action' => 'show', 'id' => $recipientModel->getAwardId()], ''))
->setType('awardReceived');
$notificationModels[] = $notificationModel;
}

// Notify all users of the team.
foreach ($users as $user) {
$notificationModel = new NotificationModel();
$notificationModel->setUserId($user->getId())
->setModule('awards')
->setMessage($this->getTranslator()->trans('awardReceivedAsTeam'))
->setURL($this->getLayout()->getUrl(['module' => 'awards', 'controller' => 'index', 'action' => 'show', 'id' => $recipientModel->getAwardId()], ''))
->setType('awardReceivedAsTeam');
$notificationModels[] = $notificationModel;
}
}

$notificationsMapper->addNotifications($notificationModels);
}

$this->redirect()
->withMessage('saveSuccess')
->to(['action' => 'index']);
Expand Down
67 changes: 67 additions & 0 deletions application/modules/awards/controllers/admin/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* @copyright Ilch 2
* @package ilch
*/

namespace Modules\Awards\Controllers\Admin;

use Ilch\Validation;

class Settings extends \Ilch\Controller\Admin
{
public function init()
{
$items = [
[
'name' => 'manage',
'active' => false,
'icon' => 'fa-solid fa-table-list',
'url' => $this->getLayout()->getUrl(['controller' => 'index', 'action' => 'index'])
],
[
'name' => 'settings',
'active' => true,
'icon' => 'fa-solid fa-gears',
'url' => $this->getLayout()->getUrl(['controller' => 'settings', 'action' => 'index'])
]
];

$this->getLayout()->addMenu(
'menuAwards',
$items
);
}

public function indexAction()
{
$this->getLayout()->getAdminHmenu()
->add($this->getTranslator()->trans('menuAwards'), ['action' => 'index'])
->add($this->getTranslator()->trans('manage'), ['action' => 'index']);

if ($this->getRequest()->isPost()) {
$rules = [
'userNotification' => 'required|numeric|integer|min:0|max:1',
];

$validation = Validation::create($this->getRequest()->getPost(), $rules);

if ($validation->isValid()) {
$this->getConfig()->set('awards_userNotification', $this->getRequest()->getPost('userNotification'));

$this->redirect()
->withMessage('saveSuccess')
->to(['action' => 'index']);
}

$this->addMessage($validation->getErrorBag()->getErrorMessages(), 'danger', true);
$this->redirect()
->withInput()
->withErrors($validation->getErrorBag())
->to(['action' => 'index']);
}

$this->getView()->set('userNotification', $this->getConfig()->get('awards_userNotification'));
}
}
3 changes: 3 additions & 0 deletions application/modules/awards/translations/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
'formerUsersOrTeams' => 'Ehemalige Benutzer oder Teams',
'awardNotFound' => 'Auszeichnung nicht gefunden',
'recipientsOfAward' => 'Auszeichnung haben erhalten',
'userNotification' => 'Benutzer über Auszeichnung benachrichtigen',
'awardReceived' => 'Sie haben eine Auszeichnung erhalten.',
'awardReceivedAsTeam' => 'Sie haben als Mitglied eines Teams eine Auszeichnung erhalten.',
];
3 changes: 3 additions & 0 deletions application/modules/awards/translations/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
'formerUsersOrTeams' => 'Former users or teams',
'awardNotFound' => 'Award not found',
'recipientsOfAward' => 'Recipients of this award',
'userNotification' => 'Notify user of received award',
'awardReceived' => 'You have received an award.',
'awardReceivedAsTeam' => 'You have received an award as a member of a team.',
];
23 changes: 23 additions & 0 deletions application/modules/awards/views/admin/settings/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/** @var \Ilch\View $this */
?>
<h1><?=$this->getTrans('settings') ?></h1>
<form method="POST">
<?=$this->getTokenField() ?>
<div class="row mb-3<?=$this->validation()->hasError('userNotification') ? ' has-error' : '' ?>">
<div class="col-xl-2 col-form-label">
<?=$this->getTrans('userNotification') ?>
</div>
<div class="col-xl-4">
<div class="flipswitch">
<input type="radio" class="flipswitch-input" id="userNotification-on" name="userNotification" value="1" <?=($this->get('userNotification') === '1') ? 'checked="checked"' : '' ?> />
<label for="userNotification-on" class="flipswitch-label flipswitch-label-on"><?=$this->getTrans('on') ?></label>
<input type="radio" class="flipswitch-input" id="userNotification-off" name="userNotification" value="0" <?=($this->get('userNotification') !== '1') ? 'checked="checked"' : '' ?> />
<label for="userNotification-off" class="flipswitch-label flipswitch-label-off"><?=$this->getTrans('off') ?></label>
<span class="flipswitch-selection"></span>
</div>
</div>
</div>
<?=$this->getSaveBar() ?>
</form>