1 |
efrain |
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 |
/**
|
|
|
18 |
* Renderable component for multiblocks.
|
|
|
19 |
*
|
|
|
20 |
* @package block_multiblock
|
|
|
21 |
* @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
namespace block_multiblock\output;
|
|
|
25 |
|
|
|
26 |
use renderable;
|
|
|
27 |
use renderer_base;
|
|
|
28 |
use templatable;
|
|
|
29 |
use block_multiblock;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Renderable component for multiblocks.
|
|
|
33 |
*
|
|
|
34 |
* @package block_multiblock
|
|
|
35 |
* @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class main implements renderable, templatable {
|
|
|
39 |
/** @var int The id of the multiblock itself. */
|
|
|
40 |
private $multiblockid;
|
|
|
41 |
|
|
|
42 |
/** @var array The instances of subblocks within this block to be rendered. */
|
|
|
43 |
private $multiblock;
|
|
|
44 |
|
|
|
45 |
/** @var string The template that we're going to pass to Mustache. */
|
|
|
46 |
private $template;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Initialises the multiblock render helper.
|
|
|
50 |
*
|
|
|
51 |
* @param int $blockid The id of the multiblock itself.
|
|
|
52 |
* @param array $multiblock The instances of subblocks within this block to be rendered.
|
|
|
53 |
* @param string $template The template that we're going to pass to Mustache.
|
|
|
54 |
*/
|
|
|
55 |
public function __construct(int $blockid, array $multiblock, string $template) {
|
|
|
56 |
$this->multiblockid = $blockid;
|
|
|
57 |
$this->multiblock = $multiblock;
|
|
|
58 |
$this->template = $template;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Get the template to be rendered for the given configured presentation of this block.
|
|
|
63 |
*
|
|
|
64 |
* @return string The template to be rendered.
|
|
|
65 |
*/
|
|
|
66 |
public function get_template(): string {
|
|
|
67 |
$presentations = block_multiblock::get_valid_presentations();
|
|
|
68 |
$presentation = isset($presentations[$this->template]) ? $this->template : block_multiblock::get_default_presentation();
|
|
|
69 |
return $presentations[$presentation]->get_template();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Export this data so it can be used as the context for a Mustache template.
|
|
|
74 |
*
|
|
|
75 |
* @param \renderer_base $output
|
|
|
76 |
* @return stdClass
|
|
|
77 |
*/
|
|
|
78 |
public function export_for_template(renderer_base $output) {
|
|
|
79 |
$context = [
|
|
|
80 |
'multiblockid' => $this->multiblockid,
|
|
|
81 |
'multiblock' => $this->multiblock,
|
|
|
82 |
'template' => $this->template,
|
|
|
83 |
];
|
|
|
84 |
|
|
|
85 |
$first = true;
|
|
|
86 |
foreach ($context['multiblock'] as $id => $block) {
|
|
|
87 |
$context['multiblock'][$id]['active'] = $first;
|
|
|
88 |
$first = false;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
return (object) $context;
|
|
|
92 |
}
|
|
|
93 |
}
|