GraphQL allow show learnpath #2644

pull/2715/head
Angel Fernando Quiroz Campos 7 years ago
parent ed82483372
commit e7d21a8d1c
  1. 1
      config/bundles.php
  2. 3
      config/routes/chamilo.yaml
  3. 16
      src/ApiBundle/ChamiloApiBundle.php
  4. 66
      src/ApiBundle/GraphQL/Controller/ApiController.php
  5. 1
      src/ApiBundle/GraphQL/Map/QueryMap.php
  6. 34
      src/ApiBundle/GraphQL/Resolver/CourseResolver.php
  7. 4
      src/ApiBundle/GraphQL/Resources/config/routing.yaml
  8. 1
      src/ApiBundle/GraphQL/Resources/config/schema.types.graphql

@ -54,6 +54,7 @@ return [
Chamilo\NotificationBundle\ChamiloNotificationBundle::class => ['all' => true],
Chamilo\SettingsBundle\ChamiloSettingsBundle::class => ['all' => true],
Chamilo\TimelineBundle\ChamiloTimelineBundle::class => ['all' => true],
Chamilo\ApiBundle\ChamiloApiBundle::class => ['all' => true],
winzou\Bundle\StateMachineBundle\winzouStateMachineBundle::class => ['all' => true],
Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle::class => ['all' => true],
Sylius\Bundle\ResourceBundle\SyliusResourceBundle::class => ['all' => true],

@ -25,3 +25,6 @@ chamilo_page:
chamilo_faq:
resource: "@ChamiloFaqBundle/Resources/config/routing.yml"
chamilo_graphql:
resource: "@ChamiloApiBundle/GraphQL/Resources/config/routing.yaml"

@ -0,0 +1,16 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\ApiBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Class ChamiloApiBundle.
*
* @package Chamilo\ApiBundle
*/
class ChamiloApiBundle extends Bundle
{
}

@ -0,0 +1,66 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\ApiBundle\GraphQL\Controller;
use Chamilo\UserBundle\Entity\User;
use Firebase\JWT\JWT;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
/**
* Class ApiController.
*
* @package Chamilo\ApiBundle\GraphQL\Controller
*/
class ApiController extends Controller
{
/**
* @Route("/learnpath/view", name="chamilo_graphql_learnpath")
*
* @param Request $request
*
* @return RedirectResponse
*
* @todo Use symfony router to generate url
*/
public function viewLearnpathAction(Request $request): RedirectResponse
{
$token = $request->query->get('token');
$secret = $this->getParameter('secret');
try {
$jwt = JWT::decode($token, $secret, ['HS384']);
} catch (\Exception $exception) {
throw $this->createAccessDeniedException($exception->getMessage());
}
/** @var User $user */
$user = $this->get('fos_user.user_manager')->find($jwt->data->user);
if (!$user) {
throw $this->createAccessDeniedException('Access denied.');
}
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->container->get('security.token_storage')->setToken($token);
$this->container->get('session')->set('_security_main', serialize($token));
$webCodePath = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?';
$urlParams = http_build_query([
'cidReq' => $jwt->data->course->code,
'id_session' => (int) $jwt->data->session,
'gidReq' => 0,
'gradebook' => 0,
'origin' => '',
'action' => 'view',
'lp_id' => (int) $jwt->data->lp,
'isStudentView' => 'true',
]);
return new RedirectResponse($webCodePath.$urlParams);
}
}

@ -344,6 +344,7 @@ class QueryMap extends ResolverMap implements ContainerAwareInterface
return $resolver->getLearnpathsByCategory($category, $context);
},
],
//'CourseLearnpath' => [],
'Session' => [
self::RESOLVE_FIELD => function (
Session $session,

@ -17,6 +17,7 @@ use Chamilo\CourseBundle\Entity\CTool;
use Chamilo\CourseBundle\Repository\CNotebookRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Firebase\JWT\JWT;
use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Error\UserError;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
@ -555,9 +556,42 @@ class CourseResolver implements ContainerAwareInterface
'id' => $lpId,
'title' => \Security::remove_XSS($lpInfo['lp_name']),
'progress' => (int) $progress,
'url' => $this->generateLpUrl($lpId, $course, $session),
];
}
return $lps;
}
/**
* @param int $lpId
* @param Course $course
* @param Session|null $session
*
* @return string
*/
private function generateLpUrl($lpId, Course $course, Session $session = null)
{
$user = $this->getCurrentUser();
$secret = $this->container->getParameter('secret');
$time = time();
$payload = [
'iat' => $time,
'exp' => $time + (60 * 30 * 1),
'data' => [
'user' => $user->getId(),
'lp' => (int) $lpId,
'course' => [
'id' => $course->getId(),
'code' => $course->getCode(),
],
'session' => $session ? $session->getId() : null,
],
];
$token = JWT::encode($payload, $secret, 'HS384');
return $this->container->get('router')->generate('chamilo_graphql_learnpath', ['token' => $token]);
}
}

@ -0,0 +1,4 @@
chamilo_graphql_course_learnpath:
resource: '@ChamiloApiBundle/GraphQL/Controller/ApiController.php'
type: annotation
prefix: /graphql

@ -266,6 +266,7 @@ type CourseLearnpath {
id: Int
title: String
progress: Int
url: String
}
"A session registered on the platform."

Loading…
Cancel
Save