You are here

public function DashboardForm::buildForm in Opigno statistics 3.x

Same name and namespace in other branches
  1. 8 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 450

Class

DashboardForm
Implements the statistics dashboard.

Namespace

Drupal\opigno_statistics\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Prepare the list of the available years.
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a');
  $query
    ->addExpression('YEAR(a.registered)', 'year');
  $data = $query
    ->groupBy('year')
    ->orderBy('year', 'DESC')
    ->execute()
    ->fetchAll();
  $year_select = $this
    ->createYearSelect($data, $form_state);
  $year_selected = (int) $year_select['#default_value'];

  // Prepare the list of months.
  $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();
  $month_select = $this
    ->createMonthSelect($data, $year_selected, $form_state);
  $month = (int) $month_select['#default_value'];
  $timestamp = mktime(0, 0, 0, $month, 1, $year_selected);
  $datetime = DrupalDateTime::createFromTimestamp($timestamp);
  $datetime
    ->add(new \DateInterval('P1M'));

  // Check if user has limited permissions for global statistic.
  $account = $this
    ->currentUser();
  $lp_ids = [];
  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('Statistics dashboard'),
      '#attributes' => [
        'class' => [
          'sr-only',
        ],
      ],
    ],
    '#attached' => $this
      ->buildTrainingsProgress($datetime, $lp_ids),
    'year' => $year_select,
  ];
  if ($year_selected) {
    $form['trainings_progress']['month'] = $month_select;
  }
  $form['trainings_progress']['content_statistics'] = [
    '#type' => 'container',
    'users' => $this
      ->buildUserMetrics($lp_ids),
  ];
  if ($this->moduleHandler
    ->moduleExists('opigno_skills_system')) {
    $form['skills_list'] = $this
      ->buildSkillsTable();
  }
  $form['trainings_list'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'content-box trainings-list',
      ],
    ],
    'title' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#attributes' => [
        'class' => [
          'trainings-content-title',
        ],
      ],
      '#value' => $this
        ->t('Trainings'),
    ],
    'table' => $this
      ->buildTrainingsList($lp_ids),
  ];
  $form['#attached'] = [
    'library' => [
      'opigno_statistics/opigno_charts',
    ],
  ];
  return $form;
}