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 |
* Provides functionality needed by customcert activities.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
21 |
* @copyright 2016 Mark Nelson <markn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace mod_customcert;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Class certificate.
|
|
|
29 |
*
|
|
|
30 |
* Helper functionality for certificates.
|
|
|
31 |
*
|
|
|
32 |
* @package mod_customcert
|
|
|
33 |
* @copyright 2016 Mark Nelson <markn@moodle.com>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class certificate {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Send the file inline to the browser.
|
|
|
40 |
*/
|
|
|
41 |
const DELIVERY_OPTION_INLINE = 'I';
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Send to the browser and force a file download
|
|
|
45 |
*/
|
|
|
46 |
const DELIVERY_OPTION_DOWNLOAD = 'D';
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* @var string the print protection variable
|
|
|
50 |
*/
|
|
|
51 |
const PROTECTION_PRINT = 'print';
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* @var string the modify protection variable
|
|
|
55 |
*/
|
|
|
56 |
const PROTECTION_MODIFY = 'modify';
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* @var string the copy protection variable
|
|
|
60 |
*/
|
|
|
61 |
const PROTECTION_COPY = 'copy';
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* @var int the number of issues that will be displayed on each page in the report
|
|
|
65 |
* If you want to display all customcerts on a page set this to 0.
|
|
|
66 |
*/
|
|
|
67 |
const CUSTOMCERT_PER_PAGE = '50';
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Date format in filename for download all zip file.
|
|
|
71 |
*/
|
|
|
72 |
private const ZIP_FILE_NAME_DOWNLOAD_ALL_CERTIFICATES_DATE_FORMAT = '%Y%m%d%H%M%S';
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* The ending part of the name of the zip file.
|
|
|
76 |
*/
|
|
|
77 |
private const ZIP_FILE_NAME_DOWNLOAD_ALL_CERTIFICATES = 'all_certificates.zip';
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Handles setting the protection field for the customcert
|
|
|
81 |
*
|
|
|
82 |
* @param \stdClass $data
|
|
|
83 |
* @return string the value to insert into the protection field
|
|
|
84 |
*/
|
|
|
85 |
public static function set_protection($data) {
|
|
|
86 |
$protection = [];
|
|
|
87 |
|
|
|
88 |
if (!empty($data->protection_print)) {
|
|
|
89 |
$protection[] = self::PROTECTION_PRINT;
|
|
|
90 |
}
|
|
|
91 |
if (!empty($data->protection_modify)) {
|
|
|
92 |
$protection[] = self::PROTECTION_MODIFY;
|
|
|
93 |
}
|
|
|
94 |
if (!empty($data->protection_copy)) {
|
|
|
95 |
$protection[] = self::PROTECTION_COPY;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// Return the protection string.
|
|
|
99 |
return implode(', ', $protection);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Handles uploading an image for the customcert module.
|
|
|
104 |
*
|
|
|
105 |
* @param int $draftitemid the draft area containing the files
|
|
|
106 |
* @param int $contextid the context we are storing this image in
|
|
|
107 |
* @param string $filearea indentifies the file area.
|
|
|
108 |
*/
|
|
|
109 |
public static function upload_files($draftitemid, $contextid, $filearea = 'image') {
|
|
|
110 |
global $CFG;
|
|
|
111 |
|
|
|
112 |
// Save the file if it exists that is currently in the draft area.
|
|
|
113 |
require_once($CFG->dirroot . '/lib/filelib.php');
|
|
|
114 |
file_save_draft_area_files($draftitemid, $contextid, 'mod_customcert', $filearea, 0);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Return the list of possible fonts to use.
|
|
|
119 |
*/
|
|
|
120 |
public static function get_fonts() {
|
|
|
121 |
global $CFG;
|
|
|
122 |
|
|
|
123 |
require_once($CFG->libdir . '/pdflib.php');
|
|
|
124 |
|
|
|
125 |
$arrfonts = [];
|
|
|
126 |
$pdf = new \pdf();
|
|
|
127 |
$fontfamilies = $pdf->get_font_families();
|
|
|
128 |
foreach ($fontfamilies as $fontfamily => $fontstyles) {
|
|
|
129 |
foreach ($fontstyles as $fontstyle) {
|
|
|
130 |
$fontstyle = strtolower($fontstyle);
|
|
|
131 |
if ($fontstyle == 'r') {
|
|
|
132 |
$filenamewoextension = $fontfamily;
|
|
|
133 |
} else {
|
|
|
134 |
$filenamewoextension = $fontfamily . $fontstyle;
|
|
|
135 |
}
|
|
|
136 |
$fullpath = \TCPDF_FONTS::_getfontpath() . $filenamewoextension;
|
|
|
137 |
// Set the name of the font to null, the include next should then set this
|
|
|
138 |
// value, if it is not set then the file does not include the necessary data.
|
|
|
139 |
$name = null;
|
|
|
140 |
// Some files include a display name, the include next should then set this
|
|
|
141 |
// value if it is present, if not then $name is used to create the display name.
|
|
|
142 |
$displayname = null;
|
|
|
143 |
// Some of the TCPDF files include files that are not present, so we have to
|
|
|
144 |
// suppress warnings, this is the TCPDF libraries fault, grrr.
|
|
|
145 |
@include($fullpath . '.php');
|
|
|
146 |
// If no $name variable in file, skip it.
|
|
|
147 |
if (is_null($name)) {
|
|
|
148 |
continue;
|
|
|
149 |
}
|
|
|
150 |
// Check if there is no display name to use.
|
|
|
151 |
if (is_null($displayname)) {
|
|
|
152 |
// Format the font name, so "FontName-Style" becomes "Font Name - Style".
|
|
|
153 |
$displayname = preg_replace("/([a-z])([A-Z])/", "$1 $2", $name);
|
|
|
154 |
$displayname = preg_replace("/([a-zA-Z])-([a-zA-Z])/", "$1 - $2", $displayname);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
$arrfonts[$filenamewoextension] = $displayname;
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
ksort($arrfonts);
|
|
|
161 |
|
|
|
162 |
return $arrfonts;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Return the list of possible font sizes to use.
|
|
|
167 |
*/
|
|
|
168 |
public static function get_font_sizes() {
|
|
|
169 |
// Array to store the sizes.
|
|
|
170 |
$sizes = [];
|
|
|
171 |
|
|
|
172 |
for ($i = 1; $i <= 200; $i++) {
|
|
|
173 |
$sizes[$i] = $i;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
return $sizes;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Get the time the user has spent in the course.
|
|
|
181 |
*
|
|
|
182 |
* @param int $courseid
|
|
|
183 |
* @param int $userid
|
|
|
184 |
* @return int the total time spent in seconds
|
|
|
185 |
*/
|
|
|
186 |
public static function get_course_time(int $courseid, int $userid = 0): int {
|
|
|
187 |
global $CFG, $DB, $USER;
|
|
|
188 |
|
|
|
189 |
if (empty($userid)) {
|
|
|
190 |
$userid = $USER->id;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
$logmanager = get_log_manager();
|
|
|
194 |
$readers = $logmanager->get_readers();
|
|
|
195 |
$enabledreaders = get_config('tool_log', 'enabled_stores');
|
|
|
196 |
if (empty($enabledreaders)) {
|
|
|
197 |
return 0;
|
|
|
198 |
}
|
|
|
199 |
$enabledreaders = explode(',', $enabledreaders);
|
|
|
200 |
|
|
|
201 |
// Go through all the readers until we find one that we can use.
|
|
|
202 |
foreach ($enabledreaders as $enabledreader) {
|
|
|
203 |
$reader = $readers[$enabledreader];
|
|
|
204 |
if ($reader instanceof \logstore_legacy\log\store) {
|
|
|
205 |
$logtable = 'log';
|
|
|
206 |
$coursefield = 'course';
|
|
|
207 |
$timefield = 'time';
|
|
|
208 |
break;
|
|
|
209 |
} else if ($reader instanceof \core\log\sql_internal_table_reader) {
|
|
|
210 |
$logtable = $reader->get_internal_log_table_name();
|
|
|
211 |
$coursefield = 'courseid';
|
|
|
212 |
$timefield = 'timecreated';
|
|
|
213 |
break;
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
// If we didn't find a reader then return 0.
|
|
|
218 |
if (!isset($logtable)) {
|
|
|
219 |
return 0;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
$sql = "SELECT id, $timefield
|
|
|
223 |
FROM {{$logtable}}
|
|
|
224 |
WHERE userid = :userid
|
|
|
225 |
AND $coursefield = :courseid
|
|
|
226 |
ORDER BY $timefield ASC";
|
|
|
227 |
$params = ['userid' => $userid, 'courseid' => $courseid];
|
|
|
228 |
$totaltime = 0;
|
|
|
229 |
if ($logs = $DB->get_recordset_sql($sql, $params)) {
|
|
|
230 |
foreach ($logs as $log) {
|
|
|
231 |
if (!isset($login)) {
|
|
|
232 |
// For the first time $login is not set so the first log is also the first login.
|
|
|
233 |
$login = $log->$timefield;
|
|
|
234 |
$lasthit = $log->$timefield;
|
|
|
235 |
$totaltime = 0;
|
|
|
236 |
}
|
|
|
237 |
$delay = $log->$timefield - $lasthit;
|
|
|
238 |
if ($delay > $CFG->sessiontimeout) {
|
|
|
239 |
// The difference between the last log and the current log is more than
|
|
|
240 |
// the timeout Register session value so that we have found a session!
|
|
|
241 |
$login = $log->$timefield;
|
|
|
242 |
} else {
|
|
|
243 |
$totaltime += $delay;
|
|
|
244 |
}
|
|
|
245 |
// Now the actual log became the previous log for the next cycle.
|
|
|
246 |
$lasthit = $log->$timefield;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
return $totaltime;
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
return 0;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Download all certificate issues.
|
|
|
257 |
*
|
|
|
258 |
* @param template $template
|
|
|
259 |
* @param array $issues
|
|
|
260 |
* @return void
|
|
|
261 |
* @throws \moodle_exception
|
|
|
262 |
*/
|
|
|
263 |
public static function download_all_issues_for_instance(\mod_customcert\template $template, array $issues): void {
|
|
|
264 |
$zipdir = make_request_directory();
|
|
|
265 |
if (!$zipdir) {
|
|
|
266 |
return;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
$zipfilenameprefix = userdate(time(), self::ZIP_FILE_NAME_DOWNLOAD_ALL_CERTIFICATES_DATE_FORMAT);
|
|
|
270 |
$zipfilename = $zipfilenameprefix . "_" . self::ZIP_FILE_NAME_DOWNLOAD_ALL_CERTIFICATES;
|
|
|
271 |
$zipfullpath = $zipdir . DIRECTORY_SEPARATOR . $zipfilename;
|
|
|
272 |
|
|
|
273 |
$ziparchive = new \zip_archive();
|
|
|
274 |
if ($ziparchive->open($zipfullpath)) {
|
|
|
275 |
foreach ($issues as $issue) {
|
|
|
276 |
$userfullname = str_replace(' ', '_', mb_strtolower(format_text(fullname($issue), FORMAT_PLAIN)));
|
|
|
277 |
$pdfname = $userfullname . DIRECTORY_SEPARATOR . 'certificate.pdf';
|
|
|
278 |
$filecontents = $template->generate_pdf(false, $issue->id, true);
|
|
|
279 |
$ziparchive->add_file_from_string($pdfname, $filecontents);
|
|
|
280 |
}
|
|
|
281 |
$ziparchive->close();
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
send_file($zipfullpath, $zipfilename);
|
|
|
285 |
exit();
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Download all certificates on the site.
|
|
|
290 |
*
|
|
|
291 |
* @return void
|
|
|
292 |
*/
|
|
|
293 |
public static function download_all_for_site(): void {
|
|
|
294 |
global $DB;
|
|
|
295 |
|
|
|
296 |
list($namefields, $nameparams) = \core_user\fields::get_sql_fullname();
|
|
|
297 |
$sql = "SELECT ci.*, $namefields as fullname, ct.id as templateid, ct.name as templatename, ct.contextid
|
|
|
298 |
FROM {customcert_issues} ci
|
|
|
299 |
JOIN {user} u
|
|
|
300 |
ON ci.userid = u.id
|
|
|
301 |
JOIN {customcert} c
|
|
|
302 |
ON ci.customcertid = c.id
|
|
|
303 |
JOIN {customcert_templates} ct
|
|
|
304 |
ON c.templateid = ct.id";
|
|
|
305 |
if ($issues = $DB->get_records_sql($sql, $nameparams)) {
|
|
|
306 |
$zipdir = make_request_directory();
|
|
|
307 |
if (!$zipdir) {
|
|
|
308 |
return;
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
$zipfilenameprefix = userdate(time(), self::ZIP_FILE_NAME_DOWNLOAD_ALL_CERTIFICATES_DATE_FORMAT);
|
|
|
312 |
$zipfilename = $zipfilenameprefix . "_" . self::ZIP_FILE_NAME_DOWNLOAD_ALL_CERTIFICATES;
|
|
|
313 |
$zipfullpath = $zipdir . DIRECTORY_SEPARATOR . $zipfilename;
|
|
|
314 |
|
|
|
315 |
$ziparchive = new \zip_archive();
|
|
|
316 |
if ($ziparchive->open($zipfullpath)) {
|
|
|
317 |
foreach ($issues as $issue) {
|
|
|
318 |
$template = new \stdClass();
|
|
|
319 |
$template->id = $issue->templateid;
|
|
|
320 |
$template->name = $issue->templatename;
|
|
|
321 |
$template->contextid = $issue->contextid;
|
|
|
322 |
$template = new \mod_customcert\template($template);
|
|
|
323 |
|
|
|
324 |
$ctname = str_replace(' ', '_', mb_strtolower($template->get_name()));
|
|
|
325 |
$userfullname = str_replace(' ', '_', mb_strtolower($issue->fullname));
|
|
|
326 |
$pdfname = $userfullname . DIRECTORY_SEPARATOR . $ctname . '_' . 'certificate.pdf';
|
|
|
327 |
$filecontents = $template->generate_pdf(false, $issue->userid, true);
|
|
|
328 |
$ziparchive->add_file_from_string($pdfname, $filecontents);
|
|
|
329 |
}
|
|
|
330 |
$ziparchive->close();
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
send_file($zipfullpath, $zipfilename);
|
|
|
334 |
exit();
|
|
|
335 |
}
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
/**
|
|
|
339 |
* Returns a list of issued customcerts.
|
|
|
340 |
*
|
|
|
341 |
* @param int $customcertid
|
|
|
342 |
* @param bool $groupmode are we in group mode
|
|
|
343 |
* @param \stdClass $cm the course module
|
|
|
344 |
* @param int $limitfrom
|
|
|
345 |
* @param int $limitnum
|
|
|
346 |
* @param string $sort
|
|
|
347 |
* @return array the users
|
|
|
348 |
*/
|
|
|
349 |
public static function get_issues($customcertid, $groupmode, $cm, $limitfrom, $limitnum, $sort = '') {
|
|
|
350 |
global $DB;
|
|
|
351 |
|
|
|
352 |
// Get the conditional SQL.
|
|
|
353 |
list($conditionssql, $conditionsparams) = self::get_conditional_issues_sql($cm, $groupmode);
|
|
|
354 |
|
|
|
355 |
// If it is empty then return an empty array.
|
|
|
356 |
if (empty($conditionsparams)) {
|
|
|
357 |
return [];
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
// Return the issues.
|
|
|
361 |
$context = \context_module::instance($cm->id);
|
|
|
362 |
$query = \core_user\fields::for_identity($context)->with_userpic()->get_sql('u', true, '', '', false);
|
|
|
363 |
|
|
|
364 |
// Add the conditional SQL and the customcertid to form all used parameters.
|
|
|
365 |
$allparams = $query->params + $conditionsparams + ['customcertid' => $customcertid];
|
|
|
366 |
|
|
|
367 |
$orderby = $sort ?: $DB->sql_fullname();
|
|
|
368 |
|
|
|
369 |
$sql = "SELECT $query->selects, ci.id as issueid, ci.code, ci.timecreated
|
|
|
370 |
FROM {user} u
|
|
|
371 |
INNER JOIN {customcert_issues} ci ON (u.id = ci.userid)
|
|
|
372 |
$query->joins
|
|
|
373 |
WHERE u.deleted = 0 AND ci.customcertid = :customcertid
|
|
|
374 |
$conditionssql
|
|
|
375 |
ORDER BY $orderby";
|
|
|
376 |
|
|
|
377 |
return $DB->get_records_sql($sql, $allparams, $limitfrom, $limitnum);
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
/**
|
|
|
381 |
* Returns the total number of issues for a given customcert.
|
|
|
382 |
*
|
|
|
383 |
* @param int $customcertid
|
|
|
384 |
* @param \stdClass $cm the course module
|
|
|
385 |
* @param bool $groupmode the group mode
|
|
|
386 |
* @return int the number of issues
|
|
|
387 |
*/
|
|
|
388 |
public static function get_number_of_issues($customcertid, $cm, $groupmode) {
|
|
|
389 |
global $DB;
|
|
|
390 |
|
|
|
391 |
// Get the conditional SQL.
|
|
|
392 |
list($conditionssql, $conditionsparams) = self::get_conditional_issues_sql($cm, $groupmode);
|
|
|
393 |
|
|
|
394 |
// If it is empty then return 0.
|
|
|
395 |
if (empty($conditionsparams)) {
|
|
|
396 |
return 0;
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
// Add the conditional SQL and the customcertid to form all used parameters.
|
|
|
400 |
$allparams = $conditionsparams + ['customcertid' => $customcertid];
|
|
|
401 |
|
|
|
402 |
// Return the number of issues.
|
|
|
403 |
$sql = "SELECT COUNT(u.id) as count
|
|
|
404 |
FROM {user} u
|
|
|
405 |
INNER JOIN {customcert_issues} ci
|
|
|
406 |
ON u.id = ci.userid
|
|
|
407 |
WHERE u.deleted = 0
|
|
|
408 |
AND ci.customcertid = :customcertid
|
|
|
409 |
$conditionssql";
|
|
|
410 |
return $DB->count_records_sql($sql, $allparams);
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
/**
|
|
|
414 |
* Returns an array of the conditional variables to use in the get_issues SQL query.
|
|
|
415 |
*
|
|
|
416 |
* @param \stdClass $cm the course module
|
|
|
417 |
* @param bool $groupmode are we in group mode ?
|
|
|
418 |
* @return array the conditional variables
|
|
|
419 |
*/
|
|
|
420 |
public static function get_conditional_issues_sql($cm, $groupmode) {
|
|
|
421 |
global $DB, $USER;
|
|
|
422 |
|
|
|
423 |
// Get all users that can manage this customcert to exclude them from the report.
|
|
|
424 |
$context = \context_module::instance($cm->id);
|
|
|
425 |
$conditionssql = '';
|
|
|
426 |
$conditionsparams = [];
|
|
|
427 |
|
|
|
428 |
// Get all users that can manage this certificate to exclude them from the report.
|
|
|
429 |
$certmanagers = array_keys(get_users_by_capability($context, 'mod/customcert:manage', 'u.id'));
|
|
|
430 |
$certmanagers = array_merge($certmanagers, array_keys(get_admins()));
|
|
|
431 |
list($sql, $params) = $DB->get_in_or_equal($certmanagers, SQL_PARAMS_NAMED, 'cert');
|
|
|
432 |
$conditionssql .= "AND NOT u.id $sql \n";
|
|
|
433 |
$conditionsparams += $params;
|
|
|
434 |
|
|
|
435 |
if ($groupmode) {
|
|
|
436 |
$canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
|
|
|
437 |
$currentgroup = groups_get_activity_group($cm);
|
|
|
438 |
|
|
|
439 |
// If we are viewing all participants and the user does not have access to all groups then return nothing.
|
|
|
440 |
if (!$currentgroup && !$canaccessallgroups) {
|
|
|
441 |
return ['', []];
|
|
|
442 |
}
|
|
|
443 |
|
|
|
444 |
if ($currentgroup) {
|
|
|
445 |
if (!$canaccessallgroups) {
|
|
|
446 |
// Guest users do not belong to any groups.
|
|
|
447 |
if (isguestuser()) {
|
|
|
448 |
return ['', []];
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
// Check that the user belongs to the group we are viewing.
|
|
|
452 |
$usersgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid);
|
|
|
453 |
if ($usersgroups) {
|
|
|
454 |
if (!isset($usersgroups[$currentgroup])) {
|
|
|
455 |
return ['', []];
|
|
|
456 |
}
|
|
|
457 |
} else { // They belong to no group, so return an empty array.
|
|
|
458 |
return ['', []];
|
|
|
459 |
}
|
|
|
460 |
}
|
|
|
461 |
|
|
|
462 |
$groupusers = array_keys(groups_get_members($currentgroup, 'u.*'));
|
|
|
463 |
if (empty($groupusers)) {
|
|
|
464 |
return ['', []];
|
|
|
465 |
}
|
|
|
466 |
|
|
|
467 |
list($sql, $params) = $DB->get_in_or_equal($groupusers, SQL_PARAMS_NAMED, 'grp');
|
|
|
468 |
$conditionssql .= "AND u.id $sql ";
|
|
|
469 |
$conditionsparams += $params;
|
|
|
470 |
}
|
|
|
471 |
}
|
|
|
472 |
|
|
|
473 |
return [$conditionssql, $conditionsparams];
|
|
|
474 |
}
|
|
|
475 |
|
|
|
476 |
/**
|
|
|
477 |
* Get number of certificates for a user.
|
|
|
478 |
*
|
|
|
479 |
* @param int $userid
|
|
|
480 |
* @return int
|
|
|
481 |
*/
|
|
|
482 |
public static function get_number_of_certificates_for_user($userid) {
|
|
|
483 |
global $DB;
|
|
|
484 |
|
|
|
485 |
$sql = "SELECT COUNT(*)
|
|
|
486 |
FROM {customcert} c
|
|
|
487 |
INNER JOIN {customcert_issues} ci
|
|
|
488 |
ON c.id = ci.customcertid
|
|
|
489 |
WHERE ci.userid = :userid";
|
|
|
490 |
return $DB->count_records_sql($sql, ['userid' => $userid]);
|
|
|
491 |
}
|
|
|
492 |
|
|
|
493 |
/**
|
|
|
494 |
* Gets the certificates for the user.
|
|
|
495 |
*
|
|
|
496 |
* @param int $userid
|
|
|
497 |
* @param int $limitfrom
|
|
|
498 |
* @param int $limitnum
|
|
|
499 |
* @param string $sort
|
|
|
500 |
* @return array
|
|
|
501 |
*/
|
|
|
502 |
public static function get_certificates_for_user($userid, $limitfrom, $limitnum, $sort = '') {
|
|
|
503 |
global $DB;
|
|
|
504 |
|
|
|
505 |
if (empty($sort)) {
|
|
|
506 |
$sort = 'ci.timecreated DESC';
|
|
|
507 |
}
|
|
|
508 |
|
|
|
509 |
$sql = "SELECT c.id, c.name, co.fullname as coursename, ci.code, ci.timecreated
|
|
|
510 |
FROM {customcert} c
|
|
|
511 |
INNER JOIN {customcert_issues} ci
|
|
|
512 |
ON c.id = ci.customcertid
|
|
|
513 |
INNER JOIN {course} co
|
|
|
514 |
ON c.course = co.id
|
|
|
515 |
WHERE ci.userid = :userid
|
|
|
516 |
ORDER BY $sort";
|
|
|
517 |
return $DB->get_records_sql($sql, ['userid' => $userid], $limitfrom, $limitnum);
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
/**
|
|
|
521 |
* Issues a certificate to a user.
|
|
|
522 |
*
|
|
|
523 |
* @param int $certificateid The ID of the certificate
|
|
|
524 |
* @param int $userid The ID of the user to issue the certificate to
|
|
|
525 |
* @return int The ID of the issue
|
|
|
526 |
*/
|
|
|
527 |
public static function issue_certificate($certificateid, $userid) {
|
|
|
528 |
global $DB;
|
|
|
529 |
|
|
|
530 |
$issue = new \stdClass();
|
|
|
531 |
$issue->userid = $userid;
|
|
|
532 |
$issue->customcertid = $certificateid;
|
|
|
533 |
$issue->code = self::generate_code();
|
|
|
534 |
$issue->emailed = 0;
|
|
|
535 |
$issue->timecreated = time();
|
|
|
536 |
|
|
|
537 |
// Insert the record into the database.
|
|
|
538 |
return $DB->insert_record('customcert_issues', $issue);
|
|
|
539 |
}
|
|
|
540 |
|
|
|
541 |
/**
|
|
|
542 |
* Generates a 10-digit code of random letters and numbers.
|
|
|
543 |
*
|
|
|
544 |
* @return string
|
|
|
545 |
*/
|
|
|
546 |
public static function generate_code() {
|
|
|
547 |
global $DB;
|
|
|
548 |
|
|
|
549 |
$uniquecodefound = false;
|
|
|
550 |
$code = random_string(10);
|
|
|
551 |
while (!$uniquecodefound) {
|
|
|
552 |
if (!$DB->record_exists('customcert_issues', ['code' => $code])) {
|
|
|
553 |
$uniquecodefound = true;
|
|
|
554 |
} else {
|
|
|
555 |
$code = random_string(10);
|
|
|
556 |
}
|
|
|
557 |
}
|
|
|
558 |
|
|
|
559 |
return $code;
|
|
|
560 |
}
|
|
|
561 |
}
|