Proyectos de Subversion Moodle

Rev

Rev 5 | Ir a la última revisión | | 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
 * Plugin local library.
19
 *
20
 * @package    block_point_view
21
 * @copyright  2021 Astor Bizard
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Check consistency of given block instance with given parent context. Throws a moodle_exception on failed check.
27
 *
28
 * @param object|int $instanceorid Block instance or id.
29
 * @param context $context Parent context of the block.
30
 * @param string $errorcontext Information about the context in which a failed check occured.
31
 * @param string $errorurl URL to redirect to on a failed check.
32
 * @throws moodle_exception
33
 */
34
function block_point_view_check_instance($instanceorid, $context, $errorcontext = '', $errorurl = '') {
35
    global $DB;
36
    if (is_object($instanceorid)) {
37
        $blockrecord = $instanceorid;
38
    } else {
39
        $blockrecord = $DB->get_record('block_instances', [ 'id' => $instanceorid ]);
40
    }
41
    if ($blockrecord === false || $blockrecord->parentcontextid != $context->id || $blockrecord->blockname != 'point_view') {
42
        throw new moodle_exception('invalidblockinstance', 'error', $errorurl,
43
                $errorcontext . ' / ' . get_string('pluginname', 'block_point_view'));
44
    }
45
}
46
 
47
/**
48
 * Find and return pix url.
49
 *
50
 * @param int $contextid Context in which to search the file.
51
 * @param string $filearea File area.
52
 * @param string $file File name.
53
 * @return string|boolean url if found, false if not.
54
 */
55
function block_point_view_pix_url($contextid, $filearea, $file) {
56
    $fs = get_file_storage();
57
    if ($fs->file_exists($contextid, 'block_point_view', $filearea, 0, '/', $file . '.png')) {
58
        return moodle_url::make_pluginfile_url($contextid, 'block_point_view', $filearea, 0, '/', $file)->out();
59
    } else {
60
        return false;
61
    }
62
 
63
}
64
 
65
/**
66
 * Get current pix for a block instance.
67
 * This method checks existence of selected pix, replacing them with default ones if the first do not exist.
68
 *
69
 * @param block_point_view $blockinstance Block instance.
70
 * @param array|null $subset Requested subset of pix. If none specified, all will be returned.
71
 * @return string[]
72
 */
73
function block_point_view_get_current_pix($blockinstance, $subset = null) {
74
    global $CFG;
75
 
76
    if ($subset !== null) {
77
        $pixfiles = $subset;
78
    } else {
79
        $pixfiles = [
80
                'easy',
81
                'better',
82
                'hard',
83
                'group_',
84
                'group_E',
85
                'group_B',
86
                'group_H',
87
                'group_EB',
88
                'group_EH',
89
                'group_BH',
90
                'group_EBH',
91
        ];
92
    }
93
 
94
    $pix = [];
95
 
96
    foreach ($pixfiles as $file) {
97
        $pix[$file] = false;
98
        if (isset($blockinstance->config->pixselect) && $blockinstance->config->pixselect == 'custom') {
99
            $pix[$file] = block_point_view_pix_url($blockinstance->context->id, 'point_views_pix', $file);
100
        }
101
        if (!$pix[$file]
102
                && get_config('block_point_view', 'enable_pix_admin')
103
                && (!isset($blockinstance->config->pixselect) || $blockinstance->config->pixselect != 'default')) {
104
            $pix[$file] = block_point_view_pix_url(1, 'point_views_pix_admin', $file);
105
        }
106
        if (!$pix[$file]) {
107
            $pix[$file] = $CFG->wwwroot . '/blocks/point_view/pix/' . $file . '.png';
108
        }
109
    }
110
 
111
    return $pix;
112
}
113
 
114
/**
115
 * Get current text for a given reaction.
116
 *
117
 * @param block_point_view $blockinstance Block instance.
118
 * @param string $reaction Reaction name.
119
 * @return string Current reaction text.
120
 */
121
function block_point_view_get_reaction_text($blockinstance, $reaction) {
122
    return format_string((isset($blockinstance->config->{'pix_text_' . $reaction})) ?
123
            $blockinstance->config->{'pix_text_' . $reaction}
124
            : get_string('defaulttext' . $reaction, 'block_point_view'));
125
}
126
 
