You are here

public function LearningPathManagerController::updateLink in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Controller/LearningPathManagerController.php \Drupal\opigno_learning_path\Controller\LearningPathManagerController::updateLink()

Update a link minimum score to go to next step.

1 string reference to 'LearningPathManagerController::updateLink'
opigno_learning_path.routing.yml in ./opigno_learning_path.routing.yml
opigno_learning_path.routing.yml

File

src/Controller/LearningPathManagerController.php, line 396

Class

LearningPathManagerController
Controller for all the actions of the Learning Path manager app.

Namespace

Drupal\opigno_learning_path\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) || isset($datas->requiredScore) === FALSE) {
    return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
  }

  // Then get the params.
  $parentCid = $datas->parentCid;
  $childCid = $datas->childCid;
  $requiredScore = $datas->requiredScore;

  // Get the links that use the same LP ID,
  // parent CID and child CID. Should be only one.
  try {
    $links = LPManagedLink::loadByProperties([
      'learning_path_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 = LPManagedLink::createWithValues($group
      ->id(), $parentCid, $childCid, $requiredScore);
    $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
      ->save();
  }
  return new JsonResponse(NULL, Response::HTTP_OK);
}