public function UserController::buildBadges in Opigno statistics 3.x
Same name and namespace in other branches
- 8 src/Controller/UserController.php \Drupal\opigno_statistics\Controller\UserController::buildBadges()
Builds render array for a user badges block.
Parameters
\Drupal\user\UserInterface $user: User.
Return value
array Render array.
File
- src/
Controller/ UserController.php, line 246
Class
- UserController
- Statistics user controller.
Namespace
Drupal\opigno_statistics\ControllerCode
public function buildBadges(UserInterface $user) {
$output = [];
if (\Drupal::moduleHandler()
->moduleExists('opigno_module')) {
try {
$opigno_module_fields = \Drupal::entityTypeManager()
->getStorage('opigno_module')
->getFieldStorageDefinitions();
} catch (\Exception $e) {
$this
->messenger()
->addMessage($e
->getMessage(), 'error');
return [];
}
if (array_key_exists('badge_active', $opigno_module_fields)) {
$uid = $user
->id();
// Get user modules and courses with active badges.
$modules = OpignoModuleBadges::opignoModuleGetUserActiveBadgesModules($uid);
// Prepare active badges data.
$rows = [];
if ($modules) {
foreach ($modules as $module) {
if (!empty($module->field_media_image_target_id)) {
$fid = $module->field_media_image_target_id;
$file = File::load($fid);
$variables = [
'style_name' => 'thumbnail',
'uri' => $file
->getFileUri(),
];
$image = \Drupal::service('image.factory')
->get($file
->getFileUri());
if ($image
->isValid()) {
$variables['width'] = $image
->getWidth();
$variables['height'] = $image
->getHeight();
}
else {
$variables['width'] = $variables['height'] = NULL;
}
$logo_render_array = [
'#theme' => 'image_style',
'#width' => $variables['width'],
'#height' => $variables['height'],
'#style_name' => $variables['style_name'],
'#uri' => $variables['uri'],
];
$badge_image = render($logo_render_array);
}
else {
$badge_image = '';
}
$badge_description_html = !empty($module->badge_description) ? [
'data' => [
'#markup' => $module->badge_description,
],
] : '';
// Get existing badge count.
$badges = OpignoModuleBadges::opignoModuleGetBadges($uid, $module->gid, $module->typology, $module->entity_id);
if (!empty($module->name) && !empty($module->badge_criteria) && !empty($badges)) {
if ($module->badge_criteria == 'finished' && in_array($module->status, [
'passed',
'failed',
]) || $module->badge_criteria == 'success' && $module->status == 'passed') {
$rows[] = [
'data-id' => $module->entity_id,
'data-typology' => strtolower($module->typology),
'data' => [
$module->training,
$badge_image,
$module->badge_name,
$badge_description_html,
$badges,
],
];
}
}
}
}
if ($rows) {
$output = [
'#type' => 'container',
'#attributes' => [
'class' => [
'badges',
],
],
[
'#type' => 'table',
'#attributes' => [
'class' => [
'statistics-table',
'table-striped',
],
],
'#header' => [
$this
->t('Training'),
'',
$this
->t('Badge'),
$this
->t('Description'),
$this
->t('Earned'),
],
'#rows' => $rows,
],
];
}
}
}
return $output;
}