Skip to content

Commit

Permalink
show inactive role on closed case, and give option to active role on …
Browse files Browse the repository at this point in the history
…case
  • Loading branch information
sunilpawar committed Jun 9, 2021
1 parent 16d5ffe commit 27d7688
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 26 deletions.
47 changes: 35 additions & 12 deletions CRM/Activity/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
Expand All @@ -162,17 +162,19 @@ 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;
}
else {
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'];
Expand All @@ -196,33 +198,54 @@ 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 "<br />(Case Manager)" to label
if (!empty($row['relation_type']) && !empty($row['relationship_direction']) && $row['relation_type'] . '_' . $row['relationship_direction'] == $managerRoleId) {
$row['relation'] .= '<br />' . '(' . ts('Case Manager') . ')';
}
// view user links
if (!empty($row['cid'])) {
$row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System::url('civicrm/contact/view',
'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>';
$row['sort_name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System::url('civicrm/contact/view',
'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['sort_name'] . '</a>';
}
// email column links/icon
if ($row['email']) {
$row['email'] = '<a class="crm-hover-button crm-popup" href="' . CRM_Utils_System::url('civicrm/activity/email/add', 'reset=1&action=add&atype=3&cid=' . $row['cid']) . '&caseid=' . $caseID . '" title="' . ts('Send an Email') . '"><i class="crm-i fa-envelope" aria-hidden="true"></i></a>';
}

// 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) {
$contactType = empty($row['relation_type']) ? '' : (string) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b');
$contactType = $contactType == 'Contact' ? '' : $contactType;
switch ($row['source']) {
case 'caseRel':
$row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Reassign %1', [1 => $typeLabel]) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-rel_id="' . $row['rel_id'] . '"data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
'<i class="crm-i fa-pencil" aria-hidden="true"></i>' .
'</a>' .
'<a href="#deleteCaseRoleDialog" title="' . ts('Remove %1', [1 => $typeLabel]) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/delcaserole') . '">' .
'<span class="icon delete-icon"></span>' .
'</a>';
if (empty($row['end_date'])) {
$row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Reassign %1', [1 => $typeLabel]) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-rel_id="' . $row['rel_id'] . '"data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
'<i class="crm-i fa-pencil" aria-hidden="true"></i>' .
'</a>' .
'<a href="#deleteCaseRoleDialog" title="' . ts('Remove %1', [1 => $typeLabel]) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/delcaserole') . '">' .
'<span class="icon delete-icon"></span>' .
'</a>';
}
break;

case 'caseRoles':
Expand Down
63 changes: 54 additions & 9 deletions CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -800,16 +803,36 @@ 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 = [];
while ($dao->fetch()) {
$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;
Expand Down Expand Up @@ -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 = [
Expand All @@ -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('<a href="%s">%s</a>',
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('<a href="%s">%s</a>',
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']
);
}
}
Expand Down
10 changes: 8 additions & 2 deletions CRM/Case/Form/CaseView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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]);

Expand Down
42 changes: 39 additions & 3 deletions templates/CRM/Case/Form/CaseView.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,40 @@
<div><label for="edit_role_contact_id">{ts}Change To{/ts}:</label></div>
<div><input name="edit_role_contact_id" placeholder="{ts}- select contact -{/ts}" class="huge" /></div>
</div>

<div id="caseRoles-selector-show-active">
{* add checkbox to show only active role on case, default value is unchecked (it show all roles) *}
{if $caseDetails.status_class eq 'Opened'}{assign var=statusclass value='0'}{else}{assign var=statusclass value='1'}{/if}
<label><input type="checkbox" id="role_inactive" name="role_inactive[]">{ts}Show Inactive relationships{/ts}</label>
</div>
{literal}
<script type="text/javascript">
(function($) {
var statusClass = {/literal}{$statusclass}{literal};
// for closed cases, 'Show Inactive relationships' checkbox is checked on page load.
if (statusClass) {
CRM.$("#role_inactive").prop("checked", true);
}
// hide the inactive role when checkbox is checked
$('input[type=checkbox][id=role_inactive]').change(function() {
if (this.checked == true) {
CRM.$('[id^=caseRoles-selector] tbody tr').not('.disabled').hide();
CRM.$('[id^=caseRoles-selector] tbody tr.disabled').show();
} else if (this.checked == false) {
CRM.$('[id^=caseRoles-selector] tbody tr').not('.disabled').show();
CRM.$('[id^=caseRoles-selector] tbody tr.disabled').hide();
}
});
})(CRM.$);
</script>
{/literal}
<table id="caseRoles-selector-{$caseID}" class="report-layout crm-ajax-table" data-page-length="10">
<thead>
<tr>
<th data-data="relation">{ts}Case Role{/ts}</th>
<th data-data="name">{ts}Name{/ts}</th>
<th data-data="sort_name">{ts}Name{/ts}</th>
<th data-data="phone">{ts}Phone{/ts}</th>
<th data-data="email">{ts}Email{/ts}</th>
<th data-data="end_date">{ts}End Date{/ts}</th>
{if $relId neq 'client' and $hasAccessToAllCases}
<th data-data="actions" data-orderable="false">{ts}Actions{/ts}</th>
{/if}
Expand All @@ -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.$);
Expand Down

0 comments on commit 27d7688

Please sign in to comment.