1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\Form\Form;
5use dokuwiki\Extension\AuthPlugin;
6
7/**
8 * DokuWiki Plugin acknowledge (Admin Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Andreas Gohr, Anna Dabrowska <dokuwiki@cosmocode.de>
12 */
13class admin_plugin_acknowledge_report extends AdminPlugin
14{
15    /** @inheritdoc */
16    public function forAdminOnly()
17    {
18        return false;
19    }
20
21    /** @inheritdoc */
22    public function handle()
23    {
24    }
25
26    /** @inheritdoc */
27    public function html()
28    {
29        global $INPUT;
30
31        echo '<div class="plugin_acknowledgement_admin">';
32        echo $this->locale_xhtml('report');
33        $this->htmlForms();
34        $user = $INPUT->str('user');
35        $pg = $INPUT->str('pg');
36        if ($pg) {
37            $this->htmlPageStatus($pg, $user);
38        } elseif ($user) {
39            $this->htmlUserStatus($user);
40        } else {
41            $this->htmlLatest();
42        }
43        echo '</div>';
44    }
45
46    /**
47     * Show which users have or need ot acknowledge a specific page
48     *
49     * @param string $pattern A page assignment pattern
50     * @param string $user Optional user
51     */
52    protected function htmlPageStatus($pattern, $user = '')
53    {
54        global $lang;
55        global $INPUT;
56
57        /** @var helper_plugin_acknowledge $helper */
58        $helper = plugin_load('helper', 'acknowledge');
59
60        $status = $INPUT->str('status');
61        $pages = $helper->getPagesMatchingPattern($pattern);
62        $acknowledgements = [];
63
64        foreach ($pages as $pattern) {
65            $acknowledgements = array_merge(
66                $acknowledgements,
67                $helper->getPageAcknowledgements($pattern, $user, $status, 1000)
68            );
69            if (count($acknowledgements) > 1000) {
70                // don't show too many
71                msg($this->getLang('toomanyresults'), 0);
72                break;
73            }
74        }
75
76        if (!$acknowledgements) {
77            echo '<p>' . $lang['nothingfound'] . '</p>';
78            return;
79        }
80
81        $this->htmlTable($acknowledgements);
82    }
83
84    /**
85     * Show what a given user should sign and has
86     *
87     * @param string $user
88     */
89    protected function htmlUserStatus($user)
90    {
91        /** @var AuthPlugin $auth */
92        global $auth;
93        global $lang;
94        global $INPUT;
95
96        $user = $auth->cleanUser($user);
97        $userinfo = $auth->getUserData($user, true);
98        if (!$userinfo) {
99            echo '<p>' . $lang['nothingfound'] . '</p>';
100            return;
101        }
102
103        /** @var helper_plugin_acknowledge $helper */
104        $helper = plugin_load('helper', 'acknowledge');
105
106        $status = $INPUT->str('status');
107
108        if ($status === 'current') {
109            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'current');
110        } elseif ($status === 'due') {
111            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'due');
112        } else {
113            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'all');
114        }
115        $count = $this->htmlTable($assignments);
116        echo '<p>' . sprintf($this->getLang('count'), hsc($user), $count, count($assignments)) . '</p>';
117    }
118
119    /**
120     * Show the latest 100 acknowledgements
121     */
122    protected function htmlLatest()
123    {
124        /** @var helper_plugin_acknowledge $helper */
125        $helper = plugin_load('helper', 'acknowledge');
126        $acks = $helper->getAcknowledgements();
127        $this->htmlTable($acks);
128        echo '<p>' . $this->getLang('overviewHistory') . '</p>';
129    }
130
131    /**
132     * @return void
133     */
134    protected function htmlForms()
135    {
136        echo '<nav>';
137        echo $this->homeLink();
138
139        $form = new Form(['method' => 'GET']);
140        $form->id('acknowledge__user-autocomplete');
141        $form->setHiddenField('do', 'admin');
142        $form->setHiddenField('page', 'acknowledge_report');
143        $form->addTextInput('pg', $this->getLang('pattern'));
144        $form->addTextInput('user', $this->getLang('overviewUser'))
145            ->attr('type', 'search');
146        $form->addDropdown(
147            'status',
148            [
149                'all' => $this->getLang('all'),
150                'current' => $this->getLang('current'),
151                'due' => $this->getLang('due'),
152            ],
153            $this->getLang('status')
154        );
155        $form->addButton('', '>');
156        echo $form->toHTML();
157        echo '</nav>';
158    }
159
160    /**
161     * Print the given acknowledge data
162     *
163     * @param array $data
164     * @return int number of acknowledged entries
165     */
166    protected function htmlTable($data)
167    {
168        echo '<table>';
169        echo '<tr>';
170        echo '<th>#</th>';
171        echo '<th>' . $this->getLang('overviewPage') . '</th>';
172        echo '<th>' . $this->getLang('overviewUser') . '</th>';
173        echo '<th>' . $this->getLang('overviewMod') . '</th>';
174        echo '<th>' . $this->getLang('overviewTime') . '</th>';
175        echo '<th>' . $this->getLang('overviewCurrent') . '</th>';
176        echo '</tr>';
177
178        $count = 0;
179        $i = 0;
180        foreach ($data as $item) {
181            $current = $item['ack'] >= $item['lastmod'];
182            if ($current) $count++;
183            $i++;
184            echo '<tr>';
185            echo "<td>$i</td>";
186            echo '<td>' . $this->pageLink($item['page']) . '</td>';
187            echo '<td>' . $this->userLink($item['user']) . '</td>';
188            echo '<td>' . html_wikilink(
189                ':' . $item['page'],
190                ($item['lastmod'] ? dformat($item['lastmod']) : '?')
191            ) . '</td>';
192            echo '<td>' . ($item['ack'] ? dformat($item['ack']) : '') . '</td>';
193            echo '<td>' . ($current ? $this->getLang('yes') : '') . '</td>';
194            echo '</tr>';
195        }
196        echo '</table>';
197
198        return $count;
199    }
200
201    protected function homeLink()
202    {
203        global $ID;
204
205        $url = wl(
206            $ID,
207            [
208                'do' => 'admin',
209                'page' => 'acknowledge_report',
210            ]
211        );
212
213        return '<a href="' . $url . '">' . $this->getLang('home') . '</a>';
214    }
215
216    /**
217     * Link to the user overview
218     *
219     * @param string $user
220     * @return string
221     */
222    protected function userLink($user)
223    {
224        global $ID;
225
226        $url = wl(
227            $ID,
228            [
229                'do' => 'admin',
230                'page' => 'acknowledge_report',
231                'user' => $user,
232            ]
233        );
234
235        return '<a href="' . $url . '">' . hsc($user) . '</a>';
236    }
237
238    /**
239     * Link to the page overview
240     *
241     * @param string $page
242     * @return string
243     */
244    protected function pageLink($page)
245    {
246        global $ID;
247
248        $url = wl(
249            $ID,
250            [
251                'do' => 'admin',
252                'page' => 'acknowledge_report',
253                'pg' => $page,
254            ]
255        );
256
257        return '<a href="' . $url . '">' . hsc($page) . '</a>';
258    }
259
260    /** @inheritdoc */
261    public function getTOC()
262    {
263        global $ID;
264        return [
265            html_mktocitem(
266                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_report']),
267                $this->getLang('menu'),
268                0,
269                ''
270            ),
271            html_mktocitem(
272                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_assign']),
273                $this->getLang('menu_assign'),
274                0,
275                ''
276            ),
277        ];
278    }
279}
280