xref: /plugin/acknowledge/admin/report.php (revision 27e79df01fc6dd7340fe3876c504eb6600f00ff1)
1f09444ffSAndreas Gohr<?php
2f09444ffSAndreas Gohr
33b76424dSanndause dokuwiki\Extension\AdminPlugin;
43b76424dSanndause dokuwiki\Form\Form;
5f09444ffSAndreas Gohruse dokuwiki\Extension\AuthPlugin;
6f09444ffSAndreas Gohr
7f09444ffSAndreas Gohr/**
8f09444ffSAndreas Gohr * DokuWiki Plugin acknowledge (Admin Component)
9f09444ffSAndreas Gohr *
10f09444ffSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11f09444ffSAndreas Gohr * @author  Andreas Gohr, Anna Dabrowska <dokuwiki@cosmocode.de>
12f09444ffSAndreas Gohr */
133b76424dSanndaclass admin_plugin_acknowledge_report extends AdminPlugin
14f09444ffSAndreas Gohr{
15f09444ffSAndreas Gohr    /** @inheritdoc */
16f09444ffSAndreas Gohr    public function forAdminOnly()
17f09444ffSAndreas Gohr    {
18f09444ffSAndreas Gohr        return false;
19f09444ffSAndreas Gohr    }
20f09444ffSAndreas Gohr
21f09444ffSAndreas Gohr    /** @inheritdoc */
22f09444ffSAndreas Gohr    public function handle()
23f09444ffSAndreas Gohr    {
24f09444ffSAndreas Gohr    }
25f09444ffSAndreas Gohr
26f09444ffSAndreas Gohr    /** @inheritdoc */
27f09444ffSAndreas Gohr    public function html()
28f09444ffSAndreas Gohr    {
29f09444ffSAndreas Gohr        global $INPUT;
30f09444ffSAndreas Gohr
31f09444ffSAndreas Gohr        echo '<div class="plugin_acknowledgement_admin">';
32*27e79df0SAnna Dabrowska        echo $this->locale_xhtml('report');
33f09444ffSAndreas Gohr        $this->htmlForms();
345966046cSAnna Dabrowska        $user = $INPUT->str('user');
355966046cSAnna Dabrowska        $pg = $INPUT->str('pg');
36dee5618cSAnna Dabrowska        if ($pg) {
375966046cSAnna Dabrowska            $this->htmlPageStatus($pg, $user);
38dee5618cSAnna Dabrowska        } elseif ($user) {
39dee5618cSAnna Dabrowska            $this->htmlUserStatus($user);
40f09444ffSAndreas Gohr        } else {
41f09444ffSAndreas Gohr            $this->htmlLatest();
42f09444ffSAndreas Gohr        }
43f09444ffSAndreas Gohr        echo '</div>';
44f09444ffSAndreas Gohr    }
45f09444ffSAndreas Gohr
46f09444ffSAndreas Gohr    /**
47f09444ffSAndreas Gohr     * Show which users have or need ot acknowledge a specific page
48f09444ffSAndreas Gohr     *
4903962179SAndreas Gohr     * @param string $pattern A page assignment pattern
505966046cSAnna Dabrowska     * @param string $user Optional user
51f09444ffSAndreas Gohr     */
525966046cSAnna Dabrowska    protected function htmlPageStatus($pattern, $user = '')
53f09444ffSAndreas Gohr    {
54f09444ffSAndreas Gohr        global $lang;
55dee5618cSAnna Dabrowska        global $INPUT;
56f09444ffSAndreas Gohr
57f09444ffSAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
58f09444ffSAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
59f09444ffSAndreas Gohr
60dee5618cSAnna Dabrowska        $status = $INPUT->str('status');
6103962179SAndreas Gohr        $pages = $helper->getPagesMatchingPattern($pattern);
6203962179SAndreas Gohr        $acknowledgements = [];
6303962179SAndreas Gohr
6403962179SAndreas Gohr        foreach ($pages as $pattern) {
65dee5618cSAnna Dabrowska            $acknowledgements = array_merge($acknowledgements, $helper->getPageAcknowledgements($pattern, $user, $status, 1000));
66b6817aacSAndreas Gohr            if (count($acknowledgements) > 1000) {
67b6817aacSAndreas Gohr                // don't show too many
68b6817aacSAndreas Gohr                msg($this->getLang('toomanyresults'), 0);
69b6817aacSAndreas Gohr                break;
70b6817aacSAndreas Gohr            }
7103962179SAndreas Gohr        }
7203962179SAndreas Gohr
73f09444ffSAndreas Gohr        if (!$acknowledgements) {
74f09444ffSAndreas Gohr            echo '<p>' . $lang['nothingfound'] . '</p>';
75f09444ffSAndreas Gohr            return;
76f09444ffSAndreas Gohr        }
77f09444ffSAndreas Gohr
7803962179SAndreas Gohr        $this->htmlTable($acknowledgements);
79f09444ffSAndreas Gohr    }
80f09444ffSAndreas Gohr
81f09444ffSAndreas Gohr    /**
82f09444ffSAndreas Gohr     * Show what a given user should sign and has
83f09444ffSAndreas Gohr     *
84f09444ffSAndreas Gohr     * @param string $user
85f09444ffSAndreas Gohr     */
86f09444ffSAndreas Gohr    protected function htmlUserStatus($user)
87f09444ffSAndreas Gohr    {
88f09444ffSAndreas Gohr        /** @var AuthPlugin $auth */
89f09444ffSAndreas Gohr        global $auth;
90f09444ffSAndreas Gohr        global $lang;
915966046cSAnna Dabrowska        global $INPUT;
92f09444ffSAndreas Gohr
93f09444ffSAndreas Gohr        $user = $auth->cleanUser($user);
94f09444ffSAndreas Gohr        $userinfo = $auth->getUserData($user, true);
95f09444ffSAndreas Gohr        if (!$userinfo) {
96f09444ffSAndreas Gohr            echo '<p>' . $lang['nothingfound'] . '</p>';
97f09444ffSAndreas Gohr            return;
98f09444ffSAndreas Gohr        }
99f09444ffSAndreas Gohr
100f09444ffSAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
101f09444ffSAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
102f09444ffSAndreas Gohr
1035966046cSAnna Dabrowska        $status = $INPUT->str('status');
1045966046cSAnna Dabrowska
1055966046cSAnna Dabrowska        if ($status === 'current') {
1065966046cSAnna Dabrowska            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'current');
1075966046cSAnna Dabrowska        } elseif ($status === 'due') {
1085966046cSAnna Dabrowska            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'due');
1095966046cSAnna Dabrowska        } else {
1105966046cSAnna Dabrowska            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'all');
1115966046cSAnna Dabrowska        }
112f09444ffSAndreas Gohr        $count = $this->htmlTable($assignments);
113f09444ffSAndreas Gohr        echo '<p>' . sprintf($this->getLang('count'), hsc($user), $count, count($assignments)) . '</p>';
114f09444ffSAndreas Gohr    }
115f09444ffSAndreas Gohr
116f09444ffSAndreas Gohr    /**
117f09444ffSAndreas Gohr     * Show the latest 100 acknowledgements
118f09444ffSAndreas Gohr     */
119f09444ffSAndreas Gohr    protected function htmlLatest()
120f09444ffSAndreas Gohr    {
121f09444ffSAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
122f09444ffSAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
123f09444ffSAndreas Gohr        $acks = $helper->getAcknowledgements();
124f09444ffSAndreas Gohr        $this->htmlTable($acks);
125f09444ffSAndreas Gohr        echo '<p>' . $this->getLang('overviewHistory') . '</p>';
126f09444ffSAndreas Gohr    }
127f09444ffSAndreas Gohr
128f09444ffSAndreas Gohr    /**
129f09444ffSAndreas Gohr     * @return void
130f09444ffSAndreas Gohr     */
131f09444ffSAndreas Gohr    protected function htmlForms()
132f09444ffSAndreas Gohr    {
133f09444ffSAndreas Gohr        echo '<nav>';
134f09444ffSAndreas Gohr        echo $this->homeLink();
135f09444ffSAndreas Gohr
1363b76424dSannda        $form = new Form(['method' => 'GET']);
13745240794SAnna Dabrowska        $form->id('acknowledge__user-autocomplete');
138f09444ffSAndreas Gohr        $form->setHiddenField('do', 'admin');
139f09444ffSAndreas Gohr        $form->setHiddenField('page', 'acknowledge_report');
1405966046cSAnna Dabrowska        $form->addTextInput('pg', $this->getLang('pattern'));
14145240794SAnna Dabrowska        $form->addTextInput('user', $this->getLang('overviewUser'))
14245240794SAnna Dabrowska            ->attr('type', 'search');
1435966046cSAnna Dabrowska        $form->addDropdown(
1445966046cSAnna Dabrowska            'status',
1455966046cSAnna Dabrowska            [
1465966046cSAnna Dabrowska                'all' => $this->getLang('all'),
1475966046cSAnna Dabrowska                'current' => $this->getLang('current'),
1485966046cSAnna Dabrowska                'due' => $this->getLang('due'),
1495966046cSAnna Dabrowska            ],
1505966046cSAnna Dabrowska            $this->getLang('status')
1515966046cSAnna Dabrowska        );
152f09444ffSAndreas Gohr        $form->addButton('', '>');
153f09444ffSAndreas Gohr        echo $form->toHTML();
154f09444ffSAndreas Gohr        echo '</nav>';
155f09444ffSAndreas Gohr    }
156f09444ffSAndreas Gohr
157f09444ffSAndreas Gohr    /**
158f09444ffSAndreas Gohr     * Print the given acknowledge data
159f09444ffSAndreas Gohr     *
160f09444ffSAndreas Gohr     * @param array $data
161f09444ffSAndreas Gohr     * @return int number of acknowledged entries
162f09444ffSAndreas Gohr     */
163f09444ffSAndreas Gohr    protected function htmlTable($data)
164f09444ffSAndreas Gohr    {
165f09444ffSAndreas Gohr        echo '<table>';
166f09444ffSAndreas Gohr        echo '<tr>';
167dee5618cSAnna Dabrowska        echo '<th>#</th>';
168f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewPage') . '</th>';
169f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewUser') . '</th>';
170f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewMod') . '</th>';
171f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewTime') . '</th>';
172f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewCurrent') . '</th>';
173f09444ffSAndreas Gohr        echo '</tr>';
174f09444ffSAndreas Gohr
175f09444ffSAndreas Gohr        $count = 0;
176dee5618cSAnna Dabrowska        $i = 0;
177f09444ffSAndreas Gohr        foreach ($data as $item) {
178f09444ffSAndreas Gohr            $current = $item['ack'] >= $item['lastmod'];
179f09444ffSAndreas Gohr            if ($current) $count++;
180dee5618cSAnna Dabrowska            $i++;
181f09444ffSAndreas Gohr            echo '<tr>';
182dee5618cSAnna Dabrowska            echo "<td>$i</td>";
183f09444ffSAndreas Gohr            echo '<td>' . $this->pageLink($item['page']) . '</td>';
184f09444ffSAndreas Gohr            echo '<td>' . $this->userLink($item['user']) . '</td>';
1853b76424dSannda            echo '<td>' . html_wikilink(
1863b76424dSannda                ':' . $item['page'],
1873b76424dSannda                ($item['lastmod'] ? dformat($item['lastmod']) : '?')
1883b76424dSannda            ) . '</td>';
189f09444ffSAndreas Gohr            echo '<td>' . ($item['ack'] ? dformat($item['ack']) : '') . '</td>';
190f09444ffSAndreas Gohr            echo '<td>' . ($current ? $this->getLang('yes') : '') . '</td>';
191f09444ffSAndreas Gohr            echo '</tr>';
192f09444ffSAndreas Gohr        }
193f09444ffSAndreas Gohr        echo '</table>';
194f09444ffSAndreas Gohr
195f09444ffSAndreas Gohr        return $count;
196f09444ffSAndreas Gohr    }
197f09444ffSAndreas Gohr
198f09444ffSAndreas Gohr    protected function homeLink()
199f09444ffSAndreas Gohr    {
200f09444ffSAndreas Gohr        global $ID;
201f09444ffSAndreas Gohr
202f09444ffSAndreas Gohr        $url = wl(
203f09444ffSAndreas Gohr            $ID,
204f09444ffSAndreas Gohr            [
205f09444ffSAndreas Gohr                'do' => 'admin',
206f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
207f09444ffSAndreas Gohr            ]
208f09444ffSAndreas Gohr        );
209f09444ffSAndreas Gohr
210f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . $this->getLang('home') . '</a>';
211f09444ffSAndreas Gohr    }
212f09444ffSAndreas Gohr
213f09444ffSAndreas Gohr    /**
214f09444ffSAndreas Gohr     * Link to the user overview
215f09444ffSAndreas Gohr     *
216f09444ffSAndreas Gohr     * @param string $user
217f09444ffSAndreas Gohr     * @return string
218f09444ffSAndreas Gohr     */
219f09444ffSAndreas Gohr    protected function userLink($user)
220f09444ffSAndreas Gohr    {
221f09444ffSAndreas Gohr        global $ID;
222f09444ffSAndreas Gohr
223f09444ffSAndreas Gohr        $url = wl(
224f09444ffSAndreas Gohr            $ID,
225f09444ffSAndreas Gohr            [
226f09444ffSAndreas Gohr                'do' => 'admin',
227f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
228f09444ffSAndreas Gohr                'user' => $user,
229f09444ffSAndreas Gohr            ]
230f09444ffSAndreas Gohr        );
231f09444ffSAndreas Gohr
232f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . hsc($user) . '</a>';
233f09444ffSAndreas Gohr    }
234f09444ffSAndreas Gohr
235f09444ffSAndreas Gohr    /**
236f09444ffSAndreas Gohr     * Link to the page overview
237f09444ffSAndreas Gohr     *
238f09444ffSAndreas Gohr     * @param string $page
239f09444ffSAndreas Gohr     * @return string
240f09444ffSAndreas Gohr     */
241f09444ffSAndreas Gohr    protected function pageLink($page)
242f09444ffSAndreas Gohr    {
243f09444ffSAndreas Gohr        global $ID;
244f09444ffSAndreas Gohr
245f09444ffSAndreas Gohr        $url = wl(
246f09444ffSAndreas Gohr            $ID,
247f09444ffSAndreas Gohr            [
248f09444ffSAndreas Gohr                'do' => 'admin',
249f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
250f09444ffSAndreas Gohr                'pg' => $page,
251f09444ffSAndreas Gohr            ]
252f09444ffSAndreas Gohr        );
253f09444ffSAndreas Gohr
254f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . hsc($page) . '</a>';
255f09444ffSAndreas Gohr    }
256f09444ffSAndreas Gohr
257f09444ffSAndreas Gohr    /** @inheritdoc */
258f09444ffSAndreas Gohr    public function getTOC()
259f09444ffSAndreas Gohr    {
260f09444ffSAndreas Gohr        global $ID;
261f09444ffSAndreas Gohr        return [
262f09444ffSAndreas Gohr            html_mktocitem(
263f09444ffSAndreas Gohr                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_report']),
2643b76424dSannda                $this->getLang('menu'),
2653b76424dSannda                0,
2663b76424dSannda                ''
267f09444ffSAndreas Gohr            ),
268f09444ffSAndreas Gohr            html_mktocitem(
269f09444ffSAndreas Gohr                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_assign']),
2703b76424dSannda                $this->getLang('menu_assign'),
2713b76424dSannda                0,
2723b76424dSannda                ''
273f09444ffSAndreas Gohr            ),
274f09444ffSAndreas Gohr        ];
275f09444ffSAndreas Gohr    }
276f09444ffSAndreas Gohr}
277