certificate.lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. *
  39. * If no ID given, take user_id and try to generate one
  40. */
  41. public function __construct($certificate_id = null)
  42. {
  43. $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  44. $this->certificate_data = null;
  45. if (isset($certificate_id)) {
  46. $this->certificate_data = $this->get($certificate_id);
  47. $this->user_id = $this->certificate_data['user_id'];
  48. } else {
  49. //Try with the current user
  50. $this->user_id = api_get_user_id();
  51. }
  52. if ($this->user_id) {
  53. // Need to be called before any operation
  54. $this->check_certificate_path();
  55. // To force certification generation
  56. if ($this->force_certificate_generation) {
  57. $this->generate();
  58. }
  59. if (isset($this->certificate_data) && $this->certificate_data) {
  60. if (empty($this->certificate_data['path_certificate'])) {
  61. $this->generate();
  62. }
  63. }
  64. }
  65. //Setting the qr and html variables
  66. if (isset($certificate_id) && !empty($this->certification_user_path)) {
  67. $pathinfo = pathinfo($this->certificate_data['path_certificate']);
  68. $this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  69. $this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png';
  70. }
  71. }
  72. /**
  73. * Checks if the certificate user path directory is created
  74. */
  75. public function check_certificate_path()
  76. {
  77. $this->certification_user_path = null;
  78. //Setting certification path
  79. $path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'system');
  80. $web_path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'web');
  81. if (!empty($path_info) && isset($path_info['dir'])) {
  82. $this->certification_user_path = $path_info['dir'].'certificate/';
  83. $this->certification_web_user_path = $web_path_info['dir'].'certificate/';
  84. if (!is_dir($path_info['dir'])) {
  85. mkdir($path_info['dir'], 0777, true);
  86. }
  87. if (!is_dir($this->certification_user_path)) {
  88. mkdir($this->certification_user_path, 0777);
  89. }
  90. }
  91. }
  92. /**
  93. * Deletes the current certificate object. This is generally triggered by
  94. * the teacher from the gradebook tool to re-generate the certificate because
  95. * the original version wa flawed.
  96. * @param bool $force_delete
  97. * @return bool
  98. */
  99. public function delete($force_delete = false)
  100. {
  101. $delete_db = false;
  102. if (!empty($this->certificate_data)) {
  103. if (!is_null($this->html_file) || $this->html_file != '' || strlen($this->html_file)) {
  104. //Deleting HTML file
  105. if (is_file($this->html_file)) {
  106. @unlink($this->html_file);
  107. if (is_file($this->html_file) === false) {
  108. $delete_db = true;
  109. } else {
  110. $delete_db = false;
  111. }
  112. }
  113. //Deleting QR code PNG image file
  114. if (is_file($this->qr_file)) {
  115. @unlink($this->qr_file);
  116. }
  117. if ($delete_db || $force_delete) {
  118. return parent::delete($this->certificate_data['id']);
  119. }
  120. } else {
  121. return parent::delete($this->certificate_data['id']);
  122. }
  123. }
  124. return false;
  125. }
  126. /**
  127. * Generates an HTML Certificate and fills the path_certificate field in the DB
  128. **/
  129. public function generate($params = array())
  130. {
  131. //The user directory should be set
  132. if (empty($this->certification_user_path) && $this->force_certificate_generation == false) {
  133. return false;
  134. }
  135. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  136. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  137. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
  138. $params['hide_print_button'] = isset($params['hide_print_button']) ? true : false;
  139. $my_category = Category :: load($this->certificate_data['cat_id']);
  140. if (isset($my_category[0]) &&
  141. $my_category[0]->is_certificate_available($this->user_id)
  142. ) {
  143. $user = api_get_user_info($this->user_id);
  144. $scoredisplay = ScoreDisplay :: instance();
  145. $scorecourse = $my_category[0]->calc_score($this->user_id);
  146. $scorecourse_display = isset($scorecourse) ? $scoredisplay->display_score($scorecourse, SCORE_AVERAGE) : get_lang('NoResultsAvailable');
  147. // Prepare all necessary variables:
  148. $organization_name = api_get_setting('Institution');
  149. //$portal_name = api_get_setting('siteName');
  150. $stud_fn = $user['firstname'];
  151. $stud_ln = $user['lastname'];
  152. //@todo this code is not needed
  153. $certif_text = sprintf(
  154. get_lang('CertificateWCertifiesStudentXFinishedCourseYWithGradeZ'),
  155. $organization_name,
  156. $stud_fn.' '.$stud_ln,
  157. $my_category[0]->get_name(),
  158. $scorecourse_display
  159. );
  160. $certif_text = str_replace("\\n", "\n", $certif_text);
  161. //If the gradebook is related to skills we added the skills to the user
  162. $skill = new Skill();
  163. $skill->add_skill_to_user(
  164. $this->user_id,
  165. $this->certificate_data['cat_id']
  166. );
  167. if (is_dir($this->certification_user_path)) {
  168. if (!empty($this->certificate_data)) {
  169. $new_content_html = get_user_certificate_content(
  170. $this->user_id,
  171. $my_category[0]->get_course_code(),
  172. false,
  173. $params['hide_print_button']
  174. );
  175. if ($my_category[0]->get_id() == strval(intval($this->certificate_data['cat_id']))) {
  176. $name = $this->certificate_data['path_certificate'];
  177. $my_path_certificate = $this->certification_user_path.basename($name);
  178. if (file_exists($my_path_certificate) &&
  179. !empty($name) &&
  180. !is_dir($my_path_certificate) &&
  181. $this->force_certificate_generation == false
  182. ) {
  183. //Seems that the file was already generated
  184. return true;
  185. } else {
  186. // Creating new name
  187. $name = md5($this->user_id.$this->certificate_data['cat_id']).'.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($this->certification_web_user_path.$file_info['filename'].'_qr.png', 'QR'),
  196. $new_content_html['content']
  197. );
  198. $my_new_content_html = mb_convert_encoding($my_new_content_html, 'UTF-8', api_get_system_encoding());
  199. $result = @file_put_contents($my_path_certificate, $my_new_content_html);
  200. if ($result) {
  201. //Updating the path
  202. self::update_user_info_about_certificate(
  203. $this->certificate_data['cat_id'],
  204. $this->user_id,
  205. $path_certificate
  206. );
  207. $this->certificate_data['path_certificate'] = $path_certificate;
  208. if ($this->html_file_is_generated()) {
  209. if (!empty($file_info)) {
  210. $text = $this->parse_certificate_variables($new_content_html['variables']);
  211. $this->generate_qr($text, $qr_code_filename);
  212. }
  213. }
  214. }
  215. return $result;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. return false;
  222. }
  223. /**
  224. * update user info about certificate
  225. * @param int $cat_id category id
  226. * @param int $user_id user id
  227. * @param string $path_certificate the path name of the certificate
  228. * @return void()
  229. */
  230. public function update_user_info_about_certificate(
  231. $cat_id,
  232. $user_id,
  233. $path_certificate
  234. ) {
  235. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  236. if (!UserManager::is_user_certified($cat_id, $user_id)) {
  237. $sql='UPDATE '.$table_certificate.' SET path_certificate="'.Database::escape_string($path_certificate).'"
  238. WHERE cat_id="'.intval($cat_id).'" AND user_id="'.intval($user_id).'" ';
  239. Database::query($sql);
  240. }
  241. }
  242. /**
  243. * Check if the file was generated
  244. *
  245. * @return boolean
  246. */
  247. public function html_file_is_generated()
  248. {
  249. if (empty($this->certification_user_path)) {
  250. return false;
  251. }
  252. if (!empty($this->certificate_data) &&
  253. isset($this->certificate_data['path_certificate']) &&
  254. !empty($this->certificate_data['path_certificate'])
  255. ) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * Generates a QR code for the certificate. The QR code embeds the text given
  262. * @param string $text Text to be added in the QR code
  263. * @param string $path file path of the image
  264. * @return mixed
  265. * */
  266. public function generate_qr($text, $path)
  267. {
  268. //Make sure HTML certificate is generated
  269. if (!empty($text) && !empty($path)) {
  270. require_once api_get_path(LIBRARY_PATH).'phpqrcode/qrlib.php';
  271. //L low, M - Medium, L large error correction
  272. return QRcode::png($text, $path, 'M', 2, 2);
  273. }
  274. return false;
  275. }
  276. /**
  277. * Transforms certificate tags into text values. This function is very static
  278. * (it doesn't allow for much flexibility in terms of what tags are printed).
  279. * @param array $array Contains two array entris: first are the headers,
  280. * second is an array of contents
  281. * @return string The translated string
  282. */
  283. public function parse_certificate_variables($array)
  284. {
  285. $text = '';
  286. $headers = $array[0];
  287. $content = $array[1];
  288. $final_content = array();
  289. if (!empty($content)) {
  290. foreach ($content as $key => $value) {
  291. $my_header = str_replace(array('((', '))'), '', $headers[$key]);
  292. $final_content[$my_header] = $value;
  293. }
  294. }
  295. /* Certificate tags
  296. *
  297. 0 => string '((user_firstname))' (length=18)
  298. 1 => string '((user_lastname))' (length=17)
  299. 2 => string '((gradebook_institution))' (length=25)
  300. 3 => string '((gradebook_sitename))' (length=22)
  301. 4 => string '((teacher_firstname))' (length=21)
  302. 5 => string '((teacher_lastname))' (length=20)
  303. 6 => string '((official_code))' (length=17)
  304. 7 => string '((date_certificate))' (length=20)
  305. 8 => string '((course_code))' (length=15)
  306. 9 => string '((course_title))' (length=16)
  307. 10 => string '((gradebook_grade))' (length=19)
  308. 11 => string '((certificate_link))' (length=20)
  309. 12 => string '((certificate_link_html))' (length=25)
  310. 13 => string '((certificate_barcode))' (length=23)
  311. */
  312. $break_space = " \n\r ";
  313. $text = $final_content['gradebook_institution'].' - '.$final_content['gradebook_sitename'].' - '.get_lang('Certification').$break_space.
  314. get_lang('Student'). ': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space.
  315. get_lang('Teacher'). ': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space.
  316. get_lang('Date'). ': '.$final_content['date_certificate'].$break_space.
  317. get_lang('Score'). ': '.$final_content['gradebook_grade'].$break_space.
  318. 'URL'. ': '.$final_content['certificate_link'];
  319. return $text;
  320. }
  321. /**
  322. * Shows the student's certificate (HTML file). If the global setting
  323. * allow_public_certificates is set to 'false', no certificate can be printed.
  324. * If the global allow_public_certificates is set to 'true' and the course
  325. * setting allow_public_certificates is set to 0, no certificate *in this
  326. * course* can be printed (for anonymous users). Connected users can always
  327. * print them.
  328. */
  329. public function show()
  330. {
  331. // Special rules for anonymous users
  332. $failed = false;
  333. if (api_is_anonymous()) {
  334. if (api_get_setting('allow_public_certificates') != 'true') {
  335. // The "non-public" setting is set, so do not print
  336. $failed = true;
  337. } else {
  338. // Check the course-level setting to make sure the certificate
  339. // can be printed publicly
  340. if (isset($this->certificate_data) && isset($this->certificate_data['cat_id'])) {
  341. $gradebook = new Gradebook();
  342. $gradebook_info = $gradebook->get($this->certificate_data['cat_id']);
  343. if (!empty($gradebook_info['course_code'])) {
  344. $allow_public_certificates = api_get_course_setting('allow_public_certificates', $gradebook_info['course_code']);
  345. if ($allow_public_certificates == 0) {
  346. // Printing not allowed
  347. $failed = true;
  348. }
  349. } else {
  350. // No course ID defined (should never get here)
  351. Display :: display_reduced_header();
  352. Display :: display_warning_message(get_lang('NoCertificateAvailable'));
  353. exit;
  354. }
  355. }
  356. }
  357. }
  358. if ($failed) {
  359. Display :: display_reduced_header();
  360. Display :: display_warning_message(get_lang('CertificateExistsButNotPublic'));
  361. exit;
  362. }
  363. //Read file or preview file
  364. if (!empty($this->certificate_data['path_certificate'])) {
  365. $user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  366. if (file_exists($user_certificate)) {
  367. header('Content-Type: text/html; charset='. api_get_system_encoding());
  368. echo @file_get_contents($user_certificate);
  369. }
  370. } else {
  371. Display :: display_reduced_header();
  372. Display :: display_warning_message(get_lang('NoCertificateAvailable'));
  373. }
  374. exit;
  375. }
  376. }