certificate.lib.php 16 KB

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