Need help with the share content plugin and adding userpoints for sharing.

Hi all.

I've modified "addthis_share" plugin for my purpose.

mod/addthis_share/start.php:

<?php

elgg_register_event_handler('init', 'system', 'addthis_share_init');

    function addthis_share_init()
        {
        elgg_extend_view('object/elements/summary', 'addthis_share/addthis');
        }
?>

And mod/addthis_share/views/default/addthis_share/addthis.php:

 

<?php
   $full = elgg_extract('full_view', $vars, FALSE);
   $context =  elgg_get_context();

if ($context != 'thewire' && $context != 'widgets' && $context != 'blog' && $context != 'groups' && $context != 'friends' && $context != 'videos' && $context != 'bookmarks')  {
echo '<br><div align="center"><table><tr><td class="ya_share"><div align="center" class="ya-share2" data-services="vkontakte,facebook,odnoklassniki,moimir,twitter,lj,viber,whatsapp,skype,telegram" data-title="Relax Time!" data-description="Relax time!" ></div></td></tr></table></div>';
echo '<script src="//yastatic.net/share2/share.js" charset="utf-8" async="async"></script><br>';
} ?>

I want an userpoints (other plugin) will be adding to users for share content of my site.

Author of userpoints plugin are busy irl atm and can't help with that in the nearest days. But he gave me an explanation how to do it:

The main thing to get working is not the handling of userpoints as such. This could be implemented easily if there would be any kind of feedback available (as in terms of an Elgg plugin hook getting triggered) when something gets shared. The implementation of the triggering of the plugin hook is the tricky part. For this to work, the Javascript code of Addthis would have to be loaded in a different way, i.e. not just with a <script> tag but using elgg_define_js to allow for loading it in the right order together with Elgg's elgg.js library to have the Elgg JS API functions available. The Addthis code has event listeners. These can be used to trigger an Elgg plugin hook which in turn can be used to trigger the awarding of userpoints.

I'm not using exactly "AddThis service" for sharing content, it can be seen above from code.

In documentation of that service:

1. Add script block:

<script src="https://yastatic.net/share2/share.js"></script>

2. Call method "Ya_share2":

var share = Ya.share2('my-share', {
    hooks: {
        onshare: function () {
            alert('Button pressed');
        }
    }
});

Or

var myShare = document.getElementById('my-share');
var share = Ya.share2(my-share, {
    hooks: {
        onshare: function () {
            alert('Button pressed');
        }
    }
});

What and where I need to write for making it work? If I understand correctly- need to write elgg_register_plugin_hook_handler in the init function, but I don't understand how, especially with JS.

