diff --git a/application/modules/awards/config/config.php b/application/modules/awards/config/config.php index e2b928bf9..14e8c5f56 100644 --- a/application/modules/awards/config/config.php +++ b/application/modules/awards/config/config.php @@ -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', @@ -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() @@ -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); } } } diff --git a/application/modules/awards/controllers/admin/Index.php b/application/modules/awards/controllers/admin/Index.php index 8a219a496..73df8df9f 100644 --- a/application/modules/awards/controllers/admin/Index.php +++ b/application/modules/awards/controllers/admin/Index.php @@ -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; @@ -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']) ] ]; @@ -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']); diff --git a/application/modules/awards/controllers/admin/Settings.php b/application/modules/awards/controllers/admin/Settings.php new file mode 100644 index 000000000..95629a4f3 --- /dev/null +++ b/application/modules/awards/controllers/admin/Settings.php @@ -0,0 +1,67 @@ + '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')); + } +} diff --git a/application/modules/awards/translations/de.php b/application/modules/awards/translations/de.php index 76a798e21..cf3c4765e 100644 --- a/application/modules/awards/translations/de.php +++ b/application/modules/awards/translations/de.php @@ -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.', ]; diff --git a/application/modules/awards/translations/en.php b/application/modules/awards/translations/en.php index cdc5fc478..bf4ff2092 100644 --- a/application/modules/awards/translations/en.php +++ b/application/modules/awards/translations/en.php @@ -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.', ]; diff --git a/application/modules/awards/views/admin/settings/index.php b/application/modules/awards/views/admin/settings/index.php new file mode 100644 index 000000000..ece3940f7 --- /dev/null +++ b/application/modules/awards/views/admin/settings/index.php @@ -0,0 +1,23 @@ + +