127
/**
128
 * Retrieve difficulty settings for modules within a course.
129
 *
130
 * @param block_point_view $blockinstance Block instance.
131
 * @param int $courseid Course id.
132
 * @return array Difficulty settings for every course module. One entry for each module, empty array if difficulty tracks disabled.
133
 */
134
function block_point_view_get_difficulty_levels($blockinstance, $courseid) {
135
 
136
    // If difficulty tracks are disabled, do not put any track.
137
    if (!isset($blockinstance->config->enable_difficultytracks)
138
            || !$blockinstance->config->enable_difficultytracks) {
139
        return [];
140
    }
141
 
142
    $cms = get_fast_modinfo($courseid, -1)->cms;
143
 
144
    $difficultylevels = [];
145
 
146
    // Loop through modules.
147
    foreach ($cms as $cm) {
148
        if (isset($blockinstance->config->{'difficulty_' . $cm->id})) {
149
            $difficulty = $blockinstance->config->{'difficulty_' . $cm->id};
150
        } else {
151
            $difficulty = 0;
152
        }
153
 
154
        $difficultylevels[] = [
155
                'id' => $cm->id,
156
                'difficultyLevel' => $difficulty,
157
        ];
158
    }
159
 
160
    return $difficultylevels;
161
}
162
 
163
/**
164
 * Retrieve reactions settings for modules within a course.
165
 *
166
 * @param block_point_view $blockinstance Block instance.
167
 * @param int $userid User id, as returned array contains information about user's current vote.
168
 * @param int $courseid Course id.
169
 * @return array Reactions settings for every course module. One entry for each module with reactions enabled.
170
 */
171
function block_point_view_get_modules_with_reactions($blockinstance, $userid, $courseid) {
172
    global $DB;
173
 
174
    if (empty($blockinstance->config->enable_point_views)) {
175
        return [];
176
    }
177
 
178
    $cms = get_fast_modinfo($courseid, $userid)->cms;
179
    $moduleswithreactions = [];
180
    foreach ($cms as $cm) {
181
        if (isset($blockinstance->config->{'moduleselectm' . $cm->id})
182
        && $blockinstance->config->{'moduleselectm' . $cm->id} > 0
183
        && $cm->uservisible) {
184
            $moduleswithreactions[] = $cm->id;
185
        }
186
    }
187
 
188
    if (empty($moduleswithreactions)) {
189
        return [];
190
    }
191
 
192
    list($insql, $inparams) = $DB->get_in_or_equal($moduleswithreactions, SQL_PARAMS_NAMED);
193
 
194
    $params = array_merge($inparams, [ 'userid' => $userid, 'courseid' => $courseid ]);
195
 
196
    if (isset($blockinstance->config->show_other_users_reactions)
197
            && !$blockinstance->config->show_other_users_reactions
198
            && !has_capability('block/point_view:access_overview', $blockinstance->context)) {
199
 
200
        $sql = 'SELECT cm.id as cmid, COALESCE(bpv.vote, 0) as uservote
201
            FROM {course_modules} cm
202
            LEFT JOIN (SELECT cmid, vote FROM {block_point_view} WHERE userid = :userid) bpv ON bpv.cmid = cm.id
203
            WHERE cm.course = :courseid AND cm.id ' . $insql . '
204
            GROUP BY cm.id, bpv.vote';
205
 
206
        $result = array_values($DB->get_records_sql($sql, $params));
207
        foreach ($result as &$cmrow) {
208
            $cmrow->totaleasy = $cmrow->uservote == 1 ? 1 : 0;
209
            $cmrow->totalbetter = $cmrow->uservote == 2 ? 1 : 0;
210
            $cmrow->totalhard = $cmrow->uservote == 3 ? 1 : 0;
211
        }
212
        return $result;
213
    } else {
214
        $sql = 'SELECT cm.id as cmid,
215
            COALESCE(tableeasy.totaleasy, 0) as totaleasy,
216
            COALESCE(tablebetter.totalbetter, 0) as totalbetter,
217
            COALESCE(tablehard.totalhard, 0) as totalhard,
218
            COALESCE(tableuser.vote, 0) as uservote
219
            FROM {course_modules} cm
220
            LEFT JOIN (SELECT cmid, COUNT(vote) as totaleasy FROM {block_point_view}
221
                    WHERE vote = 1 GROUP BY cmid) as tableeasy ON tableeasy.cmid = cm.id
222
            LEFT JOIN (SELECT cmid, COUNT(vote) as totalbetter FROM {block_point_view}
223
                    WHERE vote = 2 GROUP BY cmid) as tablebetter ON tablebetter.cmid = cm.id
224
            LEFT JOIN (SELECT cmid, COUNT(vote) as totalhard FROM {block_point_view}
225
                    WHERE vote = 3 GROUP BY cmid) as tablehard ON tablehard.cmid = cm.id
226
            LEFT JOIN (SELECT cmid, vote FROM {block_point_view}
227
                    WHERE userid = :userid) AS tableuser ON tableuser.cmid = cm.id
228
            WHERE cm.id ' . $insql . '
229
            AND cm.course = :courseid
230
            GROUP BY cm.id, tableeasy.totaleasy, tablebetter.totalbetter, tablehard.totalhard, tableuser.vote';
231
 
232
        // TODO optimize this loading time, maybe add some indexes to the table.
233
 
234
        $params = array_merge($inparams, [ 'userid' => $userid, 'courseid' => $courseid ]);
235
 
236
        return array_values($DB->get_records_sql($sql, $params)); // Takes < 0.1s on small DB.
237
    }
238
}
239
 
