NbrParticipation.php 8.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php

use CRM_Nbrprojectvolunteerlist_ExtensionUtil as E;

/**
 * Class for participation processing specific to the study participant management screen
 *
 * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
 * @date 19 Oct 2020
 * @license AGPL-3.0
 */
class CRM_Nbrprojectvolunteerlist_NbrParticipation {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  /**
   * Method to process the build form hook for caseview
   * - when participation, remove email and pdf actions from list
   *
   * @param $form
   */
  public function caseViewBuildForm(&$form) {
    $caseId = $form->getVar('_caseID');
    $participationType = Civi::service('nbrBackbone')->getParticipationCaseTypeName();
    $caseType = $form->getVar('_caseType');
    if ($caseType == $participationType) {
      $activityList = $form->getElement('add_activity_type_id');
      $options = &$activityList->_options;
      foreach($options as $optionKey => $optionValues) {
        if ($optionValues['text'] == "Email" || $optionValues['text'] == "Print/Merge Document") {
          unset($options[$optionKey]);
        }
      }
    }
  }
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

  /**
   * Method to process the build form hook for email
   * (only when coming from the MSP screen)
   * @param $form
   */
  public function emailBuildForm(&$form) {
    // only for custom searches
    $context = $form->getVar('_context');
    if ($context == "custom") {
      $searchFormValues = $form->controller->exportValues('Custom');
      // only if search form = Manage Study Participation
      $msp = new CRM_Nbrprojectvolunteerlist_SearchTasks();
      $mspCsId = $msp->getCsId();
      if (isset($searchFormValues['csid']) && (int) $searchFormValues['csid'] == $mspCsId) {
        if (isset($searchFormValues['study_id'])) {
          $studyId = (int) $searchFormValues['study_id'];
          if ($studyId) {
            $contactIds = $form->getVar('_contactIds');
52
            $caseIds = [];
53
            foreach ($contactIds as $contactId) {
54
              $caseIds[$contactId] = CRM_Nihrbackbone_NbrVolunteerCase::getActiveParticipationCaseId($studyId, $contactId);
55
56
57
            }
            if (!empty($caseIds)) {
              $session = CRM_Core_Session::singleton();
58
              $session->nbr_email_pdf_case_ids = $caseIds;
59
60
61
62
63
64
65
            }
          }
        }
      }
    }
  }

ErikHommel's avatar
ErikHommel committed
66
67
  /**
   * Method to process the build form hook for pdf
68
   * (only when coming from the MSP screen) -> add study id to form
ErikHommel's avatar
ErikHommel committed
69
70
71
72
73
74
75
76
   * @param $form
   */
  public function pdfBuildForm(&$form) {
    $searchFormValues = $form->controller->exportValues();
    // only if search form = Manage Study Participation
    $msp = new CRM_Nbrprojectvolunteerlist_SearchTasks();
    $mspCsId = $msp->getCsId();
    if (isset($searchFormValues['csid']) && (int) $searchFormValues['csid'] == $mspCsId) {
77
      if (isset($searchFormValues['study_id'])) {
ErikHommel's avatar
ErikHommel committed
78
        $caseIds = [];
79
        $studyId = (int) $searchFormValues['study_id'];
ErikHommel's avatar
ErikHommel committed
80
81
82
83
84
        foreach ($form->_contactIds as $contactId) {
          $caseIds[$contactId] = CRM_Nihrbackbone_NbrVolunteerCase::getActiveParticipationCaseId($studyId, $contactId);
        }
        $session = CRM_Core_Session::singleton();
        $session->nbr_email_pdf_case_ids = $caseIds;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        $form->removeElement('campaign_id');
        $form->add('hidden', 'study_id');
        $form->setDefaults(['study_id' => $studyId]);
      }
    }
  }

