From a079403d2b57aac892b886bc22a80cd7a63dd81d Mon Sep 17 00:00:00 2001 From: Samuel Solis Date: Sat, 17 Jan 2015 11:50:42 +0000 Subject: [PATCH] Issue #1109992 by jefgodesky, estoyausente: Check when a user has a certain number of points --- userpoints_rules.rules.inc | 70 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/userpoints_rules.rules.inc b/userpoints_rules.rules.inc index 142045c..bcd3943 100644 --- a/userpoints_rules.rules.inc +++ b/userpoints_rules.rules.inc @@ -10,6 +10,34 @@ */ function userpoints_rules_rules_action_info() { return array( + 'userpoints_condition_check_points' => array( + 'label' => t('Check how many !points a user has', userpoints_translation()), + 'named parameter' => TRUE, + 'parameter' => array( + 'user' => array( + 'type' => 'user', + 'label' => t('User'), + 'description' => t('The user whose !points to check', userpoints_translation()), + ), + 'tid' => array( + 'label' => t('!Points category', userpoints_translation()), + 'type' => 'integer', + 'options list' => 'userpoints_rules_get_categories', + ), + 'threshold' => array( + 'type' => 'integer', + 'label' => t('Threshold'), + 'description' => t('Compare the number of !points the user has to this number', userpoints_translation()), + ), + 'comparison' => array( + 'type' => 'text', + 'label' => t('Comparison'), + 'description' => t('How to compare the user\'s !points with the threshold', userpoints_translation()), + 'options list' => 'userpoints_condition_get_comparisons', + ), + ), + 'group' => t('!Points', userpoints_translation()), + ), 'userpoints_action_grant_points' => array( 'label' => t('Grant !points to a user', userpoints_translation()), 'named parameter' => TRUE, @@ -101,6 +129,44 @@ function userpoints_rules_rules_action_info() { } /** + * Rules condition - compare the user's points to a threshold + */ +function userpoints_condition_check_points($params) { + $points = userpoints_get_current_points($params['user']->uid, $params['tid']); + $threshold = $params['threshold']; + switch ($params['comparison']) { + case 'lt': + if ($threshold > $points) { return true; } else { return false; } + break; + case 'le': + if ($threshold >= $points) { return true; } else { return false; } + break; + case 'eq': + if ($threshold == $points) { return true; } else { return false; } + break; + case 'ge': + if ($threshold <= $points) { return true; } else { return false; } + break; + case 'gt': + if ($threshold < $points) { return true; } else { return false; } + break; + } + return false; +} + +/** + * Returns options for ways to compare a number to a number of points. + */ +function userpoints_condition_get_comparisons() { + return array( + 'lt' => t('Less than'), + 'le' => t('Less than or equal to'), + 'eq' => t('Equal to'), + 'ge' => t('Greater than or equal to'), + 'gt' => t('Greater than'), + ); +} +/** * Wrapper function for userpoints_get_categories(). * * Rules.module uses different arguments for option list callbacks than @@ -114,7 +180,7 @@ function userpoints_rules_get_categories() { * Simple callback that lists the categories including an option for all. */ function userpoints_rules_get_all_categories() { - return array('all' => t('All categories')) + userpoints_get_categories(); + return array('all' => t('All categories')) userpoints_get_categories(); } /** @@ -193,7 +259,7 @@ function userpoints_action_grant_points($params) { // Rules defaults to FALSE if the date format can not be parsed. // Use NULL instead since FALSE means no expiration. 'expirydate' => $params['expirydate'] ? $params['expirydate'] : NULL, - ) + $params; + ) $params; unset($params['state']); unset($params['user']); -- 1.7.9.5