xref: /plugin/acknowledge/admin/report.php (revision 5966046c9f2860981ac6f31f94d13d9b1dc9b700) !
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 '<h1>' . $this->getLang('menu') . '</h1>';
33        $this->htmlForms();
34        $user = $INPUT->str('user');
35        $pg = $INPUT->str('pg');
36        if ($user) {
37            $this->htmlUserStatus($user);
38        } elseif ($pg) {
39            $this->htmlPageStatus($pg, $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
56        /** @var helper_plugin_acknowledge $helper */
57        $helper = plugin_load('helper', 'acknowledge');
58
59        $pages = $helper->getPagesMatchingPattern($pattern);
60        $acknowledgements = [];
61
62        foreach ($pages as $pattern) {
63            $acknowledgements = array_merge($acknowledgements, $helper->getPageAcknowledgements($pattern, 1000, $user));
64            if (count($acknowledgements) > 1000) {
65                // don't show too many
66                msg($this->getLang('toomanyresults'), 0);
67                break;
68            }
69        }
70
71        if (!$acknowledgements) {
72            echo '<p>' . $lang['nothingfound'] . '</p>';
73            return;
74        }
75
76        $this->htmlTable($acknowledgements);
77    }
78
79    /**
80     * Show what a given user should sign and has
81     *
82     * @param string $user
83     */
84    protected function htmlUserStatus($user)
85    {
86        /** @var AuthPlugin $auth */
87        global $auth;
88        global $lang;
89        global $INPUT;
90
91        $user = $auth->cleanUser($user);
92        $userinfo = $auth->getUserData($user, true);
93        if (!$userinfo) {
94            echo '<p>' . $lang['nothingfound'] . '</p>';
95            return;
96        }
97
98        /** @var helper_plugin_acknowledge $helper */
99        $helper = plugin_load('helper', 'acknowledge');
100
101        $status = $INPUT->str('status');
102
103        if ($status === 'current') {
104            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'current');
105        } elseif ($status === 'due') {
106            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'due');
107        } else {
108            $assignments = $helper->getUserAcknowledgements($user, $userinfo['grps'], 'all');
109        }
110        $count = $this->htmlTable($assignments);
111        echo '<p>' . sprintf($this->getLang('count'), hsc($user), $count, count($assignments)) . '</p>';
112    }
113
114    /**
115     * Show the latest 100 acknowledgements
116     */
117    protected function htmlLatest()
118    {
119        /** @var helper_plugin_acknowledge $helper */
120        $helper = plugin_load('helper', 'acknowledge');
121        $acks = $helper->getAcknowledgements();
122        $this->htmlTable($acks);
123        echo '<p>' . $this->getLang('overviewHistory') . '</p>';
124    }
125
126    /**
127     * @return void
128     */
129    protected function htmlForms()
130    {
131        echo '<nav>';
132        echo $this->homeLink();
133
134        $form = new Form(['method' => 'GET']);
135        $form->id('acknowledge__user-autocomplete');
136        $form->setHiddenField('do', 'admin');
137        $form->setHiddenField('page', 'acknowledge_report');
138        $form->addTextInput('pg', $this->getLang('pattern'));
139        $form->addTextInput('user', $this->getLang('overviewUser'))
140            ->attr('type', 'search');
141        $form->addDropdown(
142            'status',
143            [
144                'all' => $this->getLang('all'),
145                'current' => $this->getLang('current'),
146                'due' => $this->getLang('due'),
147            ],
148            $this->getLang('status')
149        );
150        $form->addButton('', '>');
151        echo $form->toHTML();
152        echo '</nav>';
153    }
154
155    /**
156     * Print the given acknowledge data
157     *
158     * @param array $data
159     * @return int number of acknowledged entries
160     */
161    protected function htmlTable($data)
162    {
163        echo '<table>';
164        echo '<tr>';
165        echo '<th>' . $this->getLang('overviewPage') . '</th>';
166        echo '<th>' . $this->getLang('overviewUser') . '</th>';
167        echo '<th>' . $this->getLang('overviewMod') . '</th>';
168        echo '<th>' . $this->getLang('overviewTime') . '</th>';
169        echo '<th>' . $this->getLang('overviewCurrent') . '</th>';
170        echo '</tr>';
171
172        $count = 0;
173        foreach ($data as $item) {
174            $current = $item['ack'] >= $item['lastmod'];
175            if ($current) $count++;
176
177            echo '<tr>';
178            echo '<td>' . $this->pageLink($item['page']) . '</td>';
179            echo '<td>' . $this->userLink($item['user']) . '</td>';
180            echo '<td>' . html_wikilink(
181                ':' . $item['page'],
182                ($item['lastmod'] ? dformat($item['lastmod']) : '?')
183            ) . '</td>';
184            echo '<td>' . ($item['ack'] ? dformat($item['ack']) : '') . '</td>';
185            echo '<td>' . ($current ? $this->getLang('yes') : '') . '</td>';
186            echo '</tr>';
187        }
188        echo '</table>';
189
190        return $count;
191    }
192
193    protected function homeLink()
194    {
195        global $ID;
196
197        $url = wl(
198            $ID,
199            [
200                'do' => 'admin',
201                'page' => 'acknowledge_report',
202            ]
203        );
204
205        return '<a href="' . $url . '">' . $this->getLang('home') . '</a>';
206    }
207
208    /**
209     * Link to the user overview
210     *
211     * @param string $user
212     * @return string
213     */
214    protected function userLink($user)
215    {
216        global $ID;
217
218        $url = wl(
219            $ID,
220            [
221                'do' => 'admin',
222                'page' => 'acknowledge_report',
223                'user' => $user,
224            ]
225        );
226
227        return '<a href="' . $url . '">' . hsc($user) . '</a>';
228    }
229
230    /**
231     * Link to the page overview
232     *
233     * @param string $page
234     * @return string
235     */
236    protected function pageLink($page)
237    {
238        global $ID;
239
240        $url = wl(
241            $ID,
242            [
243                'do' => 'admin',
244                'page' => 'acknowledge_report',
245                'pg' => $page,
246            ]
247        );
248
249        return '<a href="' . $url . '">' . hsc($page) . '</a>';
250    }
251
252    /** @inheritdoc */
253    public function getTOC()
254    {
255        global $ID;
256        return [
257            html_mktocitem(
258                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_report']),
259                $this->getLang('menu'),
260                0,
261                ''
262            ),
263            html_mktocitem(
264                wl($ID, ['do' => 'admin', 'page' => 'acknowledge_assign']),
265                $this->getLang('menu_assign'),
266                0,
267                ''
268            ),
269        ];
270    }
271}
272