  /**
   * Method to process the build form hook for pdf invite
   * (only when coming from the MSP screen)
   * @param $form
   */
  public function pdfInviteBuildForm(&$form) {
    $searchFormValues = $form->controller->exportValues();
    // only if search form = Manage Study Participation
    $msp = new CRM_Nbrprojectvolunteerlist_SearchTasks();
    $mspCsId = $msp->getCsId();
    if (isset($searchFormValues['csid']) && (int) $searchFormValues['csid'] == $mspCsId) {
ErikHommel's avatar
ErikHommel committed
103
104
      if (isset($searchFormValues['study_id'])) {
        $studyId = (int) $searchFormValues['study_id'];
105
106
107
108
        $contactIds = $form->getVar('_contactIds');
        $invalidIds = [];
        $invitedIds = [];
        $form->removeElement('campaign_id');
ErikHommel's avatar
ErikHommel committed
109
110
        $form->add('hidden', 'study_id');
        $form->setDefaults(['study_id' => $studyId]);
111
112
        $form->add('hidden', 'is_nbr_invite');
        $form->setDefaults(['is_nbr_invite' => TRUE]);
ErikHommel's avatar
ErikHommel committed
113
114
115
        if ($studyId) {
          $caseIds = [];
          foreach ($contactIds as $contactId) {
116
            $caseId = CRM_Nihrbackbone_NbrVolunteerCase::getActiveParticipationCaseId($studyId, $contactId);
117
            $this->addInvalidVolunteer($contactId, $caseId, $invitedIds, $invalidIds, $caseIds);
ErikHommel's avatar
ErikHommel committed
118
          }
119
120
121
122
          if (!empty($caseIds)) {
            $session = CRM_Core_Session::singleton();
            $session->nbr_email_pdf_case_ids = $caseIds;
          }
123
124
125
126
127
128
129
          // remove all invalids from _contactIds and _componentIds in form object
          $resultIds = [];
          foreach ($invitedIds as $invitedId => $invitedData) {
            $resultIds[] = (string) $invitedId;
          }
          $form->setVar("_contactIds", $resultIds);
          $form->setVar("_componentIds", $resultIds);
130
131
          $form->assign('invalid_ids', $invalidIds);
          $form->assign('invited_ids', $invitedIds);
ErikHommel's avatar
ErikHommel committed
132
133
134
135
136
        }
      }
    }
  }

137
138
139
140
141
142
143
144
  /**
   * Method to check if volunteer is invalid for invite
   * (not eligible or status is not selected)
   *
   * @param $contactId
   * @param $caseId
   * @param $invitedIds
   * @param $invalidIds
145
   * @param $caseIds
146
   */
147
  private function addInvalidVolunteer($contactId, $caseId, &$invitedIds, &$invalidIds, &$caseIds) {
148
149
150
151
152
153
154
155
156
    $eligible = CRM_Nihrbackbone_NbrVolunteerCase::getCurrentEligibleStatus($caseId);
    $status = CRM_Nihrbackbone_NbrVolunteerCase::getCurrentStudyStatus($caseId);
    if ($eligible[0] == Civi::service('nbrBackbone')->getEligibleEligibilityStatusValue() &&
      $status == Civi::service('nbrBackbone')->getSelectedParticipationStatusValue()) {
      $invitedIds[$contactId] = [
        'display_name' => CRM_Nihrbackbone_Utils::getContactName($contactId, 'display_name'),
        'study_status' => CRM_Nihrbackbone_Utils::getOptionValueLabel($status, CRM_Nihrbackbone_BackboneConfig::singleton()->getStudyParticipationStatusOptionGroupId()),
        'eligible_status' => CRM_Nihrbackbone_Utils::getOptionValueLabel($eligible[0], CRM_Nihrbackbone_BackboneConfig::singleton()->getEligibleStatusOptionGroupId()),
      ];
157
      $caseIds[$contactId] = $caseId;
158
159
160
161
162
163
164
165
166
167
    }
    else {
      $invalidIds[$contactId] = [
        'display_name' => CRM_Nihrbackbone_Utils::getContactName($contactId, 'display_name'),
        'study_status' => CRM_Nihrbackbone_Utils::getOptionValueLabel($status, CRM_Nihrbackbone_BackboneConfig::singleton()->getStudyParticipationStatusOptionGroupId()),
        'eligible_status' => CRM_Nihrbackbone_Utils::getOptionValueLabel($eligible[0], CRM_Nihrbackbone_BackboneConfig::singleton()->getEligibleStatusOptionGroupId()),
      ];
    }
  }

ErikHommel's avatar
ErikHommel committed
168
169
170
171
172
173
  /**
   * Method to file email activities on cases
   *
   * @param $activityId
   * @param $activityTypeId
   */
ErikHommel's avatar
ErikHommel committed
174
  public static function fileActivityOnCases($activityId, $activityTypeId) {
ErikHommel's avatar
ErikHommel committed
175
    $caseIds = [];
ErikHommel's avatar
ErikHommel committed
176
    $fileOnCase = TRUE;
177
    $session = CRM_Core_Session::singleton();
178
179
180
    if ($activityTypeId == Civi::service('nbrBackbone')->getEmailActivityTypeId() || $activityTypeId == Civi::service('nbrBackbone')->getLetterActivityTypeId()) {
      if (isset($session->nbr_email_pdf_case_ids)) {
        $caseIds = $session->nbr_email_pdf_case_ids;
181
      }
ErikHommel's avatar
ErikHommel committed
182
183
    }
    if (!empty($caseIds)) {
184
      foreach ($caseIds as $caseContactId => $caseId) {
ErikHommel's avatar
ErikHommel committed
185
        // only if contact is OK for PDF activity!
186
        if ($activityTypeId == Civi::service('nbrBackbone')->getLetterActivityTypeId()) {
ErikHommel's avatar
ErikHommel committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
          $fileOnCase = FALSE;
          $query = "SELECT contact_id FROM civicrm_activity_contact WHERE activity_id = %1 AND record_type_id = %2";
          $activityTargetId = CRM_Core_DAO::singleValueQuery($query, [
            1 => [(int) $activityId, 'Integer'],
            2 => [Civi::service('nbrBackbone')->getTargetRecordTypeId(), 'Integer'],
          ]);
          if ($activityTargetId == $caseContactId) {
            $fileOnCase = TRUE;
          }
        }
        if ($fileOnCase) {
          $insert = "INSERT INTO civicrm_case_activity (case_id, activity_id) VALUES(%1, %2)";
          CRM_Core_DAO::executeQuery($insert, [
            1 => [(int) $caseId, "Integer"],
            2 => [(int) $activityId, "Integer"],
          ]);
          // if PPF, check if invite processing required
          if ($activityTypeId == Civi::service('nbrBackbone')->getLetterActivityTypeId()) {
            $studyId = CRM_Utils_Request::retrieveValue('study_id', 'Integer');
            $isInvite = CRM_Utils_Request::retrieveValue('is_nbr_invite', 'Boolean');
            if ($studyId && $isInvite) {
              CRM_Nihrbackbone_NbrInvitation::addInviteActivity($caseId, $caseContactId, $studyId, "invite by letter");
            }
210
          }
211
        }
212
213
214
      }
    }
  }
215
216
}