MDL-59265 report_insights: Fix PHP notice when no predictions
[moodle.git] / report / insights / classes / output / insights_list.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 * Insights list 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 * Shows report_insights insights list.
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 insights_list implements \renderable, \templatable {
373b1de1
DM
37
38 /**
39 * @var \core_analytics\model
40 */
41 protected $model;
42
43 /**
44 * @var \context
45 */
46 protected $context;
47
48 /**
413f19bc 49 * @var \core_analytics\model[]
373b1de1
DM
50 */
51 protected $othermodels;
52
68bfe1de
DW
53 /**
54 * @var int
55 */
56 protected $page;
57
58 /**
59 * @var int
60 */
61 protected $perpage;
62
413f19bc
DM
63 /**
64 * Constructor
65 *
66 * @param \core_analytics\model $model
67 * @param \context $context
68 * @param \core_analytics\model[] $othermodels
68bfe1de
DW
69 * @param int $page
70 * @param int $perpage The max number of results to fetch
413f19bc
DM
71 * @return void
72 */
68bfe1de 73 public function __construct(\core_analytics\model $model, \context $context, $othermodels, $page = 0, $perpage = 100) {
373b1de1
DM
74 $this->model = $model;
75 $this->context = $context;
76 $this->othermodels = $othermodels;
68bfe1de
DW
77 $this->page = $page;
78 $this->perpage = $perpage;
373b1de1
DM
79 }
80
81 /**
82 * Exports the data.
83 *
84 * @param \renderer_base $output
85 * @return \stdClass
86 */
87 public function export_for_template(\renderer_base $output) {
88 global $PAGE;
89
90 $data = new \stdClass();
68bfe1de 91 $total = 0;
373b1de1 92
f9e7447f 93 if ($this->model->uses_insights()) {
3592b64a 94 $predictionsdata = $this->model->get_predictions($this->context, $this->page, $this->perpage);
373b1de1 95
f9e7447f 96 $data->insights = array();
3592b64a
DM
97 if ($predictionsdata) {
98 list($total, $predictions) = $predictionsdata;
99
100 foreach ($predictions as $prediction) {
101 $insightrenderable = new \report_insights\output\insight($prediction, $this->model, true);
102 $data->insights[] = $insightrenderable->export_for_template($output);
103 }
f9e7447f
DM
104 }
105
68bfe1de 106 if (empty($data->insights) && $this->page == 0) {
f9e7447f
DM
107 if ($this->model->any_prediction_obtained()) {
108 $data->noinsights = get_string('noinsights', 'analytics');
109 } else {
110 $data->noinsights = get_string('nopredictionsyet', 'analytics');
111 }
112 }
113 } else {
114 $data->noinsights = get_string('noinsights', 'analytics');
373b1de1
DM
115 }
116
f9e7447f
DM
117 if (!empty($data->noinsights)) {
118 $notification = new \core\output\notification($data->noinsights);
119 $data->noinsights = $notification->export_for_template($output);
373b1de1
DM
120 }
121
122 if ($this->othermodels) {
123
124 $options = array();
125 foreach ($this->othermodels as $model) {
126 $options[$model->get_id()] = $model->get_target()->get_name();
127 }
128
129 // New moodle_url instance returned by magic_get_url.
130 $url = $PAGE->url;
131 $url->remove_params('modelid');
132 $modelselector = new \single_select($url, 'modelid', $options, '',
133 array('' => get_string('selectotherinsights', 'report_insights')));
134 $data->modelselector = $modelselector->export_for_template($output);
135 }
136
68bfe1de
DW
137 $data->pagingbar = $output->render(new \paging_bar($total, $this->page, $this->perpage, $PAGE->url));
138
373b1de1
DM
139 return $data;
140 }
141}