GraphQL Add query for user sessions #2644

pull/2715/head
Angel Fernando Quiroz Campos 6 years ago
parent 6f47f4492f
commit 9798310e5a
  1. 56
      src/ApiBundle/GraphQL/Resolver/SessionCategoryResolver.php
  2. 71
      src/ApiBundle/GraphQL/Resolver/SessionResolver.php
  3. 166
      src/ApiBundle/GraphQL/Resolver/UserResolver.php
  4. 59
      src/ApiBundle/GraphQL/Resources/config/Query.types.yaml

@ -0,0 +1,56 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\ApiBundle\GraphQL\Resolver;
use Chamilo\ApiBundle\GraphQL\ApiGraphQLTrait;
use Chamilo\CoreBundle\Entity\SessionCategory;
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
/**
* Class SessionCategoryResolver
*
* @package Chamilo\ApiBundle\GraphQL\Resolver
*/
class SessionCategoryResolver implements ResolverInterface, AliasedInterface, ContainerAwareInterface
{
use ApiGraphQLTrait;
/**
* Returns methods aliases.
*
* For instance:
* array('myMethod' => 'myAlias')
*
* @return array
*/
public static function getAliases(): array
{
return [
'resolveStartDate' => 'sessioncategory_startdate',
'resolveEndDate' => 'sessioncategory_enddate',
];
}
/**
* @param SessionCategory $category
*
* @return \DateTime
*/
public function resolveStartDate(SessionCategory $category): \DateTime
{
return $category->getDateStart();
}
/**
* @param SessionCategory $category
*
* @return \DateTime
*/
public function resolveEndDate(SessionCategory $category): \DateTime
{
return $category->getDateEnd();
}
}

@ -0,0 +1,71 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\ApiBundle\GraphQL\Resolver;
use Chamilo\ApiBundle\GraphQL\ApiGraphQLTrait;
use Chamilo\CoreBundle\Entity\Session;
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
/**
* Class SessionResolver
*
* @package Chamilo\ApiBundle\GraphQL\Resolver
*/
class SessionResolver implements ResolverInterface, AliasedInterface, ContainerAwareInterface
{
use ApiGraphQLTrait;
/**
* Returns methods aliases.
*
* For instance:
* array('myMethod' => 'myAlias')
*
* @return array
*/
public static function getAliases(): array
{
return [
'resolveDescription' => 'session_description',
'resolveNumberCourses' => 'session_nbrcourses',
'resolveNumberUsers' => 'session_nbrusers',
];
}
/**
* @param Session $session
*
* @return string
*/
public function resolveDescription(Session $session): string
{
if (false === $session->getShowDescription()) {
return '';
}
return $session->getDescription();
}
/**
* @param Session $session
*
* @return int
*/
public function resolveNumberCourses(Session $session): int
{
return $session->getNbrCourses();
}
/**
* @param Session $session
*
* @return int
*/
public function resolveNumberUsers(Session $session): int
{
return $session->getNbrUsers();
}
}

