1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
use Endroid\QrCode\ErrorCorrectionLevel; |
6
|
|
|
use Endroid\QrCode\QrCode; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Certificate Class |
10
|
|
|
* Generate certificates based in the gradebook tool. |
11
|
|
|
*/ |
12
|
|
|
class Certificate extends Model |
13
|
|
|
{ |
14
|
|
|
public $table; |
15
|
|
|
public $columns = [ |
16
|
|
|
'id', |
17
|
|
|
'cat_id', |
18
|
|
|
'score_certificate', |
19
|
|
|
'created_at', |
20
|
|
|
'path_certificate', |
21
|
|
|
]; |
22
|
|
|
/** |
23
|
|
|
* Certification data. |
24
|
|
|
*/ |
25
|
|
|
public $certificate_data = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Student's certification path. |
29
|
|
|
*/ |
30
|
|
|
public $certification_user_path = null; |
31
|
|
|
public $certification_web_user_path = null; |
32
|
|
|
public $html_file = null; |
33
|
|
|
public $qr_file = null; |
34
|
|
|
public $user_id; |
35
|
|
|
|
36
|
|
|
/** If true every time we enter to the certificate URL |
37
|
|
|
* we would generate a new certificate (good thing because we can edit the |
38
|
|
|
* certificate and all users will have the latest certificate bad because we. |
39
|
|
|
* load the certificate every time */ |
40
|
|
|
public $force_certificate_generation = true; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor. |
44
|
|
|
* |
45
|
|
|
* @param int $certificate_id ID of the certificate |
46
|
|
|
* @param int $userId |
47
|
|
|
* @param bool $sendNotification send message to student |
48
|
|
|
* @param bool $updateCertificateData |
49
|
|
|
* |
50
|
|
|
* If no ID given, take user_id and try to generate one |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
$certificate_id = 0, |
54
|
|
|
$userId = 0, |
55
|
|
|
$sendNotification = false, |
56
|
|
|
$updateCertificateData = true |
57
|
|
|
) { |
58
|
|
|
$this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE); |
59
|
|
|
$this->user_id = !empty($userId) ? $userId : api_get_user_id(); |
60
|
|
|
|
61
|
|
|
if (!empty($certificate_id)) { |
62
|
|
|
$certificate = $this->get($certificate_id); |
63
|
|
|
if (!empty($certificate) && is_array($certificate)) { |
64
|
|
|
$this->certificate_data = $certificate; |
65
|
|
|
$this->user_id = $this->certificate_data['user_id']; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($this->user_id) { |
70
|
|
|
// Need to be called before any operation |
71
|
|
|
$this->check_certificate_path(); |
72
|
|
|
// To force certification generation |
73
|
|
|
if ($this->force_certificate_generation) { |
74
|
|
|
$this->generate([], $sendNotification); |
75
|
|
|
} |
76
|
|
|
if (isset($this->certificate_data) && $this->certificate_data) { |
77
|
|
|
if (empty($this->certificate_data['path_certificate'])) { |
78
|
|
|
$this->generate([], $sendNotification); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Setting the qr and html variables |
84
|
|
|
if (isset($certificate_id) && |
85
|
|
|
!empty($this->certification_user_path) && |
86
|
|
|
isset($this->certificate_data['path_certificate']) |
87
|
|
|
) { |
88
|
|
|
$pathinfo = pathinfo($this->certificate_data['path_certificate']); |
89
|
|
|
$this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']); |
90
|
|
|
$this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png'; |
91
|
|
|
} else { |
92
|
|
|
$this->check_certificate_path(); |
93
|
|
|
if (api_get_configuration_value('allow_general_certificate')) { |
94
|
|
|
// General certificate |
95
|
|
|
$name = md5($this->user_id).'.html'; |
96
|
|
|
$my_path_certificate = $this->certification_user_path.$name; |
97
|
|
|
$path_certificate = '/'.$name; |
98
|
|
|
|
99
|
|
|
// Getting QR filename |
100
|
|
|
$file_info = pathinfo($path_certificate); |
101
|
|
|
$content = $this->generateCustomCertificate(); |
102
|
|
|
|
103
|
|
|
$my_new_content_html = str_replace( |
104
|
|
|
'((certificate_barcode))', |
105
|
|
|
Display::img( |
106
|
|
|
$this->certification_web_user_path.$file_info['filename'].'_qr.png', |
107
|
|
|
'QR' |
108
|
|
|
), |
109
|
|
|
$content |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$my_new_content_html = mb_convert_encoding( |
113
|
|
|
$my_new_content_html, |
114
|
|
|
'UTF-8', |
115
|
|
|
api_get_system_encoding() |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$this->html_file = $my_path_certificate; |
119
|
|
|
$result = @file_put_contents($my_path_certificate, $my_new_content_html); |
120
|
|
|
|
121
|
|
|
if ($result) { |
122
|
|
|
// Updating the path |
123
|
|
|
self::updateUserCertificateInfo( |
124
|
|
|
0, |
125
|
|
|
$this->user_id, |
126
|
|
|
$path_certificate, |
127
|
|
|
$updateCertificateData |
128
|
|
|
); |
129
|
|
|
$this->certificate_data['path_certificate'] = $path_certificate; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Checks if the certificate user path directory is created. |
137
|
|
|
*/ |
138
|
|
|
public function check_certificate_path() |
139
|
|
|
{ |
140
|
|
|
$this->certification_user_path = null; |
141
|
|
|
|
142
|
|
|
// Setting certification path |
143
|
|
|
$path_info = UserManager::getUserPathById($this->user_id, 'system'); |
144
|
|
|
$web_path_info = UserManager::getUserPathById($this->user_id, 'web'); |
145
|
|
|
|
146
|
|
|
if (!empty($path_info) && isset($path_info)) { |
147
|
|
|
$this->certification_user_path = $path_info.'certificate/'; |
148
|
|
|
$this->certification_web_user_path = $web_path_info.'certificate/'; |
149
|
|
|
$mode = api_get_permissions_for_new_directories(); |
150
|
|
|
if (!is_dir($path_info)) { |
151
|
|
|
mkdir($path_info, $mode, true); |
152
|
|
|
} |
153
|
|
|
if (!is_dir($this->certification_user_path)) { |
154
|
|
|
mkdir($this->certification_user_path, $mode); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Deletes the current certificate object. This is generally triggered by |
161
|
|
|
* the teacher from the gradebook tool to re-generate the certificate because |
162
|
|
|
* the original version wa flawed. |
163
|
|
|
* |
164
|
|
|
* @param bool $force_delete |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function delete($force_delete = false) |
169
|
|
|
{ |
170
|
|
|
$delete_db = false; |
171
|
|
|
if (!empty($this->certificate_data)) { |
172
|
|
|
if (!is_null($this->html_file) || $this->html_file != '' || strlen($this->html_file)) { |
173
|
|
|
// Deleting HTML file |
174
|
|
|
if (is_file($this->html_file)) { |
175
|
|
|
@unlink($this->html_file); |
176
|
|
|
if (is_file($this->html_file) === false) { |
177
|
|
|
$delete_db = true; |
178
|
|
|
} else { |
179
|
|
|
$delete_db = false; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
// Deleting QR code PNG image file |
183
|
|
|
if (is_file($this->qr_file)) { |
184
|
|
|
@unlink($this->qr_file); |
185
|
|
|
} |
186
|
|
|
if ($delete_db || $force_delete) { |
187
|
|
|
return parent::delete($this->certificate_data['id']); |
188
|
|
|
} |
189
|
|
|
} else { |
190
|
|
|
return parent::delete($this->certificate_data['id']); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Generates an HTML Certificate and fills the path_certificate field in the DB. |
199
|
|
|
* |
200
|
|
|
* @param array $params |
201
|
|
|
* @param bool $sendNotification |
202
|
|
|
* |
203
|
|
|
* @return bool|int |
204
|
|
|
*/ |
205
|
|
|
public function generate($params = [], $sendNotification = false) |
206
|
|
|
{ |
207
|
|
|
// The user directory should be set |
208
|
|
|
if (empty($this->certification_user_path) && |
209
|
|
|
$this->force_certificate_generation === false |
210
|
|
|
) { |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$params['hide_print_button'] = isset($params['hide_print_button']) ? true : false; |
215
|
|
|
$categoryId = 0; |
216
|
|
|
$my_category = []; |
217
|
|
|
if (isset($this->certificate_data) && isset($this->certificate_data['cat_id'])) { |
218
|
|
|
$categoryId = $this->certificate_data['cat_id']; |
219
|
|
|
$my_category = Category::load($categoryId); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if (isset($my_category[0]) && !empty($categoryId) && |
223
|
|
|
$my_category[0]->is_certificate_available($this->user_id) |
224
|
|
|
) { |
225
|
|
|
/** @var Category $category */ |
226
|
|
|
$category = $my_category[0]; |
227
|
|
|
|
228
|
|
|
$courseInfo = api_get_course_info($category->get_course_code()); |
229
|
|
|
$courseId = $courseInfo['real_id']; |
230
|
|
|
$sessionId = $category->get_session_id(); |
231
|
|
|
|
232
|
|
|
$skill = new Skill(); |
233
|
|
|
$skill->addSkillToUser( |
234
|
|
|
$this->user_id, |
235
|
|
|
$category, |
236
|
|
|
$courseId, |
237
|
|
|
$sessionId |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
if (is_dir($this->certification_user_path)) { |
241
|
|
|
if (!empty($this->certificate_data)) { |
242
|
|
|
$new_content_html = GradebookUtils::get_user_certificate_content( |
243
|
|
|
$this->user_id, |
244
|
|
|
$category->get_course_code(), |
245
|
|
|
$category->get_session_id(), |
246
|
|
|
false, |
247
|
|
|
$params['hide_print_button'] |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
if ($category->get_id() == $categoryId) { |
251
|
|
|
$name = $this->certificate_data['path_certificate']; |
252
|
|
|
$myPathCertificate = $this->certification_user_path.basename($name); |
253
|
|
|
|
254
|
|
|
if (file_exists($myPathCertificate) && |
255
|
|
|
!empty($name) && |
256
|
|
|
!is_dir($myPathCertificate) && |
257
|
|
|
$this->force_certificate_generation == false |
258
|
|
|
) { |
259
|
|
|
// Seems that the file was already generated |
260
|
|
|
return true; |
261
|
|
|
} else { |
262
|
|
|
// Creating new name |
263
|
|
|
$name = md5($this->user_id.$this->certificate_data['cat_id']).'.html'; |
264
|
|
|
$myPathCertificate = $this->certification_user_path.$name; |
265
|
|
|
$path_certificate = '/'.$name; |
266
|
|
|
|
267
|
|
|
// Getting QR filename |
268
|
|
|
$file_info = pathinfo($path_certificate); |
269
|
|
|
$qr_code_filename = $this->certification_user_path.$file_info['filename'].'_qr.png'; |
270
|
|
|
|
271
|
|
|
$newContent = str_replace( |
272
|
|
|
'((certificate_barcode))', |
273
|
|
|
Display::img( |
274
|
|
|
$this->certification_web_user_path.$file_info['filename'].'_qr.png', |
275
|
|
|
'QR' |
276
|
|
|
), |
277
|
|
|
$new_content_html['content'] |
278
|
|
|
); |
279
|
|
|
|
280
|
|
|
$newContent = api_convert_encoding( |
281
|
|
|
$newContent, |
282
|
|
|
'UTF-8', |
283
|
|
|
api_get_system_encoding() |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
$result = @file_put_contents($myPathCertificate, $newContent); |
287
|
|
|
if ($result) { |
288
|
|
|
// Updating the path |
289
|
|
|
$this->updateUserCertificateInfo( |
290
|
|
|
$this->certificate_data['cat_id'], |
291
|
|
|
$this->user_id, |
292
|
|
|
$path_certificate |
293
|
|
|
); |
294
|
|
|
$this->certificate_data['path_certificate'] = $path_certificate; |
295
|
|
|
|
296
|
|
|
if ($this->isHtmlFileGenerated()) { |
297
|
|
|
if (!empty($file_info)) { |
298
|
|
|
$text = $this->parseCertificateVariables( |
299
|
|
|
$new_content_html['variables'] |
300
|
|
|
); |
301
|
|
|
$this->generateQRImage( |
302
|
|
|
$text, |
303
|
|
|
$qr_code_filename |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
if ($sendNotification) { |
307
|
|
|
$subject = get_lang('NotificationCertificateSubject'); |
308
|
|
|
$message = nl2br(get_lang('NotificationCertificateTemplate')); |
309
|
|
|
$score = $this->certificate_data['score_certificate']; |
310
|
|
|
self::sendNotification( |
311
|
|
|
$subject, |
312
|
|
|
$message, |
313
|
|
|
api_get_user_info($this->user_id), |
314
|
|
|
$courseInfo, |
315
|
|
|
[ |
316
|
|
|
'score_certificate' => $score, |
317
|
|
|
] |
318
|
|
|
); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return $result; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
} else { |
330
|
|
|
$this->check_certificate_path(); |
331
|
|
|
|
332
|
|
|
// General certificate |
333
|
|
|
$name = md5($this->user_id).'.html'; |
334
|
|
|
$my_path_certificate = $this->certification_user_path.$name; |
335
|
|
|
$path_certificate = '/'.$name; |
336
|
|
|
|
337
|
|
|
// Getting QR filename |
338
|
|
|
$file_info = pathinfo($path_certificate); |
339
|
|
|
$content = $this->generateCustomCertificate(); |
340
|
|
|
|
341
|
|
|
$my_new_content_html = str_replace( |
342
|
|
|
'((certificate_barcode))', |
343
|
|
|
Display::img( |
344
|
|
|
$this->certification_web_user_path.$file_info['filename'].'_qr.png', |
345
|
|
|
'QR' |
346
|
|
|
), |
347
|
|
|
$content |
348
|
|
|
); |
349
|
|
|
|
350
|
|
|
$my_new_content_html = mb_convert_encoding( |
351
|
|
|
$my_new_content_html, |
352
|
|
|
'UTF-8', |
353
|
|
|
api_get_system_encoding() |
354
|
|
|
); |
355
|
|
|
|
356
|
|
|
$result = @file_put_contents($my_path_certificate, $my_new_content_html); |
357
|
|
|
|
358
|
|
|
if ($result) { |
359
|
|
|
// Updating the path |
360
|
|
|
self::updateUserCertificateInfo( |
361
|
|
|
0, |
362
|
|
|
$this->user_id, |
363
|
|
|
$path_certificate |
364
|
|
|
); |
365
|
|
|
$this->certificate_data['path_certificate'] = $path_certificate; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return $result; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
return false; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return array |
376
|
|
|
*/ |
377
|
|
|
public static function notificationTags() |
378
|
|
|
{ |
379
|
|
|
$tags = [ |
380
|
|
|
'((course_title))', |
381
|
|
|
'((user_first_name))', |
382
|
|
|
'((user_last_name))', |
383
|
|
|
'((author_first_name))', |
384
|
|
|
'((author_last_name))', |
385
|
|
|
'((score))', |
386
|
|
|
'((portal_name))', |
387
|
|
|
'((certificate_link))', |
388
|
|
|
]; |
389
|
|
|
|
390
|
|
|
return $tags; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @param string $subject |
395
|
|
|
* @param string $message |
396
|
|
|
* @param array $userInfo |
397
|
|
|
* @param array $courseInfo |
398
|
|
|
* @param array $certificateInfo |
399
|
|
|
* |
400
|
|
|
* @return bool |
401
|
|
|
*/ |
402
|
|
|
public static function sendNotification( |
403
|
|
|
$subject, |
404
|
|
|
$message, |
405
|
|
|
$userInfo, |
406
|
|
|
$courseInfo, |
407
|
|
|
$certificateInfo |
408
|
|
|
) { |
409
|
|
|
if (empty($userInfo) || empty($courseInfo)) { |
410
|
|
|
return false; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
$currentUserInfo = api_get_user_info(); |
414
|
|
|
$url = api_get_path(WEB_PATH). |
415
|
|
|
'certificates/index.php?id='.$certificateInfo['id'].'&user_id='.$certificateInfo['user_id']; |
416
|
|
|
$link = Display::url($url, $url); |
417
|
|
|
|
418
|
|
|
$replace = [ |
419
|
|
|
$courseInfo['title'], |
420
|
|
|
$userInfo['firstname'], |
421
|
|
|
$userInfo['lastname'], |
422
|
|
|
$currentUserInfo['firstname'], |
423
|
|
|
$currentUserInfo['lastname'], |
424
|
|
|
$certificateInfo['score_certificate'], |
425
|
|
|
api_get_setting('Institution'), |
426
|
|
|
$link, |
427
|
|
|
]; |
428
|
|
|
|
429
|
|
|
$message = str_replace(self::notificationTags(), $replace, $message); |
430
|
|
|
MessageManager::send_message( |
431
|
|
|
$userInfo['id'], |
432
|
|
|
$subject, |
433
|
|
|
$message, |
434
|
|
|
[], |
435
|
|
|
[], |
436
|
|
|
0, |
437
|
|
|
0, |
438
|
|
|
0, |
439
|
|
|
0, |
440
|
|
|
$currentUserInfo['id'] |
441
|
|
|
); |
442
|
|
|
|
443
|
|
|
$plugin = new AppPlugin(); |
444
|
|
|
$smsPlugin = $plugin->getSMSPluginLibrary(); |
445
|
|
|
if ($smsPlugin) { |
|
|
|
|
446
|
|
|
$additionalParameters = [ |
447
|
|
|
'smsType' => SmsPlugin::CERTIFICATE_NOTIFICATION, |
448
|
|
|
'userId' => $userInfo['id'], |
449
|
|
|
'direct_message' => $message, |
450
|
|
|
]; |
451
|
|
|
$smsPlugin->send($additionalParameters); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Update user info about certificate. |
457
|
|
|
* |
458
|
|
|
* @param int $categoryId category id |
459
|
|
|
* @param int $user_id user id |
460
|
|
|
* @param string $path_certificate the path name of the certificate |
461
|
|
|
* @param bool $updateCertificateData |
462
|
|
|
*/ |
463
|
|
|
public function updateUserCertificateInfo( |
464
|
|
|
$categoryId, |
465
|
|
|
$user_id, |
466
|
|
|
$path_certificate, |
467
|
|
|
$updateCertificateData = true |
468
|
|
|
) { |
469
|
|
|
$categoryId = (int) $categoryId; |
470
|
|
|
$user_id = (int) $user_id; |
471
|
|
|
|
472
|
|
|
if ($updateCertificateData && |
473
|
|
|
!UserManager::is_user_certified($categoryId, $user_id) |
474
|
|
|
) { |
475
|
|
|
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE); |
476
|
|
|
$now = api_get_utc_datetime(); |
477
|
|
|
$sql = 'UPDATE '.$table.' SET |
478
|
|
|
path_certificate="'.Database::escape_string($path_certificate).'", |
479
|
|
|
created_at = "'.$now.'" |
480
|
|
|
WHERE cat_id = "'.$categoryId.'" AND user_id="'.$user_id.'" '; |
481
|
|
|
Database::query($sql); |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Check if the file was generated. |
487
|
|
|
* |
488
|
|
|
* @return bool |
489
|
|
|
*/ |
490
|
|
|
public function isHtmlFileGenerated() |
491
|
|
|
{ |
492
|
|
|
if (empty($this->certification_user_path)) { |
493
|
|
|
return false; |
494
|
|
|
} |
495
|
|
|
if (!empty($this->certificate_data) && |
496
|
|
|
isset($this->certificate_data['path_certificate']) && |
497
|
|
|
!empty($this->certificate_data['path_certificate']) |
498
|
|
|
) { |
499
|
|
|
return true; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
return false; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Generates a QR code for the certificate. The QR code embeds the text given. |
507
|
|
|
* |
508
|
|
|
* @param string $text Text to be added in the QR code |
509
|
|
|
* @param string $path file path of the image |
510
|
|
|
* |
511
|
|
|
* @return bool |
512
|
|
|
*/ |
513
|
|
|
public function generateQRImage($text, $path) |
514
|
|
|
{ |
515
|
|
|
if (!empty($text) && !empty($path)) { |
516
|
|
|
$qrCode = new QrCode($text); |
517
|
|
|
//$qrCode->setEncoding('UTF-8'); |
518
|
|
|
$qrCode->setSize(120); |
519
|
|
|
$qrCode->setMargin(5); |
520
|
|
|
$qrCode->setWriterByName('png'); |
521
|
|
|
$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::MEDIUM); |
522
|
|
|
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]); |
523
|
|
|
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]); |
524
|
|
|
$qrCode->setValidateResult(false); |
525
|
|
|
$qrCode->writeFile($path); |
526
|
|
|
|
527
|
|
|
return true; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
return false; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Transforms certificate tags into text values. This function is very static |
535
|
|
|
* (it doesn't allow for much flexibility in terms of what tags are printed). |
536
|
|
|
* |
537
|
|
|
* @param array $array Contains two array entries: first are the headers, |
538
|
|
|
* second is an array of contents |
539
|
|
|
* |
540
|
|
|
* @return string The translated string |
541
|
|
|
*/ |
542
|
|
|
public function parseCertificateVariables($array) |
543
|
|
|
{ |
544
|
|
|
$headers = $array[0]; |
545
|
|
|
$content = $array[1]; |
546
|
|
|
$final_content = []; |
547
|
|
|
|
548
|
|
|
if (!empty($content)) { |
549
|
|
|
foreach ($content as $key => $value) { |
550
|
|
|
$my_header = str_replace(['((', '))'], '', $headers[$key]); |
551
|
|
|
$final_content[$my_header] = $value; |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/* Certificate tags |
556
|
|
|
* |
557
|
|
|
0 => string '((user_firstname))' (length=18) |
558
|
|
|
1 => string '((user_lastname))' (length=17) |
559
|
|
|
2 => string '((gradebook_institution))' (length=25) |
560
|
|
|
3 => string '((gradebook_sitename))' (length=22) |
561
|
|
|
4 => string '((teacher_firstname))' (length=21) |
562
|
|
|
5 => string '((teacher_lastname))' (length=20) |
563
|
|
|
6 => string '((official_code))' (length=17) |
564
|
|
|
7 => string '((date_certificate))' (length=20) |
565
|
|
|
8 => string '((course_code))' (length=15) |
566
|
|
|
9 => string '((course_title))' (length=16) |
567
|
|
|
10 => string '((gradebook_grade))' (length=19) |
568
|
|
|
11 => string '((certificate_link))' (length=20) |
569
|
|
|
12 => string '((certificate_link_html))' (length=25) |
570
|
|
|
13 => string '((certificate_barcode))' (length=23) |
571
|
|
|
*/ |
572
|
|
|
|
573
|
|
|
$break_space = " \n\r "; |
574
|
|
|
$text = |
575
|
|
|
$final_content['gradebook_institution'].' - '. |
576
|
|
|
$final_content['gradebook_sitename'].' - '. |
577
|
|
|
get_lang('Certification').$break_space. |
578
|
|
|
get_lang('Student').': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space. |
579
|
|
|
get_lang('Teacher').': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space. |
580
|
|
|
get_lang('Date').': '.$final_content['date_certificate'].$break_space. |
581
|
|
|
get_lang('Score').': '.$final_content['gradebook_grade'].$break_space. |
582
|
|
|
'URL'.': '.$final_content['certificate_link']; |
583
|
|
|
|
584
|
|
|
return $text; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Check if the certificate is visible for the current user |
589
|
|
|
* If the global setting allow_public_certificates is set to 'false', no certificate can be printed. |
590
|
|
|
* If the global allow_public_certificates is set to 'true' and the course setting allow_public_certificates |
591
|
|
|
* is set to 0, no certificate *in this course* can be printed (for anonymous users). |
592
|
|
|
* Connected users can always print them. |
593
|
|
|
* |
594
|
|
|
* @return bool |
595
|
|
|
*/ |
596
|
|
|
public function isVisible() |
597
|
|
|
{ |
598
|
|
|
if (!api_is_anonymous()) { |
599
|
|
|
return true; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
if (api_get_setting('allow_public_certificates') != 'true') { |
603
|
|
|
// The "non-public" setting is set, so do not print |
604
|
|
|
return false; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
if (!isset($this->certificate_data, $this->certificate_data['cat_id'])) { |
608
|
|
|
return false; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
$gradeBook = new Gradebook(); |
612
|
|
|
$gradeBookInfo = $gradeBook->get($this->certificate_data['cat_id']); |
613
|
|
|
|
614
|
|
|
if (empty($gradeBookInfo['course_code'])) { |
615
|
|
|
return false; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
$setting = api_get_course_setting( |
619
|
|
|
'allow_public_certificates', |
620
|
|
|
api_get_course_info($gradeBookInfo['course_code']) |
621
|
|
|
); |
622
|
|
|
|
623
|
|
|
if ($setting == 0) { |
624
|
|
|
// Printing not allowed |
625
|
|
|
return false; |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
return true; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* Check if the certificate is available. |
633
|
|
|
* |
634
|
|
|
* @return bool |
635
|
|
|
*/ |
636
|
|
|
public function isAvailable() |
637
|
|
|
{ |
638
|
|
|
if (empty($this->certificate_data['path_certificate'])) { |
639
|
|
|
return false; |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
$userCertificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']); |
643
|
|
|
|
644
|
|
|
if (!file_exists($userCertificate)) { |
645
|
|
|
return false; |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
return true; |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Shows the student's certificate (HTML file). |
653
|
|
|
*/ |
654
|
|
|
public function show() |
655
|
|
|
{ |
656
|
|
|
$user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']); |
657
|
|
|
if (file_exists($user_certificate)) { |
658
|
|
|
// Needed in order to browsers don't add custom CSS |
659
|
|
|
$certificateContent = '<!DOCTYPE html>'; |
660
|
|
|
$certificateContent .= (string) file_get_contents($user_certificate); |
661
|
|
|
|
662
|
|
|
// Remove media=screen to be available when printing a document |
663
|
|
|
$certificateContent = str_replace( |
664
|
|
|
' media="screen"', |
665
|
|
|
'', |
666
|
|
|
$certificateContent |
667
|
|
|
); |
668
|
|
|
|
669
|
|
|
if ($this->user_id == api_get_user_id() && |
670
|
|
|
!empty($this->certificate_data) && |
671
|
|
|
isset($this->certificate_data['id']) |
672
|
|
|
) { |
673
|
|
|
$certificateId = $this->certificate_data['id']; |
674
|
|
|
$extraFieldValue = new ExtraFieldValue('user_certificate'); |
675
|
|
|
$value = $extraFieldValue->get_values_by_handler_and_field_variable( |
676
|
|
|
$certificateId, |
677
|
|
|
'downloaded_at' |
678
|
|
|
); |
679
|
|
|
if (empty($value)) { |
680
|
|
|
$params = [ |
681
|
|
|
'item_id' => $this->certificate_data['id'], |
682
|
|
|
'extra_downloaded_at' => api_get_utc_datetime(), |
683
|
|
|
]; |
684
|
|
|
$extraFieldValue->saveFieldValues($params); |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
header('Content-Type: text/html; charset='.api_get_system_encoding()); |
689
|
|
|
echo $certificateContent; |
690
|
|
|
|
691
|
|
|
return; |
692
|
|
|
} |
693
|
|
|
api_not_allowed(true); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* @return string |
698
|
|
|
*/ |
699
|
|
|
public function generateCustomCertificate() |
700
|
|
|
{ |
701
|
|
|
$myCertificate = GradebookUtils::get_certificate_by_user_id( |
702
|
|
|
0, |
703
|
|
|
$this->user_id |
704
|
|
|
); |
705
|
|
|
if (empty($myCertificate)) { |
706
|
|
|
GradebookUtils::registerUserInfoAboutCertificate( |
707
|
|
|
0, |
708
|
|
|
$this->user_id, |
709
|
|
|
100, |
710
|
|
|
api_get_utc_datetime() |
711
|
|
|
); |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
$userInfo = api_get_user_info($this->user_id); |
715
|
|
|
$extraFieldValue = new ExtraFieldValue('user'); |
716
|
|
|
$value = $extraFieldValue->get_values_by_handler_and_field_variable($this->user_id, 'legal_accept'); |
717
|
|
|
$termsValidationDate = ''; |
718
|
|
|
if (isset($value) && !empty($value['value'])) { |
719
|
|
|
list($id, $id2, $termsValidationDate) = explode(':', $value['value']); |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
$sessions = SessionManager::get_sessions_by_user($this->user_id, false, true); |
723
|
|
|
$totalTimeInLearningPaths = 0; |
724
|
|
|
$sessionsApproved = []; |
725
|
|
|
$coursesApproved = []; |
726
|
|
|
$courseList = []; |
727
|
|
|
|
728
|
|
|
if ($sessions) { |
729
|
|
|
foreach ($sessions as $session) { |
730
|
|
|
$allCoursesApproved = []; |
731
|
|
|
foreach ($session['courses'] as $course) { |
732
|
|
|
$courseInfo = api_get_course_info_by_id($course['real_id']); |
733
|
|
|
$courseCode = $courseInfo['code']; |
734
|
|
|
$gradebookCategories = Category::load( |
735
|
|
|
null, |
736
|
|
|
null, |
737
|
|
|
$courseCode, |
738
|
|
|
null, |
739
|
|
|
false, |
740
|
|
|
$session['session_id'] |
741
|
|
|
); |
742
|
|
|
|
743
|
|
|
if (isset($gradebookCategories[0])) { |
744
|
|
|
/** @var Category $category */ |
745
|
|
|
$category = $gradebookCategories[0]; |
746
|
|
|
$result = Category::userFinishedCourse( |
747
|
|
|
$this->user_id, |
748
|
|
|
$category, |
749
|
|
|
true |
750
|
|
|
); |
751
|
|
|
|
752
|
|
|
// Find time spent in LP |
753
|
|
|
$timeSpent = Tracking::get_time_spent_in_lp( |
754
|
|
|
$this->user_id, |
755
|
|
|
$courseCode, |
756
|
|
|
[], |
757
|
|
|
$session['session_id'] |
758
|
|
|
); |
759
|
|
|
|
760
|
|
|
if (!isset($courseList[$course['real_id']])) { |
761
|
|
|
$courseList[$course['real_id']]['approved'] = false; |
762
|
|
|
$courseList[$course['real_id']]['time_spent'] = 0; |
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
if ($result) { |
766
|
|
|
$courseList[$course['real_id']]['approved'] = true; |
767
|
|
|
$coursesApproved[$course['real_id']] = $courseInfo['title']; |
768
|
|
|
|
769
|
|
|
// Find time spent in LP |
770
|
|
|
//$totalTimeInLearningPaths += $timeSpent; |
771
|
|
|
$allCoursesApproved[] = true; |
772
|
|
|
} |
773
|
|
|
$courseList[$course['real_id']]['time_spent'] += $timeSpent; |
774
|
|
|
} |
775
|
|
|
} |
776
|
|
|
|
777
|
|
|
if (count($allCoursesApproved) == count($session['courses'])) { |
778
|
|
|
$sessionsApproved[] = $session; |
779
|
|
|
} |
780
|
|
|
} |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
$totalTimeInLearningPaths = 0; |
784
|
|
|
foreach ($courseList as $courseId => $courseData) { |
785
|
|
|
if ($courseData['approved'] === true) { |
786
|
|
|
$totalTimeInLearningPaths += $courseData['time_spent']; |
787
|
|
|
} |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
$skill = new Skill(); |
791
|
|
|
// Ofaj |
792
|
|
|
$skills = $skill->getStudentSkills($this->user_id, 2); |
793
|
|
|
$timeInSeconds = Tracking::get_time_spent_on_the_platform( |
794
|
|
|
$this->user_id, |
795
|
|
|
'ever' |
796
|
|
|
); |
797
|
|
|
$time = api_time_to_hms($timeInSeconds); |
798
|
|
|
|
799
|
|
|
$tplContent = new Template(null, false, false, false, false, false); |
800
|
|
|
|
801
|
|
|
// variables for the default template |
802
|
|
|
$tplContent->assign('complete_name', $userInfo['complete_name']); |
803
|
|
|
$tplContent->assign('time_in_platform', $time); |
804
|
|
|
$tplContent->assign('certificate_generated_date', api_get_local_time($myCertificate['created_at'])); |
805
|
|
|
if (!empty($termsValidationDate)) { |
806
|
|
|
$termsValidationDate = api_get_local_time($termsValidationDate); |
807
|
|
|
} |
808
|
|
|
$tplContent->assign('terms_validation_date', $termsValidationDate); |
809
|
|
|
|
810
|
|
|
// Ofaj |
811
|
|
|
$tplContent->assign('time_in_platform_in_hours', round($timeInSeconds / 3600, 1)); |
812
|
|
|
$tplContent->assign( |
813
|
|
|
'certificate_generated_date_no_time', |
814
|
|
|
api_get_local_time( |
815
|
|
|
$myCertificate['created_at'], |
816
|
|
|
null, |
817
|
|
|
null, |
818
|
|
|
false, |
819
|
|
|
false, |
820
|
|
|
false, |
821
|
|
|
'd-m-Y' |
822
|
|
|
) |
823
|
|
|
); |
824
|
|
|
$tplContent->assign( |
825
|
|
|
'terms_validation_date_no_time', |
826
|
|
|
api_get_local_time( |
827
|
|
|
$termsValidationDate, |
828
|
|
|
null, |
829
|
|
|
null, |
830
|
|
|
false, |
831
|
|
|
false, |
832
|
|
|
false, |
833
|
|
|
'd-m-Y' |
834
|
|
|
) |
835
|
|
|
); |
836
|
|
|
$tplContent->assign('skills', $skills); |
837
|
|
|
$tplContent->assign('sessions', $sessionsApproved); |
838
|
|
|
$tplContent->assign('courses', $coursesApproved); |
839
|
|
|
$tplContent->assign('time_spent_in_lps', api_time_to_hms($totalTimeInLearningPaths)); |
840
|
|
|
$tplContent->assign('time_spent_in_lps_in_hours', round($totalTimeInLearningPaths / 3600, 1)); |
841
|
|
|
|
842
|
|
|
$layoutContent = $tplContent->get_template('gradebook/custom_certificate.tpl'); |
843
|
|
|
$content = $tplContent->fetch($layoutContent); |
844
|
|
|
|
845
|
|
|
return $content; |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* Ofaj. |
850
|
|
|
*/ |
851
|
|
|
public function generatePdfFromCustomCertificate() |
852
|
|
|
{ |
853
|
|
|
$orientation = api_get_configuration_value('certificate_pdf_orientation'); |
854
|
|
|
|
855
|
|
|
$params['orientation'] = 'landscape'; |
856
|
|
|
if (!empty($orientation)) { |
857
|
|
|
$params['orientation'] = $orientation; |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
$params['left'] = 0; |
861
|
|
|
$params['right'] = 0; |
862
|
|
|
$params['top'] = 0; |
863
|
|
|
$params['bottom'] = 0; |
864
|
|
|
$page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4'; |
865
|
|
|
$pdf = new PDF($page_format, $params['orientation'], $params); |
866
|
|
|
|
867
|
|
|
$pdf->html_to_pdf( |
868
|
|
|
$this->html_file, |
869
|
|
|
get_lang('Certificates'), |
870
|
|
|
null, |
871
|
|
|
false, |
872
|
|
|
false |
873
|
|
|
); |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
/** |
877
|
|
|
* @param int $userId |
878
|
|
|
* |
879
|
|
|
* @return array |
880
|
|
|
*/ |
881
|
|
|
public static function getCertificateByUser($userId) |
882
|
|
|
{ |
883
|
|
|
$userId = (int) $userId; |
884
|
|
|
if (empty($userId)) { |
885
|
|
|
return []; |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE); |
889
|
|
|
$sql = "SELECT * FROM $table |
890
|
|
|
WHERE user_id= $userId"; |
891
|
|
|
$rs = Database::query($sql); |
892
|
|
|
|
893
|
|
|
return Database::store_result($rs, 'ASSOC'); |
894
|
|
|
} |
895
|
|
|
|
896
|
|
|
/** |
897
|
|
|
* @param int $userId |
898
|
|
|
*/ |
899
|
|
|
public static function generateUserSkills($userId) |
900
|
|
|
{ |
901
|
|
|
$controller = new IndexManager(get_lang('MyCourses')); |
902
|
|
|
$courseAndSessions = $controller->returnCoursesAndSessions($userId, true, null, true, false); |
903
|
|
|
|
904
|
|
|
if (isset($courseAndSessions['courses']) && !empty($courseAndSessions['courses'])) { |
905
|
|
|
foreach ($courseAndSessions['courses'] as $course) { |
906
|
|
|
$cats = Category::load( |
907
|
|
|
null, |
908
|
|
|
null, |
909
|
|
|
$course['code'], |
910
|
|
|
null, |
911
|
|
|
null, |
912
|
|
|
null, |
913
|
|
|
false |
914
|
|
|
); |
915
|
|
|
|
916
|
|
|
if (isset($cats[0]) && !empty($cats[0])) { |
917
|
|
|
Category::generateUserCertificate( |
918
|
|
|
$cats[0]->get_id(), |
919
|
|
|
$userId |
920
|
|
|
); |
921
|
|
|
} |
922
|
|
|
} |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
if (isset($courseAndSessions['sessions']) && !empty($courseAndSessions['sessions'])) { |
926
|
|
|
foreach ($courseAndSessions['sessions'] as $sessionCategory) { |
927
|
|
|
if (isset($sessionCategory['sessions'])) { |
928
|
|
|
foreach ($sessionCategory['sessions'] as $sessionData) { |
929
|
|
|
if (!empty($sessionData['courses'])) { |
930
|
|
|
$sessionId = $sessionData['session_id']; |
931
|
|
|
foreach ($sessionData['courses'] as $courseData) { |
932
|
|
|
$cats = Category::load( |
933
|
|
|
null, |
934
|
|
|
null, |
935
|
|
|
$courseData['course_code'], |
936
|
|
|
null, |
937
|
|
|
null, |
938
|
|
|
$sessionId, |
939
|
|
|
false |
940
|
|
|
); |
941
|
|
|
|
942
|
|
|
if (isset($cats[0]) && !empty($cats[0])) { |
943
|
|
|
Category::generateUserCertificate( |
944
|
|
|
$cats[0]->get_id(), |
945
|
|
|
$userId |
946
|
|
|
); |
947
|
|
|
} |
948
|
|
|
} |
949
|
|
|
} |
950
|
|
|
} |
951
|
|
|
} |
952
|
|
|
} |
953
|
|
|
} |
954
|
|
|
} |
955
|
|
|
} |
956
|
|
|
|