public function DashboardForm::buildForm in Opigno statistics 8
Same name and namespace in other branches
- 3.x src/Form/DashboardForm.php \Drupal\opigno_statistics\Form\DashboardForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ DashboardForm.php, line 390
Class
- DashboardForm
- Implements the statistics dashboard.
Namespace
Drupal\opigno_statistics\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$moduleHandler = \Drupal::service('module_handler');
$query = $this->database
->select('opigno_learning_path_achievements', 'a');
$query
->addExpression('YEAR(a.registered)', 'year');
$data = $query
->groupBy('year')
->orderBy('year', 'DESC')
->execute()
->fetchAll();
$years = [
'none' => $this
->t('- None -'),
];
foreach ($data as $row) {
$year = $row->year;
if (!isset($years[$year])) {
$years[$year] = $year;
}
}
$max_year = !empty($years) ? max(array_keys($years)) : NULL;
$year_select = [
'#type' => 'select',
'#title' => $this
->t('Year'),
'#title_display' => 'invisible',
'#options' => $years,
'#default_value' => 'none',
'#ajax' => [
'event' => 'change',
'callback' => '::submitFormAjax',
'wrapper' => 'statistics-trainings-progress',
],
];
$year_current = $form_state
->getValue('year');
if ($year_current == NULL || $year_current == 'none') {
if ($max_year == 'none') {
$max_year = date('Y');
}
$year = $max_year;
}
else {
$year = $year_current;
}
$query = $this->database
->select('opigno_learning_path_achievements', 'a');
$query
->addExpression('MONTH(a.registered)', 'month');
$query
->addExpression('YEAR(a.registered)', 'year');
$data = $query
->groupBy('month')
->groupBy('year')
->orderBy('month')
->execute()
->fetchAll();
$months = [
'none' => $this
->t('- None -'),
];
foreach ($data as $row) {
$month = $row->month;
if (!isset($months[$month]) && $row->year == $year) {
$timestamp = mktime(0, 0, 0, $month, 1);
$months[$month] = $this->date_formatter
->format($timestamp, 'custom', 'F');
}
}
$max_month = !empty($months) ? max(array_keys($months)) : NULL;
$month_select = [
'#type' => 'select',
'#title' => $this
->t('Month'),
'#title_display' => 'invisible',
'#options' => $months,
'#default_value' => 'none',
'#ajax' => [
'event' => 'change',
'callback' => '::submitFormAjax',
'wrapper' => 'statistics-trainings-progress',
],
];
$month = $form_state
->getValue('month', $max_month);
if ($month == 'none' || $year_current == NULL || $year_current == 'none') {
if ($max_month == 'none') {
$max_month = date('n');
}
$month = $max_month;
}
$timestamp = mktime(0, 0, 0, $month, 1, $year);
$datetime = DrupalDateTime::createFromTimestamp($timestamp);
$datetime
->add(new \DateInterval('P1M'));
// Check if user has limited permissions for global statistic.
$account = \Drupal::currentUser();
$lp_ids = NULL;
if (!($account
->hasPermission('view global statistics') || $account
->hasPermission('view any user statistics') || $account
->id() == 1)) {
$lp_ids = $this
->checkLimitPermissions($account);
}
$form['trainings_progress'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'statistics-trainings-progress',
],
// H2 Need for correct structure.
[
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => $this
->t('Dashboard of statistics'),
'#attributes' => [
'class' => [
'sr-only',
],
],
],
'year' => $year_select,
];
if ($year_current != NULL && $year_current != 'none') {
$form['trainings_progress']['month'] = $month_select;
}
$form['trainings_progress']['trainings_progress'] = $this
->buildTrainingsProgress($datetime, $lp_ids);
$form['content_statistics'] = [
'#type' => 'container',
'users' => $this
->buildUserMetrics($lp_ids),
];
if ($moduleHandler
->moduleExists('opigno_skills_system')) {
$form['content_statistics']['skills_list'] = $this
->buildSkillsTable();
}
$form['content_statistics']['trainings_list'] = $this
->buildTrainingsList($lp_ids);
$form['#attached']['library'][] = 'opigno_statistics/dashboard';
return $form;
}