From 263fb9d1bc1a5f074da54d5d027896e0a2983244 Mon Sep 17 00:00:00 2001 From: Jetha Chan Date: Mon, 23 Mar 2015 14:27:16 +0800 Subject: [PATCH] MDL-41608 lib: add notification renderable using mustache --- lib/classes/output/notification.php | 94 ++++++++++++++++ lib/outputrenderers.php | 103 +++++++++++++++++- .../output/notification_message.mustache | 31 ++++++ .../output/notification_problem.mustache | 31 ++++++ .../output/notification_redirect.mustache | 31 ++++++ .../output/notification_success.mustache | 31 ++++++ .../core/output/notification_message.mustache | 31 ++++++ .../core/output/notification_problem.mustache | 31 ++++++ .../output/notification_redirect.mustache | 31 ++++++ .../core/output/notification_success.mustache | 31 ++++++ .../bootstrapbase/renderers/core_renderer.php | 53 --------- 11 files changed, 444 insertions(+), 54 deletions(-) create mode 100644 lib/classes/output/notification.php create mode 100644 lib/templates/output/notification_message.mustache create mode 100644 lib/templates/output/notification_problem.mustache create mode 100644 lib/templates/output/notification_redirect.mustache create mode 100644 lib/templates/output/notification_success.mustache create mode 100644 theme/base/templates/core/output/notification_message.mustache create mode 100644 theme/base/templates/core/output/notification_problem.mustache create mode 100644 theme/base/templates/core/output/notification_redirect.mustache create mode 100644 theme/base/templates/core/output/notification_success.mustache diff --git a/lib/classes/output/notification.php b/lib/classes/output/notification.php new file mode 100644 index 00000000000..6cf19eddd84 --- /dev/null +++ b/lib/classes/output/notification.php @@ -0,0 +1,94 @@ +. + +/** + * Notification renderable component. + * + * @package core + * @copyright 2015 Jetha Chan + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\output; +use stdClass; + +/** + * Data structure representing a notification. + * + * @copyright 2015 Jetha Chan + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.9 + * @package core + * @category output + */ +class notification implements \renderable, \templatable { + + /** + * A generic message. + */ + const NOTIFY_MESSAGE = 'message'; + /** + * A message notifying the user of a successful operation. + */ + const NOTIFY_SUCCESS = 'success'; + /** + * A message notifying the user that a problem occurred. + */ + const NOTIFY_PROBLEM = 'problem'; + /** + * A message to display during a redirect.. + */ + const NOTIFY_REDIRECT = 'redirect'; + + /** + * @var string Message payload. + */ + private $message = ''; + + /** + * @var string Message type. + */ + private $messagetype = self::NOTIFY_PROBLEM; + + /** + * Notification constructor. + * + * @param string $message the message to print out + * @param string $messagetype normally NOTIFY_PROBLEM or NOTIFY_SUCCESS. + */ + public function __construct($message, $messagetype = self::NOTIFY_PROBLEM) { + + $this->message = clean_text($message); + $this->messagetype = $messagetype; + + } + + /** + * Export this data so it can be used as the context for a mustache template. + * + * @param renderer_base $output typically, the renderer that's calling this function + * @return stdClass data context for a mustache template + */ + public function export_for_template(\renderer_base $output) { + + $data = new stdClass(); + + $data->type = $this->messagetype; + $data->message = $this->message; + + return $data; + } +} diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 89b24233778..054296c888e 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -2848,7 +2848,108 @@ EOD; * @return string the HTML to output. */ public function notification($message, $classes = 'notifyproblem') { - return html_writer::tag('div', clean_text($message), array('class' => renderer_base::prepare_classes($classes))); + + $classmappings = array( + 'notifyproblem' => \core\output\notification::NOTIFY_PROBLEM, + 'notifytiny' => \core\output\notification::NOTIFY_PROBLEM, + 'notifysuccess' => \core\output\notification::NOTIFY_SUCCESS, + 'notifymessage' => \core\output\notification::NOTIFY_MESSAGE, + 'redirectmessage' => \core\output\notification::NOTIFY_REDIRECT + ); + + // Identify what type of notification this is. + $type = \core\output\notification::NOTIFY_PROBLEM; + $classarray = explode(' ', self::prepare_classes($classes)); + if (count($classarray) > 0) { + foreach ($classarray as $class) { + if (isset($classmappings[$class])) { + $type = $classmappings[$class]; + break; + } + } + } + + $n = new \core\output\notification($message, $type); + return $this->render($n); + + } + + /** + * Output a notification at a particular level - in this case, NOTIFY_PROBLEM. + * + * @param string $message the message to print out + * @return string HTML fragment. + */ + public function notify_problem($message) { + $n = new \core\output\notification($message, \core\output\notification::NOTIFY_PROBLEM); + return $this->render($n); + } + + /** + * Output a notification at a particular level - in this case, NOTIFY_SUCCESS. + * + * @param string $message the message to print out + * @return string HTML fragment. + */ + public function notify_success($message) { + $n = new \core\output\notification($message, \core\output\notification::NOTIFY_SUCCESS); + return $this->render($n); + } + + /** + * Output a notification at a particular level - in this case, NOTIFY_MESSAGE. + * + * @param string $message the message to print out + * @return string HTML fragment. + */ + public function notify_message($message) { + $n = new notification($message, notification::NOTIFY_MESSAGE); + return $this->render($n); + } + + /** + * Output a notification at a particular level - in this case, NOTIFY_REDIRECT. + * + * @param string $message the message to print out + * @return string HTML fragment. + */ + public function notify_redirect($message) { + $n = new \core\output\notification($message, \core\output\notification::NOTIFY_REDIRECT); + return $this->render($n); + } + + /** + * Render a notification (that is, a status message about something that has + * just happened). + * + * @param \core\output\notification $notification the notification to print out + * @return string the HTML to output. + */ + protected function render_notification(\core\output\notification $notification) { + + $data = $notification->export_for_template($this); + + $templatename = ''; + switch($data->type) { + case \core\output\notification::NOTIFY_MESSAGE: + $templatename = 'core/output/notification_message'; + break; + case \core\output\notification::NOTIFY_SUCCESS: + $templatename = 'core/output/notification_success'; + break; + case \core\output\notification::NOTIFY_PROBLEM: + $templatename = 'core/output/notification_problem'; + break; + case \core\output\notification::NOTIFY_REDIRECT: + $templatename = 'core/output/notification_redirect'; + break; + default: + $templatename = 'core/output/notification_message'; + break; + } + + return self::render_from_template($templatename, $data); + } /** diff --git a/lib/templates/output/notification_message.mustache b/lib/templates/output/notification_message.mustache new file mode 100644 index 00000000000..26178f81f7c --- /dev/null +++ b/lib/templates/output/notification_message.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + Moodle notification template. + + The purpose of this template is to render a message notification. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * message A cleaned string (use clean_text()) to display. +}} +
{{{message}}}
\ No newline at end of file diff --git a/lib/templates/output/notification_problem.mustache b/lib/templates/output/notification_problem.mustache new file mode 100644 index 00000000000..65b4410dff3 --- /dev/null +++ b/lib/templates/output/notification_problem.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + Moodle notification template. + + The purpose of this template is to render a problem notification. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * message A cleaned string (use clean_text()) to display. +}} +
{{{message}}}
\ No newline at end of file diff --git a/lib/templates/output/notification_redirect.mustache b/lib/templates/output/notification_redirect.mustache new file mode 100644 index 00000000000..d13295029de --- /dev/null +++ b/lib/templates/output/notification_redirect.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + Moodle notification template. + + The purpose of this template is to render a message notification. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * message A cleaned string (use clean_text()) to display. +}} +
{{{message}}}
\ No newline at end of file diff --git a/lib/templates/output/notification_success.mustache b/lib/templates/output/notification_success.mustache new file mode 100644 index 00000000000..7f52c737190 --- /dev/null +++ b/lib/templates/output/notification_success.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + Moodle notification template. + + The purpose of this template is to render a success notification. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * message A cleaned string (use clean_text()) to display. +}} +
{{{message}}}
\ No newline at end of file diff --git a/theme/base/templates/core/output/notification_message.mustache b/theme/base/templates/core/output/notification_message.mustache new file mode 100644 index 00000000000..ac0e3fa4752 --- /dev/null +++ b/theme/base/templates/core/output/notification_message.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + Moodle notification template. + + The purpose of this template is to render a message notification. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * message A cleaned string (use clean_text()) to display. +}} +
{{{message}}}
\ No newline at end of file diff --git a/theme/base/templates/core/output/notification_problem.mustache b/theme/base/templates/core/output/notification_problem.mustache new file mode 100644 index 00000000000..f87ce3573c8 --- /dev/null +++ b/theme/base/templates/core/output/notification_problem.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + Moodle notification template. + + The purpose of this template is to render a problem notification. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * message A cleaned string (use clean_text()) to display. +}} +
{{{message}}}
\ No newline at end of file diff --git a/theme/base/templates/core/output/notification_redirect.mustache b/theme/base/templates/core/output/notification_redirect.mustache new file mode 100644 index 00000000000..af5cb5b14f9 --- /dev/null +++ b/theme/base/templates/core/output/notification_redirect.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + Moodle notification template. + + The purpose of this template is to render a message notification. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * message A cleaned string (use clean_text()) to display. +}} +
{{{message}}}
\ No newline at end of file diff --git a/theme/base/templates/core/output/notification_success.mustache b/theme/base/templates/core/output/notification_success.mustache new file mode 100644 index 00000000000..3b841616ea9 --- /dev/null +++ b/theme/base/templates/core/output/notification_success.mustache @@ -0,0 +1,31 @@ +{{! + 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 . +}} +{{! + Moodle notification template. + + The purpose of this template is to render a success notification. + + Classes required for JS: + * none + + Data attributes required for JS: + * none + + Context variables required for this template: + * message A cleaned string (use clean_text()) to display. +}} +
{{{message}}}
\ No newline at end of file diff --git a/theme/bootstrapbase/renderers/core_renderer.php b/theme/bootstrapbase/renderers/core_renderer.php index e39462d8654..b169fcc769b 100644 --- a/theme/bootstrapbase/renderers/core_renderer.php +++ b/theme/bootstrapbase/renderers/core_renderer.php @@ -27,29 +27,6 @@ class theme_bootstrapbase_core_renderer extends core_renderer { /** @var custom_menu_item language The language menu if created */ protected $language = null; - /* - * This renders a notification message. - * Uses bootstrap compatible html. - */ - public function notification($message, $classes = 'notifyproblem') { - $message = clean_text($message); - $type = ''; - - if (($classes == 'notifyproblem') || ($classes == 'notifytiny')) { - $type = 'alert alert-error'; - } - if ($classes == 'notifysuccess') { - $type = 'alert alert-success'; - } - if ($classes == 'notifymessage') { - $type = 'alert alert-info'; - } - if ($classes == 'redirectmessage') { - $type = 'alert alert-block alert-info'; - } - return "
$message
"; - } - /* * This renders the navbar. * Uses bootstrap compatible html. @@ -247,34 +224,4 @@ class theme_bootstrapbase_core_renderer extends core_renderer { * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class theme_bootstrapbase_core_renderer_maintenance extends core_renderer_maintenance { - /** - * Renders notifications for maintenance scripts. - * - * We need to override this method in the same way we do for the core_renderer maintenance method - * found above. - * Please note this isn't required of every function, only functions used during maintenance. - * In this case notification is used to print errors and we want pretty errors. - * - * @param string $message - * @param string $classes - * @return string - */ - public function notification($message, $classes = 'notifyproblem') { - $message = clean_text($message); - $type = ''; - - if (($classes == 'notifyproblem') || ($classes == 'notifytiny')) { - $type = 'alert alert-error'; - } - if ($classes == 'notifysuccess') { - $type = 'alert alert-success'; - } - if ($classes == 'notifymessage') { - $type = 'alert alert-info'; - } - if ($classes == 'redirectmessage') { - $type = 'alert alert-block alert-info'; - } - return "
$message
"; - } }