Skip to content
Snippets Groups Projects
Commit 2ad87c4b authored by catch's avatar catch
Browse files

Working controller.

parent 5e25d038
No related branches found
No related tags found
No related merge requests found
......@@ -5,3 +5,8 @@ lms_certificate.certificate:
_title: 'Certificate'
requirements:
_custom_access: '\Drupal\lms_certificate_fillpdf\Controller\CertificateController::access'
options:
parameters:
type: entity:group
bundle:
- course
......@@ -7,27 +7,78 @@ namespace Drupal\lms_certificate_fillpdf\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Controller\ControllerResolverInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\fillpdf\Controller\HandlePdfController;
use Drupal\fillpdf\Service\FillPdfLinkManipulator;
use Drupal\lms\TrainingManager;
use Drupal\lms\Entity\Bundle\Course;
use Drupal\lms\Entity\CourseStatusInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class CertificateController extends ControllerBase {
public function __construct(protected ControllerResolverInterface $controllerResolver) {}
public function __construct(
protected ControllerResolverInterface $controllerResolver,
protected TrainingManager $trainingManager,
protected RequestStack $requestStack,
protected FillPdfLinkManipulator $linkManipulator,
protected HttpKernelInterface $httpKernel,
) {}
public static function create(ContainerInterface $container) {
return new static(
$container->get('controller_resolver')
$container->get('controller_resolver'),
$container->get('lms.training_manager'),
$container->get('request_stack'),
$container->get('fillpdf.link_manipulator'),
$container->get('http_kernel'),
);
}
public function getCertificate(GroupInterface $group) {
$controller_resolver = \Drupal::service('controller_resolver');
$controller = $controller_resolver->getControllerFromDefinition(HandlePdfController::class . '::populatePdf');
return $controller();
// Re-use the fillpdf controller to generate the merged PDF form
// certificate. The controller relies on query arguments from the current
// URL so call it via a subrequest.
// @todo if fill PDF adds an API to generate a PDF from context, use that
// instead.
$fid = $group->get('certificate')->entity->id();
$course_status = $this->trainingManager->loadCourseStatus($group, $this->currentUser(), ['status' => CourseStatusInterface::STATUS_PASSED]);
$query = [
'fid' => $fid,
'entity_ids' => [
'user' => [$this->currentUser()->id()],
'group' => [$group->id()],
'lms_course_status' => [$course_status->id()],
],
];
$link = $this->linkManipulator->generateLink($query);
$sub_request = Request::create($link->toString());
$response = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
return $response;
}
public function access(GroupInterface $group) {
return AccessResult::allowed();
$access_result = AccessResult::allowedIf(!$group->get('certificate')->isEmpty())->addCacheableDependency($group);
$course_status = $this->trainingManager->loadCourseStatus($group, $this->currentUser(), ['status' => CourseStatusInterface::STATUS_PASSED]);
$access_result->andIf(AccessResult::allowedIf($course_status));
if ($course_status) {
// If the user has passed, cache the result - the situation can only
// change if the course status changes again to a non-passed state.
$access_result->addCacheableDependency($course_status);
}
else {
$access_result->setcacheMaxAge(0);
}
// Set to cache per user even though the resulting response may be
// uncacheable.
$access_result->cachePerUser();
return $access_result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment