GraphQL add course announcements query #2644

pull/2715/head
Angel Fernando Quiroz Campos 6 years ago
parent 9eca9ce79d
commit 0e36d97cf6
  1. 88
      src/ApiBundle/GraphQL/Resolver/CourseAnnouncementResolver.php
  2. 1
      src/ApiBundle/GraphQL/Resolver/CourseResolver.php
  3. 2
      src/ApiBundle/GraphQL/Resolver/CourseToolResolver.php
  4. 74
      src/ApiBundle/GraphQL/Resolver/ToolAnnouncementsResolver.php
  5. 37
      src/ApiBundle/GraphQL/Resources/config/Query.types.yaml

@ -0,0 +1,88 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\ApiBundle\GraphQL\Resolver;
use Chamilo\ApiBundle\GraphQL\ApiGraphQLTrait;
use Chamilo\CourseBundle\Entity\CAnnouncement;
use Chamilo\CourseBundle\Entity\CItemProperty;
use Chamilo\UserBundle\Entity\User;
use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
/**
* Class CourseAnnouncementResolver.
*
* @package Chamilo\ApiBundle\GraphQL\Resolver
*/
class CourseAnnouncementResolver implements ResolverInterface, ContainerAwareInterface
{
use ApiGraphQLTrait;
/**
* @param array $item
* @param Argument $args
* @param ResolveInfo $info
* @param \ArrayObject $context
*
* @return mixed
*/
public function __invoke(array $item, Argument $args, ResolveInfo $info, \ArrayObject $context)
{
/** @var CAnnouncement $announcement */
$announcement = $item['announcement'];
/** @var CItemProperty $itemProperty */
$itemProperty = $item['item_property'];
$method = 'resolve'.ucfirst($info->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();
}
}

@ -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;

@ -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');
}
}
}

@ -0,0 +1,74 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\ApiBundle\GraphQL\Resolver;
use Chamilo\ApiBundle\GraphQL\ApiGraphQLTrait;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CourseBundle\Entity\CAnnouncement;
use Chamilo\CourseBundle\Entity\CTool;
use GraphQL\Error\UserError;
use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
/**
* Class ToolAnnouncementsResolver.
*
* @package Chamilo\ApiBundle\GraphQL\Resolver
*/
class ToolAnnouncementsResolver implements ResolverInterface, ContainerAwareInterface
{
use ApiGraphQLTrait;
use CourseToolResolverTrait;
/**
* @param CTool $tool
* @param Argument $args
* @param \ArrayObject $context
*
* @return array
*/
public function resolveAnnouncements(CTool $tool, Argument $args, \ArrayObject $context): array
{
/** @var Course $course */
$course = $context->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;
}
}

@ -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'

Loading…
Cancel
Save