Passed
Pull Request — master (#5329)
by Angel Fernando Quiroz
07:23
created

CStudentPublicationPostStateProcessor::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 23
nc 4
nop 4
dl 0
loc 42
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\State;
6
7
use ApiPlatform\Metadata\Operation;
8
use ApiPlatform\State\ProcessorInterface;
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ResourceLink;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Entity\User;
13
use Chamilo\CoreBundle\Settings\SettingsManager;
14
use Chamilo\CourseBundle\Entity\CCalendarEvent;
15
use Chamilo\CourseBundle\Entity\CGroup;
16
use Chamilo\CourseBundle\Entity\CStudentPublication;
17
use Chamilo\CourseBundle\Entity\CStudentPublicationAssignment;
18
use DateTime;
19
use DateTimeZone;
20
use Doctrine\ORM\EntityManagerInterface;
21
use GradebookUtils;
22
use Symfony\Bundle\SecurityBundle\Security;
23
use Symfony\Component\Routing\RouterInterface;
24
use Symfony\Contracts\Translation\TranslatorInterface;
25
26
final class CStudentPublicationPostStateProcessor implements ProcessorInterface
27
{
28
    public function __construct(
29
        private readonly ProcessorInterface $persistProcessor,
30
        private readonly EntityManagerInterface $entityManager,
31
        private readonly TranslatorInterface $translator,
32
        private readonly RouterInterface $router,
33
        private readonly Security $security,
34
        private readonly SettingsManager $settingsManager,
35
    ) {}
36
37
    public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
38
    {
39
        $result = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
40
41
        /** @var CStudentPublication $publication */
42
        $publication = $data;
43
        $assignment = $publication->getAssignment();
44
        $courseLink = $publication->getFirstResourceLink();
45
        $course = $courseLink->getCourse();
46
        $session = $courseLink->getSession();
47
        $group = $courseLink->getGroup();
48
49
        /** @var User $currentUser */
50
        $currentUser = $this->security->getUser();
51
52
        if ($publication->getQualification() > 0) {
53
            $assignment->setEnableQualification(true);
54
        }
55
56
        if ($publication->addToCalendar) {
57
            $event = $this->saveCalendarEvent($publication, $assignment, $courseLink, $course, $session, $group);
58
59
            $assignment->setEventCalendarId($event->getIid());
60
        } else {
61
            $assignment->setEventCalendarId(0);
62
        }
63
64
        $publication
65
            ->setHasProperties($assignment->getIid())
66
            ->setViewProperties(true)
67
            ->setUser($currentUser)
68
        ;
69
70
        $this->entityManager->flush();
71
72
        $this->saveGradebookConfig($publication, $course, $session);
73
74
        // Save extrafields
75
76
        $this->sendEmailAlertStudentsOnNewHomework($publication, $course, $session);
77
78
        return $result;
79
    }
80
81
    private function saveCalendarEvent(
82
        CStudentPublication $publication,
83
        CStudentPublicationAssignment $assignment,
84
        ResourceLink $courseLink,
85
        Course $course,
86
        ?Session $session,
87
        ?CGroup $group,
88
    ): CCalendarEvent {
89
        $eventTitle = sprintf(
90
            $this->translator->trans('Handing over of task %s'),
91
            $publication->getTitle()
92
        );
93
94
        $publicationUrl = $this->router->generate(
95
            'legacy_main',
96
            [
97
                'name' => 'work/work_list.php',
98
                'cid' => $course->getId(),
99
                'sid' => $session?->getId(),
100
                'gid' => $group?->getIid(),
101
                'id' => $publication->getIid(),
102
            ]
103
        );
104
105
        $content = sprintf(
106
            '<div><a href="%s">%s</a></div> %s',
107
            $publicationUrl,
108
            $publication->getTitle(),
109
            $publication->getDescription()
110
        );
111
112
        $startDate = new DateTime('now', new DateTimeZone('UTC'));
113
        $endDate = new DateTime('now', new DateTimeZone('UTC'));
114
115
        if ($expiresOn = $assignment->getExpiresOn()) {
116
            $startDate = clone $expiresOn;
117
            $endDate = clone $expiresOn;
118
        }
119
120
        $color = CCalendarEvent::COLOR_STUDENT_PUBLICATION;
121
122
        if ($agendaColors = $this->settingsManager->getSetting('agenda.agenda_colors')) {
123
            $color = $agendaColors['student_publication'];
124
        }
125
126
        $event = (new CCalendarEvent())
127
            ->setTitle($eventTitle)
128
            ->setContent($content)
129
            ->setParent($course)
130
            ->setCreator($publication->getCreator())
131
            ->addLink(clone $courseLink)
132
            ->setStartDate($startDate)
133
            ->setEndDate($endDate)
134
            ->setColor($color)
135
        ;
136
137
        $this->entityManager->persist($event);
138
        $this->entityManager->flush();
139
140
        return $event;
141
    }
142
143
    private function saveGradebookConfig(CStudentPublication $publication, Course $course, ?Session $session): void
144
    {
145
        if ($publication->gradebookCategoryId <= 0) {
146
            return;
147
        }
148
149
        $gradebookLinkInfo = GradebookUtils::isResourceInCourseGradebook(
150
            $course->getId(),
151
            LINK_STUDENTPUBLICATION,
152
            $publication->getIid(),
153
            $session?->getId()
154
        );
155
156
        $linkId = empty($gradebookLinkInfo) ? null : $gradebookLinkInfo['id'];
157
158
        if ($publication->addToGradebook) {
159
            if (empty($linkId)) {
160
                GradebookUtils::add_resource_to_course_gradebook(
161
                    $publication->gradebookCategoryId,
162
                    $course->getId(),
163
                    LINK_STUDENTPUBLICATION,
164
                    $publication->getIid(),
165
                    $publication->getTitle(),
166
                    $publication->getWeight(),
167
                    $publication->getQualification(),
168
                    $publication->getDescription(),
169
                    1,
170
                    $session?->getId()
171
                );
172
            } else {
173
                GradebookUtils::updateResourceFromCourseGradebook(
174
                    $linkId,
175
                    $course->getId(),
176
                    $publication->getWeight()
177
                );
178
            }
179
        } else {
180
            // Delete everything of the gradebook for this $linkId
181
            GradebookUtils::remove_resource_from_course_gradebook($linkId);
182
        }
183
    }
184
185
    private function sendEmailAlertStudentsOnNewHomework(
186
        CStudentPublication $publication,
187
        Course $course,
188
        ?Session $session
189
    ): void {
190
        $sendEmailAlert = api_get_course_setting('email_alert_students_on_new_homework');
191
192
        switch ($sendEmailAlert) {
193
            case 1:
194
                sendEmailToStudentsOnHomeworkCreation(
195
                    $publication->getIid(),
196
                    $course->getId(),
197
                    $session?->getId()
198
                );
199
200
                // no break
201
            case 2:
202
                sendEmailToDrhOnHomeworkCreation(
203
                    $publication->getIid(),
204
                    $course->getId(),
205
                    $session?->getId()
206
                );
207
208
                break;
209
        }
210
    }
211
}
212