From 5227842cc68cb68e68029eab01aa39b04c83ff68 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Mon, 1 Oct 2018 16:10:59 -0500 Subject: [PATCH] GraphQL add query for course agenda #2644 --- src/ApiBundle/GraphQL/Map/EnumMap.php | 1 + src/ApiBundle/GraphQL/Map/QueryMap.php | 30 ++++++++++++++ src/ApiBundle/GraphQL/Map/UnionMap.php | 2 + .../GraphQL/Resolver/CourseResolver.php | 41 +++++++++++++++++++ .../Resources/config/schema.types.graphql | 24 ++++++++++- 5 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/ApiBundle/GraphQL/Map/EnumMap.php b/src/ApiBundle/GraphQL/Map/EnumMap.php index b130999b76..cdce1871d8 100644 --- a/src/ApiBundle/GraphQL/Map/EnumMap.php +++ b/src/ApiBundle/GraphQL/Map/EnumMap.php @@ -42,6 +42,7 @@ class EnumMap extends ResolverMap implements ContainerAwareInterface 'TOOL_ANNOUNCEMENT' => TOOL_ANNOUNCEMENT, 'TOOL_NOTEBOOK' => TOOL_NOTEBOOK, 'TOOL_FORUM' => TOOL_FORUM, + 'TOOL_CALENDAR_EVENT' => TOOL_CALENDAR_EVENT, ], ]; } diff --git a/src/ApiBundle/GraphQL/Map/QueryMap.php b/src/ApiBundle/GraphQL/Map/QueryMap.php index 6aad4d158d..94050c8e72 100644 --- a/src/ApiBundle/GraphQL/Map/QueryMap.php +++ b/src/ApiBundle/GraphQL/Map/QueryMap.php @@ -289,6 +289,36 @@ class QueryMap extends ResolverMap implements ContainerAwareInterface return $parent; }, ], + 'ToolAgenda' => [ + self::RESOLVE_FIELD => function ( + CTool $tool, + Argument $args, + \ArrayObject $context, + ResolveInfo $info + ) { + if ('events' === $info->fieldName) { + $resolver = $this->container->get('chamilo_api.graphql.resolver.course'); + + return $resolver->getAgenda($context); + } + + return $this->resolveField($info->fieldName, $tool); + }, + ], + 'CourseAgendaEvent' => [ + 'id' => function (array $event) { + return $event['unique_id']; + }, + 'description' => function (array $event) { + return $event['comment']; + }, + 'startDate' => function (array $event) { + return new \DateTime($event['start'], new \DateTimeZone('UTC')); + }, + 'endDate' => function (array $event) { + return new \DateTime($event['end'], new \DateTimeZone('UTC')); + }, + ], 'Session' => [ self::RESOLVE_FIELD => function ( Session $session, diff --git a/src/ApiBundle/GraphQL/Map/UnionMap.php b/src/ApiBundle/GraphQL/Map/UnionMap.php index 4926f6aa81..b820065acb 100644 --- a/src/ApiBundle/GraphQL/Map/UnionMap.php +++ b/src/ApiBundle/GraphQL/Map/UnionMap.php @@ -34,6 +34,8 @@ class UnionMap extends ResolverMap implements ContainerAwareInterface return 'ToolNotebook'; case TOOL_FORUM: return 'ToolForums'; + case TOOL_CALENDAR_EVENT: + return 'ToolAgenda'; } }, ], diff --git a/src/ApiBundle/GraphQL/Resolver/CourseResolver.php b/src/ApiBundle/GraphQL/Resolver/CourseResolver.php index ea436f694b..93ae03e7c7 100644 --- a/src/ApiBundle/GraphQL/Resolver/CourseResolver.php +++ b/src/ApiBundle/GraphQL/Resolver/CourseResolver.php @@ -343,4 +343,45 @@ class CourseResolver implements ContainerAwareInterface return $posts; } + + /** + * @param \ArrayObject $context + * + * @return array + */ + public function getAgenda(\ArrayObject $context): array + { + /** @var Session|null $session */ + $session = $context->offsetGet('session'); + /** @var Course $course */ + $course = $context->offsetGet('course'); + + $agenda = new \Agenda( + 'course', + $this->getCurrentUser()->getId(), + $course->getId(), + $session ? $session->getId() : 0 + ); + $result = $agenda->parseAgendaFilter(null); + $firstDay = new \DateTime('now', new \DateTimeZone('UTC')); + $firstDay->modify('first day of this month'); + $firstDay->setTime(0, 0); + $lastDay = new \DateTime('now', new \DateTimeZone('UTC')); + $lastDay->modify('last day of this month'); + $lastDay->setTime(0, 0); + + $groupId = current($result['groups']); + $userId = current($result['users']); + + $events = $agenda->getEvents( + $firstDay->getTimestamp(), + $lastDay->getTimestamp(), + $course->getId(), + $groupId, + $userId, + 'array' + ); + + return $events; + } } diff --git a/src/ApiBundle/GraphQL/Resources/config/schema.types.graphql b/src/ApiBundle/GraphQL/Resources/config/schema.types.graphql index d4f7785fbe..57a617d11e 100644 --- a/src/ApiBundle/GraphQL/Resources/config/schema.types.graphql +++ b/src/ApiBundle/GraphQL/Resources/config/schema.types.graphql @@ -207,6 +207,27 @@ type CourseForumPost { status: Int } +"A comprehensive diary/calendar tool" +type ToolAgenda { + name: String + category: String + image: String + customIcon: String + events: [CourseAgendaEvent!] +} + +type CourseAgendaEvent { + id: Int + className: String + title: String + description: String + startDate: DateTime + endDate: DateTime + allDay: Boolean + borderColor: String + backgroundColor: String +} + "A session registered on the platform." type Session { "The unique ID of the session." @@ -238,7 +259,7 @@ type SessionCategory { # Unions -union CourseTool = ToolDescription | ToolAnnouncements | ToolNotebook | ToolForums +union CourseTool = ToolDescription | ToolAnnouncements | ToolNotebook | ToolForums | ToolAgenda # Enums @@ -276,6 +297,7 @@ enum CourseToolType { TOOL_ANNOUNCEMENT TOOL_NOTEBOOK TOOL_FORUM + TOOL_CALENDAR_EVENT } # Scalars