@@ -25,12 +25,6 @@ public static function autoCloseQuestions(\Elgg\Event $event): void {
2525 }
2626
2727 elgg_call (ELGG_IGNORE_ACCESS , function () use ($ auto_close_days ) {
28- $ site = elgg_get_site_entity ();
29-
30- // backup session
31- $ backup_user = elgg_get_logged_in_user_entity ();
32- $ session_manager = elgg ()->session_manager ;
33-
3428 // get open questions last modified more than x days ago
3529 /* @var $batch \ElggBatch */
3630 $ batch = elgg_get_entities ([
@@ -44,42 +38,25 @@ public static function autoCloseQuestions(\Elgg\Event $event): void {
4438 'batch ' => true ,
4539 'batch_inc_offset ' => false ,
4640 ]);
41+
4742 /* @var $question \ElggQuestion */
4843 foreach ($ batch as $ question ) {
44+ /* @var $owner \ElggUser */
4945 $ owner = $ question ->getOwnerEntity ();
50- $ session_manager ->setLoggedInUser ($ owner );
5146
5247 // close the question
5348 $ question ->close ();
5449
5550 // notify the user that the question was closed
56- $ subject = elgg_echo ('questions:notification:auto_close:subject ' , [$ question ->getDisplayName ()]);
57- $ message = elgg_echo ('questions:notification:auto_close:message ' , [
58- $ question ->getDisplayName (),
59- $ auto_close_days ,
60- $ question ->getURL (),
51+ $ owner ->notify ('close ' , $ question , [
52+ 'days ' => $ auto_close_days ,
6153 ]);
62-
63- $ notification_params = [
64- 'summary ' => elgg_echo ('questions:notification:auto_close:summary ' , [$ question ->getDisplayName ()]),
65- 'object ' => $ question ,
66- 'action ' => 'close ' ,
67- ];
68-
69- notify_user ($ owner ->guid , $ site ->guid , $ subject , $ message , $ notification_params );
70- }
71-
72- // restore session
73- if ($ backup_user instanceof \ElggUser) {
74- $ session_manager ->setLoggedInUser ($ backup_user );
75- } else {
76- $ session_manager ->removeLoggedInUser ();
7754 }
7855 });
7956 }
8057
8158 /**
82- * A event handler for the CRON, so we can send out notifications to the experts about their workload
59+ * An event handler for the CRON, so we can send out notifications to the experts about their workload
8360 *
8461 * @param \Elgg\Event $event 'cron', 'daily'
8562 *
@@ -109,7 +86,7 @@ public static function notifyQuestionExperts(\Elgg\Event $event): void {
10986 $ sub = $ qb ->subquery (MetadataTable::TABLE_NAME )
11087 ->select ('entity_guid ' )
11188 ->where ($ qb ->compare ('name ' , '= ' , 'status ' , ELGG_VALUE_STRING ))
112- ->andWhere ($ qb ->compare ('value ' , '= ' , ' closed ' , ELGG_VALUE_STRING ));
89+ ->andWhere ($ qb ->compare ('value ' , '= ' , \ElggQuestion:: STATUS_CLOSED , ELGG_VALUE_STRING ));
11390
11491 return $ qb ->compare ("{$ main_alias }.guid " , 'NOT IN ' , $ sub ->getSQL ());
11592 };
@@ -129,8 +106,9 @@ public static function notifyQuestionExperts(\Elgg\Event $event): void {
129106 // fake a logged-in user
130107 $ session_manager ->setLoggedInUser ($ expert );
131108
132- $ subject = elgg_echo ('questions:daily:notification:subject ' , [], $ expert ->getLanguage ());
133- $ message = '' ;
109+ $ overdue = '' ;
110+ $ due = '' ;
111+ $ new = '' ;
134112
135113 $ container_where = questions_get_expert_where_sql ($ expert ->guid );
136114 if (empty ($ container_where )) {
@@ -157,14 +135,9 @@ public static function notifyQuestionExperts(\Elgg\Event $event): void {
157135 ];
158136 $ questions = elgg_get_entities ($ question_options );
159137 if (!empty ($ questions )) {
160- $ message .= elgg_echo ('questions:daily:notification:message:overdue ' , [], $ expert ->getLanguage ()) . PHP_EOL ;
161-
162138 foreach ($ questions as $ question ) {
163- $ message .= " - {$ question ->getDisplayName ()} ( {$ question ->getURL ()}) " . PHP_EOL ;
139+ $ overdue .= " - {$ question ->getDisplayName ()} ( {$ question ->getURL ()}) " . PHP_EOL ;
164140 }
165-
166- $ message .= elgg_echo ('questions:daily:notification:message:more ' , [], $ expert ->getLanguage ());
167- $ message .= ' ' . elgg_generate_url ('collection:object:question:todo ' ) . PHP_EOL . PHP_EOL ;
168141 }
169142
170143 // get due questions
@@ -186,14 +159,9 @@ public static function notifyQuestionExperts(\Elgg\Event $event): void {
186159
187160 $ questions = elgg_get_entities ($ question_options );
188161 if (!empty ($ questions )) {
189- $ message .= elgg_echo ('questions:daily:notification:message:due ' , [], $ expert ->getLanguage ()) . PHP_EOL ;
190-
191162 foreach ($ questions as $ question ) {
192- $ message .= " - {$ question ->getDisplayName ()} ( {$ question ->getURL ()}) " . PHP_EOL ;
163+ $ due .= " - {$ question ->getDisplayName ()} ( {$ question ->getURL ()}) " . PHP_EOL ;
193164 }
194-
195- $ message .= elgg_echo ('questions:daily:notification:message:more ' , [], $ expert ->getLanguage ());
196- $ message .= ' ' . elgg_generate_url ('collection:object:question:todo ' ) . PHP_EOL . PHP_EOL ;
197165 }
198166
199167 // get new questions
@@ -208,20 +176,18 @@ public static function notifyQuestionExperts(\Elgg\Event $event): void {
208176
209177 $ questions = elgg_get_entities ($ question_options );
210178 if (!empty ($ questions )) {
211- $ message .= elgg_echo ('questions:daily:notification:message:new ' , [], $ expert ->getLanguage ()) . PHP_EOL ;
212-
213179 foreach ($ questions as $ question ) {
214- $ message .= " - {$ question ->getDisplayName ()} ( {$ question ->getURL ()}) " . PHP_EOL ;
180+ $ new .= " - {$ question ->getDisplayName ()} ( {$ question ->getURL ()}) " . PHP_EOL ;
215181 }
216-
217- $ message .= elgg_echo ('questions:daily:notification:message:more ' , [], $ expert ->getLanguage ());
218- $ message .= ' ' . elgg_generate_url ('collection:object:question:all ' ) . PHP_EOL . PHP_EOL ;
219182 }
220183
221- // is there content in the message
222- if (!empty ($ message )) {
223- // force to email
224- notify_user ($ expert ->guid , 0 , $ subject , $ message , [], 'email ' );
184+ // is there content for the message
185+ if (!empty ($ overdue ) || !empty ($ due ) || !empty ($ new )) {
186+ $ expert ->notify ('questions_expert_workload ' , $ expert , [
187+ 'overdue ' => $ overdue ,
188+ 'due ' => $ due ,
189+ 'new ' => $ new ,
190+ ]);
225191 }
226192 }
227193
0 commit comments