Passed
Pull Request — developer (#17141)
by Radosław
17:17
created

getWidgetTimeControl()   B

Complexity

Conditions 10
Paths 28

Size

Total Lines 64
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 64
ccs 0
cts 41
cp 0
rs 7.2896
c 0
b 0
f 0
cc 10
nc 28
nop 2
crap 110

How to fix   Long Method    Complexity   

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
/**
4
 * OSSTimeControl TimeControl dashboard class.
5
 *
6
 * @copyright YetiForce S.A.
7
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
8
 */
9
class OSSTimeControl_TimeControl_Dashboard extends Vtiger_IndexAjax_View
10
{
11
	/**
12
	 * Return search params (use to in building address URL to listview).
13
	 *
14
	 * @param int|string $owner
15
	 * @param string     $date
16
	 * @param mixed      $assignedto
17
	 *
18
	 * @return string
19
	 */
20
	public function getSearchParams($assignedto, $date)
21
	{
22
		$conditions = [];
23
		$date = \App\Fields\Date::formatToDisplay($date);
24
		$listSearchParams = [];
25
		if ($assignedto) {
26
			array_push($conditions, ['assigned_user_id', 'e', $assignedto]);
27
		}
28
		if (!empty($date)) {
29
			array_push($conditions, ['due_date', 'bw', $date . ',' . $date]);
30
		}
31
		$listSearchParams[] = $conditions;
32
		return '&search_params=' . urlencode(json_encode($listSearchParams));
33
	}
34
35
	public function getWidgetTimeControl($user, $date)
36
	{
37
		if (!$date) {
38
			return ['show_chart' => false];
39
		}
40
		$moduleName = 'OSSTimeControl';
41
		$queryGenerator = new \App\QueryGenerator($moduleName);
42
		$queryGenerator->setFields(['due_date', 'timecontrol_type'])
43
			->setCustomColumn(['sum_time' => new \yii\db\Expression('SUM(sum_time)')])
44
			->addCondition('assigned_user_id', $user, 'e')
45
			->addCondition('due_date', implode(',', $date), 'bw')
46
			->setGroup('due_date')->setGroup('timecontrol_type')->setOrder('due_date');
47
		$showMonth = \App\Fields\DateTime::getDiff($date[0], $date[1], 'days') > 31;
48
		$data = $queryGenerator->createQuery()->all();
49
		$colors = \App\Fields\Picklist::getColors('timecontrol_type', false);
50
		$chartData = [
51
			'show_chart' => false,
52
		];
53
54
		$listViewUrl = Vtiger_Module_Model::getInstance($moduleName)->getListViewUrl();
55
		$sumValuePerXAxisData = [];
56
		$chartData['xAxis']['data'] = array_map(fn ($dueDate) => $showMonth ? substr($dueDate, -5) : substr($dueDate, -2), array_unique(array_column($data, 'due_date')), []);
57
		$types = array_values(array_unique(array_column($data, 'timecontrol_type')));
58
59
		foreach ($data as $row) {
60
			$dueDate = $row['due_date'];
61
			$type = $row['timecontrol_type'];
62
			$sumTime = $row['sum_time'];
63
			$xAxisLabel = $showMonth ? substr($dueDate, -5) : substr($dueDate, -2);
64
			$sumValuePerXAxisData[$type] = ($sumValuePerXAxisData[$type] ?? 0) + (float) $sumTime;
65
			$seriesIndex = array_search($type, $types);
66
67
			if (empty($chartData['series'][$seriesIndex])) {
68
				foreach (array_keys($chartData['xAxis']['data']) as $statusIndex) {
69
					$chartData['series'][$seriesIndex]['data'][$statusIndex] = ['value' => null];
70
				}
71
			}
72
			$statusIndex = array_search($xAxisLabel, $chartData['xAxis']['data']);
73
74
			$color = $colors[$type] ?? \App\Colors::getRandomColor($type);
75
			$link = $listViewUrl . '&viewname=All&entityState=Active' . $this->getSearchParams($user, $dueDate);
76
			$label = $type ? \App\Language::translate($type, $moduleName, null, false) : '(' . \App\Language::translate('LBL_EMPTY', 'Home', null, false) . ')';
77
78
			$chartData['series'][$seriesIndex]['name'] = $label;
79
			$chartData['series'][$seriesIndex]['type'] = 'bar';
80
			$chartData['series'][$seriesIndex]['stack'] = 'total';
81
			$chartData['series'][$seriesIndex]['color'] = $color;
82
			$chartData['series'][$seriesIndex]['label'] = ['show' => false];
83
			$chartData['tooltip'] = ['trigger' => 'axis', 'axisPointer' => ['type' => 'shadow']];
84
			$chartData['labelLayout'] = ['hideOverlap' => false];
85
			$chartData['series'][$seriesIndex]['data'][$statusIndex] = ['value' => round($sumTime / 60, 2), 'itemStyle' => ['color' => $color], 'link' => $link, 'fullLabel' => \App\Fields\DateTime::formatToDay($dueDate, true), 'fullValue' => \App\Fields\RangeTime::displayElapseTime($sumTime), 'seriesLabel' => $label];
86
87
			$chartData['show_chart'] = true;
88
		}
89
90
		foreach ($sumValuePerXAxisData as $type => $value) {
91
			if (!$value) {
92
				continue;
93
			}
94
			$seriesIndex = array_search($type, $types);
95
			$chartData['series'][$seriesIndex]['name'] .= ': ' . \App\Fields\RangeTime::displayElapseTime($value);
96
		}
97
98
		return $chartData;
99
	}
100
101
	public function process(App\Request $request)
102
	{
103
		$currentUserId = \App\User::getCurrentUserId();
104
		$viewer = $this->getViewer($request);
105
		$moduleName = $request->getModule();
106
		$user = $request->getByType('user', 2);
107
		$time = $request->getDateRange('time');
108
		$widget = Vtiger_Widget_Model::getInstance($request->getInteger('linkid'), $currentUserId);
109
		if (empty($time)) {
110
			$time = Settings_WidgetsManagement_Module_Model::getDefaultDateRange($widget);
111
		}
112
		if (empty($user)) {
113
			$user = $currentUserId;
114
		}
115
		$data = $this->getWidgetTimeControl($user, $time);
116
		$viewer->assign('USER_CONDITIONS', null);
117
		$viewer->assign('TCPMODULE_MODEL', Settings_TimeControlProcesses_Module_Model::getCleanInstance()->getConfigInstance());
118
		$viewer->assign('USERID', $user);
119
		$viewer->assign('DTIME', \App\Fields\Date::formatRangeToDisplay($time));
120
		$viewer->assign('DATA', $data);
121
		$viewer->assign('WIDGET', $widget);
122
		$viewer->assign('MODULE_NAME', $moduleName);
123
		$viewer->assign('LOGGEDUSERID', $currentUserId);
124
		$viewer->assign('SOURCE_MODULE', 'OSSTimeControl');
125
		if ($request->has('content')) {
126
			$viewer->view('dashboards/TimeControlContents.tpl', $moduleName);
127
		} else {
128
			$viewer->view('dashboards/TimeControl.tpl', $moduleName);
129
		}
130
	}
131
}
132