MDL-58835 report_insights: Fix redirections
[moodle.git] / admin / tool / analytics / classes / output / renderer.php
CommitLineData
efaf8534
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/**
18 * Renderer.
19 *
e10b29ed 20 * @package tool_analytics
efaf8534
DM
21 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
e10b29ed 25namespace tool_analytics\output;
efaf8534
DM
26
27defined('MOODLE_INTERNAL') || die();
28
29use plugin_renderer_base;
30use templatable;
31use renderable;
32
33/**
34 * Renderer class.
35 *
e10b29ed 36 * @package tool_analytics
efaf8534
DM
37 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class renderer extends plugin_renderer_base {
41
42 /**
43 * Defer to template.
44 *
e10b29ed 45 * @param \tool_analytics\output\models_list $modelslist
efaf8534
DM
46 * @return string HTML
47 */
e10b29ed 48 protected function render_models_list(\tool_analytics\output\models_list $modelslist) {
3a396286 49 $data = $modelslist->export_for_template($this);
e10b29ed 50 return parent::render_from_template('tool_analytics/models_list', $data);
efaf8534
DM
51 }
52
53 /**
54 * Renders a table.
55 *
56 * @param \table_sql $table
57 * @return string HTML
58 */
59 public function render_table(\table_sql $table) {
60
61 ob_start();
62 $table->out(10, true);
63 $output = ob_get_contents();
64 ob_end_clean();
65
66 return $output;
67 }
68
69 /**
70 * Web interface evaluate results.
71 *
72 * @param \stdClass[] $results
357507b4 73 * @param string[] $logs
efaf8534
DM
74 * @return string HTML
75 */
357507b4 76 public function render_evaluate_results($results, $logs = array()) {
efaf8534
DM
77 global $OUTPUT;
78
79 $output = '';
80
81 foreach ($results as $timesplittingid => $result) {
82
83 if (!CLI_SCRIPT) {
84 $output .= $OUTPUT->box_start('generalbox m-b-3');
85 }
86
87 // Check that the array key is a string, not all results depend on time splitting methods (e.g. general errors).
88 if (!is_numeric($timesplittingid)) {
89 $timesplitting = \core_analytics\manager::get_time_splitting($timesplittingid);
90 $langstrdata = (object)array('name' => $timesplitting->get_name(), 'id' => $timesplittingid);
91
92 if (CLI_SCRIPT) {
e10b29ed 93 $output .= $OUTPUT->heading(get_string('getpredictionsresultscli', 'tool_analytics', $langstrdata), 3);
efaf8534 94 } else {
e10b29ed 95 $output .= $OUTPUT->heading(get_string('getpredictionsresults', 'tool_analytics', $langstrdata), 3);
efaf8534
DM
96 }
97 }
98
99 if ($result->status == 0) {
e10b29ed 100 $output .= $OUTPUT->notification(get_string('goodmodel', 'tool_analytics'),
efaf8534
DM
101 \core\output\notification::NOTIFY_SUCCESS);
102 } else if ($result->status === \core_analytics\model::NO_DATASET) {
e10b29ed 103 $output .= $OUTPUT->notification(get_string('nodatatoevaluate', 'tool_analytics'),
efaf8534
DM
104 \core\output\notification::NOTIFY_WARNING);
105 }
106
107 if (isset($result->score)) {
108 // Score.
e10b29ed 109 $output .= $OUTPUT->heading(get_string('accuracy', 'tool_analytics') . ': ' .
413f19bc 110 round(floatval($result->score), 4) * 100 . '%', 4);
efaf8534
DM
111 }
112
113 if (!empty($result->info)) {
114 foreach ($result->info as $message) {
115 $output .= $OUTPUT->notification($message, \core\output\notification::NOTIFY_WARNING);
116 }
117 }
118
119 if (!CLI_SCRIPT) {
120 $output .= $OUTPUT->box_end();
121 }
122 }
123
357507b4
DM
124 // Info logged during evaluation.
125 if (!empty($logs) && debugging()) {
e10b29ed 126 $output .= $OUTPUT->heading(get_string('extrainfo', 'tool_analytics'), 3);
357507b4 127 foreach ($logs as $log) {
efaf8534
DM
128 $output .= $OUTPUT->notification($log, \core\output\notification::NOTIFY_WARNING);
129 }
130 }
131
132 if (!CLI_SCRIPT) {
a6edbef5 133 $output .= $OUTPUT->single_button(new \moodle_url('/admin/tool/analytics/index.php'), get_string('continue'), 'get');
efaf8534
DM
134 }
135
136 return $output;
137 }
138
139
140 /**
357507b4 141 * Web interface training & prediction results.
efaf8534 142 *
1cc2b4ba 143 * @param \stdClass|false $trainresults
efaf8534 144 * @param string[] $trainlogs
1cc2b4ba 145 * @param \stdClass|false $predictresults
efaf8534
DM
146 * @param string[] $predictlogs
147 * @return string HTML
148 */
1cc2b4ba 149 public function render_get_predictions_results($trainresults = false, $trainlogs = array(), $predictresults = false, $predictlogs = array()) {
efaf8534
DM
150 global $OUTPUT;
151
152 $output = '';
153
154 if ($trainresults || (!empty($trainlogs) && debugging())) {
e10b29ed 155 $output .= $OUTPUT->heading(get_string('trainingresults', 'tool_analytics'), 3);
efaf8534
DM
156 }
157
158 if ($trainresults) {
159 if ($trainresults->status == 0) {
e10b29ed 160 $output .= $OUTPUT->notification(get_string('trainingprocessfinished', 'tool_analytics'),
efaf8534
DM
161 \core\output\notification::NOTIFY_SUCCESS);
162 } else if ($trainresults->status === \core_analytics\model::NO_DATASET) {
e10b29ed 163 $output .= $OUTPUT->notification(get_string('nodatatotrain', 'tool_analytics'),
efaf8534
DM
164 \core\output\notification::NOTIFY_WARNING);
165 } else {
1cc2b4ba 166 $output .= $OUTPUT->notification(get_string('generalerror', 'analytics', $trainresults->status),
efaf8534
DM
167 \core\output\notification::NOTIFY_ERROR);
168 }
169 }
170
171 if (!empty($trainlogs) && debugging()) {
e10b29ed 172 $output .= $OUTPUT->heading(get_string('extrainfo', 'tool_analytics'), 4);
efaf8534
DM
173 foreach ($trainlogs as $log) {
174 $output .= $OUTPUT->notification($log, \core\output\notification::NOTIFY_WARNING);
175 }
176 }
177
178 if ($predictresults || (!empty($predictlogs) && debugging())) {
e10b29ed 179 $output .= $OUTPUT->heading(get_string('predictionresults', 'tool_analytics'), 3, 'main m-t-3');
efaf8534
DM
180 }
181
182 if ($predictresults) {
183 if ($predictresults->status == 0) {
e10b29ed 184 $output .= $OUTPUT->notification(get_string('predictionprocessfinished', 'tool_analytics'),
efaf8534
DM
185 \core\output\notification::NOTIFY_SUCCESS);
186 } else if ($predictresults->status === \core_analytics\model::NO_DATASET) {
e10b29ed 187 $output .= $OUTPUT->notification(get_string('nodatatopredict', 'tool_analytics'),
efaf8534
DM
188 \core\output\notification::NOTIFY_WARNING);
189 } else {
1cc2b4ba 190 $output .= $OUTPUT->notification(get_string('generalerror', 'analytics', $predictresults->status),
efaf8534
DM
191 \core\output\notification::NOTIFY_ERROR);
192 }
193 }
194
195 if (!empty($predictlogs) && debugging()) {
e10b29ed 196 $output .= $OUTPUT->heading(get_string('extrainfo', 'tool_analytics'), 4);
efaf8534
DM
197 foreach ($predictlogs as $log) {
198 $output .= $OUTPUT->notification($log, \core\output\notification::NOTIFY_WARNING);
199 }
200 }
201
202 if (!CLI_SCRIPT) {
a6edbef5 203 $output .= $OUTPUT->single_button(new \moodle_url('/admin/tool/analytics/index.php'), get_string('continue'), 'get');
efaf8534
DM
204 }
205
206 return $output;
207 }
208}