xref: /plugin/acknowledge/admin/report.php (revision 3b76424d6eb7c7bbbc55197a36eab33cd6e9cf20)
1f09444ffSAndreas Gohr<?php
2f09444ffSAndreas Gohr
3*3b76424dSanndause dokuwiki\Extension\AdminPlugin;
4*3b76424dSanndause 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 */
13*3b76424dSanndaclass 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
124*3b76424dSannda        $form = new Form(['method' => 'GET']);
125f09444ffSAndreas Gohr        $form->setHiddenField('do', 'admin');
126f09444ffSAndreas Gohr        $form->setHiddenField('page', 'acknowledge_report');
127f09444ffSAndreas Gohr        $form->addTextInput('user', $this->getLang('overviewUser'));
128f09444ffSAndreas Gohr        $form->addButton('', '>');
129f09444ffSAndreas Gohr        echo $form->toHTML();
130f09444ffSAndreas Gohr
131*3b76424dSannda        $form = new Form(['method' => 'GET']);
132f09444ffSAndreas Gohr        $form->setHiddenField('do', 'admin');
133f09444ffSAndreas Gohr        $form->setHiddenField('page', 'acknowledge_report');
13403962179SAndreas Gohr        $form->addTextInput('pg', $this->getLang('pattern'))->val($ID);
135f09444ffSAndreas Gohr        $form->addButton('', '>');
136f09444ffSAndreas Gohr        echo $form->toHTML();
137f09444ffSAndreas Gohr        echo '</nav>';
138f09444ffSAndreas Gohr    }
139f09444ffSAndreas Gohr
140f09444ffSAndreas Gohr    /**
141f09444ffSAndreas Gohr     * Print the given acknowledge data
142f09444ffSAndreas Gohr     *
143f09444ffSAndreas Gohr     * @param array $data
144f09444ffSAndreas Gohr     * @return int number of acknowledged entries
145f09444ffSAndreas Gohr     */
146f09444ffSAndreas Gohr    protected function htmlTable($data)
147f09444ffSAndreas Gohr    {
148f09444ffSAndreas Gohr        echo '<table>';
149f09444ffSAndreas Gohr        echo '<tr>';
150f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewPage') . '</th>';
151f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewUser') . '</th>';
152f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewMod') . '</th>';
153f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewTime') . '</th>';
154f09444ffSAndreas Gohr        echo '<th>' . $this->getLang('overviewCurrent') . '</th>';
155f09444ffSAndreas Gohr        echo '</tr>';
156f09444ffSAndreas Gohr
157f09444ffSAndreas Gohr        $count = 0;
158f09444ffSAndreas Gohr        foreach ($data as $item) {
159f09444ffSAndreas Gohr            $current = $item['ack'] >= $item['lastmod'];
160f09444ffSAndreas Gohr            if ($current) $count++;
161f09444ffSAndreas Gohr
162f09444ffSAndreas Gohr            echo '<tr>';
163f09444ffSAndreas Gohr            echo '<td>' . $this->pageLink($item['page']) . '</td>';
164f09444ffSAndreas Gohr            echo '<td>' . $this->userLink($item['user']) . '</td>';
165*3b76424dSannda            echo '<td>' . html_wikilink(
166*3b76424dSannda                ':' . $item['page'],
167*3b76424dSannda                ($item['lastmod'] ? dformat($item['lastmod']) : '?')
168*3b76424dSannda            ) . '</td>';
169f09444ffSAndreas Gohr            echo '<td>' . ($item['ack'] ? dformat($item['ack']) : '') . '</td>';
170f09444ffSAndreas Gohr            echo '<td>' . ($current ? $this->getLang('yes') : '') . '</td>';
171f09444ffSAndreas Gohr            echo '</tr>';
172f09444ffSAndreas Gohr        }
173f09444ffSAndreas Gohr        echo '</table>';
174f09444ffSAndreas Gohr
175f09444ffSAndreas Gohr        return $count;
176f09444ffSAndreas Gohr    }
177f09444ffSAndreas Gohr
178f09444ffSAndreas Gohr    protected function homeLink()
179f09444ffSAndreas Gohr    {
180f09444ffSAndreas Gohr        global $ID;
181f09444ffSAndreas Gohr
182f09444ffSAndreas Gohr        $url = wl(
183f09444ffSAndreas Gohr            $ID,
184f09444ffSAndreas Gohr            [
185f09444ffSAndreas Gohr                'do' => 'admin',
186f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
187f09444ffSAndreas Gohr            ]
188f09444ffSAndreas Gohr        );
189f09444ffSAndreas Gohr
190f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . $this->getLang('home') . '</a>';
191f09444ffSAndreas Gohr    }
192f09444ffSAndreas Gohr
193f09444ffSAndreas Gohr    /**
194f09444ffSAndreas Gohr     * Link to the user overview
195f09444ffSAndreas Gohr     *
196f09444ffSAndreas Gohr     * @param string $user
197f09444ffSAndreas Gohr     * @return string
198f09444ffSAndreas Gohr     */
199f09444ffSAndreas Gohr    protected function userLink($user)
200f09444ffSAndreas Gohr    {
201f09444ffSAndreas Gohr        global $ID;
202f09444ffSAndreas Gohr
203f09444ffSAndreas Gohr        $url = wl(
204f09444ffSAndreas Gohr            $ID,
205f09444ffSAndreas Gohr            [
206f09444ffSAndreas Gohr                'do' => 'admin',
207f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
208f09444ffSAndreas Gohr                'user' => $user,
209f09444ffSAndreas Gohr            ]
210f09444ffSAndreas Gohr        );
211f09444ffSAndreas Gohr
212f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . hsc($user) . '</a>';
213f09444ffSAndreas Gohr    }
214f09444ffSAndreas Gohr
215f09444ffSAndreas Gohr    /**
216f09444ffSAndreas Gohr     * Link to the page overview
217f09444ffSAndreas Gohr     *
218f09444ffSAndreas Gohr     * @param string $page
219f09444ffSAndreas Gohr     * @return string
220f09444ffSAndreas Gohr     */
221f09444ffSAndreas Gohr    protected function pageLink($page)
222f09444ffSAndreas Gohr    {
223f09444ffSAndreas Gohr        global $ID;
224f09444ffSAndreas Gohr
225f09444ffSAndreas Gohr        $url = wl(
226f09444ffSAndreas Gohr            $ID,
227f09444ffSAndreas Gohr            [
228f09444ffSAndreas Gohr                'do' => 'admin',
229f09444ffSAndreas Gohr                'page' => 'acknowledge_report',
230f09444ffSAndreas Gohr                'pg' => $page,
231f09444ffSAndreas Gohr            ]
232f09444ffSAndreas Gohr        );
233f09444ffSAndreas Gohr
234f09444ffSAndreas Gohr        return '<a href="' . $url . '">' . hsc($page) . '</a>';
235f09444ffSAndreas Gohr    }
236f09444ffSAndreas Gohr
237f09444ffSAndreas Gohr    /** @inheritdoc */
238f09444ffSAndreas Gohr    public function getTOC()
239f09444ffSAndreas Gohr    {
240f09444ffSAndreas Gohr        global $ID;
241f09444ffSAndreas Gohr        return [
242f09444ffSAndreas Gohr            html_mktocitem(
243f09444ffSAndreas Gohr                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_report']),
244*3b76424dSannda                $this->getLang('menu'),
245*3b76424dSannda                0,
246*3b76424dSannda                ''
247f09444ffSAndreas Gohr            ),
248f09444ffSAndreas Gohr            html_mktocitem(
249f09444ffSAndreas Gohr                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_assign']),
250*3b76424dSannda                $this->getLang('menu_assign'),
251*3b76424dSannda                0,
252*3b76424dSannda                ''
253f09444ffSAndreas Gohr            ),
254f09444ffSAndreas Gohr        ];
255f09444ffSAndreas Gohr    }
256f09444ffSAndreas Gohr}
257