Completed
Pull Request — master (#526)
by Michael
11:09 queued 01:05
created

StatsMonthlyStats::execute()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 137
Code Lines 98

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 98
nc 3
nop 0
dl 0
loc 137
ccs 0
cts 124
cp 0
crap 42
rs 7.4214
c 2
b 1
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**************************************************************************
3
**********      English Wikipedia Account Request Interface      **********
4
***************************************************************************
5
** Wikipedia Account Request Graphic Design by Charles Melbye,           **
6
** which is licensed under a Creative Commons                            **
7
** Attribution-Noncommercial-Share Alike 3.0 United States License.      **
8
**                                                                       **
9
** All other code are released under the Public Domain                   **
10
** by the ACC Development Team.                                          **
11
**                                                                       **
12
** See CREDITS for the list of developers.                               **
13
***************************************************************************/
14
15
class StatsMonthlyStats extends StatisticsPage
16
{
17
	protected function execute()
18
	{
19
		$qb = new QueryBrowser();
20
21
		$query = <<<SQL
22
SELECT
23
    COUNT(DISTINCT id) AS 'Requests Closed',
24
    YEAR(timestamp) AS 'Year',
25
    MONTHNAME(timestamp) AS 'Month'
26
FROM log
27
WHERE action LIKE 'Closed%'
28
GROUP BY EXTRACT(YEAR_MONTH FROM timestamp)
29
ORDER BY YEAR(timestamp) , MONTH(timestamp) ASC;
30
SQL;
31
32
		$out = $qb->executeQueryToTable($query);
33
34
		global $showGraphs;
35
		if ($showGraphs == 1) {
36
			global $filepath;
37
			require_once($filepath . 'graph/pChart/pChart.class');
38
			require_once($filepath . 'graph/pChart/pData.class');
39
40
			$queries = array();
41
42
			$queries[] = array(
43
					'query' => <<<SQL
44
SELECT
45
    COUNT(DISTINCT id) AS 'y',
46
    CONCAT(YEAR(timestamp), '/', MONTHNAME(timestamp)) AS 'x'
47
FROM log
48
WHERE action LIKE 'Closed%'
49
  AND YEAR(timestamp) != 0
50
GROUP BY EXTRACT(YEAR_MONTH FROM timestamp)
51
ORDER BY YEAR(timestamp) , MONTH(timestamp) ASC;
52
SQL
53
					,
54
					'series' => "All closed requests by month"
55
				);
56
			$queries[] = array(
57
					'query' => <<<SQL
58
SELECT
59
    COUNT(DISTINCT id) AS 'y',
60
    CONCAT(YEAR(timestamp), '/', MONTHNAME(timestamp)) AS 'x'
61
FROM log
62
WHERE action LIKE 'Closed 0'
63
  AND YEAR(timestamp) != 0
64
GROUP BY EXTRACT(YEAR_MONTH FROM timestamp)
65
ORDER BY YEAR(timestamp) , MONTH(timestamp) ASC;
66
SQL
67
					,
68
					'series' => "Dropped requests by month"
69
				);
70
71
			$query = gGetDb()->query("SELECT id, name FROM emailtemplate WHERE active = '1';");
72
			if (!$query) {
73
				die("Query error.");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
74
			}
75
76
			foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) {
77
				$id = $row['id'];
78
				$name = $row['name'];
79
				$queries[] = array(
80
					'query' => <<<SQL
81
SELECT
82
    COUNT(DISTINCT id) AS 'y',
83
    CONCAT(YEAR(timestamp), '/', MONTHNAME(timestamp)) AS 'x'
84
FROM log
85
WHERE action LIKE 'Closed $id'
86
  AND YEAR(timestamp) != 0
87
GROUP BY EXTRACT(YEAR_MONTH FROM timestamp)
88
ORDER BY YEAR(timestamp) , MONTH(timestamp) ASC;
89
SQL
90
					,
91
					'series' => "$name requests by month"
92
				);
93
			}
94
95
			$queries[] = array(
96
					'query' => <<<SQL
97
SELECT
98
    COUNT(DISTINCT id) AS 'y',
99
    CONCAT(YEAR(timestamp), '/', MONTHNAME(timestamp)) AS 'x'
100
FROM log
101
WHERE action = 'Closed custom-y'
102
  AND YEAR(timestamp) != 0
103
GROUP BY EXTRACT(YEAR_MONTH FROM timestamp)
104
ORDER BY YEAR(timestamp) , MONTH(timestamp) ASC;
105
SQL
106
					,
107
					'series' => "Custom created requests by month"
108
				);
109
			$queries[] = array(
110
					'query' => <<<SQL
111
SELECT
112
    COUNT(DISTINCT id) AS 'y',
113
    CONCAT(YEAR(timestamp), '/', MONTHNAME(timestamp)) AS 'x'
114
FROM log
115
WHERE action = 'Closed custom-n'
116
  AND YEAR(timestamp) != 0
117
GROUP BY EXTRACT(YEAR_MONTH FROM timestamp)
118
ORDER BY YEAR(timestamp) , MONTH(timestamp) ASC;
119
SQL
120
					,
121
					'series' => "Custom not created requests by month"
122
				);
123
124
			global $availableRequestStates;
125
			foreach ($availableRequestStates as $state) {
126
				$queries[] = array(
127
					'query' => <<<SQL
128
SELECT
129
    COUNT(DISTINCT id) AS 'y',
130
    CONCAT(YEAR(timestamp), '/', MONTHNAME(timestamp)) AS 'x'
131
FROM log
132
WHERE action LIKE 'Deferred to {$state["defertolog"]}'
133
  AND YEAR(timestamp) != 0
134
GROUP BY EXTRACT(YEAR_MONTH FROM timestamp)
135
ORDER BY YEAR(timestamp) , MONTH(timestamp) ASC;
136
SQL
137
					,
138
					'series' => "Requests deferred to " . $state['deferto'] . " by month"
139
				);
140
			}
141
142
			global $baseurl;
143
			foreach ($this->createClosuresGraph($queries) as $i) {
144
145
				$out .= '<img src="' . $baseurl . '/render/' . $i[0] . '" alt="' . $i[1] . '"/>';
146
			}
147
148
		}
149
		else {
150
			$out .= BootstrapSkin::displayAlertBox("Graph drawing is currently disabled.", "alert-info", "", false, false, true);
151
		}
152
153
		return $out;
154
	}
155
156
	public function getPageName()
157
	{
158
		return "MonthlyStats";
159
	}
160
161
	public function getPageTitle()
162
	{
163
		return "Monthly Statistics";
164
	}
165
166
	public function isProtected()
167
	{
168
		return true;
169
	}
170
171
	public function requiresWikiDatabase()
172
	{
173
		return false;
174
	}
175
176
	private function createClosuresGraph($queries)
177
	{
178
		$qb = new QueryBrowser();
179
180
		$imagehashes = array();
181
182
		foreach ($queries as $q) {
183
			$DataSet = new pData();
0 ignored issues
show
Bug introduced by
The type pData was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
184
			$qResult = $qb->executeQueryToArray($q['query']);
185
186
			if (sizeof($qResult) > 0) {
187
188
				foreach ($qResult as $row) {
189
					$DataSet->AddPoint($row['y'], $q['series'], $row['x']);
190
				}
191
192
				$DataSet->AddAllSeries();
193
				$DataSet->SetAbsciseLabelSerie();
194
195
				$chartname = $this->createPathFromHash(md5(serialize($DataSet)));
196
197
				$imagehashes[] = array($chartname, $q['series']);
198
199
				if (!file_exists($chartname)) {
200
					$Test = new pChart(700, 280);
0 ignored issues
show
Bug introduced by
The type pChart was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
201
					$Test->setFontProperties("graph/Fonts/tahoma.ttf", 8);
202
					$Test->setGraphArea(50, 30, 680, 200);
203
					$Test->drawFilledRoundedRectangle(7, 7, 693, 273, 5, 240, 240, 240);
204
					$Test->drawRoundedRectangle(5, 5, 695, 275, 5, 230, 230, 230);
205
					$Test->drawGraphArea(255, 255, 255, true);
206
					$Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_NORMAL, 150, 150, 150, true, 45, 2);
0 ignored issues
show
Bug introduced by
The constant SCALE_NORMAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
207
					$Test->drawGrid(4, true, 230, 230, 230, 50);
208
209
					// Draw the 0 line
210
					$Test->setFontProperties("graph/Fonts/tahoma.ttf", 6);
211
					$Test->drawTreshold(0, 143, 55, 72, true, true);
212
213
					// Draw the cubic curve graph
214
					$Test->drawFilledCubicCurve($DataSet->GetData(), $DataSet->GetDataDescription(), .1, 50);
215
216
					// Finish the graph
217
					$Test->setFontProperties("graph/Fonts/tahoma.ttf", 10);
218
					$Test->drawTitle(50, 22, $q['series'], 50, 50, 50, 585);
219
					$Test->Render("render/" . $chartname);
220
				}
221
			}
222
		}
223
224
		return $imagehashes;
225
226
	}
227
228
	/**
229
	 * @param string $imghash
230
	 * @return string
231
	 */
232
	private function createPathFromHash($imghash, $basedirectory = "render/")
233
	{
234
		$imghashparts = str_split($imghash);
235
		$imgpath = array_shift($imghashparts) . "/";
236
		$imgpath .= array_shift($imghashparts) . "/";
237
		$imgpath .= array_shift($imghashparts) . "/";
238
239
		is_dir($basedirectory . $imgpath) || mkdir($basedirectory . $imgpath, 0777, true);
240
241
		$imgpath .= implode("", $imghashparts);
242
		return $imgpath;
243
	}
244
}
245