CRM-20355 - Add Case.addtimeline action
authorColeman Watts <coleman@civicrm.org>
Wed, 29 Mar 2017 01:28:57 +0000 (21:28 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 29 Mar 2017 03:21:15 +0000 (23:21 -0400)
CRM/Case/Form/CaseView.php
api/v3/Case.php
tests/phpunit/api/v3/CaseTest.php

index 00813ca08cd61b535a4c15b4369fe7b663d93ede..fac08c11a27f7b47f6ba28941976bbaacb5bcd47 100644 (file)
@@ -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');
index 3c6c85513139b6b22e34411496878c0614ff0f2a..143e65ae1433649a25663cf9b4dde5e8b12a40ca 100644 (file)
@@ -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.
  *
index bde930a2226d3eeb3990b11d5ab399a04b0fd289..f178ed0e573bf03f67614f424e4d41e54c1f2e45 100644 (file)
@@ -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']);
+  }
+
 }