Proyectos de Subversion Moodle

Rev

Rev 4 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4 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
/**
18
 * Point of View block definition
19
 *
20
 * @package    block_point_view
21
 * @copyright  2020 Quentin Fombaron, 2021 Astor Bizard
22
 * @author     Quentin Fombaron <q.fombaron@outlook.fr>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
/**
27
 * block_point_view Class
28
 *
29
 * @package    block_point_view
30
 * @copyright  2020 Quentin Fombaron, 2021 Astor Bizard
31
 * @author     Quentin Fombaron <q.fombaron@outlook.fr>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class block_point_view extends block_base {
35
    /**
36
     * Block initialization
37
     */
38
    public function init() {
39
 
40
        $this->title = get_string('pluginname', 'block_point_view');
41
 
42
    }
43
 
44
    /**
45
     * We have global config/settings data.
46
     * @return boolean
47
     */
48
    public function has_config() {
49
        return true;
50
    }
51
 
52
    /**
53
     * Enable to add the block only in a course.
54
     *
55
     * @return array
56
     */
57
    public function applicable_formats() {
58
        return [
59
            'course-view' => true,
60
        ];
61
    }
62
 
63
    /**
64
     * Build block content.
65
     * @return Object
66
     */
67
    public function get_content() {
68
        global $CFG, $COURSE;
69
 
70
        if ($this->content !== null) {
71
            // Content has already been built, do not rebuild.
72
            return $this->content;
73
        }
74
 
75
        $this->content = new stdClass();
76
        $this->content->footer = '';
77
 
78
        if (get_config('block_point_view', 'enable_point_views_admin')) {
79
 
80
            // Block text content.
81
            if (isset($this->config->text)) {
82
                $this->config->text = file_rewrite_pluginfile_urls(
83
                    $this->config->text,
84
                    'pluginfile.php',
85
                    $this->context->id,
86
                    'block_point_view',
87
                    'content',
88
                    null
89
                );
90
                $format = FORMAT_HTML;
91
                if (isset($this->config->format)) {
92
                    $format = $this->config->format;
93
                }
94
                $filteropt = new stdClass;
95
                if ($this->content_is_trusted()) {
96
                    $filteropt->noclean = true;
97
                }
98
                $this->content->text = format_text($this->config->text, $format, $filteropt);
99
                unset($filteropt);
100
            } else {
101
                $this->content->text = '<span>'.get_string('defaulttextcontent', 'block_point_view').'</span>';
102
            }
103
 
104
            // Link to overview.
105
            if (has_capability('block/point_view:access_overview', $this->context)) {
106
                $parameters = [
107
                    'instanceid' => $this->instance->id,
108
                    'contextid' => $this->context->id,
5 ariadna 109
                    'courseid' => $COURSE->id
4 ariadna 110
                ];
111
 
112
                $url = new moodle_url('/blocks/point_view/overview.php', $parameters);
113
                $title = get_string('reactionsdetails', 'block_point_view');
114
                $pix = $CFG->wwwroot . '/blocks/point_view/pix/overview.png';
115
 
116
                $this->content->text .= html_writer::link(
117
                        $url,
118
                        html_writer::empty_tag('img', [
119
                                'src' => $pix,
120
                                'alt' => $title,
121
                                'class' => 'overview-link d-block mx-auto text-center',
122
                        ]),
123
                        [ 'title' => $title ]
124
                );
125
            }
126
 
127
        } else if (has_capability('block/point_view:addinstance', $this->context)) {
128
 
129
            $this->content->text = get_string('blockdisabled', 'block_point_view');
130
 
131
        }
132
 
133
        return $this->content;
134
    }
135
 
136
    /**
137
     * Is content trusted
138
     *
139
     * @return bool
140
     */
141
    public function content_is_trusted() {
142
        global $SCRIPT;
143
 
144
        if (!$context = context::instance_by_id($this->instance->parentcontextid, IGNORE_MISSING)) {
145
            return false;
146
        }
147
        if ($context->contextlevel == CONTEXT_USER) {
148
            if ($SCRIPT === '/my/index.php') {
149
                return true;
150
            } else {
151
                return false;
152
            }
153
        }
154
 
155
        return true;
156
    }
157
 
158
    /**
159
     * {@inheritDoc}
160
     * @see block_base::get_required_javascript()
161
     */
