MDL-64783 analytics: Styles for insights sent through email
authorDavid Monllaó <davidm@moodle.com>
Wed, 3 Apr 2019 19:06:04 +0000 (21:06 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 8 Apr 2019 22:29:57 +0000 (00:29 +0200)
analytics/classes/insights_generator.php
analytics/classes/local/target/base.php
analytics/templates/insight_info_message.mustache [new file with mode: 0644]
analytics/templates/insight_info_message_prediction.mustache [new file with mode: 0644]
lang/en/analytics.php

index 44f4a9c..ea1785f 100644 (file)
@@ -71,6 +71,7 @@ class insights_generator {
      * @return  null
      */
     public function generate($samplecontexts, $predictions) {
+        global $OUTPUT;
 
         $analyserclass = $this->target->get_analyser_class();
 
@@ -87,7 +88,8 @@ class insights_generator {
 
                 $insighturl = $this->target->get_insight_context_url($this->modelid, $context);
                 $fullmessage = get_string('insightinfomessage', 'analytics', $insighturl->out(false));
-                $fullmessagehtml = get_string('insightinfomessagehtml', 'analytics', $insighturl->out());
+
+                $fullmessagehtml = $OUTPUT->render_from_template('core_analytics/insight_info_message', ['url' => $insighturl->out(false)]);
                 $this->notifications($context, $insighturl, $fullmessage, $fullmessagehtml);
             }
         }
@@ -159,11 +161,15 @@ class insights_generator {
      * @return array Three items array with formats [\moodle_url, string, string]
      */
     private function prediction_info(\core_analytics\prediction $prediction) {
+        global $OUTPUT;
 
         $predictionactions = $this->target->prediction_actions($prediction, true, true);
 
-        $messageactions  = '';
-        $messageactionshtml  = '';
+        // For FORMAT_PLAIN.
+        $fullmessageplaintext  = '';
+
+        // For FORMAT_HTML.
+        $messageactions  = [];
         $insighturl = null;
         foreach ($predictionactions as $action) {
             $actionurl = $action->get_url();
@@ -174,20 +180,16 @@ class insights_generator {
                 $actionurl->param('forwardurl', $actiondoneurl->out(false));
             }
 
-            $btnstyle = 'btn-default';
             if (empty($insighturl)) {
                 // We use the primary action url as insight url so we log that the user followed the provided link.
                 $insighturl = $action->get_url();
-                $btnstyle = 'btn-primary';
             }
-            $messageactions .= $action->get_text() . ': ' . $action->get_url()->out(false) . PHP_EOL;
-            $messageactionshtml .= '<a href="' . $action->get_url()->out() . '" class="btn ' . $btnstyle . ' m-r-1 m-b-1">' .
-                $action->get_text() . '</a>';
+            $actiondata = (object)['url' => $action->get_url()->out(false), 'text' => $action->get_text()];
+            $fullmessageplaintext .= get_string('insightinfomessageaction', 'analytics', $actiondata) . PHP_EOL;
+            $messageactions[] = $actiondata;
         }
 
-        $fullmessage = get_string('insightinfomessageprediction', 'analytics', $messageactions);
-        $fullmessagehtml = get_string('insightinfomessagepredictionhtml', 'analytics', $messageactionshtml);
-
-        return [$insighturl, $fullmessage, $fullmessagehtml];
+        $fullmessagehtml = $OUTPUT->render_from_template('core_analytics/insight_info_message_prediction', ['actions' => $messageactions]);
+        return [$insighturl, $fullmessageplaintext, $fullmessagehtml];
     }
 }
index 686d455..9ae2290 100644 (file)
@@ -250,6 +250,8 @@ abstract class base extends \core_analytics\calculable {
     /**
      * The insight notification subject.
      *
+     * This is just a default message, you should overwrite it for a custom insight message.
+     *
      * @param  int $modelid
      * @param  \context $context
      * @return string
diff --git a/analytics/templates/insight_info_message.mustache b/analytics/templates/insight_info_message.mustache
new file mode 100644 (file)
index 0000000..54426a4
--- /dev/null
@@ -0,0 +1,60 @@
+{{!
+    This file is part of Moodle - http://moodle.org/
+
+    Moodle is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Moodle is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+}}
+{{!
+    @template core_analytics/insight_info_message
+
+    HTML message for insights
+
+    Classes required for JS:
+    * none
+
+    Data attributes required for JS:
+    * none
+
+    Example context (json):
+    {
+        "url": "https://moodle.org"
+    }
+}}
+<style>
+
+{{! Default btn-default styles. These styles are not applied to Moodle's web UI as there is a body:not(.dir-ltr):not(.dir-rtl)}}
+
+body:not(.dir-ltr):not(.dir-rtl) .btn-insight {
+    margin-bottom: 1rem!important;
+    margin-right: 1rem!important;
+    color: #212529;
+    background-color: #e9ecef;
+    border-color: #e9ecef
+    display: inline-block;
+    font-weight: 400;
+    text-align: center;
+    white-space: nowrap;
+    vertical-align: middle;
+    user-select: none;
+    border: 1px solid transparent;
+    padding: .375rem .75rem;
+    font-size: .9375rem;
+    line-height: 1.5;
+    border-radius: .25rem;
+    text-decoration: none;
+}
+</style>
+
+{{#str}} insightinfomessagehtml, analytics {{/str}}
+<br/><br/>
+<a class="btn btn-default btn-insight" href="{{url}}">{{#str}} viewinsight, analytics {{/str}}</a>
\ No newline at end of file
diff --git a/analytics/templates/insight_info_message_prediction.mustache b/analytics/templates/insight_info_message_prediction.mustache
new file mode 100644 (file)
index 0000000..9896031
--- /dev/null
@@ -0,0 +1,69 @@
+{{!
+    This file is part of Moodle - http://moodle.org/
+
+    Moodle is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Moodle is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+}}
+{{!
+    @template core_analytics/insight_info_message_prediction
+
+    HTML message for insights with one single prediction
+
+    Classes required for JS:
+    * none
+
+    Data attributes required for JS:
+    * none
+
+    Example context (json):
+    {
+        "actions": [
+            {
+                "url": "https://moodle.org",
+                "text": "Moodle"
+            }, {
+                "url": "https://en.wikipedia.org/wiki/Noodle",
+                "text": "Noodle"
+            }
+        ]
+    }
+}}
+
+{{! Default btn-default styles. These styles are not applied to Moodle's web UI as there is a body:not(.dir-ltr):not(.dir-rtl)}}
+
+<style>
+body:not(.dir-ltr):not(.dir-rtl) .btn-insight {
+    margin-bottom: 1rem!important;
+    margin-right: 1rem!important;
+    color: #212529;
+    background-color: #e9ecef;
+    border-color: #e9ecef
+    display: inline-block;
+    font-weight: 400;
+    text-align: center;
+    white-space: nowrap;
+    vertical-align: middle;
+    user-select: none;
+    border: 1px solid transparent;
+    padding: .375rem .75rem;
+    font-size: .9375rem;
+    line-height: 1.5;
+    border-radius: .25rem;
+    text-decoration: none;
+}
+</style>
+
+<br/>
+{{#actions}}
+    <a class="btn btn-default m-r-1 m-b-1 btn-insight" href="{{url}}">{{text}}</a>
+{{/actions}}
index 2b6c175..2849ab6 100644 (file)
@@ -67,13 +67,10 @@ $string['errorunknownaction'] = 'Unknown action';
 $string['eventpredictionactionstarted'] = 'Prediction process started';
 $string['eventinsightsviewed'] = 'Insights viewed';
 $string['fixedack'] = 'Acknowledged';
-$string['insightmessagesubject'] = 'New insight for "{$a->contextname}": {$a->insightname}';
+$string['insightmessagesubject'] = 'New insight for "{$a}"';
 $string['insightinfomessage'] = 'The system generated an insight for you: {$a}';
-$string['insightinfomessagehtml'] = 'The system generated an insight for you.<br/><br/><a class="btn btn-primary" href="{$a}">View insight</a>';
-$string['insightinfomessageprediction'] = 'Some suggested actions you could perform:
-
-{$a}';
-$string['insightinfomessagepredictionhtml'] = 'Some suggested actions you could perform:<br/><br/>{$a}';
+$string['insightinfomessagehtml'] = 'The system generated an insight for you.';
+$string['insightinfomessageaction'] = '{$a->text}: {$a->url}';
 $string['invalidtimesplitting'] = 'Model with ID {$a} needs a time-splitting method before it can be used to train.';
 $string['invalidanalysablefortimesplitting'] = 'It cannot be analysed using {$a} time-splitting method.';
 $string['nocourses'] = 'No courses to analyse';