Latest topics
» [HELP] About Profile Cover Code
by MaHwOs Yesterday at 18:54

» bloquer un membre
by berlingo Fri 17 Nov 2017, 18:19

» IconSkouliki Graphics
by skouliki Fri 17 Nov 2017, 03:54

» [IN PROGRESS] Can this code work on pun version
by skouliki Thu 16 Nov 2017, 03:50

» Auto-suggest @mentions as You Type
by skouliki Wed 15 Nov 2017, 14:56

» [TUTORIAL] [ALL VERSIONS] Warning before posting
by demon99 Wed 15 Nov 2017, 08:12

» About Add Reply & New Topic button (popup form)
by omarpop23 Mon 13 Nov 2017, 20:31

» Hello everyone this is Adam from France
by Adam Mon 13 Nov 2017, 13:32

» Add a login popup for the toolbar
by Ape Sun 12 Nov 2017, 18:41

» Post a message automatically when locking a topic
by ThunderTB Sun 12 Nov 2017, 13:51

» Control pannel issue
by Ange Tuteur Sun 12 Nov 2017, 09:49

» BUG... forum members birthday's
by Ape Sat 11 Nov 2017, 11:44

» Display a Preview of Groups on Hover
by ThunderTB Fri 10 Nov 2017, 21:56

» Search box only visible on hover
by Ange Tuteur Mon 06 Nov 2017, 09:57

» [GAME] Animal Game
by SLGray Sun 05 Nov 2017, 15:04

Recent Tutorials
Who is online?
In total there are 7 users online :: 0 Registered, 0 Hidden and 7 Guests :: 1 Bot

None

[ View the whole list ]


Most users ever online was 172 on Fri 19 Feb 2016, 05:17

_userdata.usergroup

Page 2 of 2 Previous  1, 2

View previous topic View next topic Go down

avatar
Ace 1
Valued Member
Title :

Beta Tester
Gender : Unspecified
Age : 18
Posts : 2151
Points : 2950
Reputation : 88
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
Awards :
View all awards
View user profile http://fmdesign.forumotion.com/u190

PostAce 1 on Mon 14 Nov 2016, 09:16

First topic message reminder :

With this plugin, the variable
Code:
_userdata.usergroup
is now accessible on any forum version (including FAE).


Installation


Administration Panel > Modules > HTML & Javascript > Javascript codes management > Create new javascript

Title: _userdata.usergroup
Placement: In all the pages

Code:

