xref: /plugin/acknowledge/helper.php (revision ef3ab392f1294956892e162b1b47539c91a789f3)
14d6d17d0SAndreas Gohr<?php
24d6d17d0SAndreas Gohr/**
34d6d17d0SAndreas Gohr * DokuWiki Plugin acknowledge (Helper Component)
44d6d17d0SAndreas Gohr *
54d6d17d0SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
64d6d17d0SAndreas Gohr * @author  Andreas Gohr, Anna Dabrowska <dokuwiki@cosmocode.de>
74d6d17d0SAndreas Gohr */
84d6d17d0SAndreas Gohr
94d6d17d0SAndreas Gohr
104d6d17d0SAndreas Gohrclass helper_plugin_acknowledge extends DokuWiki_Plugin
114d6d17d0SAndreas Gohr{
124d6d17d0SAndreas Gohr
13cabb51d3SAndreas Gohr    /**
14cabb51d3SAndreas Gohr     * @return helper_plugin_sqlite|null
15cabb51d3SAndreas Gohr     */
16cabb51d3SAndreas Gohr    public function getDB()
17cabb51d3SAndreas Gohr    {
18cabb51d3SAndreas Gohr        /** @var \helper_plugin_sqlite $sqlite */
19cabb51d3SAndreas Gohr        $sqlite = plugin_load('helper', 'sqlite');
20cabb51d3SAndreas Gohr        if ($sqlite === null) {
21cabb51d3SAndreas Gohr            msg($this->getLang('error sqlite plugin missing'), -1);
22cabb51d3SAndreas Gohr            return null;
23cabb51d3SAndreas Gohr        }
24cabb51d3SAndreas Gohr        if (!$sqlite->init('acknowledgement', __DIR__ . '/db')) {
25cabb51d3SAndreas Gohr            return null;
26cabb51d3SAndreas Gohr        }
27cabb51d3SAndreas Gohr
28cabb51d3SAndreas Gohr        return $sqlite;
29cabb51d3SAndreas Gohr    }
30cabb51d3SAndreas Gohr
31cabb51d3SAndreas Gohr    /**
32*ef3ab392SAndreas Gohr     * Delete a page
33*ef3ab392SAndreas Gohr     *
34*ef3ab392SAndreas Gohr     * Cascades to delete all assigned data, etc.
35*ef3ab392SAndreas Gohr     *
36*ef3ab392SAndreas Gohr     * @param string $page Page ID
37*ef3ab392SAndreas Gohr     */
38*ef3ab392SAndreas Gohr    public function removePage($page)
39*ef3ab392SAndreas Gohr    {
40*ef3ab392SAndreas Gohr        $sqlite = $this->getDB();
41*ef3ab392SAndreas Gohr        if (!$sqlite) return;
42*ef3ab392SAndreas Gohr
43*ef3ab392SAndreas Gohr        $sql = "DELETE FROM pages WHERE page = ?";
44*ef3ab392SAndreas Gohr        $sqlite->query($sql, $page);
45*ef3ab392SAndreas Gohr    }
46*ef3ab392SAndreas Gohr
47*ef3ab392SAndreas Gohr    /**
48*ef3ab392SAndreas Gohr     * Update last modified date of page
49*ef3ab392SAndreas Gohr     *
50*ef3ab392SAndreas Gohr     * @param string $page Page ID
51*ef3ab392SAndreas Gohr     * @param int $lastmod timestamp of last non-minor change
52*ef3ab392SAndreas Gohr     */
53*ef3ab392SAndreas Gohr    public function storePageDate($page, $lastmod)
54*ef3ab392SAndreas Gohr    {
55*ef3ab392SAndreas Gohr        $sqlite = $this->getDB();
56*ef3ab392SAndreas Gohr        if (!$sqlite) return;
57*ef3ab392SAndreas Gohr
58*ef3ab392SAndreas Gohr        $sql = "REPLACE INTO pages (page, lastmod) VALUES (?,?)";
59*ef3ab392SAndreas Gohr        $sqlite->query($sql, $page, $lastmod);
60*ef3ab392SAndreas Gohr    }
61*ef3ab392SAndreas Gohr
62*ef3ab392SAndreas Gohr    /**
63cabb51d3SAndreas Gohr     * @param string $page Page ID
64cabb51d3SAndreas Gohr     * @param string $assignees comma separated list of users and groups
65cabb51d3SAndreas Gohr     */
66cabb51d3SAndreas Gohr    public function setAssignees($page, $assignees)
67cabb51d3SAndreas Gohr    {
68cabb51d3SAndreas Gohr        $sqlite = $this->getDB();
69cabb51d3SAndreas Gohr        if (!$sqlite) return;
70cabb51d3SAndreas Gohr
71cabb51d3SAndreas Gohr        $sql = "REPLACE INTO assignments ('page', 'assignee') VALUES (?,?)";
72cabb51d3SAndreas Gohr        $sqlite->query($sql, $page, $assignees);
73cabb51d3SAndreas Gohr    }
74cabb51d3SAndreas Gohr
75*ef3ab392SAndreas Gohr    /**
76*ef3ab392SAndreas Gohr     * Clears assignements for a page
77*ef3ab392SAndreas Gohr     *
78*ef3ab392SAndreas Gohr     * @param string $page Page ID
79*ef3ab392SAndreas Gohr     */
80*ef3ab392SAndreas Gohr    public function clearAssignments($page)
81*ef3ab392SAndreas Gohr    {
82*ef3ab392SAndreas Gohr        $sqlite = $this->getDB();
83*ef3ab392SAndreas Gohr        if (!$sqlite) return;
84cabb51d3SAndreas Gohr
85*ef3ab392SAndreas Gohr        $sql = "DELETE FROM assignments WHERE page = ?";
86*ef3ab392SAndreas Gohr        $sqlite->query($sql, $page);
87*ef3ab392SAndreas Gohr    }
88*ef3ab392SAndreas Gohr
89*ef3ab392SAndreas Gohr
90*ef3ab392SAndreas Gohr    /**
91*ef3ab392SAndreas Gohr     * Is the given user one of the assignees for this page
92*ef3ab392SAndreas Gohr     *
93*ef3ab392SAndreas Gohr     * @param string $page Page ID
94*ef3ab392SAndreas Gohr     * @param string $user user name to check
95*ef3ab392SAndreas Gohr     * @param string[] $groups groups this user is in
96*ef3ab392SAndreas Gohr     * @return bool
97*ef3ab392SAndreas Gohr     */
98*ef3ab392SAndreas Gohr    public function isUserAssigned($page, $user, $groups)
99*ef3ab392SAndreas Gohr    {
100*ef3ab392SAndreas Gohr        $sqlite = $this->getDB();
101*ef3ab392SAndreas Gohr        if (!$sqlite) return false;
102*ef3ab392SAndreas Gohr
103*ef3ab392SAndreas Gohr
104*ef3ab392SAndreas Gohr        $sql = "SELECT assignee FROM assignments WHERE page = ?";
105*ef3ab392SAndreas Gohr        $result = $sqlite->query($sql, $page);
106*ef3ab392SAndreas Gohr        $assignees = (string)$sqlite->res2single($result);
107*ef3ab392SAndreas Gohr        $sqlite->res_close($result);
108*ef3ab392SAndreas Gohr
109*ef3ab392SAndreas Gohr        return auth_isMember($assignees, $user, $groups);
110*ef3ab392SAndreas Gohr    }
111*ef3ab392SAndreas Gohr
112*ef3ab392SAndreas Gohr    /**
113*ef3ab392SAndreas Gohr     * Has the givenuser acknowledged the given page?
114*ef3ab392SAndreas Gohr     *
115*ef3ab392SAndreas Gohr     * @param string $page
116*ef3ab392SAndreas Gohr     * @param string $user
117*ef3ab392SAndreas Gohr     * @return bool|int timestamp of acknowledgement or fals
118*ef3ab392SAndreas Gohr     */
119*ef3ab392SAndreas Gohr    public function hasUserAcknowledged($page, $user)
120*ef3ab392SAndreas Gohr    {
121*ef3ab392SAndreas Gohr        $sqlite = $this->getDB();
122*ef3ab392SAndreas Gohr        if (!$sqlite) return false;
123*ef3ab392SAndreas Gohr
124*ef3ab392SAndreas Gohr        $sql = "SELECT ack
125*ef3ab392SAndreas Gohr                  FROM acks A, pages B
126*ef3ab392SAndreas Gohr                 WHERE A.page = B.page
127*ef3ab392SAndreas Gohr                   AND page = ?
128*ef3ab392SAndreas Gohr                   AND user = ?
129*ef3ab392SAndreas Gohr                   AND A.ack >= B.lastmod";
130*ef3ab392SAndreas Gohr
131*ef3ab392SAndreas Gohr        $result = $sqlite->query($sql, $page, $user);
132*ef3ab392SAndreas Gohr        $acktime = $sqlite->res2single($result);
133*ef3ab392SAndreas Gohr        $sqlite->res_close($result);
134*ef3ab392SAndreas Gohr
135*ef3ab392SAndreas Gohr        return $acktime ? (int)$acktime : false;
136*ef3ab392SAndreas Gohr    }
1374d6d17d0SAndreas Gohr}
1384d6d17d0SAndreas Gohr
139