2017-02-01 10:23:15 +00:00
|
|
|
<?php
|
2021-11-24 07:29:43 +00:00
|
|
|
// This file is part of the htmlcert module for Moodle - http://moodle.org/
|
2017-02-01 10:23:15 +00:00
|
|
|
//
|
|
|
|
// 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 verifying the code for a certificate.
|
|
|
|
*
|
2021-11-24 07:29:43 +00:00
|
|
|
* @package mod_htmlcert
|
|
|
|
* @copyright 2017 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
|
2017-02-01 10:23:15 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2018-10-30 13:52:10 +00:00
|
|
|
// This file does not need require_login because capability to verify can be granted to guests, skip codechecker here.
|
2017-12-04 13:19:54 +00:00
|
|
|
// @codingStandardsIgnoreLine
|
2017-02-01 10:23:15 +00:00
|
|
|
require_once('../../config.php');
|
|
|
|
|
2017-11-12 04:55:10 +00:00
|
|
|
$contextid = optional_param('contextid', context_system::instance()->id, PARAM_INT);
|
2017-02-01 10:23:15 +00:00
|
|
|
$code = optional_param('code', '', PARAM_ALPHANUM); // The code for the certificate we are verifying.
|
2019-01-03 03:05:25 +00:00
|
|
|
$qrcode = optional_param('qrcode', false, PARAM_BOOL);
|
2017-02-01 10:23:15 +00:00
|
|
|
|
2017-05-30 03:59:06 +00:00
|
|
|
$context = context::instance_by_id($contextid);
|
|
|
|
|
2017-11-12 04:55:10 +00:00
|
|
|
// Set up the page.
|
2021-11-24 07:29:43 +00:00
|
|
|
$pageurl = new moodle_url('/mod/htmlcert/verify_certificate.php', array('contextid' => $contextid));
|
2017-11-12 04:55:10 +00:00
|
|
|
|
2018-06-26 10:50:21 +00:00
|
|
|
if ($code) {
|
|
|
|
$pageurl->param('code', $code);
|
|
|
|
}
|
|
|
|
|
2017-11-12 04:55:10 +00:00
|
|
|
// Ok, a certificate was specified.
|
|
|
|
if ($context->contextlevel != CONTEXT_SYSTEM) {
|
2021-11-24 07:29:43 +00:00
|
|
|
$cm = get_coursemodule_from_id('htmlcert', $context->instanceid, 0, false, MUST_EXIST);
|
2017-11-12 04:55:10 +00:00
|
|
|
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
2021-11-24 07:29:43 +00:00
|
|
|
$htmlcert = $DB->get_record('htmlcert', array('id' => $cm->instance), '*', MUST_EXIST);
|
2017-11-12 04:55:10 +00:00
|
|
|
|
|
|
|
// Check if we are allowing anyone to verify, if so, no need to check login, or permissions.
|
2021-11-24 07:29:43 +00:00
|
|
|
if (!$htmlcert->verifyany) {
|
2017-11-12 04:55:10 +00:00
|
|
|
// Need to be logged in.
|
|
|
|
require_login($course, false, $cm);
|
|
|
|
// Ok, now check the user has the ability to verify certificates.
|
2021-11-24 07:29:43 +00:00
|
|
|
require_capability('mod/htmlcert:verifycertificate', $context);
|
2017-11-12 04:55:10 +00:00
|
|
|
} else {
|
|
|
|
$PAGE->set_cm($cm, $course);
|
|
|
|
}
|
2017-05-30 03:59:06 +00:00
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
$title = $htmlcert->name;
|
2018-06-26 10:50:21 +00:00
|
|
|
$heading = format_string($title);
|
2017-11-12 04:55:10 +00:00
|
|
|
$checkallofsite = false;
|
2017-05-06 11:14:15 +00:00
|
|
|
} else {
|
2018-06-26 10:50:21 +00:00
|
|
|
$title = $SITE->fullname;
|
|
|
|
$heading = $title;
|
|
|
|
$checkallofsite = true;
|
|
|
|
}
|
|
|
|
|
2021-11-24 07:29:43 +00:00
|
|
|
\mod_htmlcert\page_helper::page_setup($pageurl, $context, $title);
|
2018-06-26 10:50:21 +00:00
|
|
|
|
|
|
|
// Additional page setup.
|
|
|
|
if ($context->contextlevel == CONTEXT_SYSTEM) {
|
2021-11-24 07:29:43 +00:00
|
|
|
$PAGE->navbar->add(get_string('verifycertificate', 'htmlcert'));
|
2018-06-26 10:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($checkallofsite) {
|
2021-11-24 07:29:43 +00:00
|
|
|
// If the 'verifyallcertificates' is not set and the user does not have the capability 'mod/htmlcert:verifyallcertificates'
|
2017-11-12 04:55:10 +00:00
|
|
|
// then show them a message letting them know they can not proceed.
|
2021-11-24 07:29:43 +00:00
|
|
|
$verifyallcertificates = get_config('htmlcert', 'verifyallcertificates');
|
|
|
|
$canverifyallcertificates = has_capability('mod/htmlcert:verifyallcertificates', $context);
|
2017-11-12 04:55:10 +00:00
|
|
|
if (!$verifyallcertificates && !$canverifyallcertificates) {
|
|
|
|
echo $OUTPUT->header();
|
2018-06-26 10:50:21 +00:00
|
|
|
echo $OUTPUT->heading($heading);
|
2021-11-24 07:29:43 +00:00
|
|
|
echo $OUTPUT->notification(get_string('cannotverifyallcertificates', 'htmlcert'));
|
2017-11-12 04:55:10 +00:00
|
|
|
echo $OUTPUT->footer();
|
|
|
|
exit();
|
|
|
|
}
|
2017-02-01 10:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The form we are using to verify these codes.
|
2021-11-24 07:29:43 +00:00
|
|
|
$form = new \mod_htmlcert\verify_certificate_form($pageurl);
|
2017-02-01 10:23:15 +00:00
|
|
|
|
2019-01-03 03:05:25 +00:00
|
|
|
if ($code) {
|
2017-02-01 10:23:15 +00:00
|
|
|
$result = new stdClass();
|
|
|
|
$result->issues = array();
|
|
|
|
|
|
|
|
// Ok, now check if the code is valid.
|
2021-11-24 07:29:43 +00:00
|
|
|
$userfields = \mod_htmlcert\helper::get_all_user_name_fields('u');
|
2017-02-16 12:12:19 +00:00
|
|
|
$sql = "SELECT ci.id, u.id as userid, $userfields, co.id as courseid,
|
2018-06-12 08:42:27 +00:00
|
|
|
co.fullname as coursefullname, c.id as certificateid,
|
|
|
|
c.name as certificatename, c.verifyany
|
2021-11-24 07:29:43 +00:00
|
|
|
FROM {htmlcert} c
|
|
|
|
JOIN {htmlcert_issues} ci
|
|
|
|
ON c.id = ci.htmlcertid
|
2017-02-01 10:23:15 +00:00
|
|
|
JOIN {course} co
|
|
|
|
ON c.course = co.id
|
|
|
|
JOIN {user} u
|
|
|
|
ON ci.userid = u.id
|
2017-11-12 04:55:10 +00:00
|
|
|
WHERE ci.code = :code";
|
|
|
|
|
|
|
|
if ($checkallofsite) {
|
|
|
|
// Only people with the capability to verify all the certificates can verify any.
|
|
|
|
if (!$canverifyallcertificates) {
|
|
|
|
$sql .= " AND c.verifyany = 1";
|
|
|
|
}
|
|
|
|
$params = ['code' => $code];
|
|
|
|
} else {
|
2021-11-24 07:29:43 +00:00
|
|
|
$sql .= " AND c.id = :htmlcertid";
|
|
|
|
$params = ['code' => $code, 'htmlcertid' => $htmlcert->id];
|
2017-11-12 04:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 10:23:15 +00:00
|
|
|
// It is possible (though unlikely) that there is the same code for issued certificates.
|
2017-11-12 04:55:10 +00:00
|
|
|
if ($issues = $DB->get_records_sql($sql, $params)) {
|
2017-02-01 10:23:15 +00:00
|
|
|
$result->success = true;
|
|
|
|
$result->issues = $issues;
|
|
|
|
} else {
|
|
|
|
// Can't find it, let's say it's not verified.
|
|
|
|
$result->success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $OUTPUT->header();
|
2018-06-26 10:50:21 +00:00
|
|
|
echo $OUTPUT->heading($heading);
|
2019-01-03 03:05:25 +00:00
|
|
|
// Don't show the form if we are coming from a QR code.
|
|
|
|
if (!$qrcode) {
|
|
|
|
echo $form->display();
|
|
|
|
}
|
2017-02-01 10:23:15 +00:00
|
|
|
if (isset($result)) {
|
2021-11-24 07:29:43 +00:00
|
|
|
$renderer = $PAGE->get_renderer('mod_htmlcert');
|
|
|
|
$result = new \mod_htmlcert\output\verify_certificate_results($result);
|
2017-02-01 10:23:15 +00:00
|
|
|
echo $renderer->render($result);
|
|
|
|
}
|
|
|
|
echo $OUTPUT->footer();
|