You are here

public function LPManagedContent::getNextStep in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Entity/LPManagedContent.php \Drupal\opigno_learning_path\Entity\LPManagedContent::getNextStep()

Get the next LPManagedContent object according to the user score.

File

src/Entity/LPManagedContent.php, line 367

Class

LPManagedContent
Defines the Learning Path Content entity.

Namespace

Drupal\opigno_learning_path\Entity

Code

public function getNextStep($user_score) {

  // Get the child link that has the required_score
  // higher than the $score param and
  // that has the higher required_score.
  $query = \Drupal::entityTypeManager()
    ->getStorage('learning_path_link')
    ->getQuery();
  $query
    ->condition('parent_content_id', $this
    ->id());
  $query
    ->condition('required_score', $user_score, '<=');
  $query
    ->sort('required_score', 'DESC');
  $query
    ->range(0, 1);
  $result = $query
    ->execute();

  // If no result, return FALSE.
  if (empty($result)) {
    return FALSE;
  }

  // If a result is found, return the next content object.
  $next_step_id = array_pop($result);
  $next_step_link = LPManagedLink::load($next_step_id);
  return $next_step_link
    ->getChildContent();
}