You are here

protected function LearningPathContentController::sortModulesArray in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Controller/LearningPathContentController.php \Drupal\opigno_learning_path\Controller\LearningPathContentController::sortModulesArray()

Sort.

Parameters

array $modules: Initial modules array.

\Drupal\Core\Entity\EntityInterface $group: Training group.

Return value

array Sorted modules array.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to LearningPathContentController::sortModulesArray()
LearningPathContentController::getModules in src/Controller/LearningPathContentController.php
This method is called on learning path load.

File

src/Controller/LearningPathContentController.php, line 255

Class

LearningPathContentController
Controller for all the actions of the Learning Path content.

Namespace

Drupal\opigno_learning_path\Controller

Code

protected function sortModulesArray(array $modules, EntityInterface $group) : array {
  try {
    $managed_contents = OpignoGroupManagedContent::loadByProperties([
      'group_id' => $group
        ->id(),
    ]);
  } catch (InvalidPluginDefinitionException $e) {
    return $modules;
  }

  // Creating full array of training steps and courses sub-steps.
  $modules_positioned = [];
  foreach ($managed_contents as $content) {
    $id = $content
      ->getEntityId();
    $x = $content
      ->getCoordinateX();
    $y = $content
      ->getCoordinateY();
    if ($content
      ->getGroupContentType()
      ->getId() == 'ContentTypeCourse') {

      // Course steps.
      if ($course_managed_contents = OpignoGroupManagedContent::loadByProperties([
        'group_id' => $content
          ->getEntityId(),
      ])) {
        foreach ($course_managed_contents as $course_content) {
          $course_id = $course_content
            ->getEntityId();
          $course_x = $course_content
            ->getCoordinateX();
          $course_y = $course_content
            ->getCoordinateY();
          foreach ($modules as $mod) {
            if ($mod['entity_id'] == $course_id) {
              $modules_positioned[$y][$x][$course_y][$course_x] = $mod;
            }
          }
        }
      }
    }
    else {

      // Ordinary steps.
      foreach ($modules as $module) {
        if ($module['entity_id'] == $id) {
          $modules_positioned[$y][$x] = $module;
        }
      }
    }
  }

  // Training steps sorting including courses sub-steps.
  ksort($modules_positioned);
  $sorted_modules = [];
  foreach ($modules_positioned as $items) {
    $is_course = FALSE;
    $item = NULL;
    foreach ($items as $item) {
      if (!array_key_exists('entity_id', $item)) {
        $is_course = TRUE;
      }
    }
    if ($is_course) {

      // Course steps.
      if (!empty($item)) {
        ksort($item);
        foreach ($item as $i) {
          $sorted_modules = array_merge($sorted_modules, $i);
        }
      }
    }
    else {

      // Ordinary steps.
      $sorted_modules = array_merge($sorted_modules, $items);
    }
  }
  return $sorted_modules;
}