certificate.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Certificate Class
  5. * Generate certificates based in the gradebook tool.
  6. * @package chamilo.library.certificates
  7. */
  8. class Certificate extends Model
  9. {
  10. public $table;
  11. public $columns = array(
  12. 'id',
  13. 'cat_id',
  14. 'score_certificate',
  15. 'created_at',
  16. 'path_certificate'
  17. );
  18. /**
  19. * Certification data
  20. */
  21. public $certificate_data = [];
  22. /**
  23. * Student's certification path
  24. */
  25. public $certification_user_path = null;
  26. public $certification_web_user_path = null;
  27. public $html_file = null;
  28. public $qr_file = null;
  29. public $user_id;
  30. /** If true every time we enter to the certificate URL
  31. * we would generate a new certificate (good thing because we can edit the
  32. * certificate and all users will have the latest certificate bad because we
  33. * load the certificate every time */
  34. public $force_certificate_generation = true;
  35. /**
  36. * Constructor
  37. * @param int $certificate_id ID of the certificate.
  38. * @param int $userId
  39. * @param bool $sendNotification send message to student
  40. *
  41. * If no ID given, take user_id and try to generate one
  42. */
  43. public function __construct(
  44. $certificate_id = 0,
  45. $userId = 0,
  46. $sendNotification = false
  47. ) {
  48. $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  49. $this->user_id = !empty($userId) ? $userId : api_get_user_id();
  50. if (!empty($certificate_id)) {
  51. $certificate = $this->get($certificate_id);
  52. if (!empty($certificate) && is_array($certificate)) {
  53. $this->certificate_data = $certificate;
  54. $this->user_id = $this->certificate_data['user_id'];
  55. }
  56. }
  57. if ($this->user_id) {
  58. // Need to be called before any operation
  59. $this->check_certificate_path();
  60. // To force certification generation
  61. if ($this->force_certificate_generation) {
  62. $this->generate([], $sendNotification);
  63. }
  64. if (isset($this->certificate_data) && $this->certificate_data) {
  65. if (empty($this->certificate_data['path_certificate'])) {
  66. $this->generate([], $sendNotification);
  67. }
  68. }
  69. }
  70. // Setting the qr and html variables
  71. if (isset($certificate_id) &&
  72. !empty($this->certification_user_path) &&
  73. isset($this->certificate_data['path_certificate'])
  74. ) {
  75. $pathinfo = pathinfo($this->certificate_data['path_certificate']);
  76. $this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  77. $this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png';
  78. } else {
  79. // General certificate
  80. $name = md5($this->user_id).'.html';
  81. $my_path_certificate = $this->certification_user_path.$name;
  82. $path_certificate = '/'.$name;
  83. // Getting QR filename
  84. $file_info = pathinfo($path_certificate);
  85. $content = $this->generateCustomCertificate();
  86. $my_new_content_html = str_replace(
  87. '((certificate_barcode))',
  88. Display::img(
  89. $this->certification_web_user_path.$file_info['filename'].'_qr.png',
  90. 'QR'
  91. ),
  92. $content
  93. );
  94. $my_new_content_html = mb_convert_encoding(
  95. $my_new_content_html,
  96. 'UTF-8',
  97. api_get_system_encoding()
  98. );
  99. $this->html_file = $my_path_certificate;
  100. $result = @file_put_contents($my_path_certificate, $my_new_content_html);
  101. if ($result) {
  102. // Updating the path
  103. self::updateUserCertificateInfo(
  104. 0,
  105. $this->user_id,
  106. $path_certificate
  107. );
  108. $this->certificate_data['path_certificate'] = $path_certificate;
  109. if ($this->isHtmlFileGenerated()) {
  110. if (!empty($file_info)) {
  111. //$text = $this->parse_certificate_variables($new_content_html['variables']);
  112. //$this->generate_qr($text, $qr_code_filename);
  113. }
  114. }
  115. }
  116. return $result;
  117. }
  118. }
  119. /**
  120. * Checks if the certificate user path directory is created
  121. */
  122. public function check_certificate_path()
  123. {
  124. $this->certification_user_path = null;
  125. // Setting certification path
  126. $path_info = UserManager::getUserPathById($this->user_id, 'system');
  127. $web_path_info = UserManager::getUserPathById($this->user_id, 'web');
  128. if (!empty($path_info) && isset($path_info)) {
  129. $this->certification_user_path = $path_info.'certificate/';
  130. $this->certification_web_user_path = $web_path_info.'certificate/';
  131. if (!is_dir($path_info)) {
  132. mkdir($path_info, 0777, true);
  133. }
  134. if (!is_dir($this->certification_user_path)) {
  135. mkdir($this->certification_user_path, 0777);
  136. }
  137. }
  138. }
  139. /**
  140. * Deletes the current certificate object. This is generally triggered by
  141. * the teacher from the gradebook tool to re-generate the certificate because
  142. * the original version wa flawed.
  143. * @param bool $force_delete
  144. * @return bool
  145. */
  146. public function delete($force_delete = false)
  147. {
  148. $delete_db = false;
  149. if (!empty($this->certificate_data)) {
  150. if (!is_null($this->html_file) || $this->html_file != '' || strlen($this->html_file)) {
  151. // Deleting HTML file
  152. if (is_file($this->html_file)) {
  153. @unlink($this->html_file);
  154. if (is_file($this->html_file) === false) {
  155. $delete_db = true;
  156. } else {
  157. $delete_db = false;
  158. }
  159. }
  160. // Deleting QR code PNG image file
  161. if (is_file($this->qr_file)) {
  162. @unlink($this->qr_file);
  163. }
  164. if ($delete_db || $force_delete) {
  165. return parent::delete($this->certificate_data['id']);
  166. }
  167. } else {
  168. return parent::delete($this->certificate_data['id']);
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * Generates an HTML Certificate and fills the path_certificate field in the DB
  175. *
  176. * @param array $params
  177. * @param bool $sendNotification
  178. * @return bool|int
  179. */
  180. public function generate($params = [], $sendNotification = false)
  181. {
  182. // The user directory should be set
  183. if (empty($this->certification_user_path) &&
  184. $this->force_certificate_generation == false
  185. ) {
  186. return false;
  187. }
  188. $params['hide_print_button'] = isset($params['hide_print_button']) ? true : false;
  189. if (isset($this->certificate_data) && isset($this->certificate_data['cat_id'])) {
  190. $my_category = Category::load($this->certificate_data['cat_id']);
  191. }
  192. if (isset($my_category[0]) &&
  193. $my_category[0]->is_certificate_available($this->user_id)
  194. ) {
  195. $courseInfo = api_get_course_info($my_category[0]->get_course_code());
  196. $courseId = $courseInfo['real_id'];
  197. $sessionId = $my_category[0]->get_session_id();
  198. $skill = new Skill();
  199. $skill->addSkillToUser(
  200. $this->user_id,
  201. $this->certificate_data['cat_id'],
  202. $courseId,
  203. $sessionId
  204. );
  205. if (is_dir($this->certification_user_path)) {
  206. if (!empty($this->certificate_data)) {
  207. $new_content_html = GradebookUtils::get_user_certificate_content(
  208. $this->user_id,
  209. $my_category[0]->get_course_code(),
  210. $my_category[0]->get_session_id(),
  211. false,
  212. $params['hide_print_button']
  213. );
  214. if ($my_category[0]->get_id() == strval(intval($this->certificate_data['cat_id']))) {
  215. $name = $this->certificate_data['path_certificate'];
  216. $myPathCertificate = $this->certification_user_path.basename($name);
  217. if (file_exists($myPathCertificate) &&
  218. !empty($name) &&
  219. !is_dir($myPathCertificate) &&
  220. $this->force_certificate_generation == false
  221. ) {
  222. //Seems that the file was already generated
  223. return true;
  224. } else {
  225. // Creating new name
  226. $name = md5($this->user_id.$this->certificate_data['cat_id']).'.html';
  227. $myPathCertificate = $this->certification_user_path.$name;
  228. $path_certificate = '/'.$name;
  229. // Getting QR filename
  230. $file_info = pathinfo($path_certificate);
  231. $qr_code_filename = $this->certification_user_path.$file_info['filename'].'_qr.png';
  232. $newContent = str_replace(
  233. '((certificate_barcode))',
  234. Display::img(
  235. $this->certification_web_user_path.$file_info['filename'].'_qr.png',
  236. 'QR'
  237. ),
  238. $new_content_html['content']
  239. );
  240. $newContent = api_convert_encoding(
  241. $newContent,
  242. 'UTF-8',
  243. api_get_system_encoding()
  244. );
  245. $result = @file_put_contents($myPathCertificate, $newContent);
  246. if ($result) {
  247. // Updating the path
  248. self::updateUserCertificateInfo(
  249. $this->certificate_data['cat_id'],
  250. $this->user_id,
  251. $path_certificate
  252. );
  253. $this->certificate_data['path_certificate'] = $path_certificate;
  254. if ($this->isHtmlFileGenerated()) {
  255. if (!empty($file_info)) {
  256. $text = $this->parseCertificateVariables(
  257. $new_content_html['variables']
  258. );
  259. $this->generateQRImage(
  260. $text,
  261. $qr_code_filename
  262. );
  263. if ($sendNotification) {
  264. $subject = get_lang('NotificationCertificateSubject');
  265. $message = nl2br(get_lang('NotificationCertificateTemplate'));
  266. $score = $this->certificate_data['score_certificate'];
  267. Certificate::sendNotification(
  268. $subject,
  269. $message,
  270. api_get_user_info($this->user_id),
  271. $courseInfo,
  272. [
  273. 'score_certificate' => $score
  274. ]
  275. );
  276. }
  277. }
  278. }
  279. }
  280. return $result;
  281. }
  282. }
  283. }
  284. }
  285. } else {
  286. // General certificate
  287. $name = md5($this->user_id).'.html';
  288. $my_path_certificate = $this->certification_user_path.$name;
  289. $path_certificate = '/'.$name;
  290. // Getting QR filename
  291. $file_info = pathinfo($path_certificate);
  292. $content = $this->generateCustomCertificate();
  293. $my_new_content_html = str_replace(
  294. '((certificate_barcode))',
  295. Display::img(
  296. +$this->certification_web_user_path.$file_info['filename'].'_qr.png',
  297. 'QR'
  298. ),
  299. $content
  300. );
  301. $my_new_content_html = mb_convert_encoding(
  302. $my_new_content_html,
  303. 'UTF-8',
  304. api_get_system_encoding()
  305. );
  306. $result = @file_put_contents($my_path_certificate, $my_new_content_html);
  307. if ($result) {
  308. // Updating the path
  309. self::updateUserCertificateInfo(
  310. 0,
  311. $this->user_id,
  312. $path_certificate
  313. );
  314. $this->certificate_data['path_certificate'] = $path_certificate;
  315. if ($this->isHtmlFileGenerated()) {
  316. if (!empty($file_info)) {
  317. //$text = $this->parse_certificate_variables($new_content_html['variables']);
  318. //$this->generate_qr($text, $qr_code_filename);
  319. }
  320. }
  321. }
  322. return $result;
  323. }
  324. return false;
  325. }
  326. /**
  327. * @return array
  328. */
  329. public static function notificationTags()
  330. {
  331. $tags = [
  332. '((course_title))',
  333. '((user_first_name))',
  334. '((user_last_name))',
  335. '((author_first_name))',
  336. '((author_last_name))',
  337. '((score))',
  338. '((portal_name))'
  339. ];
  340. return $tags;
  341. }
  342. /**
  343. * @param string $subject
  344. * @param string $message
  345. * @param array $userInfo
  346. * @param array $courseInfo
  347. * @param array $certificateInfo
  348. *
  349. * @return bool
  350. */
  351. public static function sendNotification(
  352. $subject,
  353. $message,
  354. $userInfo,
  355. $courseInfo,
  356. $certificateInfo
  357. ) {
  358. if (empty($userInfo) || empty($courseInfo)) {
  359. return false;
  360. }
  361. $currentUserInfo = api_get_user_info();
  362. $replace = [
  363. $courseInfo['title'],
  364. $userInfo['firstname'],
  365. $userInfo['lastname'],
  366. $currentUserInfo['firstname'],
  367. $currentUserInfo['lastname'],
  368. $certificateInfo['score_certificate'],
  369. api_get_setting('Institution')
  370. ];
  371. $message = str_replace(self::notificationTags(), $replace, $message);
  372. MessageManager::send_message(
  373. $userInfo['id'],
  374. $subject,
  375. $message,
  376. [],
  377. [],
  378. 0,
  379. 0,
  380. 0,
  381. 0,
  382. $currentUserInfo['id']
  383. );
  384. $plugin = new AppPlugin();
  385. $smsPlugin = $plugin->getSMSPluginLibrary();
  386. if ($smsPlugin) {
  387. $additionalParameters = array(
  388. 'smsType' => SmsPlugin::CERTIFICATE_NOTIFICATION,
  389. 'userId' => $userInfo['id'],
  390. 'direct_message' => $message
  391. );
  392. $smsPlugin->send($additionalParameters);
  393. }
  394. }
  395. /**
  396. * Update user info about certificate
  397. * @param int $cat_id category id
  398. * @param int $user_id user id
  399. * @param string $path_certificate the path name of the certificate
  400. *
  401. */
  402. public function updateUserCertificateInfo(
  403. $cat_id,
  404. $user_id,
  405. $path_certificate
  406. ) {
  407. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  408. $now = api_get_utc_datetime();
  409. if (!UserManager::is_user_certified($cat_id, $user_id)) {
  410. $sql = 'UPDATE '.$table.' SET
  411. path_certificate="'.Database::escape_string($path_certificate).'",
  412. created_at = "'.$now.'"
  413. WHERE cat_id="'.intval($cat_id).'" AND user_id="'.intval($user_id).'" ';
  414. Database::query($sql);
  415. }
  416. }
  417. /**
  418. * Check if the file was generated
  419. *
  420. * @return boolean
  421. */
  422. public function isHtmlFileGenerated()
  423. {
  424. if (empty($this->certification_user_path)) {
  425. return false;
  426. }
  427. if (!empty($this->certificate_data) &&
  428. isset($this->certificate_data['path_certificate']) &&
  429. !empty($this->certificate_data['path_certificate'])
  430. ) {
  431. return true;
  432. }
  433. return false;
  434. }
  435. /**
  436. * Generates a QR code for the certificate. The QR code embeds the text given
  437. * @param string $text Text to be added in the QR code
  438. * @param string $path file path of the image
  439. * @return bool
  440. **/
  441. public function generateQRImage($text, $path)
  442. {
  443. //Make sure HTML certificate is generated
  444. if (!empty($text) && !empty($path)) {
  445. //L low, M - Medium, L large error correction
  446. return PHPQRCode\QRcode::png($text, $path, 'M', 2, 2);
  447. }
  448. return false;
  449. }
  450. /**
  451. * Transforms certificate tags into text values. This function is very static
  452. * (it doesn't allow for much flexibility in terms of what tags are printed).
  453. * @param array $array Contains two array entries: first are the headers,
  454. * second is an array of contents
  455. * @return string The translated string
  456. */
  457. public function parseCertificateVariables($array)
  458. {
  459. $headers = $array[0];
  460. $content = $array[1];
  461. $final_content = array();
  462. if (!empty($content)) {
  463. foreach ($content as $key => $value) {
  464. $my_header = str_replace(array('((', '))'), '', $headers[$key]);
  465. $final_content[$my_header] = $value;
  466. }
  467. }
  468. /* Certificate tags
  469. *
  470. 0 => string '((user_firstname))' (length=18)
  471. 1 => string '((user_lastname))' (length=17)
  472. 2 => string '((gradebook_institution))' (length=25)
  473. 3 => string '((gradebook_sitename))' (length=22)
  474. 4 => string '((teacher_firstname))' (length=21)
  475. 5 => string '((teacher_lastname))' (length=20)
  476. 6 => string '((official_code))' (length=17)
  477. 7 => string '((date_certificate))' (length=20)
  478. 8 => string '((course_code))' (length=15)
  479. 9 => string '((course_title))' (length=16)
  480. 10 => string '((gradebook_grade))' (length=19)
  481. 11 => string '((certificate_link))' (length=20)
  482. 12 => string '((certificate_link_html))' (length=25)
  483. 13 => string '((certificate_barcode))' (length=23)
  484. */
  485. $break_space = " \n\r ";
  486. $text =
  487. $final_content['gradebook_institution'].' - '.
  488. $final_content['gradebook_sitename'].' - '.
  489. get_lang('Certification').$break_space.
  490. get_lang('Student').': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space.
  491. get_lang('Teacher').': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space.
  492. get_lang('Date').': '.$final_content['date_certificate'].$break_space.
  493. get_lang('Score').': '.$final_content['gradebook_grade'].$break_space.
  494. 'URL'.': '.$final_content['certificate_link'];
  495. return $text;
  496. }
  497. /**
  498. * Check if the certificate is visible for the current user
  499. * If the global setting allow_public_certificates is set to 'false', no certificate can be printed.
  500. * If the global allow_public_certificates is set to 'true' and the course setting allow_public_certificates
  501. * is set to 0, no certificate *in this course* can be printed (for anonymous users).
  502. * Connected users can always print them.
  503. * @return bool
  504. */
  505. public function isVisible()
  506. {
  507. if (!api_is_anonymous()) {
  508. return true;
  509. }
  510. if (api_get_setting('allow_public_certificates') != 'true') {
  511. // The "non-public" setting is set, so do not print
  512. return false;
  513. }
  514. if (!isset($this->certificate_data, $this->certificate_data['cat_id'])) {
  515. return false;
  516. }
  517. $gradeBook = new Gradebook();
  518. $gradeBookInfo = $gradeBook->get($this->certificate_data['cat_id']);
  519. if (empty($gradeBookInfo['course_code'])) {
  520. return false;
  521. }
  522. if (api_get_course_setting('allow_public_certificates', $gradeBookInfo['course_code']) == 0) {
  523. // Printing not allowed
  524. return false;
  525. }
  526. return true;
  527. }
  528. /**
  529. * Check if the certificate is available
  530. * @return bool
  531. */
  532. public function isAvailable()
  533. {
  534. if (empty($this->certificate_data['path_certificate'])) {
  535. return false;
  536. }
  537. $user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  538. if (!file_exists($user_certificate)) {
  539. return false;
  540. }
  541. return true;
  542. }
  543. /**
  544. * Shows the student's certificate (HTML file)
  545. */
  546. public function show()
  547. {
  548. header('Content-Type: text/html; charset='.api_get_system_encoding());
  549. $user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  550. if (file_exists($user_certificate)) {
  551. $certificateContent = (string) file_get_contents($user_certificate);
  552. if ($this->user_id == api_get_user_id() && !empty($this->certificate_data)) {
  553. $certificateId = $this->certificate_data['id'];
  554. $extraFieldValue = new ExtraFieldValue('user_certificate');
  555. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  556. $certificateId,
  557. 'downloaded_at'
  558. );
  559. if (empty($value)) {
  560. $params = [
  561. 'item_id' => $this->certificate_data['id'],
  562. 'extra_downloaded_at' => api_get_utc_datetime(),
  563. ];
  564. $extraFieldValue->saveFieldValues($params);
  565. }
  566. }
  567. echo $certificateContent;
  568. return;
  569. }
  570. api_not_allowed(true);
  571. }
  572. /**
  573. * @return string
  574. */
  575. public function generateCustomCertificate()
  576. {
  577. $myCertificate = GradebookUtils::get_certificate_by_user_id(
  578. 0,
  579. $this->user_id
  580. );
  581. if (empty($myCertificate)) {
  582. GradebookUtils::registerUserInfoAboutCertificate(
  583. 0,
  584. $this->user_id,
  585. 100,
  586. api_get_utc_datetime()
  587. );
  588. }
  589. $userInfo = api_get_user_info($this->user_id);
  590. $extraFieldValue = new ExtraFieldValue('user');
  591. $value = $extraFieldValue->get_values_by_handler_and_field_variable($this->user_id, 'legal_accept');
  592. $termsValidationDate = '';
  593. if (isset($value) && !empty($value['value'])) {
  594. list($id, $id2, $termsValidationDate) = explode(':', $value['value']);
  595. }
  596. $sessions = SessionManager::get_sessions_by_user($this->user_id);
  597. $sessionsApproved = [];
  598. if ($sessions) {
  599. foreach ($sessions as $session) {
  600. $allCoursesApproved = [];
  601. foreach ($session['courses'] as $course) {
  602. $courseInfo = api_get_course_info_by_id($course['real_id']);
  603. $gradebookCategories = Category::load(
  604. null,
  605. null,
  606. $courseInfo['code'],
  607. null,
  608. false,
  609. $session['session_id']
  610. );
  611. if (isset($gradebookCategories[0])) {
  612. /** @var Category $category */
  613. $category = $gradebookCategories[0];
  614. // $categoryId = $category->get_id();
  615. // @todo how we check if user pass a gradebook?
  616. //$certificateInfo = GradebookUtils::get_certificate_by_user_id($categoryId, $this->user_id);
  617. $result = Category::userFinishedCourse(
  618. $this->user_id,
  619. $category,
  620. null,
  621. $courseInfo['code'],
  622. $session['session_id'],
  623. true
  624. );
  625. if ($result) {
  626. $allCoursesApproved[] = true;
  627. }
  628. }
  629. }
  630. if (count($allCoursesApproved) == count($session['courses'])) {
  631. $sessionsApproved[] = $session;
  632. }
  633. }
  634. }
  635. $skill = new Skill();
  636. $skills = $skill->getStudentSkills($this->user_id);
  637. $timeInSeconds = Tracking::get_time_spent_on_the_platform($this->user_id);
  638. $time = api_time_to_hms($timeInSeconds);
  639. $tplContent = new Template(null, false, false, false, false, false);
  640. // variables for the default template
  641. $tplContent->assign('complete_name', $userInfo['complete_name']);
  642. $tplContent->assign('time_in_platform', $time);
  643. $tplContent->assign('certificate_generated_date', api_get_local_time($myCertificate['created_at']));
  644. if (!empty($termsValidationDate)) {
  645. $termsValidationDate = api_get_local_time($termsValidationDate);
  646. }
  647. $tplContent->assign('terms_validation_date', $termsValidationDate);
  648. // Ofaj
  649. $tplContent->assign('time_in_platform_in_hours', round($timeInSeconds/3600, 1));
  650. $tplContent->assign(
  651. 'certificate_generated_date_no_time',
  652. api_get_local_time(
  653. $myCertificate['created_at'],
  654. null,
  655. null,
  656. false,
  657. false
  658. )
  659. );
  660. $tplContent->assign(
  661. 'terms_validation_date_no_time',
  662. api_get_local_time(
  663. $termsValidationDate,
  664. null,
  665. null,
  666. false,
  667. false
  668. )
  669. );
  670. $tplContent->assign('skills', $skills);
  671. $tplContent->assign('sessions', $sessionsApproved);
  672. $layoutContent = $tplContent->get_template('gradebook/custom_certificate.tpl');
  673. $content = $tplContent->fetch($layoutContent);
  674. return $content;
  675. }
  676. /**
  677. * Ofaj
  678. */
  679. public function generatePdfFromCustomCertificate()
  680. {
  681. $orientation = api_get_configuration_value('certificate_pdf_orientation');
  682. $params['orientation'] = 'landscape';
  683. if (!empty($orientation)) {
  684. $params['orientation'] = $orientation;
  685. }
  686. $params['left'] = 0;
  687. $params['right'] = 0;
  688. $params['top'] = 0;
  689. $params['bottom'] = 0;
  690. $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
  691. $pdf = new PDF($page_format, $params['orientation'], $params);
  692. $pdf->html_to_pdf(
  693. $this->html_file,
  694. get_lang('Certificates'),
  695. null,
  696. false,
  697. false
  698. );
  699. }
  700. }