forked from anthology-ally/moodle-tool_ally
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
104 lines (91 loc) · 3.39 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library for core hooks.
* @author Guy Thomas <[email protected]>
* @copyright Copyright (c) 2017 Blackboard Inc. (http://www.blackboard.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use tool_ally\file_processor,
tool_ally\local_file,
tool_ally\cache,
tool_ally\local_content,
tool_ally\componentsupport\interfaces\content_sub_tables;
/**
* Callback for after file deleted.
* @param stdClass $filerecord
*/
function tool_ally_after_file_deleted($filerecord) {
global $DB;
$fs = get_file_storage();
$file = $fs->get_file_instance($filerecord);
$courseid = local_file::courseid($file, IGNORE_MISSING);
if (!local_file::file_validator()->validate_stored_file($file)) {
return; // Ally does not support files outside of a course.
}
$DB->insert_record_raw('tool_ally_deleted_files', [
'courseid' => $courseid,
'pathnamehash' => $file->get_pathnamehash(),
'contenthash' => $file->get_contenthash(),
'mimetype' => $file->get_mimetype(),
'timedeleted' => time(),
], false);
cache::instance()->invalidate_file_keys($file);
}
/**
* Callback for after file created.
* @param stdClass $filerecord
*/
function tool_ally_after_file_created($filerecord) {
$fs = get_file_storage();
$file = $fs->get_file_instance($filerecord);
file_processor::push_file_update($file);
cache::instance()->invalidate_file_keys($file);
}
/**
* Callback for after file updated.
* @param stdClass $filerecord
*/
function tool_ally_after_file_updated($filerecord) {
$fs = get_file_storage();
$file = $fs->get_file_instance($filerecord);
file_processor::push_file_update($file);
cache::instance()->invalidate_file_keys($file);
}
/**
* Callback for pre-module deletion.
* @param stdClass $cm (cm record from course_modules table)
* @throws \moodle_exception Probably the cm_info could not be generated.
*/
function tool_ally_pre_course_module_delete(stdClass $cm) {
try {
list ($course, $cm) = get_course_and_cm_from_cmid($cm->id, null, $cm->course);
$component = local_content::component_instance($cm->modname);
if (!$component || !$component instanceof content_sub_tables) {
return;
}
// Queue for deletion, all records related to the main record for this course module.
$component->queue_delete_sub_tables($cm);
} catch (\moodle_exception $mex) {
// Probably caught when disabled or erratic modules are in tests.
if (!\tool_ally\local::duringtesting()) {
// Something is wrong with this module.
mtrace($mex->getMessage());
throw $mex;
}
}
}