c4c9706fcc49613fbaa9144debda287db97a220c
[moodle.git] / admin / tool / installaddon / renderer.php
1 <?php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
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  */
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;
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     }
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     }
71     /**
72      * Defines the index page layout
73      *
74      * @return string
75      */
76     public function index_page() {
78         if (is_null($this->installer)) {
79             throw new coding_exception('Installer instance has not been set.');
80         }
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();
94         return $out;
95     }
97     /**
98      * Defines the validation results page layout
99      *
100      * @return string
101      */
102     public function validation_page() {
104         if (is_null($this->installer)) {
105             throw new coding_exception('Installer instance has not been set.');
106         }
108         if (is_null($this->validator)) {
109             throw new coding_exception('Validator instance has not been set.');
110         }
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();
118         return $out;
119     }
121     /**
122      * Inform the user about invalid remote installation request.
123      *
124      * @param moodle_url $continueurl
125      * @return string
126      */
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();
135         return $out;
136     }
138     /**
139      * Inform the user that such plugin is already installed
140      *
141      * @param stdClass $data decoded request data
142      * @param moodle_url $continueurl
143      * @return string
144      */
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();
153         return $out;
154     }
156     /**
157      * Let the user confirm the remote installation request.
158      *
159      * @param stdClass $data decoded request data
160      * @param moodle_url $continueurl
161      * @param moodle_url $cancelurl
162      * @return string
163      */
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();
171         return $out;
172     }
174     /**
175      * Inform the user that the target plugin type location is not writable.
176      *
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
181      * @return string
182      */
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();
192         return $out;
193     }
195     /**
196      * Inform the user about pluginfo service call exception
197      *
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.
201      *
202      * @param stdClass $data decoded request data
203      * @param tool_installaddon_pluginfo_exception $e thrown exception
204      * @param moodle_url $continueurl
205      * @return string
206      */
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();
215         return $out;
216     }
218     /**
219      * Inform the user about the installer exception
220      *
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.
224      *
225      * @param tool_installaddon_installer_exception $e thrown exception
226      * @param moodle_url $continueurl
227      * @return string
228      */
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();
237         return $out;
238     }
240     // End of the external API /////////////////////////////////////////////////
242     /**
243      * Renders the index page heading
244      *
245      * @return string
246      */
247     protected function index_page_heading() {
248         return $this->output->heading(get_string('pluginname', 'tool_installaddon'));
249     }
251     /**
252      * Renders the widget for browsing the add-on repository
253      *
254      * @return string
255      */
256     protected function index_page_repository() {
258         $url = $this->installer->get_addons_repository_url();
260         $out = $this->box(
261             $this->output->single_button($url, get_string('installfromrepo', 'tool_installaddon'), 'get').
262             $this->output->help_icon('installfromrepo', 'tool_installaddon'),
263             'generalbox', 'installfromrepobox'
264         );
266         return $out;
267     }
269     /**
270      * Renders the widget for uploading the add-on ZIP package
271      *
272      * @return string
273      */
274     protected function index_page_upload() {
276         $form = $this->installer->get_installfromzip_form();
278         ob_start();
279         $form->display();
280         $out = ob_get_clean();
282         $out = $this->box($out, 'generalbox', 'installfromzipbox');
284         return $out;
285     }
287     /**
288      * Renders the page title and the overall validation verdict
289      *
290      * @return string
291      */
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')
301             );
302         } else {
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')
307             );
308         }
310         return $heading . $status;
311     }
313     /**
314      * Renders validation log messages.
315      *
316      * @return string
317      */
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)) {
324             return '';
325         }
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')
333         );
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()) {
341                 continue;
342             }
344             $msgstatus = get_string('validationmsglevel_'.$message->level, 'tool_installaddon');
345             $msgtext = $msgtext = s($message->msgcode);
346             if (is_null($message->addinfo)) {
347                 $msginfo = '';
348             } else {
349                 $msginfo = html_writer::tag('pre', s(print_r($message->addinfo, true)));
350             }
351             $msghelp = '';
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');
359                 }
360             }
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);
365             }
367             $row = new html_table_row(array($msgstatus, $msgtext.$msghelp, $msginfo));
368             $row->attributes['class'] = 'level-'.$message->level.' '.$message->msgcode;
370             $table->data[] = $row;
371         }
373         return html_writer::table($table);
374     }
376     /**
377      * Renders widgets to continue from the validation results page
378      *
379      * @return string
380      */
381     protected function validation_page_continue() {
383         $conturl = $this->validator->get_continue_url();
384         if (is_null($conturl)) {
385             $contbutton = '';
386         } else {
387             $contbutton = $this->output->single_button(
388                 $conturl, get_string('installaddon', 'tool_installaddon'), 'post',
389                 array('class' => 'singlebutton continuebutton'));
390         }
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');
397     }