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