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