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