162
    public function get_required_javascript() {
163
        parent::get_required_javascript();
164
 
165
        global $USER, $COURSE;
166
        if (get_config('block_point_view', 'enable_point_views_admin')
167
                && (!empty($this->config->enable_point_views) || !empty($this->config->enable_difficultytracks))
5 ariadna 168
                && !$this->page->user_is_editing()) {
4 ariadna 169
 
5 ariadna 170
            require_once(__DIR__ . '/locallib.php');
171
 
4 ariadna 172
            // Build data for javascript.
173
            $blockdata = new stdClass();
174
            $blockdata->trackcolors = block_point_view_get_track_colors();
175
            $blockdata->moduleswithreactions = block_point_view_get_modules_with_reactions($this, $USER->id, $COURSE->id);
176
            $blockdata->difficultylevels = block_point_view_get_difficulty_levels($this, $COURSE->id);
177
            $blockdata->pix = block_point_view_get_current_pix($this);
178
 
179
            global $OUTPUT;
180
            $templatecontext = new stdClass();
181
            $templatecontext->reactions = [];
182
            foreach ([ 'easy', 'better', 'hard' ] as $reactionname) {
183
                $reaction = new stdClass();
184
                $reaction->name = $reactionname;
185
                $reaction->pix = $blockdata->pix[$reactionname];
186
                $reaction->text = block_point_view_get_reaction_text($this, $reactionname);
187
                $templatecontext->reactions[] = $reaction;
188
            }
189
            $blockdata->reactionstemplate = $OUTPUT->render_from_template('block_point_view/reactions', $templatecontext);
190
 
191
            // Create and place a node containing data for the javascript.
192
            $datanode = html_writer::span('', 'block_point_view', [
193
                    'data-blockdata' => json_encode($blockdata),
194
                    'style' => 'display:none;',
195
            ]);
196
 
197
            if (($this->config->highlight_activity_rows ?? true)) {
198
                // Add shade on hover of a course module.
199
                $cssnode = '<style type="text/css">
200
                                .activity:hover{
201
                                    background:linear-gradient(to right,rgba(0,0,0,0.04),rgba(0,0,0,0.04),transparent);
202
                                    border-radius:5px;
203
                                }
204
                            </style>';
205
            } else {
206
                $cssnode = '';
207
            }
208
 
5 ariadna 209
            $this->page->requires->js_init_code('document.getElementsByClassName("course-content")[0]
4 ariadna 210
                                                 .insertAdjacentHTML("beforeend", "' . addslashes_js($datanode . $cssnode) . '");');
211
 
212
            $strings = [ 'totalreactions', 'greentrack', 'bluetrack', 'redtrack', 'blacktrack' ];
213
            $this->page->requires->strings_for_js($strings, 'block_point_view');
214
            $this->page->requires->js_call_amd('block_point_view/script_point_view', 'init', [ $COURSE->id ]);
215
        }
216
    }
217
 
218
    /**
219
     * Serialize and store config data
220
     *
221
     * Save data from file manager when user is saving configuration.
222
     * Delete file storage if user disable custom emojis.
223
     *
224
     * @param mixed $data
225
     * @param mixed $nolongerused
226
     */
227
    public function instance_config_save($data, $nolongerused = false) {
228
 
229
        $config = clone($data);
230
 
231
        $config->text = file_save_draft_area_files(
232
            $data->text['itemid'],
233
            $this->context->id,
234
            'block_point_view',
235
            'content',
236
            0,
237
            [ 'subdirs' => true ],
238
            $data->text['text']
239
        );
240
 
241
        $config->format = $data->text['format'];
242
 
243
        if ($config->pixselect == 'custom') {
244
            $config->point_views_pix = file_save_draft_area_files(
245
                $data->point_views_pix,
246
                $this->context->id,
247
                'block_point_view',
248
                'point_views_pix',
249
 
250
            );
251
        }
252
 
253
        parent::instance_config_save($config, $nolongerused);
254
 
255
    }
256
 
257
    /**
258
     * {@inheritDoc}
259
     * @see block_base::instance_config_commit()
260
     * @param mixed $nolongerused
261
     */
262
    public function instance_config_commit($nolongerused = false) {
263
        // Do not touch any files if this is a commit from somewhere else.
264
        parent::instance_config_save($this->config);
265
    }
266
 
267
    /**
268
     * Delete file storage.
269
     *
270
     * @return bool
271
     */
272
    public function instance_delete() {
273
 
274
        $fs = get_file_storage();
275
 
276
        $fs->delete_area_files($this->context->id, 'block_point_view');
277
 
278
        return true;
279
 
280
    }
281
 
282
    /**
283
     * Copy any block-specific data when copying to a new block instance.
284
     *
285
     * @param int $fromid the id number of the block instance to copy from
286
     * @return boolean
287
     */
288
    public function instance_copy($fromid) {
289
 
290
        $fromcontext = context_block::instance($fromid);
291
 
292
        $fs = get_file_storage();
293
 
294
        if (!$fs->is_area_empty($fromcontext->id, 'block_point_view', 'content', 0, false)) {
295
 
296
            $draftitemid = 0;
297
 
298
            file_prepare_draft_area(
299
                $draftitemid,
300
                $fromcontext->id,
301
                'block_point_view',
302
                'content',
303
                0,
304
                [ 'subdirs' => true ]
305
            );
306
 
307
            file_save_draft_area_files(
308
                $draftitemid,
309
                $this->context->id,
310
                'block_point_view',
311
                'content',
312
                0,
313
                [ 'subdirs' => true ]
314
            );
315
 
316
        }
317
 
318
        if (!$fs->is_area_empty($fromcontext->id, 'block_point_view', 'point_views_pix', 0, false)) {
319
 
320
            $draftitemid = 0;
321
 
322
            file_prepare_draft_area(
323
                $draftitemid,
324
                $fromcontext->id,
325
                'block_point_view',
326
                'point_views_pix',
327
                0,
328
                [ 'subdirs' => true ]
329
            );
330
 
331
            file_save_draft_area_files(
332
                $draftitemid,
333
                $this->context->id,
334
                'block_point_view',
335
                'point_views_pix',
336
                0,
337
                [ 'subdirs' => true ]
338
            );
339
 
340
        }
341
 
342
        return true;
343
 
344
    }
345
}