certificate.lib.php 9.2 KB

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