You are here

public static function LearningPathAccess::setLearningPathCourseMember in Opigno Learning path 8

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

Sets Learning Path content course member.

2 calls to LearningPathAccess::setLearningPathCourseMember()
LearningPathAccess::membershipPreSave in src/LearningPathAccess.php
Sets roles on membership presave.
opigno_learning_path_entity_delete in ./opigno_learning_path.module
Implements hook_entity_delete().

File

src/LearningPathAccess.php, line 207

Class

LearningPathAccess
Class LearningPathAccess.

Namespace

Drupal\opigno_learning_path

Code

public static function setLearningPathCourseMember(EntityInterface $membership, $mode) {

  // Get LP content courses.
  $group = $membership
    ->getGroup();
  $courses = LPManagedContent::loadByProperties([
    'learning_path_id' => $group
      ->id(),
    'lp_content_type_id' => 'ContentTypeCourse',
  ]);

  // Update courses members.
  if ($courses) {
    foreach ($courses as $course) {
      $group = Group::load($course->entity_id->value);
      $account = $membership
        ->getEntity();
      if ($group
        ->getMember($account) && $mode == 'delete') {
        LearningPathAccess::deleteUserStatus($group
          ->getMember($account)
          ->getGroupContent());
        $group
          ->removeMember($account);
      }
      if ($mode == 'update') {
        if (!$group
          ->getMember($account)) {
          $group
            ->addMember($account);
        }
        LearningPathAccess::mergeUserStatus($group
          ->getMember($account)
          ->getGroupContent());
      }
    }
  }
}