1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of the customcert module for 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 |
* Customcert module core interaction API
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
21 |
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Add customcert instance.
|
|
|
27 |
*
|
|
|
28 |
* @param stdClass $data
|
|
|
29 |
* @param mod_customcert_mod_form $mform
|
|
|
30 |
* @return int new customcert instance id
|
|
|
31 |
*/
|
|
|
32 |
function customcert_add_instance($data, $mform) {
|
|
|
33 |
global $DB;
|
|
|
34 |
|
|
|
35 |
// Create a template for this customcert to use.
|
|
|
36 |
$context = context_module::instance($data->coursemodule);
|
|
|
37 |
$template = \mod_customcert\template::create($data->name, $context->id);
|
|
|
38 |
|
|
|
39 |
// Add the data to the DB.
|
|
|
40 |
$data->templateid = $template->get_id();
|
|
|
41 |
$data->protection = \mod_customcert\certificate::set_protection($data);
|
|
|
42 |
$data->timecreated = time();
|
|
|
43 |
$data->timemodified = $data->timecreated;
|
|
|
44 |
$data->id = $DB->insert_record('customcert', $data);
|
|
|
45 |
|
|
|
46 |
// Add a page to this customcert.
|
|
|
47 |
$template->add_page(false);
|
|
|
48 |
|
|
|
49 |
return $data->id;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Update customcert instance.
|
|
|
54 |
*
|
|
|
55 |
* @param stdClass $data
|
|
|
56 |
* @param mod_customcert_mod_form $mform
|
|
|
57 |
* @return bool true
|
|
|
58 |
*/
|
|
|
59 |
function customcert_update_instance($data, $mform) {
|
|
|
60 |
global $DB;
|
|
|
61 |
|
|
|
62 |
$data->protection = \mod_customcert\certificate::set_protection($data);
|
|
|
63 |
$data->timemodified = time();
|
|
|
64 |
$data->id = $data->instance;
|
|
|
65 |
|
|
|
66 |
return $DB->update_record('customcert', $data);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Given an ID of an instance of this module,
|
|
|
71 |
* this function will permanently delete the instance
|
|
|
72 |
* and any data that depends on it.
|
|
|
73 |
*
|
|
|
74 |
* @param int $id
|
|
|
75 |
* @return bool true if successful
|
|
|
76 |
*/
|
|
|
77 |
function customcert_delete_instance($id) {
|
|
|
78 |
global $CFG, $DB;
|
|
|
79 |
|
|
|
80 |
// Ensure the customcert exists.
|
|
|
81 |
if (!$customcert = $DB->get_record('customcert', ['id' => $id])) {
|
|
|
82 |
return false;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
// Get the course module as it is used when deleting files.
|
|
|
86 |
if (!$cm = get_coursemodule_from_instance('customcert', $id)) {
|
|
|
87 |
return false;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
// Delete the customcert instance.
|
|
|
91 |
if (!$DB->delete_records('customcert', ['id' => $id])) {
|
|
|
92 |
return false;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// Now, delete the template associated with this certificate.
|
|
|
96 |
if ($template = $DB->get_record('customcert_templates', ['id' => $customcert->templateid])) {
|
|
|
97 |
$template = new \mod_customcert\template($template);
|
|
|
98 |
$template->delete();
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
// Delete the customcert issues.
|
|
|
102 |
if (!$DB->delete_records('customcert_issues', ['customcertid' => $id])) {
|
|
|
103 |
return false;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// Delete any files associated with the customcert.
|
|
|
107 |
$context = context_module::instance($cm->id);
|
|
|
108 |
$fs = get_file_storage();
|
|
|
109 |
$fs->delete_area_files($context->id);
|
|
|
110 |
|
|
|
111 |
return true;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* This function is used by the reset_course_userdata function in moodlelib.
|
|
|
116 |
* This function will remove all posts from the specified customcert
|
|
|
117 |
* and clean up any related data.
|
|
|
118 |
*
|
|
|
119 |
* @param stdClass $data the data submitted from the reset course.
|
|
|
120 |
* @return array status array
|
|
|
121 |
*/
|
|
|
122 |
function customcert_reset_userdata($data) {
|
|
|
123 |
global $DB;
|
|
|
124 |
|
|
|
125 |
$componentstr = get_string('modulenameplural', 'customcert');
|
|
|
126 |
$status = [];
|
|
|
127 |
|
|
|
128 |
if (!empty($data->reset_customcert)) {
|
|
|
129 |
$sql = "SELECT cert.id
|
|
|
130 |
FROM {customcert} cert
|
|
|
131 |
WHERE cert.course = :courseid";
|
|
|
132 |
$DB->delete_records_select('customcert_issues', "customcertid IN ($sql)", ['courseid' => $data->courseid]);
|
|
|
133 |
$status[] = ['component' => $componentstr, 'item' => get_string('deleteissuedcertificates', 'customcert'),
|
|
|
134 |
'error' => false];
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
return $status;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Implementation of the function for printing the form elements that control
|
|
|
142 |
* whether the course reset functionality affects the customcert.
|
|
|
143 |
*
|
|
|
144 |
* @param mod_customcert_mod_form $mform form passed by reference
|
|
|
145 |
*/
|
|
|
146 |
function customcert_reset_course_form_definition(&$mform) {
|
|
|
147 |
$mform->addElement('header', 'customcertheader', get_string('modulenameplural', 'customcert'));
|
|
|
148 |
$mform->addElement('advcheckbox', 'reset_customcert', get_string('deleteissuedcertificates', 'customcert'));
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Course reset form defaults.
|
|
|
153 |
*
|
|
|
154 |
* @param stdClass $course
|
|
|
155 |
* @return array
|
|
|
156 |
*/
|
|
|
157 |
function customcert_reset_course_form_defaults($course) {
|
|
|
158 |
return ['reset_customcert' => 1];
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Returns information about received customcert.
|
|
|
163 |
* Used for user activity reports.
|
|
|
164 |
*
|
|
|
165 |
* @param stdClass $course
|
|
|
166 |
* @param stdClass $user
|
|
|
167 |
* @param stdClass $mod
|
|
|
168 |
* @param stdClass $customcert
|
|
|
169 |
* @return stdClass the user outline object
|
|
|
170 |
*/
|
|
|
171 |
function customcert_user_outline($course, $user, $mod, $customcert) {
|
|
|
172 |
global $DB;
|
|
|
173 |
|
|
|
174 |
$result = new stdClass();
|
|
|
175 |
if ($issue = $DB->get_record('customcert_issues', ['customcertid' => $customcert->id, 'userid' => $user->id])) {
|
|
|
176 |
$result->info = get_string('receiveddate', 'customcert');
|
|
|
177 |
$result->time = $issue->timecreated;
|
|
|
178 |
} else {
|
|
|
179 |
$result->info = get_string('notissued', 'customcert');
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
return $result;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Returns information about received customcert.
|
|
|
187 |
* Used for user activity reports.
|
|
|
188 |
*
|
|
|
189 |
* @param stdClass $course
|
|
|
190 |
* @param stdClass $user
|
|
|
191 |
* @param stdClass $mod
|
|
|
192 |
* @param stdClass $customcert
|
|
|
193 |
* @return string the user complete information
|
|
|
194 |
*/
|
|
|
195 |
function customcert_user_complete($course, $user, $mod, $customcert) {
|
|
|
196 |
global $DB, $OUTPUT;
|
|
|
197 |
|
|
|
198 |
if ($issue = $DB->get_record('customcert_issues', ['customcertid' => $customcert->id, 'userid' => $user->id])) {
|
|
|
199 |
echo $OUTPUT->box_start();
|
|
|
200 |
echo get_string('receiveddate', 'customcert') . ": ";
|
|
|
201 |
echo userdate($issue->timecreated);
|
|
|
202 |
echo $OUTPUT->box_end();
|
|
|
203 |
} else {
|
|
|
204 |
print_string('notissued', 'customcert');
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Serves certificate issues and other files.
|
|
|
210 |
*
|
|
|
211 |
* @param stdClass $course
|
|
|
212 |
* @param stdClass $cm
|
|
|
213 |
* @param context $context
|
|
|
214 |
* @param string $filearea
|
|
|
215 |
* @param array $args
|
|
|
216 |
* @param bool $forcedownload
|
|
|
217 |
* @return bool|null false if file not found, does not return anything if found - just send the file
|
|
|
218 |
*/
|
|
|
219 |
function customcert_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
|
|
|
220 |
global $CFG;
|
|
|
221 |
|
|
|
222 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
223 |
|
|
|
224 |
// We are positioning the elements.
|
|
|
225 |
if ($filearea === 'image') {
|
|
|
226 |
if ($context->contextlevel == CONTEXT_MODULE) {
|
|
|
227 |
require_login($course, false, $cm);
|
|
|
228 |
} else if ($context->contextlevel == CONTEXT_SYSTEM && !has_capability('mod/customcert:manage', $context)) {
|
|
|
229 |
return false;
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
$relativepath = implode('/', $args);
|
|
|
233 |
$fullpath = '/' . $context->id . '/mod_customcert/image/' . $relativepath;
|
|
|
234 |
|
|
|
235 |
$fs = get_file_storage();
|
|
|
236 |
$file = $fs->get_file_by_hash(sha1($fullpath));
|
|
|
237 |
if (!$file || $file->is_directory()) {
|
|
|
238 |
return false;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
send_stored_file($file, 0, 0, $forcedownload);
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* The features this activity supports.
|
|
|
247 |
*
|
|
|
248 |
* @uses FEATURE_GROUPS
|
|
|
249 |
* @uses FEATURE_GROUPINGS
|
|
|
250 |
* @uses FEATURE_GROUPMEMBERSONLY
|
|
|
251 |
* @uses FEATURE_MOD_INTRO
|
|
|
252 |
* @uses FEATURE_COMPLETION_TRACKS_VIEWS
|
|
|
253 |
* @uses FEATURE_GRADE_HAS_GRADE
|
|
|
254 |
* @uses FEATURE_GRADE_OUTCOMES
|
|
|
255 |
* @param string $feature FEATURE_xx constant for requested feature
|
|
|
256 |
* @return mixed True if module supports feature, null if doesn't know
|
|
|
257 |
*/
|
|
|
258 |
function customcert_supports($feature) {
|
|
|
259 |
switch ($feature) {
|
|
|
260 |
case FEATURE_GROUPINGS:
|
|
|
261 |
case FEATURE_MOD_INTRO:
|
|
|
262 |
case FEATURE_SHOW_DESCRIPTION:
|
|
|
263 |
case FEATURE_COMPLETION_TRACKS_VIEWS:
|
|
|
264 |
case FEATURE_BACKUP_MOODLE2:
|
|
|
265 |
case FEATURE_GROUPS:
|
|
|
266 |
return true;
|
|
|
267 |
default:
|
|
|
268 |
return null;
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Used for course participation report (in case customcert is added).
|
|
|
274 |
*
|
|
|
275 |
* @return array
|
|
|
276 |
*/
|
|
|
277 |
function customcert_get_view_actions() {
|
|
|
278 |
return ['view', 'view all', 'view report'];
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* Used for course participation report (in case customcert is added).
|
|
|
283 |
*
|
|
|
284 |
* @return array
|
|
|
285 |
*/
|
|
|
286 |
function customcert_get_post_actions() {
|
|
|
287 |
return ['received'];
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
/**
|
|
|
291 |
* Function to be run periodically according to the moodle cron.
|
|
|
292 |
*/
|
|
|
293 |
function customcert_cron() {
|
|
|
294 |
return true;
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
/**
|
|
|
298 |
* Serve the edit element as a fragment.
|
|
|
299 |
*
|
|
|
300 |
* @param array $args List of named arguments for the fragment loader.
|
|
|
301 |
* @return string
|
|
|
302 |
*/
|
|
|
303 |
function mod_customcert_output_fragment_editelement($args) {
|
|
|
304 |
global $DB;
|
|
|
305 |
|
|
|
306 |
// Get the element.
|
|
|
307 |
$element = $DB->get_record('customcert_elements', ['id' => $args['elementid']], '*', MUST_EXIST);
|
|
|
308 |
|
|
|
309 |
$pageurl = new moodle_url('/mod/customcert/rearrange.php', ['pid' => $element->pageid]);
|
|
|
310 |
$form = new \mod_customcert\edit_element_form($pageurl, ['element' => $element]);
|
|
|
311 |
|
|
|
312 |
return $form->render();
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
/**
|
|
|
316 |
* This function extends the settings navigation block for the site.
|
|
|
317 |
*
|
|
|
318 |
* It is safe to rely on PAGE here as we will only ever be within the module
|
|
|
319 |
* context when this is called.
|
|
|
320 |
*
|
|
|
321 |
* @param settings_navigation $settings
|
|
|
322 |
* @param navigation_node $customcertnode
|
|
|
323 |
*/
|
|
|
324 |
function customcert_extend_settings_navigation(settings_navigation $settings, navigation_node $customcertnode) {
|
|
|
325 |
global $DB, $PAGE;
|
|
|
326 |
|
|
|
327 |
$keys = $customcertnode->get_children_key_list();
|
|
|
328 |
$beforekey = null;
|
|
|
329 |
$i = array_search('modedit', $keys);
|
|
|
330 |
if ($i === false && array_key_exists(0, $keys)) {
|
|
|
331 |
$beforekey = $keys[0];
|
|
|
332 |
} else if (array_key_exists($i + 1, $keys)) {
|
|
|
333 |
$beforekey = $keys[$i + 1];
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
if (has_capability('mod/customcert:manage', $settings->get_page()->cm->context)) {
|
|
|
337 |
// Get the template id.
|
|
|
338 |
$templateid = $DB->get_field('customcert', 'templateid', ['id' => $settings->get_page()->cm->instance]);
|
|
|
339 |
$node = navigation_node::create(get_string('editcustomcert', 'customcert'),
|
|
|
340 |
new moodle_url('/mod/customcert/edit.php', ['tid' => $templateid]),
|
|
|
341 |
navigation_node::TYPE_SETTING, null, 'mod_customcert_edit',
|
|
|
342 |
new pix_icon('t/edit', ''));
|
|
|
343 |
$customcertnode->add_node($node, $beforekey);
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
if (has_capability('mod/customcert:verifycertificate', $settings->get_page()->cm->context)) {
|
|
|
347 |
$node = navigation_node::create(get_string('verifycertificate', 'customcert'),
|
|
|
348 |
new moodle_url('/mod/customcert/verify_certificate.php', ['contextid' => $settings->get_page()->cm->context->id]),
|
|
|
349 |
navigation_node::TYPE_SETTING, null, 'mod_customcert_verify_certificate',
|
|
|
350 |
new pix_icon('t/check', ''));
|
|
|
351 |
$customcertnode->add_node($node, $beforekey);
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
return $customcertnode->trim_if_empty();
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
/**
|
|
|
358 |
* Add nodes to myprofile page.
|
|
|
359 |
*
|
|
|
360 |
* @param \core_user\output\myprofile\tree $tree Tree object
|
|
|
361 |
* @param stdClass $user user object
|
|
|
362 |
* @param bool $iscurrentuser
|
|
|
363 |
* @param stdClass $course Course object
|
|
|
364 |
* @return void
|
|
|
365 |
*/
|
|
|
366 |
function mod_customcert_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
|
|
|
367 |
global $USER;
|
|
|
368 |
|
|
|
369 |
if (($user->id != $USER->id)
|
|
|
370 |
&& !has_capability('mod/customcert:viewallcertificates', context_system::instance())) {
|
|
|
371 |
return;
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
$params = [
|
|
|
375 |
'userid' => $user->id,
|
|
|
376 |
];
|
|
|
377 |
if ($course) {
|
|
|
378 |
$params['course'] = $course->id;
|
|
|
379 |
}
|
|
|
380 |
$url = new moodle_url('/mod/customcert/my_certificates.php', $params);
|
|
|
381 |
$node = new core_user\output\myprofile\node('miscellaneous', 'mycustomcerts',
|
|
|
382 |
get_string('mycertificates', 'customcert'), null, $url);
|
|
|
383 |
$tree->add_node($node);
|
|
|
384 |
}
|
|
|
385 |
|
|
|
386 |
/**
|
|
|
387 |
* Handles editing the 'name' of the element in a list.
|
|
|
388 |
*
|
|
|
389 |
* @param string $itemtype
|
|
|
390 |
* @param int $itemid
|
|
|
391 |
* @param string $newvalue
|
|
|
392 |
* @return \core\output\inplace_editable
|
|
|
393 |
*/
|
|
|
394 |
function mod_customcert_inplace_editable($itemtype, $itemid, $newvalue) {
|
|
|
395 |
global $DB, $PAGE;
|
|
|
396 |
|
|
|
397 |
if ($itemtype === 'elementname') {
|
|
|
398 |
$element = $DB->get_record('customcert_elements', ['id' => $itemid], '*', MUST_EXIST);
|
|
|
399 |
$page = $DB->get_record('customcert_pages', ['id' => $element->pageid], '*', MUST_EXIST);
|
|
|
400 |
$template = $DB->get_record('customcert_templates', ['id' => $page->templateid], '*', MUST_EXIST);
|
|
|
401 |
|
|
|
402 |
// Set the template object.
|
|
|
403 |
$template = new \mod_customcert\template($template);
|
|
|
404 |
// Perform checks.
|
|
|
405 |
if ($cm = $template->get_cm()) {
|
|
|
406 |
require_login($cm->course, false, $cm);
|
|
|
407 |
} else {
|
|
|
408 |
$PAGE->set_context(context_system::instance());
|
|
|
409 |
require_login();
|
|
|
410 |
}
|
|
|
411 |
// Make sure the user has the required capabilities.
|
|
|
412 |
$template->require_manage();
|
|
|
413 |
|
|
|
414 |
// Clean input and update the record.
|
|
|
415 |
$updateelement = new stdClass();
|
|
|
416 |
$updateelement->id = $element->id;
|
|
|
417 |
$updateelement->name = clean_param($newvalue, PARAM_TEXT);
|
|
|
418 |
$DB->update_record('customcert_elements', $updateelement);
|
|
|
419 |
|
|
|
420 |
return new \core\output\inplace_editable('mod_customcert', 'elementname', $element->id, true,
|
|
|
421 |
$updateelement->name, $updateelement->name);
|
|
|
422 |
}
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
/**
|
|
|
426 |
* Get icon mapping for font-awesome.
|
|
|
427 |
*/
|
|
|
428 |
function mod_customcert_get_fontawesome_icon_map() {
|
|
|
429 |
return [
|
|
|
430 |
'mod_customcert:download' => 'fa-download',
|
|
|
431 |
];
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
/**
|
|
|
435 |
* Force custom language for current session.
|
|
|
436 |
*
|
|
|
437 |
* @param string $language
|
|
|
438 |
* @return bool
|
|
|
439 |
*/
|
|
|
440 |
function mod_customcert_force_current_language($language): bool {
|
|
|
441 |
global $USER;
|
|
|
442 |
|
|
|
443 |
$forced = false;
|
|
|
444 |
if (empty($language)) {
|
|
|
445 |
return $forced;
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
$activelangs = get_string_manager()->get_list_of_translations();
|
|
|
449 |
$userlang = $USER->lang ?? current_language();
|
|
|
450 |
|
|
|
451 |
if (array_key_exists($language, $activelangs) && $language != $userlang) {
|
|
|
452 |
force_current_language($language);
|
|
|
453 |
$forced = true;
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
return $forced;
|
|
|
457 |
}
|