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
/**
19
 * Completion Progress block definition
20
 *
21
 * @package    block_completion_progress
22
 * @copyright  2016 Michael de Raadt
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
use block_completion_progress\completion_progress;
27
use block_completion_progress\defaults;
28
 
29
/**
30
 * Completion Progress block class
31
 *
32
 * @copyright 2016 Michael de Raadt
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class block_completion_progress extends block_base {
36
 
37
    /**
38
     * Sets the block title
39
     *
40
     * @return void
41
     */
42
    public function init() {
43
        $this->title = get_string('pluginname', 'block_completion_progress');
44
    }
45
 
46
    /**
47
     *  we have global config/settings data
48
     *
49
     * @return bool
50
     */
51
    public function has_config() {
52
        return true;
53
    }
54
 
55
    /**
56
     * Controls the block title based on instance configuration
57
     *
58
     * @return bool
59
     */
60
    public function specialization() {
61
        if (isset($this->config->progressTitle) && trim($this->config->progressTitle) != '') {
62
            $this->title = format_string($this->config->progressTitle);
63
        }
64
 
65
        // Work around a quirk of in_array('opt', [0 => 0]) returning true on
66
        // PHP <8.0, causing the configuration form's 'config_group' element to
67
        // declare all its options 'selected'.
68
        if (isset($this->config->group)) {
69
            $this->config->group = (string)$this->config->group;
70
        }
71
    }
72
 
73
    /**
74
     * Controls whether multiple instances of the block are allowed on a page
75
     *
76
     * @return bool
77
     */
78
    public function instance_allow_multiple() {
79
        return !self::on_site_page($this->page);
80
    }
81
 
82
    /**
83
     * Controls whether the block is configurable
84
     *
85
     * @return bool
86
     */
87
    public function instance_allow_config() {
88
        return !self::on_site_page($this->page);
89
    }
90
 
91
    /**
92
     * Defines where the block can be added
93
     *
94
     * @return array
95
     */
96
    public function applicable_formats() {
97
        return array(
98
            'course-view'    => true,
99
            'site'           => true,
100
            'mod'            => false,
101
            'my'             => true
102
        );
103
    }
104
 
105
    /**
106
     * Creates the blocks main content
107
     *
108
     * @return string
109
     */
110
    public function get_content() {
111
        // If content has already been generated, don't waste time generating it again.
112
        if ($this->content !== null) {
113
            return $this->content;
114
        }
115
        $this->content = new stdClass;
116
        $this->content->text = '';
117
        $this->content->footer = '';
118
        $barinstances = array();
119
 
120
        // Guests do not have any progress. Don't show them the block.
121
        if (!isloggedin() || isguestuser()) {
122
            return $this->content;
123
        }
124
 
125
        if (self::on_site_page($this->page)) {
126
            // Draw the multi-bar content for the Dashboard and Front page.
127
            if (!$this->prepare_dashboard_content($barinstances)) {
128
                return $this->content;
129
            }
130
 
131
        } else {
132
            // Gather content for block on regular course.
133
            if (!$this->prepare_course_content($barinstances)) {
134
                return $this->content;
135
            }
136
        }
137
 
138
        // Organise access to JS.
139
        $this->page->requires->js_call_amd('block_completion_progress/progressbar', 'init', [
140
            'instances' => $barinstances,
141
        ]);
142
        $cachevalue = debugging('', DEBUG_DEVELOPER) ? -1 : (int)get_config('block_completion_progress', 'cachevalue');
143
        $this->page->requires->css('/blocks/completion_progress/css.php?v=' . $cachevalue);
144
 
145
        return $this->content;
146
    }
147
 
148
    /**
149
     * Produce content for the Dashboard or Front page.
150
     * @param array $barinstances receives block instance ids
151
     * @return boolean false if an early exit
152
     */
