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