certificate.lib.php 32 KB

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