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