2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Representation of a suggested action associated with a prediction.
20 * @package core_analytics
21 * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_analytics
;
27 defined('MOODLE_INTERNAL') ||
die();
30 * Representation of a suggested action associated with a prediction.
32 * @package core_analytics
33 * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class prediction_action
extends action
{
39 * Prediction action constructor.
41 * @param string $actionname They should match a-zA-Z_0-9-, as we apply a PARAM_ALPHANUMEXT filter
42 * @param \core_analytics\prediction $prediction
43 * @param \moodle_url $actionurl The final URL where the user should be forwarded.
44 * @param \pix_icon $icon Link icon
45 * @param string $text Link text
46 * @param bool $primary Primary button or secondary.
47 * @param array $attributes Link attributes
48 * @param string|false $type
51 public function __construct($actionname, \core_analytics\prediction
$prediction, \moodle_url
$actionurl, \pix_icon
$icon,
52 $text, $primary = false, $attributes = array(), $type = false) {
54 $this->actionname
= $actionname;
56 $this->set_type($type);
58 $this->url
= self
::transform_to_forward_url($actionurl, $actionname, $prediction->get_prediction_data()->id
);
60 // The \action_menu_link items are displayed as an icon with a label, no need to show any text.
61 if ($primary === false) {
62 $this->actionlink
= new \action_menu_link_secondary
($this->url
, $icon, '', $attributes);
64 $this->actionlink
= new \action_menu_link_primary
($this->url
, $icon, '', $attributes);
69 * Transforms the provided url to an action url so we can record the user actions.
71 * Note that it is the caller responsibility to check that the provided actionname is valid for the prediction target.
73 * @param \moodle_url $actionurl
74 * @param string $actionname
75 * @param int $predictionid
78 public static function transform_to_forward_url(\moodle_url
$actionurl, string $actionname, int $predictionid): \moodle_url
{
80 // We want to track how effective are our suggested actions, we pass users through a script that will log these actions.
81 $params = ['action' => $actionname, 'predictionid' => $predictionid,
82 'forwardurl' => $actionurl->out(false)];
83 return new \
moodle_url('/report/insights/action.php', $params);