xref: /plugin/acknowledge/admin/report.php (revision 45240794bdbd02483d35a247625be40921d89e8d)
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">';
32f09444ffSAndreas Gohr        echo '<h1>' . $this->getLang('menu') . '</h1>';
33f09444ffSAndreas Gohr        $this->htmlForms();
34f09444ffSAndreas Gohr        if ($INPUT->has('user')) {
35f09444ffSAndreas Gohr            $this->htmlUserStatus($INPUT->str('user'));
36f09444ffSAndreas Gohr        } elseif ($INPUT->has('pg')) {
37f09444ffSAndreas Gohr            $this->htmlPageStatus($INPUT->str('pg'));
38f09444ffSAndreas Gohr        } else {
39f09444ffSAndreas Gohr            $this->htmlLatest();
40f09444ffSAndreas Gohr        }
41f09444ffSAndreas Gohr        echo '</div>';
42f09444ffSAndreas Gohr    }
43f09444ffSAndreas Gohr
44f09444ffSAndreas Gohr    /**
45f09444ffSAndreas Gohr     * Show which users have or need ot acknowledge a specific page
46f09444ffSAndreas Gohr     *
4703962179SAndreas Gohr     * @param string $pattern A page assignment pattern
48f09444ffSAndreas Gohr     */
4903962179SAndreas Gohr    protected function htmlPageStatus($pattern)
50f09444ffSAndreas Gohr    {
51f09444ffSAndreas Gohr        global $lang;
52f09444ffSAndreas Gohr
53f09444ffSAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
54f09444ffSAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
55f09444ffSAndreas Gohr
5603962179SAndreas Gohr        $pages = $helper->getPagesMatchingPattern($pattern);
5703962179SAndreas Gohr        $acknowledgements = [];
5803962179SAndreas Gohr
5903962179SAndreas Gohr        foreach ($pages as $pattern) {
60b6817aacSAndreas Gohr            $acknowledgements = array_merge($acknowledgements, $helper->getPageAcknowledgements($pattern, 1000));
61b6817aacSAndreas Gohr            if (count($acknowledgements) > 1000) {
62b6817aacSAndreas Gohr                // don't show too many
63b6817aacSAndreas Gohr                msg($this->getLang('toomanyresults'), 0);
64b6817aacSAndreas Gohr                break;
65b6817aacSAndreas Gohr            }
6603962179SAndreas Gohr        }
6703962179SAndreas Gohr
68f09444ffSAndreas Gohr        if (!$acknowledgements) {
69f09444ffSAndreas Gohr            echo '<p>' . $lang['nothingfound'] . '</p>';
70f09444ffSAndreas Gohr            return;
71f09444ffSAndreas Gohr        }
72f09444ffSAndreas Gohr
7303962179SAndreas Gohr        $this->htmlTable($acknowledgements);
74f09444ffSAndreas Gohr    }
75f09444ffSAndreas Gohr
76f09444ffSAndreas Gohr    /**
77f09444ffSAndreas Gohr     * Show what a given user should sign and has
78f09444ffSAndreas Gohr     *
79f09444ffSAndreas Gohr     * @param string $user
80f09444ffSAndreas Gohr     */
81f09444ffSAndreas Gohr    protected function htmlUserStatus($user)
82f09444ffSAndreas Gohr    {
83f09444ffSAndreas Gohr        /** @var AuthPlugin $auth */
84f09444ffSAndreas Gohr        global $auth;
85f09444ffSAndreas Gohr        global $lang;
86f09444ffSAndreas Gohr
87f09444ffSAndreas Gohr        $user = $auth->cleanUser($user);
88f09444ffSAndreas Gohr        $userinfo = $auth->getUserData($user, true);
89f09444ffSAndreas Gohr        if (!$userinfo) {
90f09444ffSAndreas Gohr            echo '<p>' . $lang['nothingfound'] . '</p>';
91f09444ffSAndreas Gohr            return;
92f09444ffSAndreas Gohr        }
93f09444ffSAndreas Gohr
94f09444ffSAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
95f09444ffSAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
96f09444ffSAndreas Gohr
97f09444ffSAndreas Gohr        $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps']);
98f09444ffSAndreas Gohr        $count = $this->htmlTable($assignments);
99f09444ffSAndreas Gohr        echo '<p>' . sprintf($this->getLang('count'), hsc($user), $count, count($assignments)) . '</p>';
100f09444ffSAndreas Gohr    }
101f09444ffSAndreas Gohr
102f09444ffSAndreas Gohr    /**
103f09444ffSAndreas Gohr     * Show the latest 100 acknowledgements
104f09444ffSAndreas Gohr     */
105f09444ffSAndreas Gohr    protected function htmlLatest()
106f09444ffSAndreas Gohr    {
107f09444ffSAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
108f09444ffSAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
109f09444ffSAndreas Gohr        $acks = $helper->getAcknowledgements();
110f09444ffSAndreas Gohr        $this->htmlTable($acks);
111f09444ffSAndreas Gohr        echo '<p>' . $this->getLang('overviewHistory') . '</p>';
112f09444ffSAndreas Gohr    }
113f09444ffSAndreas Gohr
114f09444ffSAndreas Gohr    /**
115f09444ffSAndreas Gohr     * @return void
116f09444ffSAndreas Gohr     */
117f09444ffSAndreas Gohr    protected function htmlForms()
118f09444ffSAndreas Gohr    {
119f09444ffSAndreas Gohr        global $ID;
120f09444ffSAndreas Gohr
121f09444ffSAndreas Gohr        echo '<nav>';
122f09444ffSAndreas Gohr        echo $this->homeLink();
123f09444ffSAndreas Gohr
1243b76424dSannda        $form = new Form(['method' => 'GET']);
125*45240794SAnna Dabrowska        $form->id('acknowledge__user-autocomplete');
126f09444ffSAndreas Gohr        $form->setHiddenField('do', 'admin');
127f09444ffSAndreas Gohr        $form->setHiddenField('page', 'acknowledge_report');
128*45240794SAnna Dabrowska        $form->addTextInput('user', $this->getLang('overviewUser'))
129*45240794SAnna Dabrowska            ->attr('type', 'search');
130f09444ffSAndreas Gohr        $form->addButton('', '>');
131f09444ffSAndreas Gohr        echo $form->toHTML();
132f09444ffSAndreas Gohr
1333b76424dSannda        $form = new Form(['method' => 'GET']);
134f09444ffSAndreas Gohr        $form->setHiddenField('do', 'admin');
135f09444ffSAndreas Gohr        $form->setHiddenField('page', 'acknowledge_report');
13603962179SAndreas Gohr        $form->addTextInput('pg', $this->getLang('pattern'))->val($ID);
137f09444ffSAndreas Gohr        $form->addButton('', '>');
138f09444ffSAndreas Gohr        echo $form->toHTML();
139f09444ffSAndreas Gohr        echo '</nav>';
140f09444ffSAndreas Gohr    }
141f09444ffSAndreas Gohr
142f09444ffSAndreas Gohr    /**
143f09444ffSAndreas Gohr     * Print the given acknowledge data
144f09444ffSAndreas Gohr     *
145f09444ffSAndreas Gohr     * @param array $data
146f09444ffSAndreas Gohr     * @return int number of acknowledged entries
147f09444ffSAndreas Gohr     */
148f09444ffSAndreas Gohr    protected function htmlTable($data)
149f09444ffSAndreas Gohr    {
150f09444ffSAndreas Gohr        echo '<table>';
151f09444ffSAndreas Gohr        echo '<tr>';
152f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewPage') . '</th>';
153f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewUser') . '</th>';
154f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewMod') . '</th>';
155f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewTime') . '</th>';
156f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewCurrent') . '</th>';
157f09444ffSAndreas Gohr        echo '</tr>';
158f09444ffSAndreas Gohr
159f09444ffSAndreas Gohr        $count = 0;
160f09444ffSAndreas Gohr        foreach ($data as $item) {
161f09444ffSAndreas Gohr            $current = $item['ack'] >= $item['lastmod'];
162f09444ffSAndreas Gohr            if ($current) $count++;
163f09444ffSAndreas Gohr
164f09444ffSAndreas Gohr            echo '<tr>';
165f09444ffSAndreas Gohr            echo '<td>' . $this->pageLink($item['page']) . '</td>';
166f09444ffSAndreas Gohr            echo '<td>' . $this->userLink($item['user']) . '</td>';
1673b76424dSannda            echo '<td>' . html_wikilink(
1683b76424dSannda                ':' . $item['page'],
1693b76424dSannda                ($item['lastmod'] ? dformat($item['lastmod']) : '?')
1703b76424dSannda            ) . '</td>';
171f09444ffSAndreas Gohr            echo '<td>' . ($item['ack'] ? dformat($item['ack']) : '') . '</td>';
172f09444ffSAndreas Gohr            echo '<td>' . ($current ? $this->getLang('yes') : '') . '</td>';
173f09444ffSAndreas Gohr            echo '</tr>';
174f09444ffSAndreas Gohr        }
175f09444ffSAndreas Gohr        echo '</table>';
176f09444ffSAndreas Gohr
177f09444ffSAndreas Gohr        return $count;
178f09444ffSAndreas Gohr    }
179f09444ffSAndreas Gohr
180f09444ffSAndreas Gohr    protected function homeLink()
181f09444ffSAndreas Gohr    {
182f09444ffSAndreas Gohr        global $ID;
183f09444ffSAndreas Gohr
184f09444ffSAndreas Gohr        $url = wl(
185f09444ffSAndreas Gohr            $ID,
186f09444ffSAndreas Gohr            [
187f09444ffSAndreas Gohr                'do' => 'admin',
188f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
189f09444ffSAndreas Gohr            ]
190f09444ffSAndreas Gohr        );
191f09444ffSAndreas Gohr
192f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . $this->getLang('home') . '</a>';
193f09444ffSAndreas Gohr    }
194f09444ffSAndreas Gohr
195f09444ffSAndreas Gohr    /**
196f09444ffSAndreas Gohr     * Link to the user overview
197f09444ffSAndreas Gohr     *
198f09444ffSAndreas Gohr     * @param string $user
199f09444ffSAndreas Gohr     * @return string
200f09444ffSAndreas Gohr     */
201f09444ffSAndreas Gohr    protected function userLink($user)
202f09444ffSAndreas Gohr    {
203f09444ffSAndreas Gohr        global $ID;
204f09444ffSAndreas Gohr
205f09444ffSAndreas Gohr        $url = wl(
206f09444ffSAndreas Gohr            $ID,
207f09444ffSAndreas Gohr            [
208f09444ffSAndreas Gohr                'do' => 'admin',
209f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
210f09444ffSAndreas Gohr                'user' => $user,
211f09444ffSAndreas Gohr            ]
212f09444ffSAndreas Gohr        );
213f09444ffSAndreas Gohr
214f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . hsc($user) . '</a>';
215f09444ffSAndreas Gohr    }
216f09444ffSAndreas Gohr
217f09444ffSAndreas Gohr    /**
218f09444ffSAndreas Gohr     * Link to the page overview
219f09444ffSAndreas Gohr     *
220f09444ffSAndreas Gohr     * @param string $page
221f09444ffSAndreas Gohr     * @return string
222f09444ffSAndreas Gohr     */
223f09444ffSAndreas Gohr    protected function pageLink($page)
224f09444ffSAndreas Gohr    {
225f09444ffSAndreas Gohr        global $ID;
226f09444ffSAndreas Gohr
227f09444ffSAndreas Gohr        $url = wl(
228f09444ffSAndreas Gohr            $ID,
229f09444ffSAndreas Gohr            [
230f09444ffSAndreas Gohr                'do' => 'admin',
231f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
232f09444ffSAndreas Gohr                'pg' => $page,
233f09444ffSAndreas Gohr            ]
234f09444ffSAndreas Gohr        );
235f09444ffSAndreas Gohr
236f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . hsc($page) . '</a>';
237f09444ffSAndreas Gohr    }
238f09444ffSAndreas Gohr
239f09444ffSAndreas Gohr    /** @inheritdoc */
240f09444ffSAndreas Gohr    public function getTOC()
241f09444ffSAndreas Gohr    {
242f09444ffSAndreas Gohr        global $ID;
243f09444ffSAndreas Gohr        return [
244f09444ffSAndreas Gohr            html_mktocitem(
245f09444ffSAndreas Gohr                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_report']),
2463b76424dSannda                $this->getLang('menu'),
2473b76424dSannda                0,
2483b76424dSannda                ''
249f09444ffSAndreas Gohr            ),
250f09444ffSAndreas Gohr            html_mktocitem(
251f09444ffSAndreas Gohr                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_assign']),
2523b76424dSannda                $this->getLang('menu_assign'),
2533b76424dSannda                0,
2543b76424dSannda                ''
255f09444ffSAndreas Gohr            ),
256f09444ffSAndreas Gohr        ];
257f09444ffSAndreas Gohr    }
258f09444ffSAndreas Gohr}
259