certificate.lib.php 17 KB

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