summaryrefslogtreecommitdiff
path: root/htdocs/src/ModerationSchedule.inc.php
blob: cca669771e660fc4b31e4cbb99fd10051c9bfc6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

/* Foursquare Community Site
 * 
 * Copyright (C) 2011 Foursquare Church.
 * 
 * Developers: Jesse Morgan <jmorgan@foursquarestaff.com>
 *
 */

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);
    }
}

?>