You are here

public function TrainingForm::buildForm in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x src/Form/TrainingForm.php \Drupal\opigno_statistics\Form\TrainingForm::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/TrainingForm.php, line 706

Class

TrainingForm
Implements the training statistics page.

Namespace

Drupal\opigno_statistics\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $group = NULL) {
  $form['#title'] = $this
    ->t('Training statistics - @training', [
    '@training' => $group
      ->label(),
  ]);
  if ($group
    ->bundle() == 'opigno_class') {
    $query_class = $this->database
      ->select('group_content_field_data', 'g_c_f_d')
      ->fields('g_c_f_d', [
      'gid',
    ])
      ->condition('entity_id', $group
      ->id())
      ->condition('type', 'group_content_type_27efa0097d858')
      ->execute()
      ->fetchAll();
    $lp_ids = [];
    foreach ($query_class as $result_ids) {
      $lp_ids[] = $result_ids->gid;
    }
  }
  else {
    $lp_ids[] = $group
      ->id();
  }
  if (empty($lp_ids)) {
    $lp_ids[] = 0;
  }
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a');
  $query
    ->addExpression('YEAR(a.registered)', 'year');
  $query
    ->condition('a.gid', $lp_ids, 'IN');
  $data = $query
    ->groupBy('year')
    ->orderBy('year', 'DESC')
    ->execute()
    ->fetchAll();
  $years = [
    'none' => '- None -',
  ];
  foreach ($data as $row) {
    $year = $row->year;
    if (!isset($years[$year])) {
      $years[$year] = $year;
    }
  }
  $max_year = count($years) > 1 ? 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') {
    $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');
  $query
    ->condition('a.gid', $lp_ids, 'IN');
  $data = $query
    ->groupBy('month')
    ->groupBy('year')
    ->orderBy('month')
    ->execute()
    ->fetchAll();
  $months = [
    'none' => '- 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 = count($months) > 1 ? max(array_keys($months)) : NULL;
  $month_select = [
    '#type' => 'select',
    '#options' => $months,
    '#title' => $this
      ->t('Month'),
    '#title_display' => 'invisible',
    '#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') {
    $month = $max_month;
  }
  $datetime = FALSE;
  if ($month !== 'none' && $year !== 'none') {
    $timestamp = mktime(0, 0, 0, $month, 1, $year);
    $datetime = DrupalDateTime::createFromTimestamp($timestamp);
    $datetime
      ->add(new \DateInterval('P1M'));
  }

  // Get users with the training expired certification.
  $expired_uids = $this
    ->getExpiredUsers($group);
  $form['trainings_progress'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'statistics-trainings-progress',
    ],
    // H2 Need for correct structure.
    [
      '#type' => 'html_tag',
      '#tag' => 'h2',
      '#value' => $this
        ->t('Training statistics'),
      '#attributes' => [
        'class' => [
          'sr-only',
        ],
      ],
    ],
    'year' => $year_select,
    'month' => $month_select,
  ];
  if ($year_current != NULL && $year_current != 'none') {
    $form['trainings_progress']['month'] = $month_select;
  }
  $form['trainings_progress']['trainings_progress'] = $this
    ->buildTrainingsProgress($group, $datetime, $expired_uids);
  if ($group
    ->bundle() == 'opigno_class') {
    $form[] = [
      '#type' => 'container',
      'users' => $this
        ->buildUserMetrics($group),
    ];
    foreach ($lp_ids as $lp_id) {
      $form[] = [
        'training_class_results_' . $lp_id => $this
          ->buildUsersResultsClass($group, $lp_id),
      ];
    }
  }
  else {
    $form[] = [
      '#type' => 'container',
      'users' => $this
        ->buildUserMetrics($group),
      'training_content' => $this
        ->buildTrainingContent($group, $expired_uids),
      'user_results' => $this
        ->buildUsersResultsLp($group, $expired_uids),
    ];
  }
  $form['#attached']['library'][] = 'opigno_statistics/training';
  $form['#attached']['library'][] = 'opigno_statistics/user';
  return $form;
}