From 33f663ed35a2ed0f2fa0535ce0e47a382dda09d7 Mon Sep 17 00:00:00 2001 From: michael-grace Date: Fri, 4 Sep 2020 00:07:01 +0100 Subject: [PATCH 1/3] getters for MyRadio_Event --- src/Classes/ServiceAPI/MyRadio_Event.php | 132 +++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/Classes/ServiceAPI/MyRadio_Event.php diff --git a/src/Classes/ServiceAPI/MyRadio_Event.php b/src/Classes/ServiceAPI/MyRadio_Event.php new file mode 100644 index 000000000..754e8b7f9 --- /dev/null +++ b/src/Classes/ServiceAPI/MyRadio_Event.php @@ -0,0 +1,132 @@ +event_id = $data["event_id"]; + $this->title = $data["title"]; + $this->description = $data["description"]; + $this->timeslots = $data["timeslots"]; + } + + public function getID(){ + return $this->event_id; + } + + /** + * Get the title of the event + * @return string + */ + public function getTitle(){ + return $this->title; + } + + /** + * Get the description of the event + * @return string + */ + public function getDescription(){ + return $this->description; + } + + /** + * Get the timeslots that are part of the event + * @return MyRadio_Timeslot[] + */ + public function getTimeslots(){ + if (!isset($this->timeslots)){ + $sql = "SELECT show_season_timeslot_id FROM schedule.event_timeslots + WHERE event_id = $1"; + $rows = self::$db->fetchAll($sql, [$this->getID()]); + foreach ($rows as $row){ + $this->timeslots[] = MyRadio_Timeslot::getInstance($row["show_season_timeslot_id"]); + } + } + return $this->timeslots; + } + + public function toDataSource($mixins = []) + { + return[ + "id" => $this->getID(), + "title" => $this->getTitle(), + "description" => $this->getDescription(), + "timeslots" => $this->getTimeslots() + ]; + } + + /** + * Get all events + * @return MyRadio_Event[] + */ + public static function getAll(){ + $sql = "SELECT event_id, title, description FROM schedule.events"; + $rows = self::$db->fetchAll($sql); + + $events = []; + foreach ($rows as $row){ + $new_event = new self($row); + $new_event->getTimeslots(); + $events[] = $new_event; + } + + return CoreUtils::setToDataSource($events); + } + + protected static function factory($itemid) + { + $sql = "SELECT event_id, title, description FROM schedule.events + WHERE event_id = $1 LIMIT 1"; + $result = self::$db->fetchOne($sql, [$itemid]); + + if (empty($result)){ + throw new MyRadioException("That Event doesn't exist.", 404); + } + + $new_event = new self($result); + $new_event->getTimeslots(); + return $new_event; + } + + + } \ No newline at end of file From a58cab459acacc829e053b130c38232ac1989130 Mon Sep 17 00:00:00 2001 From: michael-grace Date: Fri, 4 Sep 2020 14:14:51 +0100 Subject: [PATCH 2/3] MyRadio_Event pt2 --- schema/patches/5.sql | 22 ++ src/Classes/ServiceAPI/MyRadio_Event.php | 262 ++++++++++++-------- src/Classes/ServiceAPI/MyRadio_Timeslot.php | 36 +++ 3 files changed, 223 insertions(+), 97 deletions(-) create mode 100644 schema/patches/5.sql diff --git a/schema/patches/5.sql b/schema/patches/5.sql new file mode 100644 index 000000000..18bb1194f --- /dev/null +++ b/schema/patches/5.sql @@ -0,0 +1,22 @@ +BEGIN; + +create table schedule.events +( + event_id text not null + constraint events_pk + primary key, + title text not null, + description text +); + +create table schedule.event_timeslots +( + event_id text not null + constraint event_timeslots_events_event_id_fk + references schedule.events, + show_season_timeslot_id int not null + constraint event_timeslots_show_season_timeslot_show_season_timeslot_id_fk + references schedule.show_season_timeslot +); + +COMMIT; \ No newline at end of file diff --git a/src/Classes/ServiceAPI/MyRadio_Event.php b/src/Classes/ServiceAPI/MyRadio_Event.php index 754e8b7f9..0c71b31a9 100644 --- a/src/Classes/ServiceAPI/MyRadio_Event.php +++ b/src/Classes/ServiceAPI/MyRadio_Event.php @@ -4,7 +4,7 @@ * Provides the Event class for MyRadio */ - namespace MyRadio\ServiceAPI; +namespace MyRadio\ServiceAPI; use MyRadio\MyRadio\CoreUtils; use MyRadio\MyRadioException; @@ -15,118 +15,186 @@ * @uses \Database */ - class MyRadio_Event extends ServiceAPI{ - /** - * The ID of the Event - * Unique name, i.e. for URL - * @var string - */ - private $event_id; - - /** - * The Title of the Event - * @var string - */ - private $title; - - /** - * The Description of the Event - */ - private $description; - - /** - * The Timeslots part of this event - * @var MyRadio_Timeslot[] - */ - private $timeslots; - - public function __construct($data) - { - parent::__construct(); - $this->event_id = $data["event_id"]; - $this->title = $data["title"]; - $this->description = $data["description"]; - $this->timeslots = $data["timeslots"]; - } - - public function getID(){ - return $this->event_id; - } - - /** - * Get the title of the event - * @return string - */ - public function getTitle(){ - return $this->title; - } - - /** - * Get the description of the event - * @return string - */ - public function getDescription(){ - return $this->description; - } - - /** - * Get the timeslots that are part of the event - * @return MyRadio_Timeslot[] - */ - public function getTimeslots(){ - if (!isset($this->timeslots)){ +class MyRadio_Event extends ServiceAPI +{ + /** + * The ID of the Event + * Unique name, i.e. for URL + * @var string + */ + private $event_id; + + /** + * The Title of the Event + * @var string + */ + private $title; + + /** + * The Description of the Event + */ + private $description; + + /** + * The Timeslots part of this event + * @var int[] + */ + private $timeslots; + + public function __construct($data) + { + parent::__construct(); + $this->event_id = $data["event_id"]; + $this->title = $data["title"]; + $this->description = $data["description"]; + $this->timeslots = $data["timeslots"]; + } + + public function getID() + { + return $this->event_id; + } + + /** + * Get the title of the event + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * Get the description of the event + * @return string + */ + public function getDescription() + { + return $this->description; + } + + /** + * Get the timeslots that are part of the event + * @return MyRadio_Timeslot[] + */ + public function getTimeslots() + { + if (!isset($this->timeslots)) { $sql = "SELECT show_season_timeslot_id FROM schedule.event_timeslots - WHERE event_id = $1"; + INNER JOIN schedule.show_season_timeslot USING (show_season_timeslot_id) + WHERE event_id = $1 + ORDER BY start_time"; $rows = self::$db->fetchAll($sql, [$this->getID()]); - foreach ($rows as $row){ - $this->timeslots[] = MyRadio_Timeslot::getInstance($row["show_season_timeslot_id"]); + foreach ($rows as $row) { + $this->timeslots[] = $row["show_season_timeslot_id"]; } - } - return $this->timeslots; - } - - public function toDataSource($mixins = []) - { - return[ - "id" => $this->getID(), - "title" => $this->getTitle(), - "description" => $this->getDescription(), - "timeslots" => $this->getTimeslots() - ]; - } - - /** - * Get all events - * @return MyRadio_Event[] - */ - public static function getAll(){ + } + return MyRadio_Timeslot::resultSetToObjArray($this->timeslots); + } + + public function toDataSource($mixins = []) + { + return [ + "id" => $this->getID(), + "title" => $this->getTitle(), + "description" => $this->getDescription(), + "timeslots" => $this->timeslots, + ]; + } + + /** + * Get all events + * @return MyRadio_Event[] + */ + public static function getAll() + { $sql = "SELECT event_id, title, description FROM schedule.events"; $rows = self::$db->fetchAll($sql); $events = []; - foreach ($rows as $row){ + foreach ($rows as $row) { $new_event = new self($row); $new_event->getTimeslots(); $events[] = $new_event; } return CoreUtils::setToDataSource($events); - } + } - protected static function factory($itemid) - { - $sql = "SELECT event_id, title, description FROM schedule.events + protected static function factory($itemid) + { + $sql = "SELECT event_id, title, description FROM schedule.events WHERE event_id = $1 LIMIT 1"; - $result = self::$db->fetchOne($sql, [$itemid]); + $result = self::$db->fetchOne($sql, [$itemid]); + + if (empty($result)) { + throw new MyRadioException("That Event doesn't exist.", 404); + } - if (empty($result)){ - throw new MyRadioException("That Event doesn't exist.", 404); - } + $new_event = new self($result); + $new_event->getTimeslots(); + return $new_event; + } + + /** + * Creates a new MyRadio_Event and returns it as an object + * + * @param array $params + * Required: event_id, title + * Optional: description + * + * @return MyRadio_Event + * + * @throws MyRadioException + */ + + public static function create($params = []) + { + // Check Required Fields + $required = ["event_id", "title"]; + foreach ($required as $field) { + if (!isset($params[$field])) { + throw new MyRadioException("Parameter " . $field . "wasn't provided.", 400); + } + } - $new_event = new self($result); - $new_event->getTimeslots(); - return $new_event; - } + // Check Unique ID + $result = self::$db->fetchOne("SELECT COUNT(1) FROM schedule.events + WHERE event_id = $1", [$params["event_id"]]); + if (sizeof($result) > 0) { + throw new MyRadioException("Event ID isn't unique.", 400); + } - } \ No newline at end of file + // Add Event + self::$db->query("INSERT INTO schedule.events + (event_id, title, description) + VALUES ($1, $2, $3)", [$params["event_id"], $params["title"], $params["description"]]); + + return self::getInstance($params["event_id"]); + } + + /** + * Add timeslot to event + * + * @param $timeslotID + * + * @throws MyRadioException + */ + + public function addTimeslot($timeslotID) + { + try { + MyRadio_Timeslot::getInstance($timeslotID); + if (!in_array($timeslotID, $this->timeslots)) { + $this->timeslots[] = $timeslotID; + self::$db->query("INSERT INTO schedule.event_timeslots + (event_id, show_season_timeslot_id) + VALUES ($1, $2)", [$this->getID(), $timeslotID]); + } + } catch (MyRadioException $e) { + // Timeslot doesn't exist + throw $e; + } + } +} diff --git a/src/Classes/ServiceAPI/MyRadio_Timeslot.php b/src/Classes/ServiceAPI/MyRadio_Timeslot.php index 2d37d7cad..f86ce606f 100644 --- a/src/Classes/ServiceAPI/MyRadio_Timeslot.php +++ b/src/Classes/ServiceAPI/MyRadio_Timeslot.php @@ -35,6 +35,7 @@ class MyRadio_Timeslot extends MyRadio_Metadata_Common private $timeslot_num; protected $owner; protected $credits; + private $events; protected function __construct($timeslot_id) { @@ -142,6 +143,11 @@ protected function __construct($timeslot_id) $this->credits[] = ['type' => (int)$credit_types[$i], 'memberid' => $credits[$i], 'User' => MyRadio_User::getInstance($credits[$i]),]; } + + // Deal with Events the timeslot could be a part of + $this->events = self::$db->fetchColumn("SELECT event_id FROM schedule.event_timeslots + WHERE show_season_timeslot_id = $1", [$this->timeslot_id]); + } public function getMeta($meta_string) @@ -216,6 +222,35 @@ public function getEndTime() return $this->getStartTime() + $duration; } + /** + * Returns the events the timeslot is a part of + * + * @return MyRadio_Event[] + */ + public function getEvents(){ + return MyRadio_Event::resultSetToObjArray($this->events); + } + + /** + * Can add this timeslot to an event + * + * @param $event_id + * + * @return MyRadio_Event + * + * @throws MyRadioException + */ + + public function addEvent($event_id){ + try{ + $event = MyRadio_Event::getInstance($event_id); + $event->addTimeslot($this->getID()); + return $event; + }catch (MyRadioException $e){ + throw $e; + } + } + /** * Gets the Timeslot that is on after this. * @@ -342,6 +377,7 @@ public function toDataSource($mixins = []) 'mixcloud_status' => $this->getMeta('upload_state'), 'mixcloud_starttime' => $this->getMeta('upload_starttime'), 'mixcloud_endtime' => $this->getMeta('upload_endtime'), + 'events' => $this->events, 'rejectlink' => [ 'display' => 'icon', 'value' => 'trash', From ecf847288380ad47c06b5054af24f83ab928e8e3 Mon Sep 17 00:00:00 2001 From: michael-grace Date: Fri, 4 Sep 2020 14:34:02 +0100 Subject: [PATCH 3/3] Event fixes --- schema/api.json | 14 ++++++++------ src/Classes/ServiceAPI/MyRadio_Event.php | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/schema/api.json b/schema/api.json index 98d54d985..34e96bc6d 100644 --- a/schema/api.json +++ b/schema/api.json @@ -4,6 +4,7 @@ "\\MyRadio\\ServiceAPI\\MyRadio_Show": "show", "\\MyRadio\\ServiceAPI\\MyRadio_Season": "season", "\\MyRadio\\ServiceAPI\\MyRadio_Timeslot": "timeslot", + "\\MyRadio\\ServiceAPI\\MyRadio_Event": "event", "\\MyRadio\\ServiceAPI\\MyRadio_Album": "album", "\\MyRadio\\ServiceAPI\\MyRadio_Demo": "demo", "\\MyRadio\\ServiceAPI\\MyRadio_List": "list", @@ -145,11 +146,12 @@ } }, "anyOf": [{ - "required": ["eduroam"] - }, - { - "required": ["email"] - }] + "required": ["eduroam"] + }, + { + "required": ["email"] + } + ] } } -} +} \ No newline at end of file diff --git a/src/Classes/ServiceAPI/MyRadio_Event.php b/src/Classes/ServiceAPI/MyRadio_Event.php index 0c71b31a9..bbfb111a2 100644 --- a/src/Classes/ServiceAPI/MyRadio_Event.php +++ b/src/Classes/ServiceAPI/MyRadio_Event.php @@ -86,7 +86,7 @@ public function getTimeslots() ORDER BY start_time"; $rows = self::$db->fetchAll($sql, [$this->getID()]); foreach ($rows as $row) { - $this->timeslots[] = $row["show_season_timeslot_id"]; + $this->timeslots[] = intval($row["show_season_timeslot_id"]); } } return MyRadio_Timeslot::resultSetToObjArray($this->timeslots);