Code:
$(function() {
  '_userdata.usergroup ©️️ 2017 Version 5.0';
  'Developed by Ace 1';
  'All rights reserved';
 
  window.faGroups = {
    groupIDs: [1, 2, 3], // list of groupIDs
    cacheTime: 30, // time in minutes to store data
 
    multipleGroups: false, // enable to return all groups the user is in
 
    storage: window.localStorage,
    user_id: _userdata.session_logged_in ? _userdata.user_id : 0,
 
    setGroup: function() {
      var group,
          i = 0, j;
 
      if (faGroups.storage && faGroups.user_id > 0) {
        if (!faGroups.storage['ug' + faGroups.user_id] || !(faGroups.storage['ug' + faGroups.user_id + '_exp'] > new Date().getTime() - faGroups.cacheTime * 60 * 1000)) {
          $.ajax({
            url: '/groups',
            type: 'get',
            async: false,
            success: function(d) {
              if (faGroups.multipleGroups) group = [];
         
              for (; i < faGroups.groupIDs.length; i++) {
                j = $('ul.noList a[href^="/g' + faGroups.groupIDs[i] + '-', d);
 
                if (j[0]) {
                  if (!faGroups.multipleGroups) {
                    group = faGroups.groupIDs[i];
                    i = faGroups.groupIDs.length;
                  } else {
                    group.push(faGroups.groupIDs[i]);
                  }
                }
              }
             
              faGroups.storage['ug' + faGroups.user_id] = group ? JSON.stringify(group) : group;
              faGroups.storage['ug' + faGroups.user_id + '_exp'] = new Date().getTime();
            }
          });
        }
 
        // sets _userdata.usergroup to the user's current group
        if (faGroups.storage['ug' + faGroups.user_id] != 'undefined') {
          try {
            _userdata.usergroup = JSON.parse(faGroups.storage['ug' + faGroups.user_id]);
          } catch(e) {
            _userdata.usergroup = faGroups.storage['ug' + faGroups.user_id];
          }
        } else {
          _userdata.usergroup = -1; // user not within any group
        }
      } else {
        _userdata.usergroup = 0; // _userdata.session_logged_in == 0
      }
    },
 
    returnGroups: function() {
      if (!_userdata.usergroup) return;
 
      return JSON.parse(faGroups.storage['ug' + faGroups.user_id]);
    },
 
    checkGroups: function() {
      var groups_to_check = [].slice.apply(arguments),
          user_groups,
          in_groups;
 
      if (groups_to_check.length < 1 || !_userdata.usergroup) return;
 
      user_groups = _userdata.usergroup;
 
      if (typeof user_groups == 'object') {
        // usergroup == array
   
        if (groups_to_check.length > 1) {
          // arguments == array
          in_groups = user_groups.every(function(val) {
            return groups_to_check.indexOf(val) > -1;
          });
     
          return in_groups ? true : false;
        } else {
          // arguments == string
          return user_groups.indexOf(groups_to_check[0]) > -1 ? true : false;
        }
      } else {
        // usergroup == string
   
        if (groups_to_check.length > 1) {
          // arguments == array
          return false;
     
        } else {
          // arguments == string
          return groups_to_check[0] == user_groups ? true : false;
        }
      }
    }
  };
 
  faGroups.setGroup();
 
  /*** Progress
    var logs = {
      'Version 1.0': 'Developed',
      'Version 2.0': 'Enabled values -1 and 0 (no groups and not logged in, respectively)',
      'Version 3.0': 'Enhanced and simplified',
      'Version 4.0': 'Enabled multipleGroups, returnGroups(), checkGroups(args)'
      'Version 5.0': 'Patched unexpected tokens in JSON as well as errors when _userdata.session_logged_in == 0'
    };
  ***/
});

Note: This is not optimized for special moderation privileges. It is not secure. If you want a javascript that is, then just leave a comment below.


Modifications


I think the modifications are pretty self-explanatory. If anyone has an issue, then feel free to leave a reply on this thread.


Group IDs
Code:
    groupIDs: [ 1, 2, 3 ],
The array
Code:
1, 2, 3
is the order of group IDs on your forum,
Code:
1
having the highest priority and
Code:
3
having the lowest.


Cache Time
Code:
    cacheTime: 30,
The amount of time that usergroups are checked.
Code:
30
is the amount of minutes your browser will store the usergroup number.


New! Multiple Groups Option
Code:
    multipleGroups: false,
This is new and I got motivation to work on it, thanks to @STBW. Basically, when this is set to
Code:
true
, all usergroups that a user is in will be searched for and stored, as opposed to just the highest usergroup. If you don't care about lower usergroups that a user is in, then leave this as is.


Use


You're probably wondering how this would be useful. With this plugin comes two new functions. One that can return the group(s) that the user is in (depending on what
Code:
multipleGroups
is set to) and another that can check if the user is in the group(s) passed into the function.


New! returnGroups
Code:
faGroups.returnGroups();

// will return a number or an array


New! checkGroups
Code:
faGroups.checkGroups(1);
faGroups.checkGroups(1, 2, ...);

// will return true or false


Let's put these to use. For example, if you are using FAE and wanted to change the background for all Administrator group members when they visit the forum, you would add the following JavaScript:

Code:
$(function() {
    faGroups.checkGroups(1) && $('#fa_edge').css('background-color', 'black');
});

Since, on any Forumotion forum, the administrator group ID will always be
Code:
1
.



Also, these are some key numbers that you should know if you use this plugin.

