* @param boolean $debugdisableredirect this redirect has been disabled for
* debugging purposes. Display a message that explains, and don't
* trigger the redirect.
+ * @param string $messagetype The type of notification to show the message in.
+ * See constants on \core\output\notification.
* @return string The HTML to display to the user before dying, may contain
* meta refresh, javascript refresh, and may have set header redirects
*/
- public function redirect_message($encodedurl, $message, $delay, $debugdisableredirect) {
+ public function redirect_message($encodedurl, $message, $delay, $debugdisableredirect,
+ $messagetype = \core\output\notification::NOTIFY_INFO) {
global $CFG;
$url = str_replace('&', '&', $encodedurl);
throw new coding_exception('You cannot redirect after the entire page has been generated');
break;
}
- $output .= $this->notification($message, 'redirectmessage');
+ $output .= $this->notification($message, $messagetype);
$output .= '<div class="continuebutton">(<a href="'. $encodedurl .'">'. get_string('continue') .'</a>)</div>';
if ($debugdisableredirect) {
$output .= '<p><strong>'.get_string('erroroutput', 'error').'</strong></p>';
* @param string $message
* @param int $delay
* @param bool $debugdisableredirect
+ * @param string $messagetype The type of notification to show the message in.
+ * See constants on \core\output\notification.
*/
- public function redirect_message($encodedurl, $message, $delay, $debugdisableredirect) {}
+ public function redirect_message($encodedurl, $message, $delay, $debugdisableredirect,
+ $messagetype = \core\output\notification::NOTIFY_INFO) {}
/**
* Prepares the start of an AJAX output.
=== 3.1 ===
+* The redirect() function will now redirect immediately if output has not
+ already started. Messages will be displayed on the subsequent page using
+ session notifications. The type of message output can be configured using the
+ fourth parameter to redirect().
* The specification of extra classes in the $OUTPUT->notification()
function, and \core\output\notification renderable have been deprecated
and will be removed in a future version.
* @param moodle_url|string $url A moodle_url to redirect to. Strings are not to be trusted!
* @param string $message The message to display to the user
* @param int $delay The delay before redirecting
+ * @param string $messagetype The type of notification to show the message in. See constants on \core\output\notification.
* @throws moodle_exception
*/
-function redirect($url, $message='', $delay=-1) {
+function redirect($url, $message='', $delay=-1, $messagetype = \core\output\notification::NOTIFY_INFO) {
global $OUTPUT, $PAGE, $CFG;
if (CLI_SCRIPT or AJAX_SCRIPT) {
$url = str_replace('&', '&', $encodedurl);
if (!empty($message)) {
- if ($delay === -1 || !is_numeric($delay)) {
- $delay = 3;
+ if (!$debugdisableredirect && !headers_sent()) {
+ // A message has been provided, and the headers have not yet been sent.
+ // Display the message as a notification on the subsequent page.
+ \core\notification::add($message, $messagetype);
+ $message = null;
+ $delay = 0;
+ } else {
+ if ($delay === -1 || !is_numeric($delay)) {
+ $delay = 3;
+ }
+ $message = clean_text($message);
}
- $message = clean_text($message);
} else {
$message = get_string('pageshouldredirect');
$delay = 0;
// Include a redirect message, even with a HTTP redirect, because that is recommended practice.
if ($PAGE) {
$CFG->docroot = false; // To prevent the link to moodle docs from being displayed on redirect page.
- echo $OUTPUT->redirect_message($encodedurl, $message, $delay, $debugdisableredirect);
+ echo $OUTPUT->redirect_message($encodedurl, $message, $delay, $debugdisableredirect, $messagetype);
exit;
} else {
echo bootstrap_renderer::early_redirect_message($encodedurl, $message, $delay);