forked from ngandrass/moodle-local_archiving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
167 lines (149 loc) · 5.38 KB
/
Copy pathlib.php
File metadata and controls
167 lines (149 loc) · 5.38 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?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/>.
/**
* Legacy lib definitions
*
* @package local_archiving
* @copyright 2025 Niels Gandraß <niels@gandrass.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use local_archiving\tsp_manager;
use local_archiving\type\filearea;
// phpcs:ignore
defined('MOODLE_INTERNAL') || die(); // @codeCoverageIgnore
/**
* Inject navigation nodes
*
* @param settings_navigation $settingsnav The root node of the settings navigation
* @param context $ctx Current context
* @return void
* @throws \core\exception\moodle_exception
* @throws coding_exception
*/
function local_archiving_extend_settings_navigation(settings_navigation $settingsnav, context $ctx) {
// Inject archiving overview node into navigation.
if ($ctx && $ctx instanceof context_course) {
if (has_capability('local/archiving:view', $ctx)) {
// Construct new navigation node.
$url = new moodle_url('/local/archiving/index.php', [
'courseid' => $ctx->get_course_context()->instanceid,
]);
$node = navigation_node::create(
get_string('pluginname', 'local_archiving'),
$url,
navigation_node::TYPE_SETTING,
'local_archiving',
'local_archiving',
new pix_icon('i/settings', '')
);
// Append to course administration node.
$parentnode = $settingsnav->find('courseadmin', null);
if ($parentnode) {
$parentnode->add_node($node);
}
}
}
// Inject activity archiving node into activity course menu.
if ($ctx && $ctx instanceof context_module) {
if (has_capability('local/archiving:view', $ctx)) {
// Construct new navigation node.
$url = new moodle_url('/local/archiving/archive.php', [
'courseid' => $ctx->get_course_context()->instanceid,
'cmid' => $ctx->instanceid,
]);
$node = navigation_node::create(
get_string('pluginname', 'local_archiving'),
$url,
navigation_node::TYPE_SETTING,
'local_archiving',
'local_archiving',
new pix_icon('i/settings', '')
);
// Append to activity course menu.
$parentnode = $settingsnav->find('modulesettings', null);
if ($parentnode) {
$parentnode->add_node($node);
}
}
}
}
/**
* Serve local_archiving files
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
*
* @return bool false if the file not found, just send the file otherwise and do not return anything
*
* @throws coding_exception
* @throws required_capability_exception
* @throws moodle_exception
*/
function local_archiving_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
// Validate filearea.
$filearea = filearea::tryFrom($filearea);
switch ($filearea) {
case filearea::FILESTORE_CACHE:
case filearea::TSP:
break;
default:
return false;
}
// Check permissions.
require_login($course, false, $cm);
if (!has_capability('local/archiving:view', $context)) {
return false;
}
// Get remaining file information from $args.
$itemid = array_shift($args);
$filename = array_pop($args);
$filepath = '/' . implode('/', $args) . '/';
// Stop here if we are running unit tests because the following functions send output and stop execution.
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST === true) {
return 'PHPUNIT_TEST';
}
// @codeCoverageIgnoreStart
// Catch virtual files.
if ($filearea == filearea::TSP) {
try {
tsp_manager::send_virtual_tsp_file($filepath, $filename);
} catch (Exception $e) {
send_header_404();
throw $e;
}
}
// Try to retrieve and serve file.
$fs = get_file_storage();
$file = $fs->get_file(
contextid: $context->id,
component: $filearea->get_component(),
filearea: $filearea->value,
itemid: $itemid,
filepath: $filepath,
filename: $filename
);
if (!$file || $file->is_directory()) {
send_file_not_found();
}
send_stored_file($file, 0, 0, $forcedownload, $options);
return true;
// @codeCoverageIgnoreEnd
}