3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Output rendering for the plugin.
21 * @package tool_installaddon
23 * @copyright 2013 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 * Implements the plugin renderer
32 * @copyright 2013 David Mudrak <david@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class tool_installaddon_renderer extends plugin_renderer_base {
37 /** @var tool_installaddon_installer */
38 protected $installer = null;
40 /** @var tool_installaddon_validator */
41 protected $validator = null;
44 * Sets the tool_installaddon_installer instance being used.
46 * @throws coding_exception if the installer has been already set
47 * @param tool_installaddon_installer $installer
49 public function set_installer_instance(tool_installaddon_installer $installer) {
50 if (is_null($this->installer)) {
51 $this->installer = $installer;
53 throw new coding_exception('Attempting to reset the installer instance.');
58 * Sets the tool_installaddon_validator instance being used.
60 * @throws coding_exception if the validator has been already set
61 * @param tool_installaddon_validator $validator
63 public function set_validator_instance(tool_installaddon_validator $validator) {
64 if (is_null($this->validator)) {
65 $this->validator = $validator;
67 throw new coding_exception('Attempting to reset the validator instance.');
72 * Defines the index page layout
76 public function index_page() {
78 if (is_null($this->installer)) {
79 throw new coding_exception('Installer instance has not been set.');
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');
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();
98 * Defines the validation results page layout
102 public function validation_page() {
104 if (is_null($this->installer)) {
105 throw new coding_exception('Installer instance has not been set.');
108 if (is_null($this->validator)) {
109 throw new coding_exception('Validator instance has not been set.');
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();
122 * Inform the user about invalid remote installation request.
124 * @param moodle_url $continueurl
127 public function remote_request_invalid_page(moodle_url $continueurl) {
129 $out = $this->output->header();
130 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
131 $out .= $this->output->box(get_string('remoterequestinvalid', 'tool_installaddon'), 'generalbox', 'notice');
132 $out .= $this->output->continue_button($continueurl, 'get');
133 $out .= $this->output->footer();
139 * Inform the user that such plugin is already installed
141 * @param stdClass $data decoded request data
142 * @param moodle_url $continueurl
145 public function remote_request_alreadyinstalled_page(stdClass $data, moodle_url $continueurl) {
147 $out = $this->output->header();
148 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
149 $out .= $this->output->box(get_string('remoterequestalreadyinstalled', 'tool_installaddon', $data), 'generalbox', 'notice');
150 $out .= $this->output->continue_button($continueurl, 'get');
151 $out .= $this->output->footer();
157 * Let the user confirm the remote installation request.
159 * @param stdClass $data decoded request data
160 * @param moodle_url $continueurl
161 * @param moodle_url $cancelurl
164 public function remote_request_confirm_page(stdClass $data, moodle_url $continueurl, moodle_url $cancelurl) {
166 $out = $this->output->header();
167 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
168 $out .= $this->output->confirm(get_string('remoterequestconfirm', 'tool_installaddon', $data), $continueurl, $cancelurl);
169 $out .= $this->output->footer();
175 * Inform the user that the target plugin type location is not writable.
177 * @param stdClass $data decoded request data
178 * @param string $plugintypepath full path to the plugin type location
179 * @param moodle_url $continueurl to repeat the write permission check
180 * @param moodle_url $cancelurl to cancel the installation
183 public function remote_request_permcheck_page(stdClass $data, $plugintypepath, moodle_url $continueurl, moodle_url $cancelurl) {
185 $data->typepath = $plugintypepath;
187 $out = $this->output->header();
188 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
189 $out .= $this->output->confirm(get_string('remoterequestpermcheck', 'tool_installaddon', $data), $continueurl, $cancelurl);
190 $out .= $this->output->footer();
196 * Inform the user about pluginfo service call exception
198 * This implementation does not actually use the passed exception. Custom renderers might want to
199 * display additional data obtained via {@link get_exception_info()}. Also note, this method is called
200 * in non-debugging mode only. If debugging is allowed at the site, default exception handler is triggered.
202 * @param stdClass $data decoded request data
203 * @param tool_installaddon_pluginfo_exception $e thrown exception
204 * @param moodle_url $continueurl
207 public function remote_request_pluginfo_exception(stdClass $data, tool_installaddon_pluginfo_exception $e, moodle_url $continueurl) {
209 $out = $this->output->header();
210 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
211 $out .= $this->output->box(get_string('remoterequestpluginfoexception', 'tool_installaddon', $data), 'generalbox', 'notice');
212 $out .= $this->output->continue_button($continueurl, 'get');
213 $out .= $this->output->footer();
219 * Inform the user about the installer exception
221 * This implementation does not actually use the passed exception. Custom renderers might want to
222 * display additional data obtained via {@link get_exception_info()}. Also note, this method is called
223 * in non-debugging mode only. If debugging is allowed at the site, default exception handler is triggered.
225 * @param tool_installaddon_installer_exception $e thrown exception
226 * @param moodle_url $continueurl
229 public function installer_exception(tool_installaddon_installer_exception $e, moodle_url $continueurl) {
231 $out = $this->output->header();
232 $out .= $this->output->heading(get_string('installfromrepo', 'tool_installaddon'));
233 $out .= $this->output->box(get_string('installexception', 'tool_installaddon'), 'generalbox', 'notice');
234 $out .= $this->output->continue_button($continueurl, 'get');
235 $out .= $this->output->footer();
240 // End of the external API /////////////////////////////////////////////////
243 * Renders the index page heading
247 protected function index_page_heading() {
248 return $this->output->heading(get_string('pluginname', 'tool_installaddon'));
252 * Renders the widget for browsing the add-on repository
256 protected function index_page_repository() {
258 $url = $this->installer->get_addons_repository_url();
261 $this->output->single_button($url, get_string('installfromrepo', 'tool_installaddon'), 'get').
262 $this->output->help_icon('installfromrepo', 'tool_installaddon'),
263 'generalbox', 'installfromrepobox'
270 * Renders the widget for uploading the add-on ZIP package
274 protected function index_page_upload() {
276 $form = $this->installer->get_installfromzip_form();
280 $out = ob_get_clean();
282 $out = $this->box($out, 'generalbox', 'installfromzipbox');
288 * Renders the page title and the overall validation verdict
292 protected function validation_page_heading() {
294 $heading = $this->output->heading(get_string('validation', 'tool_installaddon'));
296 if ($this->validator->get_result()) {
297 $status = $this->output->container(
298 html_writer::span(get_string('validationresult1', 'tool_installaddon'), 'verdict').
299 $this->output->help_icon('validationresult1', 'tool_installaddon'),
300 array('validationresult', 'success')
303 $status = $this->output->container(
304 html_writer::span(get_string('validationresult0', 'tool_installaddon'), 'verdict').
305 $this->output->help_icon('validationresult0', 'tool_installaddon'),
306 array('validationresult', 'failure')
310 return $heading . $status;
314 * Renders validation log messages.
318 protected function validation_page_messages() {
320 $validator = $this->validator; // We need this to be able to use their constants.
321 $messages = $validator->get_messages();
323 if (empty($messages)) {
327 $table = new html_table();
328 $table->attributes['class'] = 'validationmessages generaltable';
329 $table->head = array(
330 get_string('validationresultstatus', 'tool_installaddon'),
331 get_string('validationresultmsg', 'tool_installaddon'),
332 get_string('validationresultinfo', 'tool_installaddon')
334 $table->colclasses = array('msgstatus', 'msgtext', 'msginfo');
336 $stringman = get_string_manager();
338 foreach ($messages as $message) {
340 if ($message->level === $validator::DEBUG and !debugging()) {
344 $msgstatus = get_string('validationmsglevel_'.$message->level, 'tool_installaddon');
345 $msgtext = $msgtext = s($message->msgcode);
346 if (is_null($message->addinfo)) {
349 $msginfo = html_writer::tag('pre', s(print_r($message->addinfo, true)));
353 // Replace the message code with the string if it is defined.
354 if ($stringman->string_exists('validationmsg_'.$message->msgcode, 'tool_installaddon')) {
355 $msgtext = get_string('validationmsg_'.$message->msgcode, 'tool_installaddon');
356 // And check for the eventual help, too.
357 if ($stringman->string_exists('validationmsg_'.$message->msgcode.'_help', 'tool_installaddon')) {
358 $msghelp = $this->output->help_icon('validationmsg_'.$message->msgcode, 'tool_installaddon');
362 // Re-format the message info using a string if it is define.
363 if (!is_null($message->addinfo) and $stringman->string_exists('validationmsg_'.$message->msgcode.'_info', 'tool_installaddon')) {
364 $msginfo = get_string('validationmsg_'.$message->msgcode.'_info', 'tool_installaddon', $message->addinfo);
367 $row = new html_table_row(array($msgstatus, $msgtext.$msghelp, $msginfo));
368 $row->attributes['class'] = 'level-'.$message->level.' '.$message->msgcode;
370 $table->data[] = $row;
373 return html_writer::table($table);
377 * Renders widgets to continue from the validation results page
381 protected function validation_page_continue() {
383 $conturl = $this->validator->get_continue_url();
384 if (is_null($conturl)) {
387 $contbutton = $this->output->single_button(
388 $conturl, get_string('installaddon', 'tool_installaddon'), 'post',
389 array('class' => 'singlebutton continuebutton'));
392 $cancelbutton = $this->output->single_button(
393 new moodle_url('/admin/tool/installaddon/index.php'), get_string('cancel', 'core'), 'get',
394 array('class' => 'singlebutton cancelbutton'));
396 return $this->output->container($cancelbutton.$contbutton, 'postvalidationbuttons');