| 1441 |
ariadna |
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\output;
|
|
|
18 |
|
|
|
19 |
use core_useragent;
|
|
|
20 |
use moodle_url;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* A renderer that generates output for ajax scripts.
|
|
|
25 |
*
|
|
|
26 |
* This renderer prevents accidental sends back only json
|
|
|
27 |
* encoded error messages, all other output is ignored.
|
|
|
28 |
*
|
|
|
29 |
* @copyright 2010 Petr Skoda
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
* @since Moodle 2.0
|
|
|
32 |
* @package core
|
|
|
33 |
* @category output
|
|
|
34 |
*/
|
|
|
35 |
class core_renderer_ajax extends core_renderer {
|
|
|
36 |
/**
|
|
|
37 |
* Returns a template fragment representing a fatal error.
|
|
|
38 |
*
|
|
|
39 |
* @param string $message The message to output
|
|
|
40 |
* @param string $moreinfourl URL where more info can be found about the error
|
|
|
41 |
* @param string $link Link for the Continue button
|
|
|
42 |
* @param array $backtrace The execution backtrace
|
|
|
43 |
* @param null|string $debuginfo Debugging information
|
|
|
44 |
* @param string $errorcode
|
|
|
45 |
* @return string A template fragment for a fatal error
|
|
|
46 |
*/
|
|
|
47 |
public function fatal_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null, $errorcode = "") {
|
|
|
48 |
global $CFG;
|
|
|
49 |
|
|
|
50 |
// Ugly hack - make sure page context is set to something, we do not want bogus warnings here.
|
|
|
51 |
$this->page->set_context(null);
|
|
|
52 |
|
|
|
53 |
$e = new stdClass();
|
|
|
54 |
$e->error = $message;
|
|
|
55 |
$e->errorcode = $errorcode;
|
|
|
56 |
$e->stacktrace = null;
|
|
|
57 |
$e->debuginfo = null;
|
|
|
58 |
$e->reproductionlink = null;
|
|
|
59 |
if (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER) {
|
|
|
60 |
$link = (string) $link;
|
|
|
61 |
if ($link) {
|
|
|
62 |
$e->reproductionlink = $link;
|
|
|
63 |
}
|
|
|
64 |
if (!empty($debuginfo)) {
|
|
|
65 |
$e->debuginfo = $debuginfo;
|
|
|
66 |
}
|
|
|
67 |
if (!empty($backtrace)) {
|
|
|
68 |
$e->stacktrace = format_backtrace($backtrace, true);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
$this->header();
|
|
|
72 |
return json_encode($e);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Used to display a notification.
|
|
|
77 |
* For the AJAX notifications are discarded.
|
|
|
78 |
*
|
|
|
79 |
* @param string $message The message to print out.
|
|
|
80 |
* @param string $type The type of notification. See constants on \core\output\notification.
|
|
|
81 |
* @param bool $closebutton Whether to show a close icon to remove the notification (default true).
|
|
|
82 |
* @param string|null $title The title of the notification.
|
|
|
83 |
* @param ?string $titleicon if the title should have an icon you can give the icon name with the component
|
|
|
84 |
* (e.g. 'i/circleinfo, core' or 'i/circleinfo' if the icon is from core)
|
|
|
85 |
*/
|
|
|
86 |
public function notification($message, $type = null, $closebutton = true, ?string $title = null, ?string $titleicon = null) {
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Used to display a redirection message.
|
|
|
91 |
* AJAX redirections should not occur and as such redirection messages
|
|
|
92 |
* are discarded.
|
|
|
93 |
*
|
|
|
94 |
* @param moodle_url|string $encodedurl
|
|
|
95 |
* @param string $message
|
|
|
96 |
* @param int $delay
|
|
|
97 |
* @param bool $debugdisableredirect
|
|
|
98 |
* @param string $messagetype The type of notification to show the message in.
|
|
|
99 |
* See constants on \core\output\notification.
|
|
|
100 |
*/
|
|
|
101 |
public function redirect_message(
|
|
|
102 |
$encodedurl,
|
|
|
103 |
$message,
|
|
|
104 |
$delay,
|
|
|
105 |
$debugdisableredirect,
|
|
|
106 |
$messagetype = notification::NOTIFY_INFO,
|
|
|
107 |
) {
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Prepares the start of an AJAX output.
|
|
|
112 |
*/
|
|
|
113 |
public function header() {
|
|
|
114 |
// Unfortunately YUI iframe upload does not support application/json.
|
|
|
115 |
if (!empty($_FILES)) {
|
|
|
116 |
@header('Content-type: text/plain; charset=utf-8');
|
|
|
117 |
if (!core_useragent::supports_json_contenttype()) {
|
|
|
118 |
@header('X-Content-Type-Options: nosniff');
|
|
|
119 |
}
|
|
|
120 |
} else if (!core_useragent::supports_json_contenttype()) {
|
|
|
121 |
@header('Content-type: text/plain; charset=utf-8');
|
|
|
122 |
@header('X-Content-Type-Options: nosniff');
|
|
|
123 |
} else {
|
|
|
124 |
@header('Content-type: application/json; charset=utf-8');
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
// Headers to make it not cacheable and json.
|
|
|
128 |
@header('Cache-Control: no-store, no-cache, must-revalidate');
|
|
|
129 |
@header('Cache-Control: post-check=0, pre-check=0', false);
|
|
|
130 |
@header('Pragma: no-cache');
|
|
|
131 |
@header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
|
|
132 |
@header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
|
|
133 |
@header('Accept-Ranges: none');
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* There is no footer for an AJAX request, however we must override the
|
|
|
138 |
* footer method to prevent the default footer.
|
|
|
139 |
*/
|
|
|
140 |
public function footer() {
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* No need for headers in an AJAX request... this should never happen.
|
|
|
145 |
* @param string $text
|
|
|
146 |
* @param int $level
|
|
|
147 |
* @param string $classes
|
|
|
148 |
* @param string $id
|
|
|
149 |
*/
|
|
|
150 |
public function heading($text, $level = 2, $classes = 'main', $id = null) {
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
// Alias this class to the old name.
|
|
|
155 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
156 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
157 |
class_alias(core_renderer_ajax::class, \core_renderer_ajax::class);
|