Jump to content
Graham Quince

How to get a User's groups

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Similar Content

    • By Graham Quince
      Hi
      https://frog.frogcommunity.com/understanding-api
      I've tried to write it for those of you who understand code, but have never tried to access an API. Before I joined Frog, I had never been successful in using APIs. I'm sure users like JElliott * will find this insultingly simple and be appalled at the poor use of coding terms (I don't mention the DOM once). 
      For the rest of us mortals, I hope this tutorial can show you the possibilities of using Frog's APIs and that it will encourage you to build your own resources.
      Graham

      * I'd love your feedback to improve this, to be honest
×
×
  • Create New...