certificate.lib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * @package chamilo.library
  4. */
  5. class Certificate extends Model {
  6. var $table;
  7. var $columns = array('id','cat_id','score_certificate','created_at','path_certificate');
  8. /**
  9. * Certification data
  10. * */
  11. var $certificate_data = array();
  12. /**
  13. * Student's certification path
  14. * */
  15. var $certification_user_path = null;
  16. var $certification_web_user_path = null;
  17. var $html_file = null;
  18. var $qr_file = null;
  19. var $user_id;
  20. var $force_certificate_generation = false; //If true every time we enter to the certificate URL we would generate a new certificate
  21. public function __construct($certificate_id = null) {
  22. $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  23. $this->certificate_data = null;
  24. if (isset($certificate_id)) {
  25. $this->certificate_data = $this->get($certificate_id);
  26. $this->user_id = $this->certificate_data['user_id'];
  27. } else {
  28. //Try with the current user
  29. $this->user_id = api_get_user_id();
  30. }
  31. if ($this->user_id) {
  32. //Need to be called before any operation
  33. $this->check_certificate_path();
  34. //To force certification generation
  35. if ($this->force_certificate_generation) {
  36. $this->generate();
  37. }
  38. if (isset($this->certificate_data) && $this->certificate_data) {
  39. if (empty($this->certificate_data['path_certificate'])) {
  40. $this->generate();
  41. }
  42. }
  43. }
  44. //Setting the qr and html variables
  45. if (isset($certificate_id) && !empty($this->certification_user_path)) {
  46. $pathinfo = pathinfo($this->certificate_data['path_certificate']);
  47. $this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  48. $this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png';
  49. }
  50. }
  51. /**
  52. * Checks if the certificate user path directory is created
  53. */
  54. public function check_certificate_path() {
  55. $this->certification_user_path = null;
  56. //Setting certification path
  57. $path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'system', true);
  58. $web_path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'web', true);
  59. if (!empty($path_info) && isset($path_info['dir'])) {
  60. $this->certification_user_path = $path_info['dir'].'certificate/';
  61. $this->certification_web_user_path = $web_path_info['dir'].'certificate/';
  62. if (!is_dir($path_info['dir'])) {
  63. mkdir($path_info['dir'],0777);
  64. }
  65. if (!is_dir($this->certification_user_path)) {
  66. mkdir($this->certification_user_path, 0777);
  67. }
  68. }
  69. }
  70. public function delete() {
  71. if (!empty($this->certificate_data)) {
  72. if (!is_null($this->html_file) || $this->html_file!='' || strlen($this->html_file)) {
  73. //Deleting HTML file
  74. if (is_file($this->html_file)) {
  75. @unlink($this->html_file);
  76. if (is_file($this->html_file) === false) {
  77. $delete_db = true;
  78. } else {
  79. $delete_db = false;
  80. }
  81. }
  82. //Deleting QR code PNG image file
  83. if (is_file($this->qr_file)) {
  84. @unlink($this->qr_file);
  85. }
  86. if ($delete_db) {
  87. return parent::delete($this->certificate_data['id']);
  88. }
  89. } else {
  90. return parent::delete($this->certificate_data['id']);
  91. }
  92. }
  93. return false;
  94. }
  95. /**
  96. * Generates an HTML Certificate and fills the path_certificate field in the DB
  97. * */
  98. public function generate() {
  99. //The user directory should be set
  100. if (empty($this->certification_user_path) && $this->force_certificate_generation == false) {
  101. return false;
  102. }
  103. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  104. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  105. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
  106. $my_category = Category :: load($this->certificate_data['cat_id']);
  107. if (isset($my_category[0]) && $my_category[0]->is_certificate_available($this->user_id)) {
  108. $user = api_get_user_info($this->user_id);
  109. $scoredisplay = ScoreDisplay :: instance();
  110. $scorecourse = $my_category[0]->calc_score($this->user_id);
  111. $scorecourse_display = (isset($scorecourse) ? $scoredisplay->display_score($scorecourse,SCORE_AVERAGE) : get_lang('NoResultsAvailable'));
  112. $cattotal = Category :: load($this->certificate_data['cat_id']);
  113. $scoretotal= $cattotal[0]->calc_score($this->user_id);
  114. $scoretotal_display = (isset($scoretotal) ? $scoredisplay->display_score($scoretotal,SCORE_PERCENT) : get_lang('NoResultsAvailable'));
  115. //Prepare all necessary variables:
  116. $organization_name = api_get_setting('Institution');
  117. $portal_name = api_get_setting('siteName');
  118. $stud_fn = $user['firstname'];
  119. $stud_ln = $user['lastname'];
  120. //@todo this code is not needed
  121. $certif_text = sprintf(get_lang('CertificateWCertifiesStudentXFinishedCourseYWithGradeZ'), $organization_name, $stud_fn.' '.$stud_ln, $my_category[0]->get_name(), $scorecourse_display);
  122. $certif_text = str_replace("\\n","\n", $certif_text);
  123. $date = date('d/m/Y', time());
  124. if (is_dir($this->certification_user_path)) {
  125. if (!empty($this->certificate_data)) {
  126. $new_content_html = get_user_certificate_content($this->user_id, $my_category[0]->get_course_code(), false);
  127. if ($cat_id = strval(intval($this->certificate_data['cat_id']))) {
  128. $name = $this->certificate_data['path_certificate'];
  129. $my_path_certificate = $this->certification_user_path.basename($name);
  130. if (file_exists($my_path_certificate) && !empty($name) && !is_dir($my_path_certificate) && $this->force_certificate_generation == false) {
  131. //Seems that the file was already generated
  132. return true;
  133. } else {
  134. //Creating new name
  135. $name = md5($this->user_id.$this->certificate_data['cat_id']).'.html';
  136. $my_path_certificate = $this->certification_user_path.$name;
  137. $path_certificate ='/'.$name;
  138. //Getting QR filename
  139. $file_info = pathinfo($path_certificate);
  140. $qr_code_filename = $this->certification_user_path.$file_info['filename'].'_qr.png';
  141. $new_content_html['content'] = str_replace('((certificate_barcode))', Display::img($this->certification_web_user_path.$file_info['filename'].'_qr.png', 'QR'), $new_content_html['content']);
  142. $my_new_content_html = $new_content_html['content'];
  143. $my_new_content_html = mb_convert_encoding($my_new_content_html,'UTF-8', api_get_system_encoding());
  144. $result = @file_put_contents($my_path_certificate, $my_new_content_html);
  145. if ($result) {
  146. //@todo move function in this class
  147. update_user_info_about_certificate($this->certificate_data['cat_id'], $this->user_id, $path_certificate);
  148. $this->certificate_data['path_certificate'] = $path_certificate;
  149. if ($this->html_file_is_generated()) {
  150. if (!empty($file_info)) {
  151. $text = $this->parse_certificate_variables($new_content_html['variables']);
  152. $this->generate_qr($text, $qr_code_filename);
  153. }
  154. }
  155. }
  156. return $result;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. return false;
  163. }
  164. /**
  165. *
  166. * Check if the file was generated
  167. *
  168. * @return boolean
  169. */
  170. function html_file_is_generated() {
  171. if (empty($this->certification_user_path)) {
  172. return false;
  173. }
  174. if (!empty($this->certificate_data) && isset($this->certificate_data['path_certificate']) && !empty($this->certificate_data['path_certificate'])) {
  175. return true;
  176. }
  177. return false;
  178. }
  179. /**
  180. * @param string Text to be added in the QR code
  181. * @param string file path of the image
  182. * */
  183. public function generate_qr($text, $path) {
  184. //Make sure HTML certificate is generated
  185. if (!empty($text) && !empty($path)) {
  186. require_once api_get_path(LIBRARY_PATH).'phpqrcode/qrlib.php';
  187. //L low, M - Medium, L large error correction
  188. $return = QRcode::png($text, $path, 'M', 2, 2);
  189. }
  190. }
  191. private function parse_certificate_variables($array) {
  192. $text = '';
  193. $headers = $array[0];
  194. $content = $array[1];
  195. $final_content = array();
  196. foreach($content as $key => $value) {
  197. $my_header = $headers[$key];
  198. $my_header = str_replace(array('((', '))') , '', $my_header);
  199. $final_content[$my_header] = $value;
  200. }
  201. /* Certificate tags
  202. *
  203. 0 => string '((user_firstname))' (length=18)
  204. 1 => string '((user_lastname))' (length=17)
  205. 2 => string '((gradebook_institution))' (length=25)
  206. 3 => string '((gradebook_sitename))' (length=22)
  207. 4 => string '((teacher_firstname))' (length=21)
  208. 5 => string '((teacher_lastname))' (length=20)
  209. 6 => string '((official_code))' (length=17)
  210. 7 => string '((date_certificate))' (length=20)
  211. 8 => string '((course_code))' (length=15)
  212. 9 => string '((course_title))' (length=16)
  213. 10 => string '((gradebook_grade))' (length=19)
  214. 11 => string '((certificate_link))' (length=20)
  215. 12 => string '((certificate_link_html))' (length=25)
  216. 13 => string '((certificate_barcode))' (length=23)
  217. */
  218. $break_space = " \n\r ";
  219. $text = $final_content['gradebook_institution'].' - '.$final_content['gradebook_sitename'].' - '.get_lang('Certification').$break_space.
  220. get_lang('Student'). ': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space.
  221. get_lang('Teacher'). ': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space.
  222. get_lang('Date'). ': '.$final_content['date_certificate'].$break_space.
  223. get_lang('Score'). ': '.$final_content['gradebook_grade'].$break_space.
  224. 'URL'. ': '.$final_content['certificate_link'];
  225. return $text;
  226. }
  227. /**
  228. * Shows the student's certificate (HTML file)
  229. */
  230. public function show() {
  231. //Read file or preview file
  232. if (!empty($this->certificate_data['path_certificate'])) {
  233. $user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  234. if (file_exists($user_certificate)) {
  235. header('Content-Type: text/html; charset='. api_get_system_encoding());
  236. echo @file_get_contents($user_certificate);
  237. }
  238. } else {
  239. Display :: display_reduced_header();
  240. Display :: display_warning_message(get_lang('NoCertificateAvailable'));
  241. }
  242. exit;
  243. }
  244. }