You are here

public function ModuleActivitiesForm::activitiesListSubmit in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/ModuleActivitiesForm.php \Drupal\opigno_module\Form\ModuleActivitiesForm::activitiesListSubmit()

File

src/Form/ModuleActivitiesForm.php, line 184

Class

ModuleActivitiesForm
Class ModuleActivitiesForm.

Namespace

Drupal\opigno_module\Form

Code

public function activitiesListSubmit(array &$form, FormStateInterface $form_state) {

  /* @var $db_connection \Drupal\Core\Database\Connection */
  $db_connection = \Drupal::service('database');

  /* @var $opigno_module \Drupal\opigno_module\Entity\OpignoModule */
  $opigno_module = $this->opigno_module;
  $randomization = $opigno_module
    ->getRandomization();

  // Display result.
  $values = $form_state
    ->getValues();
  $deleted_activities = [];
  $exist_activities = [];
  if (!empty($values['activities_list'])) {
    foreach ($values['activities_list'] as $key => $activity) {
      list($activity_id, $activity_vid) = explode('-', $key, 2);
      $activity_id = (int) $activity_id;
      $activity_vid = (int) $activity_vid;
      $max_score = $activity['max_score'];
      $item = [
        'parent_id' => $opigno_module
          ->id(),
        'parent_vid' => $opigno_module
          ->getRevisionId(),
        'child_id' => $activity_id,
        'child_vid' => $activity_vid,
        'max_score' => $max_score,
        'weight' => $activity['weight'],
      ];
      if ($activity['delete'] == 1) {
        $deleted_activities[$key] = $item;
      }
      else {
        $exist_activities[$key] = $item;
      }
    }

    // Delete activities that were selected to delete.

    /* @todo Replace it with methods from OpignoModule class */
    if (!empty($deleted_activities)) {

      /* @todo Output some information regarding deleted items */
      foreach ($deleted_activities as $deleted_activity) {
        $delete_query = $db_connection
          ->delete('opigno_module_relationship');
        $delete_query
          ->condition('parent_id', $deleted_activity['parent_id']);
        $delete_query
          ->condition('parent_vid', $deleted_activity['parent_vid']);
        $delete_query
          ->condition('child_id', $deleted_activity['child_id']);
        $delete_query
          ->condition('child_vid', $deleted_activity['child_vid']);
        $delete_query
          ->execute();
      }
    }

    // Update activities fields.
    if (!empty($exist_activities)) {
      foreach ($exist_activities as $exist_activity) {
        $db_connection
          ->merge('opigno_module_relationship')
          ->keys([
          'parent_id' => $exist_activity['parent_id'],
          'parent_vid' => $exist_activity['parent_vid'],
          'child_id' => $exist_activity['child_id'],
          'child_vid' => $exist_activity['child_vid'],
        ])
          ->fields([
          'max_score' => $exist_activity['max_score'],
          'weight' => $exist_activity['weight'],
        ])
          ->execute();
      }

      // Update Module randomization options.
      if ($randomization == 2) {

        // Update random activities count and score per random activity.
        if (isset($values['max_score']) && isset($values['random_count'])) {
          $opigno_module
            ->setRandomActivityScore($values['max_score']);
          $random_number = $values['random_count'];
          $opigno_module
            ->setRandomActivitiesCount($random_number);
          $opigno_module
            ->save();
        }
      }
    }
  }
}