You are here

public function LearningPathMembershipController::toggleRole in Opigno Learning path 8

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

Ajax callback used in opigno_learning_path_member_overview.js.

Toggles user role in learning path.

1 string reference to 'LearningPathMembershipController::toggleRole'
opigno_learning_path.routing.yml in ./opigno_learning_path.routing.yml
opigno_learning_path.routing.yml

File

src/Controller/LearningPathMembershipController.php, line 472

Class

LearningPathMembershipController
Controller for the actions related to LP membership.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function toggleRole() {

  /** @var \Drupal\group\Entity\Group $group */
  $group = \Drupal::routeMatch()
    ->getParameter('group');
  $query = \Drupal::request()->query;
  $uid = $query
    ->get('uid');
  $user = User::load($uid);
  $role = $query
    ->get('role');
  if (!isset($group) || !isset($user) || !isset($role)) {
    throw new NotFoundHttpException();
  }
  $member = $group
    ->getMember($user);
  if (!isset($member)) {
    throw new NotFoundHttpException();
  }
  $group_content = $member
    ->getGroupContent();
  $values = $group_content
    ->get('group_roles')
    ->getValue();
  $found = FALSE;
  foreach ($values as $index => $value) {
    if ($value['target_id'] === $role) {
      $found = TRUE;
      unset($values[$index]);
      break;
    }
  }
  if ($found === FALSE) {
    $values[] = [
      'target_id' => $role,
    ];
  }
  $group_content
    ->set('group_roles', $values);
  $group_content
    ->save();
  return new JsonResponse();
}