Commit 9eb458d1 authored by Rodrigo's avatar Rodrigo
Browse files

Refactor Classes Structure

* Create Class Gen_Dashboard_loader
* Create Class Gen_Dashboard_viewer
parent 1f906b28
......@@ -12,6 +12,11 @@
.info-card { width: 95% }
}
.gen-error {
clear:both;
padding: 10px
}
.gen-container {
padding:0.05em 16px 10px 16px;
}
......
......@@ -18,6 +18,14 @@
* @subpackage Gen_Registration_Tracker/includes
*/
class Gen_Caldera_Forms_Data_Collector {
/**
* Class Instance.
*
* @since 1.0.0
* @access private
* @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.
*
......@@ -30,14 +38,57 @@ class Gen_Caldera_Forms_Data_Collector {
* @since 1.0.0
*/
const MIN_YOUTHS_AGE = 13;
/**
* Id of the Caldera Forms form.
*
* @since 1.0.0
* @var int $form_id Caldera Forms form id.
*/
private $form_id;
/**
* 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.
*/
private $all_registrants;
/**
* List of all possible diet types.
*
* @since 1.0.0
* @var array $diet_types array of string with all diet types.
*/
private $diet_types = [];
/**
* Retrieves the class instance.
*
* @param array $form_id Caldera Forms form id.
* @return Gen_Caldera_Forms_Data_Collector $instance A instance of the class.
*/
public static function get_instance( $form_id ) {
if ( ! self::$instance ) {
self::$instance = new Gen_Caldera_Forms_Data_Collector( $form_id );
}
return self::$instance;
}
/**
* Initiate an object with its attributes
*
* @param array $form_id Caldera Forms form id.
*/
private function __construct( $form_id ) {
$this->form_id = $form_id;
$this->all_registrants = self::get_registrants_from_form( $form_id );
}
/** Function that uses Caldera Forms API methods to fetch entries from a specific form.
*
* @since 1.0.0
* @return array with indexes ['Attendees'], ['Delegates'] and ['Children'], each index contain an array of the correspondent objects.
* @param string $form_id Caldera Forms form id.
* @return array with indexes ['Attendees'], ['Delegates'] and ['Children'], each index contain an array of the correspondent objects.
*/
public static function get_registrants_from_form() {
// @TODO Make it possible to select the form.
$form = Caldera_Forms_Forms::get_form( 'CF5c1a2c5ed240f' );
private function get_registrants_from_form( $form_id ) {
$form = Caldera_Forms_Forms::get_form( $form_id );
$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 = [];
......@@ -57,17 +108,17 @@ class Gen_Caldera_Forms_Data_Collector {
// $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 = self::get_registrants( $registration );
$all_registrants = $this->get_registrants( $registration );
if ( is_wp_error( $all_registrants ) ) {
Gen_Utility::print_errors( $all_registrants );
$all_registrants = [];
}
} else {
$registrants_from_registration = self::get_registrants( $registration );
$registrants_from_registration = $this->get_registrants( $registration );
if ( is_wp_error( $registrants_from_registration ) ) {
Gen_Utility::print_errors( $registrants_from_registration );
} else {
$all_registrants = array_merge_recursive( $all_registrants, self::get_registrants( $registration ) );
$all_registrants = array_merge_recursive( $all_registrants, $this->get_registrants( $registration ) );
}
}
}
......@@ -80,7 +131,7 @@ class Gen_Caldera_Forms_Data_Collector {
* @since 1.0.0
* @return array with indexes ['Attendees'], ['Delegates'] and ['Children'], each index contain an array of the correspondent objects.
*/
private static function get_registrants( $registration ) {
private function get_registrants( $registration ) {
$attendees = [];
$delegates = [];
$children = [];
......@@ -155,8 +206,18 @@ class Gen_Caldera_Forms_Data_Collector {
}
if ( isset( $registration[ $registrant_diet_index ] ) ) {
$registrant_diet = $registration[ $registrant_diet_index ];
$types = explode( ',', $registrant_diet );
foreach ( $types as $diet_type ) {
$diet_type = sanitize_text_field( $diet_type );
if ( ! in_array( $diet_type, $this->diet_types, true ) ) {
array_push( $this->diet_types, $diet_type );
}
}
} else {
$registrant_diet = 'No preferences';
if ( ! in_array( 'No preferences', $this->diet_types, true ) ) {
array_push( $this->diet_types, 'No preferences' );
}
}
if ( $registrant_age < self::MIN_YOUTHS_AGE ) {
$registration_type = 'Kid';
......@@ -236,4 +297,20 @@ class Gen_Caldera_Forms_Data_Collector {
'Children' => $children,
);
}
/** Retrieves the list of diet types.
*
* @since 1.0.0
* @return array with all possible diet types.
*/
public function get_diet_types() {
return $this->diet_types;
}
/** Retrieves the list all registrants.
*
* @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;
}
}
......@@ -42,6 +42,14 @@ class Gen_Registration_Tracker {
* @var string $version The current version of the plugin.
*/
protected $version;
/**
* List of errors occurred.
*
* @since 1.0.0
* @access protected
* @var WP_Error $error_list List of errors occurred.
*/
protected $error_list;
/**
* Define the core functionality of the plugin.
*
......@@ -75,12 +83,14 @@ class Gen_Registration_Tracker {
/**
* The Registrant Class used to handle information about each registrant.
*/
require_once plugin_dir_path( __FILE__ ) . 'class-gen-registrant.php';
require_once plugin_dir_path( __FILE__ ) . 'class-gen-attendee.php';
require_once plugin_dir_path( __FILE__ ) . 'class-gen-kid.php';
require_once plugin_dir_path( __FILE__ ) . 'class-gen-delegate.php';
require_once plugin_dir_path( __FILE__ ) . 'registrant/class-gen-registrant.php';
require_once plugin_dir_path( __FILE__ ) . 'registrant/class-gen-attendee.php';
require_once plugin_dir_path( __FILE__ ) . 'registrant/class-gen-kid.php';
require_once plugin_dir_path( __FILE__ ) . 'registrant/class-gen-delegate.php';
require_once plugin_dir_path( __FILE__ ) . 'class-gen-utility.php';
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';
// Register style sheet.
add_action( 'admin_enqueue_scripts', array( $this, 'register_styles' ) );
......@@ -138,399 +148,10 @@ class Gen_Registration_Tracker {
echo '<h1>GEN Registration Tracker</h1>';
$all_registrants = Gen_Caldera_Forms_Data_Collector::get_registrants_from_form();
self::display_age_groups_info_card( $all_registrants );
self::display_transportation_type_info_card( $all_registrants );
self::display_accommodation_type_info_card( $all_registrants );
self::display_diet_info_card( $all_registrants );
self::display_people_by_day_info_card( $all_registrants );
self::display_region_info_card( $all_registrants );
self::display_country_info_card( $all_registrants );
self::display_delegates_info_card( $all_registrants['Delegates'] );
}
/** Function that displays a card containing the information about the Delegates and their organization.
*
* @param array $all_delegates Array of Gen_Delegate objects.
* @since 1.0.0
*/
private static function display_delegates_info_card( $all_delegates ) {
?>
<div class="info-card">
<header class="gen-container gen-blue">
<h1>Delegates</h1>
</header>
<div class="gen-container">
<table class="gen-table gen-striped">
<thead>
<tr>
<th>Name</th>
<th>Organization</th>
</tr>
</thead>
<tbody>
<?php
foreach ( $all_delegates as $delegate ) {
echo '<tr><td><strong>' . esc_html( $delegate->get_name() ) . ' </strong></td><td class="gen-value">' . esc_html( $delegate->get_organization() ) . '</td></tr>';
}
?>
</tbody>
</table>
</div>
</div>
<?php
}
/** Function that displays a card containing the information about the number of participants by region.
*
* @param array $all_registrants Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] containing Gen_Registrant objects.
* @since 1.0.0
*/
private static function display_region_info_card( $all_registrants ) {
$regions_sum = [];
foreach ( $all_registrants as $registrant_group ) {
foreach ( $registrant_group as $registrant ) {
$region = Gen_Utility::country_code_to_region( $registrant->get_country() );
if ( ! array_key_exists( $region, $regions_sum ) ) {
$regions_sum[ $region ] = 0;
}
$regions_sum[ $region ]++;
}
}
arsort( $regions_sum );
?>
<div class="info-card">
<header class="gen-container gen-blue">
<h1>Number of People by Region</h1>
</header>
<div class="gen-container">
<hr>
<?php
foreach ( $regions_sum as $region => $sum ) {
echo '<strong>' . esc_html( $region ) . ' </strong><span class="gen-value">' . esc_html( $sum ) . '</span><hr>';
}
?>
</div>
</div>
<?php
}
/** Function that displays a card containing the information about the number of participants by country.
*
* @param array $all_registrants Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] containing Gen_Registrant objects.
* @since 1.0.0
*/
private static function display_country_info_card( $all_registrants ) {
$countries_sum = [];
foreach ( $all_registrants as $registrant_group ) {
foreach ( $registrant_group as $registrant ) {
if ( ! array_key_exists( $registrant->get_country(), $countries_sum ) ) {
$countries_sum[ $registrant->get_country() ] = 0;
}
$countries_sum[ $registrant->get_country() ]++;
}
}
arsort( $countries_sum );
?>
<div class="info-card">
<header class="gen-container gen-blue">
<h1>Number of People by Country</h1>
</header>
<div class="gen-container">
<hr>
<?php
foreach ( $countries_sum as $country => $sum ) {
echo '<strong>' . esc_html( Gen_Utility::code_to_country( $country ) ) . ' </strong><span class="gen-value">' . esc_html( $sum ) . '</span><hr>';
}
?>
</div>
</div>
<?php
}
/** Function that displays a card containing the information about the number of participants for each diet preference.
*
* @param array $all_registrants Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] containing Gen_Registrant objects.
* @since 1.0.0
*/
private static function display_diet_info_card( $all_registrants ) {
$diet_types_sum = array(
'No preferences' => 0,
'Vegan' => 0,
'Vegetarian' => 0,
'Lactose intolerant' => 0,
'Gluten free' => 0,
'Other' => 0,
);
foreach ( $all_registrants as $registrant_group ) {
foreach ( $registrant_group as $registrant ) {
$types = explode( ',', $registrant->get_diet_type() );
foreach ( $types as $diet_type ) {
if ( array_key_exists( $diet_type, $diet_types_sum ) ) {
$diet_types_sum[ $diet_type ]++;
}
}
}
}
?>
<div class="info-card">
<header class="gen-container gen-blue">
<h1>Number of People for each Diet type</h1>
</header>
<div class="gen-container">
<hr>
<?php
foreach ( $diet_types_sum as $type => $sum ) {
echo '<strong>' . esc_html( $type ) . ' </strong><span class="gen-value">' . esc_html( $sum ) . '</span><hr>';
}
?>
</div>
</div>
<?php
}
/** Function that displays a card containing the information about the number of participants by each day.
*
* @param array $all_registrants Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] containing Gen_Registrant objects.
* @since 1.0.0
*/
private static function display_people_by_day_info_card( $all_registrants ) {
$people_sum_by_day = [];
foreach ( $all_registrants as $registrant_group ) {
foreach ( $registrant_group as $registrant ) {
$arrival_date = $registrant->get_arrival_date()->format( 'd/m/Y' );
$departure_date = $registrant->get_departure_date()->format( 'd/m/Y' );
if ( ! array_key_exists( $arrival_date, $people_sum_by_day ) ) {
$people_sum_by_day[ $arrival_date ] = 0;
}
if ( ! array_key_exists( $departure_date, $people_sum_by_day ) ) {
$people_sum_by_day[ $departure_date ] = 0;
}
$people_sum_by_day[ $arrival_date ] ++;
$people_sum_by_day[ $departure_date ]--;
}
}
ksort( $people_sum_by_day );
?>
<div class="info-card">
<header class="gen-container gen-blue">
<h1>Number of People by Day</h1>
</header>
<div class="gen-container">
<hr>
<?php
$aux = 0;
foreach ( $people_sum_by_day as $date => $sum ) {
$people_sum_by_day[ $date ] = $sum + $aux;
$aux = $people_sum_by_day[ $date ];
echo '<strong>' . esc_html( $date ) . ' </strong><span class="gen-value">' . esc_html( $aux ) . '</span><hr>';
}
?>
</div>
</div>
<?php
}
/** Function that displays a card containing the information about the number of participants registered in each accommodation type.
*
* @param array $all_registrants Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] containing Gen_Registrant objects.
* @since 1.0.0
*/
private static function display_accommodation_type_info_card( $all_registrants ) {
$accommodation_types_sum_by_event_phases = [];
$event_phases = [];
$accommodation_types = [];
foreach ( $all_registrants as $registrant_group ) {
foreach ( $registrant_group as $registrant ) {
foreach ( $registrant->get_accommodation_type() as $phase => $accommodation ) {
if ( ! array_key_exists( $phase, $accommodation_types_sum_by_event_phases ) ) {
$accommodation_types_sum_by_event_phases[ $phase ] = [];
array_push( $event_phases, $phase );
}
if ( ! isset( $accommodation_types_sum_by_event_phases[ $phase ][ $accommodation ] ) ) {
$accommodation_types_sum_by_event_phases[ $phase ][ $accommodation ] = 1;
if ( ! in_array( $accommodation, $accommodation_types, true ) ) {
array_push( $accommodation_types, $accommodation );
}
} else {
$accommodation_types_sum_by_event_phases[ $phase ][ $accommodation ]++;
}
}
}
}
?>
<div class="info-card">
<header class="gen-container gen-blue">
<h1>Accommodation</h1>
</header>
<div class="gen-container">
<table class="gen-table gen-striped">
<thead>
<tr>
<th></th>
<?php
foreach ( $event_phases as $phase ) {
echo '<th>' . esc_html( $phase ) . '</th>';
}
?>
</tr>
</thead>
<?php
foreach ( $accommodation_types as $type ) {
echo '<tr><th>' . esc_html( $type ) . '</th>';
foreach ( $event_phases as $phase ) {
if ( isset( $accommodation_types_sum_by_event_phases[ $phase ][ $type ] ) ) {
echo '<td>' . esc_html( $accommodation_types_sum_by_event_phases[ $phase ][ $type ] ) . '</td>';
} else {
echo '<td>0</td>';
}
}
echo '</tr>';
}
?>
</table>
$this->error_list = new WP_Error();
$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 );
</div>
</div>
<?php
}
/** Function that displays a card containing the information about the number of participants that used each transportation type.
*
* @param array $all_registrants Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] containing Gen_Registrant objects.
* @since 1.0.0
*/
private static function display_transportation_type_info_card( $all_registrants ) {
$transporation_types = [];
foreach ( $all_registrants as $registrant_group ) {
foreach ( $registrant_group as $registrant ) {
if ( ! isset( $transportation_types[ $registrant->get_transportation_type() ] ) ) {
$transportation_types[ $registrant->get_transportation_type() ] = 1;
} else {
$transportation_types[ $registrant->get_transportation_type() ]++;
}
}
}
?>
<div class="info-card">
<header class="gen-container gen-blue">
<h1>Transportation</h1>
</header>
<div class="gen-container">
<hr>
<?php
foreach ( $transportation_types as $transportation => $sum ) {
echo '<strong>' . esc_html( $transportation ) . ' </strong><span class="gen-value">' . esc_html( $sum ) . '</span><hr>';
}
?>
</div>
</div>
<?php
}
/** Function that displays a card containing the information about the number of participants for each age group by registration type.
*
* @param array $all_registrants Contains 3 arrays ['Attendees'], ['Delegates'] and ['Children'] containing Gen_Registrant objects.
* @since 1.0.0
*/
private static function display_age_groups_info_card( $all_registrants ) {
if ( isset( $all_registrants['Delegates'] ) ) {
$delegates_sum = self::get_sum_by_age_ranges( $all_registrants['Delegates'] );
}
if ( isset( $all_registrants['Attendees'] ) ) {
$attendees_sum = self::get_sum_by_age_ranges( $all_registrants['Attendees'] );
}
if ( isset( $all_registrants['Children'] ) ) {
$children_sum = self::get_sum_by_age_ranges( $all_registrants['Children'] );
}
?>
<div class="info-card">
<header class="gen-container gen-blue">
<h1>Registration type and age group</h1>
</header>
<div class="gen-container">
<hr><strong> Number of Delegates: </strong><hr>
<table class="gen-table">
<tr>
<th>Adults</th>
<th>Youths</th>
<th>Total</th>
</tr>
<tr>
<td> <?php echo esc_html( $delegates_sum['Adults'] ); ?> </td>
<td> <?php echo esc_html( $delegates_sum['Youths'] ); ?></td>
<td> <?php echo esc_html( array_sum( $delegates_sum ) ); ?> </td>
</tr>
</table>
<hr><strong> Number of Attendees: </strong><hr>
<table class="gen-table">
<tr>
<th>Adults</th>
<th>Youths</th>
<th>Total</th>
</tr>
<tr>
<td> <?php echo esc_html( $attendees_sum['Adults'] ); ?> </td>
<td> <?php echo esc_html( $attendees_sum['Youths'] ); ?> </td>
<td> <?php echo esc_html( array_sum( $attendees_sum ) ); ?> </td>
</tr>
</table>
<hr><strong> Number of Children: </strong> <?php echo esc_html( $children_sum['Children'] ); ?> <hr>
</div>
</div>
<?php
}
/** Function that iterates through a list of registrants and the number of registrant for each age range
*
* @param array $registrants list of registrants of type Gen_Registrant or extensions.
* @since 1.0.0
* @return array with indexes ['Children'], ['Youths'] and ['Adults']
*/
private static function get_sum_by_age_ranges( $registrants ) {
$children_sum = 0;
$youths_sum = 0;
$adults_sum = 0;
foreach ( $registrants as $registrant ) {
if ( $registrant->get_age() > Gen_Caldera_Forms_Data_Collector::MAX_YOUTHS_AGE ) {
$adults_sum++;
} elseif ( $registrant->get_age() >= Gen_Caldera_Forms_Data_Collector::MIN_YOUTHS_AGE ) {
$youths_sum++;
} else {
$children_sum++;
}
}
return array(
'Children' => $children_sum,
'Youths' => $youths_sum,
'Adults' => $adults_sum,
);
}
}
......@@ -27,7 +27,7 @@ class Gen_Utility {
*/
public static function print_errors( $error_list ) {
foreach ( $error_list->get_error_messages() as $error ) {
echo '<div>';
echo '<div class="gen-error">';
echo '<strong>ERROR</strong>: ';