Index: trunk/core/units/users/users_event_handler.php =================================================================== diff -u -N -r3834 -r3863 --- trunk/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 3834) +++ trunk/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 3863) @@ -771,33 +771,30 @@ } } + + + /** + * Delete users from groups if their membership is expired + * + * @param kEvent $event + */ function OnCheckExpiredMembership(&$event) { - $sql = 'SELECT PortalUserId FROM '.TABLE_PREFIX.'UserGroup - WHERE MembershipExpires IS NOT NULL AND MembershipExpires < '.adodb_mktime(); - $user_ids = $this->Conn->GetCol($sql); - if(is_array($user_ids) && count($user_ids) > 0) - { - foreach($user_ids as $id) - { - $email_event_user =& $this->Application->EmailEventUser('USER.MEMBERSHIP.EXPIRED', $id); - $email_event_admin =& $this->Application->EmailEventAdmin('USER.MEMBERSHIP.EXPIRED'); - } - } - $sql = 'DELETE FROM '.TABLE_PREFIX.'UserGroup - WHERE MembershipExpires IS NOT NULL AND MembershipExpires < '.adodb_mktime(); - $this->Conn->Query($sql); - + // send pre-expiration reminders: begin $pre_expiration = adodb_mktime() + $this->Application->ConfigValue('User_MembershipExpirationReminder') * 3600 * 24; - $sql = 'SELECT PortalUserId, GroupId FROM '.TABLE_PREFIX.'UserGroup - WHERE MembershipExpires IS NOT NULL AND MembershipExpires < '.$pre_expiration.' - AND ExpirationReminderSent = 0'; - $res = $this->Conn->Query($sql); - if(is_array($res) && count($res) > 0) - { + $sql = 'SELECT PortalUserId, GroupId + FROM '.TABLE_PREFIX.'UserGroup + WHERE (MembershipExpires IS NOT NULL) AND (ExpirationReminderSent = 0) AND (MembershipExpires < '.$pre_expiration.')'; + + $skip_clause = $event->getEventParam('skip_clause'); + if ($skip_clause) { + $sql .= ' AND !('.implode(') AND !(', $skip_clause).')'; + } + + $records = $this->Conn->Query($sql); + if ($records) { $conditions = Array(); - foreach($res as $record) - { + foreach ($records as $record) { $email_event_user =& $this->Application->EmailEventUser('USER.MEMBERSHIP.EXPIRATION.NOTICE', $record['PortalUserId']); $email_event_admin =& $this->Application->EmailEventAdmin('USER.MEMBERSHIP.EXPIRATION.NOTICE'); $conditions[] = '(PortalUserId = '.$record['PortalUserId'].' AND GroupId = '.$record['GroupId'].')'; @@ -807,6 +804,23 @@ WHERE '.implode(' OR ', $conditions); $this->Conn->Query($sql); } + // send pre-expiration reminders: end + + // remove users from groups with expired membership: begin + $sql = 'SELECT PortalUserId + FROM '.TABLE_PREFIX.'UserGroup + WHERE (MembershipExpires IS NOT NULL) AND (MembershipExpires < '.adodb_mktime().')'; + $user_ids = $this->Conn->GetCol($sql); + if ($user_ids) { + foreach ($user_ids as $id) { + $email_event_user =& $this->Application->EmailEventUser('USER.MEMBERSHIP.EXPIRED', $id); + $email_event_admin =& $this->Application->EmailEventAdmin('USER.MEMBERSHIP.EXPIRED'); + } + } + $sql = 'DELETE FROM '.TABLE_PREFIX.'UserGroup + WHERE (MembershipExpires IS NOT NULL) AND (MembershipExpires < '.adodb_mktime().')'; + $this->Conn->Query($sql); + // remove users from groups with expired membership: end } /**