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