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