Skip to content

Commit

Permalink
Merge pull request #14499 from mattwire/extract_getdefaultroleid
Browse files Browse the repository at this point in the history
REF Extract getDefaultRoleID for add participant
  • Loading branch information
seamuslee001 authored Jun 25, 2019
2 parents c10dc1e + 413859f commit 458a3ae
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions CRM/Event/Form/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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),
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 458a3ae

Please sign in to comment.