Get Group Name of member user

Scenario:

I use the extras Login and MigxDB for uploading files from administrator to profile pages of member users. Basically, administrator will upload the files and will select the group of the users using the migx grid and the file it should to display in the profile page of the members users. Every user has their profile so this is where the files should be pulled from preferably from his group so I can control which files ones are shown and pull the wanted files from there.

This is my migx cmp:

{
“formtabs”:[
{
“MIGX_id”:3,
“caption”:“Info”,
“print_before_tabs”:“0”,
“fields”:[
{
“MIGX_id”:13,
“field”:“title”,
“caption”:“Title”,
“description”:“”,
“description_is_code”:“0”,
“inputTV”:“”,
“inputTVtype”:“”,
“validation”:“”,
“configs”:“”,
“restrictive_condition”:“”,
“display”:“”,
“sourceFrom”:“config”,
“sources”:“”,
“inputOptionValues”:“”,
“default”:“”,
“useDefaultIfEmpty”:“0”,
“pos”:1
},
{
“MIGX_id”:14,
“field”:“date”,
“caption”:“Published Date”,
“description”:“”,
“description_is_code”:“0”,
“inputTV”:“”,
“inputTVtype”:“date”,
“validation”:“required”,
“configs”:“”,
“restrictive_condition”:“”,
“display”:“”,
“sourceFrom”:“config”,
“sources”:“”,
“inputOptionValues”:“”,
“default”:“now”,
“useDefaultIfEmpty”:1,
“pos”:2
},
{
“MIGX_id”:15,
“field”:“upload_file”,
“caption”:“File”,
“description”:“”,
“description_is_code”:“0”,
“inputTV”:“”,
“inputTVtype”:“file”,
“validation”:“required”,
“configs”:“”,
“restrictive_condition”:“”,
“display”:“”,
“sourceFrom”:“config”,
“sources”:“”,
“inputOptionValues”:“”,
“default”:“”,
“useDefaultIfEmpty”:“0”,
“pos”:3
},
{
“MIGX_id”:16,
“field”:“user_group”,
“caption”:“User Group”,
“description”:“”,
“description_is_code”:“0”,
“inputTV”:“”,
“inputTVtype”:“listbox”,
“validation”:“required”,
“configs”:“”,
“restrictive_condition”:“”,
“display”:“”,
“sourceFrom”:“config”,
“sources”:“”,
“inputOptionValues”:“@SELECT name FROM [[+PREFIX]]membergroup_names”,
“default”:“”,
“useDefaultIfEmpty”:“0”,
“pos”:4
},
{
“MIGX_id”:17,
“field”:“published”,
“caption”:“Published”,
“description”:“”,
“description_is_code”:“0”,
“inputTV”:“”,
“inputTVtype”:“checkbox”,
“validation”:“”,
“configs”:“”,
“restrictive_condition”:“”,
“display”:“”,
“sourceFrom”:“config”,
“sources”:“”,
“inputOptionValues”:“Yes==1”,
“default”:1,
“useDefaultIfEmpty”:1,
“pos”:5
}
],
“pos”:1
}
],
“contextmenus”:“update||duplicate||publish||unpublish||remove”,
“actionbuttons”:“addItem”,
“columnbuttons”:“”,
“filters”:“”,
“extended”:{
“migx_add”:“Add File”,
“disable_add_item”:“”,
“add_items_directly”:“”,
“formcaption”:“”,
“update_win_title”:“”,
“win_id”:“files_users”,
“maxRecords”:“”,
“addNewItemAt”:“top”,
“media_source_id”:“”,
“multiple_formtabs”:“”,
“multiple_formtabs_label”:“”,
“multiple_formtabs_field”:“”,
“multiple_formtabs_optionstext”:“”,
“multiple_formtabs_optionsvalue”:“”,
“actionbuttonsperrow”:4,
“winbuttonslist”:“”,
“extrahandlers”:“”,
“filtersperrow”:4,
“packageName”:“files_users”,
“classname”:“File_User”,
“task”:“”,
“getlistsort”:“date”,
“getlistsortdir”:“DESC”,
“sortconfig”:“”,
“gridpagesize”:25,
“use_custom_prefix”:“0”,
“prefix”:“”,
“grid”:“”,
“gridload_mode”:1,
“check_resid”:1,
“check_resid_TV”:“”,
“join_alias”:“”,
“has_jointable”:“yes”,
“getlistwhere”:“”,
“joins”:“”,
“hooksnippets”:“”,
“cmpmaincaption”:“User Group Files”,
“cmptabcaption”:“Table”,
“cmptabdescription”:“Manage your User’s Files here. You can edit them by right-clicking on the respective row.”,
“cmptabcontroller”:“”,
“winbuttons”:“”,
“onsubmitsuccess”:“”,
“submitparams”:“”
},
“columns”:[
{
“MIGX_id”:1,
“header”:“ID”,
“dataIndex”:“id”,
“width”:“”,
“sortable”:true,
“show_in_grid”:1,
“customrenderer”:“”,
“renderer”:“”,
“clickaction”:“”,
“selectorconfig”:“”,
“renderchunktpl”:“”,
“renderoptions”:“”,
“editor”:“”
},
{
“MIGX_id”:2,
“header”:“Title”,
“dataIndex”:“title”,
“width”:“”,
“sortable”:true,
“show_in_grid”:1,
“customrenderer”:“”,
“renderer”:“”,
“clickaction”:“”,
“selectorconfig”:“”,
“renderchunktpl”:“”,
“renderoptions”:“”,
“editor”:“”
},
{
“MIGX_id”:3,
“header”:“File”,
“dataIndex”:“upload_file”,
“width”:“”,
“sortable”:“false”,
“show_in_grid”:“0”,
“customrenderer”:“”,
“renderer”:“”,
“clickaction”:“”,
“selectorconfig”:“”,
“renderchunktpl”:“”,
“renderoptions”:“”,
“editor”:“”
},
{
“MIGX_id”:4,
“header”:“Published Date”,
“dataIndex”:“date”,
“width”:“”,
“sortable”:true,
“show_in_grid”:1,
“customrenderer”:“”,
“renderer”:“this.renderChunk”,
“clickaction”:“”,
“selectorconfig”:“”,
“renderchunktpl”:“[[+date:strtotime:date=%H:%M %d-%m-%Y]]”,
“renderoptions”:“”,
“editor”:“”
},
{
“MIGX_id”:5,
“header”:“User Group”,
“dataIndex”:“user_group”,
“width”:“”,
“sortable”:true,
“show_in_grid”:1,
“customrenderer”:“”,
“renderer”:“”,
“clickaction”:“”,
“selectorconfig”:“”,
“renderchunktpl”:“”,
“renderoptions”:“”,
“editor”:“”
},
{
“MIGX_id”:6,
“header”:“Published”,
“dataIndex”:“published”,
“width”:“”,
“sortable”:“false”,
“show_in_grid”:1,
“customrenderer”:“”,
“renderer”:“this.renderClickCrossTick”,
“clickaction”:“”,
“selectorconfig”:“”,
“renderchunktpl”:“”,
“renderoptions”:“”,
“editor”:“”
}
],
“category”:“”
}

