Jump to content

Sending notifications by usergroup


rgf100

Recommended Posts

Posted

I'm working on a small application which will 

a) Allow those in a moderation usergroup to move new (moderated) members into the normal (unmoderated) usergroup. 

b) Send a notification

I've got a) working smoothly. I've figured out how to send the notifications I want, but only to one person. 

$notification->recipients->attach( \IPS\Member::load(1) ); 

I want to send to an entire usergroup. I can see how I can check an individual member against a usergroup with ->inGroup. But what's the most efficient way to do a foreach loop over a usergroup (which will only ever be very small, against a membership in the tens of thousands. Or alternatively, a foreach loop over every member who has the notification turn on (they'll be one and the same). 

I can see stuff that might be useful in code for sending notifications for moderators

        foreach ( \IPS\Db::i()->select( '*', 'core_members', ( count( $moderators['m'] ) ? \IPS\Db::i()->in( 'member_id', $moderators['m'] ) . ' OR ' : '' ) . \IPS\Db::i()->in( 'member_group_id', $moderators['g'] ) . ' OR ' . \IPS\Db::i()->findInSet( 'mgroup_others', $moderators['g'] ) ) as $member )
        {
            $notification->recipients->attach( \IPS\Member::constructFromData( $member ) );
        }

But I'm wondering if there's a better / recommended way of doing it. It's a little different from the usual IPS notification, as nothing is being followed. 

Posted

If you're loading multiple members in a loop, it's better to either query for them in your second example, and constructFromData or use an ActiveRecordIterator to return an array of member objects that meet your criteria.

 

Posted

Ok, so got it working with this code, for anyone else like me who finds themselves in need of it.

Quote

foreach ( \IPS\Db::i()->query ("SELECT * FROM core_members WHERE member_group_id=4") as $tonotify) 
    {
            $notification->recipients->attach( \IPS\Member::constructFromData( $tonotify ) );
        }

I spent a bit of time trying to get it work with \IPS\Db::i()->select but for some reason failed. 

I'm not done here as I'll actually need it to work on a secondary user group ultimately. I'm also unsure if this is an efficient way of doing it? It's fine on my test install, but when there are 20,000 members... Learning as I go here...

Anyway, thanks for help. 

Posted

And the foreach for a secondary user group...

Quote

   foreach ( \IPS\Db::i()->query ("SELECT * FROM core_members WHERE". \IPS\Db::i()->findInSet( 'mgroup_others', array(6) )) as $tonotify)

This took me an embarrassing amount of time to figure out, but I got there!

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...