3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
33 class CRM_Report_Form_Case_TimeSpent
extends CRM_Report_Form
{
38 public function __construct() {
40 $this->activityTypes
= CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
41 asort($this->activityTypes
);
42 $this->activityStatuses
= CRM_Core_PseudoConstant
::activityStatus();
44 $this->_columns
= array(
45 'civicrm_contact' => array(
46 'dao' => 'CRM_Contact_DAO_Contact',
49 'title' => ts('Contact ID'),
54 'title' => ts('Contact Name'),
61 'title' => ts('Contact Name'),
63 'type' => CRM_Report_Form
::OP_STRING
,
67 'civicrm_activity' => array(
68 'dao' => 'CRM_Activity_DAO_Activity',
70 'activity_type_id' => array(
71 'title' => ts('Activity Type'),
73 'type' => CRM_Utils_Type
::T_STRING
,
75 'activity_date_time' => array(
76 'title' => ts('Activity Date'),
80 'title' => ts('Activity Status'),
82 'type' => CRM_Utils_Type
::T_STRING
,
85 'title' => ts('Activity ID'),
89 'title' => ts('Duration'),
91 'type' => CRM_Utils_Type
::T_INT
,
94 'title' => ts('Activity Subject'),
99 'activity_date_time' => array(
100 'operatorType' => CRM_Report_Form
::OP_DATE
,
103 'title' => ts('Activity Subject'),
104 'type' => CRM_Utils_Type
::T_STRING
,
105 'operator' => 'like',
107 'activity_type_id' => array(
108 'title' => ts('Activity Type'),
109 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
110 'options' => $this->activityTypes
,
112 'status_id' => array(
113 'title' => ts('Activity Status'),
114 'type' => CRM_Utils_Type
::T_INT
,
115 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
116 'options' => $this->activityStatuses
,
120 'civicrm_activity_source' => array(
121 'dao' => 'CRM_Activity_DAO_ActivityContact',
123 'contact_id' => array(
124 'title' => ts('Contact ID'),
126 'no_display' => TRUE,
129 'group_bys' => array(
130 'contact_id' => array(
131 'title' => ts('Totals Only'),
135 'grouping' => 'activity-fields',
137 'civicrm_case_activity' => array(
138 'dao' => 'CRM_Case_DAO_CaseActivity',
141 'title' => ts('Case ID'),
146 'case_id_filter' => array(
148 'title' => ts('Cases?'),
149 'operatorType' => CRM_Report_Form
::OP_SELECT
,
151 1 => ts('Exclude non-case'),
152 2 => ts('Exclude cases'),
153 3 => ts('Include Both'),
161 parent
::__construct();
164 public function select() {
166 $this->_columnHeaders
= array();
168 $this->has_grouping
= !empty($this->_params
['group_bys']);
169 $this->has_activity_type
= FALSE;
171 foreach ($this->_columns
as $tableName => $table) {
172 if (array_key_exists('fields', $table)) {
173 foreach ($table['fields'] as $fieldName => $field) {
174 if (!empty($field['required']) ||
175 (!empty($this->_params
['fields'][$fieldName]) &&
176 ((!$this->has_grouping
) ||
177 !in_array($fieldName, array('case_id', 'subject', 'status_id')))
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);
185 if ($fieldName == 'activity_type_id') {
186 $this->has_activity_type
= TRUE;
189 if ($fieldName == 'duration' && $this->has_grouping
) {
190 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
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');
196 elseif ($tableName == 'civicrm_activity' && $fieldName == 'id' &&
199 $select[] = "COUNT({$field['dbAlias']}) AS {$tableName}_{$fieldName}";
200 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = ts('# Activities');
203 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
209 $this->_selectClauses
= $select;
211 $this->_select
= "SELECT " . implode(', ', $select) . " ";
214 public function from() {
217 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
218 LEFT JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_source']}
219 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_source']}.activity_id
220 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
221 ON {$this->_aliases['civicrm_activity_source']}.contact_id = {$this->_aliases['civicrm_contact']}.id
222 LEFT JOIN civicrm_case_activity {$this->_aliases['civicrm_case_activity']}
223 ON {$this->_aliases['civicrm_case_activity']}.activity_id = {$this->_aliases['civicrm_activity']}.id
227 public function where() {
228 $this->_where
= " WHERE {$this->_aliases['civicrm_activity']}.is_current_revision = 1 AND
229 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
230 {$this->_aliases['civicrm_activity']}.is_test = 0";
232 foreach ($this->_columns
as $tableName => $table) {
233 if (array_key_exists('filters', $table)) {
235 foreach ($table['filters'] as $fieldName => $field) {
237 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
238 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
239 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
240 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
242 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to);
245 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
247 // handle special case
248 if ($fieldName == 'case_id_filter') {
249 $choice = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
251 $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Not Null)";
253 elseif ($choice == 2) {
254 $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Null)";
258 $clause = $this->whereClause($field,
260 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
261 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
262 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
268 if (!empty($clause)) {
269 $clauses[] = $clause;
275 if (empty($clauses)) {
276 $this->_where
.= " ";
279 $this->_where
.= " AND " . implode(' AND ', $clauses);
283 public function groupBy() {
284 $this->_groupBy
= '';
285 if ($this->has_grouping
) {
287 "{$this->_aliases['civicrm_contact']}.id",
288 "civicrm_activity_activity_date_time",
290 if ($this->has_activity_type
) {
291 $groupBy[] = "{$this->_aliases['civicrm_activity']}.activity_type_id";
294 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
298 public function orderBy() {
299 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id";
302 public function postProcess() {
303 parent
::postProcess();
313 public static function formRule($fields, $files, $self) {
315 if (!empty($fields['group_bys']) &&
316 (!array_key_exists('id', $fields['fields']) ||
317 !array_key_exists('activity_date_time', $fields['fields']) ||
318 !array_key_exists('duration', $fields['fields']))
320 $errors['fields'] = ts('To view totals please select all of activity id, date and duration.');
326 * Alter display of rows.
328 * Iterate through the rows retrieved via SQL and make changes for display purposes,
329 * such as rendering contacts as links.
332 * Rows generated by SQL, with an array for each row.
334 public function alterDisplay(&$rows) {
337 foreach ($rows as $rowNum => $row) {
339 if (isset($row['civicrm_activity_activity_type_id'])) {
341 $val = $row['civicrm_activity_activity_type_id'];
342 $rows[$rowNum]['civicrm_activity_activity_type_id'] = isset($this->activityTypes
[$val]) ?
$this->activityTypes
[$val] : '';
345 if (isset($row['civicrm_activity_status_id'])) {
347 $val = $row['civicrm_activity_status_id'];
348 $rows[$rowNum]['civicrm_activity_status_id'] = isset($this->activityStatuses
[$val]) ?
$this->activityStatuses
[$val] : '';
351 // The next two make it easier to make pivot tables after exporting to Excel
352 if (isset($row['civicrm_activity_duration'])) {
354 $rows[$rowNum]['civicrm_activity_duration'] = (int) $row['civicrm_activity_duration'];
357 if (isset($row['civicrm_case_activity_case_id'])) {
359 $rows[$rowNum]['civicrm_case_activity_case_id'] = (int) $row['civicrm_case_activity_case_id'];