-
Notifications
You must be signed in to change notification settings - Fork 6
/
renderer.php
469 lines (402 loc) · 13.9 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* Renderer
*
* @package local_envbar
* @author Brendan Heywoody ([email protected])
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use local_envbar\local\envbarlib;
/**
* Renderer for envbar.
*
* @copyright 2016 Brendan Heywood ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_envbar_renderer extends plugin_renderer_base {
/**
* Render the envbar
*
* @param stdClass $match And environment to show
* @param bool $fixed should this bar be fixed to the header
* @param array $envs
*
* @return string
*/
public function render_envbar($match, $fixed = true, $envs = array()) {
$config = get_config('local_envbar');
$js = '';
$css = <<<EOD
.envbar {
padding: 15px;
width: 100%;
height: 50px;
text-align: center;
margin-bottom: 10px;
box-sizing: border-box;
}
.envbar.env{$match->id},
.envbar.env{$match->id} a {
background: {$match->colourbg};
color: {$match->colourtext};
}
.envbar.env{$match->id} a {
text-decoration: underline;
}
.envbar.fixed {
position: fixed;
top: 0px;
left: 0px;
z-index: 9999;
}
@media screen and (max-width: 700px) {
.envbar {
font-size: 12px;
line-height: 12px;
padding: 10px;
}
}
EOD;
// If passed a list of env's, then for any env in the list which
// isn't the one we are on, and which isn't production, add some
// css which highlights broken links which jump between env's.
if (isset($config->highlightlinks) && $config->highlightlinks) {
foreach ($envs as $env) {
if ($env->matchpattern != $match->matchpattern) {
$css .= <<<EOD
a[href^="{$env->matchpattern}"]:not(.no-envbar-highlight) {
outline: 2px solid {$env->colourbg};
padding-right: 4px;
}
a[href^="{$env->matchpattern}"]:not(.no-envbar-highlight)::before {
content: '{$env->showtext}';
background-color: {$env->colourbg};
color: {$env->colourtext};
padding: 1px 4px 1px 2px;
margin-right: 4px;
}
EOD;
}
}
}
if (isset($config->highlightlinks) && !empty($config->highlightlinks) && empty($config->highlightlinksenvbar)) {
$css .= <<<EOD
/* Restricting the rules above for elements outside the envbar with :not() does not work reliably,
so we revert the rules here. */
.envbar a[href^="{$env->matchpattern}"] {
outline: inherit;
}
.envbar a[href^="{$env->matchpattern}"]::before {
content: '';
background-color: transparent;
padding: 0;
}
EOD;
}
if ($fixed) {
$css .= empty($config->extracss) ? envbarlib::get_default_extra_css() : $config->extracss;
}
$class = 'env' . $match->id;
$class .= $fixed ? ' fixed' : '';
// Show the configured env message.
$showtext = format_string(htmlspecialchars($match->showtext));
// Just show the biggest time unit instead of 2.
if (!isset($config->stringseparator)) {
$config->stringseparator = '-'; // Set default.
}
if (isset($config->showrefresh) && $config->showrefresh) {
if (property_exists($match, 'lastrefresh') && $match->lastrefresh > 0) {
$show = format_time(time() - $match->lastrefresh);
$title = userdate($match->lastrefresh, get_string('refreshedagoformat', 'local_envbar'));
$title = get_string('refreshedagotitle', 'local_envbar', $title);
$num = strtok($show, ' ');
$unit = strtok(' ');
$show = html_writer::tag('span', "$num $unit", ['title' => $title, ]);
$showtext .= ' ' . $config->stringseparator . ' ' . get_string('refreshedago', 'local_envbar', $show);
} else {
$showtext .= ' ' . $config->stringseparator . ' ' . get_string('refreshednever', 'local_envbar');
}
}
$nextrefresh = isset($config->nextrefresh) ? $config->nextrefresh : null;
if (isset($nextrefresh)) {
if ($nextrefresh === intval($nextrefresh)) {
// Does the value look like a timestamp?
$nextrefresh = intval($nextrefresh);
} else if ( ($time = strtotime($nextrefresh)) !== false ) {
// Does the value look like a date string?
$nextrefresh = $time;
} else {
// Dunno just ignore it.
$nextrefresh = null;
}
if ($nextrefresh) {
$show = format_time($nextrefresh - time());
$num = strtok($show, ' ');
$unit = strtok(' ');
$show = "$num $unit";
$showtext .= ' ' . $config->stringseparator . ' ' . get_string('nextrefreshin', 'local_envbar', $show);
}
}
// Optionally also show the config links for admins.
$produrl = envbarlib::getprodwwwroot();
$systemcontext = context_system::instance();
$canedit = has_capability('moodle/site:config', $systemcontext);
if ($canedit && !empty($config->showconfiglink)) {
if ($produrl) {
$editlink = html_writer::link($produrl.'/local/envbar/index.php',
get_string('configureinprod', 'local_envbar'),
array('target' => 'prod', 'class' => 'no-envbar-highlight'));
} else {
$editlink = html_writer::link(new moodle_url('/local/envbar/index.php'),
get_string('configurehere', 'local_envbar'));
}
$showtext .= '<nobr> ' . $config->stringseparator . ' ' . $editlink . '</nobr>';
}
if (!empty($config->showdebugging)) {
$showtext .= $this->get_debug_text($config, $canedit);
}
if ($fixed) {
$js .= local_envbar_favicon_js($match);
$js .= local_envbar_user_menu($envs, $match);
$js .= local_envbar_title($match);
}
$envclass = strtolower($match->showtext);
$envclass = s(preg_replace('/\s+/', '', $envclass));
if ($fixed) {
$js .= <<<EOD
document.body.className += ' local_envbar local_envbar_$envclass';
EOD;
}
$html = <<<EOD
<div class="envbar $class">
<button type="button" class="close" onclick="envbar_close(this);">×</button>
$showtext
</div>
<style>
$css
</style>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
$js
});
function envbar_close(el) {
var envbar = el.parentElement;
var parent = envbar.parentElement;
var body = document.body;
body.classList.remove('local_envbar');
parent.removeChild(envbar);
parent.removeChild(document.getElementById('envbar_spacer'));
}
</script>
EOD;
if ($fixed) {
$html .= <<<EOD
<div id="envbar_spacer" style="height: 50px;"> </div>
EOD;
}
// Wrap up the envbar in tokens.
$ebstart = envbarlib::ENVBAR_START;
$ebend = envbarlib::ENVBAR_END;
$html = <<<EOD
$ebstart
$html
$ebend
EOD;
return $html;
}
/**
* Returns the debug text to be displayed in the envbar.
*
* @param stdClass $config Config
* @param bool $canedit Whether editing is allowed
* @return string Debug text
*/
protected function get_debug_text(stdClass $config, bool $canedit): string {
global $ME;
$debugtext = '';
$debugging = envbarlib::get_debugging_status_string();
if ($canedit) {
// Get the url of the current page.
$currentlink = $ME ?? '/';
$debugtogglelink = html_writer::link(
new moodle_url('/local/envbar/toggle_debugging.php',
['redirect' => base64_encode($currentlink), 'sesskey' => sesskey()] ),
envbarlib::get_debug_toggle_string()
);
$debugtext .= $this->get_debug_text_for_admin($config->stringseparator, $debugging, $debugtogglelink);
} else {
$debugtext .= '<nobr> ' . $config->stringseparator . ' ' . $debugging . '</nobr>';
}
return $debugtext;
}
/**
* Returns the debug text to be displayed for admin.
*
* @param string $stringseparator String separator
* @param string $debugging Debugging text
* @param string $debugtogglelink Debug toggle link
* @return string Debug text
*/
protected function get_debug_text_for_admin($stringseparator, $debugging, $debugtogglelink) {
global $CFG;
$debugtext = '';
// Check if debug level and debug display is set on config.php.
if (!isset($CFG->config_php_settings['debug']) && !isset($CFG->config_php_settings['debugdisplay'])) {
$debugtext .= '<nobr> ' . $stringseparator . ' ' . $debugging. ' ' . $debugtogglelink . '</nobr>';
} else {
$debuggingdefinedstr = get_string('debuggingdefinedinconfig', 'local_envbar');
// Remove link to toggle debugging.
$debugtext .= '<nobr> ' . $stringseparator . ' ' . $debugging;
$debugtext .= ' ' . $debuggingdefinedstr . '</nobr>';
}
return $debugtext;
}
}
/**
* Gets some JS which adds the env to the page title
*
* @param object $match
* @return string A chunk of JS to set the title
*/
function local_envbar_title($match) {
$config = get_config('local_envbar');
if (empty($config->enabletitleprefix)) {
return '';
}
$prefix = s(substr($match->showtext, 0, 4));
$js = <<<EOD
var title = document.querySelector('title');
title.innerText = '$prefix: ' + title.innerText;
EOD;
return $js;
}
/**
* Gets some JS which colorizes the favicon according to the env
*
* @param object $match
* @return string A chunk of JS to set the favicon
*/
function local_envbar_favicon_js($match) {
$config = get_config('local_envbar');
if (empty($config->enablefaviconcolorize)) {
return '';
}
$js = <<<EOD
var favicon;
var links = document.getElementsByTagName("link");
for (var i = 0; i < links.length; i++) {
if ((links[i].getAttribute("rel") == "icon") ||
(links[i].getAttribute("rel") == "shortcut icon")) {
favicon = links[i];
}
}
if (!favicon) {
favicon = document.createElement('link');
favicon.rel = 'shortcut icon';
favicon.type = 'image/x-icon';
favicon.href = '';
document.getElementsByTagName('head')[0].appendChild(favicon);
}
// First we make the whole thing a solid color matching the envbar.
var canvas = document.createElement('canvas');
canvas.width = 16;
canvas.height = 16;
var ctx = canvas.getContext('2d');
ctx.fillStyle = "{$match->colourbg}";
ctx.fillRect(0, 0, 16, 16);
// And then optionally if there was an existing favicon we add it back
// but partially transparent so it's still colorised.
if (favicon.href) {
var img = new Image();
img.src = favicon.href;
img.onload = function() {
ctx.globalAlpha = 0.6;
ctx.drawImage(img, 0, 0, 16, 16);
favicon.href = canvas.toDataURL("image/x-icon");
}
}
favicon.href = canvas.toDataURL("image/x-icon");
EOD;
return $js;
}
/**
* Gets some JS which inserts env jump links into the user menu
*
* @param array $envs
* @return string A chunk of JS
*/
function local_envbar_user_menu($envs) {
global $CFG, $PAGE;
$config = get_config('local_envbar');
if (empty($config->enablemenu)) {
return '';
}
if (isset($config->menuselector)) {
$menuselector = $config->menuselector;
} else {
$menuselector = '.usermenu .menu';
}
if (empty($menuselector)) {
return ''; // Not using user menu, nothing to do.
}
$html = '';
if ($PAGE->has_set_url()) {
$url = $PAGE->url->out();
foreach ($envs as $env) {
$jump = $url;
$jump = str_replace($CFG->wwwroot, $env->matchpattern, $jump);
if ($jump == $url) {
continue;
}
$jump = s($jump);
$show = s($env->showtext);
$link = <<<EOD
<li role="presentation">
<a class="icon menu-action no-envbar-highlight" role="menuitem" href="{$jump}">
<span class="menu-action-text">$show</span>
</a>
</li>
EOD;
$html .= $link;
}
}
if (!$html) {
return '';
}
if (isset($config->dividerselector)) {
$divider = $config->dividerselector;
} else {
$divider = 'filler';
}
$html = '<li role="presentation"><span class="'. $divider .'"> </span></li>' . $html;
$html = str_replace("\n", '', $html);
$html = str_replace("\"", "\\\"", $html);
$url = (new moodle_url('/admin/settings.php?section=local_envbar_presentation'))->out();
$isadmin = (is_siteadmin() ) ? '1' : '0';
$js = <<<EOD
var menu = document.querySelector('$menuselector');
var html = "$html";
if (menu) {
menu.insertAdjacentHTML('beforeend', html);
} else {
$isadmin && console.error(
"local_envbar: Menu selector is misconfigured '$menuselector' \\n Please configure it here: $url");
}
EOD;
return $js;
}