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/>.
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
25 namespace report_insights\output;
27 defined('MOODLE_INTERNAL') || die();
30 * Shows report_insights insights list.
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
36 class insights_list implements \renderable, \templatable {
39 * @var \core_analytics\model
49 * @var \core_analytics\model[]
51 protected $othermodels;
66 * @param \core_analytics\model $model
67 * @param \context $context
68 * @param \core_analytics\model[] $othermodels
70 * @param int $perpage The max number of results to fetch
73 public function __construct(\core_analytics\model $model, \context $context, $othermodels, $page = 0, $perpage = 100) {
74 $this->model = $model;
75 $this->context = $context;
76 $this->othermodels = $othermodels;
78 $this->perpage = $perpage;
84 * @param \renderer_base $output
87 public function export_for_template(\renderer_base $output) {
90 $data = new \stdClass();
91 $data->insightname = format_string($this->model->get_target()->get_name());
95 if ($this->model->uses_insights()) {
96 $predictionsdata = $this->model->get_predictions($this->context, true, $this->page, $this->perpage);
98 $data->predictions = array();
99 $predictionvalues = array();
101 if ($predictionsdata) {
102 list($total, $predictions) = $predictionsdata;
104 foreach ($predictions as $prediction) {
105 $predictedvalue = $prediction->get_prediction_data()->prediction;
107 // Only need to fill this data once.
108 if (!isset($predictionvalues[$predictedvalue])) {
110 $preddata['predictiondisplayvalue'] = $this->model->get_target()->get_display_value($predictedvalue);
111 list($preddata['style'], $preddata['outcomeicon']) =
112 insight::get_calculation_display($this->model->get_target(), floatval($predictedvalue), $output);
113 $predictionvalues[$predictedvalue] = $preddata;
116 $insightrenderable = new \report_insights\output\insight($prediction, $this->model, true);
117 $insights[$predictedvalue][] = $insightrenderable->export_for_template($output);
120 // Ok, now we have all the data we want, put it into a format that mustache can handle.
121 foreach ($predictionvalues as $key => $prediction) {
122 if (isset($insights[$key])) {
123 $prediction['insights'] = $insights[$key];
126 $data->predictions[] = $prediction;
130 if (empty($insights) && $this->page == 0) {
131 if ($this->model->any_prediction_obtained()) {
132 $data->noinsights = get_string('noinsights', 'analytics');
134 $data->noinsights = get_string('nopredictionsyet', 'analytics');
138 $data->noinsights = get_string('noinsights', 'analytics');
141 if (!empty($data->noinsights)) {
142 $notification = new \core\output\notification($data->noinsights);
143 $data->noinsights = $notification->export_for_template($output);
146 if ($this->othermodels) {
149 foreach ($this->othermodels as $model) {
150 $options[$model->get_id()] = $model->get_target()->get_name();
153 // New moodle_url instance returned by magic_get_url.
155 $url->remove_params('modelid');
156 $modelselector = new \single_select($url, 'modelid', $options, '',
157 array('' => get_string('selectotherinsights', 'report_insights')));
158 $data->modelselector = $modelselector->export_for_template($output);
161 $data->pagingbar = $output->render(new \paging_bar($total, $this->page, $this->perpage, $PAGE->url));