From ae76ce5e9221e5fa1bcd2ffa7d2c2a63ed320a6f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 28 Mar 2017 21:28:57 -0400 Subject: [PATCH] CRM-20355 - Add Case.addtimeline action --- CRM/Case/Form/CaseView.php | 18 +++------ api/v3/Case.php | 57 ++++++++++++++++++++++++++ tests/phpunit/api/v3/CaseTest.php | 66 +++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 13 deletions(-) diff --git a/CRM/Case/Form/CaseView.php b/CRM/Case/Form/CaseView.php index 00813ca08c..fac08c11a2 100644 --- a/CRM/Case/Form/CaseView.php +++ b/CRM/Case/Form/CaseView.php @@ -424,21 +424,13 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form { $session->pushUserContext($url); if (!empty($params['timeline_id']) && !empty($_POST['_qf_CaseView_next'])) { - $session = CRM_Core_Session::singleton(); - $this->_uid = $session->get('userID'); + civicrm_api3('Case', 'addtimeline', array( + 'case_id' => $this->_caseID, + 'timeline' => $params['timeline_id'], + )); + $xmlProcessor = new CRM_Case_XMLProcessor_Process(); - $xmlProcessorParams = array( - 'clientID' => $this->_contactID, - 'creatorID' => $this->_uid, - 'standardTimeline' => 0, - 'activity_date_time' => date('YmdHis'), - 'caseID' => $this->_caseID, - 'caseType' => $this->_caseType, - 'activitySetName' => $params['timeline_id'], - ); - $xmlProcessor->run($this->_caseType, $xmlProcessorParams); $reports = $xmlProcessor->get($this->_caseType, 'ActivitySets'); - CRM_Core_Session::setStatus(ts('Activities from the %1 activity set have been added to this case.', array(1 => $reports[$params['timeline_id']]) ), ts('Done'), 'success'); diff --git a/api/v3/Case.php b/api/v3/Case.php index 3c6c855131..143e65ae14 100644 --- a/api/v3/Case.php +++ b/api/v3/Case.php @@ -341,6 +341,63 @@ function civicrm_api3_case_activity_create($params) { ); } +/** + * Add a timeline to a case. + * + * @param array $params + * + * @throws API_Exception + * @return array + */ +function civicrm_api3_case_addtimeline($params) { + $caseType = CRM_Case_BAO_Case::getCaseType($params['case_id'], 'name'); + $xmlProcessor = new CRM_Case_XMLProcessor_Process(); + $xmlProcessorParams = array( + 'clientID' => CRM_Case_BAO_Case::getCaseClients($params['case_id']), + 'creatorID' => $params['creator_id'], + 'standardTimeline' => 0, + 'activity_date_time' => $params['activity_date_time'], + 'caseID' => $params['case_id'], + 'caseType' => $caseType, + 'activitySetName' => $params['timeline'], + ); + $xmlProcessor->run($caseType, $xmlProcessorParams); + return civicrm_api3_create_success(); +} + +/** + * Adjust Metadata for addtimeline action. + * + * @param array $params + * Array of parameters determined by getfields. + */ +function _civicrm_api3_case_addtimeline_spec(&$params) { + $params['case_id'] = array( + 'title' => 'Case ID', + 'description' => 'Id of case to update', + 'type' => CRM_Utils_Type::T_INT, + 'api.required' => 1, + ); + $params['timeline'] = array( + 'title' => 'Timeline', + 'description' => 'Name of activity set', + 'type' => CRM_Utils_Type::T_STRING, + 'api.required' => 1, + ); + $params['activity_date_time'] = array( + 'api.default' => 'now', + 'title' => 'Activity date time', + 'description' => 'Timeline start date', + 'type' => CRM_Utils_Type::T_DATE, + ); + $params['creator_id'] = array( + 'api.default' => 'user_contact_id', + 'title' => 'Activity creator', + 'description' => 'Contact id of timeline creator', + 'type' => CRM_Utils_Type::T_INT, + ); +} + /** * Declare deprecated api functions. * diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index bde930a222..f178ed0e57 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -631,4 +631,70 @@ class api_v3_CaseTest extends CiviCaseTestCase { $this->assertCount(3, $result['values']); } + /** + * Test the ability to add a timeline to an existing case. + * + * See the case.addtimeline api. + * + * @throws \Exception + */ + public function testCaseAddtimeline() { + $caseSpec = array( + 'title' => 'Application with Definition', + 'name' => 'Application_with_Definition', + 'is_active' => 1, + 'weight' => 4, + 'definition' => array( + 'activityTypes' => array( + array('name' => 'Follow up'), + ), + 'activitySets' => array( + array( + 'name' => 'set1', + 'label' => 'Label 1', + 'timeline' => 1, + 'activityTypes' => array( + array('name' => 'Open Case', 'status' => 'Completed'), + ), + ), + array( + 'name' => 'set2', + 'label' => 'Label 2', + 'timeline' => 1, + 'activityTypes' => array( + array('name' => 'Follow up'), + ), + ), + ), + 'caseRoles' => array( + array('name' => 'Homeless Services Coordinator', 'creator' => 1, 'manager' => 1), + ), + ), + ); + $cid = $this->individualCreate(); + $caseType = $this->callAPISuccess('CaseType', 'create', $caseSpec); + $case = $this->callAPISuccess('Case', 'create', array( + 'case_type_id' => $caseType['id'], + 'contact_id' => $cid, + 'subject' => 'Test case with timeline', + )); + // Created case should only have 1 activity per the spec + $result = $this->callAPISuccessGetSingle('Activity', array('case_id' => $case['id'], 'return' => 'activity_type_id.name')); + $this->assertEquals('Open Case', $result['activity_type_id.name']); + // Add timeline + $timeline = civicrm_api('Case', 'addtimeline', array( + 'case_id' => $case['id'], + 'timeline' => 'set2', + 'version' => 3, + )); + $result = $this->callAPISuccess('Activity', 'get', array( + 'case_id' => $case['id'], + 'return' => 'activity_type_id.name', + 'sequential' => 1, + 'options' => array('sort' => 'id'), + )); + $this->assertEquals(2, $result['count']); + $this->assertEquals('Follow up', $result['values'][1]['activity_type_id.name']); + } + } -- 2.25.1