$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');
);
}
+/**
+ * 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.
*
$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']);
+ }
+
}