Calendar: Move code to repository

pull/3939/head
Angel Fernando Quiroz Campos 3 years ago
parent b43c4ed2e6
commit d7b7a6f600
  1. 17
      public/main/announcements/announcements.php
  2. 45
      public/main/inc/lib/AnnouncementManager.php
  3. 55
      src/CourseBundle/Repository/CCalendarEventRepository.php

@ -737,15 +737,18 @@ switch ($action) {
if ($announcement) {
if (!empty($data['event_date_start']) && !empty($data['event_date_end'])) {
AnnouncementManager::createEvent(
$announcement,
api_get_utc_datetime($data['event_date_start'], true, true),
api_get_utc_datetime($data['event_date_end'], true, true),
$data['users']
);
Container::getCalendarEventRepository()
->createFromAnnouncement(
$announcement,
api_get_utc_datetime($data['event_date_start'], true, true),
api_get_utc_datetime($data['event_date_end'], true, true),
$data['users'],
api_get_course_entity(),
api_get_session_entity(),
api_get_group_entity()
);
}
Display::addFlash(
Display::return_message(
get_lang('Announcement has been added'),

@ -1904,49 +1904,4 @@ class AnnouncementManager
return $qb->getQuery()->getSingleScalarResult();
}
}
public static function createEvent(
CAnnouncement $announcement,
DateTime $startDate,
DateTime $endDate,
array $choosenUsers = []
): CCalendarEvent {
$course = api_get_course_entity();
$session = api_get_session_entity();
$group = api_get_group_entity();
$event = (new CCalendarEvent())
->setTitle($announcement->getTitle())
->setStartDate($startDate)
->setEndDate($endDate)
->setContent($announcement->getContent())
->setParent($course)
;
if (empty($choosenUsers) || (isset($choosenUsers[0]) && 'everyone' === $choosenUsers[0])) {
$event->addCourseLink($course, $session, $group);
} else {
$sendTo = AbstractResource::separateUsersGroups($choosenUsers);
if (is_array($sendTo['groups']) && !empty($sendTo['groups'])) {
$sendTo['groups'] = array_map('api_get_group_entity', $sendTo['groups']);
$sendTo['groups'] = array_filter($sendTo['groups']);
$event->addResourceToGroupList($sendTo['groups'], $course, $session);
}
// Storing the selected users
if (is_array($sendTo['users'])) {
$sendTo['users'] = array_map('api_get_user_entity', $sendTo['users']);
$event->addResourceToUserList($sendTo['users'], $course, $session, $group);
}
}
$em = Container::getEntityManager();
$em->persist($event);
$em->flush();
return $event;
}
}

@ -6,8 +6,14 @@ declare(strict_types=1);
namespace Chamilo\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CourseBundle\Entity\CAnnouncement;
use Chamilo\CourseBundle\Entity\CCalendarEvent;
use Chamilo\CourseBundle\Entity\CGroup;
use Doctrine\Persistence\ManagerRegistry;
final class CCalendarEventRepository extends ResourceRepository
@ -16,4 +22,53 @@ final class CCalendarEventRepository extends ResourceRepository
{
parent::__construct($registry, CCalendarEvent::class);
}
public function createFromAnnouncement(
CAnnouncement $announcement,
\DateTime $startDate,
\DateTime $endDate,
array $users,
Course $course,
?Session $session = null,
?CGroup $group = null
): CCalendarEvent {
$event = (new CCalendarEvent())
->setTitle($announcement->getTitle())
->setStartDate($startDate)
->setEndDate($endDate)
->setContent($announcement->getContent())
->setParent($course)
;
if (empty($users) || (isset($users[0]) && 'everyone' === $users[0])) {
$event->addCourseLink($course, $session, $group);
} else {
$sendTo = AbstractResource::separateUsersGroups($users);
if (is_array($sendTo['groups']) && !empty($sendTo['groups'])) {
$sendTo['groups'] = array_map(
fn($gId) => $this->_em->find(CGroup::class, $gId),
$sendTo['groups']
);
$sendTo['groups'] = array_filter($sendTo['groups']);
$event->addResourceToGroupList($sendTo['groups'], $course, $session);
}
// Storing the selected users
if (is_array($sendTo['users'])) {
$sendTo['users'] = array_map(
fn($uId) => $this->_em->find(User::class, $uId),
$sendTo['users']
);
$event->addResourceToUserList($sendTo['users'], $course, $session, $group);
}
}
$this->_em->persist($event);
$this->_em->flush();
return $event;
}
}

Loading…
Cancel
Save