Add repository for forum post #2644

pull/2715/head
Angel Fernando Quiroz Campos 8 years ago
parent d867a49213
commit 94f30f040a
  1. 2
      src/CourseBundle/Entity/CForumPost.php
  2. 30
      src/CourseBundle/Entity/CGroupInfo.php
  3. 80
      src/CourseBundle/Repository/CForumPostRepository.php

@ -19,7 +19,7 @@ use Doctrine\ORM\Mapping as ORM;
* @ORM\Index(name="c_id_visible_post_date", columns={"c_id", "visible", "post_date"})
* }
* )
* @ORM\Entity
* @ORM\Entity(repositoryClass="Chamilo\CourseBundle\Repository\CForumPostRepository")
*/
class CForumPost
{

@ -5,7 +5,9 @@ namespace Chamilo\CourseBundle\Entity;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Traits\CourseTrait;
use Chamilo\UserBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
/**
@ -685,4 +687,32 @@ class CGroupInfo
return $this;
}
/**
* @param User|null $user
*
* @return bool
*/
public function userIsTutor(User $user = null): bool
{
if (empty($user)) {
return false;
}
if (0 === $this->tutors->count()) {
return false;
}
$criteria = Criteria::create()
->where(
Criteria::expr()->eq('cId', $this->course)
)
->andWhere(
Criteria::expr()->eq('user', $user)
);
$relation = $this->tutors->matching($criteria);
return $relation->count() > 0;
}
}

@ -0,0 +1,80 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CourseBundle\Entity\CForumPost;
use Chamilo\CourseBundle\Entity\CForumThread;
use Chamilo\CourseBundle\Entity\CGroupInfo;
use Chamilo\UserBundle\Entity\User;
use Doctrine\ORM\EntityRepository;
/**
* Class CForumPostRepository.
*
* @package Chamilo\CourseBundle\Repository
*/
class CForumPostRepository extends EntityRepository
{
/**
* @param bool $onlyVisibles
* @param bool $isAllowedToEdit
* @param CForumThread $thread
* @param Course $course
* @param User|null $currentUser
* @param CGroupInfo|null $group
* @param string $orderDirection
*
* @return array
*/
public function findAllInCourseByThread(
$onlyVisibles,
$isAllowedToEdit,
CForumThread $thread,
Course $course,
User $currentUser = null,
CGroupInfo $group = null,
$orderDirection = 'ASC'
): array {
$conditionVisibility = $onlyVisibles ? 'p.visible = 1' : 'p.visible != 2';
$conditionModetared = '';
$filterModerated = true;
if (
(empty($group) && $isAllowedToEdit) ||
(
($group ? $group->userIsTutor($currentUser) : false) ||
!$onlyVisibles
)
) {
$filterModerated = false;
}
if ($filterModerated && $thread->getForum()->isModerated() && $onlyVisibles) {
$userId = $currentUser ? $currentUser->getId() : 0;
$conditionModetared = "AND p.status = 1 OR
(p.status = ".CForumPost::STATUS_WAITING_MODERATION." AND p.posterId = $userId) OR
(p.status = ".CForumPost::STATUS_REJECTED." AND p.poster = $userId) OR
(p.status IS NULL AND p.posterId = $userId)";
}
$dql = "SELECT p
FROM ChamiloCourseBundle:CForumPost p
WHERE
p.thread = :thread AND
p.cId = :course AND
$conditionVisibility
$conditionModetared
ORDER BY p.iid $orderDirection";
$result = $this
->_em
->createQuery($dql)
->setParameters(['thread' => $thread, 'course' => $course])
->getResult();
return $result;
}
}
Loading…
Cancel
Save