getWidgetTimeControl()   F
last analyzed

Complexity

Conditions 18
Paths 273

Size

Total Lines 100
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
eloc 75
dl 0
loc 100
ccs 0
cts 63
cp 0
rs 3.1208
c 0
b 0
f 0
cc 18
nc 273
nop 2
crap 342

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
 * Wdiget to show work time.
5
 *
6
 * @copyright YetiForce S.A.
7
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
8
 * @author    Tomasz Kur <[email protected]>
9
 * @author    Radosław Skrzypczak <[email protected]>
10
 */
11
class OSSTimeControl_AllTimeControl_Dashboard extends Vtiger_IndexAjax_View
12
{
13
	public function getSearchParams($assignedto, $date)
14
	{
15
		$conditions = [];
16
		$listSearchParams = [];
17
		if ('' != $assignedto) {
18
			array_push($conditions, ['assigned_user_id', 'e', $assignedto]);
19
		}
20
		if (!empty($date)) {
21
			array_push($conditions, ['due_date', 'bw', implode(',', $date)]);
22
		}
23
		$listSearchParams[] = $conditions;
24
		return '&search_params=' . json_encode($listSearchParams);
25
	}
26
27
	public function getWidgetTimeControl($user, $time)
28
	{
29
		if (!$time) {
30
			return ['show_chart' => false];
31
		}
32
		$currentUser = Users_Record_Model::getCurrentUserModel();
33
		if ('all' == $user) {
34
			$user = array_keys(\App\Fields\Owner::getInstance(false, $currentUser)->getAccessibleUsers());
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $moduleName of App\Fields\Owner::getInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
			$user = array_keys(\App\Fields\Owner::getInstance(/** @scrutinizer ignore-type */ false, $currentUser)->getAccessibleUsers());
Loading history...
35
		}
36
		if (!\is_array($user)) {
37
			$user = [$user];
38
		}
39
		$colors = \App\Fields\Picklist::getColors('timecontrol_type');
40
		$moduleName = 'OSSTimeControl';
41
		$query = (new App\Db\Query())->select(['sum_time', 'due_date', 'vtiger_osstimecontrol.timecontrol_type', 'vtiger_crmentity.smownerid', 'timecontrol_typeid'])
42
			->from('vtiger_osstimecontrol')
43
			->innerJoin('vtiger_crmentity', 'vtiger_osstimecontrol.osstimecontrolid = vtiger_crmentity.crmid')
44
			->innerJoin('vtiger_timecontrol_type', 'vtiger_osstimecontrol.timecontrol_type = vtiger_timecontrol_type.timecontrol_type')
45
			->where(['vtiger_crmentity.setype' => $moduleName, 'vtiger_crmentity.smownerid' => $user]);
46
		\App\PrivilegeQuery::getConditions($query, $moduleName);
47
		$query->andWhere([
48
			'and',
49
			['>=', 'vtiger_osstimecontrol.due_date', $time[0]],
50
			['<=', 'vtiger_osstimecontrol.due_date', $time[1]],
51
			['vtiger_osstimecontrol.deleted' => 0],
52
		]);
53
		$timeTypes = [];
54
		$smOwners = [];
55
		$dataReader = $query->createCommand()->query();
56
		$chartData = [
57
			'labels' => [],
58
			'fullLabels' => [],
59
			'datasets' => [],
60
			'show_chart' => false,
61
		];
62
		$time = \App\Fields\Date::formatRangeToDisplay($time);
63
		$workingTimeByType = $workingTime = [];
64
		while ($row = $dataReader->read()) {
65
			$label = \App\Language::translate($row['timecontrol_type'], 'OSSTimeControl');
66
			if (isset($workingTimeByType[$label])) {
67
				$workingTimeByType[$label] += (float) $row['sum_time'];
68
			} else {
69
				$workingTimeByType[$label] = (float) $row['sum_time'];
70
			}
71
			if (isset($workingTime[$row['smownerid']][$row['timecontrol_type']])) {
72
				$workingTime[$row['smownerid']][$row['timecontrol_type']] += (float) $row['sum_time'];
73
			} else {
74
				$workingTime[$row['smownerid']][$row['timecontrol_type']] = (float) $row['sum_time'];
75
			}
76
			if (!\in_array($row['timecontrol_type'], $timeTypes)) {
77
				$timeTypes[$row['timecontrol_typeid']] = $row['timecontrol_type'];
78
				// one dataset per type
79
				$chartData['datasets'][] = [
80
					'label' => $label,
81
					'original_label' => $label,
82
					'_type' => $row['timecontrol_type'],
83
					'data' => [],
84
					'dataFormatted' => [],
85
					'backgroundColor' => [],
86
					'links' => [],
87
				];
88
			}
89
			if (!\in_array($row['smownerid'], $smOwners)) {
90
				$smOwners[] = $row['smownerid'];
91
				$ownerName = \App\Fields\Owner::getUserLabel($row['smownerid']);
92
				$chartData['labels'][] = \App\Utils::getInitials($ownerName);
93
				$chartData['fullLabels'][] = $ownerName;
94
			}
95
		}
96
		foreach ($chartData['datasets'] as &$dataset) {
97
			$dataset['label'] .= ': ' . \App\Fields\RangeTime::displayElapseTime($workingTimeByType[$dataset['label']]);
98
		}
99
		if ($dataReader->count() > 0) {
100
			$chartData['show_chart'] = true;
101
			foreach ($workingTime as $ownerId => $timeValue) {
102
				foreach ($timeTypes as $timeTypeId => $timeType) {
103
					// if owner has this kind of type
104
					if (!empty($timeValue[$timeType])) {
105
						$userTime = $timeValue[$timeType];
106
					} else {
107
						$userTime = 0;
108
					}
109
					foreach ($chartData['datasets'] as &$dataset) {
110
						if ($dataset['_type'] === $timeType) {
111
							// each data item is an different owner time in this dataset/time type
112
							$dataset['data'][] = round($userTime / 60, 2);
113
							$dataset['backgroundColor'][] = $colors[$timeTypeId];
114
							$dataset['dataFormatted'][] = \App\Fields\RangeTime::displayElapseTime($userTime);
115
						}
116
					}
117
				}
118
			}
119
			foreach ($smOwners as $ownerId) {
120
				foreach ($chartData['datasets'] as &$dataset) {
121
					$dataset['links'][] = 'index.php?module=OSSTimeControl&view=List&viewname=All&entityState=Active' . $this->getSearchParams($ownerId, $time);
122
				}
123
			}
124
		}
125
		$dataReader->close();
126
		return $chartData;
127
	}
128
129
	public function process(App\Request $request)
130
	{
131
		$currentUserId = \App\User::getCurrentUserId();
132
		$viewer = $this->getViewer($request);
133
		$moduleName = $request->getModule();
134
		$user = $request->getByType('owner', 2);
135
		$time = $request->getDateRange('time');
136
		$widget = Vtiger_Widget_Model::getInstance($request->getInteger('linkid'), $currentUserId);
137
		if (empty($time)) {
138
			$time = Settings_WidgetsManagement_Module_Model::getDefaultDateRange($widget);
139
		}
140
		if (empty($user)) {
141
			$user = Settings_WidgetsManagement_Module_Model::getDefaultUserId($widget);
142
		}
143
		$viewer->assign('TCPMODULE_MODEL', Settings_TimeControlProcesses_Module_Model::getCleanInstance()->getConfigInstance());
144
		$viewer->assign('OWNER', $user);
145
		$viewer->assign('DTIME', \App\Fields\Date::formatRangeToDisplay($time));
146
		$viewer->assign('DATA', $this->getWidgetTimeControl($user, $time));
147
		$viewer->assign('WIDGET', $widget);
148
		$viewer->assign('MODULE_NAME', $moduleName);
149
		$viewer->assign('LOGGEDUSERID', $currentUserId);
150
		$viewer->assign('ACCESSIBLE_USERS', \App\Fields\Owner::getInstance($moduleName, $currentUserId)->getAccessibleUsersForModule());
151
		$viewer->assign('ACCESSIBLE_GROUPS', \App\Fields\Owner::getInstance($moduleName, $currentUserId)->getAccessibleGroupForModule());
152
		if ($request->has('content')) {
153
			$viewer->view('dashboards/TimeControlContents.tpl', $moduleName);
154
		} else {
155
			$viewer->view('dashboards/AllTimeControl.tpl', $moduleName);
156
		}
157
	}
158
}
159