You are here

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

Update a link minimum score to go to next step.

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

File

src/Controller/OpignoGroupManagerController.php, line 676

Class

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

Namespace

Drupal\opigno_group_manager\Controller

Code

public function updateLink(Group $group, Request $request) {

  // First, check the params.
  $datas = json_decode($request
    ->getContent());
  if (empty($datas->parentCid) || empty($datas->childCid)) {
    return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
  }

  // Then get the params.
  $parentCid = $datas->parentCid;
  $childCid = $datas->childCid;
  $requiredScore = !empty($datas->requiredScore) ? $datas->requiredScore : 0;
  $requiredActivities = !empty($datas->requiredActivities) ? serialize($datas->requiredActivities) : NULL;

  // Get the links that use the same LP ID,
  // parent CID and child CID. Should be only one.
  try {
    $links = OpignoGroupManagedLink::loadByProperties([
      'group_id' => $group
        ->id(),
      'parent_content_id' => $parentCid,
      'child_content_id' => $childCid,
    ]);
  } catch (InvalidPluginDefinitionException $e) {
    return new JsonResponse(NULL, Response::HTTP_INTERNAL_SERVER_ERROR);
  }

  // If no link returned, create it and return OK.
  if (empty($links)) {
    $new_link = OpignoGroupManagedLink::createWithValues($group
      ->id(), $parentCid, $childCid, $requiredScore, $requiredActivities);
    $new_link
      ->save();
    return new JsonResponse(NULL, Response::HTTP_OK);
  }

  // If the link is found, update it and return OK.
  foreach ($links as $link) {
    $link
      ->setRequiredScore($requiredScore);
    $link
      ->setRequiredActivities($requiredActivities);
    $link
      ->save();
  }
  return new JsonResponse(NULL, Response::HTTP_OK);
}