Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle Case Manager and roles on closed cases #13510

Closed
wants to merge 12 commits into from
41 changes: 31 additions & 10 deletions CRM/Activity/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,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);
$caseTypeName = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
$xmlProcessor = new CRM_Case_XMLProcessor_Process();
$caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
Expand Down Expand Up @@ -179,6 +179,8 @@ public static function getCaseRoles() {
$rel['name'] = '(not assigned)';
$rel['phone'] = '';
$rel['email'] = '';
$rel['is_active'] = '';
$rel['end_date'] = '';
$rel['source'] = 'caseRoles';
$caseRelationships[] = $rel;
}
Expand All @@ -198,15 +200,25 @@ public static function getCaseRoles() {

// sort clientRelationships array using jquery call params
foreach ($caseRelationships as $key => $row) {
$sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
if (isset($row[$params['_raw_values']['sort'][0]])) {
$sortArray[$key] = $row[$params['_raw_values']['sort'][0]]; // FIXME: this line is throwing php notices (undefined index). I added an isset above and while it throws less erros - they are still present.
}
}
if (!empty($row[$params['_raw_values']['sort'][0]])) {
$sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
array_multisort($sortArray, constant($sort_type), $caseRelationships); // FIXME: this line is throwing php notices (undefined index). I added an isset above and while it throws less erros - they are still present.
}
$sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
array_multisort($sortArray, constant($sort_type), $caseRelationships);

$relationships = array();

// 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']) && $row['relation_type'] == $managerRoleId) {
Expand All @@ -221,19 +233,28 @@ public static function getCaseRoles() {
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"></i></a>';
}
// view end date if set
if (!empty($row['end_date'])) {
$row['end_date'] = date("F d, Y", strtotime($row['end_date']));
}
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', array(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"></i>' .
'</a>' .
'<a href="#deleteCaseRoleDialog" title="' . ts('Remove %1', array(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', array(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"></i>' .
'</a>' .
'<a href="#deleteCaseRoleDialog" title="' . ts('Expire %1', array(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="crm-i fa-ban"></span>' .
'</a>';
}
break;

case 'caseRoles':
Expand Down
30 changes: 26 additions & 4 deletions CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ public static function getCaseRoles($contactID, $caseID, $relationshipID = NULL,
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 Down Expand Up @@ -890,6 +892,8 @@ public static function getCaseRoles($contactID, $caseID, $relationshipID = NULL,
$values[$rid]['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 @@ -1919,18 +1923,36 @@ public static function getCaseManagerContact($caseType, $caseId) {
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";

$managerRoleParams = array(
1 => array($managerRoleId, 'Integer'),
2 => array($caseId, 'Integer'),
);

$dao = CRM_Core_DAO::executeQuery($managerRoleQuery, $managerRoleParams);
if ($dao->fetch()) {
// Pull an array of ALL case managers related to the case.
$caseManagerNameArray = array();
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;
}
$dao->free();
// 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', array('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', array('cid' => $dao->casemanager_id)),
$dao->casemanager
CRM_Utils_System::url('civicrm/contact/view', array('cid' => $max)), $caseManagerNameArray[$max]['casemanager']
);
}
}
Expand Down
9 changes: 8 additions & 1 deletion CRM/Case/Form/CaseView.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,20 @@ 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 = array(
'case_type' => $caseType,
'case_status' => CRM_Utils_Array::value($values['case_status_id'], $statuses),
'case_subject' => CRM_Utils_Array::value('subject', $values),
'case_start_date' => $values['case_start_date'],
'status_class' => $statusClass['grouping'],
);

$this->_caseType = $caseTypeName;
$this->assign('caseDetails', $this->_caseDetails);

Expand Down Expand Up @@ -460,7 +467,7 @@ public function postProcess() {
* To include acivities related to current case id $form->_caseID.
*/
public static function activityForm($form, $aTypes = array()) {
$caseRelationships = CRM_Case_BAO_Case::getCaseRoles($form->_contactID, $form->_caseID);
$caseRelationships = CRM_Case_BAO_Case::getCaseRoles($form->_contactID, $form->_caseID, NULL, FALSE);
//build reporter select
$reporters = array("" => ts(' - any reporter - '));
foreach ($caseRelationships as $key => & $value) {
Expand Down
31 changes: 30 additions & 1 deletion templates/CRM/Case/Form/CaseView.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,32 @@
<div><input name="edit_role_contact_id" placeholder="{ts}- select contact -{/ts}" class="huge" /></div>
</div>

<div id="caseRoles-selector-show-inactive">
{assign var=check value='Show inactive relationships'} <!-- label for check box -->
{if $caseDetails.status_class eq 'Opened'}{assign var=statusclass value='1'}{else}{assign var=statusclass value='0'}{/if} <!-- assign the 0 if the case is closed, this will preselect the checkbox as desired -->
{html_checkboxes name='inactive' options=$check selected=$statusclass}
</div>
{literal}
<script type="text/javascript">
(function($) {
$('input[type=checkbox][name*=inactive]').change(function() {
if (this.checked == true) {
CRM.$('[id^=caseRoles-selector] tr.disabled').css('display','');
} else if (this.checked == false) {
CRM.$('[id^=caseRoles-selector] tr.disabled').css('display','none');
}
});
})(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="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 @@ -184,17 +203,27 @@
<script type="text/javascript">
(function($) {
var caseId = {/literal}{$caseID}{literal};
var statusClass = {/literal}{$statusclass}{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}
"complete" : function(){
if (statusClass == 1) {
CRM.$('[id^=caseRoles-selector] tr.disabled').css('display','none');
}
if (!$('[id^=caseRoles-selector] tr').hasClass("disabled")) {
$('#show-inactive label').addClass('disabled');
$('input[type=checkbox][name*=inactive]').prop('disabled', true);
}
}
}
});
})(CRM.$);
</script>
{/literal}

<div id="deleteCaseRoleDialog" class="hiddenElement">
{ts}Are you sure you want to end this relationship?{/ts}
{ts}Are you sure you want to expire this relationship?{/ts}
</div>

</div><!-- /.crm-accordion-body -->
Expand Down