warnings   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 129
rs 10
c 6
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
B sql_where_clause() 0 33 7
A get_condition_field() 0 3 1
A get_users_for_condition() 0 39 2
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 Warnings class
15
 */
16
class warnings 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.warnings';
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_warnings';
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_WARNINGS');
49
	}
50
51
	/**
52
	 * Get users to apply to this condition
53
	 * Warnings is called by cron or by events when warnings are issued
54
	 * to user(s). By default, get users that have between the min/max
55
	 * values assigned to this type and any users currently in groups
56
	 * assigned to this type, otherwise use the user_id(s) supplied in
57
	 * the $options arg.
58
	 *
59
	 * @param array $options Array of optional data
60
	 * @return array Array of users ids as keys and their condition data as values
61
	 * @access public
62
	 */
63
	public function get_users_for_condition($options = array())
64
	{
65
		// The user data this condition needs to check
66
		$condition_data = array(
67
			$this->get_condition_field(),
68
		);
69
70
		// Merge default options, empty user array as the default
71
		$options = array_merge(array(
72
			'users'		=> array(),
73
		), $options);
74
75
		$sql_array = array(
76
			'SELECT' => 'u.user_id, u.' . implode(', u.', $condition_data),
77
			'FROM' => array(
78
				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...
79
			),
80
			'LEFT_JOIN' => array(
81
				array(
82
					'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...
83
					'ON' => 'u.user_id = ug.user_id',
84
				),
85
			),
86
			'WHERE' => $this->sql_where_clause($options) . '
87
				AND ' . $this->db->sql_in_set('u.user_type', $this->ignore_user_types(), true),
88
			'GROUP_BY' => 'u.user_id, u.' . implode(', u.', $condition_data),
89
		);
90
91
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $sql_array);
92
		$result = $this->db->sql_query($sql);
93
94
		$user_data = array();
95
		while ($row = $this->db->sql_fetchrow($result))
96
		{
97
			$user_data[$row['user_id']] = $row;
98
		}
99
		$this->db->sql_freeresult($result);
100
101
		return $user_data;
102
	}
103
104
	/**
105
	 * Helper to generate the needed sql where clause. If user ids were
106
	 * supplied, use them. Otherwise find all qualified users to check.
107
	 *
108
	 * @param array $options Array of optional data
109
	 * @return string SQL where clause
110
	 * @access protected
111
	 */
112
	protected function sql_where_clause($options)
113
	{
114
		// If we have user id data, return a sql_in_set of user_ids
115
		if (!empty($options['users']))
116
		{
117
			return $this->db->sql_in_set('u.user_id', $this->helper->prepare_users_for_query($options['users']));
118
		}
119
120
		$sql_where = $group_ids = array();
121
		$extremes = array('min' => '>=', 'max' => '<=');
122
123
		// Get auto group rule data for this type
124
		$group_rules = $this->get_group_rules($this->get_condition_type());
125
		foreach ($group_rules as $group_rule)
126
		{
127
			$where = array();
128
			foreach ($extremes as $end => $sign)
129
			{
130
				if (!empty($group_rule['autogroups_' . $end . '_value']))
131
				{
132
					$where[] = "u.user_warnings $sign " . $group_rule['autogroups_' . $end . '_value'];
133
				}
134
			}
135
136
			if (count($where))
137
			{
138
				$sql_where[] = '(' . implode(' AND ', $where) . ')';
139
			}
140
141
			$group_ids[] = $group_rule['autogroups_group_id'];
142
		}
143
144
		return '(' . (count($sql_where) ? implode(' OR ', $sql_where) . ' OR ' : '') . $this->db->sql_in_set('ug.group_id', $group_ids, false, true) . ')';
145
	}
146
}
147