xref: /plugin/qc/Output.php (revision 578c8d9a8c687263c3e31da6236be68f9072a732)
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 */
121c845774SAndreas Gohrclass Output {
131c845774SAndreas Gohr
141c845774SAndreas Gohr    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     */
261c845774SAndreas Gohr    public function __construct($page) {
271c845774SAndreas Gohr        $this->helper = plugin_load('helper', 'qc');
281c845774SAndreas Gohr        $this->data = $this->helper->getQCData($page);
291c845774SAndreas Gohr    }
301c845774SAndreas Gohr
311c845774SAndreas Gohr    /**
321c845774SAndreas Gohr     * Get the score as icon
331c845774SAndreas Gohr     *
341c845774SAndreas Gohr     * @param $score
351c845774SAndreas Gohr     * @return string
361c845774SAndreas Gohr     */
371c845774SAndreas Gohr    public static function scoreIcon($score) {
381c845774SAndreas Gohr        $html = '';
391c845774SAndreas Gohr
401c845774SAndreas Gohr        // rate the score
411c845774SAndreas Gohr        if($score > self::MAXERR) {
421c845774SAndreas Gohr            $rating = 'bad';
431c845774SAndreas Gohr        } elseif($score) {
441c845774SAndreas Gohr            $rating = 'meh';
451c845774SAndreas Gohr        } else {
461c845774SAndreas Gohr            $rating = 'good';
471c845774SAndreas Gohr        }
481c845774SAndreas Gohr
491c845774SAndreas Gohr        // output icon and score
501c845774SAndreas Gohr        $html .= '<span class="qc_icon qc_' . $rating . '">';
511c845774SAndreas Gohr        $html .= inlineSVG(__DIR__ . '/svg/' . $rating . '.svg');
52*578c8d9aSAndreas Gohr        if($score) $html .= '<span>' . $score . '</span>';
531c845774SAndreas Gohr        $html .= '</span>';
541c845774SAndreas Gohr
551c845774SAndreas Gohr        return $html;
561c845774SAndreas Gohr    }
571c845774SAndreas Gohr
581c845774SAndreas Gohr    /**
591c845774SAndreas Gohr     * Print the short summary
601c845774SAndreas Gohr     *
611c845774SAndreas Gohr     * @return string
621c845774SAndreas Gohr     */
631c845774SAndreas Gohr    public function short() {
64d9e33559SAndreas Gohr        return self::scoreIcon($this->data['score']);
651c845774SAndreas Gohr    }
661c845774SAndreas Gohr
671c845774SAndreas Gohr    /**
681c845774SAndreas Gohr     * Print full analysis
691c845774SAndreas Gohr     *
701c845774SAndreas Gohr     * @return string
711c845774SAndreas Gohr     */
721c845774SAndreas Gohr    public function long() {
731c845774SAndreas Gohr        $html = '';
741c845774SAndreas Gohr
751c845774SAndreas Gohr        $html .= '<h1>' . $this->helper->getLang('intro_h') . '</h1>';
761c845774SAndreas Gohr
771c845774SAndreas Gohr        $html .= '<div>';
781c845774SAndreas Gohr        $html .= '<dl>';
791c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_created') . '</dt>';
801c845774SAndreas Gohr        $html .= '<dd>' . dformat($this->data['created']) . '</dd>';
811c845774SAndreas Gohr
821c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_modified') . '</dt>';
831c845774SAndreas Gohr        $html .= '<dd>' . dformat($this->data['modified']) . '</dd>';
841c845774SAndreas Gohr
851c845774SAndreas Gohr        // print top 5 authors
861c845774SAndreas Gohr        if(!is_array($this->data['authors'])) $this->data['authors'] = array();
871c845774SAndreas Gohr        arsort($this->data['authors']);
881c845774SAndreas Gohr        $top5 = array_slice($this->data['authors'], 0, 5);
891c845774SAndreas Gohr        $cnt = count($top5);
901c845774SAndreas Gohr        $i = 1;
911c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_authors') . '</dt>';
921c845774SAndreas Gohr        $html .= '<dd>';
931c845774SAndreas Gohr        foreach($top5 as $a => $e) {
941c845774SAndreas Gohr            if($a == '*') {
951c845774SAndreas Gohr                $html .= $this->helper->getLang('anonymous');
961c845774SAndreas Gohr            } else {
971c845774SAndreas Gohr                $html .= editorinfo($a);
981c845774SAndreas Gohr            }
991c845774SAndreas Gohr            $html .= ' (' . $e . ')';
1001c845774SAndreas Gohr            if($i++ < $cnt) $html .= ', ';
1011c845774SAndreas Gohr        }
1021c845774SAndreas Gohr        $html .= '</dd>';
1031c845774SAndreas Gohr
1041c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_changes') . '</dt>';
1051c845774SAndreas Gohr        $html .= '<dd>' . $this->data['changes'] . '</dd>';
1061c845774SAndreas Gohr
1071c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_chars') . '</dt>';
1081c845774SAndreas Gohr        $html .= '<dd>' . $this->data['chars'] . '</dd>';
1091c845774SAndreas Gohr
1101c845774SAndreas Gohr        $html .= '<dt>' . $this->helper->getLang('g_words') . '</dt>';
1111c845774SAndreas Gohr        $html .= '<dd>' . $this->data['words'] . '</dd>';
1121c845774SAndreas Gohr
1131c845774SAndreas Gohr        $html .= '</dl>';
1141c845774SAndreas Gohr        $html .= '</div>';
1151c845774SAndreas Gohr
1161c845774SAndreas Gohr        // output all the problems
1171c845774SAndreas Gohr        if($this->data['score']) {
1181c845774SAndreas Gohr            $html .= '<h2>' . $this->helper->getLang('errorsfound_h') . '</h2>';
1191c845774SAndreas Gohr            $html .= '<p>' . $this->helper->getLang('errorsfound') . '</p>';
1201c845774SAndreas Gohr            $html .= '<div>';
1211c845774SAndreas Gohr            arsort($this->data['err']); #sort by score
1221c845774SAndreas Gohr            foreach($this->data['err'] as $err => $val) {
1231c845774SAndreas Gohr                if($val) {
1241c845774SAndreas Gohr                    $html .= '<h3>';
1251c845774SAndreas Gohr                    $html .= sprintf($this->helper->getLang($err . '_h'), $val);
1261c845774SAndreas Gohr                    $html .= '<span class="qc_icon qc_bad">';
1271c845774SAndreas Gohr                    $html .= inlineSVG(__DIR__ . '/svg/bad.svg');
1281c845774SAndreas Gohr                    $html .= '<span>' . $val . '</span>';
1291c845774SAndreas Gohr                    $html .= '</span>';
1301c845774SAndreas Gohr                    $html .= '</h3>';
1311c845774SAndreas Gohr                    $html .= '<p>' . sprintf($this->helper->getLang($err), $val) . '</p>';
1321c845774SAndreas Gohr                }
1331c845774SAndreas Gohr            }
1341c845774SAndreas Gohr            $html .= '</div>';
1351c845774SAndreas Gohr        }
1361c845774SAndreas Gohr
1371c845774SAndreas Gohr        return $html;
1381c845774SAndreas Gohr    }
1391c845774SAndreas Gohr}
140