nlybe/special_notifications

为 Elgg 社区某些事件提供的扩展通知功能

dev-master 2017-11-23 22:52 UTC

This package is auto-updated.

Last update: 2024-09-17 12:20:54 UTC


README

Elgg 2.3

为 Elgg 社区某些事件提供的扩展通知功能。

此插件可供开发者使用,根据某些条件自动向用户发送通知,如果这些条件不满足。

例如,如果当前用户缺少特定个人资料字段,则应通知该用户。

功能

  • 通过钩子注册更多事件/条件
  • 提供不同的通知方法

如何使用

  1. 注册新检查事件的钩子,包含所需选项。示例
elgg_register_plugin_hook_handler('special_notifications:config', 'notify', "snotify_profile_location");

function snotify_profile_location($hook, $type, $return, $params) {
    
    $key = 'profile_location';
    if (!$params || (is_array($params) && $params['notifier'] == $key)) {
        $return[$key] = [
            'active' => true, 
            'hook' => 'profile_location_missing', 
            'methods' => [SpecialNotificationsOptions::SN_METHOD_INLINE], 
        ];
    }
    return $return;
}
  1. 注册一个钩子,用于检查某些标准是否满足。如果不满足,则应向用户发送一个或多个通知。示例
function special_notification_profile_location_missing($hook, $type, $return, $params) {
    if ($type !== 'user') {
        return;
    }
    
    $user = elgg_get_logged_in_user_entity();
    if (!$user) {
        return;
    }
   
    $key = 'profile_location';
    $settings = elgg_trigger_plugin_hook('special_notifications:config', 'notify', ['notifier' => $key], []);
    if (!$settings[$key]['active']) {
        return;
    }

    // check the condition for notify the user
    $notify = false;
    if (!$user->location) {
        $notify = true;
    }

    if ($notify) {
        $methods = $settings[$key]['methods'];
        foreach ($methods as $m) {
            switch ($m) {
                case "inline":
                    $close_btn = elgg_format_element('a', ['class' => 'close', 'data-dismiss' => 'alert', 'aria-label' => 'close'], '×');
                    $inline = elgg_view('special_notifications/inline',[
                        'content' => $close_btn.elgg_echo('special_notifications:profile_location:message'),
                        'class' => 'alert alert-warning fade in',
                    ]);
                    break;
                
                case "elgg_error":
                    register_error(elgg_echo('special_notifications:profile_location:message'));
                    break;
            }
            
        }
        
        if ($inline) {
            return $inline;
        }
    }
    
    return;
}
  1. 在任何地方触发钩子或特定事件之后,例如登录后。以下示例可以插入到个人资料页面并显示警告
if (elgg_is_active_plugin('special_notifications') && elgg_get_logged_in_user_guid()==$user->getGUID()) {
    if ($notifications = elgg_trigger_plugin_hook('special_notifications', "user", [], false)) {
        $content = elgg_format_element('div', ['class' => 'col-md-12 col-sm-12 col-xs-12'], $notifications);
    }
}
...
echo content;
...

例如,此插件提供了一个检查事件:检查用户是否在个人资料中输入了位置。如果位置为空,则通知用户。

未来改进

  • 实现 'elgg_notification' 方法:使用标准的 Elgg 通知方法 (notify_user)
  • 使用注解保存用户交互。例如,用户应能够选择不再被通知或在一定时间后通知。
  • 添加更多原生的用户通知检查事件