class CertificateTabController in Course 8.2
Same name and namespace in other branches
- 8.3 modules/course_certificate/src/Controller/CertificateTabController.php \Drupal\course_certificate\Controller\CertificateTabController
An example controller.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\course_certificate\Controller\CertificateTabController
Expanded class hierarchy of CertificateTabController
File
- modules/
course_certificate/ src/ Controller/ CertificateTabController.php, line 21
Namespace
Drupal\course_certificate\ControllerView source
class CertificateTabController extends ControllerBase {
/**
* Helper to plugin current user when not provided in path
* @param Course $course
* @return main cert tab
*/
public function renderDefaultCertificateTab(Course $course) {
$account = Drupal::currentUser();
return $this
->renderCertificateTab($course, $account);
}
/**
* Helper to plugin current user when not provided in path
* @param Course $course
* @return main cert tab
*/
public function accessDefaultTab(Course $course) {
$account = Drupal::currentUser();
return $this
->accessTab($course, $account);
}
/**
* @param EntityInterface $course
* The entity this belongs to
* @param AccountInterface $account
* The user account to check
* @return \Drupal\Core\Access\AccessResultInterface
* An access result
*/
public function accessTab(Course $course, AccountInterface $account = NULL) {
$has_object = FALSE;
$account = $account ?? Drupal::currentUser();
$admin = $account
->hasPermission('administer certificates');
$view_all = $account
->hasPermission('view all user certificates');
$access_result = AccessResult::forbidden("Access not granted");
if (!$account
->id()) {
return AccessResult::forbidden("Not a valid user");
}
// Does the course have a certificate object?
foreach ($course
->getObjects() as $courseObject) {
if ($courseObject
->getComponent() == 'certificate') {
$has_object = TRUE;
break;
}
}
if (!$has_object) {
return AccessResult::forbidden("No certificate object provided");
}
// Are they enrolled?
$enrollments = $this
->entityTypeManager()
->getStorage('course_enrollment')
->loadByProperties([
'uid' => $account
->id(),
'cid' => $course
->id(),
]);
if (empty($enrollments)) {
$access_result = AccessResult::forbidden();
}
else {
$enrollment = reset($enrollments);
// Are they an admin or have they completed the course?
if ($admin || $view_all || $enrollment
->isComplete()) {
$access_result = AccessResult::allowed();
}
}
return $access_result;
}
/**
* Full tab
* @param Course $course
* @param AccountInterface $account
* @return type
*/
function renderCertificateTab(Course $course, AccountInterface $account) {
// Get all templates for this entity combo
$render = [];
$valid_certs = [];
$global_certs = CertificateMapping::getGlobalCertificateMappings();
$certificate_mappers = Drupal::service('plugin.manager.certificate_mapper');
$map_defs = $certificate_mappers
->getDefinitions();
$certs = $course
->get('certificate')
->referencedEntities();
//Default to load a page
$render['info']['#markup'] = '';
foreach ($map_defs as $map_key => $maps) {
$plugin = $certificate_mappers
->createInstance($map_key, [
'of' => 'configuration values',
]);
$matches = $plugin
->processMapping($course, $account) ?? [];
foreach ($matches as $match) {
foreach ($certs as $local) {
if ($local
->isMatch($map_key, $match)) {
$valid_certs["{$map_key}.{$match}"] = $local
->get('cid')->value;
}
}
// If local is not set, check the global mappings
if (!isset($valid_certs["{$map_key}.{$match}"])) {
$render['table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Type'),
$this
->t('Download'),
],
];
foreach ($global_certs as $global) {
if ($global
->isMatch($map_key, $match) && $global
->get('cid')->value !== '-1') {
$valid_certs["{$map_key}.{$match}"] = $global
->get('cid')->value;
}
}
}
elseif ($valid_certs["{$map_key}.{$match}"] == '-1') {
unset($valid_certs["{$map_key}.{$match}"]);
}
}
}
// Return markup if we need to present messages
if (count($valid_certs) > 1) {
$render['info']['#markup'] = ' You are eligible for multiple certificates.';
}
if (empty($valid_certs)) {
$render['info']['#markup'] = 'You are not eligible for a certificate';
}
foreach ($valid_certs as $cert_name => $val) {
$opts = [
'course' => $course
->id(),
'account' => $account
->id(),
'template' => $val,
];
$render['table'][$val] = [
'type' => [
'#markup' => $cert_name,
],
'download' => Link::createFromRoute(t('Download certificate'), 'certificate.course.pdf', $opts)
->toRenderable(),
];
}
return $render;
}
/**
* Downloads
*
* @param Course $course
* @param AccountInterface $account
* @param CertificateTemplate $template
* @return type
*/
function accessPdf(Course $course, AccountInterface $account, CertificateTemplate $template) {
return $this
->accessTab($course, $account);
}
/**
* Stream a PDF to the browser
* @param Course $course
* @param AccountInterface $account
* @param CertificateTemplate $template
* @return type
*/
function returnPdf(Course $course, AccountInterface $account, CertificateTemplate $template) {
$pdf_gen = $template
->loadPrintableEngine();
// Check for a PDF engine
if ($pdf_gen === FALSE) {
$current_user = \Drupal::currentUser();
$msg = t('Current site configuration does not allow PDF file creation. Please contact an administrator.');
if ($current_user
->hasPermission('administer printable')) {
$url = Url::fromRoute('printable.format_configure_pdf');
$link = Link::createFromRoute('configure a PDF library', 'printable.format_configure_pdf');
$msg = t('Please @link to print certificates.', [
'@link' => $link
->toString(),
]);
}
return [
'#markup' => $msg,
];
}
// Everything is configured, build the PDF
$render = $template
->renderView($account, $course);
$pdf_gen
->addPage(render($render));
return $pdf_gen
->stream($pdf_gen
->getObject()
->getPdfFilename());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CertificateTabController:: |
public | function | Helper to plugin current user when not provided in path | |
CertificateTabController:: |
function | Downloads | ||
CertificateTabController:: |
public | function | ||
CertificateTabController:: |
function | Full tab | ||
CertificateTabController:: |
public | function | Helper to plugin current user when not provided in path | |
CertificateTabController:: |
function | Stream a PDF to the browser | ||
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
40 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |