Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Report_Form_Case_TimeSpent extends CRM_Report_Form { | |
73b448bf | 18 | |
74cf4551 | 19 | /** |
73b448bf | 20 | * Class constructor. |
74cf4551 | 21 | */ |
00be9182 | 22 | public function __construct() { |
6a488035 TO |
23 | |
24 | $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE); | |
25 | asort($this->activityTypes); | |
26 | $this->activityStatuses = CRM_Core_PseudoConstant::activityStatus(); | |
27 | ||
be2fb01f CW |
28 | $this->_columns = [ |
29 | 'civicrm_contact' => [ | |
6a488035 | 30 | 'dao' => 'CRM_Contact_DAO_Contact', |
be2fb01f CW |
31 | 'fields' => [ |
32 | 'id' => [ | |
9d72cede | 33 | 'title' => ts('Contact ID'), |
6a488035 TO |
34 | 'no_display' => TRUE, |
35 | 'required' => TRUE, | |
be2fb01f CW |
36 | ], |
37 | 'sort_name' => [ | |
9d72cede | 38 | 'title' => ts('Contact Name'), |
6a488035 TO |
39 | 'required' => TRUE, |
40 | 'no_repeat' => TRUE, | |
be2fb01f CW |
41 | ], |
42 | ], | |
43 | 'filters' => [ | |
44 | 'sort_name' => [ | |
9d72cede | 45 | 'title' => ts('Contact Name'), |
6a488035 TO |
46 | 'operator' => 'like', |
47 | 'type' => CRM_Report_Form::OP_STRING, | |
be2fb01f CW |
48 | ], |
49 | ], | |
50 | 'order_bys' => [ | |
51 | 'sort_name' => [ | |
a2323084 BS |
52 | 'title' => ts('Contact Name'), |
53 | 'default_weight' => '1', | |
be2fb01f CW |
54 | ], |
55 | ], | |
56 | ], | |
57 | 'civicrm_activity' => [ | |
6a488035 | 58 | 'dao' => 'CRM_Activity_DAO_Activity', |
be2fb01f CW |
59 | 'fields' => [ |
60 | 'activity_type_id' => [ | |
9d72cede | 61 | 'title' => ts('Activity Type'), |
6a488035 TO |
62 | 'default' => TRUE, |
63 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
64 | ], |
65 | 'activity_date_time' => [ | |
9d72cede | 66 | 'title' => ts('Activity Date'), |
6a488035 | 67 | 'default' => TRUE, |
be2fb01f CW |
68 | ], |
69 | 'status_id' => [ | |
9d72cede | 70 | 'title' => ts('Activity Status'), |
6a488035 TO |
71 | 'default' => FALSE, |
72 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
73 | ], |
74 | 'id' => [ | |
9d72cede | 75 | 'title' => ts('Activity ID'), |
6a488035 | 76 | 'default' => TRUE, |
be2fb01f CW |
77 | ], |
78 | 'duration' => [ | |
9d72cede | 79 | 'title' => ts('Duration'), |
6a488035 TO |
80 | 'default' => TRUE, |
81 | 'type' => CRM_Utils_Type::T_INT, | |
be2fb01f CW |
82 | ], |
83 | 'subject' => [ | |
9d72cede | 84 | 'title' => ts('Activity Subject'), |
6a488035 | 85 | 'default' => FALSE, |
be2fb01f CW |
86 | ], |
87 | ], | |
88 | 'filters' => [ | |
89 | 'activity_date_time' => [ | |
e7483cbe | 90 | 'operatorType' => CRM_Report_Form::OP_DATE, |
be2fb01f CW |
91 | ], |
92 | 'subject' => [ | |
9d72cede | 93 | 'title' => ts('Activity Subject'), |
09b5cdcf | 94 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 | 95 | 'operator' => 'like', |
be2fb01f CW |
96 | ], |
97 | 'activity_type_id' => [ | |
9d72cede | 98 | 'title' => ts('Activity Type'), |
6a488035 TO |
99 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
100 | 'options' => $this->activityTypes, | |
be2fb01f CW |
101 | ], |
102 | 'status_id' => [ | |
9d72cede | 103 | 'title' => ts('Activity Status'), |
09b5cdcf | 104 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
105 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
106 | 'options' => $this->activityStatuses, | |
be2fb01f CW |
107 | ], |
108 | ], | |
109 | 'order_bys' => [ | |
110 | 'subject' => [ | |
a2323084 | 111 | 'title' => ts('Activity Subject'), |
be2fb01f CW |
112 | ], |
113 | 'activity_type_id' => [ | |
a2323084 | 114 | 'title' => ts('Activity Type'), |
be2fb01f CW |
115 | ], |
116 | 'activity_date_time' => [ | |
a2323084 | 117 | 'title' => ts('Activity Date'), |
be2fb01f CW |
118 | ], |
119 | 'status_id' => [ | |
a2323084 | 120 | 'title' => ts('Activity Status'), |
be2fb01f CW |
121 | ], |
122 | ], | |
a2323084 | 123 | 'grouping' => 'case-fields', |
be2fb01f CW |
124 | ], |
125 | 'civicrm_activity_source' => [ | |
9d72cede | 126 | 'dao' => 'CRM_Activity_DAO_ActivityContact', |
be2fb01f CW |
127 | 'fields' => [ |
128 | 'contact_id' => [ | |
9d72cede EM |
129 | 'title' => ts('Contact ID'), |
130 | 'default' => TRUE, | |
131 | 'no_display' => TRUE, | |
be2fb01f CW |
132 | ], |
133 | ], | |
134 | 'group_bys' => [ | |
135 | 'contact_id' => [ | |
9d72cede EM |
136 | 'title' => ts('Totals Only'), |
137 | 'default' => TRUE, | |
be2fb01f CW |
138 | ], |
139 | ], | |
ecaec004 | 140 | 'grouping' => 'activity-fields', |
be2fb01f CW |
141 | ], |
142 | 'civicrm_case_activity' => [ | |
6a488035 | 143 | 'dao' => 'CRM_Case_DAO_CaseActivity', |
be2fb01f CW |
144 | 'fields' => [ |
145 | 'case_id' => [ | |
9d72cede | 146 | 'title' => ts('Case ID'), |
6a488035 | 147 | 'default' => FALSE, |
be2fb01f CW |
148 | ], |
149 | ], | |
150 | 'filters' => [ | |
151 | 'case_id_filter' => [ | |
6a488035 TO |
152 | 'name' => 'case_id', |
153 | 'title' => ts('Cases?'), | |
154 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
be2fb01f | 155 | 'options' => [ |
9d72cede EM |
156 | 1 => ts('Exclude non-case'), |
157 | 2 => ts('Exclude cases'), | |
21dfd5f5 | 158 | 3 => ts('Include Both'), |
be2fb01f | 159 | ], |
6a488035 | 160 | 'default' => 3, |
be2fb01f CW |
161 | ], |
162 | ], | |
163 | ], | |
164 | ]; | |
6a488035 TO |
165 | |
166 | parent::__construct(); | |
167 | } | |
168 | ||
00be9182 | 169 | public function select() { |
be2fb01f CW |
170 | $select = []; |
171 | $this->_columnHeaders = []; | |
6a488035 TO |
172 | |
173 | $this->has_grouping = !empty($this->_params['group_bys']); | |
174 | $this->has_activity_type = FALSE; | |
175 | ||
176 | foreach ($this->_columns as $tableName => $table) { | |
177 | if (array_key_exists('fields', $table)) { | |
178 | foreach ($table['fields'] as $fieldName => $field) { | |
a7488080 | 179 | if (!empty($field['required']) || |
9d72cede EM |
180 | (!empty($this->_params['fields'][$fieldName]) && |
181 | ((!$this->has_grouping) || | |
be2fb01f | 182 | !in_array($fieldName, ['case_id', 'subject', 'status_id'])) |
6a488035 TO |
183 | ) |
184 | ) { | |
185 | ||
9c1bc317 CW |
186 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL; |
187 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'] ?? NULL; | |
188 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = $field['no_display'] ?? NULL; | |
6a488035 TO |
189 | |
190 | if ($fieldName == 'activity_type_id') { | |
191 | $this->has_activity_type = TRUE; | |
192 | } | |
193 | ||
194 | if ($fieldName == 'duration' && $this->has_grouping) { | |
195 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; | |
196 | } | |
197 | elseif ($fieldName == 'activity_date_time' && $this->has_grouping) { | |
198 | $select[] = "EXTRACT(YEAR_MONTH FROM {$field['dbAlias']}) AS {$tableName}_{$fieldName}"; | |
199 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = ts('Month/Year'); | |
200 | } | |
9d72cede EM |
201 | elseif ($tableName == 'civicrm_activity' && $fieldName == 'id' && |
202 | $this->has_grouping | |
203 | ) { | |
6a488035 TO |
204 | $select[] = "COUNT({$field['dbAlias']}) AS {$tableName}_{$fieldName}"; |
205 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = ts('# Activities'); | |
206 | } | |
207 | else { | |
208 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
209 | } | |
210 | } | |
211 | } | |
212 | } | |
213 | } | |
d1641c51 | 214 | $this->_selectClauses = $select; |
6a488035 TO |
215 | |
216 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
217 | } | |
218 | ||
00be9182 | 219 | public function from() { |
6a488035 TO |
220 | |
221 | $this->_from = " | |
222 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} | |
ecaec004 RN |
223 | LEFT JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_source']} |
224 | ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_source']}.activity_id | |
6a488035 | 225 | LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
ecaec004 | 226 | ON {$this->_aliases['civicrm_activity_source']}.contact_id = {$this->_aliases['civicrm_contact']}.id |
6a488035 TO |
227 | LEFT JOIN civicrm_case_activity {$this->_aliases['civicrm_case_activity']} |
228 | ON {$this->_aliases['civicrm_case_activity']}.activity_id = {$this->_aliases['civicrm_activity']}.id | |
229 | "; | |
230 | } | |
231 | ||
00be9182 | 232 | public function where() { |
2f4c2f5d | 233 | $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_current_revision = 1 AND |
6a488035 TO |
234 | {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND |
235 | {$this->_aliases['civicrm_activity']}.is_test = 0"; | |
be2fb01f | 236 | $clauses = []; |
6a488035 TO |
237 | foreach ($this->_columns as $tableName => $table) { |
238 | if (array_key_exists('filters', $table)) { | |
239 | ||
240 | foreach ($table['filters'] as $fieldName => $field) { | |
241 | $clause = NULL; | |
242 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
9c1bc317 CW |
243 | $relative = $this->_params["{$fieldName}_relative"] ?? NULL; |
244 | $from = $this->_params["{$fieldName}_from"] ?? NULL; | |
245 | $to = $this->_params["{$fieldName}_to"] ?? NULL; | |
6a488035 TO |
246 | |
247 | $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to); | |
248 | } | |
249 | else { | |
9c1bc317 | 250 | $op = $this->_params["{$fieldName}_op"] ?? NULL; |
6a488035 TO |
251 | if ($op) { |
252 | // handle special case | |
253 | if ($fieldName == 'case_id_filter') { | |
9c1bc317 | 254 | $choice = $this->_params["{$fieldName}_value"] ?? NULL; |
6a488035 TO |
255 | if ($choice == 1) { |
256 | $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Not Null)"; | |
257 | } | |
258 | elseif ($choice == 2) { | |
259 | $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Null)"; | |
260 | } | |
261 | } | |
262 | else { | |
263 | $clause = $this->whereClause($field, | |
264 | $op, | |
265 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
266 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
267 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
268 | ); | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | if (!empty($clause)) { | |
274 | $clauses[] = $clause; | |
275 | } | |
276 | } | |
277 | } | |
278 | } | |
279 | ||
280 | if (empty($clauses)) { | |
281 | $this->_where .= " "; | |
282 | } | |
283 | else { | |
284 | $this->_where .= " AND " . implode(' AND ', $clauses); | |
285 | } | |
286 | } | |
287 | ||
00be9182 | 288 | public function groupBy() { |
6a488035 TO |
289 | $this->_groupBy = ''; |
290 | if ($this->has_grouping) { | |
be2fb01f | 291 | $groupBy = [ |
d1641c51 | 292 | "{$this->_aliases['civicrm_contact']}.id", |
dc852c7b | 293 | "civicrm_activity_activity_date_time", |
be2fb01f | 294 | ]; |
d1641c51 | 295 | if ($this->has_activity_type) { |
296 | $groupBy[] = "{$this->_aliases['civicrm_activity']}.activity_type_id"; | |
297 | } | |
298 | ||
b708c08d | 299 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy); |
6a488035 TO |
300 | } |
301 | } | |
302 | ||
00be9182 | 303 | public function postProcess() { |
6a488035 TO |
304 | parent::postProcess(); |
305 | } | |
306 | ||
74cf4551 EM |
307 | /** |
308 | * @param $fields | |
309 | * @param $files | |
e8cf95b4 | 310 | * @param self $self |
74cf4551 EM |
311 | * |
312 | * @return array | |
313 | */ | |
00be9182 | 314 | public static function formRule($fields, $files, $self) { |
be2fb01f | 315 | $errors = []; |
6a488035 | 316 | if (!empty($fields['group_bys']) && |
9d72cede EM |
317 | (!array_key_exists('id', $fields['fields']) || |
318 | !array_key_exists('activity_date_time', $fields['fields']) || | |
319 | !array_key_exists('duration', $fields['fields'])) | |
6a488035 TO |
320 | ) { |
321 | $errors['fields'] = ts('To view totals please select all of activity id, date and duration.'); | |
322 | } | |
6a488035 TO |
323 | return $errors; |
324 | } | |
325 | ||
74cf4551 | 326 | /** |
ced9bfed EM |
327 | * Alter display of rows. |
328 | * | |
329 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
330 | * such as rendering contacts as links. | |
331 | * | |
332 | * @param array $rows | |
333 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 334 | */ |
00be9182 | 335 | public function alterDisplay(&$rows) { |
6a488035 TO |
336 | |
337 | $entryFound = FALSE; | |
338 | foreach ($rows as $rowNum => $row) { | |
339 | ||
340 | if (isset($row['civicrm_activity_activity_type_id'])) { | |
341 | $entryFound = TRUE; | |
342 | $val = $row['civicrm_activity_activity_type_id']; | |
2e1f50d6 | 343 | $rows[$rowNum]['civicrm_activity_activity_type_id'] = $this->activityTypes[$val] ?? ''; |
6a488035 TO |
344 | } |
345 | ||
346 | if (isset($row['civicrm_activity_status_id'])) { | |
347 | $entryFound = TRUE; | |
348 | $val = $row['civicrm_activity_status_id']; | |
2e1f50d6 | 349 | $rows[$rowNum]['civicrm_activity_status_id'] = $this->activityStatuses[$val] ?? ''; |
6a488035 TO |
350 | } |
351 | ||
352 | // The next two make it easier to make pivot tables after exporting to Excel | |
353 | if (isset($row['civicrm_activity_duration'])) { | |
354 | $entryFound = TRUE; | |
355 | $rows[$rowNum]['civicrm_activity_duration'] = (int) $row['civicrm_activity_duration']; | |
356 | } | |
357 | ||
358 | if (isset($row['civicrm_case_activity_case_id'])) { | |
359 | $entryFound = TRUE; | |
360 | $rows[$rowNum]['civicrm_case_activity_case_id'] = (int) $row['civicrm_case_activity_case_id']; | |
361 | } | |
362 | ||
363 | if (!$entryFound) { | |
364 | break; | |
365 | } | |
366 | } | |
367 | } | |
96025800 | 368 | |
6a488035 | 369 | } |