Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Handles verifying the code for a certificate.
19
 *
20
 * @package   mod_customcert
21
 * @copyright 2017 Mark Nelson <markn@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
// This file does not need require_login because capability to verify can be granted to guests, skip codechecker here.
26
// @codingStandardsIgnoreLine
27
require_once('../../config.php');
28
 
29
$contextid = optional_param('contextid', context_system::instance()->id, PARAM_INT);
30
$code = optional_param('code', '', PARAM_ALPHANUM); // The code for the certificate we are verifying.
31
$qrcode = optional_param('qrcode', false, PARAM_BOOL);
32
 
33
$context = context::instance_by_id($contextid);
34
 
35
// Set up the page.
36
$pageurl = new moodle_url('/mod/customcert/verify_certificate.php', ['contextid' => $contextid]);
37
 
38
if ($code) {
39
    $pageurl->param('code', $code);
40
}
41
 
42
// Ok, a certificate was specified.
43
if ($context->contextlevel != CONTEXT_SYSTEM) {
44
    $cm = get_coursemodule_from_id('customcert', $context->instanceid, 0, false, MUST_EXIST);
45
    $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
46
    $customcert = $DB->get_record('customcert', ['id' => $cm->instance], '*', MUST_EXIST);
47
 
48
    // Check if we are allowing anyone to verify, if so, no need to check login, or permissions.
49
    if (!$customcert->verifyany) {
50
        // Need to be logged in.
51
        require_login($course, false, $cm);
52
        // Ok, now check the user has the ability to verify certificates.
53
        require_capability('mod/customcert:verifycertificate', $context);
54
    } else {
55
        $PAGE->set_cm($cm, $course);
56
    }
57
 
58
    $title = $customcert->name;
59
    $checkallofsite = false;
60
} else {
61
    $title = $SITE->fullname;
62
    $checkallofsite = true;
63
}
64
 
65
\mod_customcert\page_helper::page_setup($pageurl, $context, $title);
66
$PAGE->activityheader->set_attrs(['hidecompletion' => true,
67
            'description' => '']);
68
 
69
// Additional page setup.
70
if ($context->contextlevel == CONTEXT_SYSTEM) {
71
    $PAGE->navbar->add(get_string('verifycertificate', 'customcert'));
72
}
73
 
74
if ($checkallofsite) {
75
    // If the 'verifyallcertificates' is not set and the user does not have the capability 'mod/customcert:verifyallcertificates'
76
    // then show them a message letting them know they can not proceed.
77
    $verifyallcertificates = get_config('customcert', 'verifyallcertificates');
78
    $canverifyallcertificates = has_capability('mod/customcert:verifyallcertificates', $context);
79
    if (!$verifyallcertificates && !$canverifyallcertificates) {
80
        echo $OUTPUT->header();
81
        echo $OUTPUT->notification(get_string('cannotverifyallcertificates', 'customcert'));
82
        echo $OUTPUT->footer();
83
        exit();
84
    }
85
}
86
 
87
// The form we are using to verify these codes.
88
$form = new \mod_customcert\verify_certificate_form($pageurl);
89
 
90
if ($code) {
91
    $result = new stdClass();
92
    $result->issues = [];
93
 
94
    // Ok, now check if the code is valid.
95
    $userfields = \mod_customcert\helper::get_all_user_name_fields('u');
96
    $sql = "SELECT ci.id, u.id as userid, $userfields, co.id as courseid,
97
                   co.fullname as coursefullname, c.id as certificateid,
98
                   c.name as certificatename, c.verifyany
99
              FROM {customcert} c
100
              JOIN {customcert_issues} ci
101
                ON c.id = ci.customcertid
102
              JOIN {course} co
103
                ON c.course = co.id
104
              JOIN {user} u
105
                ON ci.userid = u.id
106
             WHERE ci.code = :code";
107
 
108
    if ($checkallofsite) {
109
        // Only people with the capability to verify all the certificates can verify any.
110
        if (!$canverifyallcertificates) {
111
            $sql .= " AND c.verifyany = 1";
112
        }
113
        $params = ['code' => $code];
114
    } else {
115
        $sql .= " AND c.id = :customcertid";
116
        $params = ['code' => $code, 'customcertid' => $customcert->id];
117
    }
118
 
119
    // It is possible (though unlikely) that there is the same code for issued certificates.
120
    if ($issues = $DB->get_records_sql($sql, $params)) {
121
        $result->success = true;
122
        $result->issues = $issues;
123
    } else {
124
        // Can't find it, let's say it's not verified.
125
        $result->success = false;
126
    }
127
}
128
 
129
echo $OUTPUT->header();
130
// Don't show the form if we are coming from a QR code.
131
if (!$qrcode) {
132
    echo $form->display();
133
}
134
if (isset($result)) {
135
    $renderer = $PAGE->get_renderer('mod_customcert');
136
    $result = new \mod_customcert\output\verify_certificate_results($result);
137
    echo $renderer->render($result);
138
}
139
echo $OUTPUT->footer();