From ff4e002caf7bd7186bcdbe446246b9f80475f719 Mon Sep 17 00:00:00 2001 From: kitan1982 Date: Mon, 22 Jun 2015 16:03:44 +0200 Subject: [PATCH] Set last message row in bold in subjects list if there are new messages --- Controller/ForumController.php | 37 +++++++++++++++++- Event/Log/ReadSubjectEvent.php | 49 ++++++++++++++++++++++++ Manager/Manager.php | 15 ++++++++ Repository/ForumRepository.php | 26 +++++++++++++ Resources/views/Forum/subjects.html.twig | 14 +++++++ 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 Event/Log/ReadSubjectEvent.php diff --git a/Controller/ForumController.php b/Controller/ForumController.php index b3c6afe..208cb5b 100644 --- a/Controller/ForumController.php +++ b/Controller/ForumController.php @@ -18,6 +18,7 @@ use Claroline\ForumBundle\Form\SubjectType; use Claroline\ForumBundle\Form\CategoryType; use Claroline\ForumBundle\Form\EditTitleType; +use Claroline\ForumBundle\Event\Log\ReadSubjectEvent; use Claroline\CoreBundle\Library\Resource\ResourceCollection; use Claroline\CoreBundle\Entity\Workspace\Workspace; use Claroline\CoreBundle\Entity\User; @@ -133,6 +134,27 @@ public function subjectsAction(Category $category, $page, $max) $isModerator = $this->authorization->isGranted('moderate', $collection) && !$isAnon; + $logs = array(); + + if (!$isAnon) { + $securityToken = $this->tokenStorage->getToken(); + + if (!is_null($securityToken)) { + $user = $securityToken->getUser(); + $logs = $this->manager->getSubjectsReadingLogs($user, $forum->getResourceNode()); + } + } + $lastAccessDates = array(); + + foreach ($logs as $log) { + $details = $log->getDetails(); + $subjectId = $details['subject']['id']; + + if (!isset($lastAccessDates[$subjectId])) { + $lastAccessDates[$subjectId] = $log->getDateLog(); + } + } + return array( 'pager' => $pager, '_resource' => $forum, @@ -141,7 +163,9 @@ public function subjectsAction(Category $category, $page, $max) 'category' => $category, 'max' => $max, 'lastMessages' => $lastMessages, - 'workspace' => $forum->getResourceNode()->getWorkspace() + 'workspace' => $forum->getResourceNode()->getWorkspace(), + 'lastAccessDates' => $lastAccessDates, + 'isAnon' => $isAnon ); } @@ -319,6 +343,17 @@ public function messagesAction(Subject $subject, $page, $max) $canPost = $this->authorization->isGranted('post', $collection); $form = $this->get('form.factory')->create(new MessageType()); + if (!$isAnon) { + $securityToken = $this->tokenStorage->getToken(); + + if (!is_null($securityToken)) { + $user = $securityToken->getUser(); + $event = new ReadSubjectEvent($subject); + $event->setDoer($user); + $this->dispatch($event); + } + } + return array( 'subject' => $subject, 'pager' => $pager, diff --git a/Event/Log/ReadSubjectEvent.php b/Event/Log/ReadSubjectEvent.php new file mode 100644 index 0000000..e1bb19d --- /dev/null +++ b/Event/Log/ReadSubjectEvent.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Claroline\ForumBundle\Event\Log; + +use Claroline\CoreBundle\Event\Log\AbstractLogResourceEvent; +use Claroline\ForumBundle\Entity\Subject; + +class ReadSubjectEvent extends AbstractLogResourceEvent +{ + const ACTION = 'resource-claroline_forum-read_subject'; + + /** + * @param Subject $subject + */ + public function __construct(Subject $subject) + { + $details = array( + 'subject' => array( + 'id' => $subject->getId(), + 'title' => $subject->getTitle() + ), + 'category' => array( + 'id' => $subject->getCategory()->getId() + ), + 'forum' => array( + 'id' => $subject->getCategory()->getForum()->getId() + ) + ); + + parent::__construct($subject->getCategory()->getForum()->getResourceNode(), $details); + } + + /** + * @return array + */ + public static function getRestriction() + { + return array(self::DISPLAYED_WORKSPACE, self::DISPLAYED_ADMIN); + } +} diff --git a/Manager/Manager.php b/Manager/Manager.php index e5df2b8..fef0db0 100644 --- a/Manager/Manager.php +++ b/Manager/Manager.php @@ -657,4 +657,19 @@ public function createDefaultPostRights(ResourceNode $node) } } } + + public function getSubjectsReadingLogs( + User $user, + ResourceNode $node, + $orderedBy = 'id', + $order = 'DESC' + ) + { + return $this->forumRepo->findSubjectsReadingLogs( + $user, + $node, + $orderedBy, + $order + ); + } } diff --git a/Repository/ForumRepository.php b/Repository/ForumRepository.php index 9af1423..314ad9e 100644 --- a/Repository/ForumRepository.php +++ b/Repository/ForumRepository.php @@ -11,6 +11,8 @@ namespace Claroline\ForumBundle\Repository; +use Claroline\CoreBundle\Entity\Resource\ResourceNode; +use Claroline\CoreBundle\Entity\User; use Claroline\ForumBundle\Entity\Forum; use Claroline\ForumBundle\Entity\Subject; use Claroline\ForumBundle\Entity\Category; @@ -269,4 +271,28 @@ public function findNonSelfNotificationsByForum( return $executeQuery ? $query->getResult() : $query; } + + public function findSubjectsReadingLogs( + User $user, + ResourceNode $node, + $orderedBy = 'id', + $order = 'DESC' + ) + { + $dql = " + SELECT l + FROM Claroline\CoreBundle\Entity\Log\Log l + WHERE l.action = 'resource-claroline_forum-read_subject' + AND l.doer = :user + AND l.resourceNode = :node + ORDER BY l.{$orderedBy} {$order} + "; + + $query = $this->_em->createQuery($dql); + $query->setParameter('user', $user); + $query->setParameter('node', $node); + + return $query->getResult(); + + } } diff --git a/Resources/views/Forum/subjects.html.twig b/Resources/views/Forum/subjects.html.twig index c1868ee..71e672b 100644 --- a/Resources/views/Forum/subjects.html.twig +++ b/Resources/views/Forum/subjects.html.twig @@ -132,9 +132,23 @@ {% if lastMessages[subject['id']] is defined and lastMessages[subject['id']] is not empty %} + {% set isNew = not isAnon and + ( + (lastAccessDates[subject['id']] is not defined) or + (lastMessages[subject['id']].getModificationDate()|date('U') > lastAccessDates[subject['id']]|date('U')) + ) + %} + + {% if isNew %} + + {% endif %} {{ lastMessages[subject['id']].getAuthor() }}
{{ lastMessages[subject['id']].getModificationDate()|intl_date_format() }} + + {% if isNew %} +
+ {% endif %} {% endif %} {% if isModerator %}