Thanks in advance.

  • You can register action.

    You can use elggx_userpoints_add() function in the action.

    This action can be call via event in your JS.

     

  • RvR, so you are advising a bit other way to implement that functionality?

    I'm not a programmer (former system architect), it's hard to understand the syntax of many things in elgg :)

  • I'm very busy to check it out but you can try something of this:

    In start.php

    //Register action
    
    elgg_register_action('points/share', __DIR__ . '/actions/points/share.php', 'logged_in');
    
    //Register JS
     elgg_define_js('share.js', [
           'src' => path_to_js('share2/share.js'),
     ]);

    In action:

    $user = elgg_get_logged_in_user_entity();
    $user_guid = $user->guid;
    $points = 1;
    $description = elgg_echo('for:sharing');
    
    elggx_userpoints_add($user_guid, $points, $description, null, null);
    
    return elgg_ok_response('', elgg_echo('Спасибо, что поделился этим'), REFERER);

    In your JS:

    define(function (require) {
        var elgg = require('elgg');
        var $ = require('jquery');
        var Ajax = require('elgg/Ajax');
        var Ya.share2 = require('share.js');
        
        var myShare = document.getElementById('my-share');
        var ajax = new Ajax();
        var share = Ya.share2(my-share, {
            hooks: {
                onshare: function () {
                    ajax.action('points/share').done(function (output) {
                        elgg.system_message(output.system_messages.success);
                    });
                }
            }
        });
    });

    Learning Elgg could help you more.

  • I wrote in mod/addthis_share/actions/points/share.php- your code above.

    In mod/addthis_share/views/default/js/share.js - your code above.

    In mod/addthis_share/start.php:

    <?php
    
    elgg_register_event_handler('init', 'system', 'addthis_share_init');
    
        function addthis_share_init()
            {
            elgg_extend_view('object/elements/summary', 'addthis_share/addthis');
            elgg_register_action('points/share', __DIR__ . '/actions/points/share.php', 'logged_in');
            elgg_define_js('share.js', [
            'src' => ('js/share.js'),
            ]);
    
            }
    ?>
    
    

    With your code elgg gave error about syntax with unexpected "(":

     PHP Parse error:  syntax error, unexpected '(', expecting ']' in /www/elgg/mod/addthis_share/start.php on line 10

    Not worked. It's able to share content but no points are adding.

    I've named JS-file "share.js", it's ok?

    Or need to download services share.js and put it in js-directory of plugin?

    Help please.

  • You can register the external JS as:

    elgg_register_js('share.js', 'https://yastatic.net/share2/share.js', 'footer');
    elgg_load_js('share.js');

    In this case you don't need to define this JS but your own JS only.

    Read how to do it.

    And then you can use your JS w/o this piece of code:

    var Ya.share2 = require('share.js');
  • mod/addthis_share/views/default/addthis_share/addthis.php:

    <?php
    
       $full = elgg_extract('full_view', $vars, FALSE);
       $context =  elgg_get_context();
    
    if ($context != 'thewire' && $context != 'widgets' && $context != 'blog' && $context != 'groups' && $context != 'friends' && $context != 'videos' && $context != 'bookmarks')  {
    
    echo '<br><div align="center"><table><tr><td class="ya_share"><div align="center" class="ya-share2" data-services="vkontakte,facebook,odnoklassniki,moimir,twitter,lj,viber,whatsapp,skype,telegram"></div></td></tr></table></div>';
    
    } ?>
    

    mod/addthis_share/views/default/js/ya-share.js:

    define(function (require) {
        var elgg = require('elgg');
        var $ = require('jquery');
        var Ajax = require('elgg/Ajax');
    //    var Ya.share2 = require('share.js');
    
        var myShare = document.getElementById('my-share');
        var ajax = new Ajax();
        var ya-share = Ya.share2('my-share', {
            hooks: {
                onshare: function () {
                    ajax.action('points/share').done(function (output) {
                        elgg.system_message(output.system_messages.success);
                    });
                }
            }
        });
    });

     

    /www/elgg/mod/addthis_share/actions/points/share.php:

    <?php
    $user = elgg_get_logged_in_user_entity();
    $user_guid = $user->guid;
    $points = 1;
    $description = elgg_echo('for:sharing');
    
    elggx_userpoints_add($user_guid, $points, $description, null, null);
    
    return elgg_ok_response('', elgg_echo('Thanks'), REFERER);
    ?>

     

    mod/addthis_share/start.php:

    <?php
    
    elgg_register_event_handler('init', 'system', 'addthis_share_init');
    
        function addthis_share_init()
            {
            elgg_extend_view('object/elements/summary', 'addthis_share/addthis');
            elgg_register_action('points/share', __DIR__ . '/actions/points/share.php', 'logged_in');
            elgg_define_js('ya-share.js', [
            'src' => ('js/ya-share.js'),
            ]);
            elgg_register_js('share.js', 'https://yastatic.net/share2/share.js', 'footer');
            elgg_load_js('share.js');
            }
    ?>

    Do not working :/ And logs are empty about that.

    It's possibly to share content (JS loaded and I removed JS-strings from addthis.php) but points not adding...

  • Flush your cache and run an upgrade. This will load your js file.

  • What's happens with this:

    mod/addthis_share/views/default/js/ya-share.js:

    define(function (require) {
        var elgg = require('elgg');
        var $ = require('jquery');
     //   var Ajax = require('elgg/Ajax');
    //    var Ya.share2 = require('share.js');
    
        var myShare = document.getElementById('my-share');
     //   var ajax = new Ajax();
        var ya-share = Ya.share2('my-share', {
            hooks: {
                onshare: function () {
                    alert('OK');
                }
            }
        });
    });
  • Flush your cache and run an upgrade. This will load your js file.

    Of course I'm doing it every time.

     

    What's happens with this:

    No JS alert. Nothing happened even if page is shared through this plugin.

    I've tryed to change names, but nothing happened also:

    define(function (require) {
            var elgg = require('elgg');
            var $ = require('jquery');
             //   var Ajax = require('elgg/Ajax');
             //    var Ya.share2 = require('share.js');
    
            var myShare = document.getElementById('ya-share');
                  //   var ajax = new Ajax();
            var share = Ya.share2('ya-share', {
              hooks: {
              onshare: function () {
              alert('OK');
          }
          }
     });
    });

     

  • Use Console in browser to inspect errors (Ctrl+Shift+I)