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
declare(strict_types=1);
18
 
19
use mod_edusharing\EduSharingService;
20
use mod_edusharing\RestoreHelper;
21
use mod_edusharing\UtilityFunctions;
22
 
23
/**
24
 * Class mod_edusharing_observer
25
 *
26
 * callback definitions for events.
27
 * @package mod_edusharing
28
 */
29
class mod_edusharing_observer {
30
    /**
31
     * Function course_module_deleted
32
     *
33
     * @param \core\event\course_module_deleted $event
34
     * @return void
35
     */
36
    public static function course_module_deleted(\core\event\course_module_deleted $event) {
37
        global $DB;
38
        $data     = $event->get_data();
39
        $objectid = $data['objectid'];
40
        // Delete es-activities in course-modules.
41
        try {
42
            $eduobjects = $DB->get_records('edusharing', ['module_id' => $objectid]);
43
        } catch (Exception $exception) {
44
            debugging($exception->getMessage());
45
            return;
46
        }
47
        $service = new EduSharingService();
48
        foreach ($eduobjects as $object) {
49
            try {
50
                $service->delete_instance($object['id']);
51
            } catch (Exception $exception) {
52
                debugging($exception->getMessage());
53
            }
54
        }
55
    }
56
 
57
    /**
58
     * Function course_module_created
59
     *
60
     * @param \core\event\course_module_created $event
61
     */
62
    public static function course_module_created(\core\event\course_module_created $event) {
63
        global $DB;
64
        $data = $event->get_data();
65
        try {
66
            $module = $DB->get_record($data['other']['modulename'], ['id' => $data['other']['instanceid']], '*', MUST_EXIST);
67
        } catch (Exception $exception) {
68
            debugging($exception->getMessage());
69
            return;
70
        }
71
        $text = $module->intro;
72
        if ($text === null) {
73
            return;
74
        }
75
        $utils         = new UtilityFunctions();
76
        $backtrace     = debug_backtrace();
77
        $isduplication = false;
78
        foreach ($backtrace as $call) {
79
            if (in_array($call['function'], ['cm_duplicate', 'duplicate_module'], true)) {
80
                $isduplication = true;
81
            }
82
        }
83
        if ($isduplication) {
84
            $createdusages = [];
85
            $matches = $utils->get_inline_object_matches($text);
86
            $transaction = $DB->start_delegated_transaction();
87
            try {
88
                $service = new EduSharingService(null, null, $utils);
89
            } catch (Exception $exception) {
90
                debugging($exception->getMessage());
91
                return;
92
            }
93
            try {
94
                foreach ($matches as $match) {
95
                    $originalresourceid = $utils->get_resource_id_from_match($match);
96
                    $edusharing = $DB->get_record('edusharing', ['id' => $originalresourceid], '*', MUST_EXIST);
97
                    unset($edusharing->id);
98
                    unset($edusharing->usage_id);
99
                    $edusharing->module_id = $data['objectid'];
100
                    $newresourceid = $service->add_instance($edusharing);
101
                    $newresourceid === false && throw new Exception('ES add instance failed');
102
                    if (isset($edusharing->usage_id)) {
103
                        $currentusage = new stdClass();
104
                        $currentusage->nodeId = $utils->get_object_id_from_url($edusharing->object_url);
105
                        $currentusage->usageId = $edusharing->usage_id;
106
                        $createdusages[] = $currentusage;
107
                    }
108
                    $text = str_replace("resourceId=$originalresourceid", "resourceId=$newresourceid", $text);
109
                }
110
                $DB->set_field($data['other']['modulename'], 'intro', $text, ['id' => $data['other']['instanceid']]);
111
                $transaction->allow_commit();
112
            } catch (Exception $exception) {
113
                try {
114
                    foreach ($createdusages as $usagedata) {
115
                        $service->delete_usage($usagedata);
116
                    }
117
                    $DB->rollback_delegated_transaction($transaction, $exception);
118
                } catch (Throwable $cleanupexception) {
119
                    debugging($cleanupexception);
120
                }
121
            }
122
            return;
123
        }
124
        try {
125
            $coursemodule = $DB->get_record($data['objecttable'], ['id' => $data['objectid']], '*', MUST_EXIST);
126
            $utils->set_moodle_ids_in_edusharing_entries($text, (int)$coursemodule->section, (int)$data['objectid']);
127
        } catch (Exception $exception) {
128
            debugging($exception);
129
        }
130
    }
131
 
132
    /**
133
     * Function course_module_updated
134
     *
135
     * @param \core\event\course_module_updated $event
136
     * @return void
137
     */
138
    public static function course_module_updated(\core\event\course_module_updated $event) {
139
        global $DB;
140
        $data = $event->get_data();
141
        try {
142
            $module = $DB->get_record($data['other']['modulename'], ['id' => $data['other']['instanceid']], '*', MUST_EXIST);
143
        } catch (Exception $exception) {
144
            debugging($exception->getMessage());
145
            return;
146
        }
147
        $text = $module->intro;
148
        if ($text === null) {
149
            return;
150
        }
151
        $utils = new UtilityFunctions();
152
        try {
153
            $coursemodule = $DB->get_record($data['objecttable'], ['id' => $data['objectid']], '*', MUST_EXIST);
154
            $utils->set_moodle_ids_in_edusharing_entries($text, (int)$coursemodule->section, (int)$data['objectid']);
155
        } catch (Exception $exception) {
156
            debugging($exception->getMessage());
157
        }
158
    }
159
 
160
    /**
161
     * Function course_section_created
162
     *
163
     * @param \core\event\course_section_created $event
164
     * @return void
165
     */
166
    public static function course_section_created(\core\event\course_section_created $event) {
167
        global $DB;
168
        $data = $event->get_data();
169
        try {
170
            $module = $DB->get_record('course_sections', ['id' => $data['objectid']], '*', MUST_EXIST);
171
        } catch (Exception $exception) {
172
            debugging($exception->getMessage());
173
            return;
174
        }
175
        $text = $module->summary;
176
        if ($text === null) {
177
            return;
178
        }
179
        $utils = new UtilityFunctions();
180
        try {
181
            $utils->set_moodle_ids_in_edusharing_entries($text, (int)$data['objectid']);
182
        } catch (Exception $exception) {
183
            debugging($exception->getMessage());
184
        }
185
    }
186
 
187
    /**
188
     * Function course_section_updated
189
     *
190
     * @param \core\event\course_section_updated $event
191
     * @return void
192
     */
193
    public static function course_section_updated(\core\event\course_section_updated $event) {
194
        global $DB;
195
        $data = $event->get_data();
196
        try {
197
            $section = $DB->get_record('course_sections', ['id' => $data['objectid']], '*', MUST_EXIST);
198
        } catch (Exception $exception) {
199
            debugging($exception->getMessage());
200
            return;
201
        }
202
        $text = $section->summary;
203
        if ($text === null) {
204
            return;
205
        }
206
        $utils = new UtilityFunctions();
207
        try {
208
            $utils->set_moodle_ids_in_edusharing_entries($text, (int)$data['objectid']);
209
        } catch (Exception $exception) {
210
            debugging($exception->getMessage());
211
        }
212
    }
213
 
214
    /**
215
     * Function course_section_deleted
216
     *
217
     * @param \core\event\course_section_deleted $event
218
     * @return void
219
     */
220
    public static function course_section_deleted(\core\event\course_section_deleted $event) {
221
        global $DB;
222
        $data     = $event->get_data();
223
        $objectid = $data['objectid'];
224
        try {
225
            $eduobjects = $DB->get_records('edusharing', ['section_id' => $objectid]);
226
        } catch (Exception $exception) {
227
            debugging($exception->getMessage());
228
            return;
229
        }
230
        $service = new EduSharingService();
231
        foreach ($eduobjects as $object) {
232
            try {
233
                $service->delete_instance($object['id']);
234
            } catch (Exception $exception) {
235
                debugging($exception->getMessage());
236
            }
237
        }
238
    }
239
 
240
    /**
241
     * Function course_deleted
242
     *
243
     * @param \core\event\course_deleted $event
244
     */
245
    public static function course_deleted(\core\event\course_deleted $event) {
246
        global $DB;
247
        $data     = $event->get_data();
248
        $objectid = $data['objectid'];
249
        try {
250
            $eduobjects = $DB->get_records('edusharing', ['course' => $objectid]);
251
        } catch (Exception $exception) {
252
            debugging($exception->getMessage());
253
            return;
254
        }
255
        $service = new EduSharingService();
256
        foreach ($eduobjects as $object) {
257
            try {
258
                $service->delete_instance($object->id);
259
            } catch (Exception $exception) {
260
                debugging($exception->getMessage());
261
            }
262
        }
263
    }
264
 
265
    /**
266
     * Function course_restored
267
     *
268
     * @param \core\event\course_restored $event
269
     */
270
    public static function course_restored(\core\event\course_restored $event) {
271
        $eventdata = $event->get_data();
272
        $courseid  = $eventdata['courseid'];
273
        try {
274
            $helper = new RestoreHelper(new EduSharingService());
275
            $helper->convert_inline_options($courseid);
276
        } catch (Exception $exception) {
277
            debugging($exception->getMessage());
278
        }
279
    }
280
 
281
    /**
282
     * Function user_loggedin
283
     *
284
     * @param \core\event\user_loggedin $event
285
     */
286
    public static function user_loggedin(\core\event\user_loggedin $event) {
287
        global $SESSION, $USER;
288
        $SESSION->edusharing_sso = [];
289
        $utils = new UtilityFunctions();
290
        try {
291
            if ($utils->get_config_entry('obfuscate_auth_param') !== '1') {
292
                return;
293
            }
294
            $authparam = $utils->get_config_entry('EDU_AUTH_PARAM_NAME_USERID');
295
            $salt = $utils->get_config_entry('SALT');
296
            if ($salt === false) {
297
                $salt = uniqid();
298
                $utils->set_config_entry('SALT', $salt);
299
            }
300
            $SESSION->edusharing_sso[$authparam] = hash('sha256', $USER->username . $salt)
301
                . '@' . $utils->get_config_entry('application_appid');
302
        } catch (Exception $exception) {
303
            debugging($exception->getMessage());
304
            return;
305
        }
306
    }
307
}