4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Report_Form_Case_TimeSpent
extends CRM_Report_Form
{
37 function __construct() {
39 $this->activityTypes
= CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
40 asort($this->activityTypes
);
41 $this->activityStatuses
= CRM_Core_PseudoConstant
::activityStatus();
43 $this->_columns
= array(
46 'dao' => 'CRM_Contact_DAO_Contact',
50 array('title' => ts('Contact ID'),
55 array('title' => ts('Contact Name'),
63 array('title' => ts('Contact Name'),
65 'type' => CRM_Report_Form
::OP_STRING
,
71 'dao' => 'CRM_Activity_DAO_Activity',
74 'source_contact_id' =>
75 array('title' => ts('Contact ID'),
80 array('title' => ts('Activity Type'),
82 'type' => CRM_Utils_Type
::T_STRING
,
84 'activity_date_time' =>
85 array('title' => ts('Activity Date'),
89 array('title' => ts('Activity Status'),
91 'type' => CRM_Utils_Type
::T_STRING
,
94 array('title' => ts('Activity ID'),
98 array('title' => ts('Duration'),
100 'type' => CRM_Utils_Type
::T_INT
,
103 array('title' => ts('Activity Subject'),
109 'activity_date_time' =>
110 //'default' => 'this.month',
112 'operatorType' => CRM_Report_Form
::OP_DATE
,
115 array('title' => ts('Activity Subject'),
116 'operator' => 'like',
118 'activity_type_id' =>
119 array('title' => ts('Activity Type'),
120 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
121 'options' => $this->activityTypes
,
124 array('title' => ts('Activity Status'),
125 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
126 'options' => $this->activityStatuses
,
131 'source_contact_id' =>
132 array('title' => ts('Totals Only'),
137 'civicrm_case_activity' =>
139 'dao' => 'CRM_Case_DAO_CaseActivity',
143 array('title' => ts('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')),
161 parent
::__construct();
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 (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')))
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);
184 if ($fieldName == 'activity_type_id') {
185 $this->has_activity_type
= TRUE;
188 if ($fieldName == 'duration' && $this->has_grouping
) {
189 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
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');
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');
200 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
207 $this->_select
= "SELECT " . implode(', ', $select) . " ";
213 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
215 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
216 ON {$this->_aliases['civicrm_activity']}.source_contact_id = {$this->_aliases['civicrm_contact']}.id
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
223 $this->_where
= " WHERE {$this->_aliases['civicrm_activity']}.is_current_revision = 1 AND
224 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
225 {$this->_aliases['civicrm_activity']}.is_test = 0";
227 foreach ($this->_columns
as $tableName => $table) {
228 if (array_key_exists('filters', $table)) {
230 foreach ($table['filters'] as $fieldName => $field) {
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
);
237 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to);
240 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
242 // handle special case
243 if ($fieldName == 'case_id_filter') {
244 $choice = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
246 $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Not Null)";
248 elseif ($choice == 2) {
249 $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Null)";
253 $clause = $this->whereClause($field,
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
)
263 if (!empty($clause)) {
264 $clauses[] = $clause;
270 if (empty($clauses)) {
271 $this->_where
.= " ";
274 $this->_where
.= " AND " . implode(' AND ', $clauses);
279 $this->_groupBy
= '';
280 if ($this->has_grouping
) {
282 GROUP BY {$this->_aliases['civicrm_contact']}.id,
284 $this->_groupBy
.= ($this->has_activity_type
) ?
"{$this->_aliases['civicrm_activity']}.activity_type_id, " : "";
285 $this->_groupBy
.= "civicrm_activity_activity_date_time
291 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id";
294 function postProcess() {
295 parent
::postProcess();
298 static function formRule($fields, $files, $self) {
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']))
303 $errors['fields'] = ts('To view totals please select all of activity id, date and duration.');
305 // CRM_Core_Error::debug('xx', print_r($fields, true));
309 function alterDisplay(&$rows) {
310 // custom code to alter rows
313 foreach ($rows as $rowNum => $row) {
315 if (isset($row['civicrm_activity_activity_type_id'])) {
317 $val = $row['civicrm_activity_activity_type_id'];
318 $rows[$rowNum]['civicrm_activity_activity_type_id'] = isset($this->activityTypes
[$val]) ?
$this->activityTypes
[$val] : '';
321 if (isset($row['civicrm_activity_status_id'])) {
323 $val = $row['civicrm_activity_status_id'];
324 $rows[$rowNum]['civicrm_activity_status_id'] = isset($this->activityStatuses
[$val]) ?
$this->activityStatuses
[$val] : '';
327 // The next two make it easier to make pivot tables after exporting to Excel
328 if (isset($row['civicrm_activity_duration'])) {
330 $rows[$rowNum]['civicrm_activity_duration'] = (int) $row['civicrm_activity_duration'];
333 if (isset($row['civicrm_case_activity_case_id'])) {
335 $rows[$rowNum]['civicrm_case_activity_case_id'] = (int) $row['civicrm_case_activity_case_id'];