toggle_expert.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. /**
  3. * This action toggles the expert role for a user on or off
  4. */
  5. $user_guid = (int) get_input('user_guid');
  6. $page_owner_guid = (int) get_input('guid');
  7. if (empty($user_guid) || empty($page_owner_guid)) {
  8. register_error(elgg_echo('error:missing_data'));
  9. forward(REFERER);
  10. }
  11. $user = get_user($user_guid);
  12. $page_owner = get_entity($page_owner_guid);
  13. if (empty($user) || empty($page_owner) || (!($page_owner instanceof ElggSite) && !($page_owner instanceof ElggGroup)) || !$page_owner->canEdit()) {
  14. register_error(elgg_echo('pageownerunavailable', [$page_owner_guid]));
  15. forward(REFERER);
  16. }
  17. // check if the user is an expert
  18. if (check_entity_relationship($user->getGUID(), QUESTIONS_EXPERT_ROLE, $page_owner->getGUID())) {
  19. // yes, so remove
  20. remove_entity_relationship($user->getGUID(), QUESTIONS_EXPERT_ROLE, $page_owner->getGUID());
  21. system_message(elgg_echo('questions:action:toggle_expert:success:remove', [$user->name, $page_owner->name]));
  22. } else {
  23. // no, so add
  24. add_entity_relationship($user->getGUID(), QUESTIONS_EXPERT_ROLE, $page_owner->getGUID());
  25. system_message(elgg_echo('questions:action:toggle_expert:success:make', [$user->name, $page_owner->name]));
  26. }
  27. forward(REFERER);