@ -36,6 +36,7 @@ class UserResolver implements ResolverInterface, AliasedInterface, ContainerAwar
'resolveUserMessages' => 'user_messages',
'resolveMessageContacts' => 'user_message_contacts',
'resolveCourses' => 'user_courses',
'resolveSessions' => 'user_sessions',
];
}
@ -142,4 +143,169 @@ class UserResolver implements ResolverInterface, AliasedInterface, ContainerAwar
return $users;
}
/**
* @param User $user
*
* @todo Based on UserManager::get_sessions_by_category. Review to integrate Symfony
*
* @return array
*/
private function getUserSessions(User $user)
{
$allowOrder = api_get_configuration_value('session_list_order');
$showAllSessions = api_get_configuration_value('show_all_sessions_on_my_course_page') === true;
$orderBySettings = api_get_configuration_value('my_courses_session_order');
$position = '';
if ($allowOrder) {
$position = ', s.position AS position ';
}
$now = api_get_utc_datetime(null, false, true);
$dql = "SELECT DISTINCT
s.id,
s.accessEndDate AS access_end_date,
s.duration,
CASE WHEN s.accessEndDate IS NULL THEN 1 ELSE 0 END HIDDEN _isFieldNull
$position
FROM ChamiloCoreBundle:Session AS s
LEFT JOIN ChamiloCoreBundle:SessionRelCourseRelUser AS scu WITH scu.session = s
INNER JOIN ChamiloCoreBundle:AccessUrlRelSession AS url WITH url.session = s.id
LEFT JOIN ChamiloCoreBundle:SessionCategory AS sc WITH s.category = sc
WHERE (scu.user = :user OR s.generalCoach = :user) AND url.url = :url";
$order = "ORDER BY sc.name, s.name";
if ($showAllSessions) {
$order = "ORDER BY s.accessStartDate";
}
if ($allowOrder) {
$order = "ORDER BY s.position";
}
if (!empty($orderBySettings) && isset($orderBySettings['field']) && isset($orderBySettings['order'])) {
$field = $orderBySettings['field'];
$orderSetting = $orderBySettings['order'];
switch ($field) {
case 'start_date':
$order = "ORDER BY s.accessStartDate $orderSetting";
break;
case 'end_date':
$order = " ORDER BY s.accessEndDate $orderSetting ";
if ($orderSetting == 'asc') {
// Put null values at the end
// https://stackoverflow.com/questions/12652034/how-can-i-order-by-null-in-dql
$order = "ORDER BY _isFieldNull asc, s.accessEndDate asc";
}
break;
}
}
$results = [];
$rows = $this->em
->createQuery("$dql $order")
->setParameters(
[
'user' => $user->getId(),
'url' => api_get_current_access_url_id(),
]
)
->getResult();
foreach ($rows as $row) {
$coachList = \SessionManager::getCoachesBySession($row['id']);
$courseList = \UserManager::get_courses_list_by_session(
$user->getId(),
$row['id']
);
$daysLeft = \SessionManager::getDayLeftInSession(
['id' => $row['id'], 'duration' => $row['duration']],
$user->getId()
);
$isGeneralCoach = \SessionManager::user_is_general_coach($user->getId(), $row['id']);
$isCoachOfCourse = in_array($user->getId(), $coachList);
if (!$isGeneralCoach && !$isCoachOfCourse) {
// Teachers can access the session depending in the access_coach date
if ($row['duration']) {
if ($daysLeft <= 0) {
continue;
}
} else {
if (isset($row['access_end_date']) && !empty($row['access_end_date'])) {
if ($row['access_end_date'] <= $now) {
continue;
}
}
}
}
$visibility = api_get_session_visibility($row['id'], null, false);
if ($visibility != SESSION_VISIBLE) {
// Course Coach session visibility.
$blockedCourseCount = 0;
$closedVisibilityList = [COURSE_VISIBILITY_CLOSED, COURSE_VISIBILITY_HIDDEN];
$sessionCourseVisibility = SESSION_INVISIBLE;
foreach ($courseList as $course) {
// Checking session visibility
$sessionCourseVisibility = api_get_session_visibility(
$row['id'],
$course['real_id'],
false
);
$courseIsVisible = !in_array($course['visibility'], $closedVisibilityList);
if ($courseIsVisible === false || $sessionCourseVisibility == SESSION_INVISIBLE) {
$blockedCourseCount++;
}
}
// If all courses are blocked then no show in the list.
if ($blockedCourseCount !== count($courseList)) {
$visibility = $sessionCourseVisibility;
}
}
if ($visibility == SESSION_INVISIBLE) {
continue;
}
$results[] = $row['id'];
}
return $results;
}
/**
* @param User $user
* @param \ArrayObject $context
*
* @return array
*/
public function resolveSessions(User $user, \ArrayObject $context): array
{
$this->protectUserData($context, $user);
$sessionsId = $this->getUserSessions($user);
$qb = $this->em->createQueryBuilder();
$result = $qb
->select('s')
->from('ChamiloCoreBundle:Session', 's')
->where(
$qb->expr()->in('s.id', $sessionsId)
)
->getQuery()
->getResult();
return $result;
}
}

@ -64,6 +64,10 @@ User:
description: 'Course list for the current user.'
type: '[Course]'
resolve: "@=resolver('user_courses', [value, context])"
sessions:
description: 'Session list for the current user.'
type: '[Session]'
resolve: "@=resolver('user_sessions', [value, context])"
UserMessage:
type: object
@ -126,3 +130,58 @@ Course:
description: 'List of names from available tools for student view.'
type: '[String]'
resolve: "@=resolver('course_tools', [value, context])"
SessionCategory:
type: object
config:
description: 'A session category.'
fields:
id:
description: 'The unique ID of the category.'
type: 'Int'
name:
type: 'String'
startDate:
type: 'DateTime'
resolve: "@=resolver('sessioncategory_startdate', [value])"
endDate:
type: 'DateTime'
resolve: "@=resolver('sessioncategory_enddate', [value])"
Session:
type: object
config:
description: 'A session registered on the platform.'
fields:
id:
description: 'The unique ID of the session.'
type: 'Int'
name:
type: 'String'
category:
type: 'SessionCategory'
description:
type: 'String'
resolve: "@=resolver('session_description', [value])"
numberOfCourses:
type: 'Int'
resolve: "@=resolver('session_nbrcourses', [value])"
numberOfUsers:
type: 'Int'
resolve: "@=resolver('session_nbrusers', [value])"
duration:
type: 'Int'
displayStartDate:
type: 'DateTime'
displayEndDate:
type: 'DateTime'
accessStartDate:
type: 'DateTime'
accessEndDate:
type: 'DateTime'
coachAccessStartDate:
type: 'DateTime'
coachAccessEndDate:
type: 'DateTime'
generalCoach:
type: 'User'

Loading…
Cancel
Save