1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\plugin\qc\Output;
5
6/**
7 * This plugin is used to display a summery of all FIXME pages
8 *
9 * @see http://dokuwiki.org/plugin:qc
10 * @author Dominik Eckelmann <dokuwiki@cosmocode.de>
11 */
12class admin_plugin_qc extends AdminPlugin
13{
14    protected $data;
15    protected $order;
16
17    /** @inheritdoc */
18    public function forAdminOnly()
19    {
20        return false;
21    }
22
23    /** @inheritDoc */
24    public function getMenuIcon()
25    {
26        return __DIR__ . '/svg/good.svg';
27    }
28
29    /** @inheritdoc */
30    public function handle()
31    {
32        global $conf;
33
34        // load the quality data
35        if (is_file($conf['tmpdir'] . '/qcgather')) {
36            $this->data = file_get_contents($conf['tmpdir'] . '/qcgather');
37            $this->data = unserialize($this->data);
38        } else {
39            $this->data = [];
40        }
41
42        // order the data
43        if (!isset($_REQUEST['pluginqc']['order'])) {
44            $_REQUEST['pluginqc']['order'] = 'quality';
45        }
46
47        switch ($_REQUEST['pluginqc']['order']) {
48            case 'fixme':
49                uasort($this->data, [$this, 'sortFixme']);
50                $this->order = 'fixme';
51                break;
52            default:
53                uasort($this->data, [$this, 'sortQuality']);
54                $this->order = 'quality';
55        }
56    }
57
58    /** @inheritdoc */
59    public function html()
60    {
61        global $ID;
62        $max = $this->getConf('maxshowen');
63        if (!$max || $max <= 0) $max = 25;
64
65        echo '<div id="plugin__qc_admin">';
66        echo '<h1>' . $this->getLang('admin_headline') . '</h1>';
67
68        echo '<p>' . sprintf($this->getLang('admin_desc'), $max) . '</p>';
69
70        echo '<table class="inline">';
71        echo '  <tr>';
72        echo '    <th>' . $this->getLang('admin_page') . '</th>';
73        echo '    <th class="quality">' . $this->getOrderArrow('quality') .
74            '<a href="' . wl($ID, ['do' => 'admin', 'page' => 'qc', 'pluginqc[order]' => 'quality']) . '">' .
75            $this->getLang('admin_quality') . '</a></th>';
76        echo '    <th class="fixme">' . $this->getOrderArrow('fixme') .
77            '<a href="' . wl($ID, ['do' => 'admin', 'page' => 'qc', 'pluginqc[order]' => 'fixme']) . '">' .
78            $this->getLang('admin_fixme') . '</a></th>';
79        echo '  </tr>';
80
81        if ($this->data) {
82            foreach ($this->data as $id => $data) {
83                if ($max == 0) break;
84                echo '  <tr>';
85                echo '    <td>';
86                tpl_pagelink(':' . $id, $id);
87                echo '</td>';
88                echo '    <td class="centeralign">' . Output::scoreIcon($data['score']) . '</td>';
89                echo '    <td class="centeralign">' . $data['err']['fixme'] . '</td>';
90                echo '  </tr>';
91                $max--;
92            }
93        }
94
95        echo '</table>';
96        echo '</div>';
97    }
98
99    /**
100     * return an arrow if currently sorted by this type
101     *
102     * @ return string
103     */
104    protected function getOrderArrow($type)
105    {
106        if ($type == $this->order) return '&darr; ';
107        return '';
108    }
109
110    /**
111     * order by quality
112     */
113    protected function sortQuality($a, $b)
114    {
115        return $b['score'] <=> $a['score'];
116    }
117
118    /**
119     * order by fixmes
120     */
121    protected function sortFixme($a, $b)
122    {
123        return $b['err']['fixme'] <=> $a['err']['fixme'];
124    }
125}
126