You are here

public function OpignoModuleManagerController::getActivitiesList in Opigno module 3.x

Same name and namespace in other branches
  1. 8 src/Controller/OpignoModuleManagerController.php \Drupal\opigno_module\Controller\OpignoModuleManagerController::getActivitiesList()

Get the list of the existing activities.

Return value

\Symfony\Component\HttpFoundation\JsonResponse Response.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'OpignoModuleManagerController::getActivitiesList'
opigno_module.routing.yml in ./opigno_module.routing.yml
opigno_module.routing.yml

File

src/Controller/OpignoModuleManagerController.php, line 487

Class

OpignoModuleManagerController
Controller for all the actions of the Opigno module manager.

Namespace

Drupal\opigno_module\Controller

Code

public function getActivitiesList(OpignoModule $opigno_module) {
  $activities_storage = \Drupal::entityTypeManager()
    ->getStorage('opigno_activity');
  $query = $activities_storage
    ->getQuery();
  if ($opigno_module
    ->getSkillsActive()) {
    $query
      ->condition('auto_skills', 1);
    if ($opigno_module
      ->getTargetSkill() && $opigno_module
      ->getTargetSkill() > 0) {
      $target_skill = $opigno_module
        ->getTargetSkill();
      $term_storage = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term');
      $skills_tree = $term_storage
        ->loadTree('skills', $target_skill);
      $skills_ids = [];
      foreach ($skills_tree as $skill) {
        $skills_ids[] = $skill->tid;
      }
      if (!empty($skills_ids)) {
        $query
          ->condition('skills_list', $skills_ids, 'IN');
      }
    }
    $activities_ids = $query
      ->execute();
  }
  else {
    $group = $query
      ->orConditionGroup()
      ->condition('auto_skills', 0)
      ->notExists('auto_skills');
    $activities_ids = $query
      ->condition($group)
      ->execute();
  }

  // Use cache because it's too hard upload all every time.
  $hash = md5(serialize($activities_ids));
  $list = \Drupal::cache()
    ->get($hash);
  if (empty($list) && !empty($activities_ids)) {
    $activities = $activities_storage
      ->loadMultiple($activities_ids);
    $list = array_map(function ($activity) {

      /** @var \Drupal\opigno_module\Entity\OpignoActivity $activity */
      $data = [];
      $data['name'] = $activity
        ->label();
      $data['activity_id'] = $activity
        ->id();
      $data['type'] = $activity
        ->bundle();

      // If H5P content, add library info.
      if ($data['type'] === 'opigno_h5p') {
        $value = $activity
          ->get('opigno_h5p')
          ->getValue();
        if ($value && $activity
          ->get('opigno_h5p')
          ->getValue()[0]['h5p_content_id'] !== NULL) {
          $cid = $activity
            ->get('opigno_h5p')
            ->getValue()[0]['h5p_content_id'];
          if ($content = H5PContent::load($cid)) {
            $library = $content
              ->getLibrary();
            $data['library'] = $library->name;
          }
        }
      }
      return $data;
    }, $activities);
    \Drupal::cache()
      ->set($hash, $list);
  }
  elseif (!empty($list)) {
    $list = $list->data;
  }
  return new JsonResponse($list, Response::HTTP_OK);
}