Commit 6a500e18 authored by ErikHommel's avatar ErikHommel
Browse files

fix issues 5490 and 5479 - invite consistently in all spots

parent 48a79882
<?php
use CRM_Nbrprojectvolunteerlist_ExtensionUtil as E;
/**
* Class to do case activity processing for this extension
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 25 Nov 2019
* @license AGPL-3.0
*/
class CRM_Nbrprojectvolunteerlist_CaseActivity {
/**
* Method to create the a case activity for a study status change
*
* @param $caseIds
*/
public static function addStatusChangeActivities($newStatusLabel, $caseIds) {
$activityTypeId = CRM_Nihrbackbone_BackboneConfig::singleton()->getChangedStudyStatusActivityTypeId();
$optionGroupId = CRM_Nihrbackbone_BackboneConfig::singleton()->getStudyParticipationStatusOptionGroupId();
foreach ($caseIds as $caseId => $caseStatus) {
if ($activityTypeId) {
$currentStatusLabel = CRM_Nihrbackbone_Utils::getOptionValueLabel($caseStatus, $optionGroupId);
try {
civicrm_api3('Activity', 'create', [
'source_contact_id' => "user_contact_id",
'activity_type_id' => $activityTypeId,
'subject' => "Changed from status " . $currentStatusLabel . " to status " . $newStatusLabel,
'case_id' => $caseId,
'status_id' => "Completed",
]);
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->error(E::ts('Could not create a change status activity for case ID ') . $caseId
. E::ts(', error message from API Activity create: ') . $ex->getMessage());
}
}
}
}
}
......@@ -82,20 +82,23 @@ class CRM_Nbrprojectvolunteerlist_Form_Task_ChangeStudyStatus extends CRM_Contac
*/
public function postProcess() {
if (isset($this->_submitValues['nbr_study_status_id'])) {
$statusColumn = CRM_Nihrbackbone_BackboneConfig::singleton()->getParticipationCustomField('nvpd_study_participation_status', 'column_name');
$updateParams = [];
$update = "";
$caseIds = $this->getRelevantCaseIds();
$elements = CRM_Nbrprojectvolunteerlist_Utils::setUpdateParams($caseIds, $statusColumn, $this->_submitValues['nbr_study_status_id'], $updateParams, $update);
if (!empty($elements)) {
$update .= implode(',', $elements) . ")";
CRM_Core_DAO::executeQuery($update, $updateParams);
$newStatusLabel = CRM_Nihrbackbone_Utils::getOptionValueLabel($this->_submitValues['nbr_study_status_id'], CRM_Nihrbackbone_BackboneConfig::singleton()->getStudyParticipationStatusOptionGroupId());
CRM_Nbrprojectvolunteerlist_CaseActivity::addStatusChangeActivities($newStatusLabel, $caseIds);
CRM_Core_Session::setStatus(E::ts('Updated status of selected volunteers on study ') . CRM_Nihrbackbone_NbrStudy::getStudyNumberWithId($this->_studyId)
. E::ts(' to ' . CRM_Nihrbackbone_Utils::getOptionValueLabel($this->_submitValues['nbr_study_status_id'],
CRM_Nihrbackbone_BackboneConfig::singleton()->getStudyParticipationStatusOptionGroupId())), E::ts('Successfully changed status on study'), 'success');
$newStatusLabel = CRM_Nihrbackbone_Utils::getOptionValueLabel($this->_submitValues['nbr_study_status_id'], CRM_Nihrbackbone_BackboneConfig::singleton()->getStudyParticipationStatusOptionGroupId());
foreach ($caseIds as $caseId => $caseData) {
CRM_Nihrbackbone_NbrVolunteerCase::updateStudyStatus($caseId, $caseData['contact_id'], $this->_submitValues['nbr_study_status_id']);
$currentStatusLabel = CRM_Nihrbackbone_Utils::getOptionValueLabel($caseData['study_status_id'], CRM_Nihrbackbone_BackboneConfig::singleton()->getStudyParticipationStatusOptionGroupId());
$activityData = [
'subject' => "Changed from status " . $currentStatusLabel . " to status " . $newStatusLabel,
'status_id' => "Completed",
];
CRM_Nihrbackbone_NbrVolunteerCase::addCaseActivity($caseId, $caseData['contact_id'], CRM_Nihrbackbone_BackboneConfig::singleton()->getChangedStudyStatusActivityTypeId(), $activityData);
// if status is invited, add invite activity too
if ($this->_submitValues['nbr_study_status_id'] == 'study_participation_status_invited') {
CRM_Nihrbackbone_NbrInvitation::addInviteActivity($caseId, $caseData['contact_id'], $this->_studyId, 'Change Case Status Action');
}
}
CRM_Core_Session::setStatus(E::ts('Updated status of selected volunteers on study ') . CRM_Nihrbackbone_NbrStudy::getStudyNumberWithId($this->_studyId)
. E::ts(' to ') . $newStatusLabel, E::ts('Successfully changed status on study'), 'success');
}
}
......@@ -109,17 +112,24 @@ class CRM_Nbrprojectvolunteerlist_Form_Task_ChangeStudyStatus extends CRM_Contac
$participationTable = CRM_Nihrbackbone_BackboneConfig::singleton()->getParticipationDataCustomGroup('table_name');
$studyStatusColumn = CRM_Nihrbackbone_BackboneConfig::singleton()->getParticipationCustomField('nvpd_study_participation_status', 'column_name');
$studyColumn = CRM_Nihrbackbone_BackboneConfig::singleton()->getParticipationCustomField('nvpd_study_id', 'column_name');
$query = "SELECT ccc.case_id, cvnpd. " . $studyStatusColumn . " AS study_status_id
$query = "SELECT ccc.case_id, cvnpd. " . $studyStatusColumn . " AS study_status_id, ccc.contact_id
FROM " . $participationTable. " AS cvnpd
LEFT JOIN civicrm_case_contact AS ccc ON cvnpd.entity_id = ccc.case_id
WHERE cvnpd." . $studyColumn . " = %1 AND ccc.contact_id IN (";
$queryParams = [1 => [$this->_studyId, "Integer"]];
$i = 1;
LEFT JOIN civicrm_case AS cc ON ccc.case_id = cc.id
WHERE cvnpd." . $studyColumn . " = %1 AND cc.is_deleted = %2 AND ccc.contact_id IN (";
$queryParams = [
1 => [$this->_studyId, "Integer"],
2 => [0, "Integer"]
];
$i = 2;
$elements = CRM_Nbrprojectvolunteerlist_Utils::processContactQueryElements($this->_contactIds, $i, $queryParams);
$query .= implode("," , $elements) . ")";
$dao = CRM_Core_DAO::executeQuery($query, $queryParams);
while ($dao->fetch()) {
$caseIds[$dao->case_id] = $dao->study_status_id;
$caseIds[$dao->case_id] = [
'study_status_id' => $dao->study_status_id,
'contact_id' => $dao->contact_id,
];
}
return $caseIds;
}
......
......@@ -189,8 +189,12 @@ class CRM_Nbrprojectvolunteerlist_Form_Task_ExportExternal extends CRM_Contact_F
// add export activity
$caseId = $selectedData['case_id'];
$contactId = $selectedData['contact_id'];
$activityParams = [
'status_id' => 'Completed',
'subject' => 'Exported to External Researcher(s)',
];
$actTypeId = CRM_Nihrbackbone_BackboneConfig::singleton()->getExportExternalActivityTypeId();
CRM_Nihrbackbone_NbrVolunteerCase::addCaseActivity($caseId, $contactId, $actTypeId, 'Completed', 'Exported to External Researcher(s)');
CRM_Nihrbackbone_NbrVolunteerCase::addCaseActivity($caseId, $contactId, $actTypeId, $activityParams);
unset($selectedData['contact_id'], $selectedData['case_id']);
$rows[] = $selectedData;
}
......
......@@ -133,21 +133,7 @@ class CRM_Nbrprojectvolunteerlist_Form_Task_InviteByEmail extends CRM_Contact_Fo
'case_id' => $caseId,
]);
// now add the invite activity
$studyNumber = CRM_Nihrbackbone_NbrStudy::getStudyNumberWithId($this->_studyId);
if ($studyNumber) {
$subject = "Invited to study " . $studyNumber;
}
else {
$subject = "Invited to study " . $this->_studyId;
}
CRM_Nihrbackbone_NbrVolunteerCase::addCaseActivity($caseId, $contactId, CRM_Nihrbackbone_BackboneConfig::singleton()->getInviteProjectActivityTypeId(), 'Completed', $subject);
// change status on study to invited
$statusCustomField = "custom_" . CRM_Nihrbackbone_BackboneConfig::singleton()->getParticipationCustomField('nvpd_study_participation_status', 'id');
civicrm_api3('Case', 'create', [
'contact_id' => $contactId,
'id' => $caseId,
$statusCustomField => "study_participation_status_invited",
]);
CRM_Nihrbackbone_NbrInvitation::addInviteActivity($caseId, $contactId, $this->_studyId, "Invite By Email Action");
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->warning(E::ts("Could not send invitation to study ") . CRM_Nihrbackbone_NbrStudy::getStudyNumberWithId($this->_studyId)
......@@ -169,12 +155,14 @@ class CRM_Nbrprojectvolunteerlist_Form_Task_InviteByEmail extends CRM_Contact_Fo
$query = "SELECT ccc.case_id, ccc.contact_id
FROM " . $participationTable. " AS cvnpd
LEFT JOIN civicrm_case_contact AS ccc ON cvnpd.entity_id = ccc.case_id
WHERE cvnpd." . $studyColumn . " = %2 AND ccc.contact_id IN (";
LEFT JOIN civicrm_case AS cc ON ccc.case_id = cc.id
WHERE cvnpd." . $studyColumn . " = %2 AND cc.is_deleted = %3 AND ccc.contact_id IN (";
$queryParams = [
1 => [1, "Integer"],
2 => [$this->_studyId, "Integer"],
3 => [0, "Integer"],
];
$i = 2;
$i = 3;
$contactIds = [];
foreach ($this->_invited as $invitedId => $invitedData) {
$contactIds[] = $invitedId;
......
......@@ -28,30 +28,6 @@ class CRM_Nbrprojectvolunteerlist_Utils {
return $elements;
}
/**
* Method to set the update params
*
* @param array $caseIds
* @param string $statusColumn
* @param string $statusId
* @param array $updateParams
* @param string $update
* @return array
*/
public static function setUpdateParams($caseIds, $statusColumn, $statusId, &$updateParams, &$update) {
$participationTable = CRM_Nihrbackbone_BackboneConfig::singleton()->getParticipationDataCustomGroup('table_name');
$update = "UPDATE " . $participationTable . " SET " . $statusColumn . " = %1 WHERE entity_id IN (";
$updateParams = [1 => [$statusId, "String"]];
$i = 1;
$elements = [];
foreach ($caseIds as $caseId => $caseStatus) {
$i++;
$elements[$i] = "%" . $i;
$updateParams[$i] = [(int) $caseId, "Integer"];
}
return $elements;
}
/**
* Method to add contact ids clause (contact_id IN (....)) to query
*
......
......@@ -14,8 +14,8 @@
<url desc="Support">https://civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2020-03-25</releaseDate>
<version>1.8</version>
<releaseDate>2020-03-27</releaseDate>
<version>1.9</version>
<develStage>beta</develStage>
<compatibility>
<ver>4.7</ver>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment