xref: /plugin/combo/action/qualitymessage.php (revision 85e82846b0a214bc35e62864fa49d9cad0723d0e)
1<?php
2
3use ComboStrap\Analytics;
4use ComboStrap\Identity;
5use ComboStrap\LogUtility;
6use ComboStrap\Message;
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/Message.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     * Disable the message totally
38     */
39    const CONF_DISABLE_QUALITY_MONITORING = "disableDynamicQualityMonitoring";
40
41    /**
42     * Key in the frontmatter that disable the message
43     */
44    const DISABLE_INDICATOR = "dynamic_quality_monitoring";
45
46
47    function __construct()
48    {
49        // enable direct access to language strings
50        // ie $this->lang
51        $this->setupLocale();
52    }
53
54
55    function register(Doku_Event_Handler $controller)
56    {
57
58        if(!$this->getConf(self::CONF_DISABLE_QUALITY_MONITORING)) {
59            $controller->register_hook(
60                'TPL_ACT_RENDER',
61                'BEFORE',
62                $this,
63                '_displayQualityMessage',
64                array()
65            );
66        }
67
68
69    }
70
71
72    /**
73     * Main function; dispatches the visual comment actions
74     * @param   $event Doku_Event
75     */
76    function _displayQualityMessage(&$event, $param)
77    {
78        if ($event->data == 'show') {
79
80            /**
81             * Quality is just for the writers
82             */
83            if (!Identity::isWriter()) {
84                return;
85            }
86
87            $note = $this->createQualityNote(PluginUtility::getPageId(), $this);
88            if ($note != null) {
89                ptln($note->toHtml());
90            }
91        }
92
93    }
94
95    /**
96     * @param $pageId
97     * @param $plugin - Plugin
98     * @return Message|null
99     */
100    static public function createQualityNote($pageId, $plugin)
101    {
102        $page = Page::createPageFromId($pageId);
103
104        if ($page->isSlot()) {
105            return null;
106        }
107
108        if (!$page->isQualityMonitored()) {
109            return null;
110        }
111
112        if ($page->exists()) {
113            $analytics = $page->getAnalyticsFromFs();
114            $rules = $analytics[Analytics::QUALITY][Analytics::RULES];
115
116
117            /**
118             * We may got null
119             * array_key_exists() expects parameter 2 to be array,
120             * null given in /opt/www/datacadamia.com/lib/plugins/combo/action/qualitymessage.php on line 113
121             */
122            if ($rules==null){
123                return null;
124            }
125
126            /**
127             * If there is no info, nothing to show
128             */
129            if (!array_key_exists(Analytics::INFO, $rules)) {
130                return null;
131            }
132
133            /**
134             * The error info
135             */
136            $qualityInfoRules = $rules[Analytics::INFO];
137
138            /**
139             * Excluding the excluded rules
140             */
141            global $conf;
142            $excludedRulesConf = $conf['plugin'][PluginUtility::PLUGIN_BASE_NAME][self::CONF_EXCLUDED_QUALITY_RULES_FROM_DYNAMIC_MONITORING];
143            $excludedRules = preg_split("/,/", $excludedRulesConf);
144            foreach ($excludedRules as $excludedRule) {
145                if (array_key_exists($excludedRule, $qualityInfoRules)) {
146                    unset($qualityInfoRules[$excludedRule]);
147                }
148            }
149
150            if (sizeof($qualityInfoRules) > 0) {
151
152                $qualityScore = $analytics[Analytics::QUALITY][renderer_plugin_combo_analytics::SCORING][renderer_plugin_combo_analytics::SCORE];
153                $message = new Message($plugin);
154                $message->addContent("<p>Well played, you got a " . PluginUtility::getUrl("quality:score", "quality score") . " of {$qualityScore} !</p>");
155                if ($page->isLowQualityPage()) {
156                    $analytics = $page->getAnalyticsFromFs(true);
157                    $mandatoryFailedRules = $analytics[Analytics::QUALITY][Analytics::FAILED_MANDATORY_RULES];
158                    $rulesUrl = PluginUtility::getUrl("quality:rule", "rules");
159                    $lqPageUrl = PluginUtility::getUrl("low_quality_page", "low quality page");
160                    $message->addContent("<div class='alert alert-info'>This is a {$lqPageUrl} because it has failed the following mandatory {$rulesUrl}:");
161                    $message->addContent("<ul style='margin-bottom: 0'>");
162                    /**
163                     * A low quality page should have
164                     * failed mandatory rules
165                     * but due to the asycn nature, sometimes
166                     * we don't have an array
167                     */
168                    if (is_array($mandatoryFailedRules)) {
169                        foreach ($mandatoryFailedRules as $mandatoryFailedRule) {
170                            $message->addContent("<li>" . PluginUtility::getUrl("quality:rule#list", $mandatoryFailedRule) . "</li>");
171                        }
172                    }
173                    $message->addContent("</ul>");
174                    $message->addContent("</div>");
175                }
176                $message->addContent("<p>You can still win a couple of points.</p>");
177                $message->addContent("<ul>");
178                foreach ($qualityInfoRules as $qualityRule => $qualityInfo) {
179                    $message->addContent("<li>");
180                    $message->addContent($qualityInfo);
181                    $message->addContent("</li>");
182                }
183                $message->addContent("</ul>");
184
185                $message->setSignatureCanonical("quality:dynamic_monitoring");
186                $message->setSignatureName("Quality Dynamic Monitoring Feature");
187                $message->setType(Message::TYPE_CLASSIC);
188                $message->setClass(self::QUALITY_BOX_CLASS);
189                return $message;
190
191
192            }
193        }
194        return null;
195    }
196
197
198}
199