-
-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathmy_certificates.php
More file actions
98 lines (85 loc) · 3.66 KB
/
my_certificates.php
File metadata and controls
98 lines (85 loc) · 3.66 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
<?php
// This file is part of the customcert module 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/>.
/**
* Handles viewing the certificates for a certain user.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_customcert\local\pagination;
use mod_customcert\my_certificates_table;
use mod_customcert\service\certificate_repository;
use mod_customcert\service\issue_repository;
use mod_customcert\service\pdf_generation_service;
use mod_customcert\template;
require_once('../../config.php');
$userid = optional_param('userid', $USER->id, PARAM_INT);
$download = optional_param('download', null, PARAM_ALPHA);
$courseid = optional_param('course', null, PARAM_INT);
$downloadcert = optional_param('downloadcert', '', PARAM_BOOL);
if ($downloadcert) {
$certificateid = required_param('certificateid', PARAM_INT);
$certrepo = new certificate_repository();
$customcert = $certrepo->get_by_id($certificateid);
// Check there exists an issued certificate for this user.
$issuerepo = new issue_repository();
if (!$issuerepo->find_by_user_certificate((int)$customcert->id, $userid)) {
throw new moodle_exception('You have not been issued a certificate');
}
}
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', pagination::CUSTOMCERT_PER_PAGE, PARAM_INT);
$pageurl = $url = new moodle_url('/mod/customcert/my_certificates.php', ['userid' => $userid,
'page' => $page, 'perpage' => $perpage]);
// Requires a login.
if ($courseid) {
require_login($courseid);
} else {
require_login();
}
// Check that we have a valid user.
$user = core_user::get_user($userid, '*', MUST_EXIST);
// If we are viewing certificates that are not for the currently logged in user then do a capability check.
if (($userid != $USER->id) && !has_capability('mod/customcert:viewallcertificates', context_system::instance())) {
throw new moodle_exception('You are not allowed to view these certificates');
}
$PAGE->set_url($pageurl);
$PAGE->set_context(context_user::instance($userid));
$PAGE->set_title(get_string('mycertificates', 'customcert'));
$PAGE->set_pagelayout('standard');
$PAGE->navigation->extend_for_user($user);
// Check if we requested to download a certificate.
if ($downloadcert) {
$template = template::load((int)$customcert->templateid);
$pdfservice = pdf_generation_service::create();
$pdfservice->generate_pdf($template, false, (int)$userid);
exit();
}
$table = new my_certificates_table($userid, $download);
$table->define_baseurl($pageurl);
if ($table->is_downloading()) {
$table->download();
exit();
}
// Additional page setup.
$PAGE->navbar->add(get_string('profile'), new moodle_url('/user/profile.php', ['id' => $userid]));
$PAGE->navbar->add(get_string('mycertificates', 'customcert'));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('mycertificates', 'customcert'));
echo html_writer::div(get_string('mycertificatesdescription', 'customcert'));
$table->out($perpage, false);
echo $OUTPUT->footer();