Skip to content

Commit

Permalink
Showing a few slots for scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelpivato committed Jun 2, 2024
1 parent 3e61a00 commit 8ee3e05
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
10 changes: 9 additions & 1 deletion mahoee-scheduling/mahoee-scheduling.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
define('MAHOEE_SCHEDULING_PLUGIN_DIR', plugin_dir_path(__FILE__));

require_once MAHOEE_SCHEDULING_PLUGIN_DIR . 'admin/site-settings.php';
require_once MAHOEE_SCHEDULING_PLUGIN_DIR . 'web/shortcode.php';

// Site admin menu
function mahoee_scheduling_site_menu()
Expand All @@ -35,4 +36,11 @@ function mahoee_scheduling_site_menu()
31
);
}
add_action('admin_menu', 'mahoee_scheduling_site_menu');
add_action('admin_menu', 'mahoee_scheduling_site_menu');

// Register the shortcode
function mahoee_scheduling_register_shortcode()
{
add_shortcode('scheduling_block', 'mahoee_scheduling_shortcode_callback');
}
add_action('init', 'mahoee_scheduling_register_shortcode');
39 changes: 39 additions & 0 deletions mahoee-scheduling/web/scheduling.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.mahoee-scheduling-block,
.mahoee-scheduling-block * {
box-sizing: border-box;
margin: 0;
padding: 0;
}

.mahoee-scheduling-block {
display: flex;
justify-content: center;
padding: 10px;
gap: 10px;
}

.mahoee-scheduling-block .option {
display: flex;
border: 2px solid #d2d2d2;
background-color: #e2e2e2;
border-radius: 10px;
flex-direction: column;
align-items: center;
width: 10rem;
padding: 10px;
gap: 5px;
}

.mahoee-scheduling-block .option .weekday {
font-weight: bold;
}

.mahoee-scheduling-block .option .date {
font-size: 0.8rem;
}

.mahoee-scheduling-block .option .shift {
font-weight: bold;
font-size: 0.8rem;
text-transform: uppercase;
}
6 changes: 6 additions & 0 deletions mahoee-scheduling/web/scheduling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
jQuery(document).ready(function ($) {
$("#main-schedule-button").on("click", function () {
$("#scheduling-options").toggle();
$("#no-days").toggle();
});
});
100 changes: 100 additions & 0 deletions mahoee-scheduling/web/shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

// Shortcode callback function
function mahoee_scheduling_shortcode_callback()
{
// Enqueue required resources
wp_enqueue_script('mahoee-scheduling', plugin_dir_url(__FILE__) . 'scheduling.js', array('jquery'), null, true);
wp_enqueue_style('mahoee-scheduling', plugin_dir_url(__FILE__) . 'scheduling.css');

// Debugging: Ensure the CSS is enqueued
if (wp_style_is('mahoee-scheduling', 'enqueued')) {
echo 'CSS is enqueued.';
} else {
echo 'CSS is NOT enqueued.';
}

// Get site weekdays and shifts
$options = get_option('mahoee_scheduling_recurring');
$weekdays = isset($options['weekdays']) ? $options['weekdays'] : [];
$shifts = isset($options['shifts']) ? $options['shifts'] : [];
$timezone = isset($options['timezone']) ? $options['timezone'] : '';
$weekdays_labels = [
0 => 'Domingo',
1 => 'Segunda-feira',
2 => 'Terça-feira',
3 => 'Quarta-feira',
4 => 'Quinta-feira',
5 => 'Sexta-feira',
6 => 'Sábado',
];
$shifts_labels = [
0 => 'de manhã',
1 => 'de tarde',
2 => 'de noite',
];

$min_random_skip = 2;
$max_random_skip = 5;
$pick_count = 3;
$required_slots = ($pick_count + 1) * $max_random_skip;

// Determine current date and time
date_default_timezone_set($timezone ? $timezone : 'UTC');
$now = new DateTime();
$current_weekday = (int) $now->format('w');
$current_time = (int) $now->format('G');

// Initialize slots array
$slots = [];

// Determine the next slots considering the current time and shifts
$days_checked = 0;
while (count($slots) < $required_slots) {
$weekday = ($current_weekday + $days_checked) % 7;
if (in_array($weekday, $weekdays)) {
foreach ($shifts as $shift) {
if ($days_checked == 0 && $shift <= floor($current_time / 8)) {
// Skip past shifts for today
continue;
}
$date = clone $now;
$date->modify("+$days_checked day");
$formatted_date = $date->format('Y-m-d');
$slots[] = [$formatted_date, $weekdays_labels[$weekday], $shifts_labels[$shift]];
}
}
$days_checked++;
}

// Pick a few slots
$selected_slots = [];
$shifts_missing = array_unique(array_column($slots, 2));
$current_index = 0;
$total_slots = count($slots);
while (count($selected_slots) < $pick_count) {
$current_slot = $slots[$current_index];
$shift = $current_slot[2];
if (empty($shifts_missing) || in_array($shift, $shifts_missing)) {
$selected_slots[] = $current_slot;
$shifts_missing = array_diff($shifts_missing, [$shift]);
}
$random_skip = rand($min_random_skip, $max_random_skip);
$current_index = ($current_index + $random_skip) % $total_slots;
}

// Generate the HTML output
$output = '<div class="mahoee-scheduling-block">';
foreach ($selected_slots as $slot) {
list($date, $weekday_label, $shift_label) = $slot;
$output .= '<div class="option">';
$output .= '<span class="weekday">' . esc_html($weekday_label) . '</span>';
$output .= '<span class="date">' . esc_html($date) . '</span>';
$output .= '<span class="shift">' . esc_html($shift_label) . '</span>';
$output .= '</div>';
}
$output .= '</div>';

// Return output string
return $output;
}

0 comments on commit 8ee3e05

Please sign in to comment.