You are here

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

Update entities ancestors.

Eg. if user switch two entities in learning path.

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

File

src/Controller/OpignoGroupManagerController.php, line 855

Class

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

Namespace

Drupal\opigno_group_manager\Controller

Code

public function updateEntities(Group $group, Request $request) {
  $datas = Json::decode($request
    ->getContent());

  // TODO Update parents links if changed.
  foreach ($datas['entities'] as $entity) {
    if (!$entity['cid']) {
      return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
    }
    $current_cid = $entity['cid'];
    $content = OpignoGroupManagedContent::load($current_cid);

    // Parent content id.
    if (!empty($entity['parents'])) {
      $parent_cid = $entity['parents'][0]['cid'];
    }
    else {
      $parent_cid = NULL;
    }

    // Can be null if there is not parent.
    $parent_link = $content
      ->getParentsLinks();
    if ($parent_link) {
      $parent_link = reset($parent_link);

      // Update parent link if was changed.
      if ($parent_cid == NULL) {

        // Delete parent link.
        $parent_link
          ->delete();
      }
      elseif ($parent_link
        ->getParentContentId() != $parent_cid) {
        $parent_link
          ->setParentContentId($parent_cid);
        $parent_link
          ->save();
      }
    }
    else {
      if ($parent_cid) {

        // Create new parent link instance.
        $new_link = OpignoGroupManagedLink::createWithValues($group
          ->id(), $parent_cid, $current_cid, $entity['successScoreMin']);
        $new_link
          ->save();
      }
    }
  }
  return new JsonResponse([], Response::HTTP_OK);
}