public function LearningPathMembershipController::toggleRole in Opigno Learning path 8
Same name and namespace in other branches
- 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'
File
- src/
Controller/ LearningPathMembershipController.php, line 472
Class
- LearningPathMembershipController
- Controller for the actions related to LP membership.
Namespace
Drupal\opigno_learning_path\ControllerCode
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();
}