Jump to content

How to get a User's groups


Graham Quince

Recommended Posts

I thought I'd share this snippet more for reference than for a specific need, but it saves you have to work it out later.  It uses an FDP API (fixed so they will not be changed, unlike the internal APIs)

This is example, it was needed to change the site's background and banner to match a student's house colours.  I know I could have set the CSS in Javascript, but pages load on demand and by rendering out the CSS, it is easy to adapt for all sites, or all pages etc...

<div class="styling"></div>

<script>
var siteUUID = 'SITE_UUID',
    user = FrogOS ? FrogOS.getUser() : this.getUser(),
    styling = arguments[0].find('.styling');
    houseColours = {
        'House1': {
            background: 'rgba(0,255,0,1)',
            banner: 'file/resource/7E4864D720030D3DB0338F5280C6A104F3BB61EC80ADD2A4'
        },
        'House2': {
            background: 'rgba(255,0,0,1)',
            banner: 'file/resource/7E4864D720030D3DB0338F5280C6A104F3BB61EC80ADD2A4'
        },
        'House3': {
            background: 'rgba(255,255,0,1)',
            banner: 'file/resource/7E4864D720030D3DB0338F5280C6A104F3BB61EC80ADD2A4'
        },
        'House4': {
            background: 'rgba(0,0,255,1)',
            banner: 'file/resource/7E4864D720030D3DB0338F5280C6A104F3BB61EC80ADD2A4'
        }
    };

    if (user.profile.type == 'admin' || user.profile.type == 'superuser') {
        user.uuid = 'STUDENT_UUID'; 
    }
FrogOS.fdp({
    url: 'group/memberships',
    path: '/api/fdp/2/',
    data: { 
        uuid: user.uuid
    }
}).done(function(response) {

    var house = 'House1';
    var groups = response.response;
    $.each(groups, function(index,group) {
        if (group.type == 'house') {
            house = group.name;
        }
    });
    
    
    var background = houseColours[house].background,
        banner = houseColours[house].banner;
    styling.html(
        '<style>'+
        'div[data-site-uuid="'+siteUUID+'"] .ui-theme-mobilesmallbanner-bodyline {'+
            'background: '+background+' !important;'+
            'background: linear-gradient(180deg, '+background+' 0%, rgba(255,255,255,1) 20%) !important;'+
        '}'+
        'div[data-site-uuid="'+siteUUID+'"] .ui-theme-mobilesmallbanner-container {'+
            'background-image: url('+banner+') !important;'+ 
        '}'+
        '</style>'
    )
    
    
}).fail(function(e) {
   // Report Error
   console.log("failed");
});


</script>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...