summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShabanaBlackborder2013-09-20 06:30:06 (GMT)
committer ShabanaBlackborder2013-09-20 06:30:06 (GMT)
commitd8bf68e47c04e6de91e8367ca15f0349dc5103a2 (patch)
treed2ce833d8f2b8767ba7291baa280a5df9d2f5854
parentf83fddbb8d1acb1ca50bbbe59395f5c313b4acfd (diff)
Issue #2035003 by Shabana Blackborder: Added Rules integration for 7.x-3.x-dev.
-rw-r--r--user_badges.admin.inc169
-rw-r--r--user_badges.module125
-rw-r--r--user_badges.rules.inc208
3 files changed, 431 insertions, 71 deletions
diff --git a/user_badges.admin.inc b/user_badges.admin.inc
index 160f482..438ab68 100644
--- a/user_badges.admin.inc
+++ b/user_badges.admin.inc
@@ -644,8 +644,8 @@ function user_badges_images_form_submit($form, &$form_state) {
function user_badges_roles_form($form, &$form_state) {
// No badges for the anonymous role.
$roles = user_roles(TRUE);
- $badges = user_badges_get_roles(NULL, array('returnbadges' => TRUE));
-
+ $role_badges = user_badges_get_roles(NULL, array('returnbadges' => TRUE));
+
$form['blocked'] = array(
'#type' => 'fieldset',
'#title' => t('Blocked Badge'),
@@ -653,16 +653,7 @@ function user_badges_roles_form($form, &$form_state) {
'#collapsed' => FALSE,
'#tree' => TRUE,
);
- $form['blocked'][0] = array(
- '#type' => 'textfield',
- '#title' => t('blocked user'),
- '#size' => 40,
- '#maxlength' => 255,
- '#autocomplete_path' => 'user_badges/autocomplete',
- '#default_value' => isset($badges[0]) ? $badges[0]->name . ' ' . t('(Badge ID') . ' ' . $badges[0]->bid . ')' : '',
- '#field_suffix' => isset($badges[0]) ? ' ' . t('Current:') . ' ' . $badges[0]->image : '',
- );
-
+
$form['roles'] = array(
'#type' => 'fieldset',
'#title' => t('Role Badges'),
@@ -670,18 +661,66 @@ function user_badges_roles_form($form, &$form_state) {
'#collapsed' => FALSE,
'#tree' => TRUE,
);
-
- foreach ($roles as $rid => $role) {
- $form['roles'][$rid] = array(
+
+ //Determine which selector type the user wants to use to set badges from their settings
+ //If the user wants to use the drop-down selector, display that if there are records in the db
+ if (variable_get('user_badges_selector_type', 1) == 0) {
+
+ //First, load all the available badges from the database by calling the following helper function
+ $result = user_badges_load_badges();
+
+ //Display the drop-down only if we get any records
+ if ($result->rowCount()) {
+ foreach ($result as $badge) {
+ $options[$badge->bid] = t('Badge') . ' ' . $badge->bid . ' ' . '-' . ' ' . $badge->name;
+ }
+
+ $form['blocked'][0] = array(
+ '#type' => 'select',
+ '#title' => t('blocked user'),
+ '#empty_option' => '- None -',
+ '#options' => $options,
+ '#default_value' => isset($role_badges[0]) ? $role_badges[0]->bid : '',
+ '#field_suffix' => isset($role_badges[0]) ? ' ' . t('Current:') . ' ' . $role_badges[0]->image : '',
+ );
+
+ foreach ($roles as $rid => $role) {
+ $form['roles'][$rid] = array(
+ '#type' => 'select',
+ '#title' => check_plain($role),
+ '#empty_option' => '- None -',
+ '#options' => $options,
+ '#default_value' => isset($role_badges[$rid]) ? $role_badges[$rid]->bid : '',
+ '#field_suffix' => isset($role_badges[$rid]) ?
+ ' ' . t('Current:') . ' ' . _user_badges_build_image($role_badges[$rid]) : '',
+ );
+ }
+ }
+ }
+ //Else, if the user wants to use the autocomplete or if there are no records in the db, display that
+ else {
+ $form['blocked'][0] = array(
'#type' => 'textfield',
- '#title' => check_plain($role),
+ '#title' => t('blocked user'),
+ '#size' => 40,
'#maxlength' => 255,
'#autocomplete_path' => 'user_badges/autocomplete',
- '#default_value' => isset($badges[$rid]) ? $badges[$rid]->name . ' '
- . t('(Badge ID') . ' ' . $badges[$rid]->bid . ')' : '',
- '#field_suffix' => isset($badges[$rid]) ?
- ' ' . t('Current:') . ' ' . _user_badges_build_image($badges[$rid]) : '',
+ '#default_value' => isset($role_badges[0]) ? $role_badges[0]->name . ' ' . t('(Badge ID') . ' ' . $role_badges[0]->bid . ')' : '',
+ '#field_suffix' => isset($role_badges[0]) ? ' ' . t('Current:') . ' ' . $role_badges[0]->image : '',
);
+
+ foreach ($roles as $rid => $role) {
+ $form['roles'][$rid] = array(
+ '#type' => 'textfield',
+ '#title' => check_plain($role),
+ '#maxlength' => 255,
+ '#autocomplete_path' => 'user_badges/autocomplete',
+ '#default_value' => isset($role_badges[$rid]) ? $role_badges[$rid]->name . ' '
+ . t('(Badge ID') . ' ' . $role_badges[$rid]->bid . ')' : '',
+ '#field_suffix' => isset($role_badges[$rid]) ?
+ ' ' . t('Current:') . ' ' . _user_badges_build_image($role_badges[$rid]) : '',
+ );
+ }
}
$form[] = array(
@@ -695,28 +734,32 @@ function user_badges_roles_form($form, &$form_state) {
* Validation function for user_badges_roles_form.
*/
function user_badges_roles_form_validate($form, &$form_state) {
- $array = $form_state['values']['roles'] + $form_state['values']['blocked'];
- // Go through all the entries and make sure they all have a valid badge ID.
- foreach ($array as $field => $value) {
- if (!empty($value)) {
- // The field isn't empty, so we should validate it.
- $validation = user_badges_badge_autocomplete_validation($value);
- //Is it correctly formatted?
- if ($validation[1] == 'string') {
- if ($field == 0) {
- form_set_error('blocked][' . $field, t('"@value" is not a valid badge name. Try using the autocomplete function (requires javascript).', array('@value' => $value)));
- }
- else {
- form_set_error('roles][' . $field, t('"@value" is not a valid badge name. Try using the autocomplete function (requires javascript).', array('@value' => $value)));
- }
- }
- // The format was correct, but we need to check the bid exists.
- elseif ($validation[1] == 'nobid') {
- if ($field == 0) {
- form_set_error('blocked][' . $field, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array('@value' => $validation[0])));
+ //If the selector type used to add badges was the autocomplete, then, validate the text in the autocomplete boxes
+ if (variable_get('user_badges_selector_type', 1) == 1) {
+ $array = $form_state['values']['roles'] + $form_state['values']['blocked'];
+
+ // Go through all the entries and make sure they all have a valid badge ID.
+ foreach ($array as $field => $value) {
+ if (!empty($value)) {
+ // The field isn't empty, so we should validate it.
+ $validation = user_badges_badge_autocomplete_validation($value);
+ //Is it correctly formatted?
+ if ($validation[1] == 'string') {
+ if ($field == 0) {
+ form_set_error('blocked][' . $field, t('"@value" is not a valid badge name. Try using the autocomplete function (requires javascript).', array('@value' => $value)));
+ }
+ else {
+ form_set_error('roles][' . $field, t('"@value" is not a valid badge name. Try using the autocomplete function (requires javascript).', array('@value' => $value)));
+ }
}
- else {
- form_set_error('roles][' . $field, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array('@value' => $validation[0])));
+ // The format was correct, but we need to check the bid exists.
+ elseif ($validation[1] == 'nobid') {
+ if ($field == 0) {
+ form_set_error('blocked][' . $field, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array('@value' => $validation[0])));
+ }
+ else {
+ form_set_error('roles][' . $field, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array('@value' => $validation[0])));
+ }
}
}
}
@@ -728,12 +771,16 @@ function user_badges_roles_form_validate($form, &$form_state) {
*/
function user_badges_roles_form_submit($form, &$form_state) {
$array = $form_state['values']['roles'] + $form_state['values']['blocked'];
- foreach ($array as $field => &$value) {
- // We now have a string as our badge, so just extract the bid.
- preg_match('/\(' . 'Badge ID' . ' (\d+)\)/', $value, $matches);
- // Transform our value into just the bid.
- if (!empty($matches)) {
- $value = $matches[1];
+
+ //If the selectory type used to enter badges was the autocomplete, then, extract the bid from the text
+ if (variable_get('user_badges_selector_type', 1) == 1) {
+ foreach ($array as $field => &$value) {
+ // We now have a string as our badge, so just extract the bid.
+ preg_match('/\(' . t('Badge ID') . ' (\d+)\)/', $value, $matches);
+ // Transform our value into just the bid.
+ if (!empty($matches)) {
+ $value = $matches[1];
+ }
}
}
user_badges_save_roles($array);
@@ -785,6 +832,32 @@ function user_badges_settings_form($form, &$form_state) {
'#description' => t('If selected, the user badges list will include all of the users who have been assigned the badge.'),
'#attributes' => array('class' => array('container-inline')),
);
+
+ $form['selector_type'] = array(
+ '#type' => 'select',
+ '#options' => array(
+ 0 => t('drop-down multi-select'),
+ 1 => t('autocomplete'),
+ ),
+ '#title' => t('Selector to set a badge'),
+ '#default_value' => variable_get('user_badges_selector_type', 1),
+ '#description' => t('Select which type of selector to use to set a badge.'),
+ '#attributes' => array('class' => array('container-inline')),
+ );
+
+ $form['enable_badge_images'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Enable badge images in drop-down multi-select'),
+ '#default_value' => 0,
+ '#description' => t('If checked, images for all the badges will appear next to their badge name in the drop-down select.
+ This might lead to performance issues if the site contains a lot of badges. Consequenlty, please exercise caution when enabling this.'),
+ '#attributes' => array('class' => array('container-inline')),
+ '#states' => array(
+ 'visible' => array(
+ ':input[name="selector_type"]' => array('value' => '0'),
+ ),
+ ),
+ );
$form['imagecache'] = array(
'#type' => 'select',
@@ -882,6 +955,8 @@ function user_badges_settings_form_submit($form, $form_state) {
variable_set('user_badges_showblocked', $form_state['values']['showblocked']);
variable_set('user_badges_userweight', $form_state['values']['userweight']);
variable_set('user_badges_list_show_users', $form_state['values']['show_users']);
+ variable_set('user_badges_selector_type', $form_state['values']['selector_type']);
+ variable_set('user_badges_enable_badge_images', $form_state['values']['enable_badge_images']);
variable_set('user_badges_imagecache', $form_state['values']['imagecache']);
variable_set('user_badges_nobadges', $form_state['values']['nobadges']);
diff --git a/user_badges.module b/user_badges.module
index 30a6b81..a79eeec 100644
--- a/user_badges.module
+++ b/user_badges.module
@@ -643,15 +643,47 @@ function user_badges_change_form($form, &$form_state, $account) {
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
-
- for ($i = 1; $i <= 5; $i++) {
- $form['add']['add' . $i] = array(
- '#type' => 'textfield',
- '#title' => t('New Badge @number', array('@number' => $i)),
- '#size' => 40,
- '#maxlength' => 255,
- '#autocomplete_path' => 'user_badges/autocomplete',
- );
+
+ //Determine which selector type the user wants to use to set badges from their settings
+ //If the user wants to use the drop-down selector, display that if there are records in the db
+ if (variable_get('user_badges_selector_type', 1) == 0) {
+
+ //First, load all the available badges from the database by calling the following helper function
+ $result = user_badges_load_badges();
+
+ //Display the drop-down only if we get any records
+ if ($result->rowCount()) {
+ foreach ($result as $badge) {
+ $options[$badge->bid] = t('Badge') . ' ' . $badge->bid . ' ' . '-' . ' ' . $badge->name;
+ }
+
+ //Initialize the $options array, which will contain all the badges fetched from the db
+ //Add a - None - option to the badge list as Drupal core won't give it to us automatically
+ //because it is a multi select list!!
+ $options[-1] = '- None -';
+
+ //Add the drop-down multi select box to add badges
+ $form['add']['add_drop_down'] = array(
+ '#type' => 'select',
+ '#title' => t('New Badges'),
+ '#options' => $options,
+ '#multiple' => TRUE,
+ );
+ }
+ }
+ //Else, if the user wants to use the autocomplete box to set badges or if there are no records in the db,
+ //then, display, 5 autocomplete boxes for them to select from
+ else {
+ for ($i = 1; $i <= 5; $i++) {
+ //Add the autocomplete boxes to add badges
+ $form['add']['add_autocomplete' . $i] = array(
+ '#type' => 'textfield',
+ '#title' => t('New Badge @number', array('@number' => $i)),
+ '#size' => 40,
+ '#maxlength' => 255,
+ '#autocomplete_path' => 'user_badges/autocomplete',
+ );
+ }
}
if (isset($account->badges_all) && count($account->badges_all)) {
@@ -679,23 +711,44 @@ function user_badges_change_form($form, &$form_state, $account) {
'#value' => t('Update Badges'),
'#weight' => 10,
);
+
return $form;
}
/**
+ * Helper function to load all the badges from the database
+ *
+ * @return
+ * An array of badge objects
+ */
+function user_badges_load_badges() {
+ //Load all the available badges from the database
+ $result = db_query("
+ SELECT b.bid, b.weight, b.name, b.image
+ FROM {user_badges_badges} b
+ ORDER BY b.weight, b.name"
+ );
+
+ return $result;
+}
+
+/**
* Validate user_badges_remove_form form submissions.
*/
function user_badges_change_form_validate($form, &$form_state) {
- for ($i = 1; $i <= 5; $i++) {
- if (!empty($form_state['values']['add' . $i])) {
- $validation = user_badges_badge_autocomplete_validation($form_state['values']['add' . $i]);
- switch ($validation[1]) {
- case 'nobid':
- form_set_error('add' . $i, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array('@value' => $validation[0])));
- break;
- case 'string':
- form_set_error('add' . $i, t('"@value" is not a valid badge. Try using the autocomplete function (requires javascript).', array('@value' => $form_state['values']['add' . $i])));
- break;
+ //If the user used the autocomplete to select badges, validate the text in them
+ if (variable_get('user_badges_selector_type', 1) == 1) {
+ for ($i = 1; $i <= 5; $i++) {
+ if (!empty($form_state['values']['add_autocomplete' . $i])) {
+ $validation = user_badges_badge_autocomplete_validation($form_state['values']['add_autocomplete' . $i]);
+ switch ($validation[1]) {
+ case 'nobid':
+ form_set_error('add_autocomplete' . $i, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array('@value' => $validation[0])));
+ break;
+ case 'string':
+ form_set_error('add_autocomplete' . $i, t('"@value" is not a valid badge. Try using the autocomplete function (requires javascript).', array('@value' => $form_state['values']['add_autocomplete' . $i])));
+ break;
+ }
}
}
}
@@ -709,11 +762,23 @@ function user_badges_change_form_validate($form, &$form_state) {
function user_badges_change_form_submit($form, &$form_state) {
$uid = $form_state['values']['uid'];
- //Add badges for non-empty fields
- for ($i = 1; $i <= 5; $i++) {
- if (!empty($form_state['values']['add' . $i])) {
- $validation = user_badges_badge_autocomplete_validation($form_state['values']['add' . $i]);
- user_badges_user_add_badge($uid, $validation[0], 'user');
+ //If the user used the multi drop-down to select badges, submit them accordingly
+ if (variable_get('user_badges_selector_type', 1) == 0) {
+ foreach ($form_state['values']['add_drop_down'] as $bid) {
+ //We have to add this validation as we don't want to add the - None - case which might have been selected
+ if ($bid > 0) {
+ user_badges_user_add_badge($uid, $bid, 'user');
+ }
+ }
+ }
+ //Else, if the user used the autocomplete to select badges, submit them accordingly
+ else {
+ //Add badges for non-empty fields
+ for ($i = 1; $i <= 5; $i++) {
+ if (!empty($form_state['values']['add_autocomplete' . $i])) {
+ $validation = user_badges_badge_autocomplete_validation($form_state['values']['add_autocomplete' . $i]);
+ user_badges_user_add_badge($uid, $validation[0], 'user');
+ }
}
}
@@ -797,6 +862,12 @@ function user_badges_user_save($edit, $uid, $quiet = TRUE) {
* @return bool with query success
*/
function user_badges_user_add_badge($uid, $bid, $type = NULL) {
+ // Integrate rules events.
+ if (module_exists('rules')) {
+ $variables = array('user' => $uid, 'badge_id' => $bid);
+ rules_invoke_event_by_args('user_badges_badge_given', $variables);
+ }
+
user_badges_user_remove_badge($uid, $bid, $type);
return $id = db_insert('user_badges_user')
->fields(array(
@@ -818,6 +889,12 @@ function user_badges_user_add_badge($uid, $bid, $type = NULL) {
* @return bool with query success
*/
function user_badges_user_remove_badge($uid, $bid, $type = NULL) {
+ // Integrate rules events.
+ if (module_exists('rules')) {
+ $variables = array('user' => $uid, 'badge_id' => $bid);
+ rules_invoke_event_by_args('user_badges_badge_removed', $variables);
+ }
+
if (is_null($type)) {
return db_delete('user_badges_user')
->condition('uid', $uid)
diff --git a/user_badges.rules.inc b/user_badges.rules.inc
new file mode 100644
index 0000000..b3f16d6
--- /dev/null
+++ b/user_badges.rules.inc
@@ -0,0 +1,208 @@
+<?php
+
+/**
+* Implements hook_rules_action_info() on behalf of the user_badges module.
+*
+*/
+function user_badges_rules_action_info() {
+ $items = array(
+ 'add_badge_by_name' => array(
+ 'label' => t('Add badge by name'),
+ 'base' => 'user_badges_rules_action_add_badge_by_name',
+ 'parameter' => array(
+ 'badge' => array(
+ 'type' => 'text',
+ 'label' => t('Badge name'),
+ 'options list' => 'user_badges_rules_get_badge_names',
+ 'restriction' => 'input',
+ ),
+ 'user' => array(
+ 'type' => 'user',
+ 'label' => t('User on whose behalf to add the badge'),
+ ),
+ ),
+ 'group' => t('User'),
+ 'access_callback' => 'user_badges_rules_integration_access',
+ ),
+ 'remove_badge_by_name' => array(
+ 'label' => t('Remove badge by name'),
+ 'base' => 'user_badges_rules_action_remove_badge_by_name',
+ 'parameter' => array(
+ 'badge' => array(
+ 'type' => 'text',
+ 'label' => t('Badge name'),
+ 'options list' => 'user_badges_rules_get_badge_names',
+ 'restriction' => 'input',
+ ),
+ 'user' => array(
+ 'type' => 'user',
+ 'label' => t('User on whose behalf to add the badge'),
+ ),
+ ),
+ 'group' => t('User'),
+ 'access_callback' => 'user_badges_rules_integration_access',
+ ),
+ );
+
+ return $items;
+}
+
+/**
+ * Implements hook_rules_event_info().
+ */
+function user_badges_rules_event_info() {
+ $variables = array(
+ 'user' => array(
+ 'type' => 'user',
+ 'label' => t('User before receiving badge'),
+ ),
+ 'badge_id' => array(
+ 'type' => 'text',
+ 'label' => t('The given Badge\'s ID'),
+ ),
+ );
+
+ $items = array(
+ 'user_badges_badge_given' => array(
+ 'group' => t('User'),
+ 'label' => t('User was given a badge'),
+ 'variables' => $variables,
+ 'access callback' => 'user_badges_rules_integration_access'
+ ),
+ 'user_badges_badge_removed' => array(
+ 'group' => t('User'),
+ 'label' => t('User was removed a badge'),
+ 'variables' => $variables,
+ 'access callback' => 'user_badges_rules_integration_access'
+ ),
+ );
+ return $items;
+}
+
+/**
+* Implementation of hook_rules_condition_info().
+*/
+function user_badges_rules_condition_info() {
+ $items = array(
+ 'user_badges_has_badge' => array(
+ 'label' => t('User has badge'),
+ 'base' => 'user_badges_rules_condition_has_badge',
+ 'parameter' => array(
+ 'badge_id' => array(
+ 'type' => 'text',
+ 'label' => t('Badge'),
+ 'options list' => 'user_badges_rules_get_badge_names',
+ 'restriction' => 'input',
+ ),
+ 'user' => array(
+ 'type' => 'user',
+ 'label' => t('User'),
+ ),
+ ),
+ 'group' => t('User'),
+ ),
+ 'user_badges_badge_count' => array(
+ 'label' => t('User badge count'),
+ 'base' => 'user_badges_rules_condition_badge_count',
+ 'parameter' => array(
+ 'user' => array(
+ 'type' => 'user',
+ 'label' => t('User')
+ ),
+ 'operator' => array(
+ 'type' => 'text',
+ 'label' => t('User\'s number of badges is: '),
+ 'default value' => '=',
+ 'options list' => 'user_badges_rules_condition_operator_options',
+ 'restriction' => 'input',
+ ),
+ 'number' => array(
+ 'type' => 'number',
+ 'label' => t('Number')
+ ),
+ ),
+ 'group' => t('User')
+ ),
+ );
+ return $items;
+}
+
+/**
+ *Function to load badges for options list
+ *
+ *@return
+ * An array of badge names
+ */
+function user_badges_rules_get_badge_names() {
+ //Load the badges by calling this function
+ $result = user_badges_load_badges();
+ if ($result->rowCount()) {
+ $badges = array();
+ foreach ($result as $badge) {
+ $badges[$badge->bid] = $badge->name . ' ' . '(Badge ID:' . ' ' . $badge->bid . ')';
+ }
+ }
+
+ return $badges;
+}
+
+/**
+* Base action implementation: user_badges.
+*/
+function user_badges_rules_action_add_badge_by_name($badge, $user) {
+ user_badges_user_add_badge($user->uid, $badge, 'user');
+}
+
+/**
+* Base action implementation: user_badges.
+*/
+function user_badges_rules_action_remove_badge_by_name($badge, $user) {
+ user_badges_user_remove_badge($user->uid, $badge, 'user');
+}
+
+/**
+ * Condition: User has badge.
+ */
+function user_badges_rules_condition_has_badge($badge_id, $user) {
+ $has_badge = FALSE;
+ $badges = user_badges_get_badges($user->uid);
+ if (isset($badges[$badge_id])) {
+ $has_badge = TRUE;
+ }
+ return $has_badge;
+}
+
+/**
+ * Options list callback for the operator parameter of the user badges badge count condition.
+ */
+function user_badges_rules_condition_operator_options() {
+ return array(
+ '>' => t('Greater than'),
+ '>=' => t('Greater than or equal'),
+ '=' => t('Equal to'),
+ '<=' => t('Less than or equal'),
+ '<' => t('Less than'),
+ );
+}
+
+/**
+ * Condition: User badge count.
+ */
+function user_badges_rules_condition_badge_count($user, $operator, $number) {
+ $badges = user_badges_get_badges($user->uid);
+ $badges_count = count($badges);
+ switch ($operator) {
+ case '>' : return $badges_count > $number;
+ case '>=': return $badges_count >= $number;
+ case '=' : return $badges_count == $number;
+ case '<' : return $badges_count < $number;
+ case '<=': return $badges_count <= $number;
+ }
+}
+
+/**
+* Rules integration access callback.
+*/
+function user_badges_rules_integration_access($type, $name) {
+ return user_access('manage badges');
+} \ No newline at end of file