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