Commit d39c1c23 authored by BohdanDmytryshyn's avatar BohdanDmytryshyn
Browse files

Release 5.5.0

parent 1dac6279
<?php
class CRM_CiviMobileAPI_Api_CiviMobileSurvey_GetContactSurveys extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns results to api
*
* @return array
* @throws api_Exception
*/
public function getResult() {
$preparedSurveys = [];
$paramsToApi = [
'options' => ['limit' => 0],
'activity_type_id' => $this->validParams['activity_type_id']
];
if (!empty($this->validParams['survey_id'])) {
$paramsToApi['id'] = $this->validParams['survey_id'];
}
$surveys = civicrm_api3('Survey', 'get', $paramsToApi)['values'];
foreach ($surveys as $key => &$surveyInfo) {
if (!empty($this->validParams['title']['LIKE'])
&& stripos($surveyInfo['title'], $this->validParams['title']['LIKE']) === false) {
continue;
}
$survey = [
'id' => $surveyInfo['id'],
'title' => $surveyInfo['title'],
'is_active' => $surveyInfo['is_active'],
'activity_type_id' => $surveyInfo['activity_type_id']
];
if (!empty($this->validParams['contact_id'])) {
$activities = civicrm_api3('Activity', 'get', [
'target_contact_id' => $this->validParams['contact_id'],
'source_record_id' => $surveyInfo['id'],
'activity_type_id' => $this->validParams['activity_type_id'],
'status_id' => "Completed",
'is_deleted' => 0,
]);
$survey['is_signed'] = (int)(boolean)$activities['count'];
if (!is_null($this->validParams['is_signed'])
&& ((!$survey['is_signed'] && $this->validParams['is_signed']) || ($survey['is_signed'] && !$this->validParams['is_signed']))
) {
continue;
}
}
$preparedSurveys[] = $survey;
}
return $preparedSurveys;
}
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws api_Exception
*/
protected function getValidParams($params) {
if (CRM_Core_Session::getLoggedInContactID()) {
if (empty($params['activity_type_id'])) {
$params['activity_type_id'] = [
'IN' => []
];
$activityTypes = civicrm_api3('OptionValue', 'get', [
'sequential' => 1,
'option_group_id' => "activity_type",
'component_id' => "CiviCampaign",
])['values'];
foreach ($activityTypes as $type) {
$params['activity_type_id']['IN'][] = $type['name'];
}
}
} else {
$params['activity_type_id'] = "Petition";
}
return [
'is_signed' => isset($params['is_signed']) ? $params['is_signed'] : NULL,
'survey_id' => isset($params['survey_id']) ? $params['survey_id'] : NULL,
'contact_id' => isset($params['contact_id']) ? $params['contact_id'] : NULL,
'activity_type_id' => $params['activity_type_id'],
'title' => isset($params['title']) ? $params['title'] : NULL,
];
}
}
<?php
class CRM_CiviMobileAPI_Api_CiviMobileSurvey_GetSignedValues extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns results to api
*
* @return array
* @throws api_Exception
*/
public function getResult() {
$preparedValues = [];
$surveyStructure = civicrm_api3('CiviMobileSurvey', 'get_structure', [
'id' => $this->validParams['survey_id'],
])['values'][0];
if (!$surveyStructure['is_petition']) {
if (!CRM_CiviMobileAPI_Utils_Permission::isEnoughPermissionToViewRespondentDetails()) {
throw new API_Exception(ts('Permission is required.'));
}
} else {
if (!CRM_CiviMobileAPI_Utils_Permission::isEnoughPermissionToViewPetitionAnswers()) {
throw new API_Exception(ts('Permission is required.'));
}
}
try {
$activity = civicrm_api3('Activity', 'getsingle', [
'target_contact_id' => $this->validParams['contact_id'],
'source_record_id' => $this->validParams['survey_id'],
'activity_type_id' => $surveyStructure['activity_type_id'],
'status_id' => "Completed"
]);
} catch (Exception $e) {
throw new api_Exception('Survey is not signed. ' . $e->getMessage(), 'survey_is_not_signed');
}
$preparedValues['note'] = $activity['details'];
$preparedValues['result'] = !empty($activity['result']) ? $activity['result'] : '';
$preparedValues['activity_profile'] = $this->getProfileValuesByActivity(
$surveyStructure['profiles']['activity_profile'],
$activity
);
$preparedValues['contact_profile'] = $this->getProfileValues(
$surveyStructure['profiles']['contact_profile'],
$this->validParams['contact_id'],
$activity['id']
);
return [$preparedValues];
}
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws api_Exception
*/
protected function getValidParams($params) {
return [
'contact_id' => $params['contact_id'],
'survey_id' => $params['survey_id'],
];
}
/**
* Returns values of submitted profile
*
* @param $profile
* @param $contactId
* @param $activityId
* @return array
*/
protected function getProfileValues($profile, $contactId, $activityId) {
$values = [];
$profileValues = civicrm_api3('Profile', 'get', [
'profile_id' => $profile['id'],
'contact_id' => $contactId,
'activity_id' => $activityId
])['values'];
foreach ($profile['fields'] as $field) {
$values[$field['name']] = $field;
if (isset($profileValues[$field['name']])) {
if (is_array($profileValues[$field['name']])) {
$selectedOptions = [];
foreach ($profileValues[$field['name']] as $key => $value) {
if (!preg_match('/^\[[\s\S]+\]$/', $key)) {
$selectedOptions[] = strval($key);
}
}
$values[$field['name']]['value'] = $selectedOptions;
} else {
$values[$field['name']]['value'] = $profileValues[$field['name']];
}
} else {
$values[$field['name']]['value'] = NULL;
}
}
return $values;
}
/**
* Returns values of submitted profile by activity
*
* @param $profile
* @param $activityId
* @return mixed
*/
protected function getProfileValuesByActivity($profile, $activity) {
foreach ($profile['fields'] as &$field) {
$field['value'] = isset($activity[$field['name']]) ? $activity[$field['name']] : NULL;
}
return $profile['fields'];
}
}
<?php
class CRM_CiviMobileAPI_Api_CiviMobileSurvey_GetStructure extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns results to api
*
* @return array
* @throws api_Exception
*/
public function getResult() {
try {
$surveyInfo = civicrm_api3('Survey', 'getsingle', [
'id' => $this->validParams['id'],
]);
} catch (Exception $e) {
throw new api_Exception('Survey does not exists','survey_does_not_exists');
}
$isPetition = true;
try {
$petitionOptionValue = civicrm_api3('OptionValue', 'getsingle', [
'sequential' => 1,
'option_group_id' => "activity_type",
'component_id' => "CiviCampaign",
'name' => "Petition",
]);
if ($surveyInfo['activity_type_id'] != $petitionOptionValue['value']) {
$isPetition = false;
}
} catch (Exception $e) {
$isPetition = false;
}
if ($isPetition) {
if (!CRM_CiviMobileAPI_Utils_Permission::isEnoughPermissionToViewPetition()) {
throw new API_Exception(ts('Permission is required.'));
}
} else {
if (!CRM_Core_Session::getLoggedInContactID()) {
throw new API_Exception(ts('Not authorized.'));
}
if (!CRM_CiviMobileAPI_Utils_Permission::isEnoughPermissionToGetSurveysList()) {
throw new API_Exception(ts('Permission is required.'));
}
}
$survey = [
'id' => $surveyInfo['id'],
'title' => $surveyInfo['title'],
'is_active' => $surveyInfo['is_active'],
'is_petition' => $isPetition ? 1 : 0,
'activity_type_id' => $surveyInfo['activity_type_id'],
'instructions' => $surveyInfo['instructions'],
'default_number_of_contacts' => $surveyInfo['default_number_of_contacts'],
'max_number_of_contacts' => $surveyInfo['max_number_of_contacts'],
'short_instructions' => $surveyInfo['instructions'] ? html_entity_decode(mb_substr(strip_tags(preg_replace('/\s\s+/', ' ', $surveyInfo['instructions'])), 0, 200), ENT_QUOTES | ENT_HTML401) : ''
];
if (!empty($surveyInfo['result_id'])) {
$resultOptions = civicrm_api3('OptionValue', 'get', [
'sequential' => 1,
'option_group_id' => $surveyInfo['result_id'],
'options' => ['limit' => 0],
])['values'];
$preparedResultOptions = [];
foreach ($resultOptions as $option) {
if ($option['is_active']) {
$preparedResultOptions[] = [
'id' => $option['id'],
'label' => $option['label'],
'name' => $option['name'],
'is_default' => $option['is_default'],
'value' => $option['value'],
];
}
}
$survey['result_id'] = $surveyInfo['result_id'];
$survey['result_set'] = $preparedResultOptions;
} else {
$survey['result_id'] = '';
$survey['result_set'] = [];
}
$joinedProfiles = civicrm_api3('UFJoin', 'get', [
'entity_table' => "civicrm_survey",
'entity_id' => $this->validParams['id'],
])['values'];
$survey['profiles']['activity_profile'] = [];
$survey['profiles']['contact_profile'] = [];
foreach ($joinedProfiles as $profile) {
$fields = CRM_Core_BAO_UFGroup::getFields($profile['uf_group_id']);
$this->prepareFields($fields);
$profile_name = 'activity_profile';
if (($profile['weight'] == 2 && $isPetition) || ($profile['weight'] == 1 && !$isPetition)) {
$profile_name = 'contact_profile';
}
$survey['profiles'][$profile_name] = [
'id' => $profile['uf_group_id'],
'fields' => $fields
];
}
return [$survey];
}
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws api_Exception
*/
protected function getValidParams($params) {
return [
'id' => $params['id'],
'activity_type_id' => $params['activity_type_id']
];
}
/**
* Prepare fields
*
* @param $fields
*/
private function prepareFields(&$fields) {
$customFieldsIds = [];
foreach ($fields as $key => $field) {
if (preg_match('/^custom_\d+$/', $field['name'])) {
array_push($customFieldsIds, (int) trim($field['name'], 'custom_'));
}
}
$customFields = [];
if (!empty($customFieldsIds)) {
$customFields = civicrm_api3('CustomField', 'get', [
'id' => ['IN' => $customFieldsIds],
])['values'];
}
foreach ($fields as &$field) {
$customField = [];
if (preg_match('/^custom_\d+$/', $field['name'])) {
$customField = $customFields[(int) trim($field['name'], 'custom_')];
}
$fieldParams = [
'name' => $field['name'],
'title' => $field['title'],
'html_type' => empty($customField) ? $field['html_type'] : $customField['html_type'],
'data_type' => empty($customField) ? $field['data_type'] : $customField['data_type'],
'attributes' => $field['attributes'],
'group_id' => $field['group_id'],
'field_id' => $field['field_id'],
"is_required" => (!empty($field['is_required'])) ? $field['is_required'] : 0,
"is_view" => (!empty($field['is_view'])) ? $field['is_view'] : 0,
"date_format" => (!empty($field['date_format'])) ? $field['date_format'] : "",
"time_format" => (!empty($field['time_format'])) ? $field['time_format'] : "",
"start_date_years" => (!empty($field['start_date_years'])) ? $field['start_date_years'] : "",
"end_date_years" => (!empty($field['end_date_years'])) ? $field['end_date_years'] : "",
"default_currency" => CRM_Core_Config::singleton()->defaultCurrency,
"default_currency_symbol" => CRM_Core_Config::singleton()->defaultCurrencySymbol,
'default_value' => (!empty($customField['default_value'])) ? $customField['default_value'] : ""
];
if (!empty($customField["option_group_id"])) {
$fieldParams['options'] = CRM_CiviMobileAPI_Utils_OptionValue::getGroupValues($customField["option_group_id"], ['is_active' => 1]);
} else if (!empty($field['pseudoconstant']['optionGroupName'])) {
$fieldParams['options'] = CRM_CiviMobileAPI_Utils_OptionValue::getGroupValues($field['pseudoconstant']['optionGroupName'], ['is_active' => 1]);
}
if ($customField['html_type'] == 'Radio' && $customField['data_type'] == "Boolean") {
$fieldParams['options'] = ['1','0'];
}
$field = $fieldParams;
}
}
}
<?php
class CRM_CiviMobileAPI_Api_CiviMobileSurvey_Sign extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns results to api
*
* @return array
* @throws api_Exception
*/
public function getResult() {
$transaction = new CRM_Core_Transaction();
if (!empty($this->validParams['values']['contact_profile']['email-Primary'])
&& $this->validParams['survey_structure']['is_petition']
&& empty($this->validParams['contact_id'])
) {
$contacts = civicrm_api3('Contact', 'get', [
'sequential' => 1,
'email' => $this->validParams['values']['contact_profile']['email-Primary']
]);
if (!empty($contacts['values'])) {
$this->validParams['contact_id'] = $contacts['values'][0]['id'];
}
}
$userSurvey = civicrm_api3('CiviMobileSurvey', 'get_contact_surveys', [
'contact_id' => $this->validParams['contact_id'],
'survey_id' => $this->validParams['survey_id'],
])['values'][0];
if ($userSurvey['is_signed']) {
$transaction->rollback();
throw new api_Exception('Survey already signed.', 'survey_already_signed');
}
if (!$this->validParams['survey_structure']['is_petition']) {
try {
$activity = civicrm_api3('Activity', 'getsingle', [
'sequential' => 1,
'status_id' => "Scheduled",
'source_record_id' => $this->validParams['survey_id'],
'target_contact_id' => $this->validParams['contact_id'],
'is_deleted' => 0,
'return' => ["assignee_contact_id", "id"]
]);
} catch (Exception $e) {
$transaction->rollback();
throw new api_Exception('The respondent is not reserved.', 'unknown_exception');
}
if (!in_array(CRM_Core_Session::getLoggedInContactID(), $activity['assignee_contact_id']) && !CRM_CiviMobileAPI_Utils_Permission::isEnoughPermissionToChangeInterviewer()) {
throw new API_Exception(ts('Permission is required.'));
}
$params = [
'voter_id' => $this->validParams['contact_id'],
'survey_id' => $this->validParams['survey_id'],
'activity_id' => $activity['id'],
'surveyTitle' => $this->validParams['survey_structure']['title'],
'activity_type_id' => $this->validParams['survey_structure']['activity_type_id'],
'details' => $this->validParams['note'],
'result' => $this->validParams['result']
] + $this->validParams['values']['contact_profile'] + $this->validParams['values']['activity_profile'];
CRM_Campaign_Form_Task_Interview::registerInterview($params);
} else {
$contactParams = [
'contactId' => $this->validParams['contact_id']
] + $this->validParams['values']['contact_profile'];
$contactId = CRM_Contact_BAO_Contact::createProfileContact($contactParams, $this->validParams['survey_structure']['profiles']['contact_profile']['fields'],
$this->validParams['contact_id'], NULL, $this->validParams['survey_structure']['contact_profile']['id'], 'Individual', TRUE);
$activityParams = [
'contactId' => $contactId,
'sid' => $this->validParams['survey_id'],
'status_id' => "Completed"
] + $this->validParams['values']['activity_profile'];
$activityParams['custom'] = CRM_Core_BAO_CustomField::postProcess($activityParams, NULL, 'Activity', FALSE, FALSE);
$petitionBAO = new CRM_Campaign_BAO_Petition();
$petitionBAO->createSignature($activityParams);
}
$transaction->commit();
return [['message' => "Survey successfully signed."]];
}
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws api_Exception`
*/
protected function getValidParams($params) {
$loggedInContactId = CRM_Core_Session::getLoggedInContactID();
$validatedParams = [];
if (!is_array($params['values'])) {
throw new api_Exception('Values must be object.', 'values_must_be_object');
}
$surveyStructure = civicrm_api3('CiviMobileSurvey', 'get_structure', [
'id' => $params['survey_id'],
])['values'][0];
if ($surveyStructure['is_petition']) {
if (!CRM_CiviMobileAPI_Utils_Permission::isEnoughPermissionToSignPetition()) {
throw new API_Exception(ts('Permission is required.'));
}
if ($loggedInContactId) {
$params['contact_id'] = $loggedInContactId;
}
} else {
if (!$loggedInContactId) {
throw new API_Exception(ts('Not authorized.'));
}
if (empty($params['contact_id'])) {
throw new API_Exception(ts('contact_id is required.'));
}
if (!CRM_Core_Permission::check('interview campaign contacts')) {
throw new API_Exception(ts('Permission is required.'));
}
}
$validatedParams['survey_id'] = $params['survey_id'];
$validatedParams['survey_structure'] = $surveyStructure;
$validatedParams['contact_id'] = !empty($params['contact_id']) ? $params['contact_id'] : NULL;
$validatedParams['contact_profile_id'] = $surveyStructure['profiles']['contact_profile']['id'];
$validatedParams['activity_profile_id'] = $surveyStructure['profiles']['activity_profile']['id'];
$validatedParams['note'] = !empty($params['note']) ? $params['note'] : NULL;
if (!empty($params['result'])) {
$resultOptionLabel = NULL;
foreach ($surveyStructure['result_set'] as $option) {
if ($option['name'] == $params['result']) {
$resultOptionLabel = $option['label'];
}
}
if (empty($resultOptionLabel)) {
throw new api_Exception("Result option doesn`t exist.", 'no_result_option');
}
$validatedParams['result'] = $resultOptionLabel;
} else {
$validatedParams['result'] = NULL;
}
$validatedParams['values'] = [
'activity_profile' => [],
'contact_profile' => []
];
foreach ($surveyStructure['profiles'] as $key => $profile) {
foreach ($profile['fields'] as $field) {
if ((!isset($params['values'][$field['name']]) || (isset($params['values'][$field['name']]) && $params['values'][$field['name']] == "")) && $field['is_required']) {
throw new api_Exception("'{$field['name']}' is required.", 'required_value');
}