fix recently performed activities on case dashboard
authordemeritcowboy <demeritcowboy@hotmail.com>
Wed, 29 Mar 2023 21:11:46 +0000 (17:11 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Wed, 29 Mar 2023 21:11:46 +0000 (17:11 -0400)
CRM/Case/BAO/Case.php
tests/phpunit/CRM/Case/BAO/CaseTest.php

index f04689a192dbb0deac6216b5aff6bd779a501e56..43e6435dc60e3f527f7c9c228a057e4140a8039f 100644 (file)
@@ -588,12 +588,13 @@ HERESQL;
       return $getCount ? 0 : $casesList;
     }
 
+    $type = $params['type'] ?? 'upcoming';
+
     // Return cached value instead of re-running query
-    if (isset(Civi::$statics[__CLASS__]['totalCount']) && $getCount) {
-      return Civi::$statics[__CLASS__]['totalCount'];
+    if (isset(Civi::$statics[__CLASS__]['totalCount'][$type]) && $getCount) {
+      return Civi::$statics[__CLASS__]['totalCount'][$type];
     }
 
-    $type = CRM_Utils_Array::value('type', $params, 'upcoming');
     $userID = CRM_Core_Session::getLoggedInContactID();
 
     // validate access for all cases.
@@ -618,7 +619,7 @@ HERESQL;
     }
     $condition = implode(' AND ', $whereClauses);
 
-    Civi::$statics[__CLASS__]['totalCount'] = $totalCount = CRM_Core_DAO::singleValueQuery(self::getCaseActivityCountQuery($type, $userID, $condition));
+    Civi::$statics[__CLASS__]['totalCount'][$type] = $totalCount = CRM_Core_DAO::singleValueQuery(self::getCaseActivityCountQuery($type, $userID, $condition));
     if ($getCount) {
       return $totalCount;
     }
index a9ba35550c1e6723c123859f794d55c87afbf145..375bcc83e629080ec0b5469e7f1964713a5ea7be 100644 (file)
@@ -1378,4 +1378,29 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test that if there's only recently performed activities in the system
+   * and no future ones then it still shows on dashboard.
+   */
+  public function testOnlyRecent() {
+    $loggedInUserId = $this->createLoggedInUser();
+    $clientId = $this->individualCreate([], 0, TRUE);
+    // old start date so there's no upcoming
+    $caseObj = $this->createCase($clientId, $loggedInUserId, ['start_date' => date('Y-m-d', strtotime('-2 years'))]);
+    // quickie hack to make them all completed
+    CRM_Core_DAO::executeQuery("UPDATE civicrm_case_activity ca INNER JOIN civicrm_activity a ON a.id = ca.activity_id SET a.status_id = 2 WHERE ca.case_id = %1", [1 => [$caseObj->id, 'Integer']]);
+    // Add a recent one
+    $activity = $this->callAPISuccess('Activity', 'create', [
+      'source_contact_id' => $loggedInUserId,
+      'target_contact_id' => $clientId,
+      'activity_type_id' => 'Follow up',
+      'status_id' => 'Completed',
+      'activity_date_time' => date('Y-m-d H:i:s', strtotime('-2 days')),
+      'subject' => 'backdated',
+      'case_id' => $caseObj->id,
+    ]);
+    $this->assertEquals(0, CRM_Case_BAO_Case::getCases(TRUE, ['type' => 'upcoming'], 'dashboard', TRUE));
+    $this->assertEquals(1, CRM_Case_BAO_Case::getCases(TRUE, ['type' => 'recent'], 'dashboard', TRUE));
+  }
+
 }