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