Fix forum resources and behat tests

pull/3064/head
Julio 5 years ago
parent ff79405544
commit 243d80a467
  1. 21
      public/main/forum/forumfunction.inc.php
  2. 9
      src/CoreBundle/Framework/Container.php
  3. 2
      src/CoreBundle/Repository/ResourceRepository.php
  4. 75
      src/CourseBundle/Entity/CForumAttachment.php
  5. 25
      src/CourseBundle/Entity/CForumForum.php
  6. 20
      src/CourseBundle/Entity/CForumPost.php
  7. 25
      src/CourseBundle/Entity/CForumThread.php
  8. 492
      src/CourseBundle/Entity/CItemProperty.php
  9. 23
      src/CourseBundle/Repository/CForumAttachmentRepository.php
  10. 97
      src/CourseBundle/Repository/CForumCategoryRepository.php
  11. 1
      src/CourseBundle/Repository/CForumForumRepository.php
  12. 1
      src/CourseBundle/Repository/CForumThreadRepository.php
  13. 4
      src/CourseBundle/Resources/config/services.yml
  14. 8
      src/GraphQlBundle/Resolver/CourseResolver.php

@ -5669,16 +5669,27 @@ function getAllAttachment($postId)
* @param int $post_id
* @param int $id_attach
*
* @return int
*
* @author Julio Montoya
* @return bool
*
* @version october 2014, chamilo 1.9.8
*/
function delete_attachment($post_id, $id_attach = 0)
function delete_attachment($postId, $id_attach = 0)
{
$_course = api_get_course_info();
$repo = Container::getForumPostRepository();
/** @var CForumPost $post */
$post = $repo->find($postId);
$repoAttachment = Container::getForumAttachmentRepository();
$attachment = $repoAttachment->find($id_attach);
$post->removeAttachment($attachment);
$repo->getEntityManager()->persist($post);
$repo->getEntityManager()->flush();
Display::addFlash(Display::return_message(get_lang('The attached file has been deleted'), 'confirmation'));
return true;
$forum_table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
$course_id = api_get_course_int_id();

@ -13,6 +13,7 @@ use Chamilo\CoreBundle\Repository\IllustrationRepository;
use Chamilo\CoreBundle\ToolChain;
use Chamilo\CourseBundle\Repository\CDocumentRepository;
use Chamilo\CourseBundle\Repository\CExerciseCategoryRepository;
use Chamilo\CourseBundle\Repository\CForumAttachmentRepository;
use Chamilo\CourseBundle\Repository\CForumCategoryRepository;
use Chamilo\CourseBundle\Repository\CForumForumRepository;
use Chamilo\CourseBundle\Repository\CForumPostRepository;
@ -334,6 +335,14 @@ class Container
return self::$container->get('Chamilo\CourseBundle\Repository\CForumPostRepository');
}
/**
* @return CForumAttachmentRepository
*/
public static function getForumAttachmentRepository()
{
return self::$container->get('Chamilo\CourseBundle\Repository\CForumAttachmentRepository');
}
/**
* @return CForumThreadRepository
*/

