forked from moodleworkplace/moodle-tool_certificate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
281 lines (253 loc) · 10.5 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
// This file is part of the tool_certificate plugin for 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/>.
/**
* Customcert module core interaction API
*
* @package tool_certificate
* @copyright 2013 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_certificate\permission;
/**
* Serves certificate issues and other files.
*
* @param stdClass $course
* @param stdClass $cm
* @param context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @param array $options
* @return bool|null false if file not found, does not return anything if found - just send the file
*/
function tool_certificate_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
global $CFG, $DB;
require_once($CFG->libdir . '/filelib.php');
// We are positioning the elements.
if ($filearea === 'image') {
if (!permission::can_manage_anywhere()) {
// Shared images are only displayed to the users during editing of a template.
return false;
}
$relativepath = implode('/', $args);
$fullpath = '/' . $context->id . '/tool_certificate/image/' . $relativepath;
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, $forcedownload);
}
// Elements can use several fileareas defined in tool_certificate.
if ($filearea === 'element' || $filearea === 'elementaux') {
$elementid = array_shift($args);
$template = \tool_certificate\template::find_by_element_id($elementid);
$template->require_can_manage();
$filename = array_pop($args);
if (!$args) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'tool_certificate', $filearea, $elementid, $filepath, $filename);
if (!$file) {
return false;
}
send_stored_file($file, null, 0, $forcedownload, $options);
}
// Issues filearea.
if ($filearea === 'issues') {
$filename = array_pop($args); // File name is actually the certificate code.
$code = pathinfo($filename, PATHINFO_FILENAME);
$issue = $DB->get_record('tool_certificate_issues', ['code' => $code], '*', MUST_EXIST);
$template = \tool_certificate\template::instance($issue->templateid);
if (!permission::can_view_issue($template, $issue) && !permission::can_verify()) {
return false;
}
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'tool_certificate', $filearea, $issue->id, '', false);
if (!$file = reset($files)) {
return false;
}
send_stored_file($file, null, 0, $forcedownload, $options);
}
return false;
}
/**
* Add nodes to myprofile page.
*
* @param \core_user\output\myprofile\tree $tree Tree object
* @param stdClass $user user object
* @param bool $iscurrentuser
* @param stdClass $course Course object
* @return bool
*/
function tool_certificate_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
global $USER;
if (permission::can_view_list($user->id)) {
if ($USER->id == $user->id) {
$link = get_string('mycertificates', 'tool_certificate');
} else {
$link = get_string('certificates', 'tool_certificate');
}
$url = new moodle_url('/admin/tool/certificate/my.php', $iscurrentuser ? [] : ['userid' => $user->id]);
$node = new core_user\output\myprofile\node('miscellaneous', 'toolcertificatemy', $link, null, $url);
$tree->add_node($node);
}
}
/**
* Handles editing the 'name' of the element in a list.
*
* @param string $itemtype
* @param int $itemid
* @param string $newvalue
* @return \core\output\inplace_editable
*/
function tool_certificate_inplace_editable($itemtype, $itemid, $newvalue) {
$newvalue = clean_param($newvalue, PARAM_TEXT);
external_api::validate_context(context_system::instance());
if ($itemtype === 'elementname') {
// Validate access.
$element = \tool_certificate\element::instance($itemid);
$element->get_template()->require_can_manage();
$element->save((object)['name' => $newvalue]);
return $element->get_inplace_editable();
}
if ($itemtype === 'templatename') {
// Validate access.
$template = \tool_certificate\template::instance($itemid);
$template->require_can_manage();
$template->save((object)['name' => $newvalue]);
return $template->get_editable_name();
}
}
/**
* Get icon mapping for font-awesome.
*/
function tool_certificate_get_fontawesome_icon_map() {
return [
'tool_certificate:download' => 'fa-download',
'tool_certificate:linkedin' => 'fa-linkedin-square'
];
}
/**
* Display the Certificate link in the course administration menu.
*
* @param settings_navigation $navigation The settings navigation object
* @param stdClass $course The course
* @param context $context Course context
*/
function tool_certificate_extend_navigation_course($navigation, $course, $context) {
if (permission::can_view_templates_in_context($context)) {
$certificatenode = $navigation->add(get_string('certificates', 'tool_certificate'),
null, navigation_node::TYPE_CONTAINER, null, 'tool_certificate');
$url = new moodle_url('/admin/tool/certificate/manage_templates.php', ['courseid' => $course->id]);
$certificatenode->add(get_string('managetemplates', 'tool_certificate'), $url, navigation_node::TYPE_SETTING,
null, 'tool_certificate');
}
}
/**
* Hook called to check if template delete is permitted when deleting category.
*
* @param \core_course_category $category The category record.
* @return bool
*/
function tool_certificate_can_course_category_delete(\core_course_category $category): bool {
// Deletion requires certificates to be present and permission to manage them.
$certificatescount = \tool_certificate\certificate::count_templates_in_category($category);
return !$certificatescount || permission::can_manage($category->get_context());
}
/**
* Hook called to check if template move is permitted when deleting category.
*
* @param \core_course_category $category The category record.
* @param \core_course_category $newcategory The new category record.
* @return bool
*/
function tool_certificate_can_course_category_delete_move(\core_course_category $category,
\core_course_category $newcategory): bool {
// Deletion with move requires certificates to move to be present and
// permission to manage them at destination category.
$certificatescount = \tool_certificate\certificate::count_templates_in_category($category);
return !$certificatescount || (permission::can_manage($category->get_context())
&& permission::can_manage($newcategory->get_context()));
}
/**
* Hook called to add information that is displayed on category deletion form.
*
* @param \core_course_category $category The category record.
* @return string
*/
function tool_certificate_get_course_category_contents(\core_course_category $category): string {
if (\tool_certificate\certificate::count_templates_in_category($category)) {
return get_string('certificatetemplates', 'tool_certificate');
}
return '';
}
/**
* Hook called before we delete a category.
* Deletes all the templates in the category.
*
* @param \stdClass $category The category record.
*/
function tool_certificate_pre_course_category_delete(\stdClass $category): void {
$context = context_coursecat::instance($category->id);
$templates = \tool_certificate\persistent\template::get_records(['contextid' => $context->id]);
foreach ($templates as $template) {
\tool_certificate\template::instance(0, $template->to_record())
->delete();
}
}
/**
* Hook called before we delete a category.
* Moves all the templates in the deleted category to the new category.
*
* @param \core_course_category $category The category record.
* @param \core_course_category $newcategory The new category record.
*/
function tool_certificate_pre_course_category_delete_move(\core_course_category $category,
\core_course_category $newcategory): void {
$context = $category->get_context();
$newcontext = $newcategory->get_context();
$templates = \tool_certificate\persistent\template::get_records(['contextid' => $context->id]);
foreach ($templates as $template) {
\tool_certificate\template::instance(0, $template->to_record())
->move_files_to_new_context($newcontext->id);
$template->set('contextid', $newcontext->id)->update();
}
}
/**
* Callback for theme_workplace, return list of workplace menu items to be added to the launcher.
*
* @return array[] The array containing the workplace menu items where each item is an array with keys:
* url => moodle_url where item will redirect
* name => string name shown in the launcher
* imageurl => string url for the icon shown in the launcher
* isglobal (optional) => bool to indicate if item is displayed in the global section.
*/
function tool_certificate_theme_workplace_menu_items(): array {
global $OUTPUT;
$menuitems = [];
if (permission::can_view_admin_tree()) {
$menuitems[] = [
'url' => new moodle_url("/admin/tool/certificate/manage_templates.php"),
'name' => get_string('certificates', 'tool_certificate'),
'imageurl' => $OUTPUT->image_url('icon', 'tool_certificate')->out(false),
'isglobal' => component_class_callback('\tool_tenant\permission', 'can_switch_tenant', [], false)
];
}
return $menuitems;
}