123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- <?php
- class Certificate extends Model
- {
- public $table;
- public $columns = array('id','cat_id','score_certificate','created_at','path_certificate');
-
- public $certificate_data = array();
-
- public $certification_user_path = null;
- public $certification_web_user_path = null;
- public $html_file = null;
- public $qr_file = null;
- public $sys_qr_file = null;
- public $user_id;
-
-
- public $force_certificate_generation = true;
-
- public function __construct($certificate_id = null)
- {
- $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
- $this->certificate_data = null;
- if (isset($certificate_id)) {
- $this->certificate_data = $this->get($certificate_id);
- $this->user_id = $this->certificate_data['user_id'];
- } else {
-
- $this->user_id = api_get_user_id();
- }
- if ($this->user_id) {
-
- $this->check_certificate_path();
-
- if ($this->force_certificate_generation) {
- $this->generate();
- }
- if (isset($this->certificate_data) && $this->certificate_data) {
- if (empty($this->certificate_data['path_certificate'])) {
- $this->generate();
- }
- }
- }
-
- if (isset($certificate_id) && !empty($this->certification_user_path)) {
- $pathinfo = pathinfo($this->certificate_data['path_certificate']);
- $this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
- $this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png';
-
- }
- }
-
- public function check_certificate_path()
- {
- $this->certification_user_path = null;
-
- $path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'system');
- $web_path_info = UserManager::get_user_picture_path_by_id($this->user_id, 'web');
- if (!empty($path_info) && isset($path_info['dir'])) {
- $this->certification_user_path = $path_info['dir'].'certificate/';
- $this->certification_web_user_path = $web_path_info['dir'].'certificate/';
- if (!is_dir($path_info['dir'])) {
- mkdir($path_info['dir'], 0777, true);
- }
- if (!is_dir($this->certification_user_path)) {
- mkdir($this->certification_user_path, 0777);
- }
- }
- }
-
- public function delete($force_delete = false)
- {
- if (!empty($this->certificate_data)) {
- if (!is_null($this->html_file) || $this->html_file != '' || strlen($this->html_file)) {
-
- if (is_file($this->html_file)) {
- @unlink($this->html_file);
- if (is_file($this->html_file) === false) {
- $delete_db = true;
- } else {
- $delete_db = false;
- }
- }
-
- if (is_file($this->qr_file)) {
- @unlink($this->qr_file);
- }
- if ($delete_db || $force_delete) {
- return parent::delete($this->certificate_data['id']);
- }
- } else {
- return parent::delete($this->certificate_data['id']);
- }
- }
- return false;
- }
-
- public function generate($params = array())
- {
-
- if (empty($this->certification_user_path) && $this->force_certificate_generation == false) {
- return false;
- }
- require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
- require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
- require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
- $params['hide_print_button'] = isset($params['hide_print_button']) ? true : false;
- $my_category = Category :: load($this->certificate_data['cat_id']);
- if (isset($my_category[0]) && $my_category[0]->is_certificate_available($this->user_id)) {
- $user = api_get_user_info($this->user_id);
- $scoredisplay = ScoreDisplay :: instance();
- $scorecourse = $my_category[0]->calc_score($this->user_id);
- $scorecourse_display = (isset($scorecourse) ? $scoredisplay->display_score($scorecourse,SCORE_AVERAGE) : get_lang('NoResultsAvailable'));
-
- $organization_name = api_get_setting('platform.institution');
-
- $stud_fn = $user['firstname'];
- $stud_ln = $user['lastname'];
-
- $certif_text = sprintf(get_lang('CertificateWCertifiesStudentXFinishedCourseYWithGradeZ'), $organization_name, $stud_fn.' '.$stud_ln, $my_category[0]->get_name(), $scorecourse_display);
- $certif_text = str_replace("\\n","\n", $certif_text);
-
- $skill = new Skill();
- $skill->add_skill_to_user($this->user_id, $this->certificate_data['cat_id']);
- if (is_dir($this->certification_user_path)) {
- if (!empty($this->certificate_data)) {
- $new_content_html = get_user_certificate_content($this->user_id, $my_category[0]->get_course_code(), false, $params['hide_print_button']);
- if ($my_category[0]->get_id() == strval(intval($this->certificate_data['cat_id']))) {
- $name = $this->certificate_data['path_certificate'];
- $my_path_certificate = $this->certification_user_path.basename($name);
- if (file_exists($my_path_certificate) && !empty($name) && !is_dir($my_path_certificate) && $this->force_certificate_generation == false) {
-
- return true;
- } else {
-
- $name = md5($this->user_id.$this->certificate_data['cat_id']).'.html';
- $my_path_certificate = $this->certification_user_path.$name;
- $path_certificate ='/'.$name;
-
- $file_info = pathinfo($path_certificate);
- $qr_code_filename = $this->certification_user_path.$file_info['filename'].'_qr.png';
- $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']);
- $my_new_content_html = mb_convert_encoding($my_new_content_html,'UTF-8', api_get_system_encoding());
- $result = @file_put_contents($my_path_certificate, $my_new_content_html);
- if ($result) {
-
- self::update_user_info_about_certificate($this->certificate_data['cat_id'], $this->user_id, $path_certificate);
- $this->certificate_data['path_certificate'] = $path_certificate;
- if ($this->html_file_is_generated()) {
- if (!empty($file_info)) {
- $text = $this->parse_certificate_variables($new_content_html['variables']);
- $this->generate_qr($text, $qr_code_filename);
- }
- }
- }
- return $result;
- }
- }
- }
- }
- }
- return false;
- }
-
- public function update_user_info_about_certificate ($cat_id,$user_id,$path_certificate)
- {
- $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
- if (!UserManager::is_user_certified($cat_id,$user_id)) {
- $sql='UPDATE '.$table_certificate.' SET path_certificate="'.Database::escape_string($path_certificate).'"
- WHERE cat_id="'.intval($cat_id).'" AND user_id="'.intval($user_id).'" ';
- Database::query($sql);
- }
- }
-
- public function html_file_is_generated()
- {
- if (empty($this->certification_user_path)) {
- return false;
- }
- if (!empty($this->certificate_data) &&
- isset($this->certificate_data['path_certificate']) &&
- !empty($this->certificate_data['path_certificate'])
- ) {
- return true;
- }
- return false;
- }
-
- public function generate_qr($text, $path)
- {
-
- if (!empty($text) && !empty($path)) {
-
- return QRcode::png($text, $path, 'M', 2, 2);
- }
- return false;
- }
-
- public function parse_certificate_variables($array)
- {
- $text = '';
- $headers = $array[0];
- $content = $array[1];
- $final_content = array();
- if (!empty($content)) {
- foreach($content as $key => $value) {
- $my_header = str_replace(array('((', '))') , '', $headers[$key]);
- $final_content[$my_header] = $value;
- }
- }
-
- $break_space = " \n\r ";
- $text = $final_content['gradebook_institution'].' - '.$final_content['gradebook_sitename'].' - '.get_lang('Certification').$break_space.
- get_lang('Student'). ': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space.
- get_lang('Teacher'). ': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space.
- get_lang('Date'). ': '.$final_content['date_certificate'].$break_space.
- get_lang('Score'). ': '.$final_content['gradebook_grade'].$break_space.
- 'URL'. ': '.$final_content['certificate_link'];
- return $text;
- }
-
- public function show()
- {
-
- $failed = false;
- if (api_is_anonymous()) {
- if (api_get_setting('allow_public_certificates') != 'true') {
-
- $failed = true;
- } else {
-
-
- if (isset($this->certificate_data) && isset($this->certificate_data['cat_id'])) {
- $gradebook = new Gradebook();
- $gradebook_info = $gradebook->get($this->certificate_data['cat_id']);
- if (!empty($gradebook_info['course_code'])) {
- $allow_public_certificates = api_get_course_setting('allow_public_certificates', $gradebook_info['course_code']);
- if ($allow_public_certificates == 0) {
-
- $failed = true;
- }
- } else {
-
- Display :: display_reduced_header();
- Display :: display_warning_message(get_lang('NoCertificateAvailable'));
- exit;
- }
- }
- }
- }
- if ($failed) {
- Display :: display_reduced_header();
- Display :: display_warning_message(get_lang('CertificateExistsButNotPublic'));
- exit;
- }
-
- if (!empty($this->certificate_data['path_certificate'])) {
- $user_certificate = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
- if (file_exists($user_certificate)) {
- if ($returnContent) {
- return file_get_contents($user_certificate);
- }
- header('Content-Type: text/html; charset='. api_get_system_encoding());
- echo @file_get_contents($user_certificate);
- }
- } else {
- if ($returnContent) {
- return Display :: return_message(get_lang('NoCertificateAvailable'), 'warning');
- }
- Display :: display_reduced_header();
- Display :: display_warning_message(get_lang('NoCertificateAvailable'));
- }
- exit;
- }
- static function getCertificatePublicURL($id)
- {
- return api_get_path(WEB_PUBLIC_PATH).'certificates/'.$id;
- }
- }
|