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