@ -172,6 +172,8 @@ class ResourceRepository extends BaseEntityRepository
/**
* @param null $lockMode
* @param null $lockVersion
*
* @return ResourceInterface
*/
public function find($id, $lockMode = null, $lockVersion = null)
{

@ -4,6 +4,8 @@
namespace Chamilo\CourseBundle\Entity;
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
use Chamilo\CoreBundle\Entity\Resource\ResourceInterface;
use Doctrine\ORM\Mapping as ORM;
/**
@ -17,7 +19,7 @@ use Doctrine\ORM\Mapping as ORM;
* )
* @ORM\Entity
*/
class CForumAttachment
class CForumAttachment extends AbstractResource implements ResourceInterface
{
/**
* @var int
@ -64,11 +66,12 @@ class CForumAttachment
protected $size;
/**
* @var int
* @var CForumPost
*
* @ORM\Column(name="post_id", type="integer", nullable=false)
* @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CForumPost", inversedBy="attachments")
* @ORM\JoinColumn(name="post_id", referencedColumnName="iid")
*/
protected $postId;
protected $post;
/**
* @var string
@ -77,6 +80,25 @@ class CForumAttachment
*/
protected $filename;
public function __construct()
{
}
public function __toString(): string
{
return (string) $this->getFilename();
}
/**
* Get iid.
*
* @return int
*/
public function getIid()
{
return $this->iid;
}
/**
* Set path.
*
@ -149,30 +171,6 @@ class CForumAttachment
return $this->size;
}
/**
* Set postId.
*
* @param int $postId
*
* @return CForumAttachment
*/
public function setPostId($postId)
{
$this->postId = $postId;
return $this;
}
/**
* Get postId.
*
* @return int
*/
public function getPostId()
{
return $this->postId;
}
/**
* Set filename.
*
@ -244,4 +242,25 @@ class CForumAttachment
{
return $this->cId;
}
/**
* @return CForumPost
*/
public function getPost(): CForumPost
{
return $this->post;
}
/**
* Resource identifier.
*/
public function getResourceIdentifier(): int
{
return $this->getIid();
}
public function getResourceName(): string
{
return $this->getFilename();
}
}

@ -200,11 +200,6 @@ class CForumForum extends AbstractResource implements ResourceInterface
*/
protected $moderated;
/**
* @var CItemProperty
*/
protected $itemProperty;
/**
* @var ArrayCollection
*
@ -809,26 +804,6 @@ class CForumForum extends AbstractResource implements ResourceInterface
return $this->threads;
}
/**
* Set itemProperty.
*
* @return CForumForum
*/
public function setItemProperty(CItemProperty $itemProperty)
{
$this->itemProperty = $itemProperty;
return $this;
}
/**
* @return CItemProperty
*/
public function getItemProperty()
{
return $this->itemProperty;
}
/**
* Resource identifier.
*/

@ -131,6 +131,13 @@ class CForumPost extends AbstractResource implements ResourceInterface
*/
protected $status;
/**
* @var CForumAttachment[]
*
* @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CForumAttachment", mappedBy="post")
*/
protected $attachments;
public function __construct()
{
$this->postId = 0;
@ -457,6 +464,19 @@ class CForumPost extends AbstractResource implements ResourceInterface
return $this->iid;
}
/**
* @return CForumAttachment[]
*/
public function getAttachments(): array
{
return $this->attachments;
}
public function removeAttachment(CForumAttachment $attachment)
{
$this->attachments->removeElement($attachment);
}
/**
* Resource identifier.
*/

@ -167,17 +167,12 @@ class CForumThread extends AbstractResource implements ResourceInterface
protected $lpItemId;
/**
* @var ArrayCollection
* @var CForumPost[]
*
* @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CForumPost", mappedBy="thread")
*/
protected $posts;
/**
* @var CItemProperty
*/
protected $itemProperty;
public function __construct()
{
$this->threadPeerQualify = false;
@ -662,24 +657,6 @@ class CForumThread extends AbstractResource implements ResourceInterface
return $this->posts;
}
/**
* @return CForumThread
*/
public function setItemProperty(CItemProperty $itemProperty)
{
$this->itemProperty = $itemProperty;
return $this;
}
/**
* @return CItemProperty
*/
public function getItemProperty()
{
return $this->itemProperty;
}
/**
* Resource identifier.
*/

@ -1,492 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CourseBundle\Entity;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\UserBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* CItemProperty.
*
* @deprecated.
*
* @ORM\Table(name="c_item_property", indexes={
* @ORM\Index(name="idx_item_property_toolref", columns={"tool", "ref"}),
* @ORM\Index(name="idx_cip_lasteditu", columns={"lastedit_user_id"}),
* @ORM\Index(name="idx_item_property_visibility", columns={"visibility"}),
* })
* @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\ItemPropertyRepository")
*/
class CItemProperty
{
/**
* @var int
*
* @ORM\Column(name="iid", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
protected $iid;
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=true)
*/
protected $id;
/** //, inversedBy="users",
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", cascade={"persist"})
* @ORM\JoinColumn(name="c_id", referencedColumnName="id")
*/
protected $course;
/** //, inversedBy="users",
* @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CGroupInfo", cascade={"persist"})
* @ORM\JoinColumn(name="to_group_id", referencedColumnName="iid")
*/
protected $group;
/**
* @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User", cascade={"persist"})
* @ORM\JoinColumn(name="to_user_id", referencedColumnName="id")
*/
protected $toUser;
/**
* @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User", cascade={"persist"})
* @ORM\JoinColumn(name="insert_user_id", referencedColumnName="id")
*/
protected $insertUser;
/** //, inversedBy="users",
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", cascade={"persist"})
* @ORM\JoinColumn(name="session_id", referencedColumnName="id")
*/
protected $session;
/**
* @var string
*
* @ORM\Column(name="tool", type="string", length=100, nullable=false)
*/
protected $tool;
/**
* @var \DateTime
*
* @ORM\Column(name="insert_date", type="datetime", nullable=false)
*/
protected $insertDate;
/**
* @var \DateTime
*
* @ORM\Column(name="lastedit_date", type="datetime", nullable=false)
*/
protected $lasteditDate;
/**
* @var int
*
* @ORM\Column(name="ref", type="integer", nullable=false)
*/
protected $ref;
/**
* @var string
*
* @ORM\Column(name="lastedit_type", type="string", length=100, nullable=false)
*/
protected $lasteditType;
/**
* @var int
*
* @ORM\Column(name="lastedit_user_id", type="integer", nullable=true)
*/
protected $lasteditUserId;
/**
* @var int
*
* @ORM\Column(name="visibility", type="integer", nullable=false)
*/
protected $visibility;
/**
* @var \DateTime
*
* @ORM\Column(name="start_visible", type="datetime", nullable=true)
*/
protected $startVisible;
/**
* @var \DateTime
*
* @ORM\Column(name="end_visible", type="datetime", nullable=true)
*/
protected $endVisible;
/**
* CItemProperty constructor.
*/
public function __construct(Course $course)
{
$this->visibility = 1;
$this->course = $course;
$this->insertDate = new \DateTime('now', new \DateTimeZone('UTC'));
$this->lasteditDate = new \DateTime('now', new \DateTimeZone('UTC'));
}
/**
* Set tool.
*
* @param string $tool
*
* @return CItemProperty
*/
public function setTool($tool)
{
$this->tool = $tool;
return $this;
}
/**
* Get tool.
*
* @return string
*/
public function getTool()
{
return $this->tool;
}
/**
* Set insertDate.
*
* @param \DateTime $insertDate
*
* @return CItemProperty
*/
public function setInsertDate($insertDate)
{
$this->insertDate = $insertDate;
return $this;
}
/**
* Get insertDate.
*
* @return \DateTime
*/
public function getInsertDate()
{
return $this->insertDate;
}
/**
* Set lasteditDate.
*
* @return CItemProperty
*/
public function setLasteditDate(\DateTime $lasteditDate)
{
$this->lasteditDate = $lasteditDate;
return $this;
}
/**
* Get lasteditDate.
*
* @return \DateTime
*/
public function getLasteditDate()
{
return $this->lasteditDate;
}
/**
* Set ref.
*
* @param int $ref
*
* @return CItemProperty
*/
public function setRef($ref)
{
$this->ref = $ref;
return $this;
}
/**
* Get ref.
*
* @return int
*/
public function getRef()
{
return $this->ref;
}
/**
* Set lasteditType.
*
* @param string $lasteditType
*
* @return CItemProperty
*/
public function setLasteditType($lasteditType)
{
$this->lasteditType = $lasteditType;
return $this;
}
/**
* Get lasteditType.
*
* @return string
*/
public function getLasteditType()
{
return $this->lasteditType;
}
/**
* Set lasteditUserId.
*
* @param int $lasteditUserId
*
* @return CItemProperty
*/
public function setLasteditUserId($lasteditUserId)
{
$this->lasteditUserId = $lasteditUserId;
return $this;
}
/**
* Get lasteditUserId.
*
* @return int
*/
public function getLasteditUserId()
{
return $this->lasteditUserId;
}
/**
* Set visibility.
*
* @param int $visibility
*
* @return CItemProperty
*/
public function setVisibility($visibility)
{
$this->visibility = $visibility;
return $this;
}
/**
* Get visibility.
*
* @return int
*/
public function getVisibility()
{
return $this->visibility;
}
/**
* Set startVisible.
*
* @param \DateTime $startVisible
*
* @return CItemProperty
*/
public function setStartVisible(\DateTime $startVisible = null)
{
$this->startVisible = $startVisible;
return $this;
}
/**
* Get startVisible.
*
* @return \DateTime
*/
public function getStartVisible()
{
return $this->startVisible;
}
/**
* Set endVisible.
*
* @param \DateTime $endVisible
*
* @return CItemProperty
*/
public function setEndVisible(\DateTime $endVisible = null)
{
$this->endVisible = $endVisible;
return $this;
}
/**
* Get endVisible.
*
* @return \DateTime
*/
public function getEndVisible()
{
return $this->endVisible;
}
/**
* Set id.
*
* @param int $id
*
* @return CItemProperty
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return Session
*/
public function getSession()
{
return $this->session;
}
/**
* @param Session $session
*
* @return CItemProperty
*/
public function setSession($session)
{
$this->session = $session;
return $this;
}
/**
* @return Course
*/
public function getCourse()
{
return $this->course;
}
/**
* @param Course $course
*
* @return CItemProperty
*/
public function setCourse($course)
{
$this->course = $course;
return $this;
}
/**
* @return CGroupInfo
*/
public function getGroup()
{
return $this->group;
}
/**
* @param CGroupInfo $group
*
* @return CItemProperty
*/
public function setGroup($group)
{
$this->group = $group;
return $this;
}
/**
* @return User
*/
public function getToUser()
{
return $this->toUser;
}
/**
* @param User $toUser
*
* @return $this
*/
public function setToUser($toUser)
{
$this->toUser = $toUser;
return $this;
}
/**
* @return User
*/
public function getInsertUser()
{
return $this->insertUser;
}
/**
* @return $this
*/
public function setInsertUser(User $insertUser)
{
$this->insertUser = $insertUser;
$this->lasteditUserId = $insertUser->getId();
return $this;
}
/**
* Get iid.
*
* @return int
*/
public function getIid()
{
return $this->iid;
}
}

@ -0,0 +1,23 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CGroupInfo;
use Chamilo\UserBundle\Entity\User;
/**
* Class CForumAttachmentRepository.
*/
class CForumAttachmentRepository extends ResourceRepository
{
public function getResources(User $user, ResourceNode $parentNode, Course $course = null, Session $session = null, CGroupInfo $group = null)
{
return $this->getResourcesByCourse($course, $session, $group, $parentNode);
}
}

@ -8,9 +8,7 @@ use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CForumCategory;
use Chamilo\CourseBundle\Entity\CGroupInfo;
use Chamilo\CourseBundle\Entity\CItemProperty;
use Chamilo\UserBundle\Entity\User;
class CForumCategoryRepository extends ResourceRepository
@ -19,99 +17,4 @@ class CForumCategoryRepository extends ResourceRepository
{
return $this->getResourcesByCourse($course, $session, $group, $parentNode);
}
/**
* @param bool $isAllowedToEdit
*
* @todo Remove api_get_session_condition
*/
public function findAllInCourse($isAllowedToEdit, Course $course, Session $session = null): array
{
$conditionSession = api_get_session_condition(
$session ? $session->getId() : 0,
true,
true,
'fcat.sessionId'
);
$conditionVisibility = $isAllowedToEdit ? 'ip.visibility != 2' : 'ip.visibility = 1';
$dql = "SELECT ip, fcat
FROM ChamiloCourseBundle:CItemProperty AS ip
INNER JOIN ChamiloCourseBundle:CForumCategory fcat
WITH (fcat.catId = ip.ref AND ip.course = fcat.cId)
WHERE
ip.tool = :tool AND
ip.course = :course
$conditionSession AND
$conditionVisibility
ORDER BY fcat.catOrder ASC";
$result = $this
->_em
->createQuery($dql)
->setParameters(['course' => $course, 'tool' => TOOL_FORUM_CATEGORY])
->getResult();
$categories = [];
for ($i = 0; $i < count($result); $i += 2) {
/** @var CItemProperty $ip */
$ip = $result[$i];
/** @var CForumCategory $fc */
$fc = $result[$i + 1];
$fc->setItemProperty($ip);
$categories[] = $fc;
}
return $categories;
}
/**
* @param int $id
*
* @return CForumCategory|null
*
* @todo Remove api_get_session_condition
*/
public function findOneInCourse($id, Course $course, Session $session)
{
$conditionSession = api_get_session_condition(
$session ? $session->getId() : 0,
true,
true,
'fcat.sessionId'
);
$dql = "SELECT ip, fcat
FROM ChamiloCourseBundle:CItemProperty AS ip
INNER JOIN ChamiloCourseBundle:CForumCategory fcat
WITH (fcat.catId = ip.ref AND ip.course = fcat.cId)
WHERE
ip.tool = :tool AND
ip.course = :course
fcat.iid = :id
$conditionSession AND
ORDER BY fcat.catOrder ASC";
$result = $this
->_em
->createQuery($dql)
->setParameters(['tool' => TOOL_FORUM_CATEGORY, 'course' => $course, 'id' => (int) $id])
->getResult();
if (empty($result)) {
return null;
}
/** @var CItemProperty $ip */
$ip = $result[0];
/** @var CForumCategory $fc */
$fc = $result[1];
$fc->setItemProperty($ip);
return $fc;
}
}

