You are here

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

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

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;
  }

  // Prepare the list of available years.
  $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();
  $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');
  $query
    ->condition('a.gid', $lp_ids, 'IN');
  $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'));

  // Get users with the training expired certification.
  $expired_uids = $this
    ->getExpiredUsers($group);
  $form['trainings_progress'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'statistics-training-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,
    '#attached' => $this
      ->buildTrainingsProgress($group, $datetime, $expired_uids),
  ];
  if ($group
    ->bundle() === 'opigno_class') {
    $form['trainings_progress']['user_metrics'] = [
      '#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['trainings_progress']['user_metrics'] = [
      '#type' => 'container',
      'users' => $this
        ->buildUserMetrics($group),
    ];
    $form['training_content'] = $this
      ->buildTrainingContent($group, $expired_uids);
    $form['user_results'] = $this
      ->buildUsersResultsLp($group, $expired_uids);
  }
  $form['#attached'] = [
    'library' => [
      'opigno_statistics/opigno_charts',
    ],
  ];
  return $form;
}