From e121bbebe3ccd990b66a7b04289adf481a4aa2af Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Tue, 30 Aug 2011 12:29:19 -0700 Subject: Working on the moderation schedule iterator --- htdocs/src/ModerationSchedule.inc.php | 105 ++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 htdocs/src/ModerationSchedule.inc.php (limited to 'htdocs/src') diff --git a/htdocs/src/ModerationSchedule.inc.php b/htdocs/src/ModerationSchedule.inc.php new file mode 100644 index 0000000..cca6697 --- /dev/null +++ b/htdocs/src/ModerationSchedule.inc.php @@ -0,0 +1,105 @@ + + * + */ + +require_once "base.inc.php"; + +class ModerationSchedule implements Iterator { + private $moderators; + private $exceptions; + + private $year; + private $week; + private $expos; + + public function __construct() { + $this->moderators = array(); + $this->exceptions = array(); + } + + public function getNumberOfModerators() { + return count($this->moderators); + } + + + // Iterator methods + + public function rewind() { + $this->year = date('o'); + $this->week = date('W') + 0; + $this->expos = 0; + } + + public function current() { + // Get the scheduled mod. + $modpos = $this->week % $this->getNumberOfModerators(); + $moderator = $this->moderators[$modpos]['user_id']; + + // Check for exceptions + if (count($this->exceptions) > 0) { + // Skip exceptions prior to the current() date. + while ( + // We have exceptions to search + $this->expos < count($this->exceptions) and + // and the year is less than the current() year + ($this->exceptions[$this->expos]['year'] < $this->year or + // or if it is the current() year, but less than the week. + ($this->exceptions[$this->expos]['year'] == $this->year + and $this->exceptions[$this->expos]['week'] < $this->week)) + ) { + + $this->expos++; + } + + // Check if the top exception is for today. + if ($this->exceptions[$this->expos]['year'] == $this->year + and $this->exceptions[$this->expos]['week'] == $this->week + ) { + // Yes, return the replacement + $moderator = $this->exceptions[$this->expos]['user_id']; + } + } + + return User::getById($moderator); + } + + public function key() { + // TODO: Return "key" for "current" moderator (date) + } + + public function next() { + // TODO: Impl. next + } + + public function valid() { + // The schedule continues forever. + return true; + } + + private function query() { + $db = getDatabase(); + $this->rewind(); + + // Get the moderators + $query = "SELECT * FROM moderation_schedule ORDER BY position"; + $this->moderators = $db->fetchAssocRows($query); + + // Get the exceptions + $year = date('o'); + $week = date('W'); + + $query = "SELECT * FROM moderator_exceptions" + . " WHERE year >= $year AND week >= $week" + . " ORDER BY year, week"; + $this->exceptions = $db->fetchAssocRows($query); + } +} + +?> + -- cgit v1.2.3