MDL-38509 Move the tool node in the navigation
[moodle.git] / admin / tool / installaddon / renderer.php
CommitLineData
0056f2a3
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Output rendering for the plugin.
20 *
21 * @package tool_installaddon
22 * @category output
23 * @copyright 2013 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Implements the plugin renderer
31 *
32 * @copyright 2013 David Mudrak <david@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class tool_installaddon_renderer extends plugin_renderer_base {
36
37 /** @var tool_installaddon_installer */
38 protected $installer = null;
39
cbd125e8
DM
40 /** @var tool_installaddon_validator */
41 protected $validator = null;
42
0056f2a3
DM
43 /**
44 * Sets the tool_installaddon_installer instance being used.
45 *
46 * @throws coding_exception if the installer has been already set
47 * @param tool_installaddon_installer $installer
48 */
49 public function set_installer_instance(tool_installaddon_installer $installer) {
50 if (is_null($this->installer)) {
51 $this->installer = $installer;
52 } else {
53 throw new coding_exception('Attempting to reset the installer instance.');
54 }
55 }
56
cbd125e8
DM
57 /**
58 * Sets the tool_installaddon_validator instance being used.
59 *
60 * @throws coding_exception if the validator has been already set
61 * @param tool_installaddon_validator $validator
62 */
63 public function set_validator_instance(tool_installaddon_validator $validator) {
64 if (is_null($this->validator)) {
65 $this->validator = $validator;
66 } else {
67 throw new coding_exception('Attempting to reset the validator instance.');
68 }
69 }
70
0056f2a3
DM
71 /**
72 * Defines the index page layout
73 *
74 * @return string
75 */
76 public function index_page() {
77
cbd125e8
DM
78 if (is_null($this->installer)) {
79 throw new coding_exception('Installer instance has not been set.');
80 }
81
ddab904b
DM
82 $permcheckurl = new moodle_url('/admin/tool/installaddon/permcheck.php');
83 $this->page->requires->yui_module('moodle-tool_installaddon-permcheck', 'M.tool_installaddon.permcheck.init',
84 array(array('permcheckurl' => $permcheckurl->out())));
85 $this->page->requires->strings_for_js(
86 array('permcheckprogress', 'permcheckresultno', 'permcheckresultyes', 'permcheckerror'), 'tool_installaddon');
87
0056f2a3
DM
88 $out = $this->output->header();
89 $out .= $this->index_page_heading();
90 $out .= $this->index_page_repository();
91 $out .= $this->index_page_upload();
92 $out .= $this->output->footer();
93
94 return $out;
95 }
96
cbd125e8
DM
97 /**
98 * Defines the validation results page layout
99 *
100 * @return string
101 */
102 public function validation_page() {
103
104 if (is_null($this->installer)) {
105 throw new coding_exception('Installer instance has not been set.');
106 }
107
108 if (is_null($this->validator)) {
109 throw new coding_exception('Validator instance has not been set.');
110 }
111
112 $out = $this->output->header();
113 $out .= $this->validation_page_heading();
114 $out .= $this->validation_page_messages();
115 $out .= $this->validation_page_continue();
116 $out .= $this->output->footer();
117
118 return $out;
119
120 }
121
122 // End of the external API /////////////////////////////////////////////////
123
0056f2a3
DM
124 /**
125 * Renders the index page heading
126 *
127 * @return string
128 */
129 protected function index_page_heading() {
130 return $this->output->heading(get_string('pluginname', 'tool_installaddon'));
131 }
132
133 /**
134 * Renders the widget for browsing the add-on repository
135 *
136 * @return string
137 */
138 protected function index_page_repository() {
139
140 $url = $this->installer->get_addons_repository_url();
141
142 $out = $this->box(
143 $this->output->single_button($url, get_string('installfromrepo', 'tool_installaddon'), 'get').
144 $this->output->help_icon('installfromrepo', 'tool_installaddon'),
145 'generalbox', 'installfromrepobox'
146 );
147
148 return $out;
149 }
150
151 /**
152 * Renders the widget for uploading the add-on ZIP package
153 *
154 * @return string
155 */
156 protected function index_page_upload() {
157
158 $form = $this->installer->get_installfromzip_form();
159
160 ob_start();
161 $form->display();
162 $out = ob_get_clean();
163
164 $out = $this->box($out, 'generalbox', 'installfromzipbox');
165
166 return $out;
167 }
cbd125e8
DM
168
169 /**
170 * Renders the page title and the overall validation verdict
171 *
172 * @return string
173 */
174 protected function validation_page_heading() {
175
176 $heading = $this->output->heading(get_string('validation', 'tool_installaddon'));
177
178 if ($this->validator->get_result()) {
179 $status = $this->output->container(
180 html_writer::span(get_string('validationresult1', 'tool_installaddon'), 'verdict').
181 $this->output->help_icon('validationresult1', 'tool_installaddon'),
182 array('validationresult', 'success')
183 );
184 } else {
185 $status = $this->output->container(
186 html_writer::span(get_string('validationresult0', 'tool_installaddon'), 'verdict').
187 $this->output->help_icon('validationresult0', 'tool_installaddon'),
188 array('validationresult', 'failure')
189 );
190 }
191
192 return $heading . $status;
193 }
194
195 /**
196 * Renders validation log messages.
197 *
198 * @return string
199 */
200 protected function validation_page_messages() {
201
202 $validator = $this->validator; // We need this to be able to use their constants.
203 $messages = $validator->get_messages();
204
205 if (empty($messages)) {
206 return '';
207 }
208
209 $table = new html_table();
210 $table->attributes['class'] = 'validationmessages generaltable';
211 $table->head = array(
212 get_string('validationresultstatus', 'tool_installaddon'),
213 get_string('validationresultmsg', 'tool_installaddon'),
214 get_string('validationresultinfo', 'tool_installaddon')
215 );
216 $table->colclasses = array('msgstatus', 'msgtext', 'msginfo');
217
218 $stringman = get_string_manager();
219
220 foreach ($messages as $message) {
221
222 if ($message->level === $validator::DEBUG and !debugging()) {
223 continue;
224 }
225
226 $msgstatus = get_string('validationmsglevel_'.$message->level, 'tool_installaddon');
227 $msgtext = $msgtext = s($message->msgcode);
228 if (is_null($message->addinfo)) {
229 $msginfo = '';
230 } else {
231 $msginfo = html_writer::tag('pre', s(print_r($message->addinfo, true)));
232 }
233 $msghelp = '';
234
235 // Replace the message code with the string if it is defined.
236 if ($stringman->string_exists('validationmsg_'.$message->msgcode, 'tool_installaddon')) {
237 $msgtext = get_string('validationmsg_'.$message->msgcode, 'tool_installaddon');
238 // And check for the eventual help, too.
239 if ($stringman->string_exists('validationmsg_'.$message->msgcode.'_help', 'tool_installaddon')) {
240 $msghelp = $this->output->help_icon('validationmsg_'.$message->msgcode, 'tool_installaddon');
241 }
242 }
243
244 // Re-format the message info using a string if it is define.
245 if (!is_null($message->addinfo) and $stringman->string_exists('validationmsg_'.$message->msgcode.'_info', 'tool_installaddon')) {
246 $msginfo = get_string('validationmsg_'.$message->msgcode.'_info', 'tool_installaddon', $message->addinfo);
247 }
248
249 $row = new html_table_row(array($msgstatus, $msgtext.$msghelp, $msginfo));
250 $row->attributes['class'] = 'level-'.$message->level.' '.$message->msgcode;
251
252 $table->data[] = $row;
253 }
254
255 return html_writer::table($table);
256 }
257
258 /**
259 * Renders widgets to continue from the validation results page
260 *
261 * @return string
262 */
263 protected function validation_page_continue() {
264
265 $conturl = $this->validator->get_continue_url();
266 if (is_null($conturl)) {
267 $contbutton = '';
268 } else {
269 $contbutton = $this->output->single_button(
270 $conturl, get_string('installaddon', 'tool_installaddon'), 'post',
271 array('class' => 'singlebutton continuebutton'));
272 }
273
274 $cancelbutton = $this->output->single_button(
275 new moodle_url('/admin/tool/installaddon/index.php'), get_string('cancel', 'core'), 'get',
276 array('class' => 'singlebutton cancelbutton'));
277
278 return $this->output->container($cancelbutton.$contbutton, 'postvalidationbuttons');
279 }
0056f2a3 280}