Proyectos de Subversion Moodle

Rev

| 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
 * Observer for course modules creation and deletion, to update configuration and database accordingly.
19
 *
20
 * @package    block_point_view
21
 * @copyright  2020 Jayson Haulkory, 2021 Astor Bizard
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Observer class for course modules creation and deletion.
27
 *
28
 * @package    block_point_view
29
 * @copyright  2020 Jayson Haulkory, 2021 Astor Bizard
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class block_point_view_observer {
33
 
34
    /**
35
     * Course module creation: enable reactions on it (if corresponding parameter is set in block configuration).
36
     *
37
     * @param \core\event\course_module_created $event
38
     */
39
    public static function store(\core\event\course_module_created $event) {
40
        global $DB;
41
 
42
        $coursecontext = context_course::instance($event->courseid);
43
        $blockrecord = $DB->get_record('block_instances', [ 'blockname' => 'point_view', 'parentcontextid' => $coursecontext->id ]);
44
 
45
        if ($blockrecord !== false && !empty($blockrecord->configdata)) {
46
            $blockinstance = block_instance('point_view', $blockrecord);
47
 
48
            $enablefornewmodules = isset($blockinstance->config->enable_point_views)
49
                                    && $blockinstance->config->enable_point_views
50
                                    && (!isset($blockinstance->config->enable_point_views_new_modules)
51
                                            || $blockinstance->config->enable_point_views_new_modules);
52
 
53
            if ($enablefornewmodules) {
54
                $blockinstance->config->{'moduleselectm' . $event->objectid} = $event->objectid;
55
                $blockinstance->instance_config_commit();
56
            }
57
        }
58
 
59
    }
60
 
61
    /**
62
     * Course module deleted: delete config data and database entries for votes for this module.
63
     *
64
     * @param \core\event\course_module_deleted $event
65
     */
66
    public static function remove(\core\event\course_module_deleted $event) {
67
        global $DB;
68
 
69
        $coursecontext = context_course::instance($event->courseid);
70
        $blockrecord = $DB->get_record('block_instances', [ 'blockname' => 'point_view', 'parentcontextid' => $coursecontext->id ]);
71
 
72
        if ($blockrecord !== false && !empty($blockrecord->configdata)) {
73
            $blockinstance = block_instance('point_view', $blockrecord);
74
            unset($blockinstance->config->{'moduleselectm' . $event->objectid});
75
            unset($blockinstance->config->{'difficulty_' . $event->objectid});
76
            $blockinstance->instance_config_commit();
77
        }
78
 
79
        $DB->delete_records('block_point_view', [ 'courseid' => $event->courseid, 'cmid' => $event->objectid ]);
80
    }
81
}