xref: /plugin/acknowledge/admin/stats.php (revision 21913a6d9b851a00667f66bc614ba0dda97d3508)
1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4
5/**
6 * DokuWiki Plugin acknowledge (Admin Component)
7 *
8 * Acknowledgement statistics
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Anna Dabrowska <dokuwiki@cosmocode.de>
12 */
13class admin_plugin_acknowledge_stats extends AdminPlugin
14{
15    /** @inheritdoc */
16    public function forAdminOnly()
17    {
18        return false;
19    }
20
21    /** @inheritDoc */
22    public function getMenuText($language)
23    {
24        return $this->getLang('menu_stats');
25    }
26
27    /** @inheritdoc */
28    public function handle()
29    {
30    }
31
32    /** @inheritdoc */
33    public function html()
34    {
35        global $INPUT;
36
37        /** @var helper_plugin_acknowledge $helper */
38        $helper = plugin_load('helper', 'acknowledge');
39
40        $ns = trim($INPUT->str('ns'), ':');
41
42        echo '<div class="plugin_acknowledgement_admin">';
43        echo '<h1>' . $this->getLang('menu_stats') . '</h1>';
44
45        $stats = $helper->getStatistics($ns);
46
47        // whole-wiki summary
48        $this->htmlSummary($stats['total']);
49
50        // back-link to root
51        if ($ns !== '') {
52            echo '<p class="stats-back">' . $this->nsLink('', $this->getLang('statsTotalLink')) . '</p>';
53            echo '<h2>' . $this->getLang('statsNamespace') . ' ' . $ns . '</h2>';
54        }
55
56        if (empty($stats['namespaces'])) {
57            echo '<p>' . $this->getLang('nothingfound') . '</p>';
58            echo '</div>';
59            return;
60        }
61
62        echo '<table>';
63        echo '<tr>';
64        echo '<th>' . $this->getLang('statsSubnamespace') . '</th>';
65        echo '<th>' . $this->getLang('statsAcked') . '</th>';
66        echo '<th>' . $this->getLang('statsRequired') . '</th>';
67        echo '<th>' . $this->getLang('statsPages') . '</th>';
68        echo '<th>' . $this->getLang('statsRatio') . '</th>';
69        echo '</tr>';
70
71        foreach ($stats['namespaces'] as $key => $data) {
72            $this->htmlRow($this->nsLabel($key, $ns, $data['haschildren']), $data);
73        }
74
75        echo '</table>';
76        echo '</div>';
77    }
78
79    /**
80     * Whole-wiki summary
81     *
82     * @param array $total {required:int, acked:int, pages:int}
83     * @return void
84     */
85    protected function htmlSummary(array $total): void
86    {
87        $ratio = $total['required'] ? round($total['acked'] * 100 / $total['required']) : 0;
88
89        echo '<div class="stats-summary">';
90        echo '<h2>' .  $this->getLang('statsTotal') . '</h2>';
91        echo '<ul>';
92        echo '<li><strong>' . $this->getLang('statsAcked') . ':</strong> ' . $total['acked'] . '</li>';
93        echo '<li><strong>' . $this->getLang('statsRequired') . ':</strong> ' . $total['required'] . '</li>';
94        echo '<li><strong>' . $this->getLang('statsPages') . ':</strong> ' . $total['pages'] . '</li>';
95        echo '<li><strong>' . $this->getLang('statsRatio') . ':</strong> ' . $ratio . '%' . '</li>';
96        echo '</ul>';
97        echo '</div>';
98    }
99
100    /**
101     * Namespace label, linked if it has children
102     *
103     * @param string $key Sub-namespace key (relative to the current namespace)
104     * @param string $ns Currently explored namespace
105     * @param bool $haschildren
106     * @return string HTML
107     */
108    protected function nsLabel(string $key, string $ns, bool $haschildren): string
109    {
110        if ($key === '') {
111            return $this->getLang('statsRoot');
112        }
113        if ($key === $ns) {
114            return $this->getLang('statsHere');
115        }
116
117        // show the part relative to the current namespace
118        $relative = $ns === '' ? $key : substr($key, strlen($ns) + 1);
119        $label = $relative . ':';
120
121        return $haschildren ? $this->nsLink($key, $label) : $label;
122    }
123
124    /**
125     * Link to a namespace drill-down view
126     *
127     * @param string $ns
128     * @param string $label
129     * @return string
130     */
131    protected function nsLink(string $ns, string $label): string
132    {
133        global $ID;
134
135        $params = ['do' => 'admin', 'page' => 'acknowledge_stats'];
136        if ($ns !== '') $params['ns'] = $ns;
137
138        return '<a href="' . wl($ID, $params) . '">' . $label . '</a>';
139    }
140
141    /**
142     * Render a single statistics row
143     *
144     * @param string $label
145     * @param array $data {required:int, acked:int, pages:int}
146     * @return void
147     */
148    protected function htmlRow(string $label, array $data): void
149    {
150        $ratio = $data['required'] ? round($data['acked'] * 100 / $data['required']) : 0;
151
152        echo '<tr>';
153        echo '<td>' . $label . '</td>';
154        echo '<td class="stats-num">' . $data['acked'] . '</td>';
155        echo '<td class="stats-num">' . $data['required'] . '</td>';
156        echo '<td class="stats-num">' . $data['pages'] . '</td>';
157        echo '<td class="stats-num">' . $ratio . '%</td>';
158        echo '</tr>';
159    }
160
161    /** @inheritDoc */
162    public function getTOC()
163    {
164        return (new admin_plugin_acknowledge_report())->getTOC();
165    }
166}
167