chamilo /
chamilo-lms
| 1 | <?php |
||
| 2 | |||
| 3 | /* For licensing terms, see /license.txt */ |
||
| 4 | |||
| 5 | declare(strict_types=1); |
||
| 6 | |||
| 7 | namespace Chamilo\CourseBundle\Entity; |
||
| 8 | |||
| 9 | use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
||
| 10 | use ApiPlatform\Metadata\ApiFilter; |
||
| 11 | use ApiPlatform\Metadata\ApiResource; |
||
| 12 | use ApiPlatform\Metadata\GetCollection; |
||
| 13 | use ApiPlatform\Metadata\Post; |
||
| 14 | use Chamilo\CoreBundle\Controller\Api\CreateStudentPublicationCommentAction; |
||
| 15 | use Chamilo\CoreBundle\Entity\AbstractResource; |
||
| 16 | use Chamilo\CoreBundle\Entity\ResourceInterface; |
||
| 17 | use Chamilo\CoreBundle\Entity\User; |
||
| 18 | use Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository; |
||
| 19 | use Cocur\Slugify\Slugify; |
||
| 20 | use DateTime; |
||
| 21 | use Doctrine\ORM\Mapping as ORM; |
||
| 22 | use Stringable; |
||
| 23 | use Symfony\Component\Serializer\Annotation\Groups; |
||
| 24 | use Symfony\Component\Uid\Uuid; |
||
| 25 | |||
| 26 | #[ApiResource( |
||
| 27 | operations: [ |
||
| 28 | new GetCollection( |
||
| 29 | uriTemplate: '/c_student_publication_comments', |
||
| 30 | security: "is_granted('ROLE_USER')", |
||
| 31 | ), |
||
| 32 | new Post( |
||
| 33 | uriTemplate: '/c_student_publication_comments/upload', |
||
| 34 | controller: CreateStudentPublicationCommentAction::class, |
||
| 35 | security: "is_granted('ROLE_USER')", |
||
| 36 | deserialize: false, |
||
| 37 | ), |
||
| 38 | ], |
||
| 39 | normalizationContext: ['groups' => ['student_publication_comment:read']] |
||
| 40 | )] |
||
| 41 | #[ApiFilter(SearchFilter::class, properties: [ |
||
| 42 | 'publication.iid' => 'exact', |
||
| 43 | ])] |
||
| 44 | #[ORM\Table(name: 'c_student_publication_comment')] |
||
| 45 | #[ORM\Entity(repositoryClass: CStudentPublicationCommentRepository::class)] |
||
| 46 | class CStudentPublicationComment extends AbstractResource implements ResourceInterface, Stringable |
||
| 47 | { |
||
| 48 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
| 49 | #[ORM\Id] |
||
| 50 | #[ORM\GeneratedValue] |
||
| 51 | protected ?int $iid = null; |
||
| 52 | |||
| 53 | #[ORM\ManyToOne(targetEntity: CStudentPublication::class, inversedBy: 'comments')] |
||
| 54 | #[ORM\JoinColumn(name: 'work_id', referencedColumnName: 'iid', onDelete: 'CASCADE')] |
||
| 55 | protected CStudentPublication $publication; |
||
| 56 | |||
| 57 | #[Groups(['student_publication_comment:read'])] |
||
| 58 | #[ORM\Column(name: 'comment', type: 'text', nullable: true)] |
||
| 59 | protected ?string $comment = null; |
||
| 60 | |||
| 61 | #[Groups(['student_publication_comment:read'])] |
||
| 62 | #[ORM\Column(name: 'file', type: 'string', length: 255, nullable: true)] |
||
| 63 | protected ?string $file = null; |
||
| 64 | |||
| 65 | #[Groups(['student_publication_comment:read'])] |
||
| 66 | #[ORM\ManyToOne(targetEntity: User::class)] |
||
| 67 | #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
||
| 68 | protected User $user; |
||
| 69 | |||
| 70 | #[Groups(['student_publication_comment:read'])] |
||
| 71 | #[ORM\Column(name: 'sent_at', type: 'datetime', nullable: false)] |
||
| 72 | protected DateTime $sentAt; |
||
| 73 | |||
| 74 | public function __construct() |
||
| 75 | { |
||
| 76 | $this->sentAt = new DateTime(); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function __toString(): string |
||
| 80 | { |
||
| 81 | return (string) $this->getIid(); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getIid(): ?int |
||
| 85 | { |
||
| 86 | return $this->iid; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getFile(): ?string |
||
| 90 | { |
||
| 91 | return $this->file; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function setFile(string $file): self |
||
| 95 | { |
||
| 96 | $this->file = $file; |
||
| 97 | |||
| 98 | return $this; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function getUser(): User |
||
| 102 | { |
||
| 103 | return $this->user; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function setUser(User $user): self |
||
| 107 | { |
||
| 108 | $this->user = $user; |
||
| 109 | |||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function getSentAt(): DateTime |
||
| 114 | { |
||
| 115 | return $this->sentAt; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function setSentAt(DateTime $sentAt): self |
||
| 119 | { |
||
| 120 | $this->sentAt = $sentAt; |
||
| 121 | |||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getPublication(): CStudentPublication |
||
| 126 | { |
||
| 127 | return $this->publication; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function setPublication(CStudentPublication $publication): self |
||
| 131 | { |
||
| 132 | $this->publication = $publication; |
||
| 133 | |||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getResourceIdentifier(): int|Uuid |
||
| 138 | { |
||
| 139 | return $this->getIid(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 140 | } |
||
| 141 | |||
| 142 | public function getResourceName(): string |
||
| 143 | { |
||
| 144 | $comment = trim((string) $this->getComment()); |
||
| 145 | |||
| 146 | if ('' === $comment) { |
||
| 147 | return 'comment-'.(new DateTime())->format('Ymd-His'); |
||
| 148 | } |
||
| 149 | |||
| 150 | $text = strip_tags($comment); |
||
| 151 | $text = Slugify::create()->slugify($text); |
||
| 152 | |||
| 153 | return substr($text, 0, 40); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getComment(): ?string |
||
| 157 | { |
||
| 158 | return $this->comment; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function setComment(string $comment): self |
||
| 162 | { |
||
| 163 | $this->comment = $comment; |
||
| 164 | |||
| 165 | return $this; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function setTitle(string $title): self |
||
| 169 | { |
||
| 170 | return $this->setComment($title); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function getTitle(): ?string |
||
| 174 | { |
||
| 175 | return $this->getComment(); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function setResourceName(string $name): self |
||
| 179 | { |
||
| 180 | return $this->setComment($name); |
||
| 181 | } |
||
| 182 | } |
||
| 183 |