1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of 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 |
* Serves files for the mobile app.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
21 |
* @copyright 2018 Mark Nelson <markn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* AJAX_SCRIPT - exception will be converted into JSON.
|
|
|
27 |
*/
|
|
|
28 |
define('AJAX_SCRIPT', true);
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* NO_MOODLE_COOKIES - we don't want any cookie.
|
|
|
32 |
*/
|
|
|
33 |
define('NO_MOODLE_COOKIES', true);
|
|
|
34 |
|
|
|
35 |
require_once('../../../config.php');
|
|
|
36 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
37 |
require_once($CFG->libdir . '/completionlib.php');
|
|
|
38 |
require_once($CFG->dirroot . '/webservice/lib.php');
|
|
|
39 |
require_once($CFG->dirroot . '/mod/customcert/lib.php');
|
|
|
40 |
|
|
|
41 |
// Allow CORS requests.
|
|
|
42 |
header('Access-Control-Allow-Origin: *');
|
|
|
43 |
|
|
|
44 |
// Authenticate the user.
|
|
|
45 |
$token = required_param('token', PARAM_ALPHANUM);
|
|
|
46 |
$certificateid = required_param('certificateid', PARAM_INT);
|
|
|
47 |
$userid = required_param('userid', PARAM_INT);
|
|
|
48 |
|
|
|
49 |
$webservicelib = new webservice();
|
|
|
50 |
$authenticationinfo = $webservicelib->authenticate_user($token);
|
|
|
51 |
|
|
|
52 |
// Check the service allows file download.
|
|
|
53 |
$enabledfiledownload = (int) ($authenticationinfo['service']->downloadfiles);
|
|
|
54 |
if (empty($enabledfiledownload)) {
|
|
|
55 |
throw new webservice_access_exception('Web service file downloading must be enabled in external service settings');
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
$cm = get_coursemodule_from_instance('customcert', $certificateid, 0, false, MUST_EXIST);
|
|
|
59 |
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
|
|
|
60 |
$certificate = $DB->get_record('customcert', ['id' => $certificateid], '*', MUST_EXIST);
|
|
|
61 |
$template = $DB->get_record('customcert_templates', ['id' => $certificate->templateid], '*', MUST_EXIST);
|
|
|
62 |
|
|
|
63 |
// Capabilities check.
|
|
|
64 |
require_capability('mod/customcert:view', \context_module::instance($cm->id));
|
|
|
65 |
if ($userid != $USER->id) {
|
|
|
66 |
require_capability('mod/customcert:viewreport', \context_module::instance($cm->id));
|
|
|
67 |
} else {
|
|
|
68 |
// Make sure the user has met the required time.
|
|
|
69 |
if ($certificate->requiredtime) {
|
|
|
70 |
if (\mod_customcert\certificate::get_course_time($certificate->course) < ($certificate->requiredtime * 60)) {
|
|
|
71 |
exit();
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$issue = $DB->get_record('customcert_issues', ['customcertid' => $certificateid, 'userid' => $userid]);
|
|
|
77 |
|
|
|
78 |
// If we are doing it for the logged in user then we want to issue the certificate.
|
|
|
79 |
if (!$issue) {
|
|
|
80 |
// If the other user doesn't have an issue, then there is nothing to do.
|
|
|
81 |
if ($userid != $USER->id) {
|
|
|
82 |
exit();
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
\mod_customcert\certificate::issue_certificate($certificate->id, $USER->id);
|
|
|
86 |
|
|
|
87 |
// Set the custom certificate as viewed.
|
|
|
88 |
$completion = new completion_info($course);
|
|
|
89 |
$completion->set_module_viewed($cm);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// Now we want to generate the PDF.
|
|
|
93 |
$template = new \mod_customcert\template($template);
|
|
|
94 |
$template->generate_pdf(false, $userid);
|
|
|
95 |
exit();
|