Skip to content

Commit

Permalink
Added message limits per interval per role config options to admin in…
Browse files Browse the repository at this point in the history
…terface.
  • Loading branch information
mccrodp committed Dec 12, 2014
1 parent bc68b70 commit d7fca74
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 11 deletions.
101 changes: 101 additions & 0 deletions message_private.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,106 @@ function message_private_admin_settings() {
'#description' => t('Global On / Off checkbox for emails notifying users of a new private message'),
);

$form['message_private_message_limit'] = array(
'#type' => 'checkbox',
'#title' => t('Limit Message Create By Role'),
'#default_value' => variable_get('message_private_message_limit', FALSE),
'#description' => t('Impose a message creation limit per interval. Users with multiple roles, get the highest limit from these roles'),
);

$form['message_private_message_interval_limit'] = array(
'#title' => t('Message interval limits'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#states' => array(
'invisible' => array(
':input[name="message_private_message_limit"]' => array('checked' => FALSE),
),
),
);

// Add a default fieldset.
$form['message_private_message_interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX] = array(
'#type' => 'fieldset',
'#title' => 'Default limit',
'#description' => t('Applies to all roles with blank entries below')
);

$form['message_private_message_interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX]['limit'] = array(
'#type' => 'textfield',
'#title' => t('Limit'),
'#default_value' => variable_get('message_private_default_limit', TRUE),
'#description' => t('Enter a message limit ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX),
);

$form['message_private_message_interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX]['interval'] = array(
'#type' => 'textfield',
'#title' => t('Interval'),
'#default_value' => variable_get('message_private_default_interval', TRUE),
'#description' => t('Enter an interval in minutes ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX),
);

foreach(user_roles() as $id => $role) {
$role_name = str_replace(' ', '_', $role);
$limit_name = 'message_private_' . $role_name . '_limit';
$interval_name = 'message_private_' . $role_name . '_interval';

$form['message_private_message_interval_limit'][$id] = array(
'#type' => 'fieldset',
'#title' => $role,
);
$form['message_private_message_interval_limit'][$id][$limit_name] = array(
'#type' => 'textfield',
'#title' => t('Limit'),
'#default_value' => variable_get($limit_name),
'#description' => t('Enter a message limit ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX),
);
$form['message_private_message_interval_limit'][$id][$interval_name] = array(
'#type' => 'textfield',
'#title' => t('Interval'),
'#default_value' => variable_get($interval_name),
'#description' => t('Enter an interval in minutes ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX),
);
}

return system_settings_form($form);
}

/**
* Validate the admin settings form.
*
* @param $form
* @param $form_state
*/
function message_private_admin_settings_validate($form, &$form_state) {

foreach (user_roles() as $id => $role) {
$role_name = str_replace(' ', '_', $role);
$limit_name = 'message_private_' . $role_name . '_limit';
$interval_name = 'message_private_' . $role_name . '_interval';

// Check both textfields per fieldset are set, give error if only 1 is set.
if (isset($form_state['values'][$limit_name]) && isset($form_state['values'][$interval_name])) {
// Check is numeric and between the boundaries.
if (!ctype_digit($form_state['values'][$limit_name])
|| $form_state['values'][$limit_name] > MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX
|| $form_state['values'][$limit_name] < MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN) {
form_error($form['message_private_message_interval_limit'][$id][$limit_name],
t('Enter a numerical message limit between '
. MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX . '.'));
}
// Check is numeric and between the boundaries.
if (!ctype_digit($form_state['values'][$interval_name])
|| $form_state['values'][$interval_name] > MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX
|| $form_state['values'][$interval_name] < MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN) {
form_error($form['message_private_message_interval_limit'][$id][$interval_name],
t('Enter a numerical interval in minutes between '
. MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX . '.'));
}
} elseif (isset($form_state['values'][$limit_name]) || isset($form_state['values'][$interval_name])) {
form_error($form['message_private_message_interval_limit'][$id][$limit_name], t('Both a limit and interval value are required.'));
form_error($form['message_private_message_interval_limit'][$id][$interval_name]);
}
}
}
15 changes: 14 additions & 1 deletion message_private.install
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,20 @@ function message_private_uninstall() {
field_delete_field('field_message_subject');
field_delete_field('field_message_body');
field_delete_field('field_message_user_ref');
field_delete_field('field_private_message_notify'); // Field on user entity.
// Field on user entity.
field_delete_field('field_private_message_notify');


// Delete default variables for message limit.
variable_del('message_private_default_limit');
variable_del('message_private_default_interval');

// Cycle through the roles and delete associated message limit variables.
foreach (user_roles() as $rid => $role) {
$role_name = str_replace(' ', '_', $role);
variable_del('message_private_' . $role_name . '_limit');
variable_del('message_private_' . $role_name . '_interval');
}
}

/**
Expand Down
107 changes: 101 additions & 6 deletions message_private.module
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
* Message Private with access permissions based on message fields.
*/

/**
* The default index for settings such as role.
*/
define('MESSAGE_PRIVATE_DEFAULT_INDEX', 0);

/**
* The maximum message limit.
*/
define('MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX', 1000);

/**
* The minimum message limit.
*/
define('MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN', 1);

/**
* The maximum message interval in minutes.
*/
define('MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX', 1440);

/**
* The minimum message interval in minutes.
*/
define('MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN', 1);

/**
* Implements hook_help().
*/
Expand Down Expand Up @@ -198,6 +223,56 @@ function message_private_access_control($operation, $message, $user_obj = NULL)
return FALSE;
}

/**
* Implements hook_form_FORM_ID_alter().
*
* Hide the message_text field from the message edit form. It is only useful
* post creation. i.e. - contains user data. Also add custom validate function.
*/
function message_private_form_message_ui_instance_message_manage_alter(&$form, &$form_state, $form_id) {
if ($form['#bundle'] == 'private_message') {
if (isset($form['text']['#type'])) {
$form['text']['#type'] = 'hidden';
}
$form['#validate'][] = 'message_private_form_message_ui_instance_message_manage_validate';
}
}

/**
* Validation for Private Message form.
*
* @param mixed $form
* The message form provided by message_ui.
* @param mixed $form_state
* The form state including values submitted.
*/
function message_private_form_message_ui_instance_message_manage_validate($form, &$form_state) {
// If there is an imposed message limit, set in the admin settings interface.
if (variable_get('message_private_message_limit', FALSE)) {
global $user;
$rid = _message_private_max_message_limit_role($user->roles);
$role = user_role_load($rid);
$limit_name = 'message_private_' . $role->name . '_limit';
$interval_name = 'message_private_' . $role->name . '_interval';
$interval = variable_get($interval_name);
$limit = variable_get($limit_name);
// Get total amount of messages since last interval.
$current_timestamp = time();
$interval_timestamp = strtotime('-' . $interval . ' minutes', $current_timestamp);
$query = new EntityFieldQuery();
$total = $query->entityCondition('entity_type', 'message')
->entityCondition('bundle', 'private_message')
->propertyCondition('created', $interval_timestamp, '>')
->propertyCondition('uid', $user->uid)
->count()
->execute();
// if total < limit, send message, if not display error.
if($total >= $limit) {
form_error($form, t('Message create limit reached. Please try again later.'));
}
}
}

/**
* Implements hook_message_insert().
*
Expand Down Expand Up @@ -253,13 +328,33 @@ function message_private_form_user_profile_form_alter(&$form, &$form_state, $for
}

/**
* Implements hook_form_FORM_ID_alter().
* Get the role id with the maximum allowed message create limit.
*
* Hide the message_text field from the message edit form. It is only useful
* post creation. i.e. - contains user data.
* Using the values set for each role, calculate the the lowest time interval
* required per message: INTERVAL / LIMIT and return the role with this value.
*
* @param $roles
* @return mixed
*/
function message_private_form_message_ui_instance_message_manage_alter(&$form, &$form_state, $form_id) {
if ($form['#bundle'] == 'private_message' && isset($form['text']['#type'])) {
$form['text']['#type'] = 'hidden';
function _message_private_max_message_limit_role($roles) {
$limits = array();
// Get the default limit and interval.
$limit_name = 'message_private_default_limit';
$interval_name = 'message_private_default_interval';
$limits[MESSAGE_PRIVATE_DEFAULT_INDEX] = variable_get($interval_name) / variable_get($limit_name);

// Cycle through the roles and get each limit and interval.
foreach ($roles as $rid => $role) {
$role_name = str_replace(' ', '_', $role);
$limit_name = 'message_private_' . $role_name . '_limit';
$interval_name = 'message_private_' . $role_name . '_interval';
$interval = variable_get($interval_name);
$limit = variable_get($limit_name);

// Ensure values are greater than 0 & get interval required for one message.
if ($interval >= MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN && $limit >= MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN) {
$limits[$rid] = $interval_name / $limit;
}
}
return array_search(min($limits), $limits);
}
2 changes: 1 addition & 1 deletion message_private_og/message_private_og.install
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function message_private_og_requirements($phase) {
$t = get_t();

if ($phase == 'install' || $phase == 'runtime') {
if(function_exists('og_get_all_group_bundle')) {
if (function_exists('og_get_all_group_bundle')) {
$bundles = og_get_all_group_bundle();
if (!isset($bundles['node'])) {
$requirements['message_private_og_bundle'] = array(
Expand Down
4 changes: 1 addition & 3 deletions message_private_og/message_private_og.module
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ function message_private_og_access_control($operation, $message, $user_obj = NUL
/**
* Implements hook_form_FORM_ID_alter().
*
* Hide the message_text field from the node edit form.
* The message_text field is only useful post creation.
* i.e. - contains user data. Also, set the custom validator.
* Add a custom validation function.
*/
function message_private_og_form_message_ui_instance_message_manage_alter(&$form, &$form_state, $form_id) {
/*
Expand Down

0 comments on commit d7fca74

Please sign in to comment.