3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 class CRM_Report_Form_Case_TimeSpent
extends CRM_Report_Form
{
40 public function __construct() {
42 $this->activityTypes
= CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
43 asort($this->activityTypes
);
44 $this->activityStatuses
= CRM_Core_PseudoConstant
::activityStatus();
46 $this->_columns
= array(
47 'civicrm_contact' => array(
48 'dao' => 'CRM_Contact_DAO_Contact',
51 'title' => ts('Contact ID'),
56 'title' => ts('Contact Name'),
63 'title' => ts('Contact Name'),
65 'type' => CRM_Report_Form
::OP_STRING
,
69 'civicrm_activity' => array(
70 'dao' => 'CRM_Activity_DAO_Activity',
72 'activity_type_id' => array(
73 'title' => ts('Activity Type'),
75 'type' => CRM_Utils_Type
::T_STRING
,
77 'activity_date_time' => array(
78 'title' => ts('Activity Date'),
82 'title' => ts('Activity Status'),
84 'type' => CRM_Utils_Type
::T_STRING
,
87 'title' => ts('Activity ID'),
91 'title' => ts('Duration'),
93 'type' => CRM_Utils_Type
::T_INT
,
96 'title' => ts('Activity Subject'),
101 'activity_date_time' => array(
102 'operatorType' => CRM_Report_Form
::OP_DATE
,
105 'title' => ts('Activity Subject'),
106 'type' => CRM_Utils_Type
::T_STRING
,
107 'operator' => 'like',
109 'activity_type_id' => array(
110 'title' => ts('Activity Type'),
111 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
112 'options' => $this->activityTypes
,
114 'status_id' => array(
115 'title' => ts('Activity Status'),
116 'type' => CRM_Utils_Type
::T_INT
,
117 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
118 'options' => $this->activityStatuses
,
122 'civicrm_activity_source' => array(
123 'dao' => 'CRM_Activity_DAO_ActivityContact',
125 'contact_id' => array(
126 'title' => ts('Contact ID'),
128 'no_display' => TRUE,
131 'group_bys' => array(
132 'contact_id' => array(
133 'title' => ts('Totals Only'),
137 'grouping' => 'activity-fields',
139 'civicrm_case_activity' => array(
140 'dao' => 'CRM_Case_DAO_CaseActivity',
143 'title' => ts('Case ID'),
148 'case_id_filter' => array(
150 'title' => ts('Cases?'),
151 'operatorType' => CRM_Report_Form
::OP_SELECT
,
153 1 => ts('Exclude non-case'),
154 2 => ts('Exclude cases'),
155 3 => ts('Include Both'),
163 parent
::__construct();
166 public function select() {
168 $this->_columnHeaders
= array();
170 $this->has_grouping
= !empty($this->_params
['group_bys']);
171 $this->has_activity_type
= FALSE;
173 foreach ($this->_columns
as $tableName => $table) {
174 if (array_key_exists('fields', $table)) {
175 foreach ($table['fields'] as $fieldName => $field) {
176 if (!empty($field['required']) ||
177 (!empty($this->_params
['fields'][$fieldName]) &&
178 ((!$this->has_grouping
) ||
179 !in_array($fieldName, array('case_id', 'subject', 'status_id')))
183 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
184 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
185 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
187 if ($fieldName == 'activity_type_id') {
188 $this->has_activity_type
= TRUE;
191 if ($fieldName == 'duration' && $this->has_grouping
) {
192 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
194 elseif ($fieldName == 'activity_date_time' && $this->has_grouping
) {
195 $select[] = "EXTRACT(YEAR_MONTH FROM {$field['dbAlias']}) AS {$tableName}_{$fieldName}";
196 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = ts('Month/Year');
198 elseif ($tableName == 'civicrm_activity' && $fieldName == 'id' &&
201 $select[] = "COUNT({$field['dbAlias']}) AS {$tableName}_{$fieldName}";
202 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = ts('# Activities');
205 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
211 $this->_selectClauses
= $select;
213 $this->_select
= "SELECT " . implode(', ', $select) . " ";
216 public function from() {
219 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
220 LEFT JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_source']}
221 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_source']}.activity_id
222 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
223 ON {$this->_aliases['civicrm_activity_source']}.contact_id = {$this->_aliases['civicrm_contact']}.id
224 LEFT JOIN civicrm_case_activity {$this->_aliases['civicrm_case_activity']}
225 ON {$this->_aliases['civicrm_case_activity']}.activity_id = {$this->_aliases['civicrm_activity']}.id
229 public function where() {
230 $this->_where
= " WHERE {$this->_aliases['civicrm_activity']}.is_current_revision = 1 AND
231 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
232 {$this->_aliases['civicrm_activity']}.is_test = 0";
234 foreach ($this->_columns
as $tableName => $table) {
235 if (array_key_exists('filters', $table)) {
237 foreach ($table['filters'] as $fieldName => $field) {
239 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
240 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
241 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
242 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
244 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to);
247 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
249 // handle special case
250 if ($fieldName == 'case_id_filter') {
251 $choice = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
253 $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Not Null)";
255 elseif ($choice == 2) {
256 $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Null)";
260 $clause = $this->whereClause($field,
262 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
263 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
264 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
270 if (!empty($clause)) {
271 $clauses[] = $clause;
277 if (empty($clauses)) {
278 $this->_where
.= " ";
281 $this->_where
.= " AND " . implode(' AND ', $clauses);
285 public function groupBy() {
286 $this->_groupBy
= '';
287 if ($this->has_grouping
) {
289 "{$this->_aliases['civicrm_contact']}.id",
290 "civicrm_activity_activity_date_time"
292 if ($this->has_activity_type
) {
293 $groupBy[] = "{$this->_aliases['civicrm_activity']}.activity_type_id";
296 $this->_groupBy
= "GROUP BY " . implode(', ', $groupBy);
297 $this->_groupBy
.= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
301 public function orderBy() {
302 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id";
305 public function postProcess() {
306 parent
::postProcess();
316 public static function formRule($fields, $files, $self) {
318 if (!empty($fields['group_bys']) &&
319 (!array_key_exists('id', $fields['fields']) ||
320 !array_key_exists('activity_date_time', $fields['fields']) ||
321 !array_key_exists('duration', $fields['fields']))
323 $errors['fields'] = ts('To view totals please select all of activity id, date and duration.');
329 * Alter display of rows.
331 * Iterate through the rows retrieved via SQL and make changes for display purposes,
332 * such as rendering contacts as links.
335 * Rows generated by SQL, with an array for each row.
337 public function alterDisplay(&$rows) {
340 foreach ($rows as $rowNum => $row) {
342 if (isset($row['civicrm_activity_activity_type_id'])) {
344 $val = $row['civicrm_activity_activity_type_id'];
345 $rows[$rowNum]['civicrm_activity_activity_type_id'] = isset($this->activityTypes
[$val]) ?
$this->activityTypes
[$val] : '';
348 if (isset($row['civicrm_activity_status_id'])) {
350 $val = $row['civicrm_activity_status_id'];
351 $rows[$rowNum]['civicrm_activity_status_id'] = isset($this->activityStatuses
[$val]) ?
$this->activityStatuses
[$val] : '';
354 // The next two make it easier to make pivot tables after exporting to Excel
355 if (isset($row['civicrm_activity_duration'])) {
357 $rows[$rowNum]['civicrm_activity_duration'] = (int) $row['civicrm_activity_duration'];
360 if (isset($row['civicrm_case_activity_case_id'])) {
362 $rows[$rowNum]['civicrm_case_activity_case_id'] = (int) $row['civicrm_case_activity_case_id'];