Commit 6e347b50 authored by Rodrigo's avatar Rodrigo
Browse files

Add Gen_Registration class

parent 214602c1
......@@ -7,7 +7,11 @@
* @package Gen_Registration_Tracker
* @subpackage Gen_Registration_Tracker/includes
*/
require_once plugin_dir_path( __FILE__ ) . 'class-gen-form-parameters.php';
/**
* Adds Form parameters class.
*/
require_once plugin_dir_path( __FILE__ ) . 'class-gen-form-parameters.php';
/**
* The Class that fetches data from Caldera Forms API and arrang it in the Model classes.
*
......@@ -38,9 +42,9 @@ class Gen_Caldera_Forms_Data_Collector {
* Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] with objects of type Gen_Registrant.
*
* @since 1.0.0
* @var array $all_registrants Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] with objects of type Gen_Registrant.
* @var array $all_registrations Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] with objects of type Gen_Registrant.
*/
private $all_registrants;
private $all_registrations = [];
/**
* List of all possible diet types.
*
......@@ -75,9 +79,9 @@ class Gen_Caldera_Forms_Data_Collector {
* @param array $form_id Caldera Forms form id.
*/
private function __construct( $form_id ) {
$this->form_id = $form_id;
$this->error_list = new WP_Error();
$this->all_registrants = self::get_registrants_from_form( $form_id );
$this->form_id = $form_id;
$this->error_list = new WP_Error();
$this->all_registrations = self::get_registrants_from_form( $form_id );
}
/** Function that uses Caldera Forms API methods to fetch entries from a specific form.
......@@ -91,63 +95,126 @@ class Gen_Caldera_Forms_Data_Collector {
$pages = new Caldera_Forms_Entry_Entries( $form, 9999999 ); // Gets all Form info up to 9999999 Entries.
$entries = $pages->get_page( 1 ); // Since we have all the entries (9999999) in one page we request page 1.
$all_registrations = [];
$all_registrants = [];
foreach ( $entries as $entry ) {
$rows = $pages->get_rows( 1, $entry->get_entry_id() );
$registration = array( 'id' => $entry->get_entry_id() );
$registration_array = array( 'id' => $entry->get_entry_id() );
foreach ( $rows as $field ) {
if ( is_array( $field->value ) ) {
$field->value = implode( ', ', $field->value );
}
$registration[ sanitize_key( $field->slug ) ] = sanitize_text_field( $field->value );
$registration_array[ sanitize_key( $field->slug ) ] = sanitize_text_field( $field->value );
}
// $all_registrants contains 3 arrays ['Attendees'], ['Delegates'] and ['Children']
// each array has many objects from the correspondent class extending Gen_Registrant
if ( empty( $all_registrants ) ) {
$all_registrants = $this->get_registrants( $registration );
if ( is_wp_error( $all_registrants ) ) {
$all_registrants = [];
}
} else {
$registrants_from_registration = $this->get_registrants( $registration );
if ( ! is_wp_error( $registrants_from_registration ) ) {
$all_registrants = array_merge_recursive( $all_registrants, $this->get_registrants( $registration ) );
}
$registration = $this->get_registrants( $registration_array );
if ( ! is_wp_error( $registration ) ) {
$all_registrations[] = $registration;
}
}
if ( ! empty( $this->error_list->get_error_code() ) ) {
Gen_Utility::print_errors( $this->error_list );
}
return $all_registrants;
return $all_registrations;
}
/** Function that iterates through the data of a registration entry and return arrays of objects from Gen_Registrant class.
*
* @param array $registration array of data from a registration.
* @param array $registration_array array of data from a registration.
* @since 1.0.0
* @return array with indexes ['Attendees'], ['Delegates'] and ['Children'], each index contain an array of the correspondent objects.
*/
private function get_registrants( $registration ) {
$attendees = [];
$delegates = [];
$children = [];
if ( isset( $registration[ Gen_Form_Parameters::ID ] ) ) {
$registration_id = $registration[ Gen_Form_Parameters::ID ];
private function get_registrants( $registration_array ) {
/**
* Registration ID.
*/
if ( isset( $registration_array[ Gen_Form_Parameters::ID ] ) ) {
$registration_id = $registration_array[ Gen_Form_Parameters::ID ];
} else {
$this->error_list->add( 'registration_id', 'Registration has no valid id.' );
}
/**
* Accommodation Price
*/
if ( isset( $registration_array[ Gen_Form_Parameters::ACCOMMODATION_PRICE ] ) ) {
$accommodation_price = $registration_array[ Gen_Form_Parameters::ACCOMMODATION_PRICE ];
} else {
$this->error_list->add(
'accommodation_price',
self::class . ': Accommodation price for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
/**
* Meals Price
*/
if ( isset( $registration_array[ Gen_Form_Parameters::MEALS_PRICE ] ) ) {
$meals_price = $registration_array[ Gen_Form_Parameters::MEALS_PRICE ];
} else {
$this->error_list->add(
'meals_price',
self::class . ': Meals price for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
/**
* Event Price
*/
if ( isset( $registration_array[ Gen_Form_Parameters::EVENT_PRICE ] ) ) {
$event_price = $registration_array[ Gen_Form_Parameters::EVENT_PRICE ];
} else {
$this->error_list->add(
'event_price',
self::class . ': Event price for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
/**
* Fee
*/
if ( isset( $registration_array[ Gen_Form_Parameters::FEE ] ) ) {
$fee = $registration_array[ Gen_Form_Parameters::FEE ];
} else {
$this->error_list->add(
'fee',
self::class . ': Fee price for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
/**
* Discount
*/
if ( isset( $registration_array[ Gen_Form_Parameters::DISCOUNT ] ) ) {
$discount = $registration_array[ Gen_Form_Parameters::DISCOUNT ];
} else {
$discount = 0;
}
/**
* Donation
*/
if ( isset( $registration_array[ Gen_Form_Parameters::DONATION ] ) ) {
$donation = $registration_array[ Gen_Form_Parameters::DONATION ];
} else {
$donation = 0;
}
/**
* Total Price
*/
if ( isset( $registration_array[ Gen_Form_Parameters::TOTAL_PRICE ] ) ) {
$total_price = $registration_array[ Gen_Form_Parameters::TOTAL_PRICE ];
} else {
$this->error_list->add(
'total_price',
self::class . ': Total price for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
if ( isset( $registration[ Gen_Form_Parameters::NUMBER_OF_PEOPLE ] ) ) {
$number_of_registrants = $registration[ Gen_Form_Parameters::NUMBER_OF_PEOPLE ];
if ( isset( $registration_array[ Gen_Form_Parameters::NUMBER_OF_PEOPLE ] ) ) {
$number_of_registrants = $registration_array[ Gen_Form_Parameters::NUMBER_OF_PEOPLE ];
} else {
$this->error_list->add( 'number_of_registrants', 'Number of registrants for the registration ' . esc_html( $registration_id ) . ' has not been defined.' );
$this->error_list->add(
'number_of_registrants',
'Number of registrants for the registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
return;
}
$registrants = [];
for ( $registrant_number = 1; $registrant_number <= $number_of_registrants; $registrant_number++ ) {
// This block identifies fields that has different values for each registrant inside a registration entry.
switch ( $registrant_number ) {
......@@ -192,7 +259,10 @@ class Gen_Caldera_Forms_Data_Collector {
$attendee_price_index = Gen_Form_Parameters::ATTENDEE_REGISTRATION_PRICE_FIFTH;
break;
default:
$this->error_list->add( 'number_of_registrants', self::class . ': Number of registrants for the registration ' . esc_html( $registration_id ) . ' has a value out of the range 1-5 = : ' . esc_html( $number_of_registrants ) );
$this->error_list->add(
'number_of_registrants',
self::class . ': Number of registrants for the registration ' . esc_html( $registration_id ) . ' has a value out of the range 1-5 = : ' . esc_html( $number_of_registrants )
);
return;
}
/**
......@@ -201,24 +271,30 @@ class Gen_Caldera_Forms_Data_Collector {
/**
* Registrant Name
*/
if ( isset( $registration[ $registrant_name_index ] ) ) {
$registrant_name = $registration[ $registrant_name_index ];
if ( isset( $registration_array[ $registrant_name_index ] ) ) {
$registrant_name = $registration_array[ $registrant_name_index ];
} else {
$this->error_list->add( 'registrant_name', self::class . ': One registrant name for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
$this->error_list->add(
'registrant_name',
self::class . ': One registrant name for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
/**
* Registrant Age
*/
if ( isset( $registration[ $registrant_age_index ] ) ) {
$registrant_age = $registration[ $registrant_age_index ];
if ( isset( $registration_array[ $registrant_age_index ] ) ) {
$registrant_age = $registration_array[ $registrant_age_index ];
} else {
$this->error_list->add( 'registrant_age', self::class . ': One registrant age for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
$this->error_list->add(
'registrant_age',
self::class . ': One registrant age for registration ' . esc_html( $registration_array_id ) . ' has not been defined.'
);
}
/**
* Registrant Diet
*/
if ( isset( $registration[ $registrant_diet_index ] ) ) {
$registrant_diet = $registration[ $registrant_diet_index ];
if ( isset( $registration_array[ $registrant_diet_index ] ) ) {
$registrant_diet = $registration_array[ $registrant_diet_index ];
$types = explode( ',', $registrant_diet );
foreach ( $types as $diet_type ) {
$diet_type = sanitize_text_field( $diet_type );
......@@ -236,77 +312,92 @@ class Gen_Caldera_Forms_Data_Collector {
* Registration Type
*/
if ( $registrant_age < Gen_Enum::MIN_YOUTHS_AGE ) {
$registration_type = Gen_Kid::REGISTRATION_TYPE;
$registration_price = Gen_Enum::KIDS_REGISTRATION_PRICE;
} elseif ( isset( $registration[ $registration_type_index ] ) ) {
$registration_type = $registration[ $registration_type_index ];
/**
* Registration Price
*/
$registration_type = Gen_Kid::REGISTRATION_TYPE;
$registrant_event_price = Gen_Enum::KIDS_REGISTRATION_PRICE;
} elseif ( isset( $registration_array[ $registration_type_index ] ) ) {
$registration_type = $registration_array[ $registration_type_index ];
if ( strpos( $registration_type, Gen_Delegate::REGISTRATION_TYPE ) !== false ) {
if ( isset( $registration[ $delegate_price_index ] ) ) {
$registration_price = $registration[ $delegate_price_index ];
/**
* Delegate organisation
*/
if ( ! isset( $registration_array[ Gen_Form_Parameters::ORGANISATION ] ) ) {
$organization = 'Organization not specified';
} else {
$organization = $registration_array[ Gen_Form_Parameters::ORGANISATION ];
}
/**
* Delegate registration Price
*/
if ( isset( $registration_array[ $delegate_price_index ] ) ) {
$registrant_event_price = $registration_array[ $delegate_price_index ];
} else {
Gen_Utility::print_alert( 'Delegate price for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
$registration_price = 0;
$registrant_event_price = 0;
}
} else {
if ( isset( $registration[ $attendee_price_index ] ) ) {
$registration_price = $registration[ $attendee_price_index ];
/**
* Attendee registration Price
* * @TODO: Consider Youth discount
*/
if ( isset( $registration_array[ $attendee_price_index ] ) ) {
$registrant_event_price = $registration_array[ $attendee_price_index ];
} else {
$this->error_list->add( 'registration_price', self::class . ': Registration price with slug (' . esc_html( $registration_price_index ) . ') for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
$this->error_list->add(
'registration_price',
self::class . ': Registration price with slug (' . esc_html( $registration_price_index ) . ') for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
}
} else {
$this->error_list->add( 'registrant_type', self::class . ': One registrant type for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
$this->error_list->add(
'registrant_type',
self::class . ': One registrant type for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
/**
* Transportation
*/
if ( isset( $registration[ Gen_Form_Parameters::TRANSPORTATION ] ) ) {
$transportation = $registration[ Gen_Form_Parameters::TRANSPORTATION ];
if ( isset( $registration_array[ Gen_Form_Parameters::TRANSPORTATION ] ) ) {
$transportation = $registration_array[ Gen_Form_Parameters::TRANSPORTATION ];
}
/**
* Accommodation Price
* Registrant accommodation Price
*/
if ( isset( $registration[ Gen_Form_Parameters::ACCOMMODATION_PRICE ] ) ) {
$accommodation_price = $registration[ Gen_Form_Parameters::ACCOMMODATION_PRICE ] / $number_of_registrants;
} else {
$this->error_list->add( 'accommodation_price', self::class . ': Accommodation price for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
}
$registrant_accommodation_price = $accommodation_price / $number_of_registrants;
/**
* Meals Price
* Regosistrant meals Price
*
* @TODO: Kids pay half meal price
*/
if ( isset( $registration[ Gen_Form_Parameters::MEALS_PRICE ] ) ) {
$meals_price = $registration[ Gen_Form_Parameters::MEALS_PRICE ] / $number_of_registrants;
} else {
$this->error_list->add( 'meals_price', self::class . ': Meals price for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
}
$registrant_meals_price = $meals_price / $number_of_registrants;
/**
* Accommodation Type
*/
$accommodation = [];
if ( isset( $registration[ Gen_Form_Parameters::GA_ACCOMMODATION ] ) ) {
$accommodation['General Assembly'] = $registration[ Gen_Form_Parameters::GA_ACCOMMODATION ];
if ( isset( $registration_array[ Gen_Form_Parameters::GA_ACCOMMODATION ] ) ) {
$accommodation['General Assembly'] = $registration_array[ Gen_Form_Parameters::GA_ACCOMMODATION ];
}
if ( isset( $registration[ Gen_Form_Parameters::CONFERENCE_ACCOMMODATION ] ) ) {
$accommodation['Conference'] = $registration[ Gen_Form_Parameters::CONFERENCE_ACCOMMODATION ];
if ( isset( $registration_array[ Gen_Form_Parameters::CONFERENCE_ACCOMMODATION ] ) ) {
$accommodation['Conference'] = $registration_array[ Gen_Form_Parameters::CONFERENCE_ACCOMMODATION ];
}
if ( isset( $registration[ Gen_Form_Parameters::ADDITIONAL_ACCOMMODATION ] ) ) {
$accommodation['National Gathering'] = $registration[ Gen_Form_Parameters::ADDITIONAL_ACCOMMODATION ];
if ( isset( $registration_array[ Gen_Form_Parameters::ADDITIONAL_ACCOMMODATION ] ) ) {
$accommodation['National Gathering'] = $registration_array[ Gen_Form_Parameters::ADDITIONAL_ACCOMMODATION ];
}
/**
* Dates
*/
$arrival = DateTime::createFromFormat( 'd/m/Y', $registration[ Gen_Form_Parameters::ARRIVAL_DATE ] );
$departure = DateTime::createFromFormat( 'd/m/Y', $registration[ Gen_Form_Parameters::DEPARTURE_DATE ] );
$arrival = DateTime::createFromFormat( 'd/m/Y', $registration_array[ Gen_Form_Parameters::ARRIVAL_DATE ] );
$departure = DateTime::createFromFormat( 'd/m/Y', $registration_array[ Gen_Form_Parameters::DEPARTURE_DATE ] );
/**
* Country
*/
if ( isset( $registration[ Gen_Form_Parameters::COUNTRY ] ) ) {
$country = $registration[ Gen_Form_Parameters::COUNTRY ];
if ( isset( $registration_array[ Gen_Form_Parameters::COUNTRY ] ) ) {
$country = $registration_array[ Gen_Form_Parameters::COUNTRY ];
} else {
$this->error_list->add( 'registrant_country', self::class . ': The country for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
$this->error_list->add(
'registrant_country',
self::class . ': The country for registration ' . esc_html( $registration_id ) . ' has not been defined.'
);
}
if ( ! empty( $this->error_list->get_error_code() ) ) {
......@@ -315,17 +406,27 @@ class Gen_Caldera_Forms_Data_Collector {
switch ( $registration_type ) {
case 'Attendee':
array_push( $attendees, new Gen_Attendee( $registration_id, $registrant_age, $transportation, $accommodation, $arrival, $departure, $registrant_diet, $country, $registrant_name, $registration_price, $accommodation_price, $meals_price ) );
array_push(
$registrants,
new Gen_Attendee(
$registration_id,
$registrant_age,
$transportation,
$accommodation,
$arrival,
$departure,
$registrant_diet,
$country,
$registrant_name,
$registrant_event_price,
$registrant_accommodation_price,
$registrant_meals_price
)
);
break;
case 'Delegate':
if ( ! isset( $registration[ Gen_Form_Parameters::ORGANISATION ] ) ) {
$organization = 'Organization not specified';
} else {
$organization = $registration[ Gen_Form_Parameters::ORGANISATION ];
}
array_push(
$delegates,
$registrants,
new Gen_Delegate(
$registration_id,
$registrant_age,
......@@ -337,24 +438,49 @@ class Gen_Caldera_Forms_Data_Collector {
$registrant_diet,
$country,
$registrant_name,
$registration_price,
$accommodation_price,
$meals_price
$registrant_event_price,
$registrant_accommodation_price,
$registrant_meals_price
)
);
break;
case 'Kid':
array_push( $children, new Gen_Kid( $registration_id, $registrant_age, $transportation, $accommodation, $arrival, $departure, $registrant_diet, $country, $registrant_name, $registration_price, $accommodation_price, $meals_price ) );
array_push(
$registrants,
new Gen_Kid(
$registration_id,
$registrant_age,
$transportation,
$accommodation,
$arrival,
$departure,
$registrant_diet,
$country,
$registrant_name,
$registrant_event_price,
$registrant_accommodation_price,
$registrant_meals_price
)
);
break;
default:
$this->error_list->add( 'invalid_registration_type', self::class . ': Invalid registration type ( ' . esc_html( $registration_type ) . ' ) for registration id ( ' . esc_html( $registration_id ) . ' )<br>' );
$this->error_list->add(
'invalid_registration_type',
self::class . ': Invalid registration type ( ' . esc_html( $registration_type ) . ' ) for registration id ( ' . esc_html( $registration_id ) . ' )<br>'
);
}
}
return array(
'Attendees' => $attendees,
'Delegates' => $delegates,
'Children' => $children,
return new Gen_Registration(
$registration_id,
$event_price,
$accommodation_price,
$meals_price,
$donation,
$fee,
$discount,
$total_price,
$registrants
);
}
/** Retrieves the list of diet types.
......@@ -370,7 +496,7 @@ class Gen_Caldera_Forms_Data_Collector {
* @since 1.0.0
* @return array array with indexes ['Attendees'], ['Delegates'] and ['Children'], each index contain an array of the correspondent objects.
*/
public function get_all_registrants() {
return $this->all_registrants;
public function get_all_registrations() {
return $this->all_registrations;
}
}
......@@ -30,6 +30,11 @@ abstract class Gen_Form_Parameters {
const MEALS_PRICE = 'total_meals_price';
const NUMBER_OF_PEOPLE = 'how_many_people_would_you_like_to_register';
const COUNTRY = 'country';
const EVENT_PRICE = 'conference_price';
const FEE = 'payment_option_fee_copy';
const DISCOUNT = 'group_discount_for_gen_europes_conference';
const DONATION = 'donation_for_gen_europe';
const TOTAL_PRICE = 'total_amount';
/**
* Name
*/
......
......@@ -92,6 +92,7 @@ class Gen_Registration_Tracker {
require_once plugin_dir_path( __FILE__ ) . 'dashboard/class-gen-dashboard-loader.php';
require_once plugin_dir_path( __FILE__ ) . 'dashboard/class-gen-dashboard-viewer.php';
require_once plugin_dir_path( __FILE__ ) . 'class-gen-enum.php';
require_once plugin_dir_path( __FILE__ ) . 'class-gen-registration.php';
// Register style sheet.
add_action( 'admin_enqueue_scripts', array( $this, 'register_styles' ) );
......
<?php
/**
* The file that defines the model for a registration.
*
* @since 1.0.1
*
* @package Gen_Registration_Tracker
* @subpackage Gen_Registration_Tracker/includes
*/
/**
* The class that describes the registration.
*
* @since 1.0.1
* @package Gen_Registration_Tracker
* @subpackage Gen_Registration_Tracker/includes
*/
class Gen_Registration {
/**
* Id of the registration.
*
* @since 1.0.1
* @access private
* @var int $id The id of the registration.
*/
private $id;
/**
* Array of registrants.
*
* @since 1.0.1
* @access private
* @var array $registrants Array of objects from Gen_Registrant class.
*/
private $registrants;
/**
* Price for the event ticket.
*
* @since 1.0.1
* @access private
* @var int $event_price The total price of the event ticket.
*/
private $event_price;
/**
* Price for the accommodation.
*
* @since 1.0.1
* @access private
* @var int $accommodation_price The total price of accommodation for the registration.
*/
private $accommodation_price;
/**
* Total price for meals.
*
* @since 1.0.1
* @access private
* @var int $meals_price The total price of meals for the registration.
*/
private $meals_price;
/**
* Donation Given to GEN Europe.
*
* @since 1.0.1
* @access private
* @var int $donation The total amount in Donation Given to GEN Europe.
*/
private $donation;
/**
* Total amount of fees.
*
* @since 1.0.1
* @access private
* @var int $fee The total amount of fees for the registration.
*/
private $fee;
/**
* Discount
*
* @since 1.0.1
* @access private
* @var int $discount Discount for this registration.
*/
private $discount;
/**
* Total price of the registration.
*
* @since 1.0.1
* @access private
* @var int $total_price The total price for the registration.
*/
private $total_price;
/**
* Retrieves the Regstration ID for this Registrant.
*
* @return int Registration id.
*/