Merge branch 'master' of https://github.com/chamilo/chamilo-lms
commit
f801c1a42d
@ -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; |
||||
} |
||||
} |
||||
@ -0,0 +1,190 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CourseBundle\Entity\Manager; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\CourseBundle\Entity\CGroupInfo; |
||||
use Chamilo\UserBundle\Entity\User; |
||||
use Sonata\CoreBundle\Model\BaseEntityManager; |
||||
|
||||
class AnnouncementManager extends BaseEntityManager |
||||
{ |
||||
|
||||
/** |
||||
* @param User $user |
||||
* @param Course $course |
||||
* @param CGroupInfo|null $group |
||||
* @param Session|null $session |
||||
* @param bool $allowUserEditSetting |
||||
* @param bool $allowOnlyGroup |
||||
* @param bool $getCount |
||||
* @param int|null $start |
||||
* @param int|null $limit |
||||
* @param string $titleToSearch |
||||
* @param User|null $userToSearch |
||||
* |
||||
* @return mixed |
||||
* @throws \Doctrine\ORM\NonUniqueResultException |
||||
*/ |
||||
public function getAnnouncements( |
||||
User $user, |
||||
Course $course, |
||||
CGroupInfo $group = null, |
||||
Session $session = null, |
||||
$allowUserEditSetting, |
||||
$allowOnlyGroup, |
||||
$getCount = false, |
||||
$start = null, |
||||
$limit = null, |
||||
$titleToSearch = '', |
||||
User $userToSearch = null |
||||
) { |
||||
$sessionId = $session ? $session->getId() : 0; |
||||
|
||||
$conditionSession = api_get_session_condition( |
||||
$sessionId, |
||||
true, |
||||
true, |
||||
'announcement.sessionId' |
||||
); |
||||
|
||||
$select = 'DISTINCT announcement, ip'; |
||||
$groupBy = 'GROUP BY announcement.iid'; |
||||
|
||||
if ($getCount) { |
||||
$groupBy = ''; |
||||
$select = 'COUNT(DISTINCT announcement) AS count'; |
||||
} |
||||
|
||||
$parameters = []; |
||||
$searchCondition = ''; |
||||
|
||||
if (!empty($titleToSearch)) { |
||||
$parameters['search_title'] = "%$titleToSearch%"; |
||||
$searchCondition .= " AND (title LIKE :search_title) "; |
||||
} |
||||
|
||||
if (!empty($userToSearch)) { |
||||
$searchCondition .= " AND (ip.insertUser = ".$userToSearch->getId().") "; |
||||
} |
||||
|
||||
$extraGroupCondition = ''; |
||||
|
||||
if ($allowOnlyGroup) { |
||||
$extraGroupCondition = ' AND ip.group = '.$group->getId().' '; |
||||
} |
||||
|
||||
if (api_is_allowed_to_edit(false, true) |
||||
|| ($allowUserEditSetting && !api_is_anonymous()) |
||||
) { |
||||
$dqlCondition = "AND (ip.visibility = 0 OR ip.visibility = 1)"; |
||||
|
||||
if (!empty($group)) { |
||||
$dqlCondition = "AND ip.visibility != 2 AND |
||||
(ip.group = ".$group->getId()." OR ip.group IS NULL ) |
||||
$extraGroupCondition"; |
||||
} |
||||
} else { |
||||
$groupMemberships = \GroupManager::get_group_ids( |
||||
$course->getId(), |
||||
$user->getId() |
||||
); |
||||
|
||||
if (is_array($groupMemberships) && count($groupMemberships) > 0) { |
||||
if ($allowUserEditSetting && !api_is_anonymous()) { |
||||
$condUserId = " AND ( |
||||
ip.lasteditUserId = ".$user->getId()." OR( |
||||
(ip.toUser = ".$user->getId()." OR ip.toUser IS NULL) OR |
||||
(ip.group IS NULL OR ip.group IN (0, ".implode(', ', $groupMemberships).")) |
||||
) |
||||
) "; |
||||
|
||||
if (!empty($group)) { |
||||
$condUserId = " AND ( |
||||
ip.lasteditUserId = ".$user->getId()." OR ip.group IS NULL OR ip.group IN (0, ".$group->getId() |
||||
.") |
||||
) ".$extraGroupCondition; |
||||
} |
||||
} else { |
||||
$condUserId = " AND ( |
||||
(ip.toUser = ".$user->getId().") OR ip.toUser IS NULL) AND |
||||
(ip.group IS NULL OR ip.group IN (0, ".implode(', ', $groupMemberships).")) |
||||
) "; |
||||
|
||||
if (!empty($group)) { |
||||
$condUserId = " AND ( |
||||
(ip.toUser = ".$user->getId().") OR ip.toUser IS NULL) AND |
||||
(ip.group IS NULL OR ip.group IN (0, ".$group->getId().")) |
||||
) ".$extraGroupCondition; |
||||
} |
||||
} |
||||
|
||||
$dqlCondition = "$condUserId AND ip.visibility = 1"; |
||||
} else { |
||||
if (!empty($user->getId())) { |
||||
$condUserId = " AND ( |
||||
(ip.toUser = ".$user->getId()." OR ip.toUser IS NULL) AND |
||||
(ip.group = 0 OR ip.group IS NULL) |
||||
) "; |
||||
|
||||
if ($allowUserEditSetting && !api_is_anonymous()) { |
||||
$condUserId = " AND ( |
||||
ip.lasteditUserId = ".$user->getId()." OR |
||||
( |
||||
(ip.toUser = ".$user->getId()." OR ip.toUser IS NULL) AND |
||||
(ip.group = 0 OR ip.group IS NULL) |
||||
) |
||||
) "; |
||||
} |
||||
|
||||
$dqlCondition = "$condUserId |
||||
AND ip.visibility = 1 |
||||
AND announcement.sessionId IN (0, $sessionId)"; |
||||
} else { |
||||
$condUserId = " AND ip.group = 0 OR ip.group IS NULL "; |
||||
|
||||
if ($allowUserEditSetting && !api_is_anonymous()) { |
||||
$condUserId = " AND ( |
||||
ip.lastEditUserId = ".$user->getId()." OR ip.group = 0 OR ip.group IS NULL |
||||
) "; |
||||
} |
||||
|
||||
$dqlCondition = "$condUserId |
||||
AND ip.visibility = 1 |
||||
AND announcement.sessionId IN (0, $sessionId)"; |
||||
} |
||||
} |
||||
} |
||||
|
||||
$dql = "SELECT $select |
||||
FROM ChamiloCourseBundle:CAnnouncement announcement |
||||
INNER JOIN ChamiloCourseBundle:CItemProperty AS ip |
||||
WITH (announcement.id = ip.ref AND announcement.cId = ip.course) |
||||
WHERE |
||||
ip.tool = 'announcement' AND |
||||
announcement.cId = ".$course->getId()." |
||||
$dqlCondition |
||||
$conditionSession |
||||
$searchCondition |
||||
$groupBy |
||||
ORDER BY announcement.displayOrder DESC"; |
||||
|
||||
$query = $this->getEntityManager() |
||||
->createQuery($dql) |
||||
->setParameters($parameters); |
||||
|
||||
if (!is_null($start) && !is_null($limit)) { |
||||
$query |
||||
->setFirstResult($start) |
||||
->setMaxResults($limit); |
||||
} |
||||
|
||||
if ($getCount) { |
||||
return $query->getResult(); |
||||
} |
||||
|
||||
return $query->getResult(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue