Skip to content

Commit

Permalink
added: notification to group owner when group becomes stale
Browse files Browse the repository at this point in the history
  • Loading branch information
jeabakker committed Feb 14, 2017
1 parent bd1d82d commit 22d3334
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 3 deletions.
229 changes: 229 additions & 0 deletions classes/ColdTrick/GroupTools/Cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?php

namespace ColdTrick\GroupTools;

class Cron {

/**
* Find the new stale groups and notify the owner
*
* @param string $hook the name of the hook
* @param string $type the type of the hook
* @param mixed $return_value current return value
* @param array $params supplied params
*
* @return void
*/
public static function notifyStaleGroupOwners($hook, $type, $return_value, $params) {

$time = (int) elgg_extract('time', $params, time());

$groups = self::findStaleGroups($time);
if (empty($groups)) {
return;
}

foreach ($groups as $group) {

$stale_info = group_tools_get_stale_info($group);
if (empty($stale_info)) {
// error
continue;
}

if (!$stale_info->isStale()) {
// not stale
continue;
}

self::notifyStaleGroupOwner($group);
}
}

/**
* Get all stale group, which became stale today
*
* @param int $ts timestamp to compare to
*
* @return false|\ElggGroup[]
*/
protected static function findStaleGroups($ts) {

if (empty($ts)) {
return false;
}

$stale_timeout = (int) elgg_get_plugin_setting('stale_timeout', 'group_tools');
if ($stale_timeout < 1) {
return false;
}

$compare_ts_upper = $ts - ($stale_timeout * 24 * 60 * 60);
$compare_ts_lower = $compare_ts_upper - (24 * 60 * 60);

$dbprefix = elgg_get_config('dbprefix');
$touch_md_id = elgg_get_metastring_id('group_tools_stale_touch_ts');

$row_to_guid = function ($row) {
return (int) $row->guid;
};

$group_guids = [];

// groups with touch in timespace
$options = [
'type' => 'group',
'limit' => false,
'created_time_upper' => $compare_ts_upper,
'callback' => $row_to_guid,
'wheres' => [
"e.guid IN (SELECT tmd.entity_guid
FROM {$dbprefix}metadata tmd
JOIN {$dbprefix}metastrings tmsv ON tmd.value_id = tmsv.id
WHERE tmd.name_id = {$touch_md_id}
AND tmd.entity_guid = e.guid
AND CAST(tmsv.string AS SIGNED) > {$compare_ts_lower}
AND CAST(tmsv.string AS SIGNED) < {$compare_ts_upper})
",
],
];

$groups_touch_ts = elgg_get_entities($options);
if (!empty($groups_touch_ts)) {
$group_guids = array_merge($group_guids, $groups_touch_ts);
}

// groups with last content in timespace
$searchable_objects = get_registered_entity_types('object');
$object_ids = [];
foreach ($searchable_objects as $index => $subtype) {
switch ($subtype) {
case 'page':
case 'page_top':
$object_ids[] = get_subtype_id('object', 'page');
$object_ids[] = get_subtype_id('object', 'page_top');
break;
case 'comment':
case 'discussion_reply':
// don't do these yet
break;
default:
$object_ids[] = get_subtype_id('object', $subtype);
break;
}
}

$object_ids = array_filter($object_ids);
$object_ids = array_unique($object_ids);

if (!empty($object_ids)) {
$options = [
'type' => 'group',
'limit' => false,
'created_time_upper' => $compare_ts_upper,
'callback' => $row_to_guid,
'wheres' => [
"e.guid IN (
SELECT container_guid
FROM (
SELECT container_guid, max(time_created) as time_created
FROM {$dbprefix}entities
WHERE type = 'object'
AND subtype IN (" . implode(',', $object_ids) . ")
GROUP BY container_guid
) as content
WHERE content.time_created > {$compare_ts_lower}
AND content.time_created < {$compare_ts_upper}
)",
],
];

$group_content_ts = elgg_get_entities($options);
if (!empty($group_content_ts)) {
$group_guids = array_merge($group_guids, $group_content_ts);
}
}

// groups with last comments/discussion_replies in timespace
$comment_ids = [];
$comment_ids[] = get_subtype_id('object', 'comment');
if (elgg_is_active_plugin('discussions')) {
$comment_ids[] = get_subtype_id('object', 'discussion_reply');
}

$options = [
'type' => 'group',
'limit' => false,
'created_time_upper' => $compare_ts_upper,
'callback' => $row_to_guid,
'wheres' => [
"e.guid IN (
SELECT container_guid
FROM (
SELECT ce.container_guid, max(re.time_created) as time_created
FROM {$dbprefix}entities re
JOIN {$dbprefix}entities ce ON re.container_guid = ce.guid
WHERE re.type = 'object'
AND re.subtype IN (" . implode(',', $comment_ids) . ")
GROUP BY ce.container_guid
) as comments
WHERE comments.time_created > {$compare_ts_lower}
AND comments.time_created < {$compare_ts_upper}
)",
],
];

$group_comments_ts = elgg_get_entities($options);
if (!empty($group_comments_ts)) {
$group_guids = array_merge($group_guids, $group_comments_ts);
}

$group_guids = array_unique($group_guids);
if (empty($group_guids)) {
return false;
}

return elgg_get_entities([
'type' => 'group',
'limit' => false,
'guids' => $group_guids,
'batch' => true,
]);
}

/**
* Notify the owner of the group
*
* @param \ElggGroup $entity
*
* @return void
*/
protected static function notifyStaleGroupOwner(\ElggGroup $entity) {

if (!($entity instanceof \ElggGroup)) {
return;
}

$owner = $entity->getOwnerEntity();
if (!($owner instanceof \ElggUser)) {
return;
}

$site = elgg_get_site_entity();

$subject = elgg_echo('groups_tools:state_info:notification:subject', [$entity->getDisplayName()]);
$message = elgg_echo('groups_tools:state_info:notification:message', [
$owner->getDisplayName(),
$entity->getDisplayName(),
$entity->getURL(),
]);

$mail_params = [
'object' => $entity,
'action' => 'group_tools:stale',
'summary' => elgg_echo('groups_tools:state_info:notification:summary', [$entity->getDisplayName()]),
];

notify_user($owner->getGUID(), $site->getGUID(), $subject, $message, $mail_params);
}
}
11 changes: 10 additions & 1 deletion languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
'group_tools:settings:content:title' => "Group content settings",
'group_tools:settings:default_access' => "What should be the default access for content in the groups of this site",
'group_tools:settings:stale_timeout' => "Groups become stale if no content is created within a number of days",
'group_tools:settings:stale_timeout:help' => "If no new content is created in a group within the given number of days, the group is shown as stale. A group owner/admin can tell the group is still relevant. 0 or empty to not enable this feature.",
'group_tools:settings:stale_timeout:help' => "If no new content is created in a group within the given number of days, the group is shown as stale. The group owner will receive a notification on the day the group becomes stale. A group owner/admin can tell the group is still relevant. 0 or empty to not enable this feature.",

'group_tools:settings:search_index' => "Allow closed groups to be indexed by search engines",
'group_tools:settings:auto_notification' => "Automatically enable group notification on group join",
Expand Down Expand Up @@ -578,4 +578,13 @@
'group_tools:csv_exporter:stale_info:is_stale' => "Stale group",
'group_tools:csv_exporter:stale_info:timestamp' => "Stale timestamp",
'group_tools:csv_exporter:stale_info:timestamp:readable' => "Stale timestamp (readable)",

'groups_tools:state_info:notification:subject' => "Your group '%s' has been inactive for a while",
'groups_tools:state_info:notification:summary' => "Your group '%s' has been inactive for a while",
'groups_tools:state_info:notification:message' => "Hi %s,
Your group '%s' has been inactive for a while.
Please check on the group here:
%s",
);
12 changes: 10 additions & 2 deletions languages/nl.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php
/**
* This file was created by Translation Editor v5.2
* On 2017-02-13 12:53
* On 2017-02-14 15:29
*/

return array (
'groups_tools:state_info:notification:subject' => 'Je groep \'%s\' is al een tijd inactief',
'groups_tools:state_info:notification:summary' => 'Je groep \'%s\' is al een tijd inactief',
'groups_tools:state_info:notification:message' => 'Beste %s,
Je groep \'%s\' is al een tijd inactief
Bekijk de groep hier:
%s',
'group_tools:settings:stale_timeout:help' => 'Als er geen nieuwe inhoud wordt gemaakt in een groep binnen het opgegeven aantal dagen, wordt de groep getoond als inactief. De groepseigenaar krijgt op de dag dat de groep inactief wordt een bericht. Een groep eigenaar / beheerder kan zeggen dat de groep nog steeds relevant is. 0 of leeg om deze functie niet in te schakelen.',
'group_tools:settings:admin_only' => 'Alleen site beheerders',
'group_tools:settings:edit:title' => 'Groep bewerken instellingen',
'group_tools:settings:simple_access_tab' => 'Vereenvoudigde groepstoegang selectie',
Expand All @@ -14,7 +23,6 @@
'group_tools:settings:allow_hidden_groups:help' => 'Wie mag er verborgen groepen aanmaken. Deze instelling overschrijft de instelling van de Groepen plugin.',
'group_tools:settings:content:title' => 'Groep content instellingen',
'group_tools:settings:stale_timeout' => 'Groepen worden inactief als er geen inhoud is gemaakt binnen een aantal dagen',
'group_tools:settings:stale_timeout:help' => 'Als er geen nieuwe inhoud wordt gemaakt in een groep binnen het opgegeven aantal dagen, wordt de groep getoond als inactief. Een groep eigenaar / beheerder kan zeggen dat de groep nog steeds relevant is. 0 of leeg om deze functie niet in te schakelen.',
'group_tools:edit:access_simplified:open' => 'Open groep',
'group_tools:edit:access_simplified:open:description' => '<ul><li>Iedere gebruiker mag lid worden</li><li>Content kan gedeeld worden met iedereen</li></ul>',
'group_tools:edit:access_simplified:closed' => 'Besloten groep',
Expand Down
2 changes: 2 additions & 0 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ function group_tools_init() {
elgg_register_plugin_hook_handler('export_value', 'csv_exporter', '\ColdTrick\GroupTools\CSVExporter::exportGroupAdminsForUsers');
elgg_register_plugin_hook_handler('export_value', 'csv_exporter', '\ColdTrick\GroupTools\CSVExporter::exportStaleInfo');

elgg_register_plugin_hook_handler('cron', 'daily', '\ColdTrick\GroupTools\Cron::notifyStaleGroupOwners');

// actions
elgg_register_action('group_tools/toggle_admin', dirname(__FILE__) . '/actions/toggle_admin.php');
elgg_register_action('group_tools/mail', dirname(__FILE__) . '/actions/mail.php');
Expand Down

0 comments on commit 22d3334

Please sign in to comment.