Skip to content

Commit

Permalink
fix: Fix the attendance workflow (#6)
Browse files Browse the repository at this point in the history
* fix: Fix the attendance workflow

* fix tests
  • Loading branch information
xmnlab authored Nov 13, 2023
1 parent 9cd9595 commit b15fa75
Show file tree
Hide file tree
Showing 26 changed files with 534 additions and 194 deletions.
2 changes: 1 addition & 1 deletion conda/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ dependencies:
# - php 8.*
- python
- pip
- makim 1.8.3
- pip:
- makim==1.8.2
- pygments
10 changes: 9 additions & 1 deletion src/attendance_log.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

require_once "./lib/settings.php";

global $URL_BASE;

// Redirect to templates/index.php
header("Location: templates/attendance/log.php");
$params = str_replace("+", "%2B", $_SERVER['QUERY_STRING']);
$url = "{$URL_BASE}/templates/attendance/log.php?{$params}";

header("Location: {$url}");
exit; // Make sure to exit after the redirection
?>
51 changes: 0 additions & 51 deletions src/lib/attendance-old.php

This file was deleted.

17 changes: 9 additions & 8 deletions src/lib/attendance.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

require_once __DIR__ . "/datetime.php";
require_once __DIR__ . "/event.php";
require_once __DIR__ . "/subscription.php";
require_once __DIR__ . "/person.php";
Expand All @@ -10,7 +11,7 @@ class Attendance
public ?int $id;
public Person $person;
public EventSession $eventSession;
public DateTime $logTime;
public ?DateTime $logTime;

public function __construct(
Person $person,
Expand All @@ -21,11 +22,7 @@ public function __construct(
$this->person = $person;
$this->eventSession = $eventSession;

if ($logTime === null) {
$this->logTime = new DateTime();
} else {
$this->logTime = $logTime;
}
$this->logTime = $logTime;
$this->id = $id;
}

Expand Down Expand Up @@ -82,6 +79,10 @@ public static function get(array $data): ?Attendance {

// Execute the statement
$result = $db->query($query);

if (!$result) {
return null;
}
$row = $result->fetchArray(SQLITE3_ASSOC);

if ($row === false) {
Expand All @@ -99,7 +100,7 @@ public static function get(array $data): ?Attendance {
$attendance = new Attendance(
$person,
$event_session,
new DateTime($row['log_time']),
convert_from_utc0(new DateTime($row['log_time'])),
intval($row['id'])
);

Expand All @@ -126,7 +127,7 @@ public function insert(): Attendance
// Prepare the SQL statement
$personId = $this->person->id;
$sessionId = $this->eventSession->id;
$logTime = $this->logTime->format('Y-m-d H:i:s');
$logTime = convert_to_utc0($this->logTime)->format('Y-m-d H:i:s');
$insertQuery = "
INSERT INTO attendance (
person_id, event_session_id, log_time
Expand Down
24 changes: 24 additions & 0 deletions src/lib/datetime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

function convert_from_utc0(DateTime $datetime): DateTime {
$datetime_original = new Datetime(
$datetime->format('Y-m-d H:i:s'),
new DateTimeZone('UTC')
);

$datetime_original->setTimezone(new DateTimeZone('America/La_Paz'));
return $datetime_original;
}

function convert_to_utc0(DateTime $datetime): DateTime {
$datetime_original = new Datetime(
$datetime->format('Y-m-d H:i:s'),
new DateTimeZone('America/La_Paz')
);

$datetime_original->setTimezone(new DateTimeZone('UTC'));

return $datetime_original;
}

?>
80 changes: 56 additions & 24 deletions src/lib/event.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once __DIR__ . "/db.php";
require_once __DIR__ . "/datetime.php";


class EventSangha {
Expand Down Expand Up @@ -52,8 +53,8 @@ public static function get(array $data): ?EventSangha {
return new EventSangha(
$row['name'],
$row['description'],
new DateTime($row['start_date']),
new DateTime($row['end_date']),
convert_from_utc0(new DateTime($row['start_date'])),
convert_from_utc0(new DateTime($row['end_date'])),
intval($row['id'])
);
}
Expand Down Expand Up @@ -88,8 +89,8 @@ public static function list(array $filters = []): array {
public function insert(): EventSangha {
$db = get_db();

$startDateStr = $this->startDate->format('Y-m-d H:i:s');
$endDateStr = $this->endDate->format('Y-m-d H:i:s');
$startDateStr = convert_to_utc0($this->startDate)->format('Y-m-d H:i:s');
$endDateStr = convert_to_utc0($this->endDate)->format('Y-m-d H:i:s');

$insertQuery = "
INSERT INTO event (name, description, start_date, end_date)
Expand All @@ -108,8 +109,8 @@ public function insert(): EventSangha {

public function update(): EventSangha {
$db = get_db();
$startDateStr = $this->startDate->format('Y-m-d H:i:s');
$endDateStr = $this->endDate->format('Y-m-d H:i:s');
$startDateStr = convert_to_utc0($this->startDate)->format('Y-m-d H:i:s');
$endDateStr = convert_to_utc0($this->endDate)->format('Y-m-d H:i:s');

if (!$this->id) {
throw new Exception("This event is not registered yet.");
Expand Down Expand Up @@ -211,8 +212,8 @@ public static function get(array $data): ?EventSession {
$eventSession = new EventSession(
$event,
$row['name'],
new DateTime($row['start_date']),
new DateTime($row['end_date']),
convert_from_utc0(new DateTime($row['start_date'])),
convert_from_utc0(new DateTime($row['end_date'])),
intval($row['id'])
);

Expand All @@ -239,18 +240,8 @@ public static function list(array $filters = []): array {

while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
// Fetch the associated EventSangha
$eventId = intval($row['event_id']);
$event = EventSangha::get(['id' => $eventId]);

$eventSession = new EventSession(
$event,
$row['name'],
new DateTime($row['start_date']),
new DateTime($row['end_date']),
intval($row['id'])
);

$eventSessions[] = $eventSession;
$eventSessionId = intval($row['id']);
$eventSessions[] = EventSession::get(['id' => $eventSessionId]);
}

return $eventSessions;
Expand All @@ -272,12 +263,16 @@ function insert(): EventSession {
$this->validate();

$db = get_db();

$startDate = convert_to_utc0($this->startDate)->format('Y-m-d H:i:s');
$endDate = convert_to_utc0($this->endDate)->format('Y-m-d H:i:s');

$insertQuery = "INSERT INTO event_session (event_id, name, start_date, end_date)
VALUES (
{$this->event->id},
'$this->name',
'{$this->startDate->format('Y-m-d H:i:s')}',
'{$this->endDate->format('Y-m-d H:i:s')}'
'{$startDate}',
'{$endDate}'
)";

$db->exec($insertQuery);
Expand All @@ -301,12 +296,16 @@ function update(): EventSession {
}

$db = get_db();

$startDate = convert_to_utc0($this->startDate)->format('Y-m-d H:i:s');
$endDate = convert_to_utc0($this->endDate)->format('Y-m-d H:i:s');

$updateQuery = "UPDATE event_session
SET
event_id = {$this->event->id},
name = '$this->name',
start_date = '{$this->startDate->format('Y-m-d H:i:s')}',
end_date = '{$this->endDate->format('Y-m-d H:i:s')}'
start_date = '{$startDate}',
end_date = '{$endDate}'
WHERE id = {$this->id}";

$db->exec($updateQuery);
Expand Down Expand Up @@ -345,6 +344,39 @@ public function getPreviousSessions(): array {

return $previousSessions;
}

public static function getNextSession(int $eventId): ?EventSession
{
// Get the current datetime in UTC
$currentDatetimeUtc = (
new DateTime('now', new DateTimeZone('UTC'))
)->format('Y-m-d H:i:s');

// Connect to the database (You can use your own method for this)
$db = get_db();

// Query to find the next session for the specified event
$query = "
SELECT id FROM event_session
WHERE event_id = {$eventId}
AND datetime('{$currentDatetimeUtc}') >= datetime(start_date)
AND datetime('{$currentDatetimeUtc}') <= datetime(end_date)
ORDER BY start_date ASC
LIMIT 1
";

$result = $db->query($query);

if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
// Construct and return the EventSession object
return EventSession::get(
["id" => $row['id']]
);
}

// No next session found
return null;
}
}

?>
3 changes: 3 additions & 0 deletions src/lib/settings.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

date_default_timezone_set("America/La_Paz");

require_once __DIR__ . "/dotenv.php";


Expand Down
16 changes: 10 additions & 6 deletions src/lib/subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
require_once __DIR__ . "/db.php";
require_once __DIR__ . "/person.php";
require_once __DIR__ . "/settings.php";
require_once __DIR__ . "/datetime.php";


class Subscription {
public ?int $id;
public ?Person $person;
public string $datetime;
public DateTime $datetime;
public string $qr;
public ?int $active;

function __construct(
?Person $person=null,
string $qr="",
string $datetime="",
DateTime $datetime=new DateTime(),
?int $id=null,
?int $active=null
) {
Expand Down Expand Up @@ -57,7 +58,7 @@ public static function get(array $data): ?Subscription {
$subscription->id = intval($row['id']);
$subscription->person = Person::get(["id" => $row['id']]);
$subscription->qr = $row['qr'];
$subscription->datetime = $row['datetime'];
$subscription->datetime = convert_from_utc0(new DateTime($row['datetime']));
$subscription->active = intval($row['active']);
}

Expand Down Expand Up @@ -131,13 +132,13 @@ function insert(): Subscription {
$this->person->phone
);

$datetime = date('Y-m-d H:i:s');
$datetime = convert_to_utc0($this->datetime)->format('Y-m-d H:i:s');

$db = get_db();
$person_id = $this->person->id;
// Insert the form data into the 'registrations' table
$insertQuery = "INSERT INTO subscription (person_id, datetime, qr, active)
VALUES ('$person_id', '$this->datetime', '$this->qr', 1)";
VALUES ('$person_id', '$datetime', '$this->qr', 1)";
$db->exec($insertQuery);

// Get the last inserted ID
Expand All @@ -157,12 +158,15 @@ function update(): Subscription {
}

$db = get_db();

$person_id = $this->person->id;
$datetime = convert_to_utc0($this->datetime)->format('Y-m-d H:i:s');

$updateQuery = "
UPDATE subscription
SET
person_id='$person_id',
datetime='$this->datetime',
datetime='$datetime',
qr='$this->qr',
active=$this->active
WHERE id=$this->id";
Expand Down
4 changes: 3 additions & 1 deletion src/migrations/0001.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ function migrate_0001(): void {
attendance_table();
}

migrate_0001();
if (isset($_GET['migrate']) && $_GET['migrate'] == 1) {
migrate_0001();
}
4 changes: 3 additions & 1 deletion src/migrations/0002.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ function migrate_0002(): void {
subscription_add_active_column();
}

migrate_0002();
if (isset($_GET['migrate']) && $_GET['migrate'] == 1) {
migrate_0002();
}
Loading

0 comments on commit b15fa75

Please sign in to comment.