diff --git a/src/pvv/side/agenda.php b/src/pvv/side/agenda.php index 03054a3..b37e00b 100644 --- a/src/pvv/side/agenda.php +++ b/src/pvv/side/agenda.php @@ -15,30 +15,29 @@ class Agenda { const THIS_MONTH = 3; const NEXT_MONTH = 4; - public function __construct() { - $this->activities = [ - new NerdepitsaActivity, - new AnimekveldActivity, - ]; + public function __construct($activities) { + $this->activities = $activities; } public function getEventsBetween(DateTimeImmutable $from, DateTimeImmutable $to) { - $results = [[], []]; - do { - $run = false; - for($i = 0; $i < sizeof($this->activities); $i++) { - if (sizeof($results[$i])) { - $date = end($results[$i])->getStop(); + $results = []; + for($i = 0; $i < sizeof($this->activities); $i++) { + $result = []; + do { + $run = false; + if (sizeof($result)) { + $date = end($result)->getStop(); } else { $date = $from; } $next = $this->activities[$i]->getNextEventFrom($date); if (isset($next) && $next->getStart() < $to) { - $results[$i][] = $this->activities[$i]->getNextEventFrom($date); + $result[] = $this->activities[$i]->getNextEventFrom($date); $run = true; } - } - } while ($run); + } while ($run); + $results[] = $result; + } $result = []; foreach($results as $a) foreach($a as $b) $result[] = $b; usort($result, function($a, $b) { diff --git a/src/pvv/side/dbactivity.php b/src/pvv/side/dbactivity.php new file mode 100644 index 0000000..a412c5c --- /dev/null +++ b/src/pvv/side/dbactivity.php @@ -0,0 +1,66 @@ +pdo = new PDO($dsn, $username, $password); + $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + public function getNextEventFrom(DateTimeImmutable $date) { + $query = 'SELECT name,start,stop,organiser,location FROM events WHERE start > :date ORDER BY start ASC LIMIT 1'; + return $this->retrieve($date, $query); + } + + public function getPreviousEventFrom(DateTimeImmutable $date) { + $query = 'SELECT name,start,stop,organiser,location FROM events WHERE start < :date ORDER BY start DESC LIMIT 1'; + return $this->retrieve($date, $query); + } + + private function retrieve($date, $query) { + $stmt = $this->pdo->prepare($query); + $stmt->execute(['date' => $date->format('Y-m-d H:i:s')]); + if ($result = $stmt->fetch(PDO::FETCH_ASSOC)){ + $ev = new OnceEvent( + $result['name'], + DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $result['start']), + DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $result['stop']), + $result['organiser'], + $result['location'] + ); + return $ev; + } + return null; + } + + public function getName() /* : string */ { + return "Database"; + } + + public function getLocation() /* : Location */ { + return "Location"; + } + + public function getOrganiser() /* : User */ { + return "User"; + } + + /* + public function getAllEvents(){ + global $url, $user,$pass,$db; + $events = array(); + $mysqli = new mysqli($url,$user,$pass,$db); + $result = $mysqli->query("SELECT name,start,stop,organiser,location FROM events"); + while($row = $result->fetch_assoc()){ + $ev = new OnceEvent($row['name'],$row['start'],$row['stop'],$row['organiser'],$row['location']); + array_push($events,$ev); + } + #array_sort($events); + return $events; + } + */ +} diff --git a/src/pvv/side/onceevent.php b/src/pvv/side/onceevent.php index e8c407a..3b41413 100644 --- a/src/pvv/side/onceevent.php +++ b/src/pvv/side/onceevent.php @@ -1,9 +1,9 @@ name = $name; $this->start = $start; $this->end = $end; diff --git a/www/index.php b/www/index.php index 5c5c56f..4cd56ab 100644 --- a/www/index.php +++ b/www/index.php @@ -1,4 +1,8 @@ - + Programvareverkstedet @@ -40,7 +44,12 @@ -getNextDays() as $period => $events) if ($events && $counter1 < 2 && $counter2 < 10) { $counter1++ ?> + +getNextDays() as $period => $events) if ($events && $counter1 < 2 && $counter2 < 10) { $counter1++ ?>
  • getStart()->format('Y-m-d'); ?>