@ -9,7 +9,6 @@ use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CForumCategory;
use Chamilo\CourseBundle\Entity\CForumForum;
use Chamilo\CourseBundle\Entity\CItemProperty;
final class CForumForumRepository extends ResourceRepository
{

@ -10,7 +10,6 @@ use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CForumForum;
use Chamilo\CourseBundle\Entity\CForumThread;
use Chamilo\CourseBundle\Entity\CGroupInfo;
use Chamilo\CourseBundle\Entity\CItemProperty;
/**
* Class CForumThreadRepository.

@ -78,6 +78,10 @@ services:
arguments:
$className: 'Chamilo\CourseBundle\Entity\CExerciseCategory'
Chamilo\CourseBundle\Repository\CForumAttachmentRepository:
arguments:
$className: 'Chamilo\CourseBundle\Entity\CForumAttachment'
Chamilo\CourseBundle\Repository\CForumForumRepository:
arguments:
$className: 'Chamilo\CourseBundle\Entity\CForumForum'

@ -200,7 +200,7 @@ class CourseResolver implements ContainerAwareInterface
$catRepo = $this->em->getRepository('ChamiloCourseBundle:CForumCategory');
return $catRepo->findAllInCourse(false, $course, $session);
return $catRepo->getResourcesByCourse($course, $session, $course->getResourceNode());
}
public function getForums(CForumCategory $category, \ArrayObject $context): array
@ -226,12 +226,16 @@ class CourseResolver implements ContainerAwareInterface
$course = $context->offsetGet('course');
$forumRepo = $this->em->getRepository('ChamiloCourseBundle:CForumForum');
$forum = $forumRepo->findOneInCourse($id, $course);
$forum = $forumRepo->find($id);
if (empty($forum)) {
throw new UserError($this->translator->trans('Forum not found in this course.'));
}
if ($forum->getCId() !== $course->getId()) {
throw new UserError($this->translator->trans('Forum not found in this course.'));
}
return $forum;
}

Loading…
Cancel
Save