153
    protected function prepare_dashboard_content(&$barinstances) {
154
        global $USER, $CFG, $DB;
155
 
156
        $output = $this->page->get_renderer('block_completion_progress');
157
 
158
        if (!$CFG->enablecompletion) {
159
            $this->content->text .= get_string('completion_not_enabled', 'block_completion_progress');
160
            return false;
161
        }
162
 
163
        // Show a message when the user is not enrolled in any courses.
164
        $courses = enrol_get_my_courses();
165
        if (($this->page->user_is_editing() || is_siteadmin()) && empty($courses)) {
166
            $this->content->text = get_string('no_courses', 'block_completion_progress');
167
            return false;
168
        }
169
 
170
        $coursenametoshow = get_config('block_completion_progress', 'coursenametoshow') ?:
171
            defaults::COURSENAMETOSHOW;
172
        $sql = "SELECT bi.id,
173
                       COALESCE(bp.visible, 1) AS visible,
174
                       bi.configdata
175
                  FROM {block_instances} bi
176
             LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id
177
                                           AND ".$DB->sql_like('bp.pagetype', ':pagetype', false)."
178
                 WHERE bi.blockname = 'completion_progress'
179
                   AND bi.parentcontextid = :contextid
180
              ORDER BY COALESCE(bp.region, bi.defaultregion),
181
                       COALESCE(bp.weight, bi.defaultweight),
182
                       bi.id";
183
 
184
        foreach ($courses as $course) {
185
            // Get specific block config and context.
186
            $courseprogress = new completion_progress($course);
187
            if (!$course->visible || !$courseprogress->get_completion_info()->is_enabled()) {
188
                continue;
189
            }
190
 
191
            $courseprogress->for_user($USER);
192
            $blockprogresses = [];
193
 
194
            $params = ['contextid' => $courseprogress->get_context()->id, 'pagetype' => 'course-view-%'];
195
            foreach ($DB->get_records_sql($sql, $params) as $birec) {
196
                $blockprogress = (clone $courseprogress)->for_block_instance($birec);
197
                $blockinstance = $blockprogress->get_block_instance();
198
                $blockconfig = $blockprogress->get_block_config();
199
                if (!has_capability('block/completion_progress:showbar', context_block::instance($blockinstance->id)) ||
200
                        !$blockinstance->visible ||
201
                        !$blockprogress->has_visible_activities()) {
202
                    continue;
203
                }
204
                if (!empty($blockconfig->group) &&
205
                        !has_capability('moodle/site:accessallgroups', $courseprogress->get_context()) &&
206
                        !$this->check_group_membership($blockconfig->group, $course->id)) {
207
                    continue;
208
                }
209
 
210
                $blockprogresses[$blockinstance->id] = $blockprogress;
211
                $barinstances[] = $blockinstance->id;
212
            }
213
 
214
            // Output the Progress Bar.
215
            if (!empty($blockprogresses)) {
216
                $courselink = new moodle_url('/course/view.php', array('id' => $course->id));
217
                $linktext = html_writer::tag('h3', s(format_string($course->$coursenametoshow)));
218
                $this->content->text .= html_writer::link($courselink, $linktext);
219
            }
220
            foreach ($blockprogresses as $blockprogress) {
221
                $blockinstance = $blockprogress->get_block_instance();
222
                $blockconfig = $blockprogress->get_block_config();
223
                if (($blockconfig->progressTitle ?? '') !== '') {
224
                    $this->content->text .= html_writer::tag('p', s(format_string($blockconfig->progressTitle)));
225
                }
226
                $this->content->text .= $output->render($blockprogress);
227
            }
228
        }
229
 
230
        // Show a message explaining lack of bars, but only while editing is on.
231
        if ($this->page->user_is_editing() && $this->content->text == '') {
232
            $this->content->text = get_string('no_blocks', 'block_completion_progress');
233
        }
234
 
235
        return true;
236
    }
237
 
238
    /**
239
     * Produce content for a course page.
240
     * @param array $barinstances receives block instance ids
241
     * @return boolean false if an early exit
242
     */
