FM Design
Would you like to react to this message? Create an account in a few clicks or log in to continue.

IMPORTANT

FM Design is in read-only mode, please click here for more information.

Latest topics
» Forumactif Edge - Releases
by Ange Tuteur Tue 03 Sep 2019, 11:49

» GIFActif - Giphy Button for the Editor
by Ange Tuteur Wed 08 May 2019, 17:21

» Forum Closure
by Ange Tuteur Mon 01 Jan 2018, 01:28

» Chit Chat Thread
by Valoish Sun 31 Dec 2017, 19:15

» Font/Text background color.
by Valoish Sun 31 Dec 2017, 19:11

» Forumactif Messenger - Instant Message Application for Forumotion
by Wolfuryo Sun 31 Dec 2017, 18:24

» [GAME] Count to One Million!
by brandon_g Fri 29 Dec 2017, 18:58

» Post Cards
by manikbiradar Wed 20 Dec 2017, 07:50

» [GAME] Countdown from 200,000
by Valoish Wed 13 Dec 2017, 23:22

» GeekPolice Tech Support Forums - GeekPolice.net
by Dr Jay Mon 11 Dec 2017, 19:12

» Asking about some plugin for Forumotion
by Dr Jay Mon 11 Dec 2017, 19:10

» [GAME] What are you thinking right now?
by Van-Helsing Sat 09 Dec 2017, 14:51

» Widget : Similar topics
by ranbac Wed 06 Dec 2017, 18:11

» Change the Background of the Forum and put an image and how to make prefixs?
by Clement Wed 06 Dec 2017, 15:19

» Hello from Western Australia
by SarkZKalie Wed 06 Dec 2017, 05:34

Recent Tutorials
Top posting users this month

Who is online?
In total there are 10 users online :: 0 Registered, 0 Hidden and 10 Guests

None

[ View the whole list ]


Most users ever online was 515 on Tue 14 Sep 2021, 15:24

_userdata.usergroup

Page 2 of 2 Previous  1, 2

View previous topic View next topic Go down

Ace 1
Ace 1
Valued Member
Gender : Unspecified
Age : 24
Posts : 2153
Points : 5311
Reputation : 95
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
https://fmdesign.forumotion.com/u190

PostAce 1 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

Ace 1
Ace 1
Valued Member
Gender : Unspecified
Age : 24
Posts : 2153
Points : 5311
Reputation : 95
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
https://fmdesign.forumotion.com/u190

PostAce 1 Fri 03 Mar 2017, 07:13

Just give me an estimate of the maximum amount of groups you might have.
STBW
STBW

Gender : Unspecified
Posts : 33
Points : 3371
Reputation : 2
Language : Spanish
Browser : Browser : Google Chrome Forum Version : Forum Version : Forumactif Edge

PostSTBW 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
Ace 1
Ace 1
Valued Member
Gender : Unspecified
Age : 24
Posts : 2153
Points : 5311
Reputation : 95
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
https://fmdesign.forumotion.com/u190

PostAce 1 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).
STBW
STBW

Gender : Unspecified
Posts : 33
Points : 3371
Reputation : 2
Language : Spanish
Browser : Browser : Google Chrome Forum Version : Forum Version : Forumactif Edge

PostSTBW 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
Ace 1
Ace 1
Valued Member
Gender : Unspecified
Age : 24
Posts : 2153
Points : 5311
Reputation : 95
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
https://fmdesign.forumotion.com/u190

PostAce 1 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:
Ace 1
Ace 1
Valued Member
Gender : Unspecified
Age : 24
Posts : 2153
Points : 5311
Reputation : 95
Location : USA
Language : English ?
Browser : Browser : Google Chrome Forum Version : Forum Version : phpBB3
https://fmdesign.forumotion.com/u190

PostAce 1 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
Sponsored content

PostSponsored content

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