Skip to content

Commit

Permalink
Set last message row in bold in subjects list if there are new messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kitan1982 committed Jun 22, 2015
1 parent 591b8f5 commit ff4e002
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
37 changes: 36 additions & 1 deletion Controller/ForumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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
);
}

Expand Down Expand Up @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions Event/Log/ReadSubjectEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* 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);
}
}
15 changes: 15 additions & 0 deletions Manager/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
26 changes: 26 additions & 0 deletions Repository/ForumRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

}
}
14 changes: 14 additions & 0 deletions Resources/views/Forum/subjects.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,23 @@
</td>
<td>
{% 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 %}
<b>
{% endif %}
{{ lastMessages[subject['id']].getAuthor() }}
<br>
{{ lastMessages[subject['id']].getModificationDate()|intl_date_format() }}

{% if isNew %}
</b>
{% endif %}
{% endif %}
</td>
{% if isModerator %}
Expand Down

0 comments on commit ff4e002

Please sign in to comment.