diff --git a/CRM/Activity/Page/AJAX.php b/CRM/Activity/Page/AJAX.php
index f61df22bb478..bd6cffe3f6cd 100644
--- a/CRM/Activity/Page/AJAX.php
+++ b/CRM/Activity/Page/AJAX.php
@@ -88,7 +88,7 @@ public static function getCaseClientRelationships() {
0, 0, 0, NULL, NULL, FALSE
);
- $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
+ $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID, NULL, FALSE);
// Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
// so they don't show up twice.
@@ -136,7 +136,7 @@ public static function getCaseRoles() {
$params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
- $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
+ $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID, NULL, FALSE, $params);
$caseTypeName = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
$xmlProcessor = new CRM_Case_XMLProcessor_Process();
$caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
@@ -162,9 +162,11 @@ public static function getCaseRoles() {
$rel = [];
$rel['relation'] = $value;
$rel['relation_type'] = $id;
- $rel['name'] = '(not assigned)';
+ $rel['sort_name'] = '(not assigned)';
$rel['phone'] = '';
$rel['email'] = '';
+ $rel['is_active'] = '';
+ $rel['end_date'] = '';
$rel['source'] = 'caseRoles';
$caseRelationships[] = $rel;
}
@@ -172,7 +174,7 @@ public static function getCaseRoles() {
foreach ($value as $clientRole) {
$relClient = [];
$relClient['relation'] = ts('Client');
- $relClient['name'] = $clientRole['sort_name'];
+ $relClient['sort_name'] = $clientRole['sort_name'];
$relClient['phone'] = $clientRole['phone'];
$relClient['email'] = $clientRole['email'];
$relClient['cid'] = $clientRole['contact_id'];
@@ -196,6 +198,12 @@ public static function getCaseRoles() {
// set user name, email and edit columns links
foreach ($caseRelationships as $key => &$row) {
+ // add disabled class if role is inactive
+ if (isset($row['is_active'])) {
+ if ($row['is_active'] == '0') {
+ $row['DT_RowClass'] = 'disabled';
+ }
+ }
$typeLabel = $row['relation'];
// Add "
(Case Manager)" to label
if (!empty($row['relation_type']) && !empty($row['relationship_direction']) && $row['relation_type'] . '_' . $row['relationship_direction'] == $managerRoleId) {
@@ -203,13 +211,26 @@ public static function getCaseRoles() {
}
// view user links
if (!empty($row['cid'])) {
- $row['name'] = '' . $row['name'] . '';
+ $row['sort_name'] = '' . $row['sort_name'] . '';
}
// email column links/icon
if ($row['email']) {
$row['email'] = '';
}
+
+ // view end date if set
+ if (!empty($row['end_date'])) {
+ $row['end_date'] = CRM_Utils_Date::customFormat($row['end_date']);
+ // add disabled class if end date is less than equal to current date.
+ if (CRM_Utils_Date::overdue($row['end_date'])) {
+ $row['DT_RowClass'] = 'disabled';
+ }
+ }
+ else {
+ $row['end_date'] = '';
+ }
+
// edit links
$row['actions'] = '';
if ($hasAccessToAllCases) {
@@ -217,12 +238,14 @@ public static function getCaseRoles() {
$contactType = $contactType == 'Contact' ? '' : $contactType;
switch ($row['source']) {
case 'caseRel':
- $row['actions'] = '' .
- '' .
- '' .
- '' .
- '' .
- '';
+ if (empty($row['end_date'])) {
+ $row['actions'] = '' .
+ '' .
+ '' .
+ '' .
+ '' .
+ '';
+ }
break;
case 'caseRoles':
diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php
index f4671b98b98f..5f3b2586efa6 100644
--- a/CRM/Case/BAO/Case.php
+++ b/CRM/Case/BAO/Case.php
@@ -764,18 +764,21 @@ public static function getCasesSummary($allCases = TRUE) {
* Case id.
* @param int $relationshipID
* @param bool $activeOnly
+ * @param array $caseParams
*
* @return array
* case role / relationships
*
*/
- public static function getCaseRoles($contactID, $caseID, $relationshipID = NULL, $activeOnly = TRUE) {
+ public static function getCaseRoles($contactID, $caseID, $relationshipID = NULL, $activeOnly = TRUE, $caseParams = []) {
$query = '
SELECT rel.id as civicrm_relationship_id,
con.sort_name as sort_name,
civicrm_email.email as email,
civicrm_phone.phone as phone,
con.id as civicrm_contact_id,
+ rel.is_active as is_active,
+ rel.end_date as end_date,
IF(rel.contact_id_a = %1, civicrm_relationship_type.label_a_b, civicrm_relationship_type.label_b_a) as relation,
civicrm_relationship_type.id as relation_type,
IF(rel.contact_id_a = %1, "a_b", "b_a") as relationship_direction
@@ -800,6 +803,24 @@ public static function getCaseRoles($contactID, $caseID, $relationshipID = NULL,
$query .= ' AND rel.id = %3 ';
$params[3] = [$relationshipID, 'Integer'];
}
+
+ $sortBy = $caseParams['sortBy'] ?? NULL;
+ if ($sortBy) {
+ $sortBy = CRM_Utils_Type::escape($sortBy, 'String');
+ $query .= " ORDER BY $sortBy ";
+ }
+
+ $page = $caseParams['page'] ?? NULL;
+ $rp = $caseParams['rp'] ?? NULL;
+
+ if (!$page) {
+ $page = 1;
+ }
+ if (!$rp) {
+ $rp = 10;
+ }
+ $start = (($page - 1) * $rp);
+ $query .= " LIMIT $start, $rp";
$dao = CRM_Core_DAO::executeQuery($query, $params);
$values = [];
@@ -807,9 +828,11 @@ public static function getCaseRoles($contactID, $caseID, $relationshipID = NULL,
$rid = $dao->civicrm_relationship_id;
$values[$rid]['cid'] = $dao->civicrm_contact_id;
$values[$rid]['relation'] = $dao->relation;
- $values[$rid]['name'] = $dao->sort_name;
+ $values[$rid]['sort_name'] = $dao->sort_name;
$values[$rid]['email'] = $dao->email;
$values[$rid]['phone'] = $dao->phone;
+ $values[$rid]['is_active'] = $dao->is_active;
+ $values[$rid]['end_date'] = $dao->end_date;
$values[$rid]['relation_type'] = $dao->relation_type;
$values[$rid]['rel_id'] = $dao->civicrm_relationship_id;
$values[$rid]['client_id'] = $contactID;
@@ -1723,20 +1746,24 @@ public static function getCaseManagerContact($caseType, $caseId) {
if (substr($managerRoleId, -4) == '_a_b') {
$managerRoleQuery = "
SELECT civicrm_contact.id as casemanager_id,
- civicrm_contact.sort_name as casemanager
+ civicrm_contact.sort_name as casemanager,
+ civicrm_relationship.is_active,
+ civicrm_relationship.end_date
FROM civicrm_contact
LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = civicrm_contact.id AND civicrm_relationship.relationship_type_id = %1) AND civicrm_relationship.is_active
LEFT JOIN civicrm_case ON civicrm_case.id = civicrm_relationship.case_id
- WHERE civicrm_case.id = %2 AND is_active = 1";
+ WHERE civicrm_case.id = %2";
}
if (substr($managerRoleId, -4) == '_b_a') {
$managerRoleQuery = "
SELECT civicrm_contact.id as casemanager_id,
- civicrm_contact.sort_name as casemanager
+ civicrm_contact.sort_name as casemanager,
+ civicrm_relationship.is_active,
+ civicrm_relationship.end_date
FROM civicrm_contact
LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = civicrm_contact.id AND civicrm_relationship.relationship_type_id = %1) AND civicrm_relationship.is_active
LEFT JOIN civicrm_case ON civicrm_case.id = civicrm_relationship.case_id
- WHERE civicrm_case.id = %2 AND is_active = 1";
+ WHERE civicrm_case.id = %2";
}
$managerRoleParams = [
@@ -1745,10 +1772,28 @@ public static function getCaseManagerContact($caseType, $caseId) {
];
$dao = CRM_Core_DAO::executeQuery($managerRoleQuery, $managerRoleParams);
- if ($dao->fetch()) {
+ // Pull an array of ALL case managers related to the case.
+ $caseManagerNameArray = [];
+ while ($dao->fetch()) {
+ $caseManagerNameArray[$dao->casemanager_id]['casemanager_id'] = $dao->casemanager_id;
+ $caseManagerNameArray[$dao->casemanager_id]['is_active'] = $dao->is_active;
+ $caseManagerNameArray[$dao->casemanager_id]['end_date'] = $dao->end_date;
+ $caseManagerNameArray[$dao->casemanager_id]['casemanager'] = $dao->casemanager;
+ }
+
+ // Look for an active case manager, when no active case manager (like a closed case) show the most recently expired case manager.
+ // Get the index of the manager if set to active
+ $activekey = array_search(1, array_combine(array_keys($caseManagerNameArray), array_column($caseManagerNameArray, 'is_active')));
+ if (!empty($activekey)) {
+ $caseManagerName = sprintf('%s',
+ CRM_Utils_System::url('civicrm/contact/view', ['cid' => $activekey]), $caseManagerNameArray[$activekey]['casemanager']
+ );
+ }
+ else {
+ // if there is no active case manager, get the index of the most recent end_date
+ $max = array_search(max(array_combine(array_keys($caseManagerNameArray), array_column($caseManagerNameArray, 'end_date'))), array_combine(array_keys($caseManagerNameArray), array_column($caseManagerNameArray, 'end_date')));
$caseManagerName = sprintf('%s',
- CRM_Utils_System::url('civicrm/contact/view', ['cid' => $dao->casemanager_id]),
- $dao->casemanager
+ CRM_Utils_System::url('civicrm/contact/view', ['cid' => $max]), $caseManagerNameArray[$max]['casemanager']
);
}
}
diff --git a/CRM/Case/Form/CaseView.php b/CRM/Case/Form/CaseView.php
index 9e71a0cd1a6b..45a659fcd075 100644
--- a/CRM/Case/Form/CaseView.php
+++ b/CRM/Case/Form/CaseView.php
@@ -78,12 +78,18 @@ public function preProcess() {
$statuses = CRM_Case_PseudoConstant::caseStatus('label', FALSE);
$caseTypeName = CRM_Case_BAO_Case::getCaseType($this->_caseID, 'name');
$caseType = CRM_Case_BAO_Case::getCaseType($this->_caseID);
+ $statusClass = civicrm_api3('OptionValue', 'getsingle', [
+ 'option_group_id' => "case_status",
+ 'value' => $values['case_status_id'],
+ 'return' => 'grouping',
+ ]);
$this->_caseDetails = [
'case_type' => $caseType,
'case_status' => $statuses[$values['case_status_id']] ?? NULL,
'case_subject' => $values['subject'] ?? NULL,
'case_start_date' => $values['case_start_date'],
+ 'status_class' => $statusClass['grouping'],
];
$this->_caseType = $caseTypeName;
$this->assign('caseDetails', $this->_caseDetails);
@@ -286,7 +292,7 @@ public function buildQuickForm() {
self::activityForm($this, $aTypes);
//get case related relationships (Case Role)
- $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($this->_contactID, $this->_caseID);
+ $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($this->_contactID, $this->_caseID, NULL, FALSE);
//save special label because we unset it in the loop
$managerLabel = empty($managerRoleId) ? '' : $caseRoles[$managerRoleId];
@@ -467,7 +473,7 @@ public static function activityForm($form, $aTypes = []) {
//build reporter select
$reporters = ["" => ts(' - any reporter - ')];
foreach ($caseRelationships as $key => & $value) {
- $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
+ $reporters[$value['cid']] = $value['sort_name'] . " ( {$value['relation']} )";
}
$form->add('select', 'reporter_id', ts('Reporter/Role'), $reporters, FALSE, ['id' => 'reporter_id_' . $form->_caseID]);
diff --git a/templates/CRM/Case/Form/CaseView.tpl b/templates/CRM/Case/Form/CaseView.tpl
index 1301ac5b3637..ca42afc3f858 100644
--- a/templates/CRM/Case/Form/CaseView.tpl
+++ b/templates/CRM/Case/Form/CaseView.tpl
@@ -150,14 +150,40 @@
{ts}Case Role{/ts} | -{ts}Name{/ts} | +{ts}Name{/ts} | {ts}Phone{/ts} | {ts}Email{/ts} | +{ts}End Date{/ts} | {if $relId neq 'client' and $hasAccessToAllCases}{ts}Actions{/ts} | {/if} @@ -170,7 +196,17 @@ var caseId = {/literal}{$caseID}{literal}; CRM.$('table#caseRoles-selector-' + caseId).data({ "ajax": { - "url": {/literal}'{crmURL p="civicrm/ajax/caseroles" h=0 q="snippet=4&caseID=$caseId&cid=$contactID&userID=$userID"}'{literal} + "url": {/literal}'{crmURL p="civicrm/ajax/caseroles" h=0 q="snippet=4&caseID=$caseId&cid=$contactID&userID=$userID"}'{literal}, + "complete" : function(){ + if (CRM.$('input[type=checkbox][id=role_inactive]').prop('checked')) { + CRM.$('[id^=caseRoles-selector] tbody tr').not('.disabled').hide(); + CRM.$('[id^=caseRoles-selector] tbody tr.disabled').show(); + } + else { + CRM.$('[id^=caseRoles-selector] tbody tr').not('.disabled').show(); + CRM.$('[id^=caseRoles-selector] tbody tr.disabled').hide(); + } + } } }); })(CRM.$);
---|