xref: /plugin/combo/action/qualitymessage.php (revision e68157ebf6d6af7c088f0d907234fcc5991444e9)
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            /**
112             * We may got null
113             * array_key_exists() expects parameter 2 to be array,
114             * null given in /opt/www/datacadamia.com/lib/plugins/combo/action/qualitymessage.php on line 113
115             */
116            if ($rules==null){
117                return null;
118            }
119
120            /**
121             * If there is no info, nothing to show
122             */
123            if (!array_key_exists(Analytics::INFO, $rules)) {
124                return null;
125            }
126
127            /**
128             * The error info
129             */
130            $qualityInfoRules = $rules[Analytics::INFO];
131
132            /**
133             * Excluding the excluded rules
134             */
135            global $conf;
136            $excludedRulesConf = $conf['plugin'][PluginUtility::PLUGIN_BASE_NAME][self::CONF_EXCLUDED_QUALITY_RULES_FROM_DYNAMIC_MONITORING];
137            $excludedRules = preg_split("/,/", $excludedRulesConf);
138            foreach ($excludedRules as $excludedRule) {
139                if (array_key_exists($excludedRule, $qualityInfoRules)) {
140                    unset($qualityInfoRules[$excludedRule]);
141                }
142            }
143
144            if (sizeof($qualityInfoRules) > 0) {
145
146                $qualityScore = $analytics[Analytics::QUALITY][renderer_plugin_combo_analytics::SCORING][renderer_plugin_combo_analytics::SCORE];
147                $message = new Note($plugin);
148                $message->addContent("<p>Well played, you got a " . PluginUtility::getUrl("quality:score", "quality score") . " of {$qualityScore} !</p>");
149                if ($page->isLowQualityPage()) {
150                    $analytics = $page->getAnalyticsFromFs(true);
151                    $mandatoryFailedRules = $analytics[Analytics::QUALITY][Analytics::FAILED_MANDATORY_RULES];
152                    $rulesUrl = PluginUtility::getUrl("quality:rule", "rules");
153                    $lqPageUrl = PluginUtility::getUrl("low_quality_page", "low quality page");
154                    $message->addContent("<div class='alert alert-info'>This is a {$lqPageUrl} because it has failed the following mandatory {$rulesUrl}:");
155                    $message->addContent("<ul style='margin-bottom: 0'>");
156                    /**
157                     * A low quality page should have
158                     * failed mandatory rules
159                     * but due to the asycn nature, sometimes
160                     * we don't have an array
161                     */
162                    if (is_array($mandatoryFailedRules)) {
163                        foreach ($mandatoryFailedRules as $mandatoryFailedRule) {
164                            $message->addContent("<li>" . PluginUtility::getUrl("quality:rule#list", $mandatoryFailedRule) . "</li>");
165                        }
166                    }
167                    $message->addContent("</ul>");
168                    $message->addContent("</div>");
169                }
170                $message->addContent("<p>You can still win a couple of points.</p>");
171                $message->addContent("<ul>");
172                foreach ($qualityInfoRules as $qualityRule => $qualityInfo) {
173                    $message->addContent("<li>");
174                    $message->addContent($qualityInfo);
175                    $message->addContent("</li>");
176                }
177                $message->addContent("</ul>");
178
179                $message->setSignatureCanonical("quality:dynamic_monitoring");
180                $message->setSignatureName("Quality Dynamic Monitoring Feature");
181                $message->setType(Note::TYPE_CLASSIC);
182                $message->setClass(self::QUALITY_BOX_CLASS);
183                return $message;
184
185
186            }
187        }
188        return null;
189    }
190
191
192}
193