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