Skip to content

Commit

Permalink
fix initial issues
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab committed Nov 9, 2023
1 parent 1d42477 commit ac69267
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 118 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
level: 6
paths:
- event
- src
- tests
treatPhpDocTypesAsCertain: false
72 changes: 41 additions & 31 deletions src/lib/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@

require_once __DIR__ . "/db.php";

class Event {

class EventSangha {
public ?int $id;
public string $name;
public string $description;
public DateTime $startDate;
public DateTime $endDate;

function __construct(
string $name="",
string $description="",
DateTime $startDate=null,
DateTime $endDate=null,
public function __construct(
string $name,
string $description,
DateTime $startDate,
DateTime $endDate,
?int $id=null
) {
$this->name = $name;
$this->description = $description;
$this->startDate = $startDate ?? new DateTime();
$this->endDate = $endDate ?? new DateTime();
$this->startDate = $startDate;
$this->endDate = $endDate;
$this->id = $id;
}

public static function get(array $data): ?Event {
/**
* @param array<string,string|int> $data
*/
public static function get(array $data): ?EventSangha {
$db = get_db();

$query = "SELECT * FROM event WHERE 1=1";
Expand All @@ -45,22 +49,27 @@ public static function get(array $data): ?Event {
return null;
}

$event = new Event();
$event->id = intval($row['id']);
$event->name = $row['name'];
$event->description = $row['description'];
$event->startDate = new DateTime($row['start_date']);
$event->endDate = new DateTime($row['end_date']);

return $event;
return new EventSangha(
$row['name'],
$row['description'],
new DateTime($row['start_date']),
new DateTime($row['end_date']),
intval($row['id'])
);
}

/**
* @return ?array<Event>
* @param array<string,string|int> $filter
* @return array<EventSangha>
*/
public static function list(): ?array {
public static function list(array $filters = []): array {
$db = get_db();
$query = "SELECT * FROM event";
$query = "SELECT * FROM event WHERE 1=1";

foreach ($filters as $key => $value) {
$escapedValue = $db->escapeString($value);
$query .= " AND $key='$escapedValue'";
}
$result = $db->query($query);

if (!$result) {
Expand All @@ -70,13 +79,13 @@ public static function list(): ?array {
$event_list = [];

while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$event_list[] = Event::get(["id" => $row['id']]);
$event_list[] = EventSangha::get(["id" => $row['id']]);
}

return $event_list;
}

public function insert(): Event {
public function insert(): EventSangha {
$db = get_db();

$startDateStr = $this->startDate->format('Y-m-d H:i:s');
Expand All @@ -94,10 +103,10 @@ public function insert(): Event {
$lastInsertID = $db->lastInsertRowID();
$db->close();

return Event::get(["id" => $lastInsertID]);
return EventSangha::get(["id" => $lastInsertID]);
}

public function update(): Event {
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');
Expand All @@ -119,7 +128,7 @@ public function update(): Event {
return $this;
}

public function save(): Event {
public function save(): EventSangha {
if ($this->id !== null) {
return $this->update();
} else {
Expand Down Expand Up @@ -156,13 +165,13 @@ public function getFirstSession(): ?EventSession {

class EventSession {
public ?int $id;
public Event $event;
public EventSangha $event;
public string $name;
public DateTime $startDate;
public DateTime $endDate;

function __construct(
Event $event,
EventSangha $event,
string $name,
DateTime $startDate,
DateTime $endDate,
Expand Down Expand Up @@ -195,9 +204,9 @@ public static function get(array $data): ?EventSession {
if ($row === false) {
return null;
} else {
// Fetch the associated Event
// Fetch the associated EventSangha
$eventId = intval($row['event_id']);
$event = Event::get(['id' => $eventId]);
$event = EventSangha::get(['id' => $eventId]);

$eventSession = new EventSession(
$event,
Expand All @@ -212,6 +221,7 @@ public static function get(array $data): ?EventSession {
}

/**
* @param array<string,string|int> $filter
* @return array<EventSession>
*/
public static function list(array $filters = []): array {
Expand All @@ -228,9 +238,9 @@ public static function list(array $filters = []): array {
$eventSessions = [];

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

$eventSession = new EventSession(
$event,
Expand Down
2 changes: 1 addition & 1 deletion src/templates/attendance/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

// Retrieve data for the dropdowns
$personList = Person::list();
$eventList = Event::list();
$eventList = EventSangha::list();
$eventSessionList = EventSession::list();

?>
Expand Down
2 changes: 1 addition & 1 deletion src/templates/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once dirname(__DIR__) . '/lib/settings.php';


function isEnvProduction() {
function isEnvProduction(): bool {
// Get the current server's hostname from $_SERVER['HTTP_HOST']
$currentHostWithPort = $_SERVER['HTTP_HOST'];

Expand Down
4 changes: 2 additions & 2 deletions src/templates/event-session/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
include dirname(__DIR__) . "/header.php";
require_once dirname(dirname(__DIR__)) . "/lib/event.php";

$eventList = Event::list();
$eventList = EventSangha::list();

// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
Expand All @@ -14,7 +14,7 @@

$eventSession = null;
try {
$event_selected = Event::get(["id" => $eventId]);
$event_selected = EventSangha::get(["id" => $eventId]);
$eventSession = new EventSession(
$event_selected, $name, $startDateTime, $endDateTime
);
Expand Down
4 changes: 2 additions & 2 deletions src/templates/event/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// Check if an event ID is provided for updating
if (isset($_GET['id'])) {
$event = Event::get(['id' => $_GET['id']]);
$event = EventSangha::get(['id' => $_GET['id']]);
if ($event) {
$isNewEvent = false;
}
Expand All @@ -28,7 +28,7 @@

// Create or update the event
if ($isNewEvent) {
$event = new Event($name, $description, $startDate, $endDate);
$event = new EventSangha($name, $description, $startDate, $endDate);
$event->save();
} else {
$event->name = $name;
Expand Down
2 changes: 1 addition & 1 deletion src/templates/event/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require_once dirname(dirname(__DIR__)) . "/lib/event.php";
include dirname(__DIR__) . "/header.php";

$result = Event::list();
$result = EventSangha::list();

?>
<h2>Event List</h2>
Expand Down
18 changes: 9 additions & 9 deletions src/templates/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@
<strong>Menu Suscripciones</strong>
<div class="list-group">
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/subscription/form.php">Suscripción manual</a>
href="./subscription/form.php">Suscripción manual</a>
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/subscription/list.php">Lista de suscripciones</a>
href="./subscription/list.php">Lista de suscripciones</a>
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/subscription/upload.php">Upload archivo de suscripciones</a>
href="./subscription/upload.php">Upload archivo de suscripciones</a>
</div>
</div>
<div class="container">
<strong>Menu Asistencia</strong>
<div class="list-group">
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/attendance/form.php">Registro manual de asistencia</a>
href="./attendance/form.php">Registro manual de asistencia</a>
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/attendance/list.php">Lista de asistencia</a>
href="./attendance/list.php">Lista de asistencia</a>
</div>
</div>

<div class="container">
<strong>Events</strong>
<div class="list-group">
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/event/list.php">Event List</a>
href="./event/list.php">Event List</a>
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/event/form.php">Event Form</a>
href="./event/form.php">Event Form</a>
</div>
</div>

<div class="container">
<strong>Events' Session</strong>
<div class="list-group">
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/event-session/list.php">Event Sessions List</a>
href="./event-session/list.php">Event Sessions List</a>
<a class="list-group-item list-group-item-action"
href="<?php $BASE_URL?>/templates/event-session/form.php">Event Sessions Form</a>
href="./event-session/form.php">Event Sessions Form</a>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/templates/subscription/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class="btn btn-success"
style="background:#1CD324!important;" />
</form>
<?php } else { ?>
<form action="<?php $BASE_URL?>/subscription/activation.php" method="POST">
<form action="./activation.php" method="POST">
<input type="hidden" name="id" value="<?php echo $subscription->id; ?>" />
<input type="hidden" name="active" value="0" />
<input type="submit"
Expand Down
2 changes: 1 addition & 1 deletion src/templates/subscription/resend_email.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require_once __DIR__ . "/lib/subscription.php";
require_once dirname(dirname(__DIR__)) . "/lib/subscription.php";

session_start();

Expand Down
10 changes: 7 additions & 3 deletions tests/AttendanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

class AttendanceTest extends TestCase
{
public Person $person;
public EventSangha $event;
public EventSession $eventSession;

public function setUp(): void
{
clean_db();
Expand All @@ -32,7 +36,7 @@ public function setUp(): void
$oneHourAfter->modify('+1 hour');

$this->event = (
new Event(
new EventSangha(
"Event1",
"Event1-Description",
$oneHourBefore,
Expand All @@ -50,7 +54,7 @@ public function setUp(): void
}

// Test the log method
public function testLogAttendance()
public function testLogAttendance(): void
{
$attendance = Attendance::log(
$this->person,
Expand All @@ -62,7 +66,7 @@ public function testLogAttendance()
}

// Test the insert method (if necessary)
public function testInsertAttendance()
public function testInsertAttendance(): void
{
// Create an instance of Attendance (replace with actual constructor arguments)
$attendance = new Attendance(
Expand Down
Loading

0 comments on commit ac69267

Please sign in to comment.