Commit 5aca756b authored by Rodrigo's avatar Rodrigo
Browse files

Add registration, accommodation and meals prices to Registrant classes;

Add painel to error messages;
Add an Alert panel method;
Add Enum class;
Create constants for registration types in each Registrant class.
parent 4e4f9532
......@@ -8,24 +8,56 @@
box-shadow:0 4px 10px 0 rgba(0,0,0,0.2),0 4px 20px 0 rgba(0,0,0,0.19)
}
.error-card {
background-color: #EC9787!important;
float: left;
width: 90%;
margin: 20px 20px 0 0;
box-shadow:0 4px 10px 0 rgba(0,0,0,0.2),0 4px 20px 0 rgba(0,0,0,0.19)
}
.alert-card {
float: left;
position: absolute;
right:0;
top:0;
padding: 10px;
margin: 20px 20px 0 0;
box-shadow:0 4px 10px 0 rgba(0,0,0,0.2),0 4px 20px 0 rgba(0,0,0,0.19)
}
@media only screen and (max-width: 700px) {
.info-card { width: 95% }
}
.gen-error {
.gen-message {
clear:both;
padding: 10px
padding: 3px
}
.gen-container {
padding:0.05em 16px 10px 16px;
}
.gen-main-container {
position:relative
}
.gen-blue, .gen-blue h1 {
color:#fff;
background-color:#2196F3
}
.gen-red, .gen-red h2 {
color:#fff;
background-color:#F44336
}
.gen-yellow, .gen-yellow h2 {
color:#000;
background-color:#FFEB3B
}
.gen-table {
border-collapse:collapse;
border-spacing:0;
......@@ -42,3 +74,23 @@
}
.gen-value { float: right }
.gen-button{ width:10px }
.gen-display-topright {
position: absolute;
right:0;
top:0
}
.gen_panel,.gen_panel:after, .gen_panel:before {
content:"";
display:table;
clear:both
}
.gen-button:hover{ color:#000!important; background-color:#ccc!important}
.gen-button {border:none; display:inline-block; padding:8px 16px; vertical-align:middle; overflow:hidden; text-decoration:none; color:inherit; background-color:inherit; text-align:center; cursor:pointer; white-space:nowrap}
.gen-button{-webkit-touch-callout:none; -webkit-user-select:none; -khtml-user-select:none; -moz-user-select:none; -ms-user-select:none;user-select:none}
.gen-animate-left{position:relative;animation:animateleft 0.4s}@keyframes animateleft{from{left:-300px;opacity:0} to{left:0;opacity:1}}
.gen-display-container { position:relative }
.gen-display-container:hover .gen-display-hover{display:block}.gen-display-container:hover span.gen-display-hover{display:inline-block}.gen-display-hover{display:none}
......@@ -26,18 +26,7 @@ class Gen_Caldera_Forms_Data_Collector {
* @var Gen_Caldera_Forms_Data_Collector $instance A instance of the class.
*/
private static $instance = null;
/**
* Maximun age for a person to be considered a youth.
*
* @since 1.0.0
*/
const MAX_YOUTHS_AGE = 25;
/**
* Minimun age for a person to be considered a youth.
*
* @since 1.0.0
*/
const MIN_YOUTHS_AGE = 13;
/**
* Id of the Caldera Forms form.
*
......@@ -59,6 +48,14 @@ class Gen_Caldera_Forms_Data_Collector {
* @var array $diet_types array of string with all diet types.
*/
private $diet_types = [];
/**
* List of errors occurred.
*
* @since 1.0.1
* @access protected
* @var WP_Error $error_list List of errors occurred.
*/
protected $error_list;
/**
* Retrieves the class instance.
*
......@@ -79,6 +76,7 @@ class Gen_Caldera_Forms_Data_Collector {
*/
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 );
}
/** Function that uses Caldera Forms API methods to fetch entries from a specific form.
......@@ -110,18 +108,19 @@ class Gen_Caldera_Forms_Data_Collector {
if ( empty( $all_registrants ) ) {
$all_registrants = $this->get_registrants( $registration );
if ( is_wp_error( $all_registrants ) ) {
Gen_Utility::print_errors( $all_registrants );
$all_registrants = [];
}
} else {
$registrants_from_registration = $this->get_registrants( $registration );
if ( is_wp_error( $registrants_from_registration ) ) {
Gen_Utility::print_errors( $registrants_from_registration );
} else {
if ( ! is_wp_error( $registrants_from_registration ) ) {
$all_registrants = array_merge_recursive( $all_registrants, $this->get_registrants( $registration ) );
}
}
}
if ( ! empty( $this->error_list->get_error_code() ) ) {
Gen_Utility::print_errors( $this->error_list );
}
return $all_registrants;
}
......@@ -132,77 +131,83 @@ class Gen_Caldera_Forms_Data_Collector {
* @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 = [];
$registration_errors = new WP_Error();
$attendees = [];
$delegates = [];
$children = [];
if ( isset( $registration['id'] ) ) {
$registration_id = $registration['id'];
} else {
$registration_errors->add( 'registration_id', 'Registration has no valid id.' );
$this->error_list->add( 'registration_id', 'Registration has no valid id.' );
}
// @TODO: Always chack the existence of the slug in the array.
$registrant_transportation_index = 'how_do_you_plan_to_travel_to_the_conference';
$registrant_general_assembly_accommodation_index = 'accommodation_during_the_general_assembly';
$registrant_conference_accommodation_index = 'accommodation_during_the_conference';
$registrant_pos_conference_accommodation_index = 'accommodation_during_the_rive_gathering';
$arrival_date_index = 'arrival_date';
$departure_date_index = 'departure_date';
$accommodation_price_index = 'total_accommodation_price';
$meals_price_index = 'total_meals_price';
if ( isset( $registration['how_many_people_would_you_like_to_register'] ) ) {
$number_of_registrants = $registration['how_many_people_would_you_like_to_register'];
} else {
$registration_errors->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;
}
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 ) {
case 1:
$registrant_name_index = 'full_name';
$registrant_age_index = 'age';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_you';
$registration_type_index = 'i_am_a';
$registrant_name_index = 'full_name';
$registrant_age_index = 'age';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_you';
$registration_type_index = 'i_am_a';
$registration_price_index = '_price';
break;
case 2:
$registrant_name_index = 'full_name_second_person';
$registrant_age_index = 'age_second_person';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_the_second_registrant';
$registration_type_index = 'the_second_registrant_is_a';
$registrant_name_index = 'full_name_second_person';
$registrant_age_index = 'age_second_person';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_the_second_registrant';
$registration_type_index = 'the_second_registrant_is_a';
$registration_price_index = '_price_second_person';
break;
case 3:
$registrant_name_index = 'full_name_third_person';
$registrant_age_index = 'age_third_person';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_the_third_registrant';
$registration_type_index = 'the_third_registrant_is_a';
$registrant_name_index = 'full_name_third_person';
$registrant_age_index = 'age_third_person';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_the_third_registrant';
$registration_type_index = 'the_third_registrant_is_a';
$registration_price_index = '_price_third_person';
break;
case 4:
$registrant_name_index = 'full_name_fourth_person';
$registrant_age_index = 'age_fourth_person';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_the_fourth_registrant';
$registration_type_index = 'the_fourth_registrant_is_a';
$registrant_name_index = 'full_name_fourth_person';
$registrant_age_index = 'age_fourth_person';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_the_fourth_registrant';
$registration_type_index = 'the_fourth_registrant_is_a';
$registration_price_index = '_price_fourth_person';
break;
case 5:
$registrant_name_index = 'full_name_fifth_person';
$registrant_age_index = 'age_fifth_person';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_the_fifth_registrant';
$registration_type_index = 'the_fifth_registrant_is_a';
$registrant_name_index = 'full_name_fifth_person';
$registrant_age_index = 'age_fifth_person';
$registrant_diet_index = 'please_select_the_diets_that_are_relevant_for_the_fifth_registrant';
$registration_type_index = 'the_fifth_registrant_is_a';
$registration_price_index = '_price_fifth_person';
break;
default:
return new WP_Error( 'number_of_registrants', '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;
}
if ( isset( $registration[ $registrant_name_index ] ) ) {
$registrant_name = $registration[ $registrant_name_index ];
} else {
$registration_errors->add( 'registrant_name', '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.' );
}
if ( isset( $registration[ $registrant_age_index ] ) ) {
$registrant_age = $registration[ $registrant_age_index ];
} else {
$registration_errors->add( 'registrant_age', '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_id ) . ' has not been defined.' );
}
if ( isset( $registration[ $registrant_diet_index ] ) ) {
$registrant_diet = $registration[ $registrant_diet_index ];
......@@ -219,20 +224,53 @@ class Gen_Caldera_Forms_Data_Collector {
array_push( $this->diet_types, 'No preferences' );
}
}
if ( $registrant_age < self::MIN_YOUTHS_AGE ) {
$registration_type = 'Kid';
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 ];
if ( strpos( $registration_type, Gen_Delegate::REGISTRATION_TYPE ) !== false ) {
$registration_price_index = 'delegate' . $registration_price_index;
} else {
$registration_price_index = 'attendee' . $registration_price_index;
}
if ( isset( $registration[ $registration_price_index ] ) ) {
$registration_price = $registration[ $registration_price_index ];
} else {
if ( strpos( $registration_type, Gen_Delegate::REGISTRATION_TYPE ) !== false ) {
Gen_Utility::print_alert( 'Delegate price for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
$registration_price = 0;
} 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.' );
}
}
} else {
$registration_errors->add( 'registrant_type', '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.' );
}
if ( isset( $registration[ $registrant_transportation_index ] ) ) {
$transportation = $registration[ $registrant_transportation_index ];
}
$accommodation = [];
if ( isset( $registration[ $accommodation_price_index ] ) ) {
$accommodation_price = $registration[ $accommodation_price_index ] / $number_of_registrants;
} else {
$this->error_list->add( 'accommodation_price', self::class . ': Accommodation price for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
}
if ( isset( $registration[ $meals_price_index ] ) ) {
$meals_price = $registration[ $meals_price_index ] / $number_of_registrants;
} else {
$this->error_list->add( 'meals_price', self::class . ': Meals price for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
}
if ( isset( $registration[ $accommodation_price_index ] ) ) {
$accommodation_price = $registration[ $accommodation_price_index ] / $number_of_registrants;
} else {
$this->error_list->add( 'accommodation_price', self::class . ': Accommodation price for registration ' . esc_html( $registration_id ) . ' has not been defined.' );
}
$accommodation = [];
if ( isset( $registration[ $registrant_general_assembly_accommodation_index ] ) ) {
$accommodation['General Assembly'] = $registration[ $registrant_general_assembly_accommodation_index ];
}
......@@ -249,16 +287,16 @@ class Gen_Caldera_Forms_Data_Collector {
if ( isset( $registration['country'] ) ) {
$country = $registration['country'];
} else {
$registration_errors->add( 'registrant_country', '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( $registration_errors->get_error_code() ) ) {
return $registration_errors;
if ( ! empty( $this->error_list->get_error_code() ) ) {
return $this->error_list;
}
switch ( $registration_type ) {
case 'Attendee':
array_push( $attendees, new Gen_Attendee( $registration_id, $registrant_age, $transportation, $accommodation, $arrival, $departure, $registrant_diet, $country, $registrant_name ) );
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 ) );
break;
case 'Delegate':
if ( ! isset( $registration['please_enter_the_name_of_the_community__national_network_you_are_a_delegate_for'] ) ) {
......@@ -279,15 +317,18 @@ class Gen_Caldera_Forms_Data_Collector {
$departure,
$registrant_diet,
$country,
$registrant_name
$registrant_name,
$registration_price,
$accommodation_price,
$meals_price
)
);
break;
case 'Kid':
array_push( $children, new Gen_Kid( $registration_id, $registrant_age, $transportation, $accommodation, $arrival, $departure, $registrant_diet, $country, $registrant_name ) );
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 ) );
break;
default:
return new WP_Error( 'invalid_registration_type', '<br>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>' );
}
}
......
<?php
/**
* The file that defines a class with constant values to be used in the plugin.
*
* @since 1.0.1
*
* @package Gen_Registration_Tracker
* @subpackage Gen_Registration_Tracker/includes
*/
/**
* The Utility class.
*
* This class implements useful constant values to be used in the plugin.
*
* @since 1.0.1
* @package Gen_Registration_Tracker
* @subpackage Gen_Registration_Tracker/includes
*/
abstract class Gen_Enum {
/**
* Maximun age for a person to be considered a youth.
*
* @since 1.0.1
*/
const MAX_YOUTHS_AGE = 25;
/**
* Minimun age for a person to be considered a youth.
*
* @since 1.0.1
*/
const MIN_YOUTHS_AGE = 13;
/**
* Registration price for Kids.
*
* @since 1.0.1
*/
const KIDS_REGISTRATION_PRICE = 0;
}
......@@ -91,6 +91,7 @@ class Gen_Registration_Tracker {
require_once plugin_dir_path( __FILE__ ) . 'class-gen-caldera-forms-data-collector.php';
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';
// Register style sheet.
add_action( 'admin_enqueue_scripts', array( $this, 'register_styles' ) );
......@@ -165,6 +166,5 @@ class Gen_Registration_Tracker {
$data_source = Gen_Caldera_Forms_Data_Collector::get_instance( 'CF5c1a2c5ed240f' );
$dashboard = Gen_Dashboard_Loader::get_instance( $data_source ); // get instance of a singleton.
Gen_Dashboard_Viewer::display_dashboard( $dashboard );
}
}
......@@ -26,12 +26,36 @@ class Gen_Utility {
* @since 1.0.0
*/
public static function print_errors( $error_list ) {
echo '<div class="error-card">
<header class="gen-container gen-red">
<h2>Error</h2>
</header>
<div class="gen-container">';
foreach ( $error_list->get_error_messages() as $error ) {
echo '<div class="gen-error">';
echo '<div class="gen-message">';
echo '<strong>ERROR</strong>: ';
echo esc_html( $error ) . '<br/>';
echo '</div>';
}
echo '</div>
</div>';
}
/**
* This function prints an alert message.
*
* @param string $alert_message Alert Message.
* @since 1.0.0
*/
public static function print_alert( $alert_message ) {
echo '<div class="alert-card gen-panel gen-yellow">
<span onclick="this.parentElement.style.display=\'none\'"
class="gen-button gen-display-topright">&times;</span>
<h2>Alert</h2>
' . esc_html( $alert_message ) . '
</div>';
}
/**
......
......@@ -212,9 +212,9 @@ class Gen_Dashboard_Loader {
if ( ! isset( $counter['Youths'] ) ) {
$counter['Youths'] = 0;
}
if ( $registrant->get_age() > Gen_Caldera_Forms_Data_Collector::MAX_YOUTHS_AGE ) {
if ( $registrant->get_age() > Gen_Enum::MAX_YOUTHS_AGE ) {
$counter['Adults']++;
} elseif ( $registrant->get_age() >= Gen_Caldera_Forms_Data_Collector::MIN_YOUTHS_AGE ) {
} elseif ( $registrant->get_age() >= Gen_Enum::MIN_YOUTHS_AGE ) {
$counter['Youths']++;
}
}
......
......@@ -18,21 +18,6 @@
* @subpackage Gen_Registration_Tracker/includes
*/
class Gen_Attendee extends Gen_Registrant {
/**
* Initiate an object with its attributes
*
* @param int $registration_id The id of the registration the Registrant belongs to.
* @param int $age The age of the registrant.
* @param string $transportation Tyoe of transportation to go to the event.
* @param array $accommodation Array with types of accommodation reserved for the diferent fase of the event.
* @param DateTime $arrival Arrival date to the event.
* @param DateTime $departure Departure date from the event.
* @param string $diet Type of diet selected by the registrant.
* @param string $country Country the registrant comes from.
* @param string $name Registrant's name.
*/
public function __construct( $registration_id, $age, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name ) {
const REGISTRATION_TYPE = 'Attendee';
parent::__construct( $registration_id, $age, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name );
}
}
......@@ -18,6 +18,7 @@
* @subpackage Gen_Registration_Tracker/includes
*/
class Gen_Delegate extends Gen_Registrant {
const REGISTRATION_TYPE = 'Delegate';
/**
* The name of the organization being represented by this delegate.
*
......@@ -39,10 +40,13 @@ class Gen_Delegate extends Gen_Registrant {
* @param string $diet Type of diet selected by the registrant.
* @param string $country Country the registrant comes from.
* @param string $name Registrant's name.
* @param int $registration_price The price paid for the event registration.
* @param int $accommodation_price The price paid for the event registration.
* @param int $food_price The price paid for the event registration.
*/
public function __construct( $registration_id, $age, $delegate_organization, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name ) {
public function __construct( $registration_id, $age, $delegate_organization, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name, $registration_price, $accommodation_price, $food_price ) {
parent::__construct( $registration_id, $age, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name );
parent::__construct( $registration_id, $age, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name, $registration_price, $accommodation_price, $food_price );
$this->organization = $delegate_organization;
}
/**
......
......@@ -21,21 +21,6 @@
* @subpackage Gen_Registration_Tracker/includes
*/
class Gen_Kid extends Gen_Registrant {
/**
* Initiate an object with its attributes
*
* @param int $registration_id The id of the registration the Registrant belongs to.
* @param int $age The age of the registrant.
* @param string $transportation Tyoe of transportation to go to the event.
* @param array $accommodation Array with types of accommodation reserved for the diferent fase of the event.
* @param DateTime $arrival Arrival date to the event.
* @param DateTime $departure Departure date from the event.
* @param string $diet Type of diet selected by the registrant.
* @param string $country Country the registrant comes from.
* @param string $name Registrant's name.
*/
public function __construct( $registration_id, $age, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name ) {
const REGISTRATION_TYPE = 'Kid';
parent::__construct( $registration_id, $age, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name );
}
}
......@@ -18,6 +18,7 @@
* @subpackage Gen_Registration_Tracker/includes
*/
abstract class Gen_Registrant {
const REGISTRATON_TYPE = 'None';
/**
* Id of the registration the registrant belongs to.
*
......@@ -90,6 +91,30 @@ abstract class Gen_Registrant {
* @var string $country Country the registrant comes from.
*/
private $country;
/**
* The price paid for the event registration.
*
* @since 1.0.1
* @access private
* @var int $registration_price The price paid for the event registration.
*/
private $registration_price;
/**
* The price paid for the accommodation during the event.
*
* @since 1.0.1
* @access private
* @var int $accommodation_price The price paid for the accommodation during the event.
*/
private $accommodation_price;
/**
* The price paid for the food during the event.
*
* @since 1.0.1
* @access private
* @var int $food_price The price paid for the food during the event.
*/
private $food_price;
/**
* Initiate an object with its attributes
*
......@@ -102,8 +127,11 @@ abstract class Gen_Registrant {
* @param string $diet Type of diet selected by the registrant.
* @param string $country Country the registrant comes from.
* @param string $name Registrant's name.
* @param int $registration_price The price paid for the event registration.
* @param int $accommodation_price The price paid for the event registration.
* @param int $food_price The price paid for the event registration.
*/
public function __construct( $id, $registrant_age, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name ) {
public function __construct( $id, $registrant_age, $transportation, $accommodation, $arrival, $departure, $diet, $country, $name, $registration_price, $accommodation_price, $food_price ) {
$this->registration_id = $id;