240
/**
241
 * Get difficulty tracks colors, as set in plugin administration configuration.
242
 */
243
function block_point_view_get_track_colors() {
244
    return [
245
            '',
246
            get_config('block_point_view', 'green_track_color_admin'),
247
            get_config('block_point_view', 'blue_track_color_admin'),
248
            get_config('block_point_view', 'red_track_color_admin'),
249
            get_config('block_point_view', 'black_track_color_admin'),
250
    ];
251
}
252
 
253
 
254
/**
255
 * User data string for the overview table
256
 *
257
 * @param array $userids
258
 * @param stdClass $users
259
 * @return string
260
 */
261
function block_point_view_format_users($userids, $users) {
262
    global $OUTPUT;
263
    $string = '';
264
 
265
    foreach ($userids as $userid) {
266
        $user = $users[$userid];
267
        $string .= $OUTPUT->user_picture($user) . fullname( $user ) . '<br>';
268
 
269
    }
270
 
271
    return $string;
272
}
273
 
274
/**
275
 * Call all required javascript for edit_form.
276
 *
277
 * @param int $blockcontextid Context id of the block.
278
 */
279
function block_point_view_require_edit_form_javascript($blockcontextid, $formpage = null) {
280
    global $COURSE, $PAGE;
281
    $envconf = [
282
            'courseid' => $COURSE->id,
283
            'contextid' => $blockcontextid,
284
    ];
285
 
286
    $trackcolors = block_point_view_get_track_colors();
287
 
288
    $params = [ $envconf, $trackcolors ];
289
 
290
    $jspage = $formpage ?? $PAGE;
291
 
292
    $jspage->requires->js_call_amd('block_point_view/script_config_point_view', 'init', $params);
293
    $jspage->requires->string_for_js('resetreactionsconfirmation', 'block_point_view', format_string($COURSE->fullname));
294
    $jspage->requires->string_for_js('cleanupreactionsconfirmation', 'block_point_view', format_string($COURSE->fullname));
295
    $jspage->requires->strings_for_js([
296
                                            'deleteemojiconfirmation',
297
                                            'reactionsresetsuccessfully',
298
                                            'reactionscleanedupsuccessfully',
299
                                            'resetreactionsonmoduleconfirmation',
300
                                    ], 'block_point_view');
301
    $jspage->requires->strings_for_js([ 'ok', 'info' ], 'moodle');
302
}
303
 
304
/**
305
 * Change where this block appears to display it in subcontexts (especially course module pages).
306
 *
307
 * @param int $blockinstanceid Block instance ID.
308
 */
309
function block_point_view_show_in_subcontexts($blockinstanceid) {
310
    global $DB;
311
    $DB->update_record('block_instances', [ 'id' => $blockinstanceid, 'showinsubcontexts' => 1, 'pagetypepattern' => '*' ]);
312
}