And i created snippet for showing the files based on the group of logged in user. My problem here in the profile pages is not showing something because I can not take the group of logged in user.

This is my snippet

`    <?php
$ctxs = $modx->context->key;
$is_logged_in = $modx->user->hasSessionContext($ctxs);

$output='';

if ($modx->user->get('username') !== '(anonymous)') {
    $groupname = $modx->setPlaceholder('user.groupnames', implode(', ', $modx->user->getUserGroups()));
     
    $sql = "SELECT * FROM `modx_files_users` WHERE user_group='$groupname' AND published='1' ORDER BY date DESC";     
    $query = $modx->query($sql);
    $collection = $query->fetchAll(PDO::FETCH_ASSOC);
            
    foreach($collection as $row) {
        $output .= '<p>';
        $output .= '<a href="'.$row["upload_file"].'">'.$row["title"].'</a>';
        $output .= '</p>';
    }     
    $output .= '</ul>';
}
return $output;

Anything help please?

What do you store in the column user_group of your custom database table. The ID of the group or the group name?

If you store the group name then you probably have to use $modx->user->getUserGroupNames() instead of $modx->user->getUserGroups().

Also, when you have multiple values, you have to use IN in your SQL query:

WHERE user_group IN ('group_1','group_2') ...

I store the group name but still not working.

Do you still have this line in your code?

$groupname = $modx->setPlaceholder('user.groupnames', implode(', ', $modx->user->getUserGroups()));

You should probably change that to

$groupname = implode(',', $modx->user->getUserGroupNames());
$modx->setPlaceholder('user.groupnames', $groupname);

Awesome @halftrainedharry. Its working perfect. Thank you!