[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core; 18 19 /** 20 * User Alert notifications. 21 * 22 * @package core 23 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 class notification { 30 /** 31 * A notification of level 'success'. 32 */ 33 const SUCCESS = 'success'; 34 35 /** 36 * A notification of level 'warning'. 37 */ 38 const WARNING = 'warning'; 39 40 /** 41 * A notification of level 'info'. 42 */ 43 const INFO = 'info'; 44 45 /** 46 * A notification of level 'error'. 47 */ 48 const ERROR = 'error'; 49 50 /** 51 * Add a message to the session notification stack. 52 * 53 * @param string $message The message to add to the stack 54 * @param string $level The type of message to add to the stack 55 */ 56 public static function add($message, $level = null) { 57 global $PAGE, $SESSION; 58 59 if ($PAGE && $PAGE->state === \moodle_page::STATE_IN_BODY) { 60 // Currently in the page body - just render and exit immediately. 61 // We insert some code to immediately insert this into the user-notifications created by the header. 62 $id = uniqid(); 63 echo \html_writer::span( 64 $PAGE->get_renderer('core')->render(new \core\output\notification($message, $level)), 65 '', array('id' => $id)); 66 67 // Insert this JS here using a script directly rather than waiting for the page footer to load to avoid 68 // ensure that the message is added to the user-notifications section as soon as possible after it is created. 69 echo \html_writer::script( 70 "(function() {" . 71 "var notificationHolder = document.getElementById('user-notifications');" . 72 "if (!notificationHolder) { return; }" . 73 "var thisNotification = document.getElementById('{$id}');" . 74 "if (!thisNotification) { return; }" . 75 "notificationHolder.appendChild(thisNotification.firstChild);" . 76 "thisNotification.remove();" . 77 "})();" 78 ); 79 return; 80 } 81 82 // Add the notification directly to the session. 83 // This will either be fetched in the header, or by JS in the footer. 84 if (!isset($SESSION->notifications) || !array($SESSION->notifications)) { 85 $SESSION->notifications = []; 86 } 87 $SESSION->notifications[] = (object) array( 88 'message' => $message, 89 'type' => $level, 90 ); 91 } 92 93 /** 94 * Fetch all of the notifications in the stack and clear the stack. 95 * 96 * @return array All of the notifications in the stack 97 */ 98 public static function fetch() { 99 global $SESSION; 100 101 if (!isset($SESSION) || !isset($SESSION->notifications)) { 102 return []; 103 } 104 105 $notifications = $SESSION->notifications; 106 unset($SESSION->notifications); 107 108 $renderables = []; 109 foreach ($notifications as $notification) { 110 $renderable = new \core\output\notification($notification->message, $notification->type); 111 $renderables[] = $renderable; 112 } 113 114 return $renderables; 115 } 116 117 /** 118 * Fetch all of the notifications in the stack and clear the stack. 119 * 120 * @return array All of the notifications in the stack 121 */ 122 public static function fetch_as_array(\renderer_base $renderer) { 123 $notifications = []; 124 foreach (self::fetch() as $notification) { 125 $notifications[] = [ 126 'template' => $notification->get_template_name(), 127 'variables' => $notification->export_for_template($renderer), 128 ]; 129 } 130 return $notifications; 131 } 132 133 /** 134 * Add a success message to the notification stack. 135 * 136 * @param string $message The message to add to the stack 137 */ 138 public static function success($message) { 139 return self::add($message, self::SUCCESS); 140 } 141 142 /** 143 * Add a info message to the notification stack. 144 * 145 * @param string $message The message to add to the stack 146 */ 147 public static function info($message) { 148 return self::add($message, self::INFO); 149 } 150 151 /** 152 * Add a warning message to the notification stack. 153 * 154 * @param string $message The message to add to the stack 155 */ 156 public static function warning($message) { 157 return self::add($message, self::WARNING); 158 } 159 160 /** 161 * Add a error message to the notification stack. 162 * 163 * @param string $message The message to add to the stack 164 */ 165 public static function error($message) { 166 return self::add($message, self::ERROR); 167 } 168 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |