MDL-30811 core: Make use of session notifications in redirect()

This commit is contained in:
Andrew Nicols 2016-02-16 08:49:42 +08:00
parent 0346323cec
commit 3ad964190c
3 changed files with 27 additions and 8 deletions

View File

@ -871,10 +871,13 @@ class core_renderer extends renderer_base {
* @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);
@ -905,7 +908,7 @@ class core_renderer extends renderer_base {
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>';
@ -4383,8 +4386,11 @@ class core_renderer_ajax extends core_renderer {
* @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.

View File

@ -3,6 +3,10 @@ information provided here is intended especially for developers.
=== 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.

View File

@ -2596,9 +2596,10 @@ function notice ($message, $link='', $course=null) {
* @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) {
@ -2696,10 +2697,18 @@ function redirect($url, $message='', $delay=-1) {
$url = str_replace('&amp;', '&', $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;
@ -2720,7 +2729,7 @@ function redirect($url, $message='', $delay=-1) {
// 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);