5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Case_TimeSpent
extends CRM_Report_Form
{
38 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(
47 'dao' => 'CRM_Contact_DAO_Contact',
51 array('title' => ts('Contact ID'),
56 array('title' => ts('Contact Name'),
64 array('title' => ts('Contact Name'),
66 'type' => CRM_Report_Form
::OP_STRING
,
72 'dao' => 'CRM_Activity_DAO_Activity',
75 'source_contact_id' =>
76 array('title' => ts('Contact ID'),
81 array('title' => ts('Activity Type'),
83 'type' => CRM_Utils_Type
::T_STRING
,
85 'activity_date_time' =>
86 array('title' => ts('Activity Date'),
90 array('title' => ts('Activity Status'),
92 'type' => CRM_Utils_Type
::T_STRING
,
95 array('title' => ts('Activity ID'),
99 array('title' => ts('Duration'),
101 'type' => CRM_Utils_Type
::T_INT
,
104 array('title' => ts('Activity Subject'),
110 'activity_date_time' =>
111 //'default' => 'this.month',
113 'operatorType' => CRM_Report_Form
::OP_DATE
,
116 array('title' => ts('Activity Subject'),
117 'operator' => 'like',
119 'activity_type_id' =>
120 array('title' => ts('Activity Type'),
121 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
122 'options' => $this->activityTypes
,
125 array('title' => ts('Activity Status'),
126 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
127 'options' => $this->activityStatuses
,
132 'source_contact_id' =>
133 array('title' => ts('Totals Only'),
138 'civicrm_case_activity' =>
140 'dao' => 'CRM_Case_DAO_CaseActivity',
144 array('title' => ts('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')),
162 parent
::__construct();
167 $this->_columnHeaders
= array();
169 $this->has_grouping
= !empty($this->_params
['group_bys']);
170 $this->has_activity_type
= FALSE;
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')))
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' && $this->has_grouping
) {
197 $select[] = "COUNT({$field['dbAlias']}) AS {$tableName}_{$fieldName}";
198 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = ts('# Activities');
201 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
208 $this->_select
= "SELECT " . implode(', ', $select) . " ";
214 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
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
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";
228 foreach ($this->_columns
as $tableName => $table) {
229 if (array_key_exists('filters', $table)) {
231 foreach ($table['filters'] as $fieldName => $field) {
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
);
238 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to);
241 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
243 // handle special case
244 if ($fieldName == 'case_id_filter') {
245 $choice = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
247 $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Not Null)";
249 elseif ($choice == 2) {
250 $clause = "({$this->_aliases['civicrm_case_activity']}.id Is Null)";
254 $clause = $this->whereClause($field,
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
)
264 if (!empty($clause)) {
265 $clauses[] = $clause;
271 if (empty($clauses)) {
272 $this->_where
.= " ";
275 $this->_where
.= " AND " . implode(' AND ', $clauses);
280 $this->_groupBy
= '';
281 if ($this->has_grouping
) {
283 GROUP BY {$this->_aliases['civicrm_contact']}.id,
285 $this->_groupBy
.= ($this->has_activity_type
) ?
"{$this->_aliases['civicrm_activity']}.activity_type_id, " : "";
286 $this->_groupBy
.= "civicrm_activity_activity_date_time
292 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id";
295 function postProcess() {
296 parent
::postProcess();
299 static function formRule($fields, $files, $self) {
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']))
304 $errors['fields'] = ts('To view totals please select all of activity id, date and duration.');
306 // CRM_Core_Error::debug('xx', print_r($fields, true));
310 function alterDisplay(&$rows) {
311 // custom code to alter rows
314 foreach ($rows as $rowNum => $row) {
316 if (isset($row['civicrm_activity_activity_type_id'])) {
318 $val = $row['civicrm_activity_activity_type_id'];
319 $rows[$rowNum]['civicrm_activity_activity_type_id'] = isset($this->activityTypes
[$val]) ?
$this->activityTypes
[$val] : '';
322 if (isset($row['civicrm_activity_status_id'])) {
324 $val = $row['civicrm_activity_status_id'];
325 $rows[$rowNum]['civicrm_activity_status_id'] = isset($this->activityStatuses
[$val]) ?
$this->activityStatuses
[$val] : '';
328 // The next two make it easier to make pivot tables after exporting to Excel
329 if (isset($row['civicrm_activity_duration'])) {
331 $rows[$rowNum]['civicrm_activity_duration'] = (int) $row['civicrm_activity_duration'];
334 if (isset($row['civicrm_case_activity_case_id'])) {
336 $rows[$rowNum]['civicrm_case_activity_case_id'] = (int) $row['civicrm_case_activity_case_id'];