MDL-58835 report_insights: Not useful and ack prediction actions
authorDavid Monllao <davidm@moodle.com>
Mon, 28 Aug 2017 10:39:32 +0000 (12:39 +0200)
committerDavid Monllao <davidm@moodle.com>
Thu, 7 Sep 2017 12:45:34 +0000 (14:45 +0200)
analytics/classes/local/target/base.php
report/insights/action.php
report/insights/amd/build/actions.min.js [new file with mode: 0644]
report/insights/amd/src/actions.js [new file with mode: 0644]
report/insights/classes/external.php [new file with mode: 0644]
report/insights/classes/output/insights_list.php
report/insights/db/services.php [new file with mode: 0644]
report/insights/version.php

index fdbf52f..5b741d2 100644 (file)
@@ -114,11 +114,11 @@ abstract class base extends \core_analytics\calculable {
     public function prediction_actions(\core_analytics\prediction $prediction, $includedetailsaction = false) {
         global $PAGE;
 
-        $PAGE->requires->js_call_amd('report_insights/actions', 'init');
+        $predictionid = $prediction->get_prediction_data()->id;
 
-        $actions = array();
+        $PAGE->requires->js_call_amd('report_insights/actions', 'init', array($predictionid));
 
-        $predictionid = $prediction->get_prediction_data()->id;
+        $actions = array();
 
         if ($includedetailsaction) {
 
@@ -130,15 +130,6 @@ abstract class base extends \core_analytics\calculable {
                 get_string('viewprediction', 'analytics'));
         }
 
-        // Flag as not useful.
-        $notusefulattrs = array(
-            'data-prediction-id' => $predictionid,
-            'data-prediction-methodname' => 'report_insights_set_notuseful_prediction'
-        );
-        $actions[] = new \core_analytics\prediction_action(\core_analytics\prediction::ACTION_NOT_USEFUL,
-            $prediction, new \moodle_url(''), new \pix_icon('t/delete', get_string('notuseful', 'analytics')),
-            get_string('notuseful', 'analytics'), false, $notusefulattrs);
-
         // Flag as fixed / solved.
         $fixedattrs = array(
             'data-prediction-id' => $predictionid,
@@ -148,6 +139,15 @@ abstract class base extends \core_analytics\calculable {
             $prediction, new \moodle_url(''), new \pix_icon('t/check', get_string('fixedack', 'analytics')),
             get_string('fixedack', 'analytics'), false, $fixedattrs);
 
+        // Flag as not useful.
+        $notusefulattrs = array(
+            'data-prediction-id' => $predictionid,
+            'data-prediction-methodname' => 'report_insights_set_notuseful_prediction'
+        );
+        $actions[] = new \core_analytics\prediction_action(\core_analytics\prediction::ACTION_NOT_USEFUL,
+            $prediction, new \moodle_url(''), new \pix_icon('t/delete', get_string('notuseful', 'analytics')),
+            get_string('notuseful', 'analytics'), false, $notusefulattrs);
+
         return $actions;
     }
 
index b9f0491..6f2d333 100644 (file)
@@ -34,16 +34,15 @@ if ($context->contextlevel < CONTEXT_COURSE) {
     $PAGE->set_context($context);
 }
 
+if (empty($forwardurl)) {
+    $params = array('modelid' => $model->get_id(), 'contextid' => $context->id);
+    $forwardurl = new \moodle_url('/report/insights/insights.php', $params);
+}
+
 $params = array('predictionid' => $prediction->get_prediction_data()->id, 'action' => $actionname, 'forwardurl' => $forwardurl);
 $url = new \moodle_url('/report/insights/action.php', $params);
 $PAGE->set_url($url);
 
-// Check that the provided action exists.
-$actions = $model->get_target()->prediction_actions($prediction, true);
-if (!isset($actions[$actionname])) {
-    throw new \moodle_exception('errorunknownaction', 'analytics');
-}
-
 $modelready = $model->is_enabled() && $model->is_trained() && $model->predictions_exist($context);
 if (!$modelready) {
 
@@ -58,11 +57,6 @@ if (!$modelready) {
     exit(0);
 }
 
-$eventdata = array (
-    'context' => $context,
-    'objectid' => $predictionid,
-    'other' => array('actionname' => $actionname)
-);
-\core\event\prediction_action_started::create($eventdata)->trigger();
+$prediction->action_executed($actionname, $model->get_target());
 
 redirect($forwardurl);
diff --git a/report/insights/amd/build/actions.min.js b/report/insights/amd/build/actions.min.js
new file mode 100644 (file)
index 0000000..59d5035
Binary files /dev/null and b/report/insights/amd/build/actions.min.js differ
diff --git a/report/insights/amd/src/actions.js b/report/insights/amd/src/actions.js
new file mode 100644 (file)
index 0000000..2ffe7f2
--- /dev/null
@@ -0,0 +1,67 @@
+// 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/>.
+
+/**
+ * Module to manage report insights actions that are executed using AJAX.
+ *
+ * @package    report_insights
+ * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * This module manages prediction actions that require AJAX requests.
+ *
+ * @module report_insights/actions
+ */
+define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notification) {
+
+    return {
+
+        /**
+         * Attach on click handlers to hide predictions.
+         *
+         * @param {Number} predictionId The prediction id.
+         * @access public
+         */
+        init: function(predictionId) {
+
+            // Select the prediction with the provided id ensuring that an external function is set as method name.
+            $('a[data-prediction-methodname][data-prediction-id=' + predictionId + ']').on('click', function(e) {
+                e.preventDefault();
+                var action = $(e.currentTarget);
+                var methodname = action.attr('data-prediction-methodname');
+                var predictionContainers = action.closest('tr');
+
+                if (predictionContainers.length > 0) {
+                    var promise = Ajax.call([
+                        {
+                            methodname: methodname,
+                            args: {predictionid: predictionId}
+                        }
+                    ])[0];
+                    promise.done(function() {
+                        predictionContainers[0].remove();
+
+                        // Move back if no remaining predictions.
+                        if ($('.insights-list tr').length === 0) {
+                            window.history.back();
+                        }
+                    }).fail(Notification.exception);
+                }
+            });
+        }
+    };
+});
diff --git a/report/insights/classes/external.php b/report/insights/classes/external.php
new file mode 100644 (file)
index 0000000..44e6123
--- /dev/null
@@ -0,0 +1,155 @@
+<?php
+// 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/>.
+
+/**
+ * This is the external API for this component.
+ *
+ * @package    report_insights
+ * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace report_insights;
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once("$CFG->libdir/externallib.php");
+
+use external_api;
+use external_function_parameters;
+use external_value;
+use external_single_structure;
+use external_warnings;
+
+/**
+ * This is the external API for this component.
+ *
+ * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class external extends external_api {
+
+    /**
+     * set_notuseful_prediction parameters.
+     *
+     * @return external_function_parameters
+     * @since  Moodle 3.4
+     */
+    public static function set_notuseful_prediction_parameters() {
+        return new external_function_parameters(
+            array(
+                'predictionid' => new external_value(PARAM_INT, 'The prediction id', VALUE_REQUIRED)
+            )
+        );
+    }
+
+    /**
+     * Flags a prediction as fixed so no need to display it any more.
+     *
+     * @param int $predictionid
+     * @return array an array of warnings and a boolean
+     * @since  Moodle 3.4
+     */
+    public static function set_notuseful_prediction($predictionid) {
+
+        $params = self::validate_parameters(self::set_notuseful_prediction_parameters(), array('predictionid' => $predictionid));
+
+        list($model, $prediction, $context) = self::validate_prediction($params['predictionid']);
+
+        $prediction->action_executed(\core_analytics\prediction::ACTION_NOT_USEFUL, $model->get_target());
+
+        $success = true;
+        return array('success' => $success, 'warnings' => array());
+    }
+
+    /**
+     * set_notuseful_prediction return
+     *
+     * @return external_description
+     * @since  Moodle 3.4
+     */
+    public static function set_notuseful_prediction_returns() {
+        return new external_single_structure(
+            array(
+                'success' => new external_value(PARAM_BOOL, 'True if the prediction was successfully flagged as not useful.'),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
+
+    /**
+     * set_fixed_prediction parameters.
+     *
+     * @return external_function_parameters
+     * @since  Moodle 3.4
+     */
+    public static function set_fixed_prediction_parameters() {
+        return new external_function_parameters(
+            array(
+                'predictionid' => new external_value(PARAM_INT, 'The prediction id', VALUE_REQUIRED)
+            )
+        );
+    }
+
+    /**
+     * Flags a prediction as fixed so no need to display it any more.
+     *
+     * @param int $predictionid
+     * @return array an array of warnings and a boolean
+     * @since  Moodle 3.4
+     */
+    public static function set_fixed_prediction($predictionid) {
+
+        $params = self::validate_parameters(self::set_fixed_prediction_parameters(), array('predictionid' => $predictionid));
+
+        list($model, $prediction, $context) = self::validate_prediction($params['predictionid']);
+
+        $prediction->action_executed(\core_analytics\prediction::ACTION_FIXED, $model->get_target());
+
+        $success = true;
+        return array('success' => $success, 'warnings' => array());
+    }
+
+    /**
+     * set_fixed_prediction return
+     *
+     * @return external_description
+     * @since  Moodle 3.4
+     */
+    public static function set_fixed_prediction_returns() {
+        return new external_single_structure(
+            array(
+                'success' => new external_value(PARAM_BOOL, 'True if the prediction was successfully flagged as fixed.'),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
+
+    /**
+     * Validates access to the prediction and returns it.
+     *
+     * @param int $predictionid
+     * @return array array($model, $prediction, $context)
+     */
+    protected static function validate_prediction($predictionid) {
+
+        list($model, $prediction, $context) = \core_analytics\manager::get_prediction($predictionid);
+
+        self::validate_context($context);
+
+        return array($model, $prediction, $context);
+    }
+}
index 6596750..1da683e 100644 (file)
@@ -93,7 +93,7 @@ class insights_list implements \renderable, \templatable {
         $total = 0;
 
         if ($this->model->uses_insights()) {
-            $predictionsdata = $this->model->get_predictions($this->context, $this->page, $this->perpage);
+            $predictionsdata = $this->model->get_predictions($this->context, true, $this->page, $this->perpage);
 
             $data->insights = array();
             if ($predictionsdata) {
diff --git a/report/insights/db/services.php b/report/insights/db/services.php
new file mode 100644 (file)
index 0000000..161954b
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+// 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/>.
+
+/**
+ * Report insights webservice definitions.
+ *
+ * @package    report_insights
+ * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$functions = array(
+
+    'report_insights_set_notuseful_prediction' => array(
+        'classname'   => 'report_insights\external',
+        'methodname'  => 'set_notuseful_prediction',
+        'description' => 'Flags the prediction as not useful.',
+        'type'        => 'write',
+        'ajax'          => true,
+        'services'    => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
+
+    'report_insights_set_fixed_prediction' => array(
+        'classname'   => 'report_insights\external',
+        'methodname'  => 'set_fixed_prediction',
+        'description' => 'Flags a prediction as fixed.',
+        'type'        => 'write',
+        'services'    => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
+        'ajax'          => true,
+    )
+);
+
index 8529b85..abcc897 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2017051501; // The current plugin version (Date: YYYYMMDDXX).
+$plugin->version   = 2017051502; // The current plugin version (Date: YYYYMMDDXX).
 $plugin->requires  = 2017050500; // Requires this Moodle version.
 $plugin->component = 'report_insights'; // Full name of the plugin (used for diagnostics).