You are here

public function ModuleActivitiesForm::buildForm in Opigno module 8

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

Class

ModuleActivitiesForm
Class ModuleActivitiesForm.

Namespace

Drupal\opigno_module\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, OpignoModuleInterface $opigno_module = NULL) {
  $this->opigno_module = $opigno_module;
  $activity_types = \Drupal::entityTypeManager()
    ->getStorage('opigno_activity_type')
    ->loadMultiple();

  // Current module activities list.
  $form['activities_list'] = [
    '#type' => 'table',
    '#id' => 'activities-list-table',
    '#sticky' => TRUE,
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'activities-list-order-weight',
      ],
    ],
    '#weight' => 5,
  ];
  $activities = $opigno_module
    ->getModuleActivities();
  if (empty($activities)) {
    $form['activities_list'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('There are currently no activities in this Module. Assign existing activities by using the activities bank below. You can also use the links above to create new activities.'),
    ];
  }
  else {

    // Build random activities additional fields.
    if ($opigno_module
      ->getRandomization() == 2) {
      $form['random_activities'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Settings for random activities'),
        '#weight' => 1,
        '#open' => TRUE,
      ];
      $form['random_activities']['random_count'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Number of random activities'),
        '#default_value' => $opigno_module
          ->getRandomActivitiesCount() ? $opigno_module
          ->getRandomActivitiesCount() : 0,
        '#description' => $this
          ->t('The number of activities to be randomly selected each time someone takes this module'),
      ];
      $form['random_activities']['max_score'] = [
        '#type' => 'textfield',
        '#default_value' => $opigno_module
          ->getRandomActivityScore() ? $opigno_module
          ->getRandomActivityScore() : 1,
        '#title' => $this
          ->t('Max score for each random activity'),
      ];
    }
    $this
      ->activitiesToForm($form, $activities, $opigno_module, $activity_types);
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
      '#submit' => [
        [
          $this,
          'activitiesListSubmit',
        ],
      ],
      '#weight' => 100,
    ];
  }
  return $form;
}