| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provide better integration into the rules module
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_rules_action_info().
|
| 11 |
*/
|
| 12 |
function userpoints_rules_action_info() {
|
| 13 |
return array(
|
| 14 |
'userpoints_action_grant_points' => array(
|
| 15 |
'label' => t('Grant !points to a user', userpoints_translation()),
|
| 16 |
'arguments' => array(
|
| 17 |
'user' => array('type' => 'user', 'label' => t('User'))
|
| 18 |
),
|
| 19 |
'module' => 'Userpoints',
|
| 20 |
'eval input' => array('points'),
|
| 21 |
),
|
| 22 |
);
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Implementation of hook_rules_event_info().
|
| 27 |
*/
|
| 28 |
function userpoints_rules_event_info() {
|
| 29 |
return array(
|
| 30 |
'userpoints_event_points_awarded' => array(
|
| 31 |
'label' => t('User was awarded !points', userpoints_translation()),
|
| 32 |
'arguments' => array(
|
| 33 |
'user' => array('type' => 'user', 'label' => t('updated user')),
|
| 34 |
'points' => array('type' => 'number', 'label' => t('Points awarded'))
|
| 35 |
),
|
| 36 |
'module' => 'Userpoints',
|
| 37 |
),
|
| 38 |
);
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Rules action - grant points to a user.
|
| 43 |
*/
|
| 44 |
function userpoints_action_grant_points($account_uid, $settings) {
|
| 45 |
userpoints_userpointsapi(array('uid' => $account_uid, 'points' => $settings['points']));
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Rules action form configuration - allow number of points to be set.
|
| 50 |
*/
|
| 51 |
function userpoints_action_grant_points_form($settings = array(), &$form) {
|
| 52 |
$form['settings']['points'] = array(
|
| 53 |
'#type' => 'textfield',
|
| 54 |
'#title' => t('Number of points'),
|
| 55 |
'#default_value' => isset($settings['points']) ? $settings['points'] : '',
|
| 56 |
'#description' => t('The number of !points to award. You may enter a negative number as-well', userpoints_translation())
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Rules Conditions - compare userpoints with a defined amount
|
| 63 |
*/
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Implementation of hook_rules_condition_info().
|
| 67 |
*/
|
| 68 |
function userpoints_rules_rules_condition_info() {
|
| 69 |
return array(
|
| 70 |
'userpoints_rules_amount' => array(
|
| 71 |
'label' => t('Userpoints amount is >= than x'),
|
| 72 |
'arguments' => array(
|
| 73 |
'user' => array('type' => 'user', 'label' => t('User'))
|
| 74 |
),
|
| 75 |
'module' => 'Userpoints',
|
| 76 |
'eval input' => array('amount'),
|
| 77 |
),
|
| 78 |
);
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Rules Condition form configuration - set the amount to compare
|
| 83 |
*/
|
| 84 |
function userpoints_rules_amount_form($settings = array(), &$form) {
|
| 85 |
$form['settings']['amount'] = array(
|
| 86 |
'#type' => 'textfield',
|
| 87 |
'#title' => t('amount to compare'),
|
| 88 |
'#default_value' => isset($settings['amount']) ? $settings['amount'] : '',
|
| 89 |
'#description' => t('The amount to compare if userpoints are >=. Example:30, will trigger the condition if the user userpoints are >= than 30 points.')
|
| 90 |
);
|
| 91 |
$form['settings']['type'] = array(
|
| 92 |
'#type' => 'select',
|
| 93 |
'#title' => t('Userpoints Categories to analyze'),
|
| 94 |
'#options' => userpoints_get_categories(),
|
| 95 |
'#default_value' => isset($settings['type']) ? $settings['type'] : '',
|
| 96 |
'#description' => t('Userpoints Categories to analyze'),
|
| 97 |
'#required' => TRUE,
|
| 98 |
);
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Rules Condition - Userpoints amount is >= than
|
| 103 |
*/
|
| 104 |
function userpoints_rules_amount($account_uid, $settings) {
|
| 105 |
$balance = userpoints_get_current_points($account_uid, $settings['type']);
|
| 106 |
return ($balance >= $settings['amount']);
|
| 107 |
}
|