1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Representation of a suggested action associated with a prediction.
19 *
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
23 */
24
25namespace core_analytics;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Representation of a suggested action associated with a prediction.
31 *
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
35 */
36class prediction_action extends action {
37
38    /**
39     * Prediction action constructor.
40     *
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
49     * @return void
50     */
51    public function __construct($actionname, \core_analytics\prediction $prediction, \moodle_url $actionurl, \pix_icon $icon,
52                                $text, $primary = false, $attributes = array(), $type = false) {
53
54        $this->actionname = $actionname;
55        $this->text = $text;
56        $this->set_type($type);
57
58        $this->url = self::transform_to_forward_url($actionurl, $actionname, $prediction->get_prediction_data()->id);
59
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);
63        } else {
64            $this->actionlink = new \action_menu_link_primary($this->url, $icon, '', $attributes);
65        }
66    }
67
68    /**
69     * Transforms the provided url to an action url so we can record the user actions.
70     *
71     * Note that it is the caller responsibility to check that the provided actionname is valid for the prediction target.
72     *
73     * @param  \moodle_url $actionurl
74     * @param  string      $actionname
75     * @param  int         $predictionid
76     * @return \moodle_url
77     */
78    public static function transform_to_forward_url(\moodle_url $actionurl, string $actionname, int $predictionid): \moodle_url {
79
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);
84    }
85}
86