certificate.lib.php 12 KB

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