xref: /plugin/qc/Output.php (revision 2fc45e0c1b2076ea358377649512960c5d3d694a)
11c845774SAndreas Gohr<?php
21c845774SAndreas Gohr
31c845774SAndreas Gohrnamespace dokuwiki\plugin\qc;
41c845774SAndreas Gohr
51c845774SAndreas Gohr/**
61c845774SAndreas Gohr * Class Output
71c845774SAndreas Gohr *
81c845774SAndreas Gohr * Create the HTML formatted output of the scoring analysis
91c845774SAndreas Gohr *
101c845774SAndreas Gohr * @package dokuwiki\plugin\qc
111c845774SAndreas Gohr */
1276bbc49cSAnna Dabrowskaclass Output
1376bbc49cSAnna Dabrowska{
14*2fc45e0cSsplitbrain    public const MAXERR = 10; //what score to use as total failure
151c845774SAndreas Gohr
161c845774SAndreas Gohr    /** @var array the scoring data */
171c845774SAndreas Gohr    protected $data;
181c845774SAndreas Gohr
191c845774SAndreas Gohr    /** @var  \helper_plugin_qc */
201c845774SAndreas Gohr    protected $helper;
211c845774SAndreas Gohr
221c845774SAndreas Gohr    /**
231c845774SAndreas Gohr     * Output constructor.
241c845774SAndreas Gohr     * @param string $page the page to analyze
251c845774SAndreas Gohr     */
2676bbc49cSAnna Dabrowska    public function __construct($page)
2776bbc49cSAnna Dabrowska    {
281c845774SAndreas Gohr        $this->helper = plugin_load('helper', 'qc');
291c845774SAndreas Gohr        $this->data = $this->helper->getQCData($page);
301c845774SAndreas Gohr    }
311c845774SAndreas Gohr
321c845774SAndreas Gohr    /**
331c845774SAndreas Gohr     * Get the score as icon
341c845774SAndreas Gohr     *
351c845774SAndreas Gohr     * @param $score
361c845774SAndreas Gohr     * @return string
371c845774SAndreas Gohr     */
3876bbc49cSAnna Dabrowska    public static function scoreIcon($score)
3976bbc49cSAnna Dabrowska    {
401c845774SAndreas Gohr        $html = '';
411c845774SAndreas Gohr
421c845774SAndreas Gohr        // rate the score
431c845774SAndreas Gohr        if ($score > self::MAXERR) {
441c845774SAndreas Gohr            $rating = 'bad';
451c845774SAndreas Gohr        } elseif ($score) {
461c845774SAndreas Gohr            $rating = 'meh';
471c845774SAndreas Gohr        } else {
481c845774SAndreas Gohr            $rating = 'good';
491c845774SAndreas Gohr        }
501c845774SAndreas Gohr
511c845774SAndreas Gohr        // output icon and score
521c845774SAndreas Gohr        $html .= '<span class="qc_icon qc_' . $rating . '">';
531c845774SAndreas Gohr        $html .= inlineSVG(__DIR__ . '/svg/' . $rating . '.svg');
54578c8d9aSAndreas Gohr        if ($score) $html .= '<span>' . $score . '</span>';
551c845774SAndreas Gohr        $html .= '</span>';
561c845774SAndreas Gohr
571c845774SAndreas Gohr        return $html;
581c845774SAndreas Gohr    }
591c845774SAndreas Gohr
601c845774SAndreas Gohr    /**
611c845774SAndreas Gohr     * Print the short summary
621c845774SAndreas Gohr     *
631c845774SAndreas Gohr     * @return string
641c845774SAndreas Gohr     */
6576bbc49cSAnna Dabrowska    public function short()
6676bbc49cSAnna Dabrowska    {
67d9e33559SAndreas Gohr        return self::scoreIcon($this->data['score']);
681c845774SAndreas Gohr    }
691c845774SAndreas Gohr
701c845774SAndreas Gohr    /**
711c845774SAndreas Gohr     * Print full analysis
721c845774SAndreas Gohr     *
731c845774SAndreas Gohr     * @return string
741c845774SAndreas Gohr     */
7576bbc49cSAnna Dabrowska    public function long()
7676bbc49cSAnna Dabrowska    {
771c845774SAndreas Gohr        $html = '';
781c845774SAndreas Gohr
791c845774SAndreas Gohr        $html .= '<h1>' . $this->helper->getLang('intro_h') . '</h1>';
801c845774SAndreas Gohr
811c845774SAndreas Gohr        $html .= '<div>';
821c845774SAndreas Gohr        $html .= '<dl>';
831c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_created') . '</dt>';
841c845774SAndreas Gohr        $html .= '<dd>' . dformat($this->data['created']) . '</dd>';
851c845774SAndreas Gohr
861c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_modified') . '</dt>';
871c845774SAndreas Gohr        $html .= '<dd>' . dformat($this->data['modified']) . '</dd>';
881c845774SAndreas Gohr
891c845774SAndreas Gohr        // print top 5 authors
90*2fc45e0cSsplitbrain        if (!is_array($this->data['authors'])) $this->data['authors'] = [];
911c845774SAndreas Gohr        arsort($this->data['authors']);
921c845774SAndreas Gohr        $top5 = array_slice($this->data['authors'], 0, 5);
931c845774SAndreas Gohr        $cnt = count($top5);
941c845774SAndreas Gohr        $i = 1;
951c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_authors') . '</dt>';
961c845774SAndreas Gohr        $html .= '<dd>';
971c845774SAndreas Gohr        foreach ($top5 as $a => $e) {
981c845774SAndreas Gohr            if ($a == '*') {
991c845774SAndreas Gohr                $html .= $this->helper->getLang('anonymous');
1001c845774SAndreas Gohr            } else {
1011c845774SAndreas Gohr                $html .= editorinfo($a);
1021c845774SAndreas Gohr            }
1031c845774SAndreas Gohr            $html .= ' (' . $e . ')';
1041c845774SAndreas Gohr            if ($i++ < $cnt) $html .= ', ';
1051c845774SAndreas Gohr        }
1061c845774SAndreas Gohr        $html .= '</dd>';
1071c845774SAndreas Gohr
1081c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_changes') . '</dt>';
1091c845774SAndreas Gohr        $html .= '<dd>' . $this->data['changes'] . '</dd>';
1101c845774SAndreas Gohr
1111c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_chars') . '</dt>';
1121c845774SAndreas Gohr        $html .= '<dd>' . $this->data['chars'] . '</dd>';
1131c845774SAndreas Gohr
1141c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_words') . '</dt>';
1151c845774SAndreas Gohr        $html .= '<dd>' . $this->data['words'] . '</dd>';
1161c845774SAndreas Gohr
1171c845774SAndreas Gohr        $html .= '</dl>';
1181c845774SAndreas Gohr        $html .= '</div>';
1191c845774SAndreas Gohr
1201c845774SAndreas Gohr        // output all the problems
1211c845774SAndreas Gohr        if ($this->data['score']) {
1221c845774SAndreas Gohr            $html .= '<h2>' . $this->helper->getLang('errorsfound_h') . '</h2>';
1231c845774SAndreas Gohr            $html .= '<p>' . $this->helper->getLang('errorsfound') . '</p>';
1241c845774SAndreas Gohr            $html .= '<div>';
1251c845774SAndreas Gohr            arsort($this->data['err']); #sort by score
1261c845774SAndreas Gohr            foreach ($this->data['err'] as $err => $val) {
1271c845774SAndreas Gohr                if ($val) {
1281c845774SAndreas Gohr                    $html .= '<h3>';
1291c845774SAndreas Gohr                    $html .= sprintf($this->helper->getLang($err . '_h'), $val);
1301c845774SAndreas Gohr                    $html .= '<span class="qc_icon qc_bad">';
1311c845774SAndreas Gohr                    $html .= inlineSVG(__DIR__ . '/svg/bad.svg');
1321c845774SAndreas Gohr                    $html .= '<span>' . $val . '</span>';
1331c845774SAndreas Gohr                    $html .= '</span>';
1341c845774SAndreas Gohr                    $html .= '</h3>';
1351c845774SAndreas Gohr                    $html .= '<p>' . sprintf($this->helper->getLang($err), $val) . '</p>';
1361c845774SAndreas Gohr                }
1371c845774SAndreas Gohr            }
1381c845774SAndreas Gohr            $html .= '</div>';
1391c845774SAndreas Gohr        }
1401c845774SAndreas Gohr
1411c845774SAndreas Gohr        return $html;
1421c845774SAndreas Gohr    }
1431c845774SAndreas Gohr}
144