243
    protected function prepare_course_content(&$barinstances) {
244
        global $USER, $COURSE, $CFG, $OUTPUT;
245
 
246
        $output = $this->page->get_renderer('block_completion_progress');
247
 
248
        // Check if user is in group for block.
249
        if (
250
            !empty($this->config->group) &&
251
            !has_capability('moodle/site:accessallgroups', $this->context) &&
252
            !$this->check_group_membership($this->config->group, $COURSE->id)
253
        ) {
254
            return false;
255
        }
256
 
257
        // Check if completion is enabled at site level.
258
        if (!$CFG->enablecompletion) {
259
            if (has_capability('moodle/block:edit', $this->context)) {
260
                $this->content->text .= get_string('completion_not_enabled', 'block_completion_progress');
261
            }
262
            return false;
263
        }
264
 
265
        $progress = new completion_progress($COURSE);
266
 
267
        // Check if completion is enabled at course level.
268
        if (!$progress->get_completion_info()->is_enabled()) {
269
            if (has_capability('moodle/block:edit', $this->context)) {
270
                $this->content->text .= get_string('completion_not_enabled_course', 'block_completion_progress');
271
            }
272
            return false;
273
        }
274
 
275
        $progress->for_user($USER)->for_block_instance($this->instance);
276
 
277
        // Check if any activities/resources have been created.
278
        if (!$progress->has_visible_activities()) {
279
            if (has_capability('moodle/block:edit', $this->context)) {
280
                $this->content->text .= get_string('no_activities_config_message', 'block_completion_progress');
281
            }
282
            return false;
283
        }
284
 
285
        // Display progress bar.
286
        if (has_capability('block/completion_progress:showbar', $this->context)) {
287
            $this->content->text .= $output->render($progress);
288
        }
289
        $barinstances = array($this->instance->id);
290
 
291
        // Allow teachers to access the overview page.
292
        if (has_capability('block/completion_progress:overview', $this->context)) {
293
            $parameters = array('instanceid' => $this->instance->id, 'courseid' => $COURSE->id);
294
            $url = new moodle_url('/blocks/completion_progress/overview.php', $parameters);
295
            $label = get_string('overview', 'block_completion_progress');
296
            $options = array('class' => 'overviewButton');
297
            $this->content->text .= $OUTPUT->single_button($url, $label, 'get', $options);
298
        }
299
 
300
        return true;
301
    }
302
 
303
    /**
304
     * Bumps a value to assist in caching of configured colours in css.php.
305
     */
306
    public static function increment_cache_value() {
307
        $value = get_config('block_completion_progress', 'cachevalue') + 1;
308
        set_config('cachevalue', $value, 'block_completion_progress');
309
    }
310
 
311
    /**
312
     * Determines whether the current user is a member of a given group or grouping
313
     *
314
     * @param string $group    The group or grouping identifier starting with 'group-' or 'grouping-'
315
     * @param int    $courseid The ID of the course containing the block instance
316
     * @return boolean value indicating membership
317
     */
318
    private function check_group_membership($group, $courseid) {
319
        global $USER;
320
 
321
        if ($group === '0') {
322
            return true;
323
        } else if ((substr($group, 0, 6) == 'group-') && ($groupid = intval(substr($group, 6)))) {
324
            return groups_is_member($groupid, $USER->id);
325
        } else if ((substr($group, 0, 9) == 'grouping-') && ($groupingid = intval(substr($group, 9)))) {
326
            return array_key_exists($groupingid, groups_get_user_groups($courseid, $USER->id));
327
        }
328
 
329
        return false;
330
    }
331
 
332
    /**
333
     * Checks whether the given page is site-level (Dashboard or Front page) or not.
334
     *
335
     * @param moodle_page $page the page to check, or the current page if not passed.
336
     * @return boolean True when on the Dashboard or Site home page.
337
     */
338
    public static function on_site_page($page = null) {
339
        global $PAGE;   // phpcs:ignore moodle.PHP.ForbiddenGlobalUse.BadGlobal
340
 
341
        $page = $page ?? $PAGE; // phpcs:ignore moodle.PHP.ForbiddenGlobalUse.BadGlobal
342
        $context = $page->context ?? null;
343
 
344
        if (!$page || !$context) {
345
            return false;
346
        } else if ($context->contextlevel === CONTEXT_SYSTEM && $page->requestorigin === 'restore') {
347
            return false; // When restoring from a backup, pretend the page is course-level.
348
        } else if ($context->contextlevel === CONTEXT_COURSE && $context->instanceid == SITEID) {
349
            return true;  // Front page.
350
        } else if ($context->contextlevel < CONTEXT_COURSE) {
351
            return true;  // System, user (i.e. dashboard), course category.
352
        } else {
353
            return false;
354
        }
355
    }
356
 
357
}