events.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * All event handler functions for this plugin can be found in this file.
  4. */
  5. /**
  6. * When an expert leaves the group, remove the expert role
  7. *
  8. * @param string $event the 'leave' event
  9. * @param string $type for the 'group' type
  10. * @param array $params the provided params
  11. *
  12. * @return void
  13. */
  14. function questions_leave_group_handler($event, $type, $params) {
  15. if (empty($params) || !is_array($params)) {
  16. return;
  17. }
  18. $user = elgg_extract('user', $params);
  19. $group = elgg_extract('group', $params);
  20. if (empty($user) || !($user instanceof ElggUser) || empty($group) || !($group instanceof ElggGroup)) {
  21. return;
  22. }
  23. // is the user an expert in this group
  24. if (check_entity_relationship($user->getGUID(), QUESTIONS_EXPERT_ROLE, $group->getGUID())) {
  25. // remove the expert role
  26. remove_entity_relationship($user->getGUID(), QUESTIONS_EXPERT_ROLE, $group->getGUID());
  27. }
  28. }
  29. /**
  30. * When an expert leaves the site, remove the expert role
  31. *
  32. * @param string $event the 'delete' event
  33. * @param string $type for the 'member_of_site' type
  34. * @param ElggRelationship $relationship the provided params
  35. *
  36. * @return void
  37. */
  38. function questions_leave_site_handler($event, $type, $relationship) {
  39. if (empty($relationship) || !($relationship instanceof ElggRelationship)) {
  40. return;
  41. }
  42. $user = get_user($relationship->guid_one);
  43. $site = elgg_get_site_entity($relationship->guid_two);
  44. if (empty($user) || empty($site)) {
  45. return;
  46. }
  47. // is the user an expert in this site
  48. if (check_entity_relationship($user->getGUID(), QUESTIONS_EXPERT_ROLE, $site->getGUID())) {
  49. // remove the expert role
  50. remove_entity_relationship($user->getGUID(), QUESTIONS_EXPERT_ROLE, $site->getGUID());
  51. }
  52. }