MDL-60277 report_insights: Pass the prediction as a float
[moodle.git] / report / insights / classes / output / insight.php
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/>.
17 /**
18  * Single insight view page.
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  */
25 namespace report_insights\output;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Single insight view page.
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  */
36 class insight implements \renderable, \templatable {
38     /**
39      * @var \core_analytics\model
40      */
41     protected $model;
43     /**
44      * @var \core_analytics\prediction
45      */
46     protected $prediction;
48     /**
49      * @var bool
50      */
51     protected $includedetailsaction = false;
53     /**
54      * Constructor
55      *
56      * @param \core_analytics\prediction $prediction
57      * @param \core_analytics\model $model
58      * @param bool $includedetailsaction
59      * @return void
60      */
61     public function __construct(\core_analytics\prediction $prediction, \core_analytics\model $model, $includedetailsaction = false) {
62         $this->prediction = $prediction;
63         $this->model = $model;
64         $this->includedetailsaction = $includedetailsaction;
65     }
67     /**
68      * Exports the data.
69      *
70      * @param \renderer_base $output
71      * @return \stdClass
72      */
73     public function export_for_template(\renderer_base $output) {
75         $data = new \stdClass();
76         $data->insightname = format_string($this->model->get_target()->get_name());
78         // Sample info (determined by the analyser).
79         list($data->sampledescription, $samplerenderable) = $this->model->prediction_sample_description($this->prediction);
81         // Sampleimage is a renderable we should pass it to HTML.
82         if ($samplerenderable) {
83             $data->sampleimage = $output->render($samplerenderable);
84         }
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);
90         list($data->style, $data->outcomeicon) = $this->get_calculation_display($this->model->get_target(),
91             floatval($predictedvalue), $output);
93         $actions = $this->model->get_target()->prediction_actions($this->prediction, $this->includedetailsaction);
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);
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         }
109         // Calculated indicators values.
110         $data->calculations = array();
111         $calculations = $this->prediction->get_calculations();
112         foreach ($calculations as $calculation) {
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             }
119             if ($calculation->value === null) {
120                 // We don't show values that could not be calculated.
121                 continue;
122             }
124             $obj = new \stdClass();
125             $obj->name = call_user_func(array($calculation->indicator, 'get_name'));
126             $obj->displayvalue = $calculation->indicator->get_display_value($calculation->value, $calculation->subtype);
127             list($obj->style, $obj->outcomeicon) = $this->get_calculation_display($calculation->indicator,
128                 floatval($calculation->value), $output, $calculation->subtype);
130             $data->calculations[] = $obj;
131         }
133         if (empty($data->calculations)) {
134             $data->nocalculations = (object)array(
135                 'message' => get_string('nodetailsavailable', 'report_insights'),
136                 'closebutton' => false
137             );
138         }
140         return $data;
141     }
143     /**
144      * Returns display info for the calculated value outcome.
145      *
146      * @param \core_analytics\calculable $calculable
147      * @param float $value
148      * @param \renderer_base $output
149      * @param string|false $subtype
150      * @return array The style as 'success', 'info', 'warning' or 'danger' and pix_icon
151      */
152     protected function get_calculation_display(\core_analytics\calculable $calculable, $value, $output, $subtype = false) {
153         $outcome = $calculable->get_calculation_outcome($value, $subtype);
154         switch ($outcome) {
155             case \core_analytics\calculable::OUTCOME_NEUTRAL:
156                 $style = '';
157                 $text = get_string('outcomeneutral', 'report_insights');
158                 $icon = 't/check';
159                 break;
160             case \core_analytics\calculable::OUTCOME_VERY_POSITIVE:
161                 $style = 'success';
162                 $text = get_string('outcomeverypositive', 'report_insights');
163                 $icon = 't/approve';
164                 break;
165             case \core_analytics\calculable::OUTCOME_OK:
166                 $style = 'info';
167                 $text = get_string('outcomeok', 'report_insights');
168                 $icon = 't/check';
169                 break;
170             case \core_analytics\calculable::OUTCOME_NEGATIVE:
171                 $style = 'warning';
172                 $text = get_string('outcomenegative', 'report_insights');
173                 $icon = 'i/warning';
174                 break;
175             case \core_analytics\calculable::OUTCOME_VERY_NEGATIVE:
176                 $style = 'danger';
177                 $text = get_string('outcomeverynegative', 'report_insights');
178                 $icon = 'i/warning';
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, ' .
183                     '\core_analytics\calculable::OUTCOME_OK, \core_analytics\calculable::OUTCOME_NEGATIVE, ' .
184                     '\core_analytics\calculable::OUTCOME_VERY_NEGATIVE or \core_analytics\calculable::OUTCOME_NEUTRAL');
185         }
186         $icon = new \pix_icon($icon, $text);
187         return array($style, $icon->export_for_template($output));
188     }