2018-05-23 06:35:23 +00:00
|
|
|
<?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/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serves files for the mobile app.
|
|
|
|
*
|
2021-11-24 07:29:43 +00:00
|
|
|
* @package mod_htmlcert
|
2018-05-23 06:35:23 +00:00
|
|
|
* @copyright 2018 Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AJAX_SCRIPT - exception will be converted into JSON.
|
|
|
|
*/
|
|
|
|
define('AJAX_SCRIPT', true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NO_MOODLE_COOKIES - we don't want any cookie.
|
|
|
|
*/
|
|
|
|
define('NO_MOODLE_COOKIES', true);
|
|
|
|
|
|
|
|
require_once('../../../config.php');
|
|
|
|
require_once($CFG->libdir . '/filelib.php');
|
2020-05-30 11:03:02 +00:00
|
|
|
require_once($CFG->libdir . '/completionlib.php');
|
2018-05-23 06:35:23 +00:00
|
|
|
require_once($CFG->dirroot . '/webservice/lib.php');
|
|
|
|
|
|
|
|
// Allow CORS requests.
|
|
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
|
|
|
|
// Authenticate the user.
|
|
|
|
$token = required_param('token', PARAM_ALPHANUM);
|
|
|
|
$certificateid = required_param('certificateid', PARAM_INT);
|
|
|
|
$userid = required_param('userid', PARAM_INT);
|
|
|
|
|
|
|
|
$webservicelib = new webservice();
|
|
|
|
$authenticationinfo = $webservicelib->authenticate_user($token);
|
|
|
|
|
|
|
|
// Check the service allows file download.
|
|
|
|
$enabledfiledownload = (int) ($authenticationinfo['service']->downloadfiles);
|
|
|
|
if (empty($enabledfiledownload)) {
|
|
|
|
throw new webservice_access_exception('Web service file downloading must be enabled in external service settings');
|
|
|
|
}
|
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$cm = get_coursemodule_from_instance('htmlcert', $certificateid, 0, false, MUST_EXIST);
|
2020-05-30 11:03:02 +00:00
|
|
|
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
2021-11-24 07:29:43 +00:00
|
|
|
$certificate = $DB->get_record('htmlcert', ['id' => $certificateid], '*', MUST_EXIST);
|
|
|
|
$template = $DB->get_record('htmlcert_templates', ['id' => $certificate->templateid], '*', MUST_EXIST);
|
2018-05-23 06:35:23 +00:00
|
|
|
|
|
|
|
// Capabilities check.
|
2021-11-24 07:29:43 +00:00
|
|
|
require_capability('mod/htmlcert:view', \context_module::instance($cm->id));
|
2018-05-23 06:35:23 +00:00
|
|
|
if ($userid != $USER->id) {
|
2021-11-24 07:29:43 +00:00
|
|
|
require_capability('mod/htmlcert:viewreport', \context_module::instance($cm->id));
|
2018-05-23 06:35:23 +00:00
|
|
|
} else {
|
|
|
|
// Make sure the user has met the required time.
|
|
|
|
if ($certificate->requiredtime) {
|
2021-11-24 07:29:43 +00:00
|
|
|
if (\mod_htmlcert\certificate::get_course_time($certificate->course) < ($certificate->requiredtime * 60)) {
|
2018-05-23 06:35:23 +00:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$issue = $DB->get_record('htmlcert_issues', ['htmlcertid' => $certificateid, 'userid' => $userid]);
|
2018-05-23 06:35:23 +00:00
|
|
|
|
|
|
|
// If we are doing it for the logged in user then we want to issue the certificate.
|
|
|
|
if (!$issue) {
|
|
|
|
// If the other user doesn't have an issue, then there is nothing to do.
|
|
|
|
if ($userid != $USER->id) {
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
\mod_htmlcert\certificate::issue_certificate($certificate->id, $USER->id);
|
2020-05-30 11:03:02 +00:00
|
|
|
|
|
|
|
// Set the custom certificate as viewed.
|
|
|
|
$completion = new completion_info($course);
|
|
|
|
$completion->set_module_viewed($cm);
|
2018-05-23 06:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we want to generate the PDF.
|
2021-11-24 07:29:43 +00:00
|
|
|
$template = new \mod_htmlcert\template($template);
|
2018-05-23 06:35:23 +00:00
|
|
|
$template->generate_pdf(false, $userid);
|
|
|
|
exit();
|