membership   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 39
dl 0
loc 122
rs 10
c 3
b 1
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_users_for_condition() 0 42 2
A sql_where_clause() 0 24 5
A get_condition_field() 0 3 1
A get_condition_type() 0 3 1
A get_condition_type_name() 0 3 1
1
<?php
2
/**
3
*
4
* Auto Groups extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace phpbb\autogroups\conditions\type;
12
13
/**
14
 * Auto Groups Membership class
15
 */
16
class membership extends \phpbb\autogroups\conditions\type\base
17
{
18
	/**
19
	 * Get condition type
20
	 *
21
	 * @return string Condition type
22
	 * @access public
23
	 */
24
	public function get_condition_type()
25
	{
26
		return 'phpbb.autogroups.type.membership';
27
	}
28
29
	/**
30
	 * Get condition field (this is the field to check)
31
	 *
32
	 * @return string Condition field name
33
	 * @access public
34
	 */
35
	public function get_condition_field()
36
	{
37
		return 'user_regdate';
38
	}
39
40
	/**
41
	 * Get condition type name
42
	 *
43
	 * @return string Condition type name
44
	 * @access public
45
	 */
46
	public function get_condition_type_name()
47
	{
48
		return $this->language->lang('AUTOGROUPS_TYPE_MEMBERSHIP');
49
	}
50
51
	/**
52
	 * Get users to apply to this condition
53
	 * Memberships is typically called via cron with no $options arguments.
54
	 * By default, get users that have between the min/max values assigned
55
	 * to this type, and users currently in groups assigned to this type.
56
	 *
57
	 * @param array $options Array of optional data
58
	 * @return array Array of users ids as keys and their condition data as values
59
	 * @throws \Exception
60
	 * @access public
61
	 */
62
	public function get_users_for_condition($options = array())
63
	{
64
		// The user data this condition needs to check
65
		$condition_data = array(
66
			$this->get_condition_field(),
67
		);
68
69
		// Merge default options, empty user array as the default
70
		$options = array_merge(array(
71
			'users'		=> array(),
72
		), $options);
73
74
		$sql_array = array(
75
			'SELECT' => 'u.user_id, u.' . implode(', u.', $condition_data),
76
			'FROM' => array(
77
				USERS_TABLE => 'u',
0 ignored issues
show
Bug introduced by
The constant phpbb\autogroups\conditions\type\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
			),
79
			'LEFT_JOIN' => array(
80
				array(
81
					'FROM' => array(USER_GROUP_TABLE => 'ug'),
0 ignored issues
show
Bug introduced by
The constant phpbb\autogroups\conditions\type\USER_GROUP_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
82
					'ON' => 'u.user_id = ug.user_id',
83
				),
84
			),
85
			'WHERE' => $this->sql_where_clause($options) . '
86
				AND ' . $this->db->sql_in_set('u.user_type', $this->ignore_user_types(), true),
87
			'GROUP_BY' => 'u.user_id, u.' . implode(', u.', $condition_data),
88
		);
89
90
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $sql_array);
91
		$result = $this->db->sql_query($sql);
92
93
		$user_data = array();
94
		while ($row = $this->db->sql_fetchrow($result))
95
		{
96
			// Convert from timestamp to number of days
97
			$row[$this->get_condition_field()] = $this->timestamp_to_days($row[$this->get_condition_field()]);
98
99
			$user_data[$row['user_id']] = $row;
100
		}
101
		$this->db->sql_freeresult($result);
102
103
		return $user_data;
104
	}
105
106
	/**
107
	 * Helper to generate the needed sql where clause. If user ids were
108
	 * supplied, use them. Otherwise find all qualified users to check.
109
	 *
110
	 * @param array $options Array of optional data
111
	 * @return string SQL where clause
112
	 * @access protected
113
	 */
114
	protected function sql_where_clause($options)
115
	{
116
		// If we have user id data, return a sql_in_set of user_ids
117
		if (!empty($options['users']))
118
		{
119
			return $this->db->sql_in_set('u.user_id', $this->helper->prepare_users_for_query($options['users']));
120
		}
121
122
		$sql_where = $group_ids = array();
123
124
		// Get auto group rule data for this type
125
		$group_rules = $this->get_group_rules($this->get_condition_type());
126
		foreach ($group_rules as $group_rule)
127
		{
128
			$min = $this->days_to_timestamp($group_rule['autogroups_min_value']);
129
			$max = $this->days_to_timestamp($group_rule['autogroups_max_value']);
130
131
			$max = ($min >= $max) ? $max : 1; // For cases where no max_value was set (no end limit).
132
133
			$sql_where[] = "(u.{$this->get_condition_field()} BETWEEN $max AND $min)";
134
			$group_ids[] = $group_rule['autogroups_group_id'];
135
		}
136
137
		return '(' . (count($sql_where) ? implode(' OR ', $sql_where) . ' OR ' : '') . $this->db->sql_in_set('ug.group_id', $group_ids, false, true) . ')';
138
	}
139
}
140