MDL-60277 report_insights: Pass the prediction as a float
[moodle.git] / report / insights / classes / output / insight.php
CommitLineData
373b1de1
DM
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/**
f9e7447f 18 * Single insight view page.
373b1de1
DM
19 *
20 * @package report_insights
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 report_insights\output;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
f9e7447f 30 * Single insight view page.
373b1de1
DM
31 *
32 * @package report_insights
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 */
f9e7447f 36class insight implements \renderable, \templatable {
373b1de1
DM
37
38 /**
39 * @var \core_analytics\model
40 */
41 protected $model;
42
43 /**
44 * @var \core_analytics\prediction
45 */
46 protected $prediction;
47
1611308b
DM
48 /**
49 * @var bool
50 */
51 protected $includedetailsaction = false;
52
413f19bc
DM
53 /**
54 * Constructor
55 *
56 * @param \core_analytics\prediction $prediction
57 * @param \core_analytics\model $model
58 * @param bool $includedetailsaction
59 * @return void
60 */
1611308b 61 public function __construct(\core_analytics\prediction $prediction, \core_analytics\model $model, $includedetailsaction = false) {
373b1de1
DM
62 $this->prediction = $prediction;
63 $this->model = $model;
1611308b 64 $this->includedetailsaction = $includedetailsaction;
373b1de1
DM
65 }
66
67 /**
68 * Exports the data.
69 *
70 * @param \renderer_base $output
71 * @return \stdClass
72 */
73 public function export_for_template(\renderer_base $output) {
74
75 $data = new \stdClass();
1d5b1d01 76 $data->insightname = format_string($this->model->get_target()->get_name());
373b1de1
DM
77
78 // Sample info (determined by the analyser).
357507b4 79 list($data->sampledescription, $samplerenderable) = $this->model->prediction_sample_description($this->prediction);
373b1de1
DM
80
81 // Sampleimage is a renderable we should pass it to HTML.
357507b4 82 if ($samplerenderable) {
1d5b1d01 83 $data->sampleimage = $output->render($samplerenderable);
373b1de1
DM
84 }
85
86 // Prediction info.
87 $predictedvalue = $this->prediction->get_prediction_data()->prediction;
88 $predictionid = $this->prediction->get_prediction_data()->id;
89 $data->predictiondisplayvalue = $this->model->get_target()->get_display_value($predictedvalue);
7c37e56f
DM
90 list($data->style, $data->outcomeicon) = $this->get_calculation_display($this->model->get_target(),
91 floatval($predictedvalue), $output);
373b1de1 92
1611308b 93 $actions = $this->model->get_target()->prediction_actions($this->prediction, $this->includedetailsaction);
373b1de1
DM
94 if ($actions) {
95 $actionsmenu = new \action_menu();
96 $actionsmenu->set_menu_trigger(get_string('actions'));
97 $actionsmenu->set_owner_selector('prediction-actions-' . $predictionid);
98 $actionsmenu->set_alignment(\action_menu::TL, \action_menu::BL);
99
100 // Add all actions defined by the target.
101 foreach ($actions as $action) {
102 $actionsmenu->add($action->get_action_link());
103 }
104 $data->actions = $actionsmenu->export_for_template($output);
105 } else {
106 $data->actions = false;
107 }
108
109 // Calculated indicators values.
110 $data->calculations = array();
111 $calculations = $this->prediction->get_calculations();
112 foreach ($calculations as $calculation) {
113
114 // Hook for indicators with extra features that should not be displayed (e.g. discrete indicators).
115 if (!$calculation->indicator->should_be_displayed($calculation->value, $calculation->subtype)) {
116 continue;
117 }
118
119 if ($calculation->value === null) {
120 // We don't show values that could not be calculated.
121 continue;
122 }
123
124 $obj = new \stdClass();
1611308b 125 $obj->name = call_user_func(array($calculation->indicator, 'get_name'));
373b1de1 126 $obj->displayvalue = $calculation->indicator->get_display_value($calculation->value, $calculation->subtype);
7c37e56f
DM
127 list($obj->style, $obj->outcomeicon) = $this->get_calculation_display($calculation->indicator,
128 floatval($calculation->value), $output, $calculation->subtype);
373b1de1
DM
129
130 $data->calculations[] = $obj;
131 }
132
1d5b1d01
DM
133 if (empty($data->calculations)) {
134 $data->nocalculations = (object)array(
135 'message' => get_string('nodetailsavailable', 'report_insights'),
136 'closebutton' => false
137 );
138 }
139
373b1de1
DM
140 return $data;
141 }
27ae9af4
DM
142
143 /**
1d5b1d01 144 * Returns display info for the calculated value outcome.
27ae9af4
DM
145 *
146 * @param \core_analytics\calculable $calculable
5c5cb3ee 147 * @param float $value
1d5b1d01 148 * @param \renderer_base $output
27ae9af4 149 * @param string|false $subtype
1d5b1d01 150 * @return array The style as 'success', 'info', 'warning' or 'danger' and pix_icon
27ae9af4 151 */
1d5b1d01 152 protected function get_calculation_display(\core_analytics\calculable $calculable, $value, $output, $subtype = false) {
27ae9af4
DM
153 $outcome = $calculable->get_calculation_outcome($value, $subtype);
154 switch ($outcome) {
155 case \core_analytics\calculable::OUTCOME_NEUTRAL:
156 $style = '';
1d5b1d01
DM
157 $text = get_string('outcomeneutral', 'report_insights');
158 $icon = 't/check';
27ae9af4
DM
159 break;
160 case \core_analytics\calculable::OUTCOME_VERY_POSITIVE:
1d5b1d01
DM
161 $style = 'success';
162 $text = get_string('outcomeverypositive', 'report_insights');
163 $icon = 't/approve';
27ae9af4
DM
164 break;
165 case \core_analytics\calculable::OUTCOME_OK:
1d5b1d01
DM
166 $style = 'info';
167 $text = get_string('outcomeok', 'report_insights');
168 $icon = 't/check';
27ae9af4
DM
169 break;
170 case \core_analytics\calculable::OUTCOME_NEGATIVE:
1d5b1d01
DM
171 $style = 'warning';
172 $text = get_string('outcomenegative', 'report_insights');
173 $icon = 'i/warning';
27ae9af4
DM
174 break;
175 case \core_analytics\calculable::OUTCOME_VERY_NEGATIVE:
1d5b1d01
DM
176 $style = 'danger';
177 $text = get_string('outcomeverynegative', 'report_insights');
178 $icon = 'i/warning';
27ae9af4
DM
179 break;
180 default:
181 throw new \coding_exception('The outcome returned by ' . get_class($calculable) . '::get_calculation_outcome is ' .
182 'not one of the accepted values. Please use \core_analytics\calculable::OUTCOME_VERY_POSITIVE, ' .
5c5cb3ee
DM
183 '\core_analytics\calculable::OUTCOME_OK, \core_analytics\calculable::OUTCOME_NEGATIVE, ' .
184 '\core_analytics\calculable::OUTCOME_VERY_NEGATIVE or \core_analytics\calculable::OUTCOME_NEUTRAL');
27ae9af4 185 }
1d5b1d01
DM
186 $icon = new \pix_icon($icon, $text);
187 return array($style, $icon->export_for_template($output));
27ae9af4 188 }
373b1de1 189}