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