xref: /plugin/qc/Output.php (revision 60fceb20f92b9e9bcdb91f0bd8600b534493ef73)
1<?php
2
3namespace dokuwiki\plugin\qc;
4
5/**
6 * Class Output
7 *
8 * Create the HTML formatted output of the scoring analysis
9 *
10 * @package dokuwiki\plugin\qc
11 */
12class Output
13{
14
15    const MAXERR = 10; //what score to use as total failure
16
17    /** @var array the scoring data */
18    protected $data;
19
20    /** @var  \helper_plugin_qc */
21    protected $helper;
22
23    /**
24     * Output constructor.
25     * @param string $page the page to analyze
26     */
27    public function __construct($page)
28    {
29        $this->helper = plugin_load('helper', 'qc');
30        $this->data = $this->helper->getQCData($page);
31    }
32
33    /**
34     * Get the score as icon
35     *
36     * @param $score
37     * @return string
38     */
39    public static function scoreIcon($score)
40    {
41        $html = '';
42
43        // rate the score
44        if ($score > self::MAXERR) {
45            $rating = 'bad';
46        } elseif ($score) {
47            $rating = 'meh';
48        } else {
49            $rating = 'good';
50        }
51
52        // output icon and score
53        $html .= '<span class="qc_icon qc_' . $rating . '">';
54        $html .= inlineSVG(__DIR__ . '/svg/' . $rating . '.svg');
55        if ($score) $html .= '<span>' . $score . '</span>';
56        $html .= '</span>';
57
58        return $html;
59    }
60
61    /**
62     * Print the short summary
63     *
64     * @return string
65     */
66    public function short()
67    {
68        return self::scoreIcon($this->data['score']);
69    }
70
71    /**
72     * Print full analysis
73     *
74     * @return string
75     */
76    public function long()
77    {
78        $html = '';
79
80        $html .= '<h1>' . $this->helper->getLang('intro_h') . '</h1>';
81
82        $html .= '<div>';
83        $html .= '<dl>';
84        $html .= '<dt>' . $this->helper->getLang('g_created') . '</dt>';
85        $html .= '<dd>' . dformat($this->data['created']) . '</dd>';
86
87        $html .= '<dt>' . $this->helper->getLang('g_modified') . '</dt>';
88        $html .= '<dd>' . dformat($this->data['modified']) . '</dd>';
89
90        // print top 5 authors
91        if (!is_array($this->data['authors'])) $this->data['authors'] = array();
92        arsort($this->data['authors']);
93        $top5 = array_slice($this->data['authors'], 0, 5);
94        $cnt = count($top5);
95        $i = 1;
96        $html .= '<dt>' . $this->helper->getLang('g_authors') . '</dt>';
97        $html .= '<dd>';
98        foreach ($top5 as $a => $e) {
99            if ($a == '*') {
100                $html .= $this->helper->getLang('anonymous');
101            } else {
102                $html .= editorinfo($a);
103            }
104            $html .= ' (' . $e . ')';
105            if ($i++ < $cnt) $html .= ', ';
106        }
107        $html .= '</dd>';
108
109        $html .= '<dt>' . $this->helper->getLang('g_changes') . '</dt>';
110        $html .= '<dd>' . $this->data['changes'] . '</dd>';
111
112        $html .= '<dt>' . $this->helper->getLang('g_chars') . '</dt>';
113        $html .= '<dd>' . $this->data['chars'] . '</dd>';
114
115        $html .= '<dt>' . $this->helper->getLang('g_words') . '</dt>';
116        $html .= '<dd>' . $this->data['words'] . '</dd>';
117
118        $html .= '</dl>';
119        $html .= '</div>';
120
121        // output all the problems
122        if ($this->data['score']) {
123            $html .= '<h2>' . $this->helper->getLang('errorsfound_h') . '</h2>';
124            $html .= '<p>' . $this->helper->getLang('errorsfound') . '</p>';
125            $html .= '<div>';
126            arsort($this->data['err']); #sort by score
127            foreach ($this->data['err'] as $err => $val) {
128                if ($val) {
129                    $html .= '<h3>';
130                    $html .= sprintf($this->helper->getLang($err . '_h'), $val);
131                    $html .= '<span class="qc_icon qc_bad">';
132                    $html .= inlineSVG(__DIR__ . '/svg/bad.svg');
133                    $html .= '<span>' . $val . '</span>';
134                    $html .= '</span>';
135                    $html .= '</h3>';
136                    $html .= '<p>' . sprintf($this->helper->getLang($err), $val) . '</p>';
137                }
138            }
139            $html .= '</div>';
140        }
141
142        return $html;
143    }
144}
145