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 |
* This file contains the customcert element QR code's core interaction API.
|
|
|
19 |
*
|
|
|
20 |
* @package customcertelement_qrcode
|
|
|
21 |
* @copyright 2019 Mark Nelson <markn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace customcertelement_qrcode;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
require_once($CFG->libdir . '/tcpdf/tcpdf_barcodes_2d.php');
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* The customcert element QR code's core interaction API.
|
|
|
33 |
*
|
|
|
34 |
* @package customcertelement_qrcode
|
|
|
35 |
* @copyright 2019 Mark Nelson <markn@moodle.com>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class element extends \mod_customcert\element {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* @var string The barcode type.
|
|
|
42 |
*/
|
|
|
43 |
const BARCODETYPE = 'QRCODE';
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* This function renders the form elements when adding a customcert element.
|
|
|
47 |
*
|
|
|
48 |
* @param \MoodleQuickForm $mform the edit_form instance
|
|
|
49 |
*/
|
|
|
50 |
public function render_form_elements($mform) {
|
|
|
51 |
\mod_customcert\element_helper::render_form_element_width($mform);
|
|
|
52 |
|
|
|
53 |
\mod_customcert\element_helper::render_form_element_height($mform);
|
|
|
54 |
|
|
|
55 |
if ($this->showposxy) {
|
|
|
56 |
\mod_customcert\element_helper::render_form_element_position($mform);
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Performs validation on the element values.
|
|
|
62 |
*
|
|
|
63 |
* @param array $data the submitted data
|
|
|
64 |
* @param array $files the submitted files
|
|
|
65 |
* @return array the validation errors
|
|
|
66 |
*/
|
|
|
67 |
public function validate_form_elements($data, $files) {
|
|
|
68 |
// Array to return the errors.
|
|
|
69 |
$errors = [];
|
|
|
70 |
|
|
|
71 |
// Validate the width.
|
|
|
72 |
$errors += \mod_customcert\element_helper::validate_form_element_width($data, false);
|
|
|
73 |
|
|
|
74 |
// Validate the height.
|
|
|
75 |
$errors += \mod_customcert\element_helper::validate_form_element_height($data, false);
|
|
|
76 |
|
|
|
77 |
if ($this->showposxy) {
|
|
|
78 |
$errors += \mod_customcert\element_helper::validate_form_element_position($data);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
return $errors;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* This will handle how form data will be saved into the data column in the
|
|
|
86 |
* customcert_elements table.
|
|
|
87 |
*
|
|
|
88 |
* @param \stdClass $data the form data
|
|
|
89 |
* @return string the json encoded array
|
|
|
90 |
*/
|
|
|
91 |
public function save_unique_data($data) {
|
|
|
92 |
$arrtostore = [
|
|
|
93 |
'width' => !empty($data->width) ? (int)$data->width : 0,
|
|
|
94 |
'height' => !empty($data->height) ? (int)$data->height : 0,
|
|
|
95 |
];
|
|
|
96 |
|
|
|
97 |
return json_encode($arrtostore);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Sets the data on the form when editing an element.
|
|
|
102 |
*
|
|
|
103 |
* @param \MoodleQuickForm $mform the edit_form instance
|
|
|
104 |
*/
|
|
|
105 |
public function definition_after_data($mform) {
|
|
|
106 |
parent::definition_after_data($mform);
|
|
|
107 |
|
|
|
108 |
// Set the image, width, height and alpha channel for this element.
|
|
|
109 |
if (!empty($this->get_data())) {
|
|
|
110 |
$imageinfo = json_decode($this->get_data());
|
|
|
111 |
|
|
|
112 |
if (!empty($imageinfo->height)) {
|
|
|
113 |
$element = $mform->getElement('height');
|
|
|
114 |
$element->setValue($imageinfo->height);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Handles rendering the element on the pdf.
|
|
|
121 |
*
|
|
|
122 |
* @param \pdf $pdf the pdf object
|
|
|
123 |
* @param bool $preview true if it is a preview, false otherwise
|
|
|
124 |
* @param \stdClass $user the user we are rendering this for
|
|
|
125 |
*/
|
|
|
126 |
public function render($pdf, $preview, $user) {
|
|
|
127 |
global $DB;
|
|
|
128 |
|
|
|
129 |
// If there is no element data, we have nothing to display.
|
|
|
130 |
if (empty($this->get_data())) {
|
|
|
131 |
return;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
$imageinfo = json_decode($this->get_data());
|
|
|
135 |
|
|
|
136 |
if ($preview) {
|
|
|
137 |
// Generate the URL to verify this.
|
|
|
138 |
$qrcodeurl = new \moodle_url('/');
|
|
|
139 |
$qrcodeurl = $qrcodeurl->out(false);
|
|
|
140 |
} else {
|
|
|
141 |
// Get the information we need.
|
|
|
142 |
$sql = "SELECT c.id, c.verifyany, ct.contextid, ci.code
|
|
|
143 |
FROM {customcert_issues} ci
|
|
|
144 |
JOIN {customcert} c
|
|
|
145 |
ON ci.customcertid = c.id
|
|
|
146 |
JOIN {customcert_templates} ct
|
|
|
147 |
ON c.templateid = ct.id
|
|
|
148 |
JOIN {customcert_pages} cp
|
|
|
149 |
ON cp.templateid = ct.id
|
|
|
150 |
WHERE ci.userid = :userid
|
|
|
151 |
AND cp.id = :pageid";
|
|
|
152 |
|
|
|
153 |
// Now we can get the issue for this user.
|
|
|
154 |
$issue = $DB->get_record_sql($sql, ['userid' => $user->id, 'pageid' => $this->get_pageid()],
|
|
|
155 |
'*', MUST_EXIST);
|
|
|
156 |
$code = $issue->code;
|
|
|
157 |
|
|
|
158 |
$context = \context::instance_by_id($issue->contextid);
|
|
|
159 |
|
|
|
160 |
$urlparams = [
|
|
|
161 |
'code' => $code,
|
|
|
162 |
'qrcode' => 1,
|
|
|
163 |
];
|
|
|
164 |
|
|
|
165 |
// We only add the 'contextid' to the link if the site setting for verifying all certificates is off,
|
|
|
166 |
// or if the individual certificate doesn't allow verification. However, if the user has the
|
|
|
167 |
// mod/customcert:verifyallcertificates then they can verify anything regardless.
|
|
|
168 |
$verifyallcertificatessitesetting = get_config('customcert', 'verifyallcertificates');
|
|
|
169 |
$verifycertificateactivitysettings = $issue->verifyany;
|
|
|
170 |
$canverifyallcertificates = has_capability('mod/customcert:verifyallcertificates', $context);
|
|
|
171 |
if ((!$verifyallcertificatessitesetting || !$verifycertificateactivitysettings)
|
|
|
172 |
&& !$canverifyallcertificates) {
|
|
|
173 |
$urlparams['contextid'] = $issue->contextid;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
$qrcodeurl = new \moodle_url('/mod/customcert/verify_certificate.php', $urlparams);
|
|
|
177 |
$qrcodeurl = $qrcodeurl->out(false);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
$barcode = new \TCPDF2DBarcode($qrcodeurl, self::BARCODETYPE);
|
|
|
181 |
$image = $barcode->getBarcodePngData($imageinfo->width, $imageinfo->height);
|
|
|
182 |
|
|
|
183 |
$location = make_request_directory() . '/target';
|
|
|
184 |
file_put_contents($location, $image);
|
|
|
185 |
|
|
|
186 |
$pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Render the element in html.
|
|
|
191 |
*
|
|
|
192 |
* This function is used to render the element when we are using the
|
|
|
193 |
* drag and drop interface to position it.
|
|
|
194 |
*
|
|
|
195 |
* @return string the html
|
|
|
196 |
*/
|
|
|
197 |
public function render_html() {
|
|
|
198 |
// If there is no element data, we have nothing to display.
|
|
|
199 |
if (empty($this->get_data())) {
|
|
|
200 |
return;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
$imageinfo = json_decode($this->get_data());
|
|
|
204 |
|
|
|
205 |
$qrcodeurl = new \moodle_url('/');
|
|
|
206 |
$qrcodeurl = $qrcodeurl->out(false);
|
|
|
207 |
|
|
|
208 |
$barcode = new \TCPDF2DBarcode($qrcodeurl, self::BARCODETYPE);
|
|
|
209 |
return $barcode->getBarcodeHTML($imageinfo->width / 10, $imageinfo->height / 10);
|
|
|
210 |
}
|
|
|
211 |
}
|