From 3adf4b183758dbb07255fe8f03059e4f5b44da44 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Fri, 2 Dec 2016 16:59:09 +0800 Subject: [PATCH] MDL-57149 admin: Make langimport page Bootstrap-compatible * Create template for the language import page. * Apply Bootstrap classes to form elements. * Use core notifications for displaying error/success messages. --- .../classes/output/langimport_page.php | 110 ++++++++++++++++ .../langimport/classes/output/renderer.php | 50 ++++++++ admin/tool/langimport/index.php | 66 ++-------- admin/tool/langimport/styles.css | 8 +- .../langimport/templates/langimport.mustache | 117 ++++++++++++++++++ 5 files changed, 292 insertions(+), 59 deletions(-) create mode 100644 admin/tool/langimport/classes/output/langimport_page.php create mode 100644 admin/tool/langimport/classes/output/renderer.php create mode 100644 admin/tool/langimport/templates/langimport.mustache diff --git a/admin/tool/langimport/classes/output/langimport_page.php b/admin/tool/langimport/classes/output/langimport_page.php new file mode 100644 index 00000000000..97ff74361dc --- /dev/null +++ b/admin/tool/langimport/classes/output/langimport_page.php @@ -0,0 +1,110 @@ +. + +/** + * Language import page. + * + * @package tool_langimport + * @copyright 2016 Jun Pataleta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace tool_langimport\output; +defined('MOODLE_INTERNAL') || die(); + +use moodle_url; +use renderable; +use renderer_base; +use stdClass; +use templatable; + +/** + * Language import page class. + * + * @package tool_langimport + * @copyright 2016 Jun Pataleta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class langimport_page implements renderable, templatable { + + /** @var array Array of currently installed languages. */ + protected $installedlanguages; + + /** @var array Array of languages that can be installed. */ + protected $availablelanguages; + + /** @var moodle_url The URL to be used for uninstalling the selected existing language packs. */ + protected $uninstallurl; + + /** @var moodle_url The URL to be used for updating the installed language packs. */ + protected $updateurl; + + /** @var moodle_url The URL to be used for installing the selected language packs to be installed. */ + protected $installurl; + + + /** + * langimport_page constructor. + * + * @param array $installedlanguages Array of currently installed languages. + * @param array $availablelanguages Array of languages that can be installed. + * @param moodle_url $uninstallurl The URL to be used for uninstalling the selected existing language packs. + * @param moodle_url $updateurl The URL to be used for updating the installed language packs. + * @param moodle_url $installurl The URL to be used for installing the selected language packs to be installed. + */ + public function __construct($installedlanguages, $availablelanguages, $uninstallurl, $updateurl, $installurl) { + $this->installedlanguages = $installedlanguages; + $this->availablelanguages = $availablelanguages; + $this->uninstallurl = $uninstallurl; + $this->updateurl = $updateurl; + $this->installurl = $installurl; + } + + /** + * Export the data. + * + * @param renderer_base $output + * @return stdClass + */ + public function export_for_template(renderer_base $output) { + $data = new stdClass(); + $data->uninstallurl = $this->uninstallurl; + $data->sesskey = sesskey(); + + $data->installedoptions = []; + foreach ($this->installedlanguages as $code => $language) { + $option = new stdClass(); + $option->value = $code; + $option->text = $language; + $data->installedoptions[] = $option; + } + + $data->updateurl = $this->updateurl; + + if (!empty($this->availablelanguages)) { + $data->toinstalloptions = []; + foreach ($this->availablelanguages as $code => $language) { + $option = new stdClass(); + $option->value = $code; + $option->text = $language; + $data->toinstalloptions[] = $option; + } + $data->installurl = $this->installurl; + $data->caninstall = true; + } + + return $data; + } +} diff --git a/admin/tool/langimport/classes/output/renderer.php b/admin/tool/langimport/classes/output/renderer.php new file mode 100644 index 00000000000..f74b11ecd21 --- /dev/null +++ b/admin/tool/langimport/classes/output/renderer.php @@ -0,0 +1,50 @@ +. + +/** + * Renderers. + * + * @package tool_langimport + * @copyright 2016 Jun Pataleta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_langimport\output; + +defined('MOODLE_INTERNAL') || die(); + +use plugin_renderer_base; + +/** + * Renderer class. + * + * @package tool_langimport + * @copyright 2016 Jun Pataleta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class renderer extends plugin_renderer_base { + + /** + * Defer to template. + * + * @param langimport_page $page + * @return string + */ + public function render_langimport_page(langimport_page $page) { + $data = $page->export_for_template($this); + return parent::render_from_template('tool_langimport/langimport', $data); + } +} diff --git a/admin/tool/langimport/index.php b/admin/tool/langimport/index.php index c5f49dafff3..0e98a5d2de4 100644 --- a/admin/tool/langimport/index.php +++ b/admin/tool/langimport/index.php @@ -124,23 +124,22 @@ if ($availablelangs = $controller->availablelangs) { } else { $remote = false; $availablelangs = array(); - echo $OUTPUT->box_start(); $a = [ 'src' => $controller->lang_pack_url(), 'dest' => $CFG->dataroot.'/lang/', ]; - print_string('downloadnotavailable', 'tool_langimport', $a); - echo $OUTPUT->box_end(); + $errormessage = get_string('downloadnotavailable', 'tool_langimport', $a); + \core\notification::error($errormessage); } if ($controller->info) { $info = implode('
', $controller->info); - echo $OUTPUT->notification($info, 'notifysuccess'); + \core\notification::success($info); } if ($controller->errors) { $info = implode('
', $controller->errors); - echo $OUTPUT->notification($info, 'notifyproblem'); + \core\notification::error($info); } if ($missingparents) { @@ -155,67 +154,29 @@ if ($missingparents) { } } $info = get_string('missinglangparent', 'tool_langimport', $a); - echo $OUTPUT->notification($info, 'notifyproblem'); + \core\notification::error($info); } } -echo $OUTPUT->box_start(); - -echo html_writer::start_tag('table'); -echo html_writer::start_tag('tr'); - -// list of installed languages -$url = new moodle_url('/admin/tool/langimport/index.php', array('mode' => DELETION_OF_SELECTED_LANG)); -echo html_writer::start_tag('td', array('valign' => 'top')); -echo html_writer::start_tag('form', array('id' => 'uninstallform', 'action' => $url->out(), 'method' => 'post')); -echo html_writer::start_tag('fieldset'); -echo html_writer::label(get_string('installedlangs', 'tool_langimport'), 'menuuninstalllang'); -echo html_writer::empty_tag('br'); -echo html_writer::select($installedlangs, 'uninstalllang[]', '', false, array('size' => 15, 'multiple' => 'multiple')); -echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())); -echo html_writer::empty_tag('br'); -echo html_writer::empty_tag('input', array('id' => 'languninstallbutton', - 'type' => 'submit', - 'value' => get_string('uninstall', 'tool_langimport')) - ); -echo html_writer::end_tag('fieldset'); -echo html_writer::end_tag('form'); +$uninstallurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => DELETION_OF_SELECTED_LANG)); +$updateurl = null; if ($remote) { - $url = new moodle_url('/admin/tool/langimport/index.php', array('mode' => UPDATE_ALL_LANG)); - echo html_writer::start_tag('form', array('id' => 'updateform', 'action' => $url->out(), 'method' => 'post')); - echo html_writer::tag('fieldset', html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('updatelangs','tool_langimport')))); - echo html_writer::end_tag('form'); + $updateurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => UPDATE_ALL_LANG)); } -echo html_writer::end_tag('td'); +$installurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => INSTALLATION_OF_SELECTED_LANG)); -// list of available languages +// List of available languages. $options = array(); foreach ($availablelangs as $alang) { if (!empty($alang[0]) and trim($alang[0]) !== 'en' and !$controller->is_installed_lang($alang[0], $alang[1])) { $options[$alang[0]] = $alang[2].' ‎('.$alang[0].')‎'; } } -if (!empty($options)) { - echo html_writer::start_tag('td', array('valign' => 'top')); - $url = new moodle_url('/admin/tool/langimport/index.php', array('mode' => INSTALLATION_OF_SELECTED_LANG)); - echo html_writer::start_tag('form', array('id' => 'installform', 'action' => $url->out(), 'method' => 'post')); - echo html_writer::start_tag('fieldset'); - echo html_writer::label(get_string('availablelangs','install'), 'menupack'); - echo html_writer::empty_tag('br'); - echo html_writer::select($options, 'pack[]', '', false, array('size' => 15, 'multiple' => 'multiple')); - echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())); - echo html_writer::empty_tag('br'); - echo html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('install','tool_langimport'))); - echo html_writer::end_tag('fieldset'); - echo html_writer::end_tag('form'); - echo html_writer::end_tag('td'); -} -echo html_writer::end_tag('tr'); -echo html_writer::end_tag('table'); -echo $OUTPUT->box_end(); +$renderable = new \tool_langimport\output\langimport_page($installedlangs, $options, $uninstallurl, $updateurl, $installurl); +$output = $PAGE->get_renderer('tool_langimport'); +echo $output->render($renderable); -$uninstallurl = new moodle_url('/admin/tool/langimport/index.php'); $PAGE->requires->strings_for_js(array('uninstallconfirm', 'uninstall', 'selectlangs', 'noenglishuninstall'), 'tool_langimport'); $PAGE->requires->yui_module('moodle-core-languninstallconfirm', @@ -223,4 +184,3 @@ $PAGE->requires->yui_module('moodle-core-languninstallconfirm', array(array('uninstallUrl' => $uninstallurl->out())) ); echo $OUTPUT->footer(); -die(); diff --git a/admin/tool/langimport/styles.css b/admin/tool/langimport/styles.css index 42d8a69dcef..f23c99bc3d0 100644 --- a/admin/tool/langimport/styles.css +++ b/admin/tool/langimport/styles.css @@ -1,9 +1,5 @@ -#page-admin-tool-langimport-index .generalbox table { +#page-admin-tool-langimport-index .langimport { margin: auto; + float: none; width: 100%; } - -#page-admin-tool-langimport-index .generalbox, -#page-admin-tool-langimport-index .generalbox table { - text-align: center; -} diff --git a/admin/tool/langimport/templates/langimport.mustache b/admin/tool/langimport/templates/langimport.mustache new file mode 100644 index 00000000000..25dc7002370 --- /dev/null +++ b/admin/tool/langimport/templates/langimport.mustache @@ -0,0 +1,117 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template tool_langimport/langimport + + Template for the language import page. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * sesskey string The session key. + * uninstallurl string The URL for the uninstall action. + * updateurl string The URL for the update-language-packs action. + * installurl string The URL for the install action. + * installedoptions array The list of languages installed. + * toinstalloptions array The list of languages to be installed. + * caninstall boolean Flag to indicate if there are language packs that can be installed. + + Example context (json): + { + "sesskey": "sesskey", + "uninstallurl": "#", + "updateurl": "#", + "installurl": "#", + "installedoptions": [ + { + "value": "en", + "text": "English", + "selected": true + } + ], + "toinstalloptions": [ + { + "value": "ja", + "text": "Japanese" + }, + { + "value": "fr", + "text": "French" + }, + { + "value": "es", + "text": "Spanish" + } + ], + "caninstall": true + } +}} +
+
+
+
+
+
+ + +
+
+ + +
+
+
+ {{#updateurl}} +
+
+
+ +
+
+
+ {{/updateurl}} +
+ {{#caninstall}} +
+
+
+
+ + +
+
+ + +
+
+
+
+ {{/caninstall}} +
+