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