Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Helper class for creating block instance content.
19
 *
20
 * @package    block_dash
21
 * @copyright  2019 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\local;
26
 
27
use block_dash\local\configuration\configuration_interface;
28
use block_dash\local\configuration\configuration;
29
use block_dash\output\query_debug;
30
use block_dash\output\renderer;
31
use html_writer;
32
 
33
/**
34
 * Helper class for creating block instance content.
35
 *
36
 * @package block_dash
37
 */
38
class block_builder {
39
 
40
    /**
41
     * @var configuration_interface
42
     */
43
    private $configuration;
44
 
45
    /**
46
     * @var \block_base
47
     */
48
    private $blockinstance;
49
 
50
    /**
51
     * block_builder constructor.
52
     *
53
     * @param \block_base $blockinstance
54
     * @throws \coding_exception
55
     */
56
    protected function __construct(\block_base $blockinstance) {
57
        $this->blockinstance = $blockinstance;
58
        $this->configuration = configuration::create_from_instance($blockinstance);
59
    }
60
 
61
    /**
62
     * Get configuration.
63
     *
64
     * @return configuration_interface
65
     */
66
    public function get_configuration() {
67
        return $this->configuration;
68
    }
69
 
70
    /**
71
     * Verify the datasource is collapsible addon.
72
     *
73
     * @param bool $checksection
74
     * @return bool
75
     */
76
    public function is_collapsible_content_addon($checksection = false) {
77
 
78
        if ($this->blockinstance->page->course->id != SITEID) {
79
            $format = course_get_format($this->blockinstance->page->course->id);
80
            $course = $format->get_course();
81
            if (isset($this->blockinstance->config->data_source_idnumber) && $this->blockinstance->page->user_is_editing() &&
82
                $this->blockinstance->config->data_source_idnumber == 'dashaddon_content\local\block_dash\content_customtype') {
83
                return true;
84
            }
85
        }
86
        return false;
87
    }
88
 
89
    /**
90
     * Confirm the block is configured to display only for the section.
91
     *
92
     * @return bool
93
     */
94
    public function is_section_expand_content_addon() {
95
        if ($this->is_collapsible_content_addon()) {
96
            $currentsection = optional_param('section', 0, PARAM_INT);
97
            if (isset($this->blockinstance->config->preferences)) {
98
                $preferneces = $this->blockinstance->config->preferences;
99
                if (isset($preferneces['filters'])) {
100
                    $restrictedsections = isset($preferneces['filters']['sectiondisplay']['sections']) ?
101
                        $preferneces['filters']['sectiondisplay']['sections'] : [];
102
                    if (in_array((int)$currentsection, $restrictedsections)) {
103
                        return true;
104
                    }
105
                }
106
            }
107
        }
108
        return false;
109
    }
110
 
111
    /**
112
     * Get content object for block instance.
113
     *
114
     * @return \stdClass
115
     * @throws \coding_exception
116
     * @throws \moodle_exception
117
     */
118
    public function get_block_content() {
119
 
120
        global $OUTPUT, $CFG;
121
 
122
        /** @var renderer $renderer */
123
        $renderer = $this->blockinstance->page->get_renderer('block_dash');
124
 
125
        $text = '';
126
        $editing = ($this->blockinstance->page->user_is_editing() &&
127
            has_capability('block/dash:addinstance', $this->blockinstance->context));
128
 
129
        $data = [
130
            'block_instance_id' => $this->blockinstance->instance->id,
131
            'block_context_id' => $this->blockinstance->context->id,
132
            'editing' => $editing,
133
            'istotara' => block_dash_is_totara(),
134
            'pagelayout' => $this->blockinstance->page->pagelayout,
135
            'pagecontext' => $this->blockinstance->page->context->id,
136
            'collapseaction' => $this->is_collapsible_content_addon(),
137
            'showcollapseblock' => $this->is_section_expand_content_addon(),
138
        ];
139
 
140
        if ($this->configuration->is_fully_configured()) {
141
            $bb = self::create($this->blockinstance);
142
 
143
            $supportsdebug = false;
144
            $prefernece = true;
145
            $bb->get_configuration()->get_data_source()->get_paginator()->set_current_page(0);
146
            if ($bb->get_configuration()->get_data_source()->is_widget()) {
147
                $source = $bb->get_configuration()->get_data_source();
148
                $preload = $renderer->render_data_source($source);
149
            } else {
150
                $supportsdebug = true;
151
                $source = $bb->get_configuration()->get_data_source();
152
                $preload = $renderer->render_data_source($source);
153
            }
154
 
155
            $editing = ($this->blockinstance->page->user_is_editing() &&
156
                has_capability('block/dash:addinstance', $this->blockinstance->context) && $prefernece);
157
            $data += [
158
                'preloaded' => $preload,
159
                'editing' => $editing,
160
            ];
161
            if (isset($this->blockinstance->config->header_content)) {
162
                $data['header_content'] = format_text($this->blockinstance->config->header_content['text'],
163
                        $this->blockinstance->config->header_content['format'], ['noclean' => true]);
164
            }
165
 
166
            if (isset($this->blockinstance->config->footer_content)) {
167
                $data['footer_content'] = format_text($this->blockinstance->config->footer_content['text'],
168
                    $this->blockinstance->config->footer_content['format'], ['noclean' => true]);
169
            }
170
 
171
            $source->update_data_before_render($data);
172
 
173
            $text .= $OUTPUT->render_from_template('block_dash/block', $data);
174
 
175
            if (is_siteadmin() && $supportsdebug && $CFG->debug > 0) {
176
                [$sql, $params] = $bb->get_configuration()->get_data_source()->get_query()->get_sql_and_params();
177
                $text .= $renderer->render(new query_debug($sql, $params));
178
            }
179
        } else {
180
            // @codingStandardsIgnoreStart
181
            // Ignore the phplint due to block class not allowed to include the PAGE global variable.
182
            if ($this->blockinstance->page->user_is_editing()) {
183
                // @codingStandardsIgnoreEnd
184
                require_once($CFG->dirroot.'/blocks/edit_form.php');
185
                require_once($CFG->dirroot.'/blocks/dash/edit_form.php');
186
 
187
                $form = new \block_dash_featuresform(null, ['block' => $this->blockinstance->context]);
188
 
189
                $desc = html_writer::tag('p', get_string('choosefeature', 'block_dash'));
190
                $data['preloaded'] = html_writer::tag('div',
191
                    $desc.$form->render(), ['class' => 'dash-configuration-form hide']);
192
                $text .= $OUTPUT->render_from_template('block_dash/block', $data);
193
            } else {
194
                $text .= \html_writer::tag('p', get_string('editthisblock', 'block_dash'));
195
            }
196
        }
197
 
198
        $content = new \stdClass();
199
        $content->text = $text;
200
 
201
        return $content;
202
    }
203
 
204
    /**
205
     * Create block builder.
206
     *
207
     * @param \block_base $blockinstance
208
     * @return block_builder
209
     * @throws \coding_exception
210
     */
211
    public static function create(\block_base $blockinstance) {
212
        return new block_builder($blockinstance);
213
    }
214
 
215
}