-
Notifications
You must be signed in to change notification settings - Fork 42
/
renderer.php
1351 lines (1209 loc) · 49.7 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Block XP renderer.
*
* @package block_xp
* @copyright 2014 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_xp\local\course_world;
use block_xp\local\activity\activity;
use block_xp\local\utils\user_utils;
use block_xp\local\xp\level;
use block_xp\local\xp\level_with_badge;
use block_xp\local\xp\level_with_name;
use block_xp\local\xp\state;
use block_xp\output\xp_widget;
/**
* Block XP renderer class.
*
* @package block_xp
* @copyright 2014 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_xp_renderer extends plugin_renderer_base {
/** Notice flag. */
const NOTICE_FLAG_QUEST = 'block_xp_notice_quest';
/** @var string Notices flag. */
protected $noticesflag = 'block_xp_notices';
/**
* Advanced heading.
*
* @param string $heading The heading.
* @param array $options The options.
*/
public function advanced_heading($heading, $options = []) {
$options = array_merge(['level' => 3, 'actions' => [], 'intro' => null, 'help' => null, 'visible' => null,
'menu' => []], $options);
$level = (int) $options['level'];
$actions = (array) $options['actions'];
$menu = (array) $options['menu'];
$intro = !empty($options['intro']) ? $options['intro'] : null;
$visible = isset($options['visible']) ? (bool) $options['visible'] : null;
$help = $options['help'] instanceof \help_icon ? $options['help'] : null;
$menuitems = array_values(array_filter(array_map(function($item) {
$attrs = [];
$classes = [];
if (empty($item['label'])) {
return ['isdivider' => true];
}
foreach ($item as $key => $value) {
if ($key === 'label' || $key === 'class' || $key === 'disabled' || $key === 'addonrequired') {
continue;
} else if ($key === 'danger') {
$classes[] = $value ? 'text-danger' : null;
continue;
}
$attrs[] = [
'name' => $key,
'value' => $value instanceof moodle_url ? $value->out(false) : (string) $value,
];
}
return [
'label' => $item['label'],
'disabled' => !empty($item['disabled']),
'addonrequired' => !empty($item['addonrequired']),
'attributes' => $attrs,
'classes' => array_filter($classes),
];
}, $menu)));
// Filter out orphan or doubled dividers.
$menuitems = array_values(array_filter($menuitems, function($v, $k) use ($menuitems) {
if (empty($v['isdivider'])) {
return true;
}
if ($k === 0 || $k === count($menuitems) - 1) {
return false;
} else if (array_key_exists('isdivider', $menuitems[$k - 1] ?? [])) {
return false;
}
return true;
}, ARRAY_FILTER_USE_BOTH));
return $this->render_from_template('block_xp/advanced-heading', [
'title' => $heading,
'level' => $level,
'islevel2' => $level === 2,
'hasintro' => !empty($intro),
'intro' => $intro,
'helpicon' => $help ? $help->export_for_template($this) : null,
'hasvisibility' => $visible !== null,
'isvisible' => $visible,
'hasactions' => !empty($actions),
'actions' => array_map([$this, 'render'], $actions),
'hasmenu' => !empty($menuitems),
'menuitems' => $menuitems,
]);
}
/**
* Confirm reset.
*
* @param string $title The title.
* @param string $message The message.
* @param \moodle_url $confirmurl The confirm URL.
* @param \moodle_url $cancelurl The cancel URL.
* @param array $options Some options.
* @return string
*/
public function confirm_reset($title, $message, \moodle_url $confirmurl, \moodle_url $cancelurl, $options = []) {
return $this->confirm_step($title, $message, $confirmurl, $cancelurl, $options + [
'confirmlabel' => get_string('reset', 'core'),
]);
}
/**
* Confirm step.
*
* This supercedes the default confirm renderer to accomodate for variation between versions,
* and to set our own sensible defaults. For instance, the confirm button is red by default.
*
* Note that the parameters are not the same as the {@see \core_renderer::confirm}.
*
* @param string $title The title.
* @param string $message The message.
* @param \moodle_url $confirmurl The confirm URL.
* @param \moodle_url $cancelurl The cancel URL.
* @param array $options Some options.
* @return string
*/
public function confirm_step($title, $message, \moodle_url $confirmurl, \moodle_url $cancelurl, $options = []) {
global $CFG;
if ($CFG->branch < 400) {
return parent::confirm($message, $confirmurl, $cancelurl);
}
return parent::confirm($message, $confirmurl, $cancelurl, [
'confirmtitle' => $title,
'continuestr' => $options['confirmlabel'] ?? null,
'cancelstr' => $options['cancellabel'] ?? null,
'type' => defined('single_button::BUTTON_DANGER') ? single_button::BUTTON_DANGER : null,
]);
}
/**
* Render a control menu.
*
* @param action_menu_link $actions
*/
public function control_menu($actions) {
$menu = new action_menu();
// Without this, the control menu can wrap on the next line when placed next to another item.
$menu->attributes['class'] .= ' xp-inline-block';
// Styles copied from core_courseformat\output\local\content\cm::get_action_menu() in 4.1dev.
$icon = $this->pix_icon('i/menu', get_string('menu', 'block_xp'));
$menu->set_menu_trigger($icon, 'btn btn-icon d-flex align-items-center justify-content-center after:xp-hidden');
foreach ($actions as $action) {
$action->primary = false;
$menu->add_secondary_action($action);
}
return $this->render($menu);
}
/**
* Heading with divider.
*
* @param string $text The heading text.
* @param array $options The options.
* @return string
*/
public function heading_with_divider($text, $options = []) {
$options = array_merge(['level' => 3], $options);
return html_writer::tag('div', $this->heading($text, $options['level'], 'xp-m-0'),
['class' => 'xp-mb-6 xp-mt-8 xp-border-0 xp-border-solid xp-border-t xp-border-gray-100 xp-pt-8']);
}
/**
* Get a user's picture.
*
* @param object $user The user.
* @return moodle_url The URL to the picture.
*/
public function get_user_picture($user) {
$pic = new user_picture($user);
$pic->size = 1;
return $pic->get_url($this->page);
}
/**
* Print a level's badge.
*
* @param level $level The level.
* @return string
*/
public function level_badge(level $level) {
return $this->level_badge_with_options($level);
}
/**
* Small level badge.
*
* @param level $level The level.
* @return string.
*/
public function small_level_badge(level $level) {
return $this->level_badge_with_options($level, ['small' => true]);
}
/**
* Medium level badge.
*
* @param level $level The level.
* @return string.
*/
public function medium_level_badge(level $level) {
return $this->level_badge_with_options($level, ['medium' => true]);
}
/**
* Print a level's badge.
*
* @param level $level The level.
* @param array $options The options.
* @return string
*/
protected function level_badge_with_options(level $level, array $options = []) {
$size = null;
if (!empty($options['medium'])) {
$size = 'medium';
} else if (!empty($options['small'])) {
$size = 'small';
}
$badgeurl = $level instanceof level_with_badge ? $level->get_badge_url() : null;
return $this->render_from_template('block_xp/level-badge', [
'badgeurl' => $badgeurl ? $badgeurl->out(false) : null,
'level' => $level->get_level(),
'size' => $size,
]);
}
/**
* Level name.
*
* @param level $level The level.
* @param bool $force When forced, there will always be an name displayed.
* @return string
*/
public function level_name(level $level, $force = false) {
$name = $level instanceof level_with_name ? $level->get_name() : null;
if (empty($name) && $force) {
$name = get_string('levelx', 'block_xp', $level->get_level());
}
if (empty($name)) {
return '';
}
return html_writer::tag('div', $name, ['class' => 'level-name']);
}
/**
* Levels grid.
*
* @param array $levels The levels.
* @return string
*/
public function levels_grid(array $levels) {
// If at least one level has a custom name, we will always show the name.
$alwaysshowname = array_reduce($levels, function($carry, $level) {
$name = $level instanceof \block_xp\local\xp\level_with_name ? $level->get_name() : '';
return $carry + !empty($name) ? 1 : 0;
}, 0) > 0;
$o = '';
$o .= html_writer::start_div('block_xp-level-grid');
foreach ($levels as $level) {
$desc = $level instanceof \block_xp\local\xp\level_with_description ? $level->get_description() : '';
$classes = ['block_xp-level-boxed'];
if ($desc) {
$classes[] = 'block_xp-level-boxed-with-desc';
}
$o .= html_writer::start_div(implode(' ', $classes));
$o .= html_writer::start_div('block_xp-level-box');
$o .= html_writer::start_div('block_xp-level-no');
$o .= '#' . $level->get_level();
$o .= html_writer::end_div();
$o .= html_writer::start_div();
$o .= $this->level_badge($level);
$o .= html_writer::end_div();
$o .= $this->level_name($level, $alwaysshowname);
$o .= html_writer::start_div();
$o .= $this->xp($level->get_xp_required());
$o .= html_writer::end_div();
$o .= html_writer::start_div('block_xp-level-desc');
$o .= $desc;
$o .= html_writer::end_div();
$o .= html_writer::end_div();
$o .= html_writer::end_div();
}
$o .= html_writer::end_div();
return $o;
}
/**
* Levels preview.
*
* @param level[] $levels The levels.
* @return string
*/
public function levels_preview(array $levels) {
$o = '';
$o .= html_writer::start_div('xp-grid xp-gap-2 xp-grid-cols-6 sm:xp-grid-cols-10');
foreach ($levels as $level) {
$o .= html_writer::start_div('xp-relative xp-bg-gray-100 xp-rounded xp-p-1');
$o .= html_writer::div('' . $level->get_level(), 'xp-whitespace-nowrap xp-text-center xp-mb-1 xp-absolute'
. ' xp-top-0.5 xp-left-0.5 xp-text-2xs xp-text-gray-500');
$o .= $this->small_level_badge($level);
$o .= html_writer::end_div();
}
$o .= html_writer::end_div();
return $o;
}
/**
* Return the notices.
*
* @param \block_xp\local\course_world $world The world.
* @return string The notices.
*/
public function notices(\block_xp\local\course_world $world) {
global $CFG;
$o = '';
if (!$world->get_access_permissions()->can_manage()) {
return $o;
}
$notice = null;
$candidates = [
[
static::NOTICE_FLAG_QUEST,
function() {
$questblogurl = new moodle_url('https://www.levelup.plus/blog/quest-moodle-gamification-plugin?ref=xp_notice');
$questurl = new moodle_url('https://www.levelup.plus/quest?ref=xp_notice');
return strip_tags(markdown_to_html(get_string('questreleasenotice', 'block_xp', (object) [
'questblogurl' => $questblogurl->out(false),
'questurl' => $questurl->out(false),
])), '<a><em><strong>');
},
], [
$this->noticesflag,
function() {
$moodleorgurl = new moodle_url('https://moodle.org/plugins/view.php?plugin=block_xp');
$githuburl = new moodle_url('https://github.com/FMCorz/moodle-block_xp');
return get_string('likenotice', 'block_xp', (object) [
'moodleorg' => $moodleorgurl->out(),
'github' => $githuburl->out(),
]);
},
],
];
foreach ($candidates as $candidate) {
if (!get_user_preferences($candidate[0], false)) {
$notice = $candidate;
break;
}
}
if ($notice) {
list($flag, $textfn) = $notice;
if ($CFG->branch >= 403) {
$this->page->requires->js_amd_inline("require(['core_user/repository'], function(UserRepo) {
const flag = '$flag';
const n = document.querySelector('.block-xp-rocks');
if (!n) return;
n.addEventListener('click', function(e) {
e.preventDefault();
UserRepo.setUserPreference(flag, true);
const notice = document.querySelector('.block-xp-notices');
if (!notice) return;
notice.style.display = 'none';
});
});");
} else {
require_once($CFG->libdir . '/ajax/ajaxlib.php');
user_preference_allow_ajax_update($flag, PARAM_BOOL);
$this->page->requires->js_amd_inline("require([], function() {
const flag = '$flag';
const n = document.querySelector('.block-xp-rocks');
if (!n) return;
n.addEventListener('click', function(e) {
e.preventDefault();
M.util.set_user_preference(flag, 1);
const notice = document.querySelector('.block-xp-notices');
if (!notice) return;
notice.style.display = 'none';
});
});");
}
$icon = new pix_icon('t/close', get_string('dismissnotice', 'block_xp'), 'block_xp');
$actionicon = $this->action_icon(new moodle_url($this->page->url), $icon, null, ['class' => 'block-xp-rocks']);
$text = html_writer::start_div('xp-flex xp-gap-1');
$text .= html_writer::div($textfn(), 'xp-flex-1 [&_a]:xp-font-normal [&_a]:xp-underline');
$text .= html_writer::div($actionicon, 'xp-grow-0 dismiss-action');
$text .= html_writer::end_div();
$o .= html_writer::div($this->notification_without_close($text, 'success'),
'block_xp-dismissable-notice block-xp-notices');
}
return $o;
}
/**
* Outputs the navigation.
*
* This specifically requires a course_world and not a world.
*
* @param course_world $world The world.
* @param string $page The page we are on.
* @return string The navigation.
* @deprecated Since Level Up XP 3.12, use tab_navigation instead.
*/
public function course_world_navigation(course_world $world, $page) {
debugging('The method course_world_navigation is deprecated, please use tab_navigation instead.', DEBUG_DEVELOPER);
$factory = \block_xp\di::get('course_world_navigation_factory');
$links = $factory->get_course_navigation($world);
// If there is only one page, then that is the page we are on.
if (count($links) <= 1) {
return '';
}
return $this->tab_navigation($links, $page);
}
/**
* Output a JSON script.
*
* @param mixed $data The data.
* @param string $id The HTML ID to use.
* @return string
*/
public function json_script($data, $id) {
$jsondata = json_encode($data, JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS);
return html_writer::tag('script', $jsondata, ['id' => $id, 'type' => 'application/json']);
}
/**
* Get the context of the navbar widget.
*
* @param course_world $world The world.
* @param state $state The user's state.
* @return array
*/
protected function get_navbar_widget_context(course_world $world, state $state) {
$urlresolver = \block_xp\di::get('url_resolver');
$worldconfig = $world->get_config();
$infopageurl = null;
if ($worldconfig->get('enableinfos')) {
$infopageurl = $urlresolver->reverse('infos', ['courseid' => $world->get_courseid()]);
}
$leaderboardurl = null;
if ($worldconfig->get('enableladder')) {
$leaderboardurl = $urlresolver->reverse('ladder', ['courseid' => $world->get_courseid()]);
}
$validurls = array_filter([$infopageurl, $leaderboardurl]);
$linkurl = reset($validurls) ?: null;
return [
'badgehtml' => $this->small_level_badge($state->get_level()),
'linkurl' => $linkurl ? $linkurl->out(false) : null,
'infopageurl' => $infopageurl ? $infopageurl->out(false) : null,
'leaderboardurl' => $leaderboardurl ? $leaderboardurl->out(false) : null,
];
}
/**
* Make a single button.
*
* This is mostly for the convenience of handling multiple versions.
*
* @param moodle_url $url The URL.
* @param string $text The text.
* @param array $options The options, see code.
* @return single_button
*/
public function make_single_button($url, $text, $options = []) {
$method = $options['method'] ?? 'get';
$button = new single_button($url, $text, $method);
if (!empty($options['primary'])) {
if (defined('single_button::BUTTON_PRIMARY')) {
$button->type = single_button::BUTTON_PRIMARY;
} else {
$button->primary = true;
}
} else if (!empty($options['danger'])) {
if (defined('single_button::BUTTON_DANGER')) {
$button->type = single_button::BUTTON_DANGER;
}
}
return $button;
}
/**
* Navbar widget.
*
* @param course_world $world The world.
* @param state $state The user's state.
* @return string
*/
public function navbar_widget(course_world $world, state $state) {
return $this->render_from_template('block_xp/navbar-widget', $this->get_navbar_widget_context($world, $state));
}
/**
* New dot.
*
* @return string
*/
public function new_dot() {
return html_writer::div(html_writer::tag('span', get_string('new'), ['class' => 'accesshide']), 'has-new');
}
/**
* Print a notification without a close button.
*
* @param string|lang_string $message The message.
* @param string $type The notification type.
* @return string
*/
public function notification_without_close($message, $type) {
if (class_exists('core\output\notification')) {
// Of course, it would be too easy if they didn't add and change constants
// between two releases... Who reads the upgrade.txt, seriously?
if (defined('core\output\notification::NOTIFY_INFO')) {
$info = core\output\notification::NOTIFY_INFO;
} else {
$info = core\output\notification::NOTIFY_MESSAGE;
}
if (defined('core\output\notification::NOTIFY_SUCCESS')) {
$success = core\output\notification::NOTIFY_SUCCESS;
} else {
$success = core\output\notification::NOTIFY_MESSAGE;
}
if (defined('core\output\notification::NOTIFY_WARNING')) {
$warning = core\output\notification::NOTIFY_WARNING;
} else {
$warning = core\output\notification::NOTIFY_REDIRECT;
}
if (defined('core\output\notification::NOTIFY_ERROR')) {
$error = core\output\notification::NOTIFY_ERROR;
} else {
$error = core\output\notification::NOTIFY_PROBLEM;;
}
$typemappings = [
'success' => $success,
'info' => $info,
'warning' => $warning,
'error' => $error,
'notifyproblem' => $error,
'notifytiny' => $error,
'notifyerror' => $error,
'notifysuccess' => $success,
'notifymessage' => $info,
'notifyredirect' => $info,
'redirectmessage' => $info,
];
} else {
// Old-style notifications.
$typemappings = [
'success' => 'notifysuccess',
'info' => 'notifymessage',
'warning' => 'notifyproblem',
'error' => 'notifyproblem',
'notifyproblem' => 'notifyproblem',
'notifytiny' => 'notifyproblem',
'notifyerror' => 'notifyproblem',
'notifysuccess' => 'notifysuccess',
'notifymessage' => 'notifymessage',
'notifyredirect' => 'notifyredirect',
'redirectmessage' => 'redirectmessage',
];
}
$type = $typemappings[$type];
if (class_exists('core\output\notification')) {
$notification = new \core\output\notification($message, $type);
if (method_exists($notification, 'set_show_closebutton')) {
$notification->set_show_closebutton(false);
}
if (method_exists($notification, 'set_announce')) {
$notification->set_announce(false);
}
return $this->render($notification);
}
return $this->notification($message, $type);
}
/**
* Page size selector.
*
* @param array $options Array of [(int) $perpage, (moodle_url) $url].
* @param int $current The current selectin.
* @return string
*/
public function pagesize_selector($options, $current) {
$o = '';
$o .= html_writer::start_div('xp-text-center');
$o .= html_writer::start_tag('small');
$o .= get_string('perpagecolon', 'block_xp') . ' ';
$options = array_values($options);
$lastindex = count($options) - 1;
foreach ($options as $i => $option) {
list($perpage, $url) = $option;
$o .= $current == $perpage ? $current : html_writer::link($url, (string) $perpage);
$o .= $i < $lastindex ? ' - ' : '';
}
$o .= html_writer::end_tag('small');
$o .= html_writer::end_div();
return $o;
}
/**
* Override pix_url to auto-handle deprecation.
*
* It's just simpler than having to deal with differences between
* Moodle < 3.3, and Moodle >= 3.3.
*
* @param string $image The file.
* @param string $component The component.
* @return string
*/
public function pix_url($image, $component = 'moodle') {
if (method_exists($this, 'image_url')) {
return $this->image_url($image, $component);
}
return parent::pix_url($image, $component);
}
/**
* Override render method.
*
* @param renderable $renderable The renderable.
* @param array $options Options.
* @return string
*/
public function render(renderable $renderable, $options = []) {
if ($renderable instanceof block_xp_ruleset) {
return $this->render_block_xp_ruleset($renderable, $options);
} else if ($renderable instanceof block_xp_rule) {
return $this->render_block_xp_rule($renderable, $options);
}
return parent::render($renderable);
}
/**
* Renders a block XP filter.
*
* Not very proud of the way I implement this... The HTML is tied to Javascript
* and to the rule objects themselves. Careful when changing something!
*
* @param block_xp_filter $filter The filter.
* @return string
*/
public function render_block_xp_filter($filter) {
static $i = 0;
$o = '';
$basename = 'filters[' . $i++ . ']';
$o .= html_writer::start_tag('li', ['class' => 'filter', 'data-basename' => $basename]);
if ($filter->is_editable()) {
$content = '';
$content .= html_writer::start_div('xp-flex xp-min-h-10 xp-group');
$content .= html_writer::start_div('xp-flex-none xp-h-10 xp-flex xp-items-center');
$content .= $this->render(new pix_icon('i/dragdrop', get_string('moverule', 'block_xp'), '',
['class' => 'iconsmall filter-move']));
$content .= html_writer::end_div();
$content .= html_writer::start_div('xp-flex-1 xp-overflow-hidden xp-min-h-full xp-flex'
. ' xp-items-center xp-leading-tight');
$content .= get_string('awardaxpwhen', 'block_xp',
html_writer::empty_tag('input', [
'type' => 'text',
'value' => $filter->get_points(),
'size' => 3,
'name' => $basename . '[points]',
'class' => 'form-control block_xp-form-control-inline !xp-mr-1', ])
);
$content .= html_writer::end_div();
$content .= html_writer::start_div('xp-flex-none xp-h-10 xp-flex xp-items-center');
$content .= $this->render_block_xp_rule_delete_action_link('deleterule', 'filter-delete');
$content .= html_writer::end_div();
$content .= html_writer::end_div();
$o .= html_writer::div($content, 'xp-group');
$o .= html_writer::empty_tag('input', [
'type' => 'hidden',
'value' => $filter->get_id(),
'name' => $basename . '[id]', ]);
$o .= html_writer::empty_tag('input', [
'type' => 'hidden',
'value' => $filter->get_sortorder(),
'name' => $basename . '[sortorder]', ]);
$basename .= '[rule]';
} else {
$o .= html_writer::tag('p', get_string('awardaxpwhen', 'block_xp', $filter->get_points()));
}
$o .= html_writer::start_tag('ul', ['class' => 'filter-rules']);
$o .= $this->render($filter->get_rule(), ['iseditable' => $filter->is_editable(), 'basename' => $basename]);
$o .= html_writer::end_tag('ul');
$o .= html_writer::end_tag('li');
return $o;
}
/**
* Renders a block XP rule.
*
* @param block_xp_rule_base $rule The rule.
* @param array $options
* @return string
*/
public function render_block_xp_rule($rule, $options) {
static $i = 0;
$iseditable = !empty($options['iseditable']);
$basename = isset($options['basename']) ? $options['basename'] : '';
if ($iseditable) {
$content = '';
$content .= html_writer::start_div('xp-flex xp-min-h-10');
$content .= html_writer::start_div('xp-flex-none xp-h-10 xp-flex xp-items-center');
$content .= $this->render(new pix_icon('i/dragdrop', get_string('movecondition', 'block_xp'), '',
['class' => 'iconsmall rule-move']));
$content .= html_writer::end_div();
$content .= html_writer::start_div('xp-flex-1 xp-overflow-hidden xp-min-h-full xp-flex xp-items-center'
. ' xp-leading-tight');
$content .= html_writer::div($rule->get_form($basename), 'xp-w-full xp-max-w-full');
$content .= html_writer::end_div();
$content .= html_writer::start_div('xp-flex-none xp-h-10 xp-flex xp-items-center');
$content .= $this->render_block_xp_rule_delete_action_link('deletecondition', 'rule-delete');
$content .= html_writer::end_div();
$content .= html_writer::end_div();
} else {
$content = s($rule->get_description());
}
$o = '';
$o .= html_writer::start_tag('li', ['class' => 'rule rule-type-rule xp-group']);
$o .= html_writer::tag('div', $content, ['class' => 'rule-definition', 'data-basename' => $basename]);
$o .= html_writer::end_tag('li');
return $o;
}
/**
* Renders a block XP ruleset.
*
* @param block_xp_ruleset $ruleset The ruleset.
* @param array $options The options
* @return string
*/
public function render_block_xp_ruleset($ruleset, $options) {
static $i = 0;
$iseditable = !empty($options['iseditable']);
$basename = isset($options['basename']) ? $options['basename'] : '';
$o = '';
$o .= html_writer::start_tag('li', ['class' => 'rule rule-type-ruleset']);
if ($iseditable) {
$content = '';
$content .= html_writer::start_div('xp-flex xp-min-h-10');
$content .= html_writer::start_div('xp-flex-none xp-h-10 xp-flex xp-items-center');
$content .= $this->render(new pix_icon('i/dragdrop', get_string('movecondition', 'block_xp'), '',
['class' => 'iconsmall rule-move']));
$content .= html_writer::end_div();
$content .= html_writer::start_div('xp-flex-1 xp-overflow-hidden xp-min-h-full xp-flex'
. ' xp-items-center xp-leading-tight');
$content .= html_writer::div($ruleset->get_form($basename));
$content .= html_writer::end_div();
$content .= html_writer::start_div('xp-flex-none xp-h-10 xp-flex xp-items-center');
$content .= $this->render_block_xp_rule_delete_action_link('deletecondition', 'rule-delete');
$content .= html_writer::end_div();
$content .= html_writer::end_div();
} else {
$content = s($ruleset->get_description());
}
$o .= html_writer::tag('div', $content, ['class' => 'rule-definition xp-h-10 xp-group', 'data-basename' => $basename]);
$o .= html_writer::start_tag('ul', ['class' => 'rule-rules', 'data-basename' => $basename . '[rules]']);
foreach ($ruleset->get_rules() as $rule) {
if ($iseditable) {
$options['basename'] = $basename . '[rules][' . $i++ . ']';
}
$o .= $this->render($rule, $options);
}
if ($iseditable) {
$o .= html_writer::start_tag('li', ['class' => 'rule-add']);
$o .= $this->action_link('#', get_string('addacondition', 'block_xp'), null, null,
new pix_icon('t/add', '', '', ['class' => 'iconsmall']));
$o .= html_writer::end_tag('li');
}
$o .= html_writer::end_tag('ul');
$o .= html_writer::end_tag('li');
return $o;
}
/**
* Render the delete action icon in rules.
*
* @param string $str The string identifier for the title.
* @param string $classname Classes to add to the action link.
* @return string
*/
public function render_block_xp_rule_delete_action_link($str, $classname = '') {
$icon = new pix_icon('t/delete', get_string($str, 'block_xp'), '', ['class' => 'iconsmall']);
return $this->action_link('#', '', null, [
'class' => $classname . ' supports-hover:xp-opacity-0 supports-hover:xp-pointer-events-none'
. ' focus:xp-opacity-100 focus:xp-pointer-events-auto'
. ' group-hover:xp-opacity-100 group-hover:xp-pointer-events-auto xp-transition-opacity',
], $icon);
}
/**
* Render a dismissable notice.
*
* Yes, we cannot use CSS IDs in there because they are stripped out... turns out they
* are considered dangerous. Oh well, we use a class instead. Not pretty, but it works...
*
* @param renderable $notice The notice.
* @return string
*/
public function render_dismissable_notice(renderable $notice) {
$id = html_writer::random_id();
// Tell the indicator that it should be expecing this notice.
$indicator = \block_xp\di::get('user_notice_indicator');
if ($indicator instanceof \block_xp\local\indicator\user_indicator_with_acceptance) {
$indicator->set_acceptable_user_flag($notice->name);
}
$url = \block_xp\di::get('ajax_url_resolver')->reverse('notice/dismiss', ['name' => $notice->name]);
$this->page->requires->js_init_call(<<<EOT
Y.one('.$id .dismiss-action a').on('click', function(e) {
e.preventDefault();
Y.one('.$id').hide();
var url = '$url';
var cfg = {
method: 'POST'
};
Y.io(url, cfg);
});
EOT
);
$icon = new pix_icon('t/close', get_string('dismissnotice', 'block_xp'), 'block_xp');
$actionicon = $this->action_icon('#', $icon, null);
$text = html_writer::div($actionicon, 'dismiss-action') . $notice->message;
return html_writer::div($this->notification_without_close($text, $notice->type),
'block_xp-dismissable-notice ' . $id);
}
/**
* Render the filters widget.
*
* /!\ We only support one editable widget per page!
*
* @param renderable $widget The widget.
* @return string
*/
public function render_filters_widget(renderable $widget) {
$containerid = html_writer::random_id();
if ($widget->editable) {
$templatefilter = $this->render($widget->filter);
$templatetypes = [];
foreach ($widget->rules as $rule) {
$templatetypes[] = [
'name' => $rule->name,
'info' => !empty($rule->info) ? $rule->info : null,
'template' => $this->render($rule->rule, ['iseditable' => true, 'basename' => 'XXXXX']),
];
}
// Prepare Javascript.
$this->page->requires->yui_module('moodle-block_xp-filters', 'Y.M.block_xp.Filters.init', [[
'containerSelector' => '#' . $containerid,
'filter' => $templatefilter,
'rules' => $templatetypes,
], ]);
$this->page->requires->strings_for_js(['pickaconditiontype', 'deleterule', 'deletecondition'], 'block_xp');
$this->page->requires->strings_for_js(['areyousure'], 'core');
}
echo html_writer::start_div('block-xp-filters-wrapper', ['id' => $containerid]);
$addlink = '';
if ($widget->editable) {
$addlink = html_writer::start_tag('li', ['class' => 'filter-add']);
$addlink .= $this->action_link('#', get_string('addarule', 'block_xp'), null, null,
new pix_icon('t/add', '', '', ['class' => 'iconsmall']));
$addlink .= html_writer::end_tag('li');
}
$class = $widget->editable ? 'filters-editable' : 'filters-readonly';
echo html_writer::start_tag('ul', ['class' => 'filters-list ' . $class]);
echo $addlink;
foreach ($widget->filters as $filter) {
echo $this->render($filter);
echo $addlink;
}
echo html_writer::end_tag('ul');
echo html_writer::end_div();
}
/**
* Render the filters widget group.
*
* @param renderable $element The group.
* @return string
*/
public function render_filters_widget_element(renderable $element) {
if (!empty($element->title)) {
$title = $element->title . ($element->helpicon ? $this->render($element->helpicon) : '');
echo html_writer::tag('h4', $title);
if (!empty($element->description)) {
echo $element->description;
}
}
$this->render($element->widget);
}
/**
* Render the filters widget group.
*
* @param renderable $group The group.
* @return string
*/
public function render_filters_widget_group(renderable $group) {
global $CFG;