diff --git a/src/ApiBundle/GraphQL/Resolver/CourseAnnouncementResolver.php b/src/ApiBundle/GraphQL/Resolver/CourseAnnouncementResolver.php new file mode 100644 index 0000000000..2c12aa5c50 --- /dev/null +++ b/src/ApiBundle/GraphQL/Resolver/CourseAnnouncementResolver.php @@ -0,0 +1,88 @@ +fieldName); + + if (method_exists($this, $method)) { + return $this->$method($announcement, $itemProperty, $args, $context); + } + + $method = 'get'.ucfirst($info->fieldName); + + if (method_exists($announcement, $method)) { + return $announcement->$method(); + } + + if (method_exists($itemProperty, $method)) { + return $itemProperty->$method(); + } + + return null; + } + + /** + * @param CAnnouncement $announcement + * + * @return int + */ + public function resolveId(CAnnouncement $announcement) + { + return $announcement->getIid(); + } + + /** + * @param CAnnouncement $announcement + * @param CItemProperty $itemProperty + * + * @return User + */ + public function resolveBy(CAnnouncement $announcement, CItemProperty $itemProperty) + { + return $itemProperty->getInsertUser(); + } + + /** + * @param CAnnouncement $announcement + * @param CItemProperty $itemProperty + * + * @return \DateTime + */ + public function resolveLastUpdateDate(CAnnouncement $announcement, CItemProperty $itemProperty) + { + return $itemProperty->getLasteditDate(); + } +} \ No newline at end of file diff --git a/src/ApiBundle/GraphQL/Resolver/CourseResolver.php b/src/ApiBundle/GraphQL/Resolver/CourseResolver.php index e426fd127c..f90c4a3443 100644 --- a/src/ApiBundle/GraphQL/Resolver/CourseResolver.php +++ b/src/ApiBundle/GraphQL/Resolver/CourseResolver.php @@ -113,6 +113,7 @@ class CourseResolver implements ResolverInterface, ContainerAwareInterface $tools = array_filter($tools, function ($tool) { switch ($tool['name']) { case TOOL_COURSE_DESCRIPTION: + case TOOL_ANNOUNCEMENT: return true; default: return false; diff --git a/src/ApiBundle/GraphQL/Resolver/CourseToolResolver.php b/src/ApiBundle/GraphQL/Resolver/CourseToolResolver.php index c71395e355..5dab76a8c0 100644 --- a/src/ApiBundle/GraphQL/Resolver/CourseToolResolver.php +++ b/src/ApiBundle/GraphQL/Resolver/CourseToolResolver.php @@ -52,6 +52,8 @@ class CourseToolResolver implements ResolverInterface, ContainerAwareInterface switch ($tool->getName()) { case TOOL_COURSE_DESCRIPTION: return $this->typeResolver->resolve('ToolDescription'); + case TOOL_ANNOUNCEMENT: + return $this->typeResolver->resolve('ToolAnnouncements'); } } } diff --git a/src/ApiBundle/GraphQL/Resolver/ToolAnnouncementsResolver.php b/src/ApiBundle/GraphQL/Resolver/ToolAnnouncementsResolver.php new file mode 100644 index 0000000000..309c63c5c9 --- /dev/null +++ b/src/ApiBundle/GraphQL/Resolver/ToolAnnouncementsResolver.php @@ -0,0 +1,74 @@ +offsetGet('course'); + /** @var Session $session */ + $session = null; + + if ($context->offsetExists('session')) { + $session = $context->offsetGet('session'); + } + + $em = $this->container->get('chamilo_course.entity.manager.announcement_manager'); + + try { + $announcementsInfo = $em->getAnnouncements( + $this->getCurrentUser(), + $course, + null, + $session, + api_get_course_setting('allow_user_edit_announcement') === 'true', + api_get_configuration_value('hide_base_course_announcements_in_group') === true + ); + } catch (\Exception $exception) { + throw new UserError($exception->getMessage()); + } + + if (empty($announcementsInfo)) { + return []; + } + + $announcements = []; + + for ($z = 0; $z < count($announcementsInfo); $z += 2) { + $announcements[] = [ + 'announcement' => $announcementsInfo[$z], + 'item_property' => $announcementsInfo[$z + 1], + ]; + } + + return $announcements; + } +} \ No newline at end of file diff --git a/src/ApiBundle/GraphQL/Resources/config/Query.types.yaml b/src/ApiBundle/GraphQL/Resources/config/Query.types.yaml index 629bd54bc9..b9e0c47c3a 100644 --- a/src/ApiBundle/GraphQL/Resources/config/Query.types.yaml +++ b/src/ApiBundle/GraphQL/Resources/config/Query.types.yaml @@ -241,3 +241,40 @@ CourseDescription: type: 'String' type: type: 'Int' + +ToolAnnouncements: + type: object + config: + description: 'Announcements related to the course.' + interfaces: [CourseTool] + resolveField: '@=resolver("Chamilo\\ApiBundle\\GraphQL\\Resolver\\ToolAnnouncementsResolver", [value, args, info, context])' + fields: + name: + type: 'String' + category: + type: 'String' + image: + type: 'String' + customIcon: + type: 'String' + isVisible: + type: 'Boolean' + announcements: + type: '[CourseAnnouncement]!' + +CourseAnnouncement: + type: object + config: + description: 'Course announcement.' + resolveField: '@=resolver("Chamilo\\ApiBundle\\GraphQL\\Resolver\\CourseAnnouncementResolver", [value, args, info, context])' + fields: + id: + type: 'Int' + title: + type: 'String' + content: + type: 'String' + by: + type: 'User' + lastUpdateDate: + type: 'DateTime'