final public function join_badminton_date(BadmintonDate $badminton_date) { /* (Date) -> Bool The current user attempts to join the badminton date */ if ($this->user_id && $badminton_date->date_id) { $sql = "INSERT INTO joins (date_id, user_id, date_joined, status) VALUES('$badminton_date->date_id', '$this->user_id', NOW(), '" . self::JOINED_STATUS . "') ON DUPLICATE KEY UPDATE join_id = join_id"; //echo $sql; $result = $this->dbc->query($sql) or die ($this->dbc->error); $affected_rows = $this->dbc->affected_rows; if ($affected_rows == 1) { //New row was inserted //Push the notificatoins and check for method allowed $this->get_fields(); $action = new JoinBadmintonDate(array( 'joiner' => clone $this, 'badminton_date' => $badminton_date, 'trigger' => clone $this) ); //Push action ActionPusher::push_action($action); //Push notification, only if the current user is not the creator of the badminton date //Push the pending action to evaluate the hoster, only if the current user is not the creator of the badminton date if ($this->user_id != $badminton_date->creator->user_id) { NotificationPusher::push_notification($action); $sql = "SELECT end_datetime FROM badminton_date WHERE date_id = '$badminton_date->date_id'"; $result = $this->dbc->query($sql) or die ($this->dbc->error); if ($result->num_rows == 1){ $end_datetime = mysqli_fetch_row($result)[0]; //Issue the pending endorsement $sql = "INSERT INTO `pending_actions` (date_issued, awaiting_user, active) VALUES ('$end_datetime', '$this->user_id', '" . self::ACTIVE . "')"; $result = $this->dbc->query($sql) or die ($this->dbc->error); $token_id = $this->dbc->insert_id; $badminton_date->get_creator(); $sql = "INSERT INTO `pending_user_endorsements` (token_id, user_to_endorse, date_id) VALUES ('$token_id', '{$badminton_date->creator->user_id}', '$badminton_date->date_id')"; $result = $this->dbc->query($sql) or die ($this->dbc->error); return true; } else { throw new OutOfRangeException('OutOfRangeException occured on method call ' . __METHOD__ . ' because the date id does not exist'); } } else { return true; } } return true; } else { return false; } }
final public function leave_date(BadmintonDate $badminton_date) { /* (BadmintonDate) -> Bool Attempts for the current user to leave the badminton date For now this is the notify absence function */ if ($this->user_id && $badminton_date->date_id) { if ($this->in_date($badminton_date)) { if (strtotime($badminton_date->begin_datetime) > time()) { if ($badminton_date->can_be_left()) { $sql = "UPDATE `joins` SET status = '" . self::LEFT_STATUS . "' WHERE date_id = '$badminton_date->date_id' AND user_id = '$this->user_id' LIMIT 1"; $result = $this->dbc->query($sql) or die ($this->dbc->error); //echo $sql; $action = new LeaveBadmintonDate(array( 'leaver' => clone $this, 'badminton_date' => $badminton_date) ); $affected_rows = $this->dbc->affected_rows; if ($affected_rows == 1) { //New row was inserted //Push the action ActionPusher::push_action($action); //Push notifications to the group NotificationPusher::push_notification($action); } return true; } else { return false; } } else { throw new OutOfBoundsException('OutOfBoundsException occured on method call leave_date, the date has already passed and cannot be left'); } } else { throw new OutOfRangeException('OutOfRangeException occured on method call leave_date since the current user is not even in the badminton date'); } } else { throw new UnexpectedValueException('UnexpectedValueException occured on request on method call leave_date, invalid parameters'); } }