xref: /plugin/combo/action/qualitymessage.php (revision eee76a3db97db19452d160583c8cef2cf6cb01d3)
1<?php
2
3use ComboStrap\Analytics;
4use ComboStrap\Auth;
5use ComboStrap\LogUtility;
6use ComboStrap\Note;
7use ComboStrap\Page;
8use ComboStrap\PagesIndex;
9use ComboStrap\PluginUtility;
10
11if (!defined('DOKU_INC')) die();
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
13
14
15require_once(__DIR__ . '/../class/Page.php');
16require_once(__DIR__ . '/../class/Note.php');
17
18/**
19 *
20 * Show a quality message
21 *
22 *
23 *
24 */
25class action_plugin_combo_qualitymessage extends DokuWiki_Action_Plugin
26{
27
28    // a class can not start with a number
29    const QUALITY_BOX_CLASS = "quality-message";
30
31    /**
32     * The quality rules that will not show
33     * up in the messages
34     */
35    const CONF_EXCLUDED_QUALITY_RULES_FROM_DYNAMIC_MONITORING = "excludedQualityRulesFromDynamicMonitoring";
36
37    /**
38     * Key in the frontmatter that disable the message
39     */
40    const DISABLE_INDICATOR = "dynamic_quality_monitoring";
41
42
43    function __construct()
44    {
45        // enable direct access to language strings
46        // ie $this->lang
47        $this->setupLocale();
48    }
49
50
51    function register(Doku_Event_Handler $controller)
52    {
53
54        $controller->register_hook(
55            'TPL_ACT_RENDER',
56            'BEFORE',
57            $this,
58            '_displayQualityMessage',
59            array()
60        );
61
62
63    }
64
65
66    /**
67     * Main function; dispatches the visual comment actions
68     * @param   $event Doku_Event
69     */
70    function _displayQualityMessage(&$event, $param)
71    {
72        if ($event->data == 'show') {
73
74            /**
75             * Quality is just for the writers
76             */
77            if (!Auth::isWriter()) {
78                return;
79            }
80
81            $note = $this->createQualityNote(PluginUtility::getPageId(), $this);
82            if ($note != null) {
83                ptln($note->getHtml());
84            }
85        }
86
87    }
88
89    /**
90     * @param $pageId
91     * @param $plugin - Plugin
92     * @return Note|null
93     */
94    static public function createQualityNote($pageId, $plugin)
95    {
96        $page = new Page($pageId);
97
98        if ($page->isBar()) {
99            return null;
100        }
101
102        if (!$page->isQualityMonitored()) {
103            return null;
104        }
105
106        if ($page->existInFs()) {
107            $analytics = $page->getAnalyticsFromFs();
108            $rules = $analytics[Analytics::QUALITY][Analytics::RULES];
109
110            /**
111             * If there is no info, nothing to show
112             */
113            if (!array_key_exists(Analytics::INFO, $rules)) {
114                return null;
115            }
116
117            /**
118             * The error info
119             */
120            $qualityInfoRules = $rules[Analytics::INFO];
121
122            /**
123             * Excluding the excluded rules
124             */
125            global $conf;
126            $excludedRulesConf = $conf['plugin'][PluginUtility::PLUGIN_BASE_NAME][self::CONF_EXCLUDED_QUALITY_RULES_FROM_DYNAMIC_MONITORING];
127            $excludedRules = preg_split("/,/", $excludedRulesConf);
128            foreach ($excludedRules as $excludedRule) {
129                if (array_key_exists($excludedRule, $qualityInfoRules)) {
130                    unset($qualityInfoRules[$excludedRule]);
131                }
132            }
133
134            if (sizeof($qualityInfoRules) > 0) {
135
136                $qualityScore = $analytics[Analytics::QUALITY][renderer_plugin_combo_analytics::SCORING][renderer_plugin_combo_analytics::SCORE];
137                $message = new Note($plugin);
138                $message->addContent("<p>Well played, you got a " . PluginUtility::getUrl("quality:score", "quality score") . " of {$qualityScore} !</p>");
139                if ($page->isLowQualityPage()) {
140                    $analytics = $page->getAnalyticsFromFs(true);
141                    $mandatoryFailedRules = $analytics[Analytics::QUALITY][Analytics::FAILED_MANDATORY_RULES];
142                    $rulesUrl = PluginUtility::getUrl("quality:rule", "rules");
143                    $lqPageUrl = PluginUtility::getUrl("low_quality_page", "low quality page");
144                    $message->addContent("<div class='alert alert-info'>This is a {$lqPageUrl} because it has failed the following mandatory {$rulesUrl}:");
145                    $message->addContent("<ul style='margin-bottom: 0'>");
146
147                    foreach ($mandatoryFailedRules as $mandatoryFailedRule) {
148                        $message->addContent("<li>".PluginUtility::getUrl("quality:rule#list", $mandatoryFailedRule)."</li>");
149                    }
150                    $message->addContent("</ul>");
151                    $message->addContent("</div>");
152                }
153                $message->addContent("<p>You can still win a couple of points.</p>");
154                $message->addContent("<ul>");
155                foreach ($qualityInfoRules as $qualityRule => $qualityInfo) {
156                    $message->addContent("<li>");
157                    $message->addContent($qualityInfo);
158                    $message->addContent("</li>");
159                }
160                $message->addContent("</ul>");
161
162                $message->setSignatureCanonical("quality:dynamic_monitoring");
163                $message->setSignatureName("Quality Dynamic Monitoring Feature");
164                $message->setType(Note::TYPE_CLASSIC);
165                $message->setClass(self::QUALITY_BOX_CLASS);
166                return $message;
167
168
169            }
170        }
171        return null;
172    }
173
174
175}
176