GraphQL add course announcements query #2644
parent
9eca9ce79d
commit
0e36d97cf6
@ -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(); |
||||||
|
} |
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue