certificate.lib.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 = array();
  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 everytime*/
  34. public $force_certificate_generation = true;
  35. /**
  36. * Constructor
  37. * @param int $certificate_id ID of the certificate.
  38. * @param int $userId
  39. *
  40. * If no ID given, take user_id and try to generate one
  41. */
  42. public function __construct($certificate_id = 0, $userId = 0)
  43. {
  44. $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  45. $this->user_id = !empty($userId) ? $userId : api_get_user_id();
  46. if (!empty($certificate_id)) {
  47. $certificate = $this->get($certificate_id);
  48. if (!empty($certificate) && is_array($certificate)) {
  49. $this->certificate_data = $certificate;
  50. $this->user_id = $this->certificate_data['user_id'];
  51. }
  52. }
  53. if ($this->user_id) {
  54. // Need to be called before any operation
  55. $this->check_certificate_path();
  56. // To force certification generation
  57. if ($this->force_certificate_generation) {
  58. $this->generate();
  59. }
  60. if (isset($this->certificate_data) && $this->certificate_data) {
  61. if (empty($this->certificate_data['path_certificate'])) {
  62. $this->generate();
  63. }
  64. }
  65. }
  66. // Setting the qr and html variables
  67. if (isset($certificate_id) &&
  68. !empty($this->certification_user_path) &&
  69. isset($this->certificate_data['path_certificate'])
  70. ) {
  71. $pathinfo = pathinfo($this->certificate_data['path_certificate']);
  72. $this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  73. $this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png';
  74. }
  75. }
  76. /**
  77. * Checks if the certificate user path directory is created
  78. */
  79. public function check_certificate_path()
  80. {
  81. $this->certification_user_path = null;
  82. //Setting certification path
  83. $path_info = UserManager::getUserPathById($this->user_id, 'system');
  84. $web_path_info = UserManager::getUserPathById($this->user_id, 'web');
  85. if (!empty($path_info) && isset($path_info)) {
  86. $this->certification_user_path = $path_info.'certificate/';
  87. $this->certification_web_user_path = $web_path_info.'certificate/';
  88. if (!is_dir($path_info)) {
  89. mkdir($path_info, 0777, true);
  90. }
  91. if (!is_dir($this->certification_user_path)) {
  92. mkdir($this->certification_user_path, 0777);
  93. }
  94. }
  95. }
  96. /**
  97. * Deletes the current certificate object. This is generally triggered by
  98. * the teacher from the gradebook tool to re-generate the certificate because
  99. * the original version wa flawed.
  100. * @param bool $force_delete
  101. * @return bool
  102. */
  103. public function delete($force_delete = false)
  104. {
  105. $delete_db = false;
  106. if (!empty($this->certificate_data)) {
  107. if (!is_null($this->html_file) || $this->html_file != '' || strlen($this->html_file)) {
  108. //Deleting HTML file
  109. if (is_file($this->html_file)) {
  110. @unlink($this->html_file);
  111. if (is_file($this->html_file) === false) {
  112. $delete_db = true;
  113. } else {
  114. $delete_db = false;
  115. }
  116. }
  117. //Deleting QR code PNG image file
  118. if (is_file($this->qr_file)) {
  119. @unlink($this->qr_file);
  120. }
  121. if ($delete_db || $force_delete) {
  122. return parent::delete($this->certificate_data['id']);
  123. }
  124. } else {
  125. return parent::delete($this->certificate_data['id']);
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. * Generates an HTML Certificate and fills the path_certificate field in the DB
  132. *
  133. * @param array $params
  134. * @return bool|int
  135. */
  136. public function generate($params = array())
  137. {
  138. // The user directory should be set
  139. if (empty($this->certification_user_path) &&
  140. $this->force_certificate_generation == false
  141. ) {
  142. return false;
  143. }
  144. $params['hide_print_button'] = isset($params['hide_print_button']) ? true : false;
  145. if (isset($this->certificate_data) && isset($this->certificate_data['cat_id'])) {
  146. $my_category = Category::load($this->certificate_data['cat_id']);
  147. }
  148. if (isset($my_category[0]) &&
  149. $my_category[0]->is_certificate_available($this->user_id)
  150. ) {
  151. $courseId = api_get_course_int_id();
  152. $sessionId = api_get_session_id();
  153. $skill = new Skill();
  154. $skill->add_skill_to_user(
  155. $this->user_id,
  156. $this->certificate_data['cat_id'],
  157. $courseId,
  158. $sessionId
  159. );
  160. if (is_dir($this->certification_user_path)) {
  161. if (!empty($this->certificate_data)) {
  162. $new_content_html = GradebookUtils::get_user_certificate_content(
  163. $this->user_id,
  164. $my_category[0]->get_course_code(),
  165. $my_category[0]->get_session_id(),
  166. false,
  167. $params['hide_print_button']
  168. );
  169. if ($my_category[0]->get_id() == strval(
  170. intval($this->certificate_data['cat_id'])
  171. )) {
  172. $name = $this->certificate_data['path_certificate'];
  173. $my_path_certificate = $this->certification_user_path.basename(
  174. $name
  175. );
  176. if (file_exists($my_path_certificate) &&
  177. !empty($name) &&
  178. !is_dir($my_path_certificate) &&
  179. $this->force_certificate_generation == false
  180. ) {
  181. //Seems that the file was already generated
  182. return true;
  183. } else {
  184. // Creating new name
  185. $name = md5(
  186. $this->user_id.$this->certificate_data['cat_id']
  187. ).'.html';
  188. $my_path_certificate = $this->certification_user_path.$name;
  189. $path_certificate = '/'.$name;
  190. // Getting QR filename
  191. $file_info = pathinfo($path_certificate);
  192. $qr_code_filename = $this->certification_user_path.$file_info['filename'].'_qr.png';
  193. $my_new_content_html = str_replace(
  194. '((certificate_barcode))',
  195. Display::img(
  196. $this->certification_web_user_path.$file_info['filename'].'_qr.png',
  197. 'QR'
  198. ),
  199. $new_content_html['content']
  200. );
  201. $my_new_content_html = api_convert_encoding(
  202. $my_new_content_html,
  203. 'UTF-8',
  204. api_get_system_encoding()
  205. );
  206. $result = @file_put_contents(
  207. $my_path_certificate,
  208. $my_new_content_html
  209. );
  210. if ($result) {
  211. // Updating the path
  212. self::update_user_info_about_certificate(
  213. $this->certificate_data['cat_id'],
  214. $this->user_id,
  215. $path_certificate
  216. );
  217. $this->certificate_data['path_certificate'] = $path_certificate;
  218. if ($this->html_file_is_generated()) {
  219. if (!empty($file_info)) {
  220. $text = $this->parse_certificate_variables(
  221. $new_content_html['variables']
  222. );
  223. $this->generate_qr(
  224. $text,
  225. $qr_code_filename
  226. );
  227. }
  228. }
  229. }
  230. return $result;
  231. }
  232. }
  233. }
  234. }
  235. } else {
  236. // General certificate
  237. $name = md5($this->user_id).'.html';
  238. $my_path_certificate = $this->certification_user_path.$name;
  239. $path_certificate = '/'.$name;
  240. // Getting QR filename
  241. $file_info = pathinfo($path_certificate);
  242. $qr_code_filename = $this->certification_user_path.$file_info['filename'].'_qr.png';
  243. $content = $this->generateCustomCertificate();
  244. $my_new_content_html = str_replace(
  245. '((certificate_barcode))',
  246. Display::img(
  247. $this->certification_web_user_path.$file_info['filename'].'_qr.png',
  248. 'QR'
  249. ),
  250. $content
  251. );
  252. $my_new_content_html = mb_convert_encoding(
  253. $my_new_content_html,
  254. 'UTF-8',
  255. api_get_system_encoding()
  256. );
  257. $result = @file_put_contents($my_path_certificate, $my_new_content_html);
  258. if ($result) {
  259. // Updating the path
  260. self::update_user_info_about_certificate(
  261. 0,
  262. $this->user_id,
  263. $path_certificate
  264. );
  265. $this->certificate_data['path_certificate'] = $path_certificate;
  266. if ($this->html_file_is_generated()) {
  267. if (!empty($file_info)) {
  268. //$text = $this->parse_certificate_variables($new_content_html['variables']);
  269. //$this->generate_qr($text, $qr_code_filename);
  270. }
  271. }
  272. }
  273. return $result;
  274. }
  275. return false;
  276. }
  277. /**
  278. * update user info about certificate
  279. * @param int $cat_id category id
  280. * @param int $user_id user id
  281. * @param string $path_certificate the path name of the certificate
  282. * @return void
  283. */
  284. public function update_user_info_about_certificate(
  285. $cat_id,
  286. $user_id,
  287. $path_certificate
  288. ) {
  289. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  290. $now = api_get_utc_datetime();
  291. if (!UserManager::is_user_certified($cat_id, $user_id)) {
  292. $sql = 'UPDATE '.$table_certificate.' SET
  293. path_certificate="'.Database::escape_string($path_certificate).'",
  294. created_at = "'.$now.'"
  295. WHERE cat_id="'.intval($cat_id).'" AND user_id="'.intval($user_id).'" ';
  296. Database::query($sql);
  297. }
  298. }
  299. /**
  300. *
  301. * Check if the file was generated
  302. *
  303. * @return boolean
  304. */
  305. public function html_file_is_generated()
  306. {
  307. if (empty($this->certification_user_path)) {
  308. return false;
  309. }
  310. if (!empty($this->certificate_data) &&
  311. isset($this->certificate_data['path_certificate']) &&
  312. !empty($this->certificate_data['path_certificate'])
  313. ) {
  314. return true;
  315. }
  316. return false;
  317. }
  318. /**
  319. * Generates a QR code for the certificate. The QR code embeds the text given
  320. * @param string $text Text to be added in the QR code
  321. * @param string $path file path of the image
  322. * */
  323. public function generate_qr($text, $path)
  324. {
  325. //Make sure HTML certificate is generated
  326. if (!empty($text) && !empty($path)) {
  327. //L low, M - Medium, L large error correction
  328. return PHPQRCode\QRcode::png($text, $path, 'M', 2, 2);
  329. }
  330. return false;
  331. }
  332. /**
  333. * Transforms certificate tags into text values. This function is very static
  334. * (it doesn't allow for much flexibility in terms of what tags are printed).
  335. * @param array $array Contains two array entries: first are the headers,
  336. * second is an array of contents
  337. * @return string The translated string
  338. */
  339. public function parse_certificate_variables($array)
  340. {
  341. $headers = $array[0];
  342. $content = $array[1];
  343. $final_content = array();
  344. if (!empty($content)) {
  345. foreach ($content as $key => $value) {
  346. $my_header = str_replace(array('((', '))'), '', $headers[$key]);
  347. $final_content[$my_header] = $value;
  348. }
  349. }
  350. /* Certificate tags
  351. *
  352. 0 => string '((user_firstname))' (length=18)
  353. 1 => string '((user_lastname))' (length=17)
  354. 2 => string '((gradebook_institution))' (length=25)
  355. 3 => string '((gradebook_sitename))' (length=22)
  356. 4 => string '((teacher_firstname))' (length=21)
  357. 5 => string '((teacher_lastname))' (length=20)
  358. 6 => string '((official_code))' (length=17)
  359. 7 => string '((date_certificate))' (length=20)
  360. 8 => string '((course_code))' (length=15)
  361. 9 => string '((course_title))' (length=16)
  362. 10 => string '((gradebook_grade))' (length=19)
  363. 11 => string '((certificate_link))' (length=20)
  364. 12 => string '((certificate_link_html))' (length=25)
  365. 13 => string '((certificate_barcode))' (length=23)
  366. */
  367. $break_space = " \n\r ";
  368. $text =
  369. $final_content['gradebook_institution'].' - '.
  370. $final_content['gradebook_sitename'].' - '.
  371. get_lang('Certification').$break_space.
  372. get_lang('Student').': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space.
  373. get_lang('Teacher').': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space.
  374. get_lang('Date').': '.$final_content['date_certificate'].$break_space.
  375. get_lang('Score').': '.$final_content['gradebook_grade'].$break_space.
  376. 'URL'.': '.$final_content['certificate_link'];
  377. return $text;
  378. }
  379. /**
  380. * Check if the certificate is visible for the current user
  381. * If the global setting allow_public_certificates is set to 'false', no certificate can be printed.
  382. * If the global allow_public_certificates is set to 'true' and the course setting allow_public_certificates
  383. * is set to 0, no certificate *in this course* can be printed (for anonymous users).
  384. * Connected users can always print them.
  385. * @return bool
  386. */
  387. public function isVisible()
  388. {
  389. if (!api_is_anonymous()) {
  390. return true;
  391. }
  392. if (api_get_setting('allow_public_certificates') != 'true') {
  393. // The "non-public" setting is set, so do not print
  394. return false;
  395. }
  396. if (!isset($this->certificate_data, $this->certificate_data['cat_id'])) {
  397. return false;
  398. }
  399. $gradebook = new Gradebook();
  400. $gradebook_info = $gradebook->get($this->certificate_data['cat_id']);
  401. if (empty($gradebook_info['course_code'])) {
  402. return false;
  403. }
  404. if (api_get_course_setting('allow_public_certificates', $gradebook_info['course_code']) == 0) {
  405. // Printing not allowed
  406. return false;
  407. }
  408. return true;
  409. }
  410. /**
  411. * Check if the certificate is available
  412. * @return bool
  413. */
  414. public function isAvailable()
  415. {
  416. if (empty($this->certificate_data['path_certificate'])) {
  417. return false;
  418. }
  419. $user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  420. if (!file_exists($user_certificate)) {
  421. return false;
  422. }
  423. return true;
  424. }
  425. /**
  426. * Shows the student's certificate (HTML file)
  427. */
  428. public function show()
  429. {
  430. header('Content-Type: text/html; charset='.api_get_system_encoding());
  431. $user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  432. if (file_exists($user_certificate)) {
  433. $certificateContent = (string) file_get_contents($user_certificate);
  434. echo $certificateContent;
  435. exit;
  436. }
  437. api_not_allowed(true);
  438. }
  439. /**
  440. * @return string
  441. */
  442. public function generateCustomCertificate()
  443. {
  444. $myCertificate = GradebookUtils::get_certificate_by_user_id(
  445. 0,
  446. $this->user_id
  447. );
  448. if (empty($myCertificate)) {
  449. GradebookUtils::registerUserInfoAboutCertificate(
  450. 0,
  451. $this->user_id,
  452. 100,
  453. api_get_utc_datetime()
  454. );
  455. }
  456. $userInfo = api_get_user_info($this->user_id);
  457. $extraFieldValue = new ExtraFieldValue('user');
  458. $value = $extraFieldValue->get_values_by_handler_and_field_variable($this->user_id, 'legal_accept');
  459. list($id, $id2, $termsValidationDate) = explode(':', $value['value']);
  460. $sessions = SessionManager::get_sessions_by_user($this->user_id);
  461. $sessionsApproved = [];
  462. if ($sessions) {
  463. foreach ($sessions as $session) {
  464. $allCoursesApproved = [];
  465. foreach ($session['courses'] as $course) {
  466. $courseInfo = api_get_course_info_by_id($course['real_id']);
  467. $gradebookCategories = Category::load(null, null, $courseInfo['code'], null, false, $session['session_id']);
  468. if (isset($gradebookCategories[0])) {
  469. /** @var Category $category */
  470. $category = $gradebookCategories[0];
  471. // $categoryId = $category->get_id();
  472. // @todo how we check if user pass a gradebook?
  473. //$certificateInfo = GradebookUtils::get_certificate_by_user_id($categoryId, $this->user_id);
  474. $result = Category::userFinishedCourse(
  475. $this->user_id,
  476. $category,
  477. null,
  478. $courseInfo['code'],
  479. $session['session_id'],
  480. true
  481. );
  482. if ($result) {
  483. $allCoursesApproved[] = true;
  484. }
  485. }
  486. }
  487. if (count($allCoursesApproved) == count($session['courses'])) {
  488. $sessionsApproved[] = $session;
  489. }
  490. }
  491. }
  492. $skill = new Skill();
  493. $skills = $skill->getStudentSkills($this->user_id);
  494. $time = api_time_to_hms(Tracking::get_time_spent_on_the_platform($this->user_id));
  495. $tplContent = new Template(null, false, false, false, false, false);
  496. // variables for the default template
  497. $tplContent->assign('complete_name', $userInfo['complete_name']);
  498. $tplContent->assign('time_in_platform', $time);
  499. $tplContent->assign('certificate_generated_date', api_get_local_time($myCertificate['created_at']));
  500. $tplContent->assign('terms_validation_date', api_get_local_time($termsValidationDate));
  501. // Ofaj
  502. $tplContent->assign('time_in_platform_in_hours', round($time/3600, 1));
  503. $tplContent->assign(
  504. 'certificate_generated_date_no_time',
  505. api_get_local_time(
  506. $myCertificate['created_at'],
  507. null,
  508. null,
  509. false,
  510. false
  511. )
  512. );
  513. $tplContent->assign(
  514. 'terms_validation_date_no_time',
  515. api_get_local_time(
  516. $termsValidationDate,
  517. null,
  518. null,
  519. false,
  520. false
  521. )
  522. );
  523. $tplContent->assign('skills', $skills);
  524. $tplContent->assign('sessions', $sessionsApproved);
  525. $layoutContent = $tplContent->get_template('gradebook/custom_certificate.tpl');
  526. $content = $tplContent->fetch($layoutContent);
  527. return $content;
  528. }
  529. /**
  530. * Ofaj
  531. */
  532. public function generatePdfFromCustomCertificate()
  533. {
  534. $orientation = api_get_configuration_value('certificate_pdf_orientation');
  535. $params['orientation'] = 'landscape';
  536. if (!empty($orientation)) {
  537. $params['orientation'] = $orientation;
  538. }
  539. $params['left'] = 0;
  540. $params['right'] = 0;
  541. $params['top'] = 0;
  542. $params['bottom'] = 0;
  543. $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
  544. $pdf = new PDF($page_format, $params['orientation'], $params);
  545. $pdf->html_to_pdf(
  546. $this->html_file,
  547. get_lang('Certificates'),
  548. null,
  549. false,
  550. false
  551. );
  552. }
  553. }