When
Code:
_userdata.usergroup
is:

Code:
0
, the user is not logged in
Code:
-1
, the user is not in any group

I hope you guys like it

Rose


Last edited by Ace 1 on Fri 06 Oct 2017, 09:53; edited 17 times in total

avatar
Ace 1
Valued Member
Title :

Beta Tester
Gender : Unspecified
Age : 18
Posts : 2151
Points : 2950
Reputation : 88
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
Awards :
View all awards
View user profile http://fmdesign.forumotion.com/u190

PostAce 1 on Fri 03 Mar 2017, 07:13

Just give me an estimate of the maximum amount of groups you might have.
avatar
STBW

Title :
Gender : Unspecified
Posts : 33
Points : 1029
Reputation : 2
Language : Spanish
Browser : Browser : Google Chrome Forum Version : Forum Version : Forumactif Edge
Awards :
View all awards
View user profile

PostSTBW on Fri 03 Mar 2017, 07:38

Hmmm.. put around 150-200 (If it is not too much >.<) if not I can try to restrict it to users, I am being optimist with the possible amount Rolling Eyes Razz
avatar
Ace 1
Valued Member
Title :

Beta Tester
Gender : Unspecified
Age : 18
Posts : 2151
Points : 2950
Reputation : 88
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
Awards :
View all awards
View user profile http://fmdesign.forumotion.com/u190

PostAce 1 on Fri 03 Mar 2017, 08:08

Oh damn... That's a lot of groups. To be honest, I'm surprised the code above hasn't already made things unpleasant for the members of your forum. I mean, I feel bad telling you that getting what you want to be difficult. The script iterates through all the group URLs (and all the pages within each) to find the currently logged on user. It stops at the first one; this saves a lot of time. If I were to make it loop through ALL groups EVERY time it could put strain on your browser.

Maybe some time in the future Forumotion could add usergroup as a default to the _userdata object (but even then, doing your task could be really detrimental to end-users' experiences).
avatar
STBW

Title :
Gender : Unspecified
Posts : 33
Points : 1029
Reputation : 2
Language : Spanish
Browser : Browser : Google Chrome Forum Version : Forum Version : Forumactif Edge
Awards :
View all awards
View user profile

PostSTBW on Fri 03 Mar 2017, 08:53

Haha! Alright, I see what do you mean.

And.. what if we make the code to work just for 20 specific groups? So if a user is on more of one of those 20 specific groups the functions associed to those groups work at same time. There is any possibility of that?

Ty for your time

EDITED: Forgotten to mention you @Ace 1
avatar
Ace 1
Valued Member
Title :

Beta Tester
Gender : Unspecified
Age : 18
Posts : 2151
Points : 2950
Reputation : 88
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
Awards :
View all awards
View user profile http://fmdesign.forumotion.com/u190

PostAce 1 on Fri 18 Aug 2017, 21:13

Well, it's been a while but I looked back over this when trying to decrease loading time on my forum and I decided that like always my methods were extremely convoluted and could be greatly simplified. So that's precisely what I did. like said topic

Hope there's less lag now.

@STBW:
dw I haven't forgotten about you I'll be working on a solution rn (should be done in the next 2 days)
avatar
Ace 1
Valued Member
Title :

Beta Tester
Gender : Unspecified
Age : 18
Posts : 2151
Points : 2950
Reputation : 88
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
Awards :
View all awards
View user profile http://fmdesign.forumotion.com/u190

PostAce 1 on Sat 19 Aug 2017, 18:16

Sorry for the double post; I'm just pointing out that I've made a bunch of revisions to the script.

I hope it is used and enjoyed. Especially by @Ange Tuteur

Page 2 of 2 Previous  1, 2

View previous topic View next topic Back to top

Create an account or log in to leave a reply

You need to be a member in order to leave a reply.

Create an account

Join our community by creating a new account. It's easy!


Create a new account

Log in

Already have an account? No problem, log in here.


Log in

 
Permissions in this forum:
You cannot reply to topics in this forum