Proyectos de Subversion Moodle

Rev

Rev 5 | | 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,
6 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
 
6 ariadna 127
            if ($this->page->cm !== null
128
                    && (!empty($this->config->enable_point_views) || !empty($this->config->enable_difficultytracks))
129
                    && has_capability('moodle/block:edit', $this->context)) {
130
                $this->content->text .= html_writer::div(
131
                        get_string('forthismodule', 'block_point_view', $this->page->cm->get_formatted_name()), 'mt-3 mb-2');
132
                $moduleform = new block_point_view\module_form(new moodle_url('/blocks/point_view/action/updatemodule.php'),
133
                        [
134
                                'cmid' => $this->page->cm->id,
135
                                'blockconfig' => $this->config,
136
                                'blockinstanceid' => $this->instance->id,
137
                                'returnurl' => $this->page->url,
138
                        ]);
139
                $moduleform->set_data([
140
                        'enablereactions' => ($this->config->{'moduleselectm' . $this->page->cm->id} ?? 0) > 0 ? 1 : 0,
141
                        'difficultytrack' => $this->config->{'difficulty_' . $this->page->cm->id} ?? 0,
142
                ]);
143
                $this->content->text .= $moduleform->render();
144
            }
145
 
146
            if ((!$this->instance->showinsubcontexts || !preg_match('/^(?:mod-)?\\*$/', $this->instance->pagetypepattern))
147
                    && has_capability('moodle/block:edit', $this->context)) {
148
                $this->content->text .=
149
                    '<div>
150
                        <i class="fa fa-info-circle text-info mr-1"></i>' .
151
                        get_string('blockonlyonmainpage', 'block_point_view') .
152
                    '</div>
153
                    <form method="post" action="' . $CFG->wwwroot . '/blocks/point_view/action/showinsubcontexts.php">
154
                        <input name="sesskey" type="hidden" value="' . sesskey() . '">
155
                        <input name="blockinstanceid" type="hidden" value="' . $this->instance->id . '">
156
                        <input name="returnurl" type="hidden" value="' . $this->page->url . '">
157
                        <input id="block_point_view_showinsubcontexts" type="submit"
158
                            value="' . get_string('showinsubcontexts', 'block_point_view') . '" class="btn btn-link p-0">
159
                    </form>';
160
                $this->page->requires->event_handler('#block_point_view_showinsubcontexts', 'click', 'M.util.show_confirm_dialog',
161
                        [ 'message' => get_string('confirmshowinsubcontexts', 'block_point_view') ]);
162
            }
163
 
4 ariadna 164
        } else if (has_capability('block/point_view:addinstance', $this->context)) {
165
 
166
            $this->content->text = get_string('blockdisabled', 'block_point_view');
167
 
168
        }
169
 
170
        return $this->content;
171
    }
172
 
173
    /**
174
     * Is content trusted
175
     *
176
     * @return bool
177
     */
178
    public function content_is_trusted() {
179
        global $SCRIPT;
180
 
181
        if (!$context = context::instance_by_id($this->instance->parentcontextid, IGNORE_MISSING)) {
182
            return false;
183
        }
184
        if ($context->contextlevel == CONTEXT_USER) {
185
            if ($SCRIPT === '/my/index.php') {
186
                return true;
187
            } else {
188
                return false;
189
            }
190
        }
191
 
192
        return true;
193
    }
194
 
195
    /**
196
     * {@inheritDoc}
197
     * @see block_base::get_required_javascript()
198
     */
199
    public function get_required_javascript() {
200
        parent::get_required_javascript();
201
 
6 ariadna 202
        require_once(__DIR__ . '/locallib.php');
203
 
4 ariadna 204
        global $USER, $COURSE;
205
        if (get_config('block_point_view', 'enable_point_views_admin')
206
                && (!empty($this->config->enable_point_views) || !empty($this->config->enable_difficultytracks))
6 ariadna 207
                && (!$this->page->user_is_editing() || $this->page->cm !== null)) {
4 ariadna 208
 
209
            // Build data for javascript.
210
            $blockdata = new stdClass();
211
            $blockdata->trackcolors = block_point_view_get_track_colors();
212
            $blockdata->moduleswithreactions = block_point_view_get_modules_with_reactions($this, $USER->id, $COURSE->id);
213
            $blockdata->difficultylevels = block_point_view_get_difficulty_levels($this, $COURSE->id);
214
            $blockdata->pix = block_point_view_get_current_pix($this);
215
 
216
            global $OUTPUT;
217
            $templatecontext = new stdClass();
218
            $templatecontext->reactions = [];
219
            foreach ([ 'easy', 'better', 'hard' ] as $reactionname) {
220
                $reaction = new stdClass();
221
                $reaction->name = $reactionname;
222
                $reaction->pix = $blockdata->pix[$reactionname];
223
                $reaction->text = block_point_view_get_reaction_text($this, $reactionname);
224
                $templatecontext->reactions[] = $reaction;
225
            }
226
            $blockdata->reactionstemplate = $OUTPUT->render_from_template('block_point_view/reactions', $templatecontext);
227
 
228
            // Create and place a node containing data for the javascript.
229
            $datanode = html_writer::span('', 'block_point_view', [
230
                    'data-blockdata' => json_encode($blockdata),
231
                    'style' => 'display:none;',
232
            ]);
233
 
234
            if (($this->config->highlight_activity_rows ?? true)) {
235
                // Add shade on hover of a course module.
236
                $cssnode = '<style type="text/css">
237
                                .activity:hover{
238
                                    background:linear-gradient(to right,rgba(0,0,0,0.04),rgba(0,0,0,0.04),transparent);
239
                                    border-radius:5px;
240
                                }
241
                            </style>';
242
            } else {
243
                $cssnode = '';
244
            }
245
 
6 ariadna 246
            $this->page->requires->js_init_code('document.querySelectorAll(".course-content, #page")[0]
4 ariadna 247
                                                 .insertAdjacentHTML("beforeend", "' . addslashes_js($datanode . $cssnode) . '");');
248
 
249
            $strings = [ 'totalreactions', 'greentrack', 'bluetrack', 'redtrack', 'blacktrack' ];
250
            $this->page->requires->strings_for_js($strings, 'block_point_view');
251
            $this->page->requires->js_call_amd('block_point_view/script_point_view', 'init', [ $COURSE->id ]);
252
        }
6 ariadna 253
 
254
        if (get_config('block_point_view', 'enable_point_views_admin') && $this->page->cm !== null) {
255
            $this->page->requires->js_call_amd('block_point_view/script_config_point_view',
256
                    'setupDifficultyTrackChange', [ block_point_view_get_track_colors() ]);
257
        }
4 ariadna 258
    }
259
 
260
    /**
261
     * Serialize and store config data
262
     *
263
     * Save data from file manager when user is saving configuration.
264
     * Delete file storage if user disable custom emojis.
265
     *
266
     * @param mixed $data
267
     * @param mixed $nolongerused
268
     */
269
    public function instance_config_save($data, $nolongerused = false) {
270
 
271
        $config = clone($data);
272
 
273
        $config->text = file_save_draft_area_files(
274
            $data->text['itemid'],
275
            $this->context->id,
276
            'block_point_view',
277
            'content',
278
            0,
279
            [ 'subdirs' => true ],
280
            $data->text['text']
281
        );
282
 
283
        $config->format = $data->text['format'];
284
 
285
        if ($config->pixselect == 'custom') {
286
            $config->point_views_pix = file_save_draft_area_files(
287
                $data->point_views_pix,
288
                $this->context->id,
289
                'block_point_view',
290
                'point_views_pix',
291
 
292
            );
293
        }
294
 
295
        parent::instance_config_save($config, $nolongerused);
296
 
297
    }
298
 
299
    /**
300
     * {@inheritDoc}
301
     * @see block_base::instance_config_commit()
302
     * @param mixed $nolongerused
303
     */
304
    public function instance_config_commit($nolongerused = false) {
305
        // Do not touch any files if this is a commit from somewhere else.
306
        parent::instance_config_save($this->config);
307
    }
308
 
309
    /**
6 ariadna 310
     * {@inheritDoc}
311
     * @see block_base::instance_create()
312
     */
313
    public function instance_create() {
314
        require_once(__DIR__ . '/locallib.php');
315
        // Show the block in subcontexts (we need it to be present on activities pages).
316
        block_point_view_show_in_subcontexts($this->instance->id);
317
        return true;
318
    }
319
 
320
    /**
4 ariadna 321
     * Delete file storage.
322
     *
323
     * @return bool
324
     */
325
    public function instance_delete() {
326
 
327
        $fs = get_file_storage();
328
 
329
        $fs->delete_area_files($this->context->id, 'block_point_view');
330
 
331
        return true;
332
 
333
    }
334
 
335
    /**
336
     * Copy any block-specific data when copying to a new block instance.
337
     *
338
     * @param int $fromid the id number of the block instance to copy from
339
     * @return boolean
340
     */
341
    public function instance_copy($fromid) {
342
 
343
        $fromcontext = context_block::instance($fromid);
344
 
345
        $fs = get_file_storage();
346
 
347
        if (!$fs->is_area_empty($fromcontext->id, 'block_point_view', 'content', 0, false)) {
348
 
349
            $draftitemid = 0;
350
 
351
            file_prepare_draft_area(
352
                $draftitemid,
353
                $fromcontext->id,
354
                'block_point_view',
355
                'content',
356
                0,
357
                [ 'subdirs' => true ]
358
            );
359
 
360
            file_save_draft_area_files(
361
                $draftitemid,
362
                $this->context->id,
363
                'block_point_view',
364
                'content',
365
                0,
366
                [ 'subdirs' => true ]
367
            );
368
 
369
        }
370
 
371
        if (!$fs->is_area_empty($fromcontext->id, 'block_point_view', 'point_views_pix', 0, false)) {
372
 
373
            $draftitemid = 0;
374
 
375
            file_prepare_draft_area(
376
                $draftitemid,
377
                $fromcontext->id,
378
                'block_point_view',
379
                'point_views_pix',
380
                0,
381
                [ 'subdirs' => true ]
382
            );
383
 
384
            file_save_draft_area_files(
385
                $draftitemid,
386
                $this->context->id,
387
                'block_point_view',
388
                'point_views_pix',
389
                0,
390
                [ 'subdirs' => true ]
391
            );
392
 
393
        }
394
 
395
        return true;
396
 
397
    }
398
}