MDL-48493 admin: Move the acknowledgement to the plugin validation page
[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(
1f6a9391
DM
86 array('permcheckprogress', 'permcheckresultno', 'permcheckresultyes', 'permcheckerror', 'permcheckrepeat'),
87 'tool_installaddon');
ddab904b 88
0056f2a3
DM
89 $out = $this->output->header();
90 $out .= $this->index_page_heading();
91 $out .= $this->index_page_repository();
92 $out .= $this->index_page_upload();
93 $out .= $this->output->footer();
94
95 return $out;
96 }
97
cbd125e8
DM
98 /**
99 * Defines the validation results page layout
100 *
101 * @return string
102 */
103 public function validation_page() {
104
105 if (is_null($this->installer)) {
106 throw new coding_exception('Installer instance has not been set.');
107 }
108
109 if (is_null($this->validator)) {
110 throw new coding_exception('Validator instance has not been set.');
111 }
112
113 $out = $this->output->header();
114 $out .= $this->validation_page_heading();
115 $out .= $this->validation_page_messages();
116 $out .= $this->validation_page_continue();
117 $out .= $this->output->footer();
118
119 return $out;
af96f120
DM
120 }
121
122 /**
123 * Inform the user about invalid remote installation request.
124 *
125 * @param moodle_url $continueurl
126 * @return string
127 */
128 public function remote_request_invalid_page(moodle_url $continueurl) {
129
130 $out = $this->output->header();
131 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
132 $out .= $this->output->box(get_string('remoterequestinvalid', 'tool_installaddon'), 'generalbox', 'notice');
133 $out .= $this->output->continue_button($continueurl, 'get');
134 $out .= $this->output->footer();
135
136 return $out;
137 }
138
139 /**
140 * Inform the user that such plugin is already installed
141 *
142 * @param stdClass $data decoded request data
143 * @param moodle_url $continueurl
144 * @return string
145 */
146 public function remote_request_alreadyinstalled_page(stdClass $data, moodle_url $continueurl) {
147
148 $out = $this->output->header();
149 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
150 $out .= $this->output->box(get_string('remoterequestalreadyinstalled', 'tool_installaddon', $data), 'generalbox', 'notice');
151 $out .= $this->output->continue_button($continueurl, 'get');
152 $out .= $this->output->footer();
153
154 return $out;
155 }
156
157 /**
158 * Let the user confirm the remote installation request.
159 *
160 * @param stdClass $data decoded request data
161 * @param moodle_url $continueurl
162 * @param moodle_url $cancelurl
163 * @return string
164 */
165 public function remote_request_confirm_page(stdClass $data, moodle_url $continueurl, moodle_url $cancelurl) {
166
167 $out = $this->output->header();
168 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
169 $out .= $this->output->confirm(get_string('remoterequestconfirm', 'tool_installaddon', $data), $continueurl, $cancelurl);
170 $out .= $this->output->footer();
171
172 return $out;
173 }
cbd125e8 174
af96f120
DM
175 /**
176 * Inform the user that the target plugin type location is not writable.
177 *
178 * @param stdClass $data decoded request data
179 * @param string $plugintypepath full path to the plugin type location
180 * @param moodle_url $continueurl to repeat the write permission check
181 * @param moodle_url $cancelurl to cancel the installation
182 * @return string
183 */
184 public function remote_request_permcheck_page(stdClass $data, $plugintypepath, moodle_url $continueurl, moodle_url $cancelurl) {
185
186 $data->typepath = $plugintypepath;
187
188 $out = $this->output->header();
189 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
190 $out .= $this->output->confirm(get_string('remoterequestpermcheck', 'tool_installaddon', $data), $continueurl, $cancelurl);
191 $out .= $this->output->footer();
192
193 return $out;
194 }
195
196 /**
197 * Inform the user about pluginfo service call exception
198 *
199 * This implementation does not actually use the passed exception. Custom renderers might want to
200 * display additional data obtained via {@link get_exception_info()}. Also note, this method is called
201 * in non-debugging mode only. If debugging is allowed at the site, default exception handler is triggered.
202 *
203 * @param stdClass $data decoded request data
204 * @param tool_installaddon_pluginfo_exception $e thrown exception
205 * @param moodle_url $continueurl
206 * @return string
207 */
208 public function remote_request_pluginfo_exception(stdClass $data, tool_installaddon_pluginfo_exception $e, moodle_url $continueurl) {
209
210 $out = $this->output->header();
211 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
212 $out .= $this->output->box(get_string('remoterequestpluginfoexception', 'tool_installaddon', $data), 'generalbox', 'notice');
213 $out .= $this->output->continue_button($continueurl, 'get');
214 $out .= $this->output->footer();
215
216 return $out;
217 }
218
219 /**
220 * Inform the user about the installer exception
221 *
222 * This implementation does not actually use the passed exception. Custom renderers might want to
223 * display additional data obtained via {@link get_exception_info()}. Also note, this method is called
224 * in non-debugging mode only. If debugging is allowed at the site, default exception handler is triggered.
225 *
226 * @param tool_installaddon_installer_exception $e thrown exception
227 * @param moodle_url $continueurl
228 * @return string
229 */
230 public function installer_exception(tool_installaddon_installer_exception $e, moodle_url $continueurl) {
231
232 $out = $this->output->header();
233 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
234 $out .= $this->output->box(get_string('installexception', 'tool_installaddon'), 'generalbox', 'notice');
235 $out .= $this->output->continue_button($continueurl, 'get');
236 $out .= $this->output->footer();
237
238 return $out;
cbd125e8
DM
239 }
240
241 // End of the external API /////////////////////////////////////////////////
242
0056f2a3
DM
243 /**
244 * Renders the index page heading
245 *
246 * @return string
247 */
248 protected function index_page_heading() {
249 return $this->output->heading(get_string('pluginname', 'tool_installaddon'));
250 }
251
252 /**
253 * Renders the widget for browsing the add-on repository
254 *
255 * @return string
256 */
257 protected function index_page_repository() {
258
259 $url = $this->installer->get_addons_repository_url();
260
261 $out = $this->box(
262 $this->output->single_button($url, get_string('installfromrepo', 'tool_installaddon'), 'get').
263 $this->output->help_icon('installfromrepo', 'tool_installaddon'),
264 'generalbox', 'installfromrepobox'
265 );
266
267 return $out;
268 }
269
270 /**
271 * Renders the widget for uploading the add-on ZIP package
272 *
273 * @return string
274 */
275 protected function index_page_upload() {
276
277 $form = $this->installer->get_installfromzip_form();
278
279 ob_start();
280 $form->display();
281 $out = ob_get_clean();
282
283 $out = $this->box($out, 'generalbox', 'installfromzipbox');
284
285 return $out;
286 }
cbd125e8
DM
287
288 /**
289 * Renders the page title and the overall validation verdict
290 *
291 * @return string
292 */
293 protected function validation_page_heading() {
294
295 $heading = $this->output->heading(get_string('validation', 'tool_installaddon'));
296
297 if ($this->validator->get_result()) {
298 $status = $this->output->container(
299 html_writer::span(get_string('validationresult1', 'tool_installaddon'), 'verdict').
300 $this->output->help_icon('validationresult1', 'tool_installaddon'),
301 array('validationresult', 'success')
302 );
303 } else {
304 $status = $this->output->container(
305 html_writer::span(get_string('validationresult0', 'tool_installaddon'), 'verdict').
306 $this->output->help_icon('validationresult0', 'tool_installaddon'),
307 array('validationresult', 'failure')
308 );
309 }
310
311 return $heading . $status;
312 }
313
314 /**
315 * Renders validation log messages.
316 *
317 * @return string
318 */
319 protected function validation_page_messages() {
320
321 $validator = $this->validator; // We need this to be able to use their constants.
322 $messages = $validator->get_messages();
323
324 if (empty($messages)) {
325 return '';
326 }
327
328 $table = new html_table();
329 $table->attributes['class'] = 'validationmessages generaltable';
330 $table->head = array(
331 get_string('validationresultstatus', 'tool_installaddon'),
332 get_string('validationresultmsg', 'tool_installaddon'),
333 get_string('validationresultinfo', 'tool_installaddon')
334 );
335 $table->colclasses = array('msgstatus', 'msgtext', 'msginfo');
336
337 $stringman = get_string_manager();
338
339 foreach ($messages as $message) {
340
341 if ($message->level === $validator::DEBUG and !debugging()) {
342 continue;
343 }
344
345 $msgstatus = get_string('validationmsglevel_'.$message->level, 'tool_installaddon');
346 $msgtext = $msgtext = s($message->msgcode);
347 if (is_null($message->addinfo)) {
348 $msginfo = '';
349 } else {
350 $msginfo = html_writer::tag('pre', s(print_r($message->addinfo, true)));
351 }
352 $msghelp = '';
353
354 // Replace the message code with the string if it is defined.
355 if ($stringman->string_exists('validationmsg_'.$message->msgcode, 'tool_installaddon')) {
356 $msgtext = get_string('validationmsg_'.$message->msgcode, 'tool_installaddon');
357 // And check for the eventual help, too.
358 if ($stringman->string_exists('validationmsg_'.$message->msgcode.'_help', 'tool_installaddon')) {
359 $msghelp = $this->output->help_icon('validationmsg_'.$message->msgcode, 'tool_installaddon');
360 }
361 }
362
363 // Re-format the message info using a string if it is define.
364 if (!is_null($message->addinfo) and $stringman->string_exists('validationmsg_'.$message->msgcode.'_info', 'tool_installaddon')) {
365 $msginfo = get_string('validationmsg_'.$message->msgcode.'_info', 'tool_installaddon', $message->addinfo);
366 }
367
368 $row = new html_table_row(array($msgstatus, $msgtext.$msghelp, $msginfo));
369 $row->attributes['class'] = 'level-'.$message->level.' '.$message->msgcode;
370
371 $table->data[] = $row;
372 }
373
374 return html_writer::table($table);
375 }
376
377 /**
378 * Renders widgets to continue from the validation results page
379 *
380 * @return string
381 */
382 protected function validation_page_continue() {
383
edb13e3c 384 $output = '';
cbd125e8 385 $conturl = $this->validator->get_continue_url();
edb13e3c 386
cbd125e8
DM
387 if (is_null($conturl)) {
388 $contbutton = '';
edb13e3c 389
cbd125e8
DM
390 } else {
391 $contbutton = $this->output->single_button(
392 $conturl, get_string('installaddon', 'tool_installaddon'), 'post',
393 array('class' => 'singlebutton continuebutton'));
edb13e3c
DM
394 $output .= $this->output->heading(get_string('acknowledgement', 'tool_installaddon'), 3);
395 $output .= $this->output->container(get_string('acknowledgementtext', 'tool_installaddon'));
cbd125e8
DM
396 }
397
398 $cancelbutton = $this->output->single_button(
399 new moodle_url('/admin/tool/installaddon/index.php'), get_string('cancel', 'core'), 'get',
400 array('class' => 'singlebutton cancelbutton'));
401
edb13e3c
DM
402 $output .= $this->output->container($cancelbutton.$contbutton, 'postvalidationbuttons');
403
404 return $output;
cbd125e8 405 }
0056f2a3 406}