You are here

public function OpignoGroupManagerController::getItems 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::getItems()

This method is called on learning path load.

It returns all the steps and their links in JSON format.

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

File

src/Controller/OpignoGroupManagerController.php, line 385

Class

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

Namespace

Drupal\opigno_group_manager\Controller

Code

public function getItems(Group $group) {
  $courses_storage = \Drupal::entityTypeManager()
    ->getStorage('group');

  // Init the response and get all the contents from this learning path.
  $entities = [];
  $managed_contents = OpignoGroupManagedContent::loadByProperties([
    'group_id' => $group
      ->id(),
  ]);

  // TODO: Maybe extend the class LPManagedContent
  // with LearningPathContent
  // (and use Parent::__constructor() to fill the params).
  // Convert all the LPManagedContent to LearningPathContent
  // and convert it to an array.
  foreach ($managed_contents as $managed_content) {

    // Need the content type object to get the LearningPathContent object.
    $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;
    }

    // Create the array that is ready for JSON.
    $manager_array = $lp_content
      ->toManagerArray($managed_content);
    if ($lp_content
      ->getGroupContentTypeId() == 'ContentTypeCourse') {
      $course = $courses_storage
        ->load($lp_content
        ->getEntityId());
      $group_content = $course
        ->getContent('opigno_module_group');
      $manager_array['modules_count'] = count($group_content);
    }

    // Add translatable values into Angular.
    switch ($lp_content
      ->getGroupContentTypeId()) {
      case 'ContentTypeCourse':
        $manager_array['translate']['content_type_title'] = $this
          ->t('Course')
          ->render();
        break;
      case 'ContentTypeModule':
        $manager_array['translate']['content_type_title'] = $this
          ->t('Opigno module')
          ->render();
        break;
      case 'ContentTypeMeeting':
        $manager_array['translate']['content_type_title'] = $this
          ->t('Live meeting')
          ->render();
        break;
      case 'ContentTypeILT':
        $manager_array['translate']['content_type_title'] = $this
          ->t('Instructor-Led Training')
          ->render();
        break;
    }
    $manager_array['translate']['text_close'] = $this
      ->t('close')
      ->render();
    $manager_array['translate']['text_add_a_new_item'] = $this
      ->t('Click here to add a new item')
      ->render();
    $manager_array['translate']['text_do_not_show_this_message_again'] = $this
      ->t('Do not show this message again')
      ->render();
    $manager_array['translate']['text_modules'] = $this
      ->t('modules')
      ->render();
    $manager_array['translate']['text_module'] = $this
      ->t('module')
      ->render();
    $manager_array['translate']['text_add'] = $this
      ->t('add')
      ->render();
    $manager_array['translate']['text_score'] = $this
      ->t('score')
      ->render();
    $manager_array['translate']['text_update'] = $this
      ->t('update')
      ->render();
    $manager_array['translate']['text_delete'] = $this
      ->t('delete')
      ->render();
    $manager_array['translate']['text_mandatory'] = $this
      ->t('Mandatory')
      ->render();
    $manager_array['translate']['text_minimum_score_to_validate_step'] = $this
      ->t('Minimum score to validate step')
      ->render();
    $entities[] = $manager_array;
  }

  // Return all the contents in JSON format.
  return new JsonResponse($entities, Response::HTTP_OK);
}