Notifications.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. namespace ColdTrick\Questions;
  3. class Notifications {
  4. /**
  5. * Set the correct message content for when a question is created
  6. *
  7. * @param string $hook the name of the hook
  8. * @param string $type the type of the hook
  9. * @param \Elgg\Notifications\Notification $return_value current return value
  10. * @param array $params supplied params
  11. *
  12. * @return void|\Elgg\Notifications\Notification
  13. */
  14. public static function createQuestion($hook, $type, $return_value, $params) {
  15. if (!($return_value instanceof \Elgg\Notifications\Notification)) {
  16. return;
  17. }
  18. $event = elgg_extract('event', $params);
  19. $recipient = elgg_extract('recipient', $params);
  20. $language = elgg_extract('language', $params);
  21. if (!($event instanceof \Elgg\Notifications\Event) || !($recipient instanceof \ElggUser)) {
  22. return;
  23. }
  24. $actor = $event->getActor();
  25. $question = $event->getObject();
  26. $return_value->subject = elgg_echo('questions:notifications:create:subject', [], $language);
  27. $return_value->summary = elgg_echo('questions:notifications:create:summary', [], $language);
  28. $return_value->body = elgg_echo('questions:notifications:create:message', [
  29. $recipient->name,
  30. $question->title,
  31. $question->getURL(),
  32. ], $language);
  33. return $return_value;
  34. }
  35. /**
  36. * Set the correct message content for when a question is moved
  37. *
  38. * @param string $hook the name of the hook
  39. * @param string $type the type of the hook
  40. * @param \Elgg\Notifications\Notification $return_value current return value
  41. * @param array $params supplied params
  42. *
  43. * @return void|\Elgg\Notifications\Notification
  44. */
  45. public static function moveQuestion($hook, $type, $return_value, $params) {
  46. if (!($return_value instanceof \Elgg\Notifications\Notification)) {
  47. return;
  48. }
  49. $event = elgg_extract('event', $params);
  50. $recipient = elgg_extract('recipient', $params);
  51. $language = elgg_extract('language', $params);
  52. if (!($event instanceof \Elgg\Notifications\Event) || !($recipient instanceof \ElggUser)) {
  53. return;
  54. }
  55. $actor = $event->getActor();
  56. $question = $event->getObject();
  57. $return_value->subject = elgg_echo('questions:notifications:move:subject', [], $language);
  58. $return_value->summary = elgg_echo('questions:notifications:move:summary', [], $language);
  59. $return_value->body = elgg_echo('questions:notifications:move:message', [
  60. $recipient->name,
  61. $question->title,
  62. $question->getURL(),
  63. ], $language);
  64. return $return_value;
  65. }
  66. /**
  67. * Set the correct message content for when a answer is created
  68. *
  69. * @param string $hook the name of the hook
  70. * @param string $type the type of the hook
  71. * @param \Elgg\Notifications\Notification $return_value current return value
  72. * @param array $params supplied params
  73. *
  74. * @return void|\Elgg\Notifications\Notification
  75. */
  76. public static function createAnswer($hook, $type, $return_value, $params) {
  77. if (!($return_value instanceof \Elgg\Notifications\Notification)) {
  78. return;
  79. }
  80. $event = elgg_extract('event', $params);
  81. $recipient = elgg_extract('recipient', $params);
  82. $language = elgg_extract('language', $params);
  83. if (!($event instanceof \Elgg\Notifications\Event) || !($recipient instanceof \ElggUser)) {
  84. return;
  85. }
  86. $actor = $event->getActor();
  87. $answer = $event->getObject();
  88. $question = $answer->getContainerEntity();
  89. $return_value->subject = elgg_echo('questions:notifications:answer:create:subject', [$question->title], $language);
  90. $return_value->summary = elgg_echo('questions:notifications:answer:create:summary', [$question->title], $language);
  91. $return_value->body = elgg_echo('questions:notifications:answer:create:message', [
  92. $recipient->name,
  93. $actor->name,
  94. $question->title,
  95. $answer->description,
  96. $answer->getURL(),
  97. ], $language);
  98. return $return_value;
  99. }
  100. /**
  101. * Set the correct message content for when a answer is marked as correct
  102. *
  103. * @param string $hook the name of the hook
  104. * @param string $type the type of the hook
  105. * @param \Elgg\Notifications\Notification $return_value current return value
  106. * @param array $params supplied params
  107. *
  108. * @return void|\Elgg\Notifications\Notification
  109. */
  110. public static function correctAnswer($hook, $type, $return_value, $params) {
  111. if (!($return_value instanceof \Elgg\Notifications\Notification)) {
  112. return;
  113. }
  114. $event = elgg_extract('event', $params);
  115. $recipient = elgg_extract('recipient', $params);
  116. $language = elgg_extract('language', $params);
  117. if (!($event instanceof \Elgg\Notifications\Event) || !($recipient instanceof \ElggUser)) {
  118. return;
  119. }
  120. $actor = $event->getActor();
  121. $answer = $event->getObject();
  122. $question = $answer->getContainerEntity();
  123. $return_value->subject = elgg_echo('questions:notifications:answer:correct:subject', [$question->title], $language);
  124. $return_value->summary = elgg_echo('questions:notifications:answer:correct:summary', [$question->title], $language);
  125. $return_value->body = elgg_echo('questions:notifications:answer:correct:message', [
  126. $recipient->name,
  127. $actor->name,
  128. $question->title,
  129. $answer->description,
  130. $answer->getURL(),
  131. ], $language);
  132. return $return_value;
  133. }
  134. /**
  135. * Change the notification message for comments on answers
  136. *
  137. * @param string $hook the name of the hook
  138. * @param stirng $type the type of the hook
  139. * @param \Elgg\Notifications\Notification $return_value the current return value
  140. * @param array $params supplied values
  141. *
  142. * @return void|\Elgg\Notifications\Notification
  143. */
  144. public static function createCommentOnAnswer($hook, $type, $return_value, $params) {
  145. if (!($return_value instanceof \Elgg\Notifications\Notification)) {
  146. return;
  147. }
  148. $event = elgg_extract('event', $params);
  149. if (!($event instanceof \Elgg\Notifications\Event)) {
  150. return;
  151. }
  152. $comment = $event->getObject();
  153. $object = $comment->getContainerEntity();
  154. if (!($object instanceof \ElggAnswer)) {
  155. return;
  156. }
  157. $actor = $event->getActor();
  158. $question = $object->getContainerEntity();
  159. $language = elgg_extract('language', $params, get_current_language());
  160. $recipient = elgg_extract('recipient', $params);
  161. $return_value->subject = elgg_echo('questions:notifications:answer:comment:subject', [], $language);
  162. $return_value->summary = elgg_echo('questions:notifications:answer:comment:summary', [], $language);
  163. $return_value->body = elgg_echo('questions:notifications:answer:comment:message', [
  164. $recipient->name,
  165. $actor->name,
  166. $question->title,
  167. $comment->description,
  168. $object->getURL(),
  169. ], $language);
  170. return $return_value;
  171. }
  172. /**
  173. * Add experts to the subscribers for a question
  174. *
  175. * @param string $hook the name of the hook
  176. * @param string $type the type of the hook
  177. * @param array $return_value current return value
  178. * @param array $params supplied params
  179. *
  180. * @return void|array
  181. */
  182. public static function addExpertsToSubscribers($hook, $type, $return_value, $params) {
  183. $event = elgg_extract('event', $params);
  184. if (!($event instanceof \Elgg\Notifications\Event)) {
  185. return;
  186. }
  187. $question = $event->getObject();
  188. if (!($question instanceof \ElggQuestion)) {
  189. return;
  190. }
  191. $moving = ($event->getAction() === 'moving');
  192. // when moving only notify experts
  193. if ($moving) {
  194. $return_value = [];
  195. }
  196. if (!questions_experts_enabled()) {
  197. // experts isn't enabled
  198. return $return_value;
  199. }
  200. $container = $question->getContainerEntity();
  201. if (!($container instanceof \ElggGroup)) {
  202. $container = elgg_get_site_entity();
  203. }
  204. $experts = [];
  205. $options = [
  206. 'type' => 'user',
  207. 'site_guids' => false,
  208. 'limit' => false,
  209. 'relationship' => QUESTIONS_EXPERT_ROLE,
  210. 'relationship_guid' => $container->getGUID(),
  211. 'inverse_relationship' => true,
  212. ];
  213. $users = elgg_get_entities_from_relationship($options);
  214. if (!empty($users)) {
  215. $experts = $users;
  216. }
  217. // trigger a hook so others can extend the list
  218. $params = [
  219. 'entity' => $question,
  220. 'experts' => $experts,
  221. 'moving' => $moving,
  222. ];
  223. $experts = elgg_trigger_plugin_hook('notify_experts', 'questions', $params, $experts);
  224. if (empty($experts) || !is_array($experts)) {
  225. return;
  226. }
  227. foreach ($experts as $expert) {
  228. if (!isset($return_value[$expert->getGUID()])) {
  229. // no notification for this user, so add email notification
  230. $return_value[$expert->getGUID()] = ['email'];
  231. continue;
  232. }
  233. if (!in_array('email', $return_value[$expert->getGUID()])) {
  234. // user already got a notification, but not email so add that
  235. $return_value[$expert->getGUID()][] = 'email';
  236. continue;
  237. }
  238. }
  239. // small bit of cleanup
  240. unset($experts);
  241. return $return_value;
  242. }
  243. /**
  244. * Add question owner to the subscribers for an answer
  245. *
  246. * @param string $hook the name of the hook
  247. * @param string $type the type of the hook
  248. * @param array $return_value current return value
  249. * @param array $params supplied params
  250. *
  251. * @return void|array
  252. */
  253. public static function addQuestionOwnerToAnswerSubscribers($hook, $type, $return_value, $params) {
  254. $event = elgg_extract('event', $params);
  255. if (!($event instanceof \Elgg\Notifications\Event)) {
  256. return;
  257. }
  258. $answer = $event->getObject();
  259. if (!($answer instanceof \ElggAnswer)) {
  260. return;
  261. }
  262. $question = $answer->getContainerEntity();
  263. $owner = $question->getOwnerEntity();
  264. $methods = get_user_notification_settings($owner->getGUID());
  265. if (empty($methods)) {
  266. return;
  267. }
  268. $filtered_methods = [];
  269. foreach ($methods as $method => $value) {
  270. if (empty($value)) {
  271. continue;
  272. }
  273. $filtered_methods[] = $method;
  274. }
  275. if (empty($filtered_methods)) {
  276. return;
  277. }
  278. $return_value[$owner->getGUID()] = $filtered_methods;
  279. return $return_value;
  280. }
  281. /**
  282. * Add answer owner to the subscribers for an answer
  283. *
  284. * @param string $hook the name of the hook
  285. * @param string $type the type of the hook
  286. * @param array $return_value current return value
  287. * @param array $params supplied params
  288. *
  289. * @return void|array
  290. */
  291. public static function addAnswerOwnerToAnswerSubscribers($hook, $type, $return_value, $params) {
  292. $event = elgg_extract('event', $params);
  293. if (!($event instanceof \Elgg\Notifications\Event)) {
  294. return;
  295. }
  296. $answer = $event->getObject();
  297. if (!($answer instanceof \ElggAnswer)) {
  298. return;
  299. }
  300. $owner = $answer->getOwnerEntity();
  301. $methods = get_user_notification_settings($owner->getGUID());
  302. if (empty($methods)) {
  303. return;
  304. }
  305. $filtered_methods = [];
  306. foreach ($methods as $method => $value) {
  307. if (empty($value)) {
  308. continue;
  309. }
  310. $filtered_methods[] = $method;
  311. }
  312. if (empty($filtered_methods)) {
  313. return;
  314. }
  315. $return_value[$owner->getGUID()] = $filtered_methods;
  316. return $return_value;
  317. }
  318. /**
  319. * Add question subscribers to the subscribers for an answer
  320. *
  321. * @param string $hook the name of the hook
  322. * @param string $type the type of the hook
  323. * @param array $return_value current return value
  324. * @param array $params supplied params
  325. *
  326. * @return void|array
  327. */
  328. public static function addQuestionSubscribersToAnswerSubscribers($hook, $type, $return_value, $params) {
  329. $event = elgg_extract('event', $params);
  330. if (!($event instanceof \Elgg\Notifications\Event)) {
  331. return;
  332. }
  333. $object = $event->getObject();
  334. $container = $object->getContainerEntity();
  335. if (!($container instanceof \ElggAnswer)) {
  336. return;
  337. }
  338. $question = $container->getContainerEntity();
  339. $subscribers = elgg_get_subscriptions_for_container($question->getGUID());
  340. if (empty($subscribers)) {
  341. return;
  342. }
  343. return ($return_value + $subscribers);
  344. }
  345. }