certificate.lib.php 16 KB

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