You are here

public function OpignoGroupManagerController::getPositions in Opigno group manager 8

Same name and namespace in other branches
  1. 3.x src/Controller/OpignoGroupManagerController.php \Drupal\opigno_group_manager\Controller\OpignoGroupManagerController::getPositions()

This function is called on learning path load.

It return the coordinates of every steps.

Parameters

\Drupal\group\Entity\Group $group: Group object.

Return value

\Symfony\Component\HttpFoundation\JsonResponse Response.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'OpignoGroupManagerController::getPositions'
opigno_group_manager.routing.yml in ./opigno_group_manager.routing.yml
opigno_group_manager.routing.yml

File

src/Controller/OpignoGroupManagerController.php, line 463

Class

OpignoGroupManagerController
Controller for all the actions of the Opigno group manager app.

Namespace

Drupal\opigno_group_manager\Controller

Code

public function getPositions(Group $group) {

  // Get the positions from DB.
  $entityPositions = [];
  try {
    $managed_contents = OpignoGroupManagedContent::loadByProperties([
      'group_id' => $group
        ->id(),
    ]);
  } catch (InvalidPluginDefinitionException $e) {
    return new JsonResponse(NULL, Response::HTTP_INTERNAL_SERVER_ERROR);
  }

  // Then, create a big array with the positions and return OK.
  foreach ($managed_contents as $managed_content) {

    // Check if it's not deleted.
    $content_type_id = $managed_content
      ->getGroupContentTypeId();
    $content_type = $this->content_types_manager
      ->createInstance($content_type_id);
    $lp_content = $content_type
      ->getContent($managed_content
      ->getEntityId());
    if ($lp_content === FALSE) {
      continue;
    }
    $entityPositions[] = [
      'cid' => $managed_content
        ->id(),
      'col' => $managed_content
        ->getCoordinateX() ? $managed_content
        ->getCoordinateX() : 1,
      'row' => $managed_content
        ->getCoordinateY() ? $managed_content
        ->getCoordinateY() : 1,
    ];
  }
  return new JsonResponse($entityPositions, Response::HTTP_OK);
}