From 413859f516d1d5b0bbf863fac53d24625fc70aa5 Mon Sep 17 00:00:00 2001 From: "Matthew Wire (MJW Consulting)" Date: Tue, 11 Jun 2019 18:50:11 +0100 Subject: [PATCH] Extract getDefaultRoleID --- CRM/Event/Form/Registration.php | 36 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index eec9bd33800d..d8694f4f2530 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -813,23 +813,6 @@ public static function addParticipant(&$form, $contactID) { $params = $form->_params; $transaction = new CRM_Core_Transaction(); - $groupName = 'participant_role'; - $query = " -SELECT v.label as label ,v.value as value -FROM civicrm_option_value v, - civicrm_option_group g -WHERE v.option_group_id = g.id - AND g.name = %1 - AND v.is_active = 1 - AND g.is_active = 1 -"; - $p = array(1 => array($groupName, 'String')); - - $dao = CRM_Core_DAO::executeQuery($query, $p); - if ($dao->fetch()) { - $roleID = $dao->value; - } - // handle register date CRM-4320 $registerDate = NULL; if (!empty($form->_allowConfirmation) && $form->_participantId) { @@ -850,9 +833,7 @@ public static function addParticipant(&$form, $contactID) { 'status_id' => CRM_Utils_Array::value('participant_status', $params, 1 ), - 'role_id' => CRM_Utils_Array::value('participant_role_id', - $params, $roleID - ), + 'role_id' => CRM_Utils_Array::value('participant_role_id', $params) ?: self::getDefaultRoleID(), 'register_date' => ($registerDate) ? $registerDate : date('YmdHis'), 'source' => CRM_Utils_String::ellipsify( isset($params['participant_source']) ? CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params), @@ -904,6 +885,21 @@ public static function addParticipant(&$form, $contactID) { return $participant; } + /** + * Get the ID of the default (first) participant role + * + * @return int + * @throws \CiviCRM_API3_Exception + */ + private static function getDefaultRoleID() { + return (int) civicrm_api3('OptionValue', 'getvalue', [ + 'return' => "value", + 'option_group_id' => "participant_role", + 'is_active' => 1, + 'options' => ['limit' => 1, 'sort' => "is_default DESC"], + ]); + } + /** * Calculate the total participant count as per params. *