certificate.lib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. var $table;
  11. var $columns = array('id','cat_id','score_certificate','created_at','path_certificate');
  12. /**
  13. * Certification data
  14. */
  15. var $certificate_data = array();
  16. /**
  17. * Student's certification path
  18. */
  19. var $certification_user_path = null;
  20. var $certification_web_user_path = null;
  21. var $html_file = null;
  22. var $qr_file = null;
  23. var $user_id;
  24. //If true every time we enter to the certificate URL we would generate a new certificate
  25. // (good thing because we can edit the certificate and all users will have the latest certificate bad because we load the certificate everytime)
  26. var $force_certificate_generation = true; //default true
  27. /**
  28. * Constructor
  29. * @param int ID of the certificate. If no ID given, take user_id and try to generate one
  30. */
  31. public function __construct($certificate_id = null) {
  32. $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  33. $this->certificate_data = null;
  34. if (isset($certificate_id)) {
  35. $this->certificate_data = $this->get($certificate_id);
  36. $this->user_id = $this->certificate_data['user_id'];
  37. } else {
  38. //Try with the current user
  39. $this->user_id = api_get_user_id();
  40. }
  41. if ($this->user_id) {
  42. //Need to be called before any operation
  43. $this->check_certificate_path();
  44. //To force certification generation
  45. if ($this->force_certificate_generation) {
  46. $this->generate();
  47. }
  48. if (isset($this->certificate_data) && $this->certificate_data) {
  49. if (empty($this->certificate_data['path_certificate'])) {
  50. $this->generate();
  51. }
  52. }
  53. }
  54. //Setting the qr and html variables
  55. if (isset($certificate_id) && !empty($this->certification_user_path)) {
  56. $pathinfo = pathinfo($this->certificate_data['path_certificate']);
  57. $this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
  58. $this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png';
  59. }
  60. }
  61. /**
  62. * Checks if the certificate user path directory is created
  63. */
  64. public function check_certificate_path() {
  65. $this->certification_user_path = null;
  66. //Setting certification path
  67. $path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'system', true);
  68. $web_path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'web', true);
  69. if (!empty($path_info) && isset($path_info['dir'])) {
  70. $this->certification_user_path = $path_info['dir'].'certificate/';
  71. $this->certification_web_user_path = $web_path_info['dir'].'certificate/';
  72. if (!is_dir($path_info['dir'])) {
  73. mkdir($path_info['dir'], 0777, true);
  74. }
  75. if (!is_dir($this->certification_user_path)) {
  76. mkdir($this->certification_user_path, 0777);
  77. }
  78. }
  79. }
  80. /**
  81. * Deletes the current certificate object. This is generally triggered by
  82. * the teacher from the gradebook tool to re-generate the certificate because
  83. * the original version wa flawed.
  84. */
  85. public function delete($force_delete = false) {
  86. if (!empty($this->certificate_data)) {
  87. if (!is_null($this->html_file) || $this->html_file != '' || strlen($this->html_file)) {
  88. //Deleting HTML file
  89. if (is_file($this->html_file)) {
  90. @unlink($this->html_file);
  91. if (is_file($this->html_file) === false) {
  92. $delete_db = true;
  93. } else {
  94. $delete_db = false;
  95. }
  96. }
  97. //Deleting QR code PNG image file
  98. if (is_file($this->qr_file)) {
  99. @unlink($this->qr_file);
  100. }
  101. if ($delete_db || $force_delete) {
  102. return parent::delete($this->certificate_data['id']);
  103. }
  104. } else {
  105. return parent::delete($this->certificate_data['id']);
  106. }
  107. }
  108. return false;
  109. }
  110. /**
  111. * Generates an HTML Certificate and fills the path_certificate field in the DB
  112. * */
  113. public function generate($params = array()) {
  114. //The user directory should be set
  115. if (empty($this->certification_user_path) && $this->force_certificate_generation == false) {
  116. return false;
  117. }
  118. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  119. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  120. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
  121. $params['hide_print_button'] = isset($params['hide_print_button']) ? true : false;
  122. $my_category = Category :: load($this->certificate_data['cat_id']);
  123. if (isset($my_category[0]) && $my_category[0]->is_certificate_available($this->user_id)) {
  124. $user = api_get_user_info($this->user_id);
  125. $scoredisplay = ScoreDisplay :: instance();
  126. $scorecourse = $my_category[0]->calc_score($this->user_id);
  127. $scorecourse_display = (isset($scorecourse) ? $scoredisplay->display_score($scorecourse,SCORE_AVERAGE) : get_lang('NoResultsAvailable'));
  128. //Prepare all necessary variables:
  129. $organization_name = api_get_setting('Institution');
  130. //$portal_name = api_get_setting('siteName');
  131. $stud_fn = $user['firstname'];
  132. $stud_ln = $user['lastname'];
  133. //@todo this code is not needed
  134. $certif_text = sprintf(get_lang('CertificateWCertifiesStudentXFinishedCourseYWithGradeZ'), $organization_name, $stud_fn.' '.$stud_ln, $my_category[0]->get_name(), $scorecourse_display);
  135. $certif_text = str_replace("\\n","\n", $certif_text);
  136. //If the gradebook is related to skills we added the skills to the user
  137. $skill = new Skill();
  138. $skill->add_skill_to_user($this->user_id, $this->certificate_data['cat_id']);
  139. if (is_dir($this->certification_user_path)) {
  140. if (!empty($this->certificate_data)) {
  141. $new_content_html = get_user_certificate_content($this->user_id, $my_category[0]->get_course_code(), false, $params['hide_print_button']);
  142. if ($my_category[0]->get_id() == strval(intval($this->certificate_data['cat_id']))) {
  143. $name = $this->certificate_data['path_certificate'];
  144. $my_path_certificate = $this->certification_user_path.basename($name);
  145. if (file_exists($my_path_certificate) && !empty($name) && !is_dir($my_path_certificate) && $this->force_certificate_generation == false) {
  146. //Seems that the file was already generated
  147. return true;
  148. } else {
  149. //Creating new name
  150. $name = md5($this->user_id.$this->certificate_data['cat_id']).'.html';
  151. $my_path_certificate = $this->certification_user_path.$name;
  152. $path_certificate ='/'.$name;
  153. //Getting QR filename
  154. $file_info = pathinfo($path_certificate);
  155. $qr_code_filename = $this->certification_user_path.$file_info['filename'].'_qr.png';
  156. $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']);
  157. $my_new_content_html = mb_convert_encoding($my_new_content_html,'UTF-8', api_get_system_encoding());
  158. $result = @file_put_contents($my_path_certificate, $my_new_content_html);
  159. if ($result) {
  160. //Updating the path
  161. self::update_user_info_about_certificate($this->certificate_data['cat_id'], $this->user_id, $path_certificate);
  162. $this->certificate_data['path_certificate'] = $path_certificate;
  163. if ($this->html_file_is_generated()) {
  164. if (!empty($file_info)) {
  165. $text = $this->parse_certificate_variables($new_content_html['variables']);
  166. $this->generate_qr($text, $qr_code_filename);
  167. }
  168. }
  169. }
  170. return $result;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. return false;
  177. }
  178. /**
  179. * update user info about certificate
  180. * @param int The category id
  181. * @param int The user id
  182. * @param string the path name of the certificate
  183. * @return void()
  184. */
  185. function update_user_info_about_certificate ($cat_id,$user_id,$path_certificate) {
  186. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  187. if (!UserManager::is_user_certified($cat_id,$user_id)) {
  188. $sql='UPDATE '.$table_certificate.' SET path_certificate="'.Database::escape_string($path_certificate).'"
  189. WHERE cat_id="'.intval($cat_id).'" AND user_id="'.intval($user_id).'" ';
  190. Database::query($sql);
  191. }
  192. }
  193. /**
  194. *
  195. * Check if the file was generated
  196. *
  197. * @return boolean
  198. */
  199. function html_file_is_generated() {
  200. if (empty($this->certification_user_path)) {
  201. return false;
  202. }
  203. if (!empty($this->certificate_data) && isset($this->certificate_data['path_certificate']) && !empty($this->certificate_data['path_certificate'])) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. /**
  209. * Generates a QR code for the certificate. The QR code embeds the text given
  210. * @param string Text to be added in the QR code
  211. * @param string file path of the image
  212. * */
  213. public function generate_qr($text, $path) {
  214. //Make sure HTML certificate is generated
  215. if (!empty($text) && !empty($path)) {
  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($returnContent = false) {
  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. if ($returnContent) {
  310. return file_get_contents($user_certificate);
  311. }
  312. header('Content-Type: text/html; charset='. api_get_system_encoding());
  313. echo @file_get_contents($user_certificate);
  314. }
  315. } else {
  316. if ($returnContent) {
  317. return Display :: return_message(get_lang('NoCertificateAvailable'), 'warning');
  318. }
  319. Display :: display_reduced_header();
  320. Display :: display_warning_message(get_lang('NoCertificateAvailable'));
  321. }
  322. exit;
  323. }
  324. static function getCertificatePublicURL($id) {
  325. return api_get_path(WEB_PUBLIC_PATH).'certificates/'.